Class: CDK::BUTTONBOX

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

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

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

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, x_pos, y_pos, height, width, title, rows, cols, buttons, button_count, highlight, box, shadow) ⇒ BUTTONBOX

Returns a new instance of BUTTONBOX.



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
# File 'lib/cdk/components/buttonbox.rb', line 7

def initialize(cdkscreen, x_pos, y_pos, height, width, title, rows, cols,
    buttons, button_count, highlight, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  col_width = 0
  current_button = 0
  @button = []
  @button_len = []
  @button_pos = []
  @column_widths = []

  if button_count <= 0
    self.destroy
    return nil
  end

  self.setBox(box)

  # Set some default values for the widget.
  @row_adjust = 0
  @col_adjust = 0

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

  # 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, 0)

  box_width = self.setTitle(title, box_width)

  # Translate the buttons string to a chtype array
  (0...button_count).each do |x|
    button_len = []
    @button << char2Chtype(buttons[x], button_len ,[])
    @button_len << button_len[0]
  end

  # Set the button positions.
  (0...cols).each do |x|
    max_col_width = -2**31

    # Look for the widest item in this column.
    (0...rows).each do |y|
      if current_button < button_count
        max_col_width = [@button_len[current_button], max_col_width].max
        current_button += 1
      end
    end

    # Keep the maximum column width for this column.
    @column_widths << max_col_width
    col_width += max_col_width
  end
  box_width += 1

  # 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

  # Now we have to readjust the x and y positions
  xtmp = [x_pos]
  ytmp = [y_pos]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Set up the buttonbox box attributes.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  @shadow_win = nil
  @button_count = button_count
  @current_button = 0
  @rows = rows
  @cols = [button_count, cols].min
  @box_height = box_height
  @box_width = box_width
  @highlight = highlight
  @accepts_focus = true
  @input_window = @win
  @shadow = shadow
  @button_attrib = Ncurses::A_NORMAL

  # Set up the row adjustment.
  if box_height - rows - @title_lines > 0
    @row_adjust = (box_height - rows - @title_lines) / @rows
  end

  # Set the col adjustment
  if box_width - col_width > 0
    @col_adjust = ((box_width - col_width) / @cols) - 1
  end

  # If we couldn't create the window, we should return a null value.
  if @win.nil?
    self.destroy
    return nil
  end
  @win.keypad(true)

  # Was there a shadow?
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

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

Instance Attribute Details

#current_buttonObject (readonly)

Returns the value of attribute current_button.



5
6
7
# File 'lib/cdk/components/buttonbox.rb', line 5

def current_button
  @current_button
end

Instance Method Details

#activate(actions) ⇒ Object

This activates the widget.



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
146
147
148
# File 'lib/cdk/components/buttonbox.rb', line 121

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

  if actions.nil? || actions.size == 0
    while true
      input = self.getch([])

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

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

#destroyObject

This destroys the widget



313
314
315
316
317
318
319
320
321
322
# File 'lib/cdk/components/buttonbox.rb', line 313

def destroy
  self.cleanTitle

  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  self.cleanBindings(:BUTTONBOX)

  CDK::SCREEN.unregister(:BUTTONBOX, self)
end

#draw(box) ⇒ Object

This draws the buttonbox box widget.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/cdk/components/buttonbox.rb', line 250

def draw(box)
  # Is there a shadow?
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end

  # Box the widget if they asked.
  if box
    Draw.drawObjBox(@win, self)
  end

  # Draw in the title if there is one.
  self.drawTitle(@win)

  # Draw in the buttons.
  self.drawButtons
end

#drawButtonsObject

This draws the buttons on the button box widget.



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
# File 'lib/cdk/components/buttonbox.rb', line 269

def drawButtons
  row = @title_lines + 1
  col = @col_adjust / 2
  current_button = 0
  cur_row = -1
  cur_col = -1

  # Draw the buttons.
  while current_button < @button_count
    (0...@cols).each do |x|
      row = @title_lines + @border_size

      (0...@rows).each do |y|
        attr = @button_attrib
        if current_button == @current_button
          attr = @highlight
          cur_row = row
          cur_col = col
        end
        Draw.writeChtypeAttrib(@win, col, row,
            @button[current_button], attr, CDK::HORIZONTAL, 0,
            @button_len[current_button])
        row += (1 + @row_adjust)
        current_button += 1
      end
      col += @column_widths[x] + @col_adjust + @border_size
    end
  end

  if cur_row >= 0 && cur_col >= 0
    @win.wmove(cur_row, cur_col)
  end
  wrefresh
end

#eraseObject

This erases the buttonbox box from the screen.



305
306
307
308
309
310
# File 'lib/cdk/components/buttonbox.rb', line 305

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

#focusObject



338
339
340
# File 'lib/cdk/components/buttonbox.rb', line 338

def focus
  self.draw(@box)
end

#getButtonCountObject



334
335
336
# File 'lib/cdk/components/buttonbox.rb', line 334

def getButtonCount
  @button_count
end

#getCurrentButtonObject



330
331
332
# File 'lib/cdk/components/buttonbox.rb', line 330

def getCurrentButton
  @current_button
end

#getHighlightObject



240
241
242
# File 'lib/cdk/components/buttonbox.rb', line 240

def getHighlight
  return @highlight
end

#inject(input) ⇒ Object

This injects a single character into the widget.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
# File 'lib/cdk/components/buttonbox.rb', line 151

def inject(input)
  first_button = 0
  last_button = @button_count - 1
  pp_return = 1
  ret = -1
  @complete = false

  # Set the exit type
  self.setExitType(0)

  unless @pre_process_func.nil?
    pp_return = @pre_process_func.call(:BUTTONBOX, self,
        @pre_process_data, input)
  end

  # Should we continue?
  if pp_return != 0
    # Check for a key binding.
    if self.checkBind(:BUTTONBOX, input)
      @complete = true
    else
      case input
      when Ncurses::KEY_LEFT, Ncurses::KEY_BTAB, Ncurses::KEY_BACKSPACE
        if @current_button - @rows < first_button
          @current_button = last_button
        else
          @current_button -= @rows
        end
      when Ncurses::KEY_RIGHT, CDK::KEY_TAB, ' '.ord
        if @current_button + @rows > last_button
          @current_button = first_button
        else
          @current_button += @rows
        end
      when Ncurses::KEY_UP
        if @current_button -1 < first_button
          @current_button = last_button
        else
          @current_button -= 1
        end
      when Ncurses::KEY_DOWN
        if @current_button + 1 > last_button
          @current_button = first_button
        else
          @current_button += 1
        end
      when CDK::REFRESH
        @screen.erase
        @screen.refresh
      when CDK::KEY_ESC
        self.setExitType(input)
        @complete = true
      when Ncurses::ERR
        self.setExitType(input)
        @complete = true
      when CDK::KEY_RETURN, Ncurses::KEY_ENTER
        self.setExitType(input)
        ret = @current_button
        @complete = true
      end
    end

    if !@complete && !(@post_process_func.nil?)
      @post_process_func.call(:BUTTONBOX, self, @post_process_data,
          input)
    end

  end
    
  unless @complete
    self.drawButtons
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#object_typeObject



346
347
348
# File 'lib/cdk/components/buttonbox.rb', line 346

def object_type
  :BUTTONBOX
end

#positionObject



350
351
352
# File 'lib/cdk/components/buttonbox.rb', line 350

def position
  super(@win)
end

#set(highlight, box) ⇒ Object

This sets multiple attributes of the widget.



230
231
232
233
# File 'lib/cdk/components/buttonbox.rb', line 230

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

#setBKattr(attrib) ⇒ Object

This sets th background attribute of the widget.



245
246
247
# File 'lib/cdk/components/buttonbox.rb', line 245

def setBKattr(attrib)
  @win.wbkgd(attrib)
end

#setCurrentButton(button) ⇒ Object



324
325
326
327
328
# File 'lib/cdk/components/buttonbox.rb', line 324

def setCurrentButton(button)
  if button >= 0 && button < @button_count
    @current_button = button
  end
end

#setHighlight(highlight) ⇒ Object

This sets the highlight attribute for the buttonboxes



236
237
238
# File 'lib/cdk/components/buttonbox.rb', line 236

def setHighlight(highlight)
  @highlight = highlight
end

#unfocusObject



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

def unfocus
  self.draw(@box)
end