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
@@ -0,0 +1,412 @@
1
+ require 'rndk'
2
+
3
+ module RNDK
4
+ class HISTOGRAM < RNDK::Widget
5
+ def initialize(rndkscreen, xplace, yplace, height, width, orient,
6
+ title, box, shadow)
7
+ super()
8
+ parent_width = Ncurses.getmaxx(rndkscreen.window)
9
+ parent_height = Ncurses.getmaxy(rndkscreen.window)
10
+
11
+ self.setBox(box)
12
+
13
+ box_height = RNDK.setWidgetDimension(parent_height, height, 2)
14
+ old_height = box_height
15
+
16
+ box_width = RNDK.setWidgetDimension(parent_width, width, 0)
17
+ old_width = box_width
18
+
19
+ box_width = self.setTitle(title, -(box_width + 1))
20
+
21
+ # Increment the height by number of lines in in the title
22
+ box_height += @title_lines
23
+
24
+ # Make sure we didn't extend beyond the dimensions of the window.
25
+ box_width = if box_width > parent_width
26
+ then old_width
27
+ else box_width
28
+ end
29
+ box_height = if box_height > parent_height
30
+ then old_height
31
+ else box_height
32
+ end
33
+
34
+ # Rejustify the x and y positions if we need to.
35
+ xtmp = [xplace]
36
+ ytmp = [yplace]
37
+ RNDK.alignxy(rndkscreen.window, xtmp, ytmp, box_width, box_height)
38
+ xpos = xtmp[0]
39
+ ypos = ytmp[0]
40
+
41
+ # Set up the histogram data
42
+ @screen = rndkscreen
43
+ @parent = rndkscreen.window
44
+ @win = Ncurses.newwin(box_height, box_width, ypos, xpos)
45
+ @shadow_win = nil
46
+ @box_width = box_width
47
+ @box_height = box_height
48
+ @field_width = box_width - 2 * @border_size
49
+ @field_height = box_height - @title_lines - 2 * @border_size
50
+ @orient = orient
51
+ @shadow = shadow
52
+
53
+ # Is the window nil
54
+ if @win.nil?
55
+ self.destroy
56
+ return nil
57
+ end
58
+
59
+ Ncurses.keypad(@win, true)
60
+
61
+ # Set up some default values.
62
+ @filler = '#'.ord | Ncurses::A_REVERSE
63
+ @stats_attr = Ncurses::A_NORMAL
64
+ @stats_pos = RNDK::TOP
65
+ @view_type = :REAL
66
+ @high = 0
67
+ @low = 0
68
+ @value = 0
69
+ @lowx = 0
70
+ @lowy = 0
71
+ @highx = 0
72
+ @highy = 0
73
+ @curx = 0
74
+ @cury = 0
75
+ @low_string = ''
76
+ @high_string = ''
77
+ @cur_string = ''
78
+
79
+ # Do we want a shadow?
80
+ if shadow
81
+ @shadow_win = Ncurses.newwin(box_height,
82
+ box_width,
83
+ ypos + 1,
84
+ xpos + 1)
85
+ end
86
+
87
+ rndkscreen.register(:HISTOGRAM, self)
88
+ end
89
+
90
+ # This was added for the builder
91
+ def activate(actions)
92
+ self.draw(@box)
93
+ end
94
+
95
+ # Set various widget attributes
96
+ def set(view_type, stats_pos, stats_attr, low, high, value, filler, box)
97
+ self.setDisplayType(view_type)
98
+ self.setStatsPos(stats_pos)
99
+ self.setValue(low, high, value)
100
+ self.setFillerChar(filler)
101
+ self.setStatsAttr(stats_attr)
102
+ self.setBox(box)
103
+ end
104
+
105
+ # Set the values for the widget.
106
+ def setValue(low, high, value)
107
+ # We should error check the information we have.
108
+ @low = if low <= high then low else 0 end
109
+ @high = if low <= high then high else 0 end
110
+ @value = if low <= value && value <= high then value else 0 end
111
+ # Determine the percentage of the given value.
112
+ @percent = if @high == 0 then 0 else 1.0 * @value / @high end
113
+
114
+ # Determine the size of the histogram bar.
115
+ if @orient == RNDK::VERTICAL
116
+ @bar_size = @percent * @field_height
117
+ else
118
+ @bar_size = @percent * @field_width
119
+ end
120
+
121
+ # We have a number of variables which determine the personality of the
122
+ # histogram. We have to go through each one methodically, and set them
123
+ # correctly. This section does this.
124
+ if @view_type != :NONE
125
+ if @orient == RNDK::VERTICAL
126
+ if @stats_pos == RNDK::LEFT || @stats_pos == RNDK::BOTTOM
127
+ # Set the low label attributes.
128
+ @low_string = @low.to_s
129
+ @lowx = 1
130
+ @lowy = @box_height - @low_string.size - 1
131
+
132
+ # Set the high label attributes
133
+ @high_string = @high.to_s
134
+ @highx = 1
135
+ @highy = @title_lines + 1
136
+
137
+ string = ''
138
+ # Set the current value attributes.
139
+ string = if @view_type == :PERCENT
140
+ then "%3.1f%%" % [1.0 * @percent * 100]
141
+ elsif @view_type == :FRACTION
142
+ string = "%d/%d" % [@value, @high]
143
+ else string = @value.to_s
144
+ end
145
+ @cur_string = string
146
+ @curx = 1
147
+ @cury = (@field_height - string.size) / 2 + @title_lines + 1
148
+ elsif @stats_pos == RNDK::CENTER
149
+ # Set the lower label attributes
150
+ @low_string = @low.to_s
151
+ @lowx = @field_width / 2 + 1
152
+ @lowy = @box_height - @low_string.size - 1
153
+
154
+ # Set the high label attributes
155
+ @high_string = @high.to_s
156
+ @highx = @field_width / 2 + 1
157
+ @highy = @title_lines + 1
158
+
159
+ # Set the stats label attributes
160
+ string = if @view_type == :PERCENT
161
+ then "%3.2f%%" % [1.0 * @percent * 100]
162
+ elsif @view_type == :FRACTIOn
163
+ "%d/%d" % [@value, @high]
164
+ else @value.to_s
165
+ end
166
+
167
+ @cur_string = string
168
+ @curx = @field_width / 2 + 1
169
+ @cury = (@field_height - string.size) / 2 + @title_lines + 1
170
+ elsif @stats_pos == RNDK::RIGHT || @stats_pos == RNDK::TOP
171
+ # Set the low label attributes.
172
+ @low_string = @low.to_s
173
+ @lowx = @field_width
174
+ @lowy = @box_height - @low_string.size - 1
175
+
176
+ # Set the high label attributes.
177
+ @high_string = @high.to_s
178
+ @highx = @field_width
179
+ @highy = @title_lines + 1
180
+
181
+ # Set the stats label attributes.
182
+ string = if @view_type == :PERCENT
183
+ then "%3.2f%%" % [1.0 * @percent * 100]
184
+ elsif @view_type == :FRACTION
185
+ "%d/%d" % [@value, @high]
186
+ else @value.to_s
187
+ end
188
+ @cur_string = string
189
+ @curx = @field_width
190
+ @cury = (@field_height - string.size) / 2 + @title_lines + 1
191
+ end
192
+ else
193
+ # Alignment is HORIZONTAL
194
+ if @stats_pos == RNDK::TOP || @stats_pos == RNDK::RIGHT
195
+ # Set the low label attributes.
196
+ @low_string = @low.to_s
197
+ @lowx = 1
198
+ @lowy = @title_lines + 1
199
+
200
+ # Set the high label attributes.
201
+ @high_string = @high.to_s
202
+ @highx = @box_width - @high_string.size - 1
203
+ @highy = @title_lines + 1
204
+
205
+ # Set the stats label attributes.
206
+ string = if @view_type == :PERCENT
207
+ then "%3.1f%%" % [1.0 * @percent * 100]
208
+ elsif @view_type == :FRACTION
209
+ "%d/%d" % [@value, @high]
210
+ else @value.to_s
211
+ end
212
+ @cur_string = string
213
+ @curx = (@field_width - @cur_string.size) / 2 + 1
214
+ @cury = @title_lines + 1
215
+ elsif @stats_pos == RNDK::CENTER
216
+ # Set the low label attributes.
217
+ @low_string = @low.to_s
218
+ @lowx = 1
219
+ @lowy = (@field_height / 2) + @title_lines + 1
220
+
221
+ # Set the high label attributes.
222
+ @high_string = @high.to_s
223
+ @highx = @box_width - @high_string.size - 1
224
+ @highy = @field_height / 2 + @title_lines + 1
225
+
226
+ # Set the stats label attributes.
227
+ string = if @view_type == :PERCENT
228
+ then "%3.1f%%" % [1.0 * @percent * 100]
229
+ elsif @view_type == :FRACTION
230
+ "%d/%d" % [@value, @high]
231
+ else @value.to_s
232
+ end
233
+ @cur_string = string
234
+ @curx = (@field_width - @cur_string.size) / 2 + 1
235
+ @cury = @field_height / 2 + @title_lines + 1
236
+ elsif @stats_pos == RNDK::BOTTOM || @stats_pos == RNDK::LEFT
237
+ # Set the low label attributes.
238
+ @low_string = @low.to_s
239
+ @lowx = 1
240
+ @lowy = @box_height -2 * @border_size
241
+
242
+ # Set the high label attributes.
243
+ @high_string = @high.to_s
244
+ @highx = @box_width - @high_string.size - 1
245
+ @highy = @box_height - 2 * @border_size
246
+
247
+ # Set the stats label attributes.
248
+ string = if @view_type == :PERCENT
249
+ then "%3.1f%%" % [1.0 * @percent * 100]
250
+ elsif @view_type == :FRACTION
251
+ "%d/%d" % [@value, @high]
252
+ else @value.to_s
253
+ end
254
+ @cur_string = string
255
+ @curx = (@field_width - @cur_string.size) / 2 + 1
256
+ @cury = @box_height - 2 * @border_size
257
+ end
258
+ end
259
+ end
260
+ end
261
+
262
+ def getValue
263
+ return @value
264
+ end
265
+
266
+ def getLowValue
267
+ return @low
268
+ end
269
+
270
+ def getHighValue
271
+ return @high
272
+ end
273
+
274
+ # Set the histogram display type
275
+ def setDisplayType(view_type)
276
+ @view_type = view_type
277
+ end
278
+
279
+ def getViewType
280
+ return @view_type
281
+ end
282
+
283
+ # Set the position of the statistics information.
284
+ def setStatsPos(stats_pos)
285
+ @stats_pos = stats_pos
286
+ end
287
+
288
+ def getStatsPos
289
+ return @stats_pos
290
+ end
291
+
292
+ # Set the attribute of the statistics.
293
+ def setStatsAttr(stats_attr)
294
+ @stats_attr = stats_attr
295
+ end
296
+
297
+ def getStatsAttr
298
+ return @stats_attr
299
+ end
300
+
301
+ # Set the character to use when drawing the widget.
302
+ def setFillerChar(character)
303
+ @filler = character
304
+ end
305
+
306
+ def getFillerChar
307
+ return @filler
308
+ end
309
+
310
+ # Set the background attribute of the widget.
311
+ def setBKattr(attrib)
312
+ Ncurses.wbkgd(@win, attrib)
313
+ end
314
+
315
+ # Move the histogram field to the given location.
316
+ # Inherited
317
+ # def move(xplace, yplace, relative, refresh_flag)
318
+ # end
319
+
320
+ # Draw the widget.
321
+ def draw(box)
322
+ battr = 0
323
+ bchar = 0
324
+ fattr = @filler & Ncurses::A_ATTRIBUTES
325
+ hist_x = @title_lines + 1
326
+ hist_y = @bar_size
327
+
328
+ Ncurses.werase(@win)
329
+
330
+ # Box the widget if asked.
331
+ if box
332
+ Draw.drawObjBox(@win, self)
333
+ end
334
+
335
+ # Do we have a shadow to draw?
336
+ if !(@shadow.nil?)
337
+ Draw.drawShadow(@shadow_win)
338
+ end
339
+
340
+ self.drawTitle(@win)
341
+
342
+ # If the user asked for labels, draw them in.
343
+ if @view_type != :NONE
344
+ # Draw in the low label.
345
+ if @low_string.size > 0
346
+ Draw.writeCharAttrib(@win, @lowx, @lowy, @low_string,
347
+ @stats_attr, @orient, 0, @low_string.size)
348
+ end
349
+
350
+ # Draw in the current value label.
351
+ if @cur_string.size > 0
352
+ Draw.writeCharAttrib(@win, @curx, @cury, @cur_string,
353
+ @stats_attr, @orient, 0, @cur_string.size)
354
+ end
355
+
356
+ # Draw in the high label.
357
+ if @high_string.size > 0
358
+ Draw.writeCharAttrib(@win, @highx, @highy, @high_string,
359
+ @stats_attr, @orient, 0, @high_string.size)
360
+ end
361
+ end
362
+
363
+ if @orient == RNDK::VERTICAL
364
+ hist_x = @box_height - @bar_size - 1
365
+ hist_y = @field_width
366
+ end
367
+
368
+ # Draw the histogram bar.
369
+ (hist_x...@box_height - 1).to_a.each do |x|
370
+ (1..hist_y).each do |y|
371
+ battr = Ncurses.mvwinch(@win, x, y)
372
+
373
+ if battr == ' '.ord
374
+ Ncurses.mvwaddch(@win, x, y, @filler)
375
+ else
376
+ Ncurses.mvwaddch(@win, x, y, battr | fattr)
377
+ end
378
+ end
379
+ end
380
+
381
+ # Refresh the window
382
+ Ncurses.wrefresh @win
383
+ end
384
+
385
+ # Destroy the widget.
386
+ def destroy
387
+ self.cleanTitle
388
+
389
+ # Clean up the windows.
390
+ RNDK.deleteCursesWindow(@shadow_win)
391
+ RNDK.deleteCursesWindow(@win)
392
+
393
+ # Clean the key bindings.
394
+ self.cleanBindings(:HISTOGRAM)
395
+
396
+ # Unregister this object.
397
+ RNDK::Screen.unregister(:HISTOGRAM, self)
398
+ end
399
+
400
+ # Erase the widget from the screen.
401
+ def erase
402
+ if self.validRNDKObject
403
+ RNDK.eraseCursesWindow(@win)
404
+ RNDK.eraseCursesWindow(@shadow_win)
405
+ end
406
+ end
407
+
408
+ def object_type
409
+ :HISTOGRAM
410
+ end
411
+ end
412
+ end
@@ -0,0 +1,474 @@
1
+ require 'rndk'
2
+
3
+ module RNDK
4
+ class ITEMLIST < RNDK::Widget
5
+ def initialize(rndkscreen, xplace, yplace, title, label, item, count,
6
+ default_item, box, shadow)
7
+ super()
8
+ parent_width = Ncurses.getmaxx(rndkscreen.window)
9
+ parent_height = Ncurses.getmaxy(rndkscreen.window)
10
+ field_width = 0
11
+
12
+ if !self.createList(item, count)
13
+ self.destroy
14
+ return nil
15
+ end
16
+
17
+ self.setBox(box)
18
+ box_height = (@border_size * 2) + 1
19
+
20
+ # Set some basic values of the item list
21
+ @label = ''
22
+ @label_len = 0
23
+ @label_win = nil
24
+
25
+ # Translate the label string to a chtype array
26
+ if !(label.nil?) && label.size > 0
27
+ label_len = []
28
+ @label = RNDK.char2Chtype(label, label_len, [])
29
+ @label_len = label_len[0]
30
+ end
31
+
32
+ # Set the box width. Allow an extra char in field width for cursor
33
+ field_width = self.maximumFieldWidth + 1
34
+ box_width = field_width + @label_len + 2 * @border_size
35
+ box_width = self.setTitle(title, box_width)
36
+ box_height += @title_lines
37
+
38
+ # Make sure we didn't extend beyond the dimensions of the window
39
+ @box_width = [box_width, parent_width].min
40
+ @box_height = [box_height, parent_height].min
41
+ self.updateFieldWidth
42
+
43
+ # Rejustify the x and y positions if we need to.
44
+ xtmp = [xplace]
45
+ ytmp = [yplace]
46
+ RNDK.alignxy(rndkscreen.window, xtmp, ytmp, box_width, box_height)
47
+ xpos = xtmp[0]
48
+ ypos = ytmp[0]
49
+
50
+ # Make the window.
51
+ @win = Ncurses.newwin(box_height, box_width, ypos, xpos)
52
+ if @win.nil?
53
+ self.destroy
54
+ return nil
55
+ end
56
+
57
+ # Make the label window if there was a label.
58
+ if @label.size > 0
59
+ @label_win = Ncurses.subwin(@win, 1, @label_len,
60
+ ypos + @border_size + @title_lines,
61
+ xpos + @border_size)
62
+
63
+ if @label_win.nil?
64
+ self.destroy
65
+ return nil
66
+ end
67
+ end
68
+
69
+ Ncurses.keypad(@win, true)
70
+
71
+ # Make the field window.
72
+ if !self.createFieldWin(
73
+ ypos + @border_size + @title_lines,
74
+ xpos + @label_len + @border_size)
75
+ self.destroy
76
+ return nil
77
+ end
78
+
79
+ # Set up the rest of the structure
80
+ @screen = rndkscreen
81
+ @parent = rndkscreen.window
82
+ @shadow_win = nil
83
+ @accepts_focus = true
84
+ @shadow = shadow
85
+
86
+ # Set the default item.
87
+ if default_item >= 0 && default_item < @list_size
88
+ @current_item = default_item
89
+ @default_item = default_item
90
+ else
91
+ @current_item = 0
92
+ @default_item = 0
93
+ end
94
+
95
+ # Do we want a shadow?
96
+ if shadow
97
+ @shadow_win = Ncurses.newwin(box_height, box_width,
98
+ ypos + 1, xpos + 1)
99
+ if @shadow_win.nil?
100
+ self.destroy
101
+ return nil
102
+ end
103
+ end
104
+
105
+ # Register this baby.
106
+ rndkscreen.register(:ITEMLIST, self)
107
+ end
108
+
109
+ # This allows the user to play with the widget.
110
+ def activate(actions)
111
+ ret = -1
112
+
113
+ # Draw the widget.
114
+ self.draw(@box)
115
+ self.drawField(true)
116
+
117
+ if actions.nil? || actions.size == 0
118
+ input = 0
119
+
120
+ while true
121
+ input = self.getch([])
122
+
123
+ # Inject the character into the widget.
124
+ ret = self.inject(input)
125
+ if @exit_type != :EARLY_EXIT
126
+ return ret
127
+ end
128
+ end
129
+ else
130
+ # Inject each character one at a time.
131
+ actions.each do |action|
132
+ ret = self.inject(action)
133
+ if @exit_type != :EARLY_EXIT
134
+ return ret
135
+ end
136
+ end
137
+ end
138
+
139
+ # Set the exit type and exit.
140
+ self.setExitType(0)
141
+ return ret
142
+ end
143
+
144
+ # This injects a single character into the widget.
145
+ def inject(input)
146
+ pp_return = 1
147
+ ret = -1
148
+ complete = false
149
+
150
+ # Set the exit type.
151
+ self.setExitType(0)
152
+
153
+ # Draw the widget field
154
+ self.drawField(true)
155
+
156
+ # Check if there is a pre-process function to be called.
157
+ unless @pre_process_func.nil?
158
+ pp_return = @pre_process_func.call(:ITEMLIST, self,
159
+ @pre_process_data, input)
160
+ end
161
+
162
+ # Should we continue?
163
+ if pp_return != 0
164
+ # Check a predefined binding.
165
+ if self.checkBind(:ITEMLIST, input)
166
+ complete = true
167
+ else
168
+ case input
169
+ when Ncurses::KEY_UP, Ncurses::KEY_RIGHT, ' '.ord, '+'.ord, 'n'.ord
170
+ if @current_item < @list_size - 1
171
+ @current_item += 1
172
+ else
173
+ @current_item = 0
174
+ end
175
+ when Ncurses::KEY_DOWN, Ncurses::KEY_LEFT, '-'.ord, 'p'.ord
176
+ if @current_item > 0
177
+ @current_item -= 1
178
+ else
179
+ @current_item = @list_size - 1
180
+ end
181
+ when 'd'.ord, 'D'.ord
182
+ @current_item = @default_item
183
+ when '0'.ord
184
+ @current_item = 0
185
+ when '$'.ord
186
+ @current_item = @list_size - 1
187
+ when RNDK::KEY_ESC
188
+ self.setExitType(input)
189
+ complete = true
190
+ when Ncurses::ERR
191
+ self.setExitType(input)
192
+ complete = true
193
+ when RNDK::KEY_TAB, RNDK::KEY_RETURN, Ncurses::KEY_ENTER
194
+ self.setExitType(input)
195
+ ret = @current_item
196
+ complete = true
197
+ when RNDK::REFRESH
198
+ @screen.erase
199
+ @screen.refresh
200
+ else
201
+ RNDK.beep
202
+ end
203
+ end
204
+
205
+ # Should we call a post-process?
206
+ if !complete && !(@post_process_func.nil?)
207
+ @post_process_func.call(:ITEMLIST, self, @post_process_data, input)
208
+ end
209
+ end
210
+
211
+ if !complete
212
+ self.drawField(true)
213
+ self.setExitType(0)
214
+ end
215
+
216
+ @result_data = ret
217
+ return ret
218
+ end
219
+
220
+ # This moves the itemlist field to the given location.
221
+ def move(xplace, yplace, relative, refresh_flag)
222
+ windows = [@win, @field_win, @label_win, @shadow_win]
223
+ self.move_specific(xplace, yplace, relative, refresh_flag,
224
+ windows, [])
225
+ end
226
+
227
+ # This draws the widget on the screen.
228
+ def draw(box)
229
+ # Did we ask for a shadow?
230
+ unless @shadow_win.nil?
231
+ Draw.drawShadow(@shadow_win)
232
+ end
233
+
234
+ self.drawTitle(@win)
235
+
236
+ # Draw in the label to the widget.
237
+ unless @label_win.nil?
238
+ Draw.writeChtype(@label_win, 0, 0, @label, RNDK::HORIZONTAL,
239
+ 0, @label.size)
240
+ end
241
+
242
+ # Box the widget if asked.
243
+ if box
244
+ Draw.drawObjBox(@win, self)
245
+ end
246
+
247
+ Ncurses.wrefresh @win
248
+
249
+ # Draw in the field.
250
+ self.drawField(false)
251
+ end
252
+
253
+ # This sets the background attribute of the widget
254
+ def setBKattr(attrib)
255
+ Ncurses.wbkgd(@win, attrib)
256
+ Ncurses.wbkgd(@field_win, attrib)
257
+ Ncurses.wbkgd(@label_win, attrib) unless @label_win.nil?
258
+ end
259
+
260
+ # This function draws the contents of the field.
261
+ def drawField(highlight)
262
+ # Declare local vars.
263
+ current_item = @current_item
264
+
265
+ # Determine how much we have to draw.
266
+ len = [@item_len[current_item], @field_width].min
267
+
268
+ # Erase the field window.
269
+ Ncurses.werase(@field_win)
270
+
271
+ # Draw in the current item in the field.
272
+ (0...len).each do |x|
273
+ c = @item[current_item][x]
274
+
275
+ if highlight
276
+ c = c.ord | Ncurses::A_REVERSE
277
+ end
278
+
279
+ Ncurses.mvwaddch(@field_win, 0, x + @item_pos[current_item], c)
280
+ end
281
+
282
+ # Redraw the field window.
283
+ Ncurses.wrefresh(@field_win)
284
+ end
285
+
286
+ # This function removes the widget from the screen.
287
+ def erase
288
+ if self.validRNDKObject
289
+ RNDK.eraseCursesWindow(@field_win)
290
+ RNDK.eraseCursesWindow(@label_win)
291
+ RNDK.eraseCursesWindow(@win)
292
+ RNDK.eraseCursesWindow(@shadow_win)
293
+ end
294
+ end
295
+
296
+ def destroyInfo
297
+ @list_size = 0
298
+ @item = ''
299
+ end
300
+
301
+ # This function destroys the widget and all the memory it used.
302
+ def destroy
303
+ self.cleanTitle
304
+ self.destroyInfo
305
+
306
+ # Delete the windows
307
+ RNDK.deleteCursesWindow(@field_win)
308
+ RNDK.deleteCursesWindow(@label_win)
309
+ RNDK.deleteCursesWindow(@shadow_win)
310
+ RNDK.deleteCursesWindow(@win)
311
+
312
+ # Clean the key bindings.
313
+ self.cleanBindings(:ITEMLIST)
314
+
315
+ RNDK::Screen.unregister(:ITEMLIST, self)
316
+ end
317
+
318
+ # This sets multiple attributes of the widget.
319
+ def set(list, count, current, box)
320
+ self.setValues(list, count, current)
321
+ self.setBox(box)
322
+ end
323
+
324
+ # This function sets the contents of the list
325
+ def setValues(item, count, default_item)
326
+ if self.createList(item, count)
327
+ old_width = @field_width
328
+
329
+ # Set the default item.
330
+ if default_item >= 0 && default_item < @list_size
331
+ @current_item = default_item
332
+ @default_item = default_item
333
+ end
334
+
335
+ # This will not resize the outer windows but can still make a usable
336
+ # field width if the title made the outer window wide enough
337
+ self.updateFieldWidth
338
+ if @field_width > old_width
339
+ self.createFieldWin(@field_win.getbegy, @field_win.getbegx)
340
+ end
341
+
342
+ # Draw the field.
343
+ self.erase
344
+ self.draw(@box)
345
+ end
346
+ end
347
+
348
+ def getValues(size)
349
+ size << @list_size
350
+ return @item
351
+ end
352
+
353
+ # This sets the default/current item of the itemlist
354
+ def setCurrentItem(current_item)
355
+ # Set the default item.
356
+ if current_item >= 0 && current_item < @list_size
357
+ @current_item = current_item
358
+ end
359
+ end
360
+
361
+ def getCurrentItem
362
+ return @current_item
363
+ end
364
+
365
+ # This sets the default item in the list.
366
+ def setDefaultItem(default_item)
367
+ # Make sure the item is in the correct range.
368
+ if default_item < 0
369
+ @default_item = 0
370
+ elsif default_item >= @list_size
371
+ @default_item = @list_size - 1
372
+ else
373
+ @default_item = default_item
374
+ end
375
+ end
376
+
377
+ def getDefaultItem
378
+ return @default_item
379
+ end
380
+
381
+ def focus
382
+ self.drawField(true)
383
+ end
384
+
385
+ def unfocus
386
+ self.drawField(false)
387
+ end
388
+
389
+ def createList(item, count)
390
+ status = false
391
+ new_items = []
392
+ new_pos = []
393
+ new_len = []
394
+ if count >= 0
395
+ field_width = 0
396
+
397
+ # Go through the list and determine the widest item.
398
+ status = true
399
+ (0...count).each do |x|
400
+ # Copy the item to the list.
401
+ lentmp = []
402
+ postmp = []
403
+ new_items << RNDK.char2Chtype(item[x], lentmp, postmp)
404
+ new_len << lentmp[0]
405
+ new_pos << postmp[0]
406
+ if new_items[0] == 0
407
+ status = false
408
+ break
409
+ end
410
+ field_width = [field_width, new_len[x]].max
411
+ end
412
+
413
+ # Now we need to justify the strings.
414
+ (0...count).each do |x|
415
+ new_pos[x] = RNDK.justifyString(field_width + 1,
416
+ new_len[x], new_pos[x])
417
+ end
418
+
419
+ if status
420
+ self.destroyInfo
421
+
422
+ # Copy in the new information
423
+ @list_size = count
424
+ @item = new_items
425
+ @item_pos = new_pos
426
+ @item_len = new_len
427
+ end
428
+ else
429
+ self.destroyInfo
430
+ status = true
431
+ end
432
+
433
+ return status
434
+ end
435
+
436
+ # Go through the list and determine the widest item.
437
+ def maximumFieldWidth
438
+ max_width = -2**30
439
+
440
+ (0...@list_size).each do |x|
441
+ max_width = [max_width, @item_len[x]].max
442
+ end
443
+ max_width = [max_width, 0].max
444
+
445
+ return max_width
446
+ end
447
+
448
+ def updateFieldWidth
449
+ want = self.maximumFieldWidth + 1
450
+ have = @box_width - @label_len - 2 * @border_size
451
+ @field_width = [want, have].min
452
+ end
453
+
454
+ # Make the field window.
455
+ def createFieldWin(ypos, xpos)
456
+ @field_win = Ncurses.subwin(@win, 1, @field_width, ypos, xpos)
457
+
458
+ unless @field_win.nil?
459
+ Ncurses.keypad(@field_win, true)
460
+ @input_window = @field_win
461
+ return true
462
+ end
463
+ return false
464
+ end
465
+
466
+ def position
467
+ super(@win)
468
+ end
469
+
470
+ def object_type
471
+ :ITEMLIST
472
+ end
473
+ end
474
+ end