Class: RubyCurses::Menu

Inherits:
MenuItem show all
Defined in:
lib/rbcurse/rmenu.rb,
lib/rbcurse/rpopupmenu.rb

Overview

class Menu

Direct Known Subclasses

PopupMenu

Constant Summary collapse

[]
@@row =
0
@@col =
0

Instance Attribute Summary collapse

Attributes inherited from MenuItem

#accelerator, #mnemonic

Instance Method Summary collapse

Methods inherited from MenuItem

#command

Constructor Details

#initialize(text, &block) ⇒ Menu

Returns a new instance of Menu.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rbcurse/rmenu.rb', line 152

def initialize text, &block
  super text, nil, &block
  @text = text
  @items = []
  @enabled = true
  @current_menu = []
  instance_eval &block if block_given?
  @row ||=10
  @col ||=10
  @@menus ||= []
end

Instance Attribute Details

#colObject

Returns the value of attribute col.



137
138
139
# File 'lib/rbcurse/rmenu.rb', line 137

def col
  @col
end

#current_menuObject (readonly)

Returns the value of attribute current_menu.



144
145
146
# File 'lib/rbcurse/rmenu.rb', line 144

def current_menu
  @current_menu
end

#enabledObject

Returns the value of attribute enabled.



139
140
141
# File 'lib/rbcurse/rmenu.rb', line 139

def enabled
  @enabled
end

#itemsObject (readonly)

attr_reader :text



153
154
155
# File 'lib/rbcurse/rpopupmenu.rb', line 153

def items
  @items
end

#panelObject (readonly)

Returns the value of attribute panel.



143
144
145
# File 'lib/rbcurse/rmenu.rb', line 143

def panel
  @panel
end

#parentObject

NEW



135
136
137
# File 'lib/rbcurse/rmenu.rb', line 135

def parent
  @parent
end

#rowObject

Returns the value of attribute row.



136
137
138
# File 'lib/rbcurse/rmenu.rb', line 136

def row
  @row
end

#row_marginObject (readonly)

2009-01-21 12:06 NEW



145
146
147
# File 'lib/rbcurse/rmenu.rb', line 145

def row_margin
  @row_margin
end

#textObject (readonly)

Returns the value of attribute text.



140
141
142
# File 'lib/rbcurse/rmenu.rb', line 140

def text
  @text
end

#widthObject

Returns the value of attribute width.



138
139
140
# File 'lib/rbcurse/rmenu.rb', line 138

def width
  @width
end

#windowObject (readonly)

Returns the value of attribute window.



142
143
144
# File 'lib/rbcurse/rmenu.rb', line 142

def window
  @window
end

Instance Method Details

#add(menuitem) ⇒ Object

item could be menuitem or another menu or a string or a Action support for action added 2009-01-21 18:08



171
172
173
174
# File 'lib/rbcurse/rmenu.rb', line 171

def add menuitem
  @items << menuitem
  return self
end

#add_separatorObject



178
179
180
# File 'lib/rbcurse/rmenu.rb', line 178

def add_separator 
  @items << MenuSeparator.new
end

#array_width(a) ⇒ Object

private



344
345
346
347
348
# File 'lib/rbcurse/rmenu.rb', line 344

def array_width a
  longest = a.max {|a,b| a.to_s.length <=> b.to_s.length }
  $log.debug "array width #{longest}"
  longest.to_s.length
end

#check_mnemonics(cmenu, ch) ⇒ Object

checks given key against current menu’s items and fires key if added on 2008-11-27 12:07



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/rbcurse/rmenu.rb', line 417

def check_mnemonics cmenu, ch
#     $log.debug "inside check_mnemonics #{ch}"
  key = ch.chr.downcase rescue ""
  cmenu.items.each do |item|
    next if !item.respond_to? :mnemonic or item.mnemonic.nil?
#       $log.debug "inside check_mnemonics #{item.mnemonic}"
    if key == item.mnemonic.downcase
      ret = item.fire
      return ret #0 2009-01-23 00:45 
    end
  end
  return :UNHANDLED
end

#clear_menusObject

called upon firing so when we next show menubar there are not any left overs in here.



164
165
166
# File 'lib/rbcurse/rmenu.rb', line 164

def clear_menus
  @@menus = []
end

#create_action_component(action) ⇒ Object

create a Menuitem given an Action if menuitem.kind_of? RubyCurses::Action



180
181
182
183
184
185
# File 'lib/rbcurse/rpopupmenu.rb', line 180

def create_action_component action
    m = MenuItem.new(action.name, action.mnemonic)
    m.command { action.call }
    m.accelerator = action.accelerator
    return m
end

#create_windowObject

menu XXX



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
339
340
341
342
# File 'lib/rbcurse/rmenu.rb', line 312

def create_window # menu
  margin = 3
  @width = array_width @items
  $log.debug "create window menu #{@text}: #{@row} ,#{@col},wd #{@width}   " 
  @layout = { :height => @items.length+3, :width => @width+margin, :top => @row+1, :left => @col } 
  @win = VER::Window.new(@layout)
  @window = @win
  @win.bkgd(Ncurses.COLOR_PAIR($datacolor));
  @panel = @win.panel
    @window.printstring( 0, 0, "+%s+" % ("-"*@width), $reversecolor)
    r = 1
    @items.each do |item|
      #if item == :SEPARATOR
      #  @window.printstring( r, 0, "|%s|" % ("-"*@width), $reversecolor)
      #else
        item.row = r
        item.col = 0
        item.col = @col+@width+margin # margins???
 #         $log.debug "create window menu loop passing col : #{item.col} " 
        item.width = @width
        #item.window = @window
        item.parent = self
        item.repaint
      #end
      r+=1
    end
    @window.printstring( r, 0, "+%s+" % ("-"*@width), $reversecolor)
  select_item 0
  @window.refresh
  return @window
end

#destroyObject



349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rbcurse/rmenu.rb', line 349

def destroy
  $log.debug "DESTRY menu #{@text}"
  return if @window.nil?
  @visible = false
  panel = @window.panel
  Ncurses::Panel.del_panel(panel) if !panel.nil?   
  @window.delwin if !@window.nil?
  @items.each do |item|
    #next if item == :SEPARATOR
    item.destroy
  end
  @window = nil
end

#fireObject

menu -



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rbcurse/rmenu.rb', line 194

def fire
  $log.debug "menu fire called: #{text}  " 
  if @window.nil?
    #repaint
    create_window
    if !@parent.is_a? RubyCurses::MenuBar 
      @parent.current_menu << self
      @@menus << self # NEW
    end
  else
    ### shouod this not just show ?
    $log.debug "menu fire called: #{text} ELSE XXX WHEN IS THIS CALLED ? 658  " 
    return @items[@active_index].fire # this should happen if selected. else selected()
  end
  #@action.call if [email protected]?
end

#get_item(i) ⇒ Object

added 2009-01-21 12:09 NEW



182
183
184
# File 'lib/rbcurse/rmenu.rb', line 182

def get_item i
  @items[i]
end

#handle_key(ch) ⇒ Object

menu LEFT, RIGHT, DOWN, UP, ENTER item could be menuitem or another menu



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/rbcurse/rmenu.rb', line 365

def handle_key ch
  if !@current_menu.empty?
    cmenu = @current_menu.last
  else 
    cmenu = self
  end
  if !@@menus.empty?
   cmenu = @@menus.last
  else 
   cmenu = self
  end
  case ch
  when KEY_DOWN
      cmenu.select_next_item
  when KEY_UP
    cmenu.select_prev_item
  when KEY_ENTER, 10, 13, 32 # added 32 2008-11-27 23:50 
    return cmenu.fire
  when KEY_LEFT
    if cmenu.parent.is_a? RubyCurses::Menu 
   $log.debug "LEFT IN MENU : #{cmenu.parent.class} len: #{cmenu.parent.current_menu.length}"
   $log.debug "left IN MENU : #{cmenu.parent.class} len: #{cmenu.current_menu.length}"
    end
    if cmenu.parent.is_a? RubyCurses::Menu and !cmenu.parent.current_menu.empty?
   $log.debug " ABOU TO DESTROY DUE TO LEFT"
      cmenu.parent.current_menu.pop
      @@menus.pop ## NEW
      cmenu.destroy
    else
      return :UNHANDLED
    end
  when KEY_RIGHT
   $log.debug "RIGHTIN MENU : "
    if cmenu.parent.is_a? RubyCurses::Menu 
   $log.debug "right IN MENU : #{cmenu.parent.class} len: #{cmenu.parent.current_menu.length}"
   $log.debug "right IN MENU : #{cmenu.parent.class} len: #{cmenu.current_menu.length}"
    end
    if cmenu.parent.is_a? RubyCurses::Menu and !cmenu.parent.current_menu.empty?
      $log.debug " ABOU TO DESTROY DUE TO RIGHT"
      cmenu.parent.current_menu.pop
      @@menus.pop
      cmenu.destroy
    end
    return :UNHANDLED
  else
    ret = check_mnemonics cmenu, ch
    return ret
  end
end

#highlight(tf = true) ⇒ Object

menu



302
303
304
305
306
307
308
309
310
311
# File 'lib/rbcurse/rmenu.rb', line 302

def highlight tf=true # menu
      $log.debug "MENU SUBMENU menu highlight: #{text} #{@row} #{@col}, PW #{@parent.width}  " 
  color = tf ? $datacolor : $reversecolor
  att = tf ? Ncurses::A_REVERSE : Ncurses::A_NORMAL
  #@parent.window.mvchgat(y=@row, x=1, @width, Ncurses::A_NORMAL, color, nil)
  #@parent.window.mvchgat(y=@row, x=1, @parent.width, Ncurses::A_NORMAL, color, nil)
  # above line did not work with vt100/vt200 next does
  @parent.window.mvchgat(y=@row, x=1, @parent.width, att, $reversecolor, nil)
  @parent.window.wrefresh
end

#insert(menuitem, ix) ⇒ Object

added 2009-01-20 13:28 NEW



194
195
196
197
198
199
200
# File 'lib/rbcurse/rpopupmenu.rb', line 194

def insert menuitem, ix
  if menuitem.kind_of? RubyCurses::Action
    menuitem = create_action_component menuitem
  end
  @items.insert ix, menuitem
  return self
end

#insert_separator(ix) ⇒ Object



175
176
177
# File 'lib/rbcurse/rmenu.rb', line 175

def insert_separator ix
  @items.insert ix, MenuSeparator.new
end

#on_enterObject

menu.on_enter



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

def on_enter # menu.on_enter
  $log.debug "menu onenter: #{text} #{@row} #{@col}  " 
  # call parent method. XXX
    if @parent.is_a? RubyCurses::MenuBar 
      @parent.window.printstring( @row, @col, " %s " % text, $datacolor)
    else
      highlight
    end
    if !@window.nil? #and @parent.selected
      $log.debug "menu onenter: #{text} calling window,show"
      @window.show
      select_item 0
    elsif @parent.is_a? RubyCurses::MenuBar and  @parent.selected
      # only on the top level do we open a window if a previous one was opened
      $log.debug "menu onenter: #{text} calling repaint CLASS: #{@parent.class}"
    #  repaint
      create_window
    end
end

#on_leaveObject

menu.on_leave



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rbcurse/rmenu.rb', line 287

def on_leave # menu.on_leave
  $log.debug "menu onleave: #{text} #{@row} #{@col}  " 
  # call parent method. XXX
    if @parent.is_a? RubyCurses::MenuBar 
      @parent.window.printstring( @row, @col, " %s " % text, $reversecolor)
      @window.hide if !@window.nil?
    else
      $log.debug "MENU SUBMEN. menu onleave: #{text} #{@row} #{@col}  " 
      # parent is a menu
      highlight false
      #@parent.current_menu.pop
      #@@menus.pop
      #destroy
    end
end

#remove(n) ⇒ Object

added 2009-01-21 12:09 NEW



186
187
188
189
190
191
192
# File 'lib/rbcurse/rmenu.rb', line 186

def remove n
  if n.is_a? Fixnum
    @items.delete_at n
  else
    @items.delete n
  end
end

#repaintObject

user has clicked down, we shoud display items DRAW menuitems



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rbcurse/rmenu.rb', line 212

def repaint # menu.repaint
  return if @items.nil? or @items.empty?
  $log.debug "menu repaint: #{text} row #{@row} col #{@col}  " 
  if !@parent.is_a? RubyCurses::MenuBar 
    @parent.window.printstring( @row, 0, "|%-*s>|" % [@width-1, text], $reversecolor)
    @parent.window.refresh
  end
  if @window.nil?
    #create_window
  else
    @window.show
    select_item 0
    @window.refresh
  end
end

#select_item(ix0) ⇒ Object

recursive if given one not enabled goes to next enabled



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rbcurse/rmenu.rb', line 229

def select_item ix0
  return if @items.nil? or @items.empty?
   $log.debug "insdie select  item :  #{ix0} active: #{@active_index}" 
  if !@active_index.nil?
    @items[@active_index].on_leave 
  end
  previtem = @active_index
  @active_index = ix0
  if @items[ix0].enabled
    @items[ix0].on_enter
  else
    $log.debug "insdie sele nxt item ENABLED FALSE :  #{ix0}" 
    if @active_index > previtem
      select_next_item
    else
      select_prev_item
    end
  end
  @window.refresh
end

#select_next_itemObject



249
250
251
252
253
254
255
256
257
258
# File 'lib/rbcurse/rmenu.rb', line 249

def select_next_item
  return if @items.nil? or @items.empty?
   $log.debug "insdie sele nxt item :  #{@active_index}" 
  @active_index = -1 if @active_index.nil?
  if @active_index < @items.length-1
    select_item @active_index + 1
  else
  #  select_item 0
  end
end

#select_prev_itemObject



259
260
261
262
263
264
265
266
267
# File 'lib/rbcurse/rmenu.rb', line 259

def select_prev_item
  return if @items.nil? or @items.empty?
   $log.debug "insdie sele prv item :  #{@active_index}" 
  if @active_index > 0
    select_item @active_index - 1
  else
  #select_item @items.length-1
  end
end

#showObject

menu



431
432
433
434
435
436
437
438
# File 'lib/rbcurse/rmenu.rb', line 431

def show # menu.show
  $log.debug "show (menu) : #{@text} "
  if @window.nil?
    create_window
  end
    @window.show 
    select_item 0
end

#to_sObject



167
168
169
# File 'lib/rbcurse/rmenu.rb', line 167

def to_s
  @text
end