Class: RubyCurses::ComboBox

Inherits:
Field show all
Includes:
EventHandler
Defined in:
lib/rbcurse/rcombo.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.

Constant Summary

Constants included from Io

Io::ERROR_COLOR_PAIR, Io::FOOTER_COLOR_PAIR, Io::LINEONE, Io::MAIN_WINDOW_COLOR_PAIR

Instance Attribute Summary collapse

Attributes inherited from Field

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

Attributes inherited from Widget

#col_offset, #cols_panned, #config, #curpos, #ext_col_offset, #ext_row_offset, #form, #id, #parent_component, #row_offset, #rows_panned, #should_create_buffer, #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, #modified?, #on_enter, #set_buffer, #set_focusable, #set_label, #text_variable

Methods inherited from Widget

#OLDbind_key, #buffer_to_screen, #buffer_to_window, #create_buffer, #destroy, #destroy_buffer, #focus, #get_buffer, #get_color, #get_preferred_size, #getvalue, #getvalue_for_paint, #height, #height=, #hide, #is_double_buffered?, #modified?, #move, #on_enter, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #safe_create_buffer, #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

#askchoice, #askyesno, #askyesnocancel, #clear_error, #clear_this, #get_string, #newaskyesno, #old_print_header, #old_print_top_right, #print_action, #print_error, #print_footer_help, #print_header, #print_headers, #print_help, #print_help_page, #print_in_middle, #print_key_labels, #print_key_labels_row, #print_screen_labels, #print_status, #print_this, #print_top_right, #rbgetstr

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from DSL

#OLD_method_missing

Constructor Details

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

Returns a new instance of ComboBox.



38
39
40
41
42
43
44
# File 'lib/rbcurse/rcombo.rb', line 38

def initialize form, config={}, &block
  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
end

Instance Attribute Details

#COMBO_SYMBOLObject

the symbol you want to use for combos



35
36
37
# File 'lib/rbcurse/rcombo.rb', line 35

def COMBO_SYMBOL
  @COMBO_SYMBOL
end

#current_indexObject

INSERT_AFTER_CURRENT, INSERT_BEFORE_CURRENT,INSERT_ALPHABETICALLY



33
34
35
# File 'lib/rbcurse/rcombo.rb', line 33

def current_index
  @current_index
end

#show_symbolObject

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



36
37
38
# File 'lib/rbcurse/rcombo.rb', line 36

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rbcurse/rcombo.rb', line 74

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 ch
  #when KEY_UP  # show previous value
  #  previous_row
  #when KEY_DOWN  # show previous value
  #  next_row
  when KEY_DOWN+ RubyCurses::META_KEY # alt down
    popup  # pop up the popup
  else
    super
  end
end

#init_varsObject



45
46
47
48
49
50
51
# File 'lib/rbcurse/rcombo.rb', line 45

def init_vars
  super
  @show_symbol ||= true
  @COMBO_SYMBOL ||= Ncurses::ACS_GEQUAL
  bind_key(KEY_UP) { previous_row }
  bind_key(KEY_DOWN) { next_row }
end

#list(alist = nil) ⇒ Object

convert given list to datamodel



61
62
63
64
# File 'lib/rbcurse/rcombo.rb', line 61

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

#list_data_model(ldm) ⇒ Object

set given datamodel



67
68
69
70
# File 'lib/rbcurse/rcombo.rb', line 67

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rbcurse/rcombo.rb', line 176

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



100
101
102
103
104
105
106
# File 'lib/rbcurse/rcombo.rb', line 100

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.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rbcurse/rcombo.rb', line 201

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rbcurse/rcombo.rb', line 119

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



93
94
95
96
97
98
99
# File 'lib/rbcurse/rcombo.rb', line 93

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



148
149
150
151
152
153
154
155
156
157
# File 'lib/rbcurse/rcombo.rb', line 148

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbcurse/rcombo.rb', line 160

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rbcurse/rcombo.rb', line 226

def repaint
  super
  c = @col + @display_length
 # @form.window.mvwvline( @row, c, ACS_VLINE, 1)
  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
  end
 # @form.window.mvwvline( @row, c+2, ACS_VLINE, 1)
 # @form.window.mvwaddch @row, c+2, Ncurses::ACS_S1
 # @form.window.mvwaddch @row, c+3, Ncurses::ACS_S9
 # @form.window.mvwaddch @row, c+4, Ncurses::ACS_LRCORNER
 # @form.window.mvwhline( @row, c+5, ACS_HLINE, 2)
end

#selected_indexObject



55
56
57
# File 'lib/rbcurse/rcombo.rb', line 55

def selected_index
  @current_index
end

#selected_itemObject



52
53
54
# File 'lib/rbcurse/rcombo.rb', line 52

def selected_item
  @list[@current_index]
end