Class: CDK::RADIO

Inherits:
SCROLLER show all
Defined in:
lib/cdk/components/radio.rb

Instance Attribute Summary

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

Instance Method Summary collapse

Methods inherited from SCROLLER

#KEY_DOWN, #KEY_END, #KEY_HOME, #KEY_LEFT, #KEY_NPAGE, #KEY_PPAGE, #KEY_RIGHT, #KEY_UP, #maxViewSize, #setPosition, #setViewSize

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, #drawTitle, #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

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, xplace, yplace, splace, height, width, title, list, list_size, choice_char, def_item, highlight, box, shadow) ⇒ RADIO

Returns a new instance of RADIO.



5
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cdk/components/radio.rb', line 5

def initialize(cdkscreen, xplace, yplace, splace, height, width, title,
    list, list_size, choice_char, def_item, highlight, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = width
  box_height = height
  widest_item = 0

  bindings = {
    CDK::BACKCHAR => Ncurses::KEY_PPAGE,
    CDK::FORCHAR  => Ncurses::KEY_NPAGE,
    'g'           => Ncurses::KEY_HOME,
    '1'           => Ncurses::KEY_HOME,
    'G'           => Ncurses::KEY_END,
    '<'           => Ncurses::KEY_HOME,
    '>'           => Ncurses::KEY_END,
  }
  
  self.setBox(box)

  # If the height is a negative value, height will be ROWS-height,
  # otherwise the height will be the given height.
  box_height = CDK.setWidgetDimension(parent_height, height, 0)

  # If the width is a negative value, the width will be COLS-width,
  # otherwise the width will be the given width.
  box_width = CDK.setWidgetDimension(parent_width, width, 5)

  box_width = self.setTitle(title, box_width)

  # Set the box height.
  if @title_lines > box_height
    box_height = @title_lines + [list_size, 8].min + 2 * @border_size
  end

  # Adjust the box width if there is a scroll bar.
  if splace == CDK::LEFT || splace == CDK::RIGHT
    box_width += 1
    @scrollbar = true
  else
    scrollbar = false
  end

  # Make sure we didn't extend beyond the dimensions of the window
  @box_width = [box_width, parent_width].min
  @box_height = [box_height, parent_height].min

  self.setViewSize(list_size)

  # Each item in the needs to be converted to chtype array
  widest_item = self.createList(list, list_size, @box_width)
  if widest_item > 0
    self.updateViewWidth(widest_item)
  elsif list_size > 0
    self.destroy
    return nil
  end

  # Rejustify the x and y positions if we need to.
  xtmp = [xplace]
  ytmp = [yplace]
  alignxy(cdkscreen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Make the radio window
  @win = Ncurses::WINDOW.new(@box_height, @box_width, ypos, xpos)

  # Is the window nil?
  if @win.nil?
    self.destroy
    return nil
  end

  # Turn on the keypad.
  @win.keypad(true)

  # Create the scrollbar window.
  if splace == CDK::RIGHT
    @scrollbar_win = @win.subwin(self.maxViewSize, 1,
        self.SCREEN_YPOS(ypos), xpos + @box_width - @border_size - 1)
  elsif splace == CDK::LEFT
    @scrollbar_win = @win.subwin(self.maxViewSize, 1,
        self.SCREEN_YPOS(ypos), self.SCREEN_XPOS(xpos))
  else
    @scrollbar_win = nil
  end

  # Set the rest of the variables
  @screen = cdkscreen
  @parent = cdkscreen.window
  @scrollbar_placement = splace
  @widest_item = widest_item
  @left_char = 0
  @selected_item = 0
  @highlight = highlight
  @choice_char = choice_char.ord
  @left_box_char = '['.ord
  @right_box_char = ']'.ord
  @def_item = def_item
  @input_window = @win
  @accepts_focus = true
  @shadow = shadow

  self.setCurrentItem(0)

  # Do we need to create the shadow?
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width + 1,
        ypos + 1, xpos + 1)
  end

  # Setup the key bindings
  bindings.each do |from, to|
    self.bind(:RADIO, from, :getc, to)
  end

  cdkscreen.register(:RADIO, self)
end

Instance Method Details

#activate(actions) ⇒ Object

This actually manages the radio widget.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cdk/components/radio.rb', line 137

def activate(actions)
  # Draw the radio list.
  self.draw(@box)

  if actions.nil? || actions.size == 0
    while true
      self.fixCursorPosition
      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|
      ret = self.inject(action)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  end

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

#AvailableWidthObject

Determine how many characters we can shift to the right before all the items have been scrolled off the screen.



510
511
512
# File 'lib/cdk/components/radio.rb', line 510

def AvailableWidth
  @box_width - 2 * @border_size - 3
end

#createList(list, list_size, box_width) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/cdk/components/radio.rb', line 472

def createList(list, list_size, box_width)
  status = false
  widest_item = 0

  if list_size >= 0
    new_list = []
    new_len = []
    new_pos = []

    # Each item in the needs to be converted to chtype array
    status = true
    box_width -= 2 + @border_size
    (0...list_size).each do |j|
      lentmp = []
      postmp = []
      new_list << char2Chtype(list[j], lentmp, postmp)
      new_len << lentmp[0]
      new_pos << postmp[0]
      if new_list[j].nil? || new_list[j].size == 0
        status = false
        break
      end
      new_pos[j] = justify_string(box_width, new_len[j], new_pos[j]) + 3
      widest_item = [widest_item, new_len[j]].max
    end
    if status
      self.destroyInfo
      @item = new_list
      @item_len = new_len
      @item_pos = new_pos
    end
  end

  return (if status then widest_item else 0 end)
end

#destroyObject

This function destroys the radio widget.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/cdk/components/radio.rb', line 347

def destroy
  self.cleanTitle
  self.destroyInfo

  # Clean up the windows.
  CDK.deleteCursesWindow(@scrollbar_win)
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  # Clean up the key bindings.
  self.cleanBindings(:RADIO)

  # Unregister this object.
  CDK::SCREEN.unregister(:RADIO, self)
end

#destroyInfoObject



342
343
344
# File 'lib/cdk/components/radio.rb', line 342

def destroyInfo
  @item = ''
end

#draw(box) ⇒ Object

This function draws the radio widget.



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cdk/components/radio.rb', line 255

def draw(box)
  # Do we need to draw in the shadow?
  if !(@shadow_win.nil?)
    Draw.drawShadow(@shadow_win)
  end

  self.drawTitle(@win)

  # Draw in the radio list.
  self.drawList(@box)
end

#drawList(box) ⇒ Object

This redraws the radio list.



268
269
270
271
272
273
274
275
276
277
278
279
280
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/cdk/components/radio.rb', line 268

def drawList(box)
  scrollbar_adj = if @scrollbar_placement == CDK::LEFT then 1 else 0 end
  screen_pos = 0

  # Draw the list
  (0...@view_size).each do |j|
    k = j + @current_top
    if k < @list_size
      xpos = self.SCREEN_XPOS(0)
      ypos = self.SCREEN_YPOS(j)

      screen_pos = self.SCREENPOS(k, scrollbar_adj)

      # Draw the empty string.
      Draw.writeBlanks(@win, xpos, ypos, CDK::HORIZONTAL, 0,
          @box_width - @border_size)

      # Draw the line.
      Draw.writeChtype(@win,
          if screen_pos >= 0 then screen_pos else 1 end,
          ypos, @item[k], CDK::HORIZONTAL,
          if screen_pos >= 0 then 0 else 1 - screen_pos end,
          @item_len[k])

      # Draw the selected choice
      xpos += scrollbar_adj
      @win.mvwaddch(ypos, xpos, @left_box_char)
      @win.mvwaddch(ypos, xpos + 1,
          if k == @selected_item then @choice_char else ' '.ord end)
      @win.mvwaddch(ypos, xpos + 2, @right_box_char)
    end
  end

  # Highlight the current item
  if @has_focus
    k = @current_item
    if k < @list_size
      screen_pos = self.SCREENPOS(k, scrollbar_adj)
      ypos = self.SCREEN_YPOS(@current_high)

      Draw.writeChtypeAttrib(@win,
          if screen_pos >= 0 then screen_pos else 1 + scrollbar_adj end,
          ypos, @item[k], @highlight, CDK::HORIZONTAL,
          if screen_pos >= 0 then 0 else 1 - screen_pos end,
          @item_len[k])
    end
  end

  if @scrollbar
    @toggle_pos = (@current_item * @step).floor
    @toggle_pos = [@toggle_pos, @scrollbar_win.getmaxy - 1].min

    @scrollbar_win.mvwvline(0, 0, Ncurses::ACS_CKBOARD,
        @scrollbar_win.getmaxy)
    @scrollbar_win.mvwvline(@toggle_pos, 0, ' '.ord | Ncurses::A_REVERSE,
        @toggle_size)
  end

  # Box it if needed.
  if box
    Draw.drawObjBox(@win, self)
  end

  self.fixCursorPosition
end

#eraseObject

This function erases the radio widget



364
365
366
367
368
369
# File 'lib/cdk/components/radio.rb', line 364

def erase
  if self.validCDKObject
    CDK.eraseCursesWindow(@win)
    CDK.eraseCursesWindow(@shadow_win)
  end
end

#fixCursorPositionObject

Put the cursor on the currently-selected item.



127
128
129
130
131
132
133
134
# File 'lib/cdk/components/radio.rb', line 127

def fixCursorPosition
  scrollbar_adj = if @scrollbar_placement == CDK::LEFT then 1 else 0 end
  ypos = self.SCREEN_YPOS(@current_item - @current_top)
  xpos = self.SCREEN_XPOS(0) + scrollbar_adj

  @input_window.wmove(ypos, xpos)
  wrefresh(@input_window)
end

#focusObject



464
465
466
# File 'lib/cdk/components/radio.rb', line 464

def focus
  self.drawList(@box)
end

#getChoiceCharacterObject



421
422
423
# File 'lib/cdk/components/radio.rb', line 421

def getChoiceCharacter
  return @choice_char
end

#getCurrentItemObject



451
452
453
# File 'lib/cdk/components/radio.rb', line 451

def getCurrentItem
  return @current_item
end

#getHighlightObject



412
413
414
# File 'lib/cdk/components/radio.rb', line 412

def getHighlight
  return @highlight
end

#getItems(list) ⇒ Object



400
401
402
403
404
405
# File 'lib/cdk/components/radio.rb', line 400

def getItems(list)
  (0...@list_size).each do |j|
    list << chtype2Char(@item[j])
  end
  return @list_size
end

#getLeftBraceObject



431
432
433
# File 'lib/cdk/components/radio.rb', line 431

def getLeftBrace
  return @left_box_char
end

#getRightBraceObject



441
442
443
# File 'lib/cdk/components/radio.rb', line 441

def getRightBrace
  return @right_box_char
end

#getSelectedItemObject



460
461
462
# File 'lib/cdk/components/radio.rb', line 460

def getSelectedItem
  return @selected_item
end

#inject(input) ⇒ Object

This injects a single character into the widget.



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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
# File 'lib/cdk/components/radio.rb', line 167

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

  # Set the exit type
  self.setExitType(0)

  # Draw the widget list
  self.drawList(@box)

  # 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(:RADIO, self,
        @pre_process_data, input)
  end

  # Should we continue?
  if pp_return != 0
    # Check for a predefined key binding.
    if self.checkBind(:RADIO, input)
      @complete = true
    else
      case input
      when Ncurses::KEY_UP
        self.KEY_UP
      when Ncurses::KEY_DOWN
        self.KEY_DOWN
      when Ncurses::KEY_RIGHT
        self.KEY_RIGHT
      when Ncurses::KEY_LEFT
        self.KEY_LEFT
      when Ncurses::KEY_PPAGE
        self.KEY_PPAGE
      when Ncurses::KEY_NPAGE
        self.KEY_NPAGE
      when Ncurses::KEY_HOME
        self.KEY_HOME
      when Ncurses::KEY_END
        self.KEY_END
      when '$'.ord
        @left_char = @max_left_char
      when '|'.ord
        @left_char = 0
      when ' '.ord
        @selected_item = @current_item
      when CDK::KEY_ESC
        self.setExitType(input)
        ret = -1
        @complete = true
      when Ncurses::ERR
        self.setExitType(input)
        @complete = true
      when CDK::KEY_TAB, CDK::KEY_RETURN, Ncurses::KEY_ENTER
        self.setExitType(input)
        ret = @selected_item
        @complete = true
      when CDK::REFRESH
        @screen.erase
        @screen.refresh
      end
    end

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

  if !@complete
    self.drawList(@box)
    self.setExitType(0)
  end

  self.fixCursorPosition
  @return_data = ret
  return ret
end

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

This moves the radio field to the given location.



248
249
250
251
252
# File 'lib/cdk/components/radio.rb', line 248

def move(xplace, yplace, relative, refresh_flag)
  windows = [@win, @scrollbar_win, @shadow_win]
  self.move_specific(xplace, yplace, relative, refresh_flag,
      windows, subwidgets)
end

#object_typeObject



529
530
531
# File 'lib/cdk/components/radio.rb', line 529

def object_type
  :RADIO
end

#SCREENPOS(n, scrollbar_adj) ⇒ Object



525
526
527
# File 'lib/cdk/components/radio.rb', line 525

def SCREENPOS(n, scrollbar_adj)
  @item_pos[n] - @left_char + scrollbar_adj + @border_size
end

#set(highlight, choice_char, box) ⇒ Object

This sets various attributes of the radio list.



372
373
374
375
376
# File 'lib/cdk/components/radio.rb', line 372

def set(highlight, choice_char, box)
  self.setHighlight(highlight)
  self.setChoiceCHaracter(choice_char)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



335
336
337
338
339
340
# File 'lib/cdk/components/radio.rb', line 335

def setBKattr(attrib)
  @win.wbkgd(attrib)
  unless @scrollbar_win.nil?
    @scrollbar_win.wbkgd(attrib)
  end
end

#setChoiceCharacter(character) ⇒ Object

This sets the character to use when selecting na item in the list.



417
418
419
# File 'lib/cdk/components/radio.rb', line 417

def setChoiceCharacter(character)
  @choice_char = character
end

#setCurrentItem(item) ⇒ Object

This sets the current highlighted item of the widget



446
447
448
449
# File 'lib/cdk/components/radio.rb', line 446

def setCurrentItem(item)
  self.setPosition(item)
  @selected_item = item
end

#setHighlight(highlight) ⇒ Object

This sets the highlight bar of the radio list.



408
409
410
# File 'lib/cdk/components/radio.rb', line 408

def setHighlight(highlight)
  @highlight = highlight
end

#setItems(list, list_size) ⇒ Object

This sets the radio list items.



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/cdk/components/radio.rb', line 379

def setItems(list, list_size)
  widest_item = self.createList(list, list_size, @box_width)
  if widest_item <= 0
    return
  end

  # Clean up the display.
  (0...@view_size).each do |j|
    Draw.writeBlanks(@win, self.SCREEN_XPOS(0), self.SCREEN_YPOS(j),
        CDK::HORIZONTAL, 0, @box_width - @border_size)
  end

  self.setViewSize(list_size)

  self.setCurrentItem(0)
  @left_char = 0
  @selected_item = 0

  self.updateViewWidth(widest_item)
end

#setLeftBrace(character) ⇒ Object

This sets the character to use to drw the left side of the choice box on the list



427
428
429
# File 'lib/cdk/components/radio.rb', line 427

def setLeftBrace(character)
  @left_box_char = character
end

#setRightBrace(character) ⇒ Object

This sets the character to use to draw the right side of the choice box on the list



437
438
439
# File 'lib/cdk/components/radio.rb', line 437

def setRightBrace(character)
  @right_box_char = character
end

#setSelectedItem(item) ⇒ Object

This sets the selected item of the widget



456
457
458
# File 'lib/cdk/components/radio.rb', line 456

def setSelectedItem(item)
  @selected_item = item
end

#unfocusObject



468
469
470
# File 'lib/cdk/components/radio.rb', line 468

def unfocus
  self.drawList(@box)
end

#updateViewWidth(widest) ⇒ Object



514
515
516
517
518
519
# File 'lib/cdk/components/radio.rb', line 514

def updateViewWidth(widest)
  @max_left_char = if @box_width > widest
                   then 0
                   else widest - self.AvailableWidth
                   end
end

#WidestItemObject



521
522
523
# File 'lib/cdk/components/radio.rb', line 521

def WidestItem
  @max_left_char + self.AvailableWidth
end