class Canis::ComboBox

the quick approach would be to use field, and just add a popup. Or since we are not editing, we could use a Label and a popup Or just display a label and a popup without using anything else. Thre is an undocumented variable width which is the size of the label

This is used to position the combo symbol and the popup. This can be calculated
based on the label. 2014-03-24 - 16:42

Attributes

combo_symbol[RW]

the symbol you want to use for combos

current_index[RW]
show_symbol[RW]

Public Class Methods

new(form, config={}) click to toggle source
Calls superclass method
# File lib/canis/core/widgets/rcombo.rb, line 37
def initialize form, config={}, &block
  @arrow_key_policy = :ignore
  @editable         = false
  #@combo_symbol = "v".ord  # trying this out
  # thanks hramrach for fix
  if RUBY_VERSION < "1.9" then
    @combo_symbol = "v"[0]  # trying this out
  else
    @combo_symbol = "v".ord  # trying this out
  end 
  @current_index    = 0
  super
  ## this was getting overridden this making the combo editable 2014-03-24 - 16:24
  @editable         = false
  # added if  check since it was overriding text in creation. 2009-01-18 00:03
  text @list[@current_index].dup if @buffer.nil? or @buffer.empty?
  init_vars
  @_events.push(*[:CHANGE, :ENTER_ROW, :LEAVE_ROW])
end

Public Instance Methods

handle_key(ch) click to toggle source

combo edit box key handling removed UP and DOWN and bound it, so it can be unbound

Calls superclass method
# File lib/canis/core/widgets/rcombo.rb, line 80
def handle_key(ch)
  @current_index ||= 0
  # added 2009-01-18 22:44 no point moving horiz or passing up to Field if not edit
  if !@editable
    if ch == KEY_LEFT or ch == KEY_RIGHT
      return :UNHANDLED
    end
  end
  case @arrow_key_policy 
  when :ignore
    if ch == KEY_DOWN or ch == KEY_UP
      return :UNHANDLED
    end
  when :popup
    if ch == KEY_DOWN or ch == KEY_UP
      popup
    end
  end
  case ch
  #when KEY_UP  # show previous value
  #  previous_row
  #when KEY_DOWN  # show previous value
  #  next_row
    # adding spacebar to popup combo, as in microemacs 2010-10-01 13:21
  when 32, KEY_DOWN+ META_KEY # alt down
    popup  # pop up the popup
  else
    super
  end
end
init_vars() click to toggle source
Calls superclass method
# File lib/canis/core/widgets/rcombo.rb, line 56
def init_vars
  super
  @show_symbol = true if @show_symbol.nil? # if set to false don't touch
  #@show_symbol = false if @label # 2011-11-13
  @combo_symbol ||= FFI::NCurses::ACS_DARROW #GEQUAL

end
list(alist=nil) click to toggle source

convert given list to datamodel

# File lib/canis/core/widgets/rcombo.rb, line 72
def list alist=nil
  return @list if alist.nil?
  #@list = Canis::ListDataModel.new(alist)
  @list = alist
end
next_match(char) click to toggle source

the sets the next match in the edit field

# File lib/canis/core/widgets/rcombo.rb, line 169
def next_match char
  start = @current_index
  start.upto(@list.length-1) do |ix|
    if @list[ix][0,1].casecmp(char) == 0
      return @list[ix] unless @list[ix] == @buffer
    end
    @current_index += 1
  end
  ## could not find, start from zero
  @current_index = 0
  start = [@list.length()-1, start].min
  0.upto(start) do |ix|
    if @list[ix][0,1].casecmp(char) == 0
      return @list[ix] unless @list[ix] == @buffer
    end
    @current_index += 1
  end
  @current_index = [@list.length()-1, @current_index].min
  return nil
end
on_leave() click to toggle source

on leaving the listbox, update the combo/datamodel. we are using methods of the datamodel. Updating our list will have no effect on the list, and wont trigger events. Do not override.

# File lib/canis/core/widgets/rcombo.rb, line 194
def on_leave
  fire_handler :LEAVE, self
end
popup() click to toggle source

calls a popup list TODO: should not be positioned so that it goes off edge user's customizations of list should be passed in The dup of listconfig is due to a tricky feature/bug. I try to keep the config hash and instance variables in synch. So this config hash is sent to popuplist which updates its row col and next time we pop up the popup row and col are zero.

added dup in PRESS since editing edit field mods this on pressing ENTER, value set back and current_index updated

putc(c) click to toggle source

Field putc advances cursor when it gives a char so we override this

# File lib/canis/core/widgets/rcombo.rb, line 140
def putc c
  if c >= 0 and c <= 127
    ret = putch c.chr
    if ret == 0
      addcol 1 if @editable
      set_modified 
    end
  end
  return -1 # always ??? XXX
end
putch(char) click to toggle source

field does not give char to non-editable fields so we override

Calls superclass method
# File lib/canis/core/widgets/rcombo.rb, line 152
def putch char
  @current_index ||= 0
  if @editable 
    raise "how is it editable here in combo"
    super
    return 0
  else
    match = next_match(char)
    text match unless match.nil?
    fire_handler :ENTER_ROW, self
  end
  @modified = true
  fire_handler :CHANGE, self    # 2008-12-09 14:51  ???
  0
end
repaint() click to toggle source
Calls superclass method
# File lib/canis/core/widgets/rcombo.rb, line 198
def repaint
  super
  c = @col + @width
  if @show_symbol # 2009-01-11 18:47
    # i have changed c +1 to c, since we have no right to print beyond width
    @form.window.mvwaddch @row, c, @combo_symbol # Ncurses::ACS_GEQUAL
    @form.window.mvchgat(y=@row, x=c, max=1, Ncurses::A_REVERSE|Ncurses::A_UNDERLINE, $datacolor, nil)
  end
end
selected_index() click to toggle source
# File lib/canis/core/widgets/rcombo.rb, line 66
def selected_index
  @current_index
end
selected_item() click to toggle source
# File lib/canis/core/widgets/rcombo.rb, line 63
def selected_item
  @list[@current_index]
end