Class: RubyCurses::ComboBoxEdit

Inherits:
Field
  • Object
show all
Includes:
EventHandler
Defined in:
lib/rbcurse/extras/widgets/rcomboedit.rb

Overview

TODO : i no longer use values, i now use “list” or better “list_data_model” try to make it so values gets converted to list. NOTE: 2010-10-01 13:25 spacebar and enter will popup in addition to Alt-Down

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ComboBoxEdit.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 41

def initialize form, config={}, &block
  @arrow_key_policy = :ignore
  @show_symbol = true
  @COMBO_SYMBOL = "v".ord  # trying this out
  super
  @current_index ||= 0
  # 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



37
38
39
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 37

def COMBO_SYMBOL
  @COMBO_SYMBOL
end

#current_indexObject

INSERT_AFTER_CURRENT, INSERT_BEFORE_CURRENT,INSERT_ALPHABETICALLY



35
36
37
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 35

def current_index
  @current_index
end

#show_symbolObject

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



38
39
40
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 38

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



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
110
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 81

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



52
53
54
55
56
57
58
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 52

def init_vars
  super
  #@show_symbol = false if @label  # commented out 2011-11-13 maybe it doesn't place properly if label
  @COMBO_SYMBOL ||= FFI::NCurses::ACS_DARROW #GEQUAL # now this won't work since i've set it above
  bind_key(KEY_UP) { previous_row }
  bind_key(KEY_DOWN) { next_row }
end

#list(alist = nil) ⇒ Object

convert given list to datamodel



68
69
70
71
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 68

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

#list_data_model(ldm) ⇒ Object

set given datamodel



74
75
76
77
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 74

def list_data_model ldm
  raise "Expecting list_data_model" unless ldm.is_a? RubyCurses::ListDataModel
  @list = ldm
end

#next_match(char) ⇒ Object

the sets the next match in the edit field



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 194

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

#next_rowObject



118
119
120
121
122
123
124
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 118

def next_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

#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.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 219

def on_leave
  if !@list.include? @buffer and !@buffer.strip.empty?
    _insert_policy = @insert_policy || :INSERT_AT_BOTTOM
    case _insert_policy
    when :INSERT_AT_BOTTOM, :INSERT_AT_END
      @list.append  @buffer
    when :INSERT_AT_TOP
      @list.insert(0, @buffer)
    when :INSERT_AFTER_CURRENT
      @current_index += 1
      @list.insert(@current_index, @buffer)

    when :INSERT_BEFORE_CURRENT
      #_index = @current_index-1 if @current_index>0
      _index = @current_index
      @list.insert(_index, @buffer)
    when :INSERT_AT_CURRENT
      @list[@current_index]=@buffer
    when :NO_INSERT
      ; # take a break
    end
  end
  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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 137

def popup
  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

#previous_rowObject



111
112
113
114
115
116
117
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 111

def previous_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

#putc(c) ⇒ Object

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



166
167
168
169
170
171
172
173
174
175
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 166

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 178

def putch char
  @current_index ||= 0
  if @editable 
    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



244
245
246
247
248
249
250
251
252
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 244

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



62
63
64
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 62

def selected_index
  @current_index
end

#selected_itemObject



59
60
61
# File 'lib/rbcurse/extras/widgets/rcomboedit.rb', line 59

def selected_item
  @list[@current_index]
end