Method: CDK::MENU#initialize

Defined in:
lib/cdk/menu.rb

#initialize(cdkscreen, menu_list, menu_items, subsize, menu_location, menu_pos, title_attr, subtitle_attr) ⇒ MENU

Returns a new instance of MENU.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cdk/menu.rb', line 12

def initialize(cdkscreen, menu_list, menu_items, subsize,
    menu_location, menu_pos, title_attr, subtitle_attr)
  super()

  right_count = menu_items - 1
  rightloc = cdkscreen.window.getmaxx
  leftloc = 0
  xpos = cdkscreen.window.getbegx
  ypos = cdkscreen.window.getbegy
  ymax = cdkscreen.window.getmaxy
  
  # Start making a copy of the information.
  @screen = cdkscreen
  @box = false
  @accepts_focus = false
  rightcount = menu_items - 1
  @parent = cdkscreen.window
  @menu_items = menu_items
  @title_attr = title_attr
  @subtitle_attr = subtitle_attr
  @current_title = 0
  @current_subtitle = 0
  @last_selection = -1
  @menu_pos = menu_pos

  @pull_win = [nil] * menu_items
  @title_win = [nil] * menu_items
  @title = [''] * menu_items
  @title_len = [0] * menu_items
  @sublist = (1..menu_items).map {[nil] * subsize.max}.compact
  @sublist_len = (1..menu_items).map {
      [0] * subsize.max}.compact
  @subsize = [0] * menu_items


  # Create the pull down menus.
  (0...menu_items).each do |x|
    x1 = if menu_location[x] == CDK::LEFT
         then x
         else 
           rightcount -= 1
           rightcount + 1
         end
    x2 = 0
    y1 = if menu_pos == CDK::BOTTOM then ymax - 1 else 0 end
    y2 = if menu_pos == CDK::BOTTOM
         then ymax - subsize[x] - 2
         else CDK::MENU::TITLELINES
         end
    high = subsize[x] + CDK::MENU::TITLELINES

    # Limit the menu height to fit on the screen.
    if high + y2 > ymax
      high = ymax - CDK::MENU::TITLELINES
    end

    max = -1
    (CDK::MENU::TITLELINES...subsize[x]).to_a.each do |y|
      y0 = y - CDK::MENU::TITLELINES
      sublist_len = []
      @sublist[x1][y0] = CDK.char2Chtype(menu_list[x][y],
          sublist_len, [])
      @sublist_len[x1][y0] = sublist_len[0]
      max = [max, sublist_len[0]].max
    end

    if menu_location[x] == CDK::LEFT
      x2 = leftloc
    else
      x2 = (rightloc -= max + 2)
    end

    title_len = []
    @title[x1] = CDK.char2Chtype(menu_list[x][0], title_len, [])
    @title_len[x1] = title_len[0]
    @subsize[x1] = subsize[x] - CDK::MENU::TITLELINES
    @title_win[x1] = cdkscreen.window.subwin(CDK::MENU::TITLELINES,
        @title_len[x1] + 2, ypos + y1, xpos + x2)
    @pull_win[x1] = cdkscreen.window.subwin(high, max + 2,
        ypos + y2, xpos + x2)
    if @title_win[x1].nil? || @pull_win[x1].nil?
      self.destroy
      return nil
    end

    leftloc += @title_len[x] + 1
    @title_win[x1].keypad(true)
    @pull_win[x1].keypad(true)
  end
  @input_window = @title_win[@current_title]

  # Register this baby.
  cdkscreen.register(:MENU, self)
end