Class: CDK::MENU

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/components/menu.rb

Constant Summary collapse

TITLELINES =
1
MAX_MENU_ITEMS =
30
MAX_SUB_ITEMS =
98

Instance Attribute Summary collapse

Attributes included from HasTitle

#title_attrib

Attributes included from HasScreen

#is_visible, #screen, #screen_index

Attributes included from ExitConditions

#exit_type

Attributes included from Bindings

#binding_list

Attributes included from Focusable

#accepts_focus, #has_focus

Attributes included from Borders

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #border_size, #box

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CDKOBJS

#setBackgroundColor, #timeout, #validCDKObject, #validObjType

Methods included from WindowHooks

#refreshData, #saveData

Methods included from WindowInput

#getc, #getch, #setPostProcess, #setPreProcess

Methods included from HasTitle

#cleanTitle, #init_title, #setTitle

Methods included from HasScreen

#SCREEN_XPOS, #SCREEN_YPOS, #init_screen, #wrefresh

Methods included from ExitConditions

#init_exit_conditions, #resetExitType, #setExitType

Methods included from Bindings

#bind, #bindableObject, #checkBind, #cleanBindings, #init_bindings, #isBind, #unbind

Methods included from Focusable

#init_focus, #unfocus

Methods included from Borders

#getBox, #init_borders, #setBXattr, #setBox, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar

Methods included from Movement

#move_specific, #position

Methods included from Converters

#char2Chtype, #charOf, #chtype2Char, #chtype2String, #decode_attribute, #encode_attribute

Methods included from Justifications

#justify_string

Methods included from Alignments

#alignxy

Constructor Details

#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/components/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] = 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] = 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

Instance Attribute Details

#current_subtitleObject (readonly)

Returns the value of attribute current_subtitle.



9
10
11
# File 'lib/cdk/components/menu.rb', line 9

def current_subtitle
  @current_subtitle
end

#current_titleObject (readonly)

Returns the value of attribute current_title.



9
10
11
# File 'lib/cdk/components/menu.rb', line 9

def current_title
  @current_title
end

#sublistObject (readonly)

Returns the value of attribute sublist.



10
11
12
# File 'lib/cdk/components/menu.rb', line 10

def sublist
  @sublist
end

Class Method Details

.wrapped(within, limit) ⇒ Object

The “%” operator is simpler but does not handle negative values



435
436
437
438
439
440
441
442
# File 'lib/cdk/components/menu.rb', line 435

def self.wrapped(within, limit)
  if within < 0
    within = limit - 1
  elsif within >= limit
    within = 0
  end
  return within
end

Instance Method Details

#acrossSubmenus(step) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cdk/components/menu.rb', line 195

def acrossSubmenus(step)
  next_item = CDK::MENU.wrapped(@current_title + step, @menu_items)

  if next_item != @current_title
    # Erase the menu sub-window.
    self.eraseSubwin
    @screen.refresh

    # Set the values.
    @current_title = next_item
    @current_subtitle = 0

    # Draw the new menu sub-window.
    self.drawSubwin
    @input_window = @title_win[@current_title]
  end
end

#activate(actions) ⇒ Object

This activates the CDK Menu



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cdk/components/menu.rb', line 108

def activate(actions)
  ret = 0

  # Draw in the screen.
  @screen.refresh

  # Display the menu titles.
  self.draw(@box)

  # Highlight the current title and window.
  self.drawSubwin

  # If the input string is empty this is an interactive activate.
  if actions.nil? || actions.size == 0
    @input_window = @title_win[@current_title]

    # Start taking input from the keyboard.
    while true
      input = self.getch([])

      # Inject the character into the widget.
      ret = self.inject(input)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  else
    actions.each do |action|
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  end

  # Set the exit type and return.
  self.setExitType(0)
  return -1
end

#cleanUpMenuObject

Exit the menu.



420
421
422
423
424
425
426
427
# File 'lib/cdk/components/menu.rb', line 420

def cleanUpMenu
  # Erase the sub-menu.
  self.eraseSubwin
  wrefresh(@pull_win[@current_title])

  # Refresh the screen.
  @screen.refresh
end

#destroyObject

Destroy a menu widget.



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/cdk/components/menu.rb', line 357

def destroy
  # Clean up the windows
  (0...@menu_items).each do |x|
    CDK.deleteCursesWindow(@title_win[x])
    CDK.deleteCursesWindow(@pull_win[x])
  end

  # Clean the key bindings.
  self.cleanBindings(:MENU)

  # Unregister the object
  CDK::SCREEN.unregister(:MENU, self)
end

#draw(box) ⇒ Object

Draw the menu.



330
331
332
333
334
335
336
# File 'lib/cdk/components/menu.rb', line 330

def draw(box)
  # Draw in the menu titles.
  (0...@menu_items).each do |x|
    self.drawTitle(x)
    wrefresh(@title_win[x])
  end
end

#drawItem(item, offset) ⇒ Object



152
153
154
155
156
157
# File 'lib/cdk/components/menu.rb', line 152

def drawItem(item, offset)
  Draw.writeChtype(@pull_win[@current_title], 1,
      item + CDK::MENU::TITLELINES - offset,
      @sublist[@current_title][item],
      CDK::HORIZONTAL, 0, @sublist_len[@current_title][item])
end

#drawSubwinObject

Draw a menu item subwindow



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/cdk/components/menu.rb', line 281

def drawSubwin
  high = @pull_win[@current_title].getmaxy - 2
  x0 = 0
  x1 = @subsize[@current_title]

  if x1 > high
    x1 = high
  end

  if @current_subtitle >= x1
    x0 = @current_subtitle - x1 + 1
    x1 += x0
  end

  # Box the window
  @pull_win[@current_title]
  @pull_win[@current_title].box(Ncurses::ACS_VLINE, Ncurses::ACS_HLINE)
  if @menu_pos == CDK::BOTTOM
    @pull_win[@current_title].mvwaddch(@subsize[@current_title] + 1,
        0, Ncurses::ACS_LTEE)
  else
    @pull_win[@current_title].mvwaddch(0, 0, Ncurses::ACS_LTEE)
  end

  # Draw the items.
  (x0...x1).each do |x|
    self.drawItem(x, x0)
  end

  self.selectItem(@current_subtitle, x0)
  wrefresh(@pull_win[@current_title])

  # Highlight the title.
  Draw.writeChtypeAttrib(@title_win[@current_title], 0, 0,
      @title[@current_title], @title_attr, CDK::HORIZONTAL,
      0, @title_len[@current_title])
  wrefresh(@title_win[@current_title])
end

#drawTitle(item) ⇒ Object



147
148
149
150
# File 'lib/cdk/components/menu.rb', line 147

def drawTitle(item)
  Draw.writeChtype(@title_win[item], 0, 0, @title[item],
      CDK::HORIZONTAL, 0, @title_len[item])
end

#eraseObject

Erase the menu widget from the screen.



372
373
374
375
376
377
378
379
380
381
# File 'lib/cdk/components/menu.rb', line 372

def erase
  if self.validCDKObject
    (0...@menu_items).each do |x|
      @title_win[x].werase
      wrefresh(@title_win[x])
      @pull_win[x].werase
      wrefresh(@pull_win[x])
    end
  end
end

#eraseSubwinObject

Erase a menu item subwindow



321
322
323
324
325
326
327
# File 'lib/cdk/components/menu.rb', line 321

def eraseSubwin
  CDK.eraseCursesWindow(@pull_win[@current_title])

  # Redraw the sub-menu title.
  self.drawTitle(@current_title)
  wrefresh(@title_win[@current_title])
end

#focusObject



429
430
431
432
# File 'lib/cdk/components/menu.rb', line 429

def focus
  self.drawSubwin
  @input_window = @title_win[@current_title]
end

#getCurrentItem(menu_item, submenu_item) ⇒ Object



396
397
398
399
# File 'lib/cdk/components/menu.rb', line 396

def getCurrentItem(menu_item, submenu_item)
  menu_item << @current_title
  submenu_item << @current_subtitle
end

#getSubTitleHighlightObject



415
416
417
# File 'lib/cdk/components/menu.rb', line 415

def getSubTitleHighlight
  return @subtitle_attr
end

#getTitleHighlightObject



406
407
408
# File 'lib/cdk/components/menu.rb', line 406

def getTitleHighlight
  return @title_attr
end

#inject(input) ⇒ Object

Inject a character into the menu widget.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/cdk/components/menu.rb', line 214

def inject(input)
  pp_return = 1
  ret = -1
  @complete = false

  # Set the exit type.
  self.setExitType(0)

  # Check if there is a pre-process function to be called.
  unless @pre_process_func.nil?
    # Call the pre-process function.
    pp_return = @pre_process_func.call(:MENU, self,
        @pre_process_data, input)
  end

  # Should we continue?

  if pp_return != 0
    # Check for key bindings.
    if self.checkBind(:MENU, input)
      @complete = true
    else
      case input
      when Ncurses::KEY_LEFT
        self.acrossSubmenus(-1)
      when Ncurses::KEY_RIGHT, CDK::KEY_TAB
        self.acrossSubmenus(1)
      when Ncurses::KEY_UP
        self.withinSubmenu(-1)
      when Ncurses::KEY_DOWN, ' '.ord
        self.withinSubmenu(1)
      when Ncurses::KEY_ENTER, CDK::KEY_RETURN
        self.cleanUpMenu
        self.setExitType(input)
        @last_selection = @current_title * 100 + @current_subtitle
        ret = @last_selection
        @complete = true
      when CDK::KEY_ESC
        self.cleanUpMenu
        self.setExitType(input)
        @last_selection = -1
        ret = @last_selection
        @complete = true
      when Ncurses::ERR
        self.setExitType(input)
        @complete = true
      when CDK::REFRESH
        self.erase
        self.refresh
      end
    end

    # Should we call a post-process?
    if !@complete && !(@post_process_func.nil?)
      @post_process_func.call(:MENU, self, @post_process_data, input)
    end
  end

  if !@complete
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#move(xplace, yplace, relative, refresh_flag) ⇒ Object

Move the menu to the given location.



339
340
341
342
343
344
345
346
# File 'lib/cdk/components/menu.rb', line 339

def move(xplace, yplace, relative, refresh_flag)
  windows = [@screen.window]
  (0...@menu_items).each do |x|
    windows << @title_win[x]
  end
  self.move_specific(xplace, yplace, relative, refresh_flag,
      windows, [])
end

#object_typeObject



444
445
446
# File 'lib/cdk/components/menu.rb', line 444

def object_type
  :MENU
end

#selectItem(item, offset) ⇒ Object

Highlight the current sub-menu item



160
161
162
163
164
165
# File 'lib/cdk/components/menu.rb', line 160

def selectItem(item, offset)
  Draw.writeChtypeAttrib(@pull_win[@current_title], 1,
      item + CDK::MENU::TITLELINES - offset,
      @sublist[@current_title][item], @subtitle_attr,
      CDK::HORIZONTAL, 0, @sublist_len[@current_title][item])
end

#set(menu_item, submenu_item, title_highlight, subtitle_highlight) ⇒ Object



383
384
385
386
387
# File 'lib/cdk/components/menu.rb', line 383

def set(menu_item, submenu_item, title_highlight, subtitle_highlight)
  self.setCurrentItem(menu_item, submenu_item)
  self.setTitleHighlight(title_highlight)
  self.setSubTitleHighlight(subtitle_highlight)
end

#setBKattr(attrib) ⇒ Object

Set the background attribute of the widget.



349
350
351
352
353
354
# File 'lib/cdk/components/menu.rb', line 349

def setBKattr(attrib)
  (0...@menu_items).each do |x|
    @title_win[x].wbkgd(attrib)
    @pull_win[x].wbkgd(attrib)
  end
end

#setCurrentItem(menuitem, submenuitem) ⇒ Object

Set the current menu item to highlight.



390
391
392
393
394
# File 'lib/cdk/components/menu.rb', line 390

def setCurrentItem(menuitem, submenuitem)
  @current_title = CDK::MENU.wrapped(menuitem, @menu_items)
  @current_subtitle = CDK::MENU.wrapped(
      submenuitem, @subsize[@current_title])
end

#setSubTitleHighlight(highlight) ⇒ Object

Set the attribute of the sub-title.



411
412
413
# File 'lib/cdk/components/menu.rb', line 411

def setSubTitleHighlight(highlight)
  @subtitle_attr = highlight
end

#setTitleHighlight(highlight) ⇒ Object

Set the attribute of the menu titles.



402
403
404
# File 'lib/cdk/components/menu.rb', line 402

def setTitleHighlight(highlight)
  @title_attr = highlight
end

#withinSubmenu(step) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cdk/components/menu.rb', line 167

def withinSubmenu(step)
  next_item = CDK::MENU.wrapped(@current_subtitle + step,
      @subsize[@current_title])

  if next_item != @current_subtitle
    ymax = @screen.window.getmaxy

    if 1 + @pull_win[@current_title].getbegy + @subsize[@current_title] >=
        ymax
      @current_subtitle = next_item
      self.drawSubwin
    else
      # Erase the old subtitle.
      self.drawItem(@current_subtitle, 0)

      # Set the values
      @current_subtitle = next_item

      # Draw the new sub-title.
      self.selectItem(@current_subtitle, 0)

      wrefresh(@pull_win[@current_title])
    end

    @input_window = @title_win[@current_title]
  end
end