Class: PPCurses::RadioMenu

Inherits:
BaseMenu show all
Defined in:
lib/ppcurses/menu/RadioMenu.rb

Overview

noinspection RubyResolve

Instance Attribute Summary

Attributes inherited from BaseMenu

#menu_items, #selection, #side_wall_char, #top_bot_wall_char

Instance Method Summary collapse

Methods inherited from BaseMenu

#build_menu_items, #close, #create_window, #find_max_menu_width, #hide, #set_origin, #set_sub_menu

Constructor Details

#initialize(menu_items, action_items = nil) ⇒ RadioMenu

TODO - duplicate code from Menu …



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ppcurses/menu/RadioMenu.rb', line 6

def initialize( menu_items, action_items=nil )
  @items = Array.new
  @actions = Array.new

  @menu_length = 0

   menu_items.each do |item|
     @items.push item
      @menu_length += item.length + 5
   end

  @selection = 0

  unless action_items.nil?

    action_items.each do |item|
      @actions.push item      
    end
  end 
 
  w_width = @menu_length + 4

  @win = Window.new(3, w_width ,0, (Curses.cols - w_width) / 2)

  @win.timeout=-1
  # Enables reading arrow keys in getch 
  @win.keypad(true)
end

Instance Method Details

#handle_menu_selection(c) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ppcurses/menu/RadioMenu.rb', line 70

def handle_menu_selection(c)
  n_choices = @items.length

  if c == KEY_RIGHT
    if @selection < n_choices - 1 then @selection += 1 else @selection = 0 end
    self.show
  end

  if c == KEY_LEFT
    if @selection > 0 then @selection -= 1 else @selection = n_choices-1 end
    self.show
  end

  if c == ENTER then
    unless @actions.nil?
      @actions[@selection].execute
      self.show
    end
  end
end


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ppcurses/menu/RadioMenu.rb', line 52

def menu_selection

  while 1
    c = @win.getch

    self.handle_menu_selection(c)

    if c == ESCAPE
      @win.clear
      @win.refresh
      break
    end

  end 

end

#selected_menu_nameObject



92
93
94
# File 'lib/ppcurses/menu/RadioMenu.rb', line 92

def selected_menu_name
  @items[@selection]
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ppcurses/menu/RadioMenu.rb', line 35

def show
  @win.box('|', '-')
  y = 1
  x = 2

  @win.setpos(y, x)

  for i in 0...@items.length
    @win.addstr( @items[i] )
    if @selection == i then @win.addstr(' [*] ') else @win.addstr(' [ ] ') end
  end

 @win.refresh

end