Class: RubyCurses::PopupList

Inherits:
Object
  • Object
show all
Includes:
DSL, EventHandler
Defined in:
lib/rbcurse/rlistbox.rb

Overview

pops up a list of values for selection 2008-12-10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Methods included from DSL

#OLD_method_missing

Constructor Details

#initialize(aconfig = {}, &block) ⇒ PopupList

Returns a new instance of PopupList.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rbcurse/rlistbox.rb', line 208

def initialize aconfig={}, &block
  @config = aconfig
  @selected_index = -1
  @list_config ||= {}
  @config.each_pair { |k,v| instance_variable_set("@#{k}",v) }
  instance_eval &block if block_given?
  @list_config.each_pair { |k,v|  instance_variable_set("@#{k}",v) }
  @height ||= [@max_visible_items || 10, @list.length].min 
  $log.debug " POPUP XXX #{@max_visible_items} ll:#{@list.length} h:#{@height}"
  # get widgets absolute coords
  if !@relative_to.nil?
    layout = @relative_to.form.window.layout
    @row = @row + layout[:top]
    @col = @col + layout[:left]
  end
  if !@valign.nil?
    case @valign.to_sym
    when :BELOW
      @row += 1
    when :ABOVE
      @row -= @height+1
      @row = 0 if @row < 0
    when :CENTER
      @row -= @height/2
      @row = 0 if @row < 0
    else
    end
  end

  layout(1+height, @width+4, @row, @col) # changed 2 to 1, 2008-12-17 13:48 
  @window = VER::Window.new(@layout)
  @form = RubyCurses::Form.new @window
  @window.bkgd(Ncurses.COLOR_PAIR($reversecolor));
  @window.wrefresh
  @panel = @window.panel  # useless line ?
  Ncurses::Panel.update_panels
  print_input # creates the listbox
  @form.repaint
  @window.wrefresh
  handle_keys
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



197
198
199
# File 'lib/rbcurse/rlistbox.rb', line 197

def config
  @config
end

#listboxObject (readonly)

Returns the value of attribute listbox.



206
207
208
# File 'lib/rbcurse/rlistbox.rb', line 206

def listbox
  @listbox
end

#selected_indexObject (readonly)

button index selected by user



198
199
200
# File 'lib/rbcurse/rlistbox.rb', line 198

def selected_index
  @selected_index
end

#windowObject (readonly)

required for keyboard



199
200
201
# File 'lib/rbcurse/rlistbox.rb', line 199

def window
  @window
end

Instance Method Details

#cget(param) ⇒ Object



363
364
365
# File 'lib/rbcurse/rlistbox.rb', line 363

def cget param
  @config[param]
end

#configure(*val, &block) ⇒ Object

may need to be upgraded to new one XXX FIXME



353
354
355
356
357
358
359
360
361
362
# File 'lib/rbcurse/rlistbox.rb', line 353

def configure(*val , &block)
  case val.size
  when 1
    return @config[val[0]]
  when 2
    @config[val[0]] = val[1]
    instance_variable_set("@#{val[0]}", val[1]) 
  end
  instance_eval &block if block_given?
end

#destroyObject



370
371
372
# File 'lib/rbcurse/rlistbox.rb', line 370

def destroy
  @window.destroy if !@window.nil?
end

#handle_keysObject

popuplist



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rbcurse/rlistbox.rb', line 271

def handle_keys
  begin
    while((ch = @window.getchar()) != 999 )
      case ch
      when -1
        next
      else
        press ch
        break if @stop
      end
    end
  ensure
    destroy  
  end
  return 0 #@selected_index
end

#input_valueObject



262
263
264
265
# File 'lib/rbcurse/rlistbox.rb', line 262

def input_value
  #return @listbox.getvalue if [email protected]?
  return @listbox.focussed_index if !@listbox.nil?
end

#layout(height = 0, width = 0, top = 0, left = 0) ⇒ Object



367
368
369
# File 'lib/rbcurse/rlistbox.rb', line 367

def layout(height=0, width=0, top=0, left=0)
  @layout = { :height => height, :width => width, :top => top, :left => left } 
end

#list(alist = nil) ⇒ Object



249
250
251
252
253
254
# File 'lib/rbcurse/rlistbox.rb', line 249

def list alist=nil
  return @list if alist.nil?
  @list = ListDataModel.new(alist)
  #  will we need this ? listbox made each time so data should be fresh
  #@list.bind(:LIST_DATA_EVENT) { |e| list_data_changed() }
end

#list_data_model(ldm) ⇒ Object

will we need this ? listbox made each time so data should be fresh @list.bind(:LIST_DATA_EVENT) { |e| list_data_changed() }



255
256
257
258
259
260
# File 'lib/rbcurse/rlistbox.rb', line 255

def list_data_model ldm
  raise "Expecting list_data_model" unless ldm.is_a? RubyCurses::ListDataModel
  @list = ldm
  #  will we need this ? listbox made each time so data should be fresh
  #@list.bind(:LIST_DATA_EVENT) { |e| list_data_changed() }
end

#press(ch) ⇒ Object

TODO get next match for key



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rbcurse/rlistbox.rb', line 289

def press ch
   $log.debug "popup handle_keys :  #{ch}"  if ch != -1
    case ch
    when -1
      return
    when KEY_F1, 27, ?\C-q   # 27/ESC does not come here since gobbled by keyboard.rb
      @stop = true
      return
    when KEY_ENTER, 10, 13
      fire_handler :PRESS, @listbox.focussed_index
      # since Listbox is handling enter, COMBO_SELECT will not be fired
    # $log.debug "popup ENTER : #{@selected_index} "
    # $log.debug "popup ENTER :  #{field.name}" if !field.nil?
      @stop = true
      return
    when 9
      @form.select_next_field 
    else
      # fields must return unhandled else we will miss hotkeys. 
      # On messageboxes, often if no edit field, then O and C are hot.
      field =  @form.get_current_field
      handled = field.handle_key ch

      if handled == :UNHANDLED
          @stop = true
          return
      end
    end
    @form.repaint
    Ncurses::Panel.update_panels();
    Ncurses.doupdate();
    @window.wrefresh
end


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/rbcurse/rlistbox.rb', line 322

def print_input
  r = c = 0
  width = @layout[:width]
  #$log.debug " print_input POPUP ht:#{@height} lh:#{@layout[:height]} "
  height = @layout[:height]
  #height = @height # 2010-01-06 12:52 why was this overriding previous line. its one less than layout
  # i am now using layout height since it gives a closer size to whats asked for.
  parent = @relative_to
  defaultvalue = @default_value || ""
  list = @list
  selection_mode = @list_selection_mode 
  default_values = @default_values
  @list_config['color'] ||= 'black'
  @list_config['bgcolor'] ||= 'yellow'
    @listbox = RubyCurses::Listbox.new @form, @list_config do
      name   "input" 
      row  r 
      col  c 
#         attr 'reverse'
      width width
      height height
      list_data_model  list
# ?? XXX          display_length  30
#         set_buffer defaultvalue
      selection_mode selection_mode
      default_values default_values
      is_popup true
      #add_observer parent
    end
end

#stopping?Boolean

popuplist

Returns:

  • (Boolean)


267
268
269
# File 'lib/rbcurse/rlistbox.rb', line 267

def stopping?
  @stop
end