Class: RubyCurses::PMenuBar

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

Returns a new instance of PMenuBar.



549
550
551
552
553
554
555
556
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 549

def initialize &block
  @window = nil
  @active_index = 0
  @items = []
  @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.



546
547
548
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 546

def active_index
  @active_index
end

#itemsObject (readonly)

Returns the value of attribute items.



541
542
543
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 541

def items
  @items
end

#panelObject (readonly)

Returns the value of attribute panel.



543
544
545
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 543

def panel
  @panel
end

#selectedObject (readonly)

Returns the value of attribute selected.



544
545
546
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 544

def selected
  @selected
end

#stateObject

normal, selected, highlighted



547
548
549
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 547

def state
  @state
end

#toggle_keyObject

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



548
549
550
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 548

def toggle_key
  @toggle_key
end

#visibleObject

Returns the value of attribute visible.



545
546
547
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 545

def visible
  @visible
end

#windowObject (readonly)

Returns the value of attribute window.



542
543
544
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 542

def window
  @window
end

Instance Method Details

#add(menu) ⇒ Object



560
561
562
563
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 560

def add menu
  @items << menu
  return self
end

#create_windowObject



688
689
690
691
692
693
694
695
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 688

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



652
653
654
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 652

def current_menu
  @items[@active_index]
end

#destroyObject



696
697
698
699
700
701
702
703
704
705
706
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 696

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

#focusableObject



557
558
559
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 557

def focusable
  false
end

#handle_keysObject

menubar LEFT, RIGHT, DOWN



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 592

def handle_keys
  @selected = false
  @toggle_key ||= 27 # default switch off with ESC, if nothing else defined
  set_menu 0
  begin
  catch(:popupclose) 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
  destroy  # Note that we destroy the menu bar upon exit
  end
end

#hideObject



663
664
665
666
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 663

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

#next_menuObject



564
565
566
567
568
569
570
571
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 564

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



572
573
574
575
576
577
578
579
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 572

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



676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 676

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



580
581
582
583
584
585
586
587
588
589
590
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 580

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



667
668
669
670
671
672
673
674
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 667

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

#toggleObject



655
656
657
658
659
660
661
662
# File 'lib/rbcurse/extras/widgets/rpopupmenu.rb', line 655

def toggle
  @visible = !@visible
  if !@visible
    hide
  else
    show
  end
end