class Canis::TabbedPane

Attributes

button_row[R]
current_tab[R]

index of tab that is currently open

Public Class Methods

new(form=nil, config={}) click to toggle source
Calls superclass method
# File lib/canis/core/widgets/rtabbedpane.rb, line 33
def initialize form=nil, config={}, &block
  @_events ||= []
  @_events.push(:PRESS)
  @button_gap = 2
  init_vars
  super
  @focusable = true
  @editable  = true
  @col_offset = 2
  raise ArgumentError, "NewTabbedPane : row or col not set: r: #{@row} c: #{@col} "  unless @row && @col
end

Public Instance Methods

add_tab(title, config={})
Alias for: tab
command(*args, &block) click to toggle source

a shortcut for binding a command to a press of an action button The block will be passed This is only relevant if you have asked for buttons to be created, which is only relevant in a TabbedWindow ActionEvent has source event and action_command

# File lib/canis/core/widgets/rtabbedpane.rb, line 60
def command *args, &block
  bind :PRESS, *args, &block
end
goto_component(comp) click to toggle source

set focus on given component Sometimes you have the handle to component, and you want to move focus to it

# File lib/canis/core/widgets/rtabbedpane.rb, line 323
def goto_component comp
  return if comp == @current_component
  leave_current_component
  @current_component = comp
  set_form_row
end
goto_first_item() click to toggle source

takes focus to first item (after buttons)

# File lib/canis/core/widgets/rtabbedpane.rb, line 206
def goto_first_item
  bc = @buttons.count
  @components[bc..-1].each { |c| 
    if c.focusable
      leave_current_component
      @current_component = c
      set_form_row
      break
    end
  }
end
goto_last_item() click to toggle source

takes focus to last item

# File lib/canis/core/widgets/rtabbedpane.rb, line 219
def goto_last_item
  bc = @buttons.count
  f = nil
  @components[bc..-1].each { |c| 
    if c.focusable
      f = c
    end
  }
  if f
    leave_current_component
    @current_component = f
    set_form_row
  end
end
goto_next_component() click to toggle source

take focus to the next component or item Called from DOWN, RIGHT or Tab

# File lib/canis/core/widgets/rtabbedpane.rb, line 235
def goto_next_component
  if @current_component != nil 
    leave_current_component
    if on_last_component?
      @_entered = false
      return :UNHANDLED
    end
    @current_index = @components.index(@current_component)
    index = @current_index + 1
    index.upto(@components.length-1) do |i|
      f = @components[i]
      if f.focusable
        @current_index = i
        @current_component = f
        return set_form_row
      end
    end
  end
  @_entered = false
  return :UNHANDLED
end
goto_prev_component() click to toggle source

take focus to prev component or item Called from LEFT, UP or Back Tab

# File lib/canis/core/widgets/rtabbedpane.rb, line 258
def goto_prev_component
  if @current_component != nil 
    leave_current_component
    if on_first_component?
      @_entered = false
      return :UNHANDLED
    end
    @current_index = @components.index(@current_component)
    index = @current_index -= 1
    index.downto(0) do |i|
      f = @components[i]
      if f.focusable
        @current_index = i
        @current_component = f
        return set_form_row
      end
    end
  end
  return :UNHANDLED
end
handle_key(ch) click to toggle source
# File lib/canis/core/widgets/rtabbedpane.rb, line 117
def handle_key ch
  $log.debug " NEWTABBED handle_key #{ch} "
  return if @components.empty?
  _multiplier = ($multiplier == 0 ? 1 : $multiplier )

  # should this go here 2011-10-19
  unless @_entered
    $log.warn "WARN: calling ON_ENTER since in this situation it was not called"
    on_enter
  end
  #if ch == KEY_TAB
    #$log.debug "NEWTABBED GOTO NEXT"
    #return goto_next_component
  #elsif ch == KEY_BTAB
    #return goto_prev_component
  #end
  comp = @current_component
  $log.debug " NEWTABBED handle_key #{ch}: #{comp}" 
  if comp
    ret = comp.handle_key(ch) 
    $log.debug " NEWTABBED handle_key#{ch}: #{comp} returned #{ret} " 
    if ret != :UNHANDLED
      comp.repaint # NOTE: if we don;t do this, then it won't get repainted. I will have to repaint ALL
      # in repaint of this.
      return ret 
    end
    $log.debug "XXX NEWTABBED key unhandled by comp #{comp.name} "
  else
    Ncurses.beep
    $log.warn "XXX NEWTABBED key unhandled NULL comp"
  end
  case ch
  when ?\C-c.getbyte(0)
    $multiplier = 0
    return 0
  when ?0.getbyte(0)..?9.getbyte(0)
    $log.debug " VIM coming here to set multiplier #{$multiplier} "
    $multiplier *= 10 ; $multiplier += (ch-48)
    return 0
  end
  ret = process_key ch, self
  # allow user to map left and right if he wants
  if ret == :UNHANDLED
    case ch
    when KEY_UP, KEY_BTAB
      # form will pick this up and do needful
      return goto_prev_component #unless on_first_component?
    when KEY_LEFT
      # if i don't check for first component, key will go back to form,
      # but not be processes. so focussed remain here, but be false.
      # In case of returnign an unhandled TAB, on_leave will happen and cursor will move to
      # previous component outside of this.
      return goto_prev_component unless on_first_component?
    when KEY_RIGHT, KEY_TAB
      return goto_next_component #unless on_last_component?
    when KEY_DOWN
      if on_a_button?
        return goto_first_item
      else
        return goto_next_component #unless on_last_component?
      end
    else 
      #@_entered = false
      return :UNHANDLED
    end
  end

  $multiplier = 0
  return 0
end
insert_tab(index, title, config={}) click to toggle source

insert a tab at index, with title

# File lib/canis/core/widgets/rtabbedpane.rb, line 67
def insert_tab index, title, config={}, &block
  @tabs.insert(index, Tab.new(title, self, config, &block) )
end
leave_current_component() click to toggle source

leave the component we are on. This should be followed by all containers, so that the on_leave action of earlier comp can be displayed, such as dimming components selections

# File lib/canis/core/widgets/rtabbedpane.rb, line 298
def leave_current_component
  @current_component.on_leave
  # NOTE this is required, since repaint will just not happen otherwise
  # Some components are erroneously repainting all, after setting this to true so it is
  # working there.
  @current_component.repaint_required true
  $log.debug " after on_leave VIMS XXX #{@current_component.focussed}   #{@current_component.name}"
  @current_component.repaint
end
on_a_button?() click to toggle source

returns true if user on an action button @return true or false

# File lib/canis/core/widgets/rtabbedpane.rb, line 318
def on_a_button?
  @components.index(@current_component) < @buttons.count
end
on_enter() click to toggle source

on enter processing Very often the first may be a label !

# File lib/canis/core/widgets/rtabbedpane.rb, line 189
def on_enter
  # if BTAB, the last comp
  if $current_key == KEY_BTAB
    @current_component = @components.last
  else
    @current_component = @components.first
  end
  return unless @current_component
  $log.debug " NEWTABBED came to ON_ENTER #{@current_component} "
  set_form_row
  @_entered = true
end
on_first_component?() click to toggle source

is focus on first component

# File lib/canis/core/widgets/rtabbedpane.rb, line 309
def on_first_component?
  @current_component == @components.first
end
on_last_component?() click to toggle source

is focus on last component

# File lib/canis/core/widgets/rtabbedpane.rb, line 313
def on_last_component?
  @current_component == @components.last
end
on_leave() click to toggle source
Calls superclass method
# File lib/canis/core/widgets/rtabbedpane.rb, line 201
def on_leave
  @_entered = false
  super
end
remove_all() click to toggle source

remove all tabs

# File lib/canis/core/widgets/rtabbedpane.rb, line 76
def remove_all
  @tabs = []
  self
end
remove_tab(tab) click to toggle source

remove given tab

# File lib/canis/core/widgets/rtabbedpane.rb, line 71
def remove_tab tab
  @tabs.delete tab
  self
end
remove_tab_at(index = @current_tab) click to toggle source

remove tab at given index, defaulting to current

# File lib/canis/core/widgets/rtabbedpane.rb, line 81
def remove_tab_at index = @current_tab
  @tabs.delete_at index
end
repaint() click to toggle source
# File lib/canis/core/widgets/rtabbedpane.rb, line 85
def repaint
  @current_tab ||= 0
  @button_row ||=  @row + 2
  @separator_row = @button_row + 1 # hope we have it by now, where to print separator
  @separator_row0 = @button_row - 1 unless @button_row == @row + 1
  @separator_row2 = @row + @height - 3 # hope we have it by now, where to print separator
  #return unless @repaint_required
  if @buttons.empty?
    _create_buttons
    @components = @buttons.dup
    @components.push(*@tabs[@current_tab].items)
    create_action_buttons
    @components.push(*@action_buttons)
  elsif @tab_changed
    @components = @buttons.dup
    @components.push(*@tabs[@current_tab].items)
    @components.push(*@action_buttons)
    @tab_changed = false
  end
  # if some major change has happened then repaint everything
  if @repaint_required
    $log.debug " NEWTAB repaint graphic #{@graphic} "
    print_borders unless @suppress_borders # do this once only, unless everything changes
    print_separator1
    @components.each { |e| e.repaint_all(true); e.repaint }
  else
    @components.each { |e| e.repaint }
  end # if repaint_required
  # next line is not called. earlier the 's' was missing, suppress_borders is not false
  print_borders if (@suppress_borders == false && @repaint_all) # do this once only, unless everything changes
  @repaint_required = false
end
set_current_tab(t) click to toggle source

set current tab to given tab @return self

# File lib/canis/core/widgets/rtabbedpane.rb, line 331
def set_current_tab t
  return if @current_tab == t
  @current_tab = t
  goto_component @components[t]
  @tab_changed = true
  @repaint_required = true
  self
end
set_form_col() click to toggle source

private

# File lib/canis/core/widgets/rtabbedpane.rb, line 290
def set_form_col
  return if @current_component.nil?
  $log.debug " #{@name} NEWTABBED  set_form_col calling sfc for #{@current_component.name} "
  @current_component.set_form_col 
end
set_form_row() click to toggle source

private

# File lib/canis/core/widgets/rtabbedpane.rb, line 279
def set_form_row
  return :UNHANDLED if @current_component.nil?
  $log.debug " NEWTABBED on enter sfr #{@current_component} "
  @current_component.on_enter
  @current_component.set_form_row # why was this missing in vimsplit. is it
  # that on_enter does a set_form_row
  @current_component.set_form_col # XXX
  @current_component.repaint
  # XXX compo should do set_form_row and col if it has that
end
tab(title, config={}) click to toggle source

Add a tab @param String name of tab, may have ampersand for hotkey/accelerator

# File lib/canis/core/widgets/rtabbedpane.rb, line 47
def tab title, config={}, &block
  #@tab_components[title]=[]
  #@tabs << Tab.new(title, self, config, &block)
  insert_tab @tabs.count, title, config, &block
  self
end
Also aliased as: add_tab

Private Instance Methods

_create_buttons() click to toggle source

creates the tab buttons (which are radio buttons)

# File lib/canis/core/widgets/rtabbedpane.rb, line 364
def _create_buttons
  $log.debug "XXX: INSIDE create_buttons col_offset #{@col_offset} "
  v = Variable.new
  r = @button_row # @row + 1
  col = @col + @col_offset
  @tabs.each_with_index { |t, i| 
    txt = t.text
    @buttons << TabButton.new(nil) do 
      variable v
      text  txt
      name  txt
      #value txt
      row  r
      col col
      surround_chars ['','']
      selected_bgcolor 'green'
      selected_color 'white'

    end
    b = @buttons.last
    b.command do
      set_current_tab i
    end
    b.form = @form
    b.override_graphic  @graphic
    col += txt.length + @button_gap
  }
end
center_column(textlen) click to toggle source
# File lib/canis/core/widgets/rtabbedpane.rb, line 532
def center_column textlen
  width = @col + @width
  return (width-textlen)/2
end
create_action_buttons() click to toggle source

Decides which buttons are to be created create the buttons at the bottom OK/ APPLY/ CANCEL

# File lib/canis/core/widgets/rtabbedpane.rb, line 469
def create_action_buttons
  return unless @button_type
  case @button_type.to_s.downcase
  when "ok"
    make_buttons ["&OK"]
  when "ok_cancel" #, "input", "list", "field_list"
    make_buttons %w[&OK &Cancel]
  when "ok_apply_cancel" #, "input", "list", "field_list"
    make_buttons %w[&OK &Apply &Cancel]
  when "yes_no"
    make_buttons %w[&Yes &No]
  when "yes_no_cancel"
    make_buttons ["&Yes", "&No", "&Cancel"]
  when "custom"
    raise "Blank list of buttons passed to custom" if @buttons.nil? or @buttons.size == 0
    make_buttons @buttons
  else
    $log.warn "No buttontype passed for creating tabbedpane. Not creating any"
    #make_buttons ["&OK"]
  end
end
init_vars() click to toggle source

Put all the housekeeping stuff at the end

# File lib/canis/core/widgets/rtabbedpane.rb, line 343
def init_vars
  @buttons        = []
  @tabs           = []
  #@tab_components = {}
  @bottombuttons  = []
  #
  # I'll keep current tabs comps in this to simplify
  @components     = []      
  @_entered = false
  # added on 2014-05-30 - 12:13 otherwise borders not printed first time
  @repaint_all = true
  @repaint_required = true
end
make_buttons(names) click to toggle source

actually creates the action buttons

# File lib/canis/core/widgets/rtabbedpane.rb, line 492
def make_buttons names
  @action_buttons = []
  $log.debug "XXX: came to NTP make buttons FORM= #{@form.name} names #{names}  "
  total = names.inject(0) {|total, item| total + item.length + 4}
  bcol = center_column total

  # this craps out when height is zero
  brow = @row + @height-2
  brow = FFI::NCurses.LINES-2 if brow < 0
  $log.debug "XXX: putting buttons :on #{brow} : #{@row} , #{@height} "
  button_ct=0
  tpp = self
  names.each_with_index do |bname, ix|
    text = bname
    #underline = @underlines[ix] if !@underlines.nil?

    button = Button.new nil do
      text text
      name bname
      row brow
      col bcol
      #underline underline
      highlight_bgcolor $reversecolor 
      color $datacolor
      bgcolor $datacolor
    end
    @action_buttons << button 
    button.form = @form
    button.override_graphic  @graphic
    index = button_ct
    tpp = self
    button.command { |form| @selected_index = index; @stop = true; 
      # ActionEvent has source event and action_command
      fire_handler :PRESS, ActionEvent.new(tpp, index, button.text)
      #throw(:close, @selected_index)
    }
    button_ct += 1
    bcol += text.length+6
  end
end
map_keys() click to toggle source
# File lib/canis/core/widgets/rtabbedpane.rb, line 357
def map_keys
  @keys_mapped = true
  #bind_key(?q, :myproc)
  #bind_key(32, :myproc)
end
print_borders() click to toggle source
print_separator1() click to toggle source
print_title() click to toggle source