slithernix-cdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,355 @@
1
+ require_relative 'cdk_objs'
2
+
3
+ module CDK
4
+ class BUTTONBOX < CDK::CDKOBJS
5
+ attr_reader :current_button
6
+
7
+ def initialize(cdkscreen, x_pos, y_pos, height, width, title, rows, cols,
8
+ buttons, button_count, highlight, box, shadow)
9
+ super()
10
+ parent_width = cdkscreen.window.maxx
11
+ parent_height = cdkscreen.window.maxy
12
+ col_width = 0
13
+ current_button = 0
14
+ @button = []
15
+ @button_len = []
16
+ @button_pos = []
17
+ @column_widths = []
18
+
19
+ if button_count <= 0
20
+ self.destroy
21
+ return nil
22
+ end
23
+
24
+ self.setBox(box)
25
+
26
+ # Set some default values for the widget.
27
+ @row_adjust = 0
28
+ @col_adjust = 0
29
+
30
+ # If the height is a negative value, the height will be
31
+ # ROWS-height, otherwise the height will be the given height.
32
+ box_height = CDK.setWidgetDimension(parent_height, height, rows + 1)
33
+
34
+ # If the width is a negative value, the width will be
35
+ # COLS-width, otherwise the width will be the given width.
36
+ box_width = CDK.setWidgetDimension(parent_width, width, 0)
37
+
38
+ box_width = self.setTitle(title, box_width)
39
+
40
+ # Translate the buttons string to a chtype array
41
+ (0...button_count).each do |x|
42
+ button_len = []
43
+ @button << CDK.char2Chtype(buttons[x], button_len ,[])
44
+ @button_len << button_len[0]
45
+ end
46
+
47
+ # Set the button positions.
48
+ (0...cols).each do |x|
49
+ max_col_width = -2**31
50
+
51
+ # Look for the widest item in this column.
52
+ (0...rows).each do |y|
53
+ if current_button < button_count
54
+ max_col_width = [@button_len[current_button], max_col_width].max
55
+ current_button += 1
56
+ end
57
+ end
58
+
59
+ # Keep the maximum column width for this column.
60
+ @column_widths << max_col_width
61
+ col_width += max_col_width
62
+ end
63
+ box_width += 1
64
+
65
+ # Make sure we didn't extend beyond the dimensions of the window.
66
+ box_width = [box_width, parent_width].min
67
+ box_height = [box_height, parent_height].min
68
+
69
+ # Now we have to readjust the x and y positions
70
+ xtmp = [x_pos]
71
+ ytmp = [y_pos]
72
+ CDK.alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
73
+ xpos = xtmp[0]
74
+ ypos = ytmp[0]
75
+
76
+ # Set up the buttonbox box attributes.
77
+ @screen = cdkscreen
78
+ @parent = cdkscreen.window
79
+ @win = Curses::Window.new(box_height, box_width, ypos, xpos)
80
+ @shadow_win = nil
81
+ @button_count = button_count
82
+ @current_button = 0
83
+ @rows = rows
84
+ @cols = [button_count, cols].min
85
+ @box_height = box_height
86
+ @box_width = box_width
87
+ @highlight = highlight
88
+ @accepts_focus = true
89
+ @input_window = @win
90
+ @shadow = shadow
91
+ @button_attrib = Curses::A_NORMAL
92
+
93
+ # Set up the row adjustment.
94
+ if box_height - rows - @title_lines > 0
95
+ @row_adjust = (box_height - rows - @title_lines) / @rows
96
+ end
97
+
98
+ # Set the col adjustment
99
+ if box_width - col_width > 0
100
+ @col_adjust = ((box_width - col_width) / @cols) - 1
101
+ end
102
+
103
+ # If we couldn't create the window, we should return a null value.
104
+ if @win.nil?
105
+ self.destroy
106
+ return nil
107
+ end
108
+ @win.keypad(true)
109
+
110
+ # Was there a shadow?
111
+ if shadow
112
+ @shadow_win = Curses::Window.new(box_height, box_width,
113
+ ypos + 1, xpos + 1)
114
+ end
115
+
116
+ # Register this baby.
117
+ cdkscreen.register(:BUTTONBOX, self)
118
+ end
119
+
120
+ # This activates the widget.
121
+ def activate(actions)
122
+ # Draw the buttonbox box.
123
+ self.draw(@box)
124
+
125
+ if actions.nil? || actions.size == 0
126
+ while true
127
+ input = self.getch([])
128
+
129
+ # Inject the characer into the widget.
130
+ ret = self.inject(input)
131
+ if @exit_type != :EARLY_EXIT
132
+ return ret
133
+ end
134
+ end
135
+ else
136
+ # Inject each character one at a time.
137
+ actions.each do |action|
138
+ ret = self.inject(action)
139
+ if @exit_type != :EARLY_EXIT
140
+ return ret
141
+ end
142
+ end
143
+ end
144
+
145
+ # Set the exit type and exit
146
+ self.setExitType(0)
147
+ return -1
148
+ end
149
+
150
+ # This injects a single character into the widget.
151
+ def inject(input)
152
+ first_button = 0
153
+ last_button = @button_count - 1
154
+ pp_return = 1
155
+ ret = -1
156
+ complete = false
157
+
158
+ # Set the exit type
159
+ self.setExitType(0)
160
+
161
+ unless @pre_process_func.nil?
162
+ pp_return = @pre_process_func.call(:BUTTONBOX, self,
163
+ @pre_process_data, input)
164
+ end
165
+
166
+ # Should we continue?
167
+ if pp_return != 0
168
+ # Check for a key binding.
169
+ if self.checkBind(:BUTTONBOX, input)
170
+ complete = true
171
+ else
172
+ case input
173
+ when Curses::KEY_LEFT, Curses::KEY_BTAB, Curses::KEY_BACKSPACE
174
+ if @current_button - @rows < first_button
175
+ @current_button = last_button
176
+ else
177
+ @current_button -= @rows
178
+ end
179
+ when Curses::KEY_RIGHT, CDK::KEY_TAB, ' '
180
+ if @current_button + @rows > last_button
181
+ @current_button = first_button
182
+ else
183
+ @current_button += @rows
184
+ end
185
+ when Curses::KEY_UP
186
+ if @current_button -1 < first_button
187
+ @current_button = last_button
188
+ else
189
+ @current_button -= 1
190
+ end
191
+ when Curses::KEY_DOWN
192
+ if @current_button + 1 > last_button
193
+ @current_button = first_button
194
+ else
195
+ @current_button += 1
196
+ end
197
+ when CDK::REFRESH
198
+ @screen.erase
199
+ @screen.refresh
200
+ when CDK::KEY_ESC
201
+ self.setExitType(input)
202
+ complete = true
203
+ when Curses::Error
204
+ self.setExitType(input)
205
+ complete = true
206
+ when CDK::KEY_RETURN, Curses::KEY_ENTER
207
+ self.setExitType(input)
208
+ ret = @current_button
209
+ complete = true
210
+ end
211
+ end
212
+
213
+ if !complete && !(@post_process_func.nil?)
214
+ @post_process_func.call(:BUTTONBOX, self, @post_process_data,
215
+ input)
216
+ end
217
+
218
+ end
219
+
220
+ unless complete
221
+ self.drawButtons
222
+ self.setExitType(0)
223
+ end
224
+
225
+ @result_data = ret
226
+ return ret
227
+ end
228
+
229
+ # This sets multiple attributes of the widget.
230
+ def set(highlight, box)
231
+ self.setHighlight(highlight)
232
+ self.setBox(box)
233
+ end
234
+
235
+ # This sets the highlight attribute for the buttonboxes
236
+ def setHighlight(highlight)
237
+ @highlight = highlight
238
+ end
239
+
240
+ def getHighlight
241
+ return @highlight
242
+ end
243
+
244
+ # This sets th background attribute of the widget.
245
+ def setBKattr(attrib)
246
+ @win.wbkgd(attrib)
247
+ end
248
+
249
+ # This draws the buttonbox box widget.
250
+ def draw(box)
251
+ # Is there a shadow?
252
+ unless @shadow_win.nil?
253
+ Draw.drawShadow(@shadow_win)
254
+ end
255
+
256
+ # Box the widget if they asked.
257
+ if box
258
+ Draw.drawObjBox(@win, self)
259
+ end
260
+
261
+ # Draw in the title if there is one.
262
+ self.drawTitle(@win)
263
+
264
+ # Draw in the buttons.
265
+ self.drawButtons
266
+ end
267
+
268
+ # This draws the buttons on the button box widget.
269
+ def drawButtons
270
+ row = @title_lines + 1
271
+ col = @col_adjust / 2
272
+ current_button = 0
273
+ cur_row = -1
274
+ cur_col = -1
275
+
276
+ # Draw the buttons.
277
+ while current_button < @button_count
278
+ (0...@cols).each do |x|
279
+ row = @title_lines + @border_size
280
+
281
+ (0...@rows).each do |y|
282
+ attr = @button_attrib
283
+ if current_button == @current_button
284
+ attr = @highlight
285
+ cur_row = row
286
+ cur_col = col
287
+ end
288
+ Draw.writeChtypeAttrib(@win, col, row,
289
+ @button[current_button], attr, CDK::HORIZONTAL, 0,
290
+ @button_len[current_button])
291
+ row += (1 + @row_adjust)
292
+ current_button += 1
293
+ end
294
+ col += @column_widths[x] + @col_adjust + @border_size
295
+ end
296
+ end
297
+
298
+ # This seems to just cause the window display to be all messed up
299
+ #if cur_row >= 0 && cur_col >= 0
300
+ # @win.move(cur_row, cur_col)
301
+ #end
302
+ @win.refresh
303
+ end
304
+
305
+ # This erases the buttonbox box from the screen.
306
+ def erase
307
+ if self.validCDKObject
308
+ CDK.eraseCursesWindow(@win)
309
+ CDK.eraseCursesWindow(@shadow_win)
310
+ end
311
+ end
312
+
313
+ # This destroys the widget
314
+ def destroy
315
+ self.cleanTitle
316
+
317
+ CDK.deleteCursesWindow(@shadow_win)
318
+ CDK.deleteCursesWindow(@win)
319
+
320
+ self.cleanBindings(:BUTTONBOX)
321
+
322
+ CDK::SCREEN.unregister(:BUTTONBOX, self)
323
+ end
324
+
325
+ def setCurrentButton(button)
326
+ if button >= 0 && button < @button_count
327
+ @current_button = button
328
+ end
329
+ end
330
+
331
+ def getCurrentButton
332
+ @current_button
333
+ end
334
+
335
+ def getButtonCount
336
+ @button_count
337
+ end
338
+
339
+ def focus
340
+ self.draw(@box)
341
+ end
342
+
343
+ def unfocus
344
+ self.draw(@box)
345
+ end
346
+
347
+ def object_type
348
+ :BUTTONBOX
349
+ end
350
+
351
+ def position
352
+ super(@win)
353
+ end
354
+ end
355
+ end