Class: RubyCurses::PopupList

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

Overview

TODO CAN WE MOVE THIS OUT TO ANOTHER FILE as confusing me 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

Constructor Details

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

Returns a new instance of PopupList.



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
249
250
251
252
253
254
# File 'lib/rbcurse/rlistbox.rb', line 214

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.



203
204
205
# File 'lib/rbcurse/rlistbox.rb', line 203

def config
  @config
end

#listboxObject (readonly)

Returns the value of attribute listbox.



212
213
214
# File 'lib/rbcurse/rlistbox.rb', line 212

def listbox
  @listbox
end

#selected_indexObject (readonly)

button index selected by user



204
205
206
# File 'lib/rbcurse/rlistbox.rb', line 204

def selected_index
  @selected_index
end

#windowObject (readonly)

required for keyboard



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

def window
  @window
end

Instance Method Details

#cget(param) ⇒ Object



379
380
381
# File 'lib/rbcurse/rlistbox.rb', line 379

def cget param
  @config[param]
end

#configure(*val, &block) ⇒ Object

may need to be upgraded to new one XXX FIXME



369
370
371
372
373
374
375
376
377
378
# File 'lib/rbcurse/rlistbox.rb', line 369

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



386
387
388
# File 'lib/rbcurse/rlistbox.rb', line 386

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

#handle_keysObject

popuplist



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rbcurse/rlistbox.rb', line 280

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



271
272
273
274
# File 'lib/rbcurse/rlistbox.rb', line 271

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



383
384
385
# File 'lib/rbcurse/rlistbox.rb', line 383

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

#list(alist = nil) ⇒ Object

class popup



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

def list alist=nil
  return @list if alist.nil?
  @list = ListDataModel.new(alist)
  @repaint_required = true
  #  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

class popup



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

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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/rbcurse/rlistbox.rb', line 298

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
      # if you press ENTER without selecting, it won't come here
      # it will fire button OK's fire, if that's the default button

      # returns an array of indices if multiple selection
      if @listbox.selection_mode == :multiple
        fire_handler :PRESS, @listbox
      else
        fire_handler :PRESS, @listbox.focussed_index
      end
      # since Listbox is handling enter, COMBO_SELECT will not be fired
    # $log.debug "popup ENTER :  #{field.name}" if !field.nil?
      @stop = true
      return
    when KEY_TAB
      @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


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/rbcurse/rlistbox.rb', line 338

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)


276
277
278
# File 'lib/rbcurse/rlistbox.rb', line 276

def stopping?
  @stop
end