Class: Canis::ComboBox

Inherits:
LabeledField show all
Includes:
EventHandler
Defined in:
lib/canis/core/widgets/rcombo.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Field

#above, #below, #buffer, #datatype, #field_col, #original_value, #overwrite_mode

Attributes inherited from Widget

#_object_created, #col_offset, #config, #curpos, #focussed, #form, #handler, #id, #key_label, #parent_component, #row_offset, #state

Instance Method Summary collapse

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Methods inherited from Field

#_set_buffer, #addcol, #cursor_backward, #cursor_end, #cursor_forward, #cursor_home, #delete_at, #delete_curr_char, #delete_eol, #delete_prev_char, #getvalue, #in_range?, #label, #map_keys, #modified?, #on_enter, #position_label, #restore_original_value, #set_focusable, #set_form_col, #set_label, #text, #text=, #type, #undo_delete_eol

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #command, #destroy, #focus, #focusable, #focusable?, #getvalue, #getvalue_for_paint, #hide, #modified?, #move, #on_enter, #override_graphic, #process_key, #property_set, #remove, #repaint_all, #repaint_required, #rowcol, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rb_getstr, #warn

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

Methods included from ConfigSetup

#config_setup, #variable_set

Constructor Details

#initialize(form, config = {}, &block) ⇒ ComboBox

Returns a new instance of ComboBox.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#combo_symbolObject

the symbol you want to use for combos



33
34
35
# File 'lib/canis/core/widgets/rcombo.rb', line 33

def combo_symbol
  @combo_symbol
end

#current_indexObject

Returns the value of attribute current_index.



31
32
33
# File 'lib/canis/core/widgets/rcombo.rb', line 31

def current_index
  @current_index
end

#show_symbolObject

show that funny symbol after a combo to signify its a combo



34
35
36
# File 'lib/canis/core/widgets/rcombo.rb', line 34

def show_symbol
  @show_symbol
end

Instance Method Details

#handle_key(ch) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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_varsObject



56
57
58
59
60
61
62
# 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) ⇒ Object

convert given list to datamodel



72
73
74
75
76
# 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) ⇒ Object

the sets the next match in the edit field



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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_leaveObject

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.



194
195
196
# File 'lib/canis/core/widgets/rcombo.rb', line 194

def on_leave
  fire_handler :LEAVE, self
end

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/canis/core/widgets/rcombo.rb', line 122

def popup
  @list_config ||= {}
  @list_config[:row] ||= @row
  #@list_config[:col] ||= @col
  #@list_config[:col] ||= @col + @width
  # after introducing LabeledField which starts with lcol and uses col for field
  @list_config[:col] ||= @col 
  @list_config[:relative_to] ||= self
  # this does not allow us to bind to events in the list
  index = popuplist @list, @list_config
  if index
    text @list[index].dup
    set_modified(true) if @current_index != index
    @current_index = index
  end
end

#putc(c) ⇒ Object

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



140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Object

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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

#repaintObject



198
199
200
201
202
203
204
205
206
# 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_indexObject



66
67
68
# File 'lib/canis/core/widgets/rcombo.rb', line 66

def selected_index
  @current_index
end

#selected_itemObject



63
64
65
# File 'lib/canis/core/widgets/rcombo.rb', line 63

def selected_item
  @list[@current_index]
end