rndk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/COPYING +137 -0
  4. data/Gemfile +4 -0
  5. data/README.md +100 -0
  6. data/Rakefile +3 -0
  7. data/TODO +68 -0
  8. data/demos/appointment.rb +346 -0
  9. data/demos/clock.rb +56 -0
  10. data/examples/01-hello-world.rb +56 -0
  11. data/examples/05-position-widget.rb +108 -0
  12. data/lib/rndk.rb +912 -0
  13. data/lib/rndk/alphalist.rb +572 -0
  14. data/lib/rndk/button.rb +370 -0
  15. data/lib/rndk/buttonbox.rb +359 -0
  16. data/lib/rndk/calendar.rb +766 -0
  17. data/lib/rndk/core/display.rb +63 -0
  18. data/lib/rndk/core/draw.rb +238 -0
  19. data/lib/rndk/core/quick_widgets.rb +106 -0
  20. data/lib/rndk/core/screen.rb +269 -0
  21. data/lib/rndk/core/traverse.rb +289 -0
  22. data/lib/rndk/core/widget.rb +506 -0
  23. data/lib/rndk/dialog.rb +367 -0
  24. data/lib/rndk/dscale.rb +13 -0
  25. data/lib/rndk/entry.rb +575 -0
  26. data/lib/rndk/fscale.rb +61 -0
  27. data/lib/rndk/fselect.rb +940 -0
  28. data/lib/rndk/fslider.rb +80 -0
  29. data/lib/rndk/graph.rb +401 -0
  30. data/lib/rndk/histogram.rb +412 -0
  31. data/lib/rndk/itemlist.rb +474 -0
  32. data/lib/rndk/label.rb +218 -0
  33. data/lib/rndk/marquee.rb +244 -0
  34. data/lib/rndk/matrix.rb +1189 -0
  35. data/lib/rndk/mentry.rb +619 -0
  36. data/lib/rndk/menu.rb +478 -0
  37. data/lib/rndk/radio.rb +538 -0
  38. data/lib/rndk/scale.rb +538 -0
  39. data/lib/rndk/scroll.rb +633 -0
  40. data/lib/rndk/scroller.rb +183 -0
  41. data/lib/rndk/selection.rb +630 -0
  42. data/lib/rndk/slider.rb +545 -0
  43. data/lib/rndk/swindow.rb +766 -0
  44. data/lib/rndk/template.rb +560 -0
  45. data/lib/rndk/uscale.rb +14 -0
  46. data/lib/rndk/uslider.rb +14 -0
  47. data/lib/rndk/version.rb +6 -0
  48. data/lib/rndk/viewer.rb +825 -0
  49. data/rndk.gemspec +35 -0
  50. metadata +141 -0
data/lib/rndk/radio.rb ADDED
@@ -0,0 +1,538 @@
1
+ require 'rndk/scroller'
2
+
3
+ module RNDK
4
+ class RADIO < RNDK::SCROLLER
5
+ def initialize(rndkscreen, xplace, yplace, splace, height, width, title,
6
+ list, list_size, choice_char, def_item, highlight, box, shadow)
7
+ super()
8
+ parent_width = Ncurses.getmaxx(rndkscreen.window)
9
+ parent_height = Ncurses.getmaxy(rndkscreen.window)
10
+ box_width = width
11
+ box_height = height
12
+ widest_item = 0
13
+
14
+ bindings = {
15
+ RNDK::BACKCHAR => Ncurses::KEY_PPAGE,
16
+ RNDK::FORCHAR => Ncurses::KEY_NPAGE,
17
+ 'g' => Ncurses::KEY_HOME,
18
+ '1' => Ncurses::KEY_HOME,
19
+ 'G' => Ncurses::KEY_END,
20
+ '<' => Ncurses::KEY_HOME,
21
+ '>' => Ncurses::KEY_END,
22
+ }
23
+
24
+ self.setBox(box)
25
+
26
+ # If the height is a negative value, height will be ROWS-height,
27
+ # otherwise the height will be the given height.
28
+ box_height = RNDK.setWidgetDimension(parent_height, height, 0)
29
+
30
+ # If the width is a negative value, the width will be COLS-width,
31
+ # otherwise the width will be the given width.
32
+ box_width = RNDK.setWidgetDimension(parent_width, width, 5)
33
+
34
+ box_width = self.setTitle(title, box_width)
35
+
36
+ # Set the box height.
37
+ if @title_lines > box_height
38
+ box_height = @title_lines + [list_size, 8].min + 2 * @border_size
39
+ end
40
+
41
+ # Adjust the box width if there is a scroll bar.
42
+ if splace == RNDK::LEFT || splace == RNDK::RIGHT
43
+ box_width += 1
44
+ @scrollbar = true
45
+ else
46
+ scrollbar = false
47
+ end
48
+
49
+ # Make sure we didn't extend beyond the dimensions of the window
50
+ @box_width = [box_width, parent_width].min
51
+ @box_height = [box_height, parent_height].min
52
+
53
+ self.setViewSize(list_size)
54
+
55
+ # Each item in the needs to be converted to chtype array
56
+ widest_item = self.createList(list, list_size, @box_width)
57
+ if widest_item > 0
58
+ self.updateViewWidth(widest_item)
59
+ elsif list_size > 0
60
+ self.destroy
61
+ return nil
62
+ end
63
+
64
+ # Rejustify the x and y positions if we need to.
65
+ xtmp = [xplace]
66
+ ytmp = [yplace]
67
+ RNDK.alignxy(rndkscreen.window, xtmp, ytmp, @box_width, @box_height)
68
+ xpos = xtmp[0]
69
+ ypos = ytmp[0]
70
+
71
+ # Make the radio window
72
+ @win = Ncurses.newwin(@box_height, @box_width, ypos, xpos)
73
+
74
+ # Is the window nil?
75
+ if @win.nil?
76
+ self.destroy
77
+ return nil
78
+ end
79
+
80
+ # Turn on the keypad.
81
+ Ncurses.keypad(@win, true)
82
+
83
+ # Create the scrollbar window.
84
+ if splace == RNDK::RIGHT
85
+ @scrollbar_win = Ncurses.subwin(@win, self.maxViewSize, 1,
86
+ self.Screen_YPOS(ypos), xpos + @box_width - @border_size - 1)
87
+ elsif splace == RNDK::LEFT
88
+ @scrollbar_win = Ncurses.subwin(@win, self.maxViewSize, 1,
89
+ self.Screen_YPOS(ypos), self.Screen_XPOS(xpos))
90
+ else
91
+ @scrollbar_win = nil
92
+ end
93
+
94
+ # Set the rest of the variables
95
+ @screen = rndkscreen
96
+ @parent = rndkscreen.window
97
+ @scrollbar_placement = splace
98
+ @widest_item = widest_item
99
+ @left_char = 0
100
+ @selected_item = 0
101
+ @highlight = highlight
102
+ @choice_char = choice_char.ord
103
+ @left_box_char = '['.ord
104
+ @right_box_char = ']'.ord
105
+ @def_item = def_item
106
+ @input_window = @win
107
+ @accepts_focus = true
108
+ @shadow = shadow
109
+
110
+ self.setCurrentItem(0)
111
+
112
+ # Do we need to create the shadow?
113
+ if shadow
114
+ @shadow_win = Ncurses.newwin(box_height, box_width + 1,
115
+ ypos + 1, xpos + 1)
116
+ end
117
+
118
+ # Setup the key bindings
119
+ bindings.each do |from, to|
120
+ self.bind(:RADIO, from, :getc, to)
121
+ end
122
+
123
+ rndkscreen.register(:RADIO, self)
124
+ end
125
+
126
+ # Put the cursor on the currently-selected item.
127
+ def fixCursorPosition
128
+ scrollbar_adj = if @scrollbar_placement == RNDK::LEFT then 1 else 0 end
129
+ ypos = self.Screen_YPOS(@current_item - @current_top)
130
+ xpos = self.Screen_XPOS(0) + scrollbar_adj
131
+
132
+ Ncurses.wmove(@input_window, ypos, xpos)
133
+ Ncurses.wrefresh @input_window
134
+ end
135
+
136
+ # This actually manages the radio widget.
137
+ def activate(actions)
138
+ # Draw the radio list.
139
+ self.draw(@box)
140
+
141
+ if actions.nil? || actions.size == 0
142
+ while true
143
+ self.fixCursorPosition
144
+ input = self.getch([])
145
+
146
+ # Inject the character into the widget.
147
+ ret = self.inject(input)
148
+ if @exit_type != :EARLY_EXIT
149
+ return ret
150
+ end
151
+ end
152
+ else
153
+ actions.each do |action|
154
+ ret = self.inject(action)
155
+ if @exit_type != :EARLY_EXIT
156
+ return ret
157
+ end
158
+ end
159
+ end
160
+
161
+ # Set the exit type and return
162
+ self.setExitType(0)
163
+ return -1
164
+ end
165
+
166
+ # This injects a single character into the widget.
167
+ def inject(input)
168
+ pp_return = 1
169
+ ret = -1
170
+ complete = false
171
+
172
+ # Set the exit type
173
+ self.setExitType(0)
174
+
175
+ # Draw the widget list
176
+ self.drawList(@box)
177
+
178
+ # Check if there is a pre-process function to be called
179
+ unless @pre_process_func.nil?
180
+ # Call the pre-process function.
181
+ pp_return = @pre_process_func.call(:RADIO, self,
182
+ @pre_process_data, input)
183
+ end
184
+
185
+ # Should we continue?
186
+ if pp_return != 0
187
+ # Check for a predefined key binding.
188
+ if self.checkBind(:RADIO, input)
189
+ complete = true
190
+ else
191
+ case input
192
+ when Ncurses::KEY_UP
193
+ self.KEY_UP
194
+ when Ncurses::KEY_DOWN
195
+ self.KEY_DOWN
196
+ when Ncurses::KEY_RIGHT
197
+ self.KEY_RIGHT
198
+ when Ncurses::KEY_LEFT
199
+ self.KEY_LEFT
200
+ when Ncurses::KEY_PPAGE
201
+ self.KEY_PPAGE
202
+ when Ncurses::KEY_NPAGE
203
+ self.KEY_NPAGE
204
+ when Ncurses::KEY_HOME
205
+ self.KEY_HOME
206
+ when Ncurses::KEY_END
207
+ self.KEY_END
208
+ when '$'.ord
209
+ @left_char = @max_left_char
210
+ when '|'.ord
211
+ @left_char = 0
212
+ when ' '.ord
213
+ @selected_item = @current_item
214
+ when RNDK::KEY_ESC
215
+ self.setExitType(input)
216
+ ret = -1
217
+ complete = true
218
+ when Ncurses::ERR
219
+ self.setExitType(input)
220
+ complete = true
221
+ when RNDK::KEY_TAB, RNDK::KEY_RETURN, Ncurses::KEY_ENTER
222
+ self.setExitType(input)
223
+ ret = @selected_item
224
+ complete = true
225
+ when RNDK::REFRESH
226
+ @screen.erase
227
+ @screen.refresh
228
+ end
229
+ end
230
+
231
+ # Should we call a post-process?
232
+ if !complete && !(@post_process_func.nil?)
233
+ @post_process_func.call(:RADIO, self, @post_process_data, input)
234
+ end
235
+ end
236
+
237
+ if !complete
238
+ self.drawList(@box)
239
+ self.setExitType(0)
240
+ end
241
+
242
+ self.fixCursorPosition
243
+ @return_data = ret
244
+ return ret
245
+ end
246
+
247
+ # This moves the radio field to the given location.
248
+ def move(xplace, yplace, relative, refresh_flag)
249
+ windows = [@win, @scrollbar_win, @shadow_win]
250
+ self.move_specific(xplace, yplace, relative, refresh_flag,
251
+ windows, subwidgets)
252
+ end
253
+
254
+ # This function draws the radio widget.
255
+ def draw(box)
256
+ # Do we need to draw in the shadow?
257
+ if !(@shadow_win.nil?)
258
+ Draw.drawShadow(@shadow_win)
259
+ end
260
+
261
+ self.drawTitle(@win)
262
+
263
+ # Draw in the radio list.
264
+ self.drawList(@box)
265
+ end
266
+
267
+ # This redraws the radio list.
268
+ def drawList(box)
269
+ scrollbar_adj = if @scrollbar_placement == RNDK::LEFT then 1 else 0 end
270
+ screen_pos = 0
271
+
272
+ # Draw the list
273
+ (0...@view_size).each do |j|
274
+ k = j + @current_top
275
+ if k < @list_size
276
+ xpos = self.Screen_XPOS(0)
277
+ ypos = self.Screen_YPOS(j)
278
+
279
+ screen_pos = self.ScreenPOS(k, scrollbar_adj)
280
+
281
+ # Draw the empty string.
282
+ Draw.writeBlanks(@win, xpos, ypos, RNDK::HORIZONTAL, 0,
283
+ @box_width - @border_size)
284
+
285
+ # Draw the line.
286
+ Draw.writeChtype(@win,
287
+ if screen_pos >= 0 then screen_pos else 1 end,
288
+ ypos, @item[k], RNDK::HORIZONTAL,
289
+ if screen_pos >= 0 then 0 else 1 - screen_pos end,
290
+ @item_len[k])
291
+
292
+ # Draw the selected choice
293
+ xpos += scrollbar_adj
294
+ Ncurses.mvwaddch(@win, ypos, xpos, @left_box_char)
295
+ Ncurses.mvwaddch(@win, ypos, xpos + 1,
296
+ if k == @selected_item then @choice_char else ' '.ord end)
297
+ Ncurses.mvwaddch(@win, ypos, xpos + 2, @right_box_char)
298
+ end
299
+ end
300
+
301
+ # Highlight the current item
302
+ if @has_focus
303
+ k = @current_item
304
+ if k < @list_size
305
+ screen_pos = self.ScreenPOS(k, scrollbar_adj)
306
+ ypos = self.Screen_YPOS(@current_high)
307
+
308
+ Draw.writeChtypeAttrib(@win,
309
+ if screen_pos >= 0 then screen_pos else 1 + scrollbar_adj end,
310
+ ypos, @item[k], @highlight, RNDK::HORIZONTAL,
311
+ if screen_pos >= 0 then 0 else 1 - screen_pos end,
312
+ @item_len[k])
313
+ end
314
+ end
315
+
316
+ if @scrollbar
317
+ @toggle_pos = (@current_item * @step).floor
318
+ @toggle_pos = [@toggle_pos, Ncurses.getmaxy(@scrollbar_win) - 1].min
319
+
320
+ Ncurses.mvwvline(@scrollbar_win,
321
+ 0,
322
+ 0,
323
+ Ncurses::ACS_CKBOARD,
324
+ Ncurses.getmaxy(@scrollbar_win))
325
+
326
+ Ncurses.mvwvline(@scrollbar_win,
327
+ @toggle_pos,
328
+ 0,
329
+ ' '.ord | Ncurses::A_REVERSE,
330
+ @toggle_size)
331
+ end
332
+
333
+ # Box it if needed.
334
+ if box
335
+ Draw.drawObjBox(@win, self)
336
+ end
337
+
338
+ self.fixCursorPosition
339
+ end
340
+
341
+ # This sets the background attribute of the widget.
342
+ def setBKattr(attrib)
343
+ Ncurses.wbkgd(@win, attrib)
344
+ Ncurses.wbkgd(@scrollbar_win, attrib) unless @scrollbar_win.nil?
345
+ end
346
+
347
+ def destroyInfo
348
+ @item = ''
349
+ end
350
+
351
+ # This function destroys the radio widget.
352
+ def destroy
353
+ self.cleanTitle
354
+ self.destroyInfo
355
+
356
+ # Clean up the windows.
357
+ RNDK.deleteCursesWindow(@scrollbar_win)
358
+ RNDK.deleteCursesWindow(@shadow_win)
359
+ RNDK.deleteCursesWindow(@win)
360
+
361
+ # Clean up the key bindings.
362
+ self.cleanBindings(:RADIO)
363
+
364
+ # Unregister this object.
365
+ RNDK::Screen.unregister(:RADIO, self)
366
+ end
367
+
368
+ # This function erases the radio widget
369
+ def erase
370
+ if self.validRNDKObject
371
+ RNDK.eraseCursesWindow(@win)
372
+ RNDK.eraseCursesWindow(@shadow_win)
373
+ end
374
+ end
375
+
376
+ # This sets various attributes of the radio list.
377
+ def set(highlight, choice_char, box)
378
+ self.setHighlight(highlight)
379
+ self.setChoiceCHaracter(choice_char)
380
+ self.setBox(box)
381
+ end
382
+
383
+ # This sets the radio list items.
384
+ def setItems(list, list_size)
385
+ widest_item = self.createList(list, list_size, @box_width)
386
+ if widest_item <= 0
387
+ return
388
+ end
389
+
390
+ # Clean up the display.
391
+ (0...@view_size).each do |j|
392
+ Draw.writeBlanks(@win, self.Screen_XPOS(0), self.Screen_YPOS(j),
393
+ RNDK::HORIZONTAL, 0, @box_width - @border_size)
394
+ end
395
+
396
+ self.setViewSize(list_size)
397
+
398
+ self.setCurrentItem(0)
399
+ @left_char = 0
400
+ @selected_item = 0
401
+
402
+ self.updateViewWidth(widest_item)
403
+ end
404
+
405
+ def getItems(list)
406
+ (0...@list_size).each do |j|
407
+ list << RNDK.chtype2Char(@item[j])
408
+ end
409
+ return @list_size
410
+ end
411
+
412
+ # This sets the highlight bar of the radio list.
413
+ def setHighlight(highlight)
414
+ @highlight = highlight
415
+ end
416
+
417
+ def getHighlight
418
+ return @highlight
419
+ end
420
+
421
+ # This sets the character to use when selecting na item in the list.
422
+ def setChoiceCharacter(character)
423
+ @choice_char = character
424
+ end
425
+
426
+ def getChoiceCharacter
427
+ return @choice_char
428
+ end
429
+
430
+ # This sets the character to use to drw the left side of the choice box
431
+ # on the list
432
+ def setLeftBrace(character)
433
+ @left_box_char = character
434
+ end
435
+
436
+ def getLeftBrace
437
+ return @left_box_char
438
+ end
439
+
440
+ # This sets the character to use to draw the right side of the choice box
441
+ # on the list
442
+ def setRightBrace(character)
443
+ @right_box_char = character
444
+ end
445
+
446
+ def getRightBrace
447
+ return @right_box_char
448
+ end
449
+
450
+ # This sets the current highlighted item of the widget
451
+ def setCurrentItem(item)
452
+ self.setPosition(item)
453
+ @selected_item = item
454
+ end
455
+
456
+ def getCurrentItem
457
+ return @current_item
458
+ end
459
+
460
+ # This sets the selected item of the widget
461
+ def setSelectedItem(item)
462
+ @selected_item = item
463
+ end
464
+
465
+ def getSelectedItem
466
+ return @selected_item
467
+ end
468
+
469
+ def focus
470
+ self.drawList(@box)
471
+ end
472
+
473
+ def unfocus
474
+ self.drawList(@box)
475
+ end
476
+
477
+ def createList(list, list_size, box_width)
478
+ status = false
479
+ widest_item = 0
480
+
481
+ if list_size >= 0
482
+ new_list = []
483
+ new_len = []
484
+ new_pos = []
485
+
486
+ # Each item in the needs to be converted to chtype array
487
+ status = true
488
+ box_width -= 2 + @border_size
489
+ (0...list_size).each do |j|
490
+ lentmp = []
491
+ postmp = []
492
+ new_list << RNDK.char2Chtype(list[j], lentmp, postmp)
493
+ new_len << lentmp[0]
494
+ new_pos << postmp[0]
495
+ if new_list[j].nil? || new_list[j].size == 0
496
+ status = false
497
+ break
498
+ end
499
+ new_pos[j] = RNDK.justifyString(box_width, new_len[j], new_pos[j]) + 3
500
+ widest_item = [widest_item, new_len[j]].max
501
+ end
502
+ if status
503
+ self.destroyInfo
504
+ @item = new_list
505
+ @item_len = new_len
506
+ @item_pos = new_pos
507
+ end
508
+ end
509
+
510
+ return (if status then widest_item else 0 end)
511
+ end
512
+
513
+ # Determine how many characters we can shift to the right
514
+ # before all the items have been scrolled off the screen.
515
+ def AvailableWidth
516
+ @box_width - 2 * @border_size - 3
517
+ end
518
+
519
+ def updateViewWidth(widest)
520
+ @max_left_char = if @box_width > widest
521
+ then 0
522
+ else widest - self.AvailableWidth
523
+ end
524
+ end
525
+
526
+ def WidestItem
527
+ @max_left_char + self.AvailableWidth
528
+ end
529
+
530
+ def ScreenPOS(n, scrollbar_adj)
531
+ @item_pos[n] - @left_char + scrollbar_adj + @border_size
532
+ end
533
+
534
+ def object_type
535
+ :RADIO
536
+ end
537
+ end
538
+ end