Class: RubyCurses::ComboBox

Inherits:
Field show all
Includes:
EventHandler
Defined in:
lib/rbhex/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 display_length 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

#buffer, #datatype, #field_col, #form, #handler, #original_value, #overwrite_mode, #type

Attributes inherited from Widget

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

Instance Method Summary collapse

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Methods inherited from Field

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

Methods inherited from Widget

#action_manager, #changed, #click, #color_pair, #command, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue, #getvalue_for_paint, #height, #height=, #hide, #leave, #modified?, #move, #on_enter, #override_graphic, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=

Methods included from Io

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

Methods included from Utils

#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #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/rbhex/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 set_buffer in creation. 2009-01-18 00:03
  set_buffer @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/rbhex/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/rbhex/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/rbhex/core/widgets/rcombo.rb', line 34

def show_symbol
  @show_symbol
end

Instance Method Details

#DEPnext_rowObject



121
122
123
124
125
126
127
# File 'lib/rbhex/core/widgets/rcombo.rb', line 121

def DEPnext_row
  @current_index += 1 if @current_index < @list.length()-1
  set_buffer @list[@current_index].dup
  set_modified(true)  ## ??? not required
  fire_handler :ENTER_ROW, self
  @list.on_enter_row self
end

#DEPprevious_rowObject



114
115
116
117
118
119
120
# File 'lib/rbhex/core/widgets/rcombo.rb', line 114

def DEPprevious_row
  @current_index -= 1 if @current_index > 0
  set_buffer @list[@current_index].dup
  set_modified(true)  ## ??? not required
  fire_handler :ENTER_ROW, self
  @list.on_enter_row self
end

#handle_key(ch) ⇒ Object

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



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
110
111
112
113
# File 'lib/rbhex/core/widgets/rcombo.rb', line 84

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
63
64
65
66
# File 'lib/rbhex/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

  # next 2 lines giving an error in newtabbedwindow.rb example since the methods
  # have been deprecated.
  #bind_key(KEY_UP) { previous_row }
  #bind_key(KEY_DOWN) { next_row }
end

#list(alist = nil) ⇒ Object

convert given list to datamodel



76
77
78
79
80
# File 'lib/rbhex/core/widgets/rcombo.rb', line 76

def list alist=nil
  return @list if alist.nil?
  #@list = RubyCurses::ListDataModel.new(alist)
  @list = alist
end

#next_match(char) ⇒ Object

the sets the next match in the edit field



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rbhex/core/widgets/rcombo.rb', line 212

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

#OLDpopupObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbhex/core/widgets/rcombo.rb', line 154

def OLDpopup
  listconfig = (@list_config && @list_config.dup) || {}
  dm = @list
  # current item in edit box will be focussed when list pops up
  #$log.debug "XXX POPUP: #{dm.selected_index} = #{@current_index}, value #{@buffer}"
  # we are having some problms when using this in a list. it retains earlier value
  _index = dm.index @buffer
  dm.selected_index = _index #  @current_index
  poprow = @row+0 # one row below the edit box
  popcol = @col
  dlength = @display_length
  f = self
  @popup = RubyCurses::PopupList.new do
    row  poprow
    col  popcol
    width dlength
    list_data_model dm
    list_selection_mode 'single'
    relative_to f
    list_config listconfig
    bind(:PRESS) do |index|
      f.set_buffer dm[index].dup
      f.set_modified(true) if f.current_index != index
      f.current_index = index
    end
  end
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.



237
238
239
# File 'lib/rbhex/core/widgets/rcombo.rb', line 237

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rbhex/core/widgets/rcombo.rb', line 140

def popup
  @list_config ||= {}
  @list_config[:row] ||= @row
  #@list_config[:col] ||= @col
  @list_config[:col] ||= @col + @display_length
  @list_config[:relative_to] ||= self
  # this does not allow us to bind to events in the list
  index = popuplist @list, @list_config
  if index
    set_buffer @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



183
184
185
186
187
188
189
190
191
192
# File 'lib/rbhex/core/widgets/rcombo.rb', line 183

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rbhex/core/widgets/rcombo.rb', line 195

def putch char
  @current_index ||= 0
  if @editable
    raise "how is it editable here in combo"
    super
    return 0
  else
    match = next_match(char)
    set_buffer match unless match.nil?
    fire_handler :ENTER_ROW, self
  end
  @modified = true
  fire_handler :CHANGE, self    # 2008-12-09 14:51  ???
  0
end

#repaintObject



241
242
243
244
245
246
247
248
249
# File 'lib/rbhex/core/widgets/rcombo.rb', line 241

def repaint
  super
  c = @col + @display_length
  if @show_symbol # 2009-01-11 18:47
    # i have changed c +1 to c, since we have no right to print beyond display_length
    @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



70
71
72
# File 'lib/rbhex/core/widgets/rcombo.rb', line 70

def selected_index
  @current_index
end

#selected_itemObject

next 2 lines giving an error in newtabbedwindow.rb example since the methods have been deprecated. bind_key(KEY_UP) { previous_row } bind_key(KEY_DOWN) { next_row }



67
68
69
# File 'lib/rbhex/core/widgets/rcombo.rb', line 67

def selected_item
  @list[@current_index]
end