Class: RNDK::Buttonbox

Inherits:
Widget
  • Object
show all
Defined in:
lib/rndk/buttonbox.rb

Instance Attribute Summary collapse

Attributes inherited from Widget

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #accepts_focus, #binding_list, #border_size, #box, #exit_type, #has_focus, #is_visible, #screen, #screen_index, #supported_signals, #widget_type

Instance Method Summary collapse

Methods inherited from Widget

#Screen_XPOS, #Screen_YPOS, #after_processing, #before_processing, #bind_key, #bind_signal, #bindable_widget, #clean_bindings, #clean_title, #draw_title, #get_box, #getch, #is_bound?, #move, #refresh_data, #run_key_binding, #run_signal_binding, #save_data, #setBXattr, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar, #set_box, #set_exit_type, #set_title, #unbind_key, #valid?, #valid_type?

Constructor Details

#initialize(screen, config = {}) ⇒ 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
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
146
147
# File 'lib/rndk/buttonbox.rb', line 7

def initialize(screen, config={})
  super()
  @widget_type = :buttonbox
  @supported_signals += [:before_pressing, :pressed]

  x            = 0
  y            = 0
  width        = 0
  height       = 0
  title        = "buttonbox"
  buttons      = []
  button_rows         = 0
  button_cols         = 0
  highlight    = RNDK::Color[:reverse]
  box          = true
  shadow       = false

  config.each do |key, val|
    x            = val if key == :x
    y            = val if key == :y
    width        = val if key == :width
    height       = val if key == :height
    title        = val if key == :title
    button_rows         = val if key == :button_rows
    button_cols         = val if key == :button_cols
    buttons      = val if key == :buttons
    highlight    = val if key == :highlight
    box          = val if key == :box
    shadow       = val if key == :shadow
  end

  button_count = buttons.size
  parent_width = Ncurses.getmaxx(screen.window)
  parent_height = Ncurses.getmaxy(screen.window)
  col_width = 0
  current_button = 0
  @button = []
  @button_len = []
  @button_pos = []
  @column_widths = []

  if button_count <= 0
    self.destroy
    return nil
  end

  self.set_box(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
  # BUTTON_ROWS-height, otherwise the height will be the given height.
  box_height = RNDK.set_widget_dimension(parent_height, height, button_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 = RNDK.set_widget_dimension(parent_width, width, 0)

  box_width = self.set_title(title, box_width)

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

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

    # Look for the widest item in this column.
    (0...button_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]
  ytmp = [y]
  RNDK.alignxy(screen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Set up the buttonbox box attributes.
  @screen = screen
  @parent = screen.window
  @win = Ncurses.newwin(box_height, box_width, ypos, xpos)
  @shadow_win = nil
  @button_count = button_count
  @current_button = 0
  @button_rows = button_rows
  @button_cols = [button_count, button_cols].min
  @box_height = box_height
  @box_width = box_width
  @highlight = highlight
  @accepts_focus = true
  @input_window = @win
  @shadow = shadow
  @button_color = RNDK::Color[:normal]

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

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

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

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

  # Register this baby.
  screen.register(@widget_type, self)
end

Instance Attribute Details

#current_buttonObject (readonly)

Returns the value of attribute current_button.



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

def current_button
  @current_button
end

Instance Method Details

#activate(actions = []) ⇒ Object

This activates the widget.



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

def activate(actions=[])
  # Draw the buttonbox box.
  self.draw

  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.set_exit_type(0)
  return -1
end

#destroyObject

This destroys the widget



310
311
312
313
314
315
316
317
318
319
# File 'lib/rndk/buttonbox.rb', line 310

def destroy
  self.clean_title

  RNDK.window_delete @shadow_win
  RNDK.window_delete @win

  self.clean_bindings

  @screen.unregister self
end

#drawObject

This draws the buttonbox box widget.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/rndk/buttonbox.rb', line 283

def draw
  # 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.draw_title @win

  # Draw in the buttons.
  self.draw_buttons
end

#eraseObject

This erases the buttonbox box from the screen.



302
303
304
305
306
307
# File 'lib/rndk/buttonbox.rb', line 302

def erase
  if self.valid?
    RNDK.window_erase @win
    RNDK.window_erase @shadow_win
  end
end

#focusObject



335
336
337
# File 'lib/rndk/buttonbox.rb', line 335

def focus
  self.draw
end

#getButtonCountObject



331
332
333
# File 'lib/rndk/buttonbox.rb', line 331

def getButtonCount
  @button_count
end

#getCurrentButtonObject



327
328
329
# File 'lib/rndk/buttonbox.rb', line 327

def getCurrentButton
  @current_button
end

#getHighlightObject



273
274
275
# File 'lib/rndk/buttonbox.rb', line 273

def getHighlight
  return @highlight
end

#inject(input) ⇒ Object

This injects a single character into the widget.



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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/rndk/buttonbox.rb', line 180

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

  # Set the exit type
  self.set_exit_type(0)

  keep_going = self.run_signal_binding(:before_input, input)

  # Should we continue?
  if keep_going

    # Check for a key binding.
    if self.is_bound? input
      self.run_key_binding input
      #complete = true

    else
      case input
      when Ncurses::KEY_LEFT, Ncurses::KEY_BTAB, Ncurses::KEY_BACKSPACE
        if @current_button - @button_rows < first_button
          @current_button = last_button
        else
          @current_button -= @button_rows
        end
      when Ncurses::KEY_RIGHT, RNDK::KEY_TAB, ' '.ord
        if @current_button + @button_rows > last_button
          @current_button = first_button
        else
          @current_button += @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 RNDK::REFRESH
        @screen.erase
        @screen.refresh

      when RNDK::KEY_ESC
        self.set_exit_type(input)
        complete = true

      when Ncurses::ERR
        self.set_exit_type(input)
        complete = true

      when RNDK::KEY_RETURN, Ncurses::KEY_ENTER
        keep_going = self.run_signal_binding(:before_pressing)

        if keep_going
          self.run_signal_binding(:pressed)
          self.set_exit_type input
          ret = @current_button
          complete = true
        end
      end
    end

    self.run_signal_binding(:after_input) if not complete
  end

  unless complete
    self.draw_buttons
    self.set_exit_type(0)
  end

  @result_data = ret
  return ret
end

#positionObject



343
344
345
# File 'lib/rndk/buttonbox.rb', line 343

def position
  super(@win)
end

#set(highlight, box) ⇒ Object

This sets multiple attributes of the widget.



263
264
265
266
# File 'lib/rndk/buttonbox.rb', line 263

def set(highlight, box)
  self.set_highlight(highlight)
  self.set_box(box)
end

#set_bg_color(attrib) ⇒ Object

This sets th background attribute of the widget.



278
279
280
# File 'lib/rndk/buttonbox.rb', line 278

def set_bg_color(attrib)
  Ncurses.wbkgd(@win, attrib)
end

#set_highlight(highlight) ⇒ Object

This sets the highlight attribute for the buttonboxes



269
270
271
# File 'lib/rndk/buttonbox.rb', line 269

def set_highlight(highlight)
  @highlight = highlight
end

#setCurrentButton(button) ⇒ Object



321
322
323
324
325
# File 'lib/rndk/buttonbox.rb', line 321

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

#unfocusObject



339
340
341
# File 'lib/rndk/buttonbox.rb', line 339

def unfocus
  self.draw
end