Class: RubyCurses::PopupList

Inherits:
Object
  • Object
show all
Includes:
EventHandler
Defined in:
lib/rbcurse/extras/widgets/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

Constructor Details

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

Returns a new instance of PopupList.



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
255
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 215

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



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

def config
  @config
end

#listboxObject (readonly)

Returns the value of attribute listbox.



213
214
215
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 213

def listbox
  @listbox
end

#selected_indexObject (readonly)

button index selected by user



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

def selected_index
  @selected_index
end

#windowObject (readonly)

required for keyboard



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

def window
  @window
end

Instance Method Details

#cget(param) ⇒ Object



380
381
382
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 380

def cget param
  @config[param]
end

#configure(*val, &block) ⇒ Object

may need to be upgraded to new one XXX FIXME



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

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



387
388
389
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 387

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

#handle_keysObject

popuplist



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

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



272
273
274
275
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 272

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



384
385
386
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 384

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



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

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



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

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



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
338
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 299

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


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
368
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 339

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)


277
278
279
# File 'lib/rbcurse/extras/widgets/rlistbox.rb', line 277

def stopping?
  @stop
end