class Canis::MenuBar

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

Attributes

_object_created[RW]
active_index[RW]
bgcolor[RW]
color[RW]
items[R]
panel[R]
selected[R]
state[RW]
text[R]
toggle_key[RW]
visible[RW]
window[R]

Public Class Methods

new(&block) click to toggle source
# File lib/canis/core/widgets/rmenu.rb, line 681
def initialize &block
  @window = nil
  @text = "menubar"
  @items = []
  init_vars
  @visible = false
  @cols = Ncurses.COLS-1
  instance_eval &block if block_given?
end

Public Instance Methods

<<(menu)
Alias for: add
add(menu) click to toggle source

add a precreated menu, returning self

# File lib/canis/core/widgets/rmenu.rb, line 700
def add menu
  @items << menu
  return self
end
Also aliased as: <<
create_window_menubar() click to toggle source
# File lib/canis/core/widgets/rmenu.rb, line 886
def create_window_menubar
  @layout = { :height => 1, :width => 0, :top => 0, :left => 0 } 
  $log.debug "create window menu bar "
  @win = Canis::Window.new(@layout)
  @win.name = "WINDOW:menubar"
  @window = @win
  # hack since we put all windows in there, and this should not be coming after Root.
  # This should not have been a window in the first place.
  $global_windows.delete @win
  att = get_attrib @attr
  @win.bkgd(Ncurses.COLOR_PAIR(5)); # <---- FIXME
  len = @window.width
  len = Ncurses.COLS-0 if len == 0

  # 2016-01-14 - replacing 1 with space since junk is showing up in some cases.
  space_char = " ".codepoints.first

  # print a bar across the screen , which hopefully will not go blank in some terms
  @window.attron(Ncurses.COLOR_PAIR(@color_pair) | att)
  #@window.mvhline(0, 0, 1, len)
  @window.mvhline(0, 0, space_char, len)
  @window.attroff(Ncurses.COLOR_PAIR(@color_pair) | att)
  @panel = @win.panel
  return @window
end
current_menu() click to toggle source

returns current_menu

# File lib/canis/core/widgets/rmenu.rb, line 824
def current_menu
  @items[@active_index]
end
destroy() click to toggle source
# File lib/canis/core/widgets/rmenu.rb, line 911
def destroy
  $log.debug "DESTRY menubar #{@keep_visible} "

  # when we close, but keep visible, we don't want menu to still be hightlighted
  # added on 2011-12-12
  menu = @items[@active_index]
  menu.on_leave # hide its window, if open

  @items.each do |item|
    item.destroy
  end
  $log.debug "  DESTROY finished with items "
  # TODO the current menu should not be highlighted
  # FIXME even here i think underlying windows need to be repainted.
  #return if @keep_visible
  if @keep_visible
    Window.refresh_all
    return
  end
  @visible = false
  # replacing next 3 lines with destroy. 2014-05-12 - 17:07 CANIS
  #panel = @window.panel
  #Ncurses::Panel.del_panel(panel.pointer) if !panel.nil?
  #@window.delwin if !@window.nil?
  $log.debug "  CALLING WINDOW DESTROY from menubar"
  @window.destroy
  @window = nil
end
focusable() click to toggle source

is this widget focusable, always returns false

# File lib/canis/core/widgets/rmenu.rb, line 695
def focusable
  false
end
Also aliased as: focusable?
focusable?()
Alias for: focusable
handle_keys() click to toggle source

menubar LEFT, RIGHT, DOWN

# File lib/canis/core/widgets/rmenu.rb, line 764
def handle_keys
  @selected = false
  @repaint_required = true # added 2011-12-12 otherwise keeps repainting and you see a flicker
  @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

      if !@selected
        current_menu.fire
      else
        current_menu.handle_key ch
      end
        
      @selected = true
    when KEY_ENTER, 10, 13, 32
      @selected = true
      ret = current_menu.handle_key ch
      #break; ## 2008-12-29 18:00  This will close after firing anything
      break if ret == :CLOSE
    when KEY_UP
      current_menu.handle_key ch
    when KEY_LEFT
      ret = current_menu.handle_key ch
      prev_menu if ret == :UNHANDLED
    when KEY_RIGHT
      ret = current_menu.handle_key ch
      next_menu if ret == :UNHANDLED
    when ?\C-g.getbyte(0) # abort
      throw :menubarclose
    else
      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
    @repaint_required = false
    destroy  # Note that we destroy the menu bar upon exit
  end
end
hide() click to toggle source
# File lib/canis/core/widgets/rmenu.rb, line 846
def hide
  @visible = false
  @window.hide if !@window.nil? # seems to cause auto-firing when we resume toggle 2011-09-26
end
init_vars() click to toggle source
# File lib/canis/core/widgets/rmenu.rb, line 690
def init_vars
  @active_index = 0
  @repaint_required = true
end
keep_visible(flag=nil) click to toggle source

keep the menu bar visible at all times. If not, then it appears only on using the toggle key. In any case, control only goes to the menubar when you use the toggle key, so it is best NOT to keep it visible.

# File lib/canis/core/widgets/rmenu.rb, line 757
def keep_visible flag=nil
  return @keep_visible unless flag
  @keep_visible = flag
  @visible = flag
  self
end
menu(text, &block) click to toggle source

add a menu through the block, this would happen through instance eval 2010-09-10 12:07 added while simplifying the interface this calls add so you get the MB back, not a ref to the menu created NOTE

next_menu() click to toggle source

move focus to next menu

# File lib/canis/core/widgets/rmenu.rb, line 717
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_menu() click to toggle source

move focus to previous menu

# File lib/canis/core/widgets/rmenu.rb, line 726
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
repaint() click to toggle source
menubar

TODO: check for menu to be flush right (only for last one). TODO: repaint only if needed

# File lib/canis/core/widgets/rmenu.rb, line 861
def repaint
  return if !@visible
  return unless @repaint_required
  @repaint_required = false
  @color_pair = get_color($reversecolor, @color, @bgcolor)
  @window ||= create_window_menubar
  #@window.printstring( 0, 0, "%-*s" % [@cols," "], @color_pair) # this becomes blank in some terms
  c = 1; r = 0;
  @items.each do |item|
    item.row = r; item.col = c; item.coffset = c; item.parent = self
    item.color = @color
    item.bgcolor = @bgcolor
    @window.printstring( r, c, " %s " % item.text, @color_pair)
    # 2011-09-26 V1.3.1 quick dirty highlighting of first menu on menubar
    # on opening since calling highlight was giving bug in parent.width
    #if c == 1
      #att =  Ncurses::A_REVERSE
      #@window.mvchgat(y=r, x=c+1, item.text.length+1, att, @color_pair, nil)
    #end
    c += (item.text.length + 2)
  end
  #@items[0].on_enter # 2011-09-25 V1.3.1  caused issues when toggling, first item fired on DOWN
  @items[0].highlight unless @keep_visible # 2011-09-26 V1.3.1   fixed to take both cases into account
  @window.wrefresh
end
set_menu(index) click to toggle source

set the given menu index as the current or active menu

after closing the active menu.

@param Integer index of menu to activate, starting 0

# File lib/canis/core/widgets/rmenu.rb, line 738
    def set_menu index
      #$log.debug "set meu: #{@active_index} #{index}"
      # first leave the existing window
      menu = @items[@active_index]
      menu.on_leave # hide its window, if open
      # now move to given menu
      @active_index = index
      menu = @items[@active_index]
      menu.on_enter #display window, if previous was displayed
      # move cursor to selected menu option on top, not inside list
      @window.wmove menu.row, menu.col
#     menu.show
#     menu.window.wrefresh # XXX we need this
    end
show() click to toggle source
# File lib/canis/core/widgets/rmenu.rb, line 850
def show
  @visible = true
  if @window.nil?
    repaint  # XXX FIXME
  else
    @window.show 
  end
end
toggle() click to toggle source

called by set_menu_bar in widget.rb (class Form).

# File lib/canis/core/widgets/rmenu.rb, line 828
def toggle
  # added keeping it visible, 2011-10-7 being tested in dbdemo
  if @keep_visible
    init_vars
    show
    @items[0].highlight
    @window.ungetch(KEY_DOWN)
    return
  end
  #@items.each { |i| $log.debug " ITEM DDD : #{i.text}" }
  @visible = !@visible
  if !@visible
    hide
  else
    init_vars
    show
  end
end