Class: RubyCurses::MenuBar

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

Overview

An application related menubar. Currently, I am adding this to a form. But should this not be application specific ? It should popup no matter which window you are on ?? XXX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ MenuBar

Returns a new instance of MenuBar.



453
454
455
456
457
458
459
460
# File 'lib/rbcurse/rmenu.rb', line 453

def initialize &block
  @window = nil
  @items = []
  init_vars
  @visible = false
  @cols = Ncurses.COLS-1
  instance_eval &block if block_given?
end

Instance Attribute Details

#active_indexObject

Returns the value of attribute active_index.



450
451
452
# File 'lib/rbcurse/rmenu.rb', line 450

def active_index
  @active_index
end

#itemsObject (readonly)

Returns the value of attribute items.



445
446
447
# File 'lib/rbcurse/rmenu.rb', line 445

def items
  @items
end

#panelObject (readonly)

Returns the value of attribute panel.



447
448
449
# File 'lib/rbcurse/rmenu.rb', line 447

def panel
  @panel
end

#selectedObject (readonly)

Returns the value of attribute selected.



448
449
450
# File 'lib/rbcurse/rmenu.rb', line 448

def selected
  @selected
end

#stateObject

normal, selected, highlighted



451
452
453
# File 'lib/rbcurse/rmenu.rb', line 451

def state
  @state
end

#toggle_keyObject

key used to popup, should be set prior to attaching to form



452
453
454
# File 'lib/rbcurse/rmenu.rb', line 452

def toggle_key
  @toggle_key
end

#visibleObject

Returns the value of attribute visible.



449
450
451
# File 'lib/rbcurse/rmenu.rb', line 449

def visible
  @visible
end

#windowObject (readonly)

Returns the value of attribute window.



446
447
448
# File 'lib/rbcurse/rmenu.rb', line 446

def window
  @window
end

Instance Method Details

#add(menu) ⇒ Object



467
468
469
470
# File 'lib/rbcurse/rmenu.rb', line 467

def add menu
  @items << menu
  return self
end

#create_windowObject



599
600
601
602
603
604
605
606
# File 'lib/rbcurse/rmenu.rb', line 599

def create_window
  @layout = { :height => 1, :width => 0, :top => 0, :left => 0 } 
  @win = VER::Window.new(@layout)
  @window = @win
  @win.bkgd(Ncurses.COLOR_PAIR(5));
  @panel = @win.panel
  return @window
end

#current_menuObject



561
562
563
# File 'lib/rbcurse/rmenu.rb', line 561

def current_menu
  @items[@active_index]
end

#destroyObject



607
608
609
610
611
612
613
614
615
616
617
# File 'lib/rbcurse/rmenu.rb', line 607

def destroy
  $log.debug "DESTRY menubar "
  @visible = false
  panel = @window.panel
  Ncurses::Panel.del_panel(panel) if !panel.nil?   
  @window.delwin if !@window.nil?
  @items.each do |item|
    item.destroy
  end
  @window = nil
end

#focusableObject



464
465
466
# File 'lib/rbcurse/rmenu.rb', line 464

def focusable
  false
end

#handle_keysObject

menubar LEFT, RIGHT, DOWN



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/rbcurse/rmenu.rb', line 499

def handle_keys
  @selected = false
  @toggle_key ||= 27 # default switch off with ESC, if nothing else defined
  set_menu 0
  begin
  catch(:menubarclose) do
  while((ch = @window.getchar()) != @toggle_key )
   $log.debug "menuubar inside handle_keys :  #{ch}"  if ch != -1
    case ch
    when -1
      next
    when KEY_DOWN
      $log.debug "insdie keyDOWN :  #{ch}" 
      if !@selected
        current_menu.fire
      else
        current_menu.handle_key ch
      end
        
      @selected = true
    when KEY_ENTER, 10, 13, 32
      @selected = true
        $log.debug " mb insdie ENTER :  #{current_menu}" 
        ret = current_menu.handle_key ch
        $log.debug "ret = #{ret}  mb insdie ENTER :  #{current_menu}" 
        #break; ## 2008-12-29 18:00  This will close after firing
        #anything
        break if ret == :CLOSE
    when KEY_UP
      $log.debug " mb insdie keyUPP :  #{ch}" 
      current_menu.handle_key ch
    when KEY_LEFT
      $log.debug " mb insdie KEYLEFT :  #{ch}" 
      ret = current_menu.handle_key ch
      prev_menu if ret == :UNHANDLED
      #display_items if @selected
    when KEY_RIGHT
      $log.debug " mb insdie KEYRIGHT :  #{ch}" 
      ret = current_menu.handle_key ch
      next_menu if ret == :UNHANDLED
    else
      $log.debug " mb insdie ELSE :  #{ch}" 
      ret = current_menu.handle_key ch
      if ret == :UNHANDLED
        Ncurses.beep 
      else
        break  # we handled a menu action, close menubar (THIS WORKS FOR MNEMONICS ONLY and always)
      end
    end
    Ncurses::Panel.update_panels();
    Ncurses.doupdate();

    @window.wrefresh
  end
  end # catch
  ensure
    #ensure is required becos one can throw a :close
    $log.debug " DESTROY IN ENSURE"
  current_menu.clear_menus #@@menus = [] # added 2009-01-23 13:21 
  destroy  # Note that we destroy the menu bar upon exit
  end
end

#hideObject



574
575
576
577
# File 'lib/rbcurse/rmenu.rb', line 574

def hide
  @visible = false
  @window.hide if !@window.nil?
end

#init_varsObject



461
462
463
# File 'lib/rbcurse/rmenu.rb', line 461

def init_vars
  @active_index = 0
end

#next_menuObject



471
472
473
474
475
476
477
478
# File 'lib/rbcurse/rmenu.rb', line 471

def next_menu
  $log.debug "next meu: #{@active_index}  " 
  if @active_index < @items.length-1
    set_menu @active_index + 1
  else
    set_menu 0
  end
end

#prev_menuObject



479
480
481
482
483
484
485
486
# File 'lib/rbcurse/rmenu.rb', line 479

def prev_menu
  $log.debug "prev meu: #{@active_index} " 
  if @active_index > 0
    set_menu @active_index-1
  else
    set_menu @items.length-1
  end
end

#repaintObject

menubar



587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/rbcurse/rmenu.rb', line 587

def repaint
  return if !@visible
  @window ||= create_window
  @window.printstring( 0, 0, "%-*s" % [@cols," "], $reversecolor)
  c = 1; r = 0;
  @items.each do |item|
    item.row = r; item.col = c; item.parent = self
    @window.printstring( r, c, " %s " % item.text, $reversecolor)
    c += (item.text.length + 2)
  end
  @window.wrefresh
end

#set_menu(index) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
# File 'lib/rbcurse/rmenu.rb', line 487

def set_menu index
  $log.debug "set meu: #{@active_index} #{index}" 
  menu = @items[@active_index]
  menu.on_leave # hide its window, if open
  @active_index = index
  menu = @items[@active_index]
  menu.on_enter #display window, if previous was displayed
  @window.wmove menu.row, menu.col
#     menu.show
#     menu.window.wrefresh # XXX we need this
end

#showObject



578
579
580
581
582
583
584
585
# File 'lib/rbcurse/rmenu.rb', line 578

def show
  @visible = true
  if @window.nil?
    repaint  # XXX FIXME
  else
    @window.show 
  end
end

#toggleObject



564
565
566
567
568
569
570
571
572
573
# File 'lib/rbcurse/rmenu.rb', line 564

def toggle
  @items.each { |i| $log.debug " ITEM DDD : #{i.text}" }
  @visible = !@visible
  if !@visible
    hide
  else
    init_vars
    show
  end
end