Class: RETerm::Menu

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/reterm/menu.rb

Overview

Graphical menu rendered on screen

TODO: look into working this into std component/activation subsystem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventDispatcher

#dispatch, #handle

Constructor Details

#initialize(items = {}) ⇒ Menu

Menus should be instantiated with a hash of label/value pairs to assign to menu



12
13
14
15
16
17
18
19
# File 'lib/reterm/menu.rb', line 12

def initialize(items={})
  @items = items
  @menu  = Ncurses::Menu.new_menu(items.collect { |k,v|
             Ncurses::Menu.new_item(k.to_s, v.to_s)
           })

  @menu.menu_opts_off(Ncurses::Menu::O_SHOWDESC)
end

Instance Attribute Details

#windowObject

Returns the value of attribute window.



8
9
10
# File 'lib/reterm/menu.rb', line 8

def window
  @window
end

Instance Method Details

#attach_to(win) ⇒ Object

Attach menu to specified window



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/reterm/menu.rb', line 32

def attach_to(win)
  @menu.set_menu_win(win.win)
  win.component = self

  @child_win = win.create_child :rows => rows,
                                :cols => cols,
                                :x => 1, :y => 1
  @menu.set_menu_sub(@child_win.win)
  @menu.post_menu

  self
end

#colsObject

Return number of cols in menu



27
28
29
# File 'lib/reterm/menu.rb', line 27

def cols
  @cols ||= @items.keys.max { |k| k.size }.size + 3
end

#detachObject

Destroy menu subsystem



46
47
48
# File 'lib/reterm/menu.rb', line 46

def detach
  @menu.unpost_menu
end

#drive(i) ⇒ Object

Drive menu from input



76
77
78
79
# File 'lib/reterm/menu.rb', line 76

def drive(i)
  i = driver_map[i] if i.is_a?(Symbol)
  Ncurses::Menu.menu_driver(@menu, i)
end

#driver_mapObject

Static map of menu drivers to internal representations.

XXX defined here (as opposed to a const) since these consts won’t be available until after init_scr



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/reterm/menu.rb', line 57

def driver_map
  @driver_map ||= {
    :left          => Ncurses::Menu::REQ_LEFT_ITEM,
    :right         => Ncurses::Menu::REQ_RIGHT_ITEM,
    :up            => Ncurses::Menu::REQ_UP_ITEM,
    :down          => Ncurses::Menu::REQ_DOWN_ITEM,
    :scr_up_line   => Ncurses::Menu::REQ_SCR_ULINE,
    :scr_down_line => Ncurses::Menu::REQ_SCR_DLINE,
    :scr_down_page => Ncurses::Menu::REQ_SCR_DPAGE,
    :scr_up_page   => Ncurses::Menu::REQ_SCR_UPAGE,
    :first         => Ncurses::Menu::REQ_FIRST_ITEM,
    :last          => Ncurses::Menu::REQ_LAST_ITEM,
    :next          => Ncurses::Menu::REQ_NEXT_ITEM,
    :prev          => Ncurses::Menu::REQ_PREV_ITEM,
    :toggle        => Ncurses::Menu::REQ_TOGGLE_ITEM
  }
end

#finalize!Object



50
51
# File 'lib/reterm/menu.rb', line 50

def finalize!
end

#rowsObject

Return number of rows in menu



22
23
24
# File 'lib/reterm/menu.rb', line 22

def rows
  @items.size
end