cdk 0.9.0

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.
@@ -0,0 +1,61 @@
1
+ require_relative 'slider'
2
+
3
+ module CDK
4
+ class FSLIDER < CDK::SLIDER
5
+ def initialize(cdkscreen, xplace, yplace, title, label, filler,
6
+ field_width, start, low, high, inc, fast_inc, digits, box, shadow)
7
+ @digits = digits
8
+ super(cdkscreen, xplace, yplace, title, label, filler, field_width,
9
+ start, low, high, inc, fast_inc, box, shadow)
10
+ end
11
+
12
+ # This draws the widget.
13
+ def drawField
14
+ step = 1.0 * @field_width / (@high - @low)
15
+
16
+ # Determine how many filler characters need to be drawn.
17
+ filler_characters = (@current - @low) * step
18
+
19
+ @field_win.werase
20
+
21
+ # Add the character to the window.
22
+ (0...filler_characters).each do |x|
23
+ @field_win.mvwaddch(0, x, @filler)
24
+ end
25
+
26
+ # Draw the value in the field.
27
+ digits = [@digits, 30].min
28
+ format = '%%.%if' % [digits]
29
+ temp = format % [@current]
30
+
31
+ Draw.writeCharAttrib(@field_win, @field_width, 0, temp,
32
+ Ncurses::A_NORMAL, CDK::HORIZONTAL, 0, temp.size)
33
+
34
+ self.moveToEditPosition(@field_edit)
35
+ @field_win.wrefresh
36
+ end
37
+
38
+ def formattedSize(value)
39
+ digits = [@digits, 30].min
40
+ format = '%%.%if' % [digits]
41
+ temp = format % [value]
42
+ return temp.size
43
+ end
44
+
45
+ def setDigits(digits)
46
+ @digits = [0, digits].max
47
+ end
48
+
49
+ def getDigits
50
+ return @digits
51
+ end
52
+
53
+ def SCAN_FMT
54
+ '%g%c'
55
+ end
56
+
57
+ def object_type
58
+ :FSLIDER
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,410 @@
1
+ require_relative 'cdk_objs'
2
+
3
+ module CDK
4
+ class HISTOGRAM < CDK::CDKOBJS
5
+ def initialize(cdkscreen, xplace, yplace, height, width, orient,
6
+ title, box, shadow)
7
+ super()
8
+ parent_width = cdkscreen.window.getmaxx
9
+ parent_height = cdkscreen.window.getmaxy
10
+
11
+ self.setBox(box)
12
+
13
+ box_height = CDK.setWidgetDimension(parent_height, height, 2)
14
+ old_height = box_height
15
+
16
+ box_width = CDK.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
+ CDK.alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
38
+ xpos = xtmp[0]
39
+ ypos = ytmp[0]
40
+
41
+ # Set up the histogram data
42
+ @screen = cdkscreen
43
+ @parent = cdkscreen.window
44
+ @win = Ncurses::WINDOW.new(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
+ @win.keypad(true)
60
+
61
+ # Set up some default values.
62
+ @filler = '#'.ord | Ncurses::A_REVERSE
63
+ @stats_attr = Ncurses::A_NORMAL
64
+ @stats_pos = CDK::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::WINDOW.new(box_height, box_width,
82
+ ypos + 1, xpos + 1)
83
+ end
84
+
85
+ cdkscreen.register(:HISTOGRAM, self)
86
+ end
87
+
88
+ # This was added for the builder
89
+ def activate(actions)
90
+ self.draw(@box)
91
+ end
92
+
93
+ # Set various widget attributes
94
+ def set(view_type, stats_pos, stats_attr, low, high, value, filler, box)
95
+ self.setDisplayType(view_type)
96
+ self.setStatsPos(stats_pos)
97
+ self.setValue(low, high, value)
98
+ self.setFillerChar(filler)
99
+ self.setStatsAttr(stats_attr)
100
+ self.setBox(box)
101
+ end
102
+
103
+ # Set the values for the widget.
104
+ def setValue(low, high, value)
105
+ # We should error check the information we have.
106
+ @low = if low <= high then low else 0 end
107
+ @high = if low <= high then high else 0 end
108
+ @value = if low <= value && value <= high then value else 0 end
109
+ # Determine the percentage of the given value.
110
+ @percent = if @high == 0 then 0 else 1.0 * @value / @high end
111
+
112
+ # Determine the size of the histogram bar.
113
+ if @orient == CDK::VERTICAL
114
+ @bar_size = @percent * @field_height
115
+ else
116
+ @bar_size = @percent * @field_width
117
+ end
118
+
119
+ # We have a number of variables which determine the personality of the
120
+ # histogram. We have to go through each one methodically, and set them
121
+ # correctly. This section does this.
122
+ if @view_type != :NONE
123
+ if @orient == CDK::VERTICAL
124
+ if @stats_pos == CDK::LEFT || @stats_pos == CDK::BOTTOM
125
+ # Set the low label attributes.
126
+ @low_string = @low.to_s
127
+ @lowx = 1
128
+ @lowy = @box_height - @low_string.size - 1
129
+
130
+ # Set the high label attributes
131
+ @high_string = @high.to_s
132
+ @highx = 1
133
+ @highy = @title_lines + 1
134
+
135
+ string = ''
136
+ # Set the current value attributes.
137
+ string = if @view_type == :PERCENT
138
+ then "%3.1f%%" % [1.0 * @percent * 100]
139
+ elsif @view_type == :FRACTION
140
+ string = "%d/%d" % [@value, @high]
141
+ else string = @value.to_s
142
+ end
143
+ @cur_string = string
144
+ @curx = 1
145
+ @cury = (@field_height - string.size) / 2 + @title_lines + 1
146
+ elsif @stats_pos == CDK::CENTER
147
+ # Set the lower label attributes
148
+ @low_string = @low.to_s
149
+ @lowx = @field_width / 2 + 1
150
+ @lowy = @box_height - @low_string.size - 1
151
+
152
+ # Set the high label attributes
153
+ @high_string = @high.to_s
154
+ @highx = @field_width / 2 + 1
155
+ @highy = @title_lines + 1
156
+
157
+ # Set the stats label attributes
158
+ string = if @view_type == :PERCENT
159
+ then "%3.2f%%" % [1.0 * @percent * 100]
160
+ elsif @view_type == :FRACTIOn
161
+ "%d/%d" % [@value, @high]
162
+ else @value.to_s
163
+ end
164
+
165
+ @cur_string = string
166
+ @curx = @field_width / 2 + 1
167
+ @cury = (@field_height - string.size) / 2 + @title_lines + 1
168
+ elsif @stats_pos == CDK::RIGHT || @stats_pos == CDK::TOP
169
+ # Set the low label attributes.
170
+ @low_string = @low.to_s
171
+ @lowx = @field_width
172
+ @lowy = @box_height - @low_string.size - 1
173
+
174
+ # Set the high label attributes.
175
+ @high_string = @high.to_s
176
+ @highx = @field_width
177
+ @highy = @title_lines + 1
178
+
179
+ # Set the stats label attributes.
180
+ string = if @view_type == :PERCENT
181
+ then "%3.2f%%" % [1.0 * @percent * 100]
182
+ elsif @view_type == :FRACTION
183
+ "%d/%d" % [@value, @high]
184
+ else @value.to_s
185
+ end
186
+ @cur_string = string
187
+ @curx = @field_width
188
+ @cury = (@field_height - string.size) / 2 + @title_lines + 1
189
+ end
190
+ else
191
+ # Alignment is HORIZONTAL
192
+ if @stats_pos == CDK::TOP || @stats_pos == CDK::RIGHT
193
+ # Set the low label attributes.
194
+ @low_string = @low.to_s
195
+ @lowx = 1
196
+ @lowy = @title_lines + 1
197
+
198
+ # Set the high label attributes.
199
+ @high_string = @high.to_s
200
+ @highx = @box_width - @high_string.size - 1
201
+ @highy = @title_lines + 1
202
+
203
+ # Set the stats label attributes.
204
+ string = if @view_type == :PERCENT
205
+ then "%3.1f%%" % [1.0 * @percent * 100]
206
+ elsif @view_type == :FRACTION
207
+ "%d/%d" % [@value, @high]
208
+ else @value.to_s
209
+ end
210
+ @cur_string = string
211
+ @curx = (@field_width - @cur_string.size) / 2 + 1
212
+ @cury = @title_lines + 1
213
+ elsif @stats_pos == CDK::CENTER
214
+ # Set the low label attributes.
215
+ @low_string = @low.to_s
216
+ @lowx = 1
217
+ @lowy = (@field_height / 2) + @title_lines + 1
218
+
219
+ # Set the high label attributes.
220
+ @high_string = @high.to_s
221
+ @highx = @box_width - @high_string.size - 1
222
+ @highy = @field_height / 2 + @title_lines + 1
223
+
224
+ # Set the stats label attributes.
225
+ string = if @view_type == :PERCENT
226
+ then "%3.1f%%" % [1.0 * @percent * 100]
227
+ elsif @view_type == :FRACTION
228
+ "%d/%d" % [@value, @high]
229
+ else @value.to_s
230
+ end
231
+ @cur_string = string
232
+ @curx = (@field_width - @cur_string.size) / 2 + 1
233
+ @cury = @field_height / 2 + @title_lines + 1
234
+ elsif @stats_pos == CDK::BOTTOM || @stats_pos == CDK::LEFT
235
+ # Set the low label attributes.
236
+ @low_string = @low.to_s
237
+ @lowx = 1
238
+ @lowy = @box_height -2 * @border_size
239
+
240
+ # Set the high label attributes.
241
+ @high_string = @high.to_s
242
+ @highx = @box_width - @high_string.size - 1
243
+ @highy = @box_height - 2 * @border_size
244
+
245
+ # Set the stats label attributes.
246
+ string = if @view_type == :PERCENT
247
+ then "%3.1f%%" % [1.0 * @percent * 100]
248
+ elsif @view_type == :FRACTION
249
+ "%d/%d" % [@value, @high]
250
+ else @value.to_s
251
+ end
252
+ @cur_string = string
253
+ @curx = (@field_width - @cur_string.size) / 2 + 1
254
+ @cury = @box_height - 2 * @border_size
255
+ end
256
+ end
257
+ end
258
+ end
259
+
260
+ def getValue
261
+ return @value
262
+ end
263
+
264
+ def getLowValue
265
+ return @low
266
+ end
267
+
268
+ def getHighValue
269
+ return @high
270
+ end
271
+
272
+ # Set the histogram display type
273
+ def setDisplayType(view_type)
274
+ @view_type = view_type
275
+ end
276
+
277
+ def getViewType
278
+ return @view_type
279
+ end
280
+
281
+ # Set the position of the statistics information.
282
+ def setStatsPos(stats_pos)
283
+ @stats_pos = stats_pos
284
+ end
285
+
286
+ def getStatsPos
287
+ return @stats_pos
288
+ end
289
+
290
+ # Set the attribute of the statistics.
291
+ def setStatsAttr(stats_attr)
292
+ @stats_attr = stats_attr
293
+ end
294
+
295
+ def getStatsAttr
296
+ return @stats_attr
297
+ end
298
+
299
+ # Set the character to use when drawing the widget.
300
+ def setFillerChar(character)
301
+ @filler = character
302
+ end
303
+
304
+ def getFillerChar
305
+ return @filler
306
+ end
307
+
308
+ # Set the background attribute of the widget.
309
+ def setBKattr(attrib)
310
+ @win.wbkgd(attrib)
311
+ end
312
+
313
+ # Move the histogram field to the given location.
314
+ # Inherited
315
+ # def move(xplace, yplace, relative, refresh_flag)
316
+ # end
317
+
318
+ # Draw the widget.
319
+ def draw(box)
320
+ battr = 0
321
+ bchar = 0
322
+ fattr = @filler & Ncurses::A_ATTRIBUTES
323
+ hist_x = @title_lines + 1
324
+ hist_y = @bar_size
325
+
326
+ @win.werase
327
+
328
+ # Box the widget if asked.
329
+ if box
330
+ Draw.drawObjBox(@win, self)
331
+ end
332
+
333
+ # Do we have a shadow to draw?
334
+ if !(@shadow.nil?)
335
+ Draw.drawShadow(@shadow_win)
336
+ end
337
+
338
+ self.drawTitle(@win)
339
+
340
+ # If the user asked for labels, draw them in.
341
+ if @view_type != :NONE
342
+ # Draw in the low label.
343
+ if @low_string.size > 0
344
+ Draw.writeCharAttrib(@win, @lowx, @lowy, @low_string,
345
+ @stats_attr, @orient, 0, @low_string.size)
346
+ end
347
+
348
+ # Draw in the current value label.
349
+ if @cur_string.size > 0
350
+ Draw.writeCharAttrib(@win, @curx, @cury, @cur_string,
351
+ @stats_attr, @orient, 0, @cur_string.size)
352
+ end
353
+
354
+ # Draw in the high label.
355
+ if @high_string.size > 0
356
+ Draw.writeCharAttrib(@win, @highx, @highy, @high_string,
357
+ @stats_attr, @orient, 0, @high_string.size)
358
+ end
359
+ end
360
+
361
+ if @orient == CDK::VERTICAL
362
+ hist_x = @box_height - @bar_size - 1
363
+ hist_y = @field_width
364
+ end
365
+
366
+ # Draw the histogram bar.
367
+ (hist_x...@box_height - 1).to_a.each do |x|
368
+ (1..hist_y).each do |y|
369
+ battr = @win.mvwinch(x, y)
370
+
371
+ if battr == ' '.ord
372
+ @win.mvwaddch(x, y, @filler)
373
+ else
374
+ @win.mvwaddch(x, y, battr | fattr)
375
+ end
376
+ end
377
+ end
378
+
379
+ # Refresh the window
380
+ @win.wrefresh
381
+ end
382
+
383
+ # Destroy the widget.
384
+ def destroy
385
+ self.cleanTitle
386
+
387
+ # Clean up the windows.
388
+ CDK.deleteCursesWindow(@shadow_win)
389
+ CDK.deleteCursesWindow(@win)
390
+
391
+ # Clean the key bindings.
392
+ self.cleanBindings(:HISTOGRAM)
393
+
394
+ # Unregister this object.
395
+ CDK::SCREEN.unregister(:HISTOGRAM, self)
396
+ end
397
+
398
+ # Erase the widget from the screen.
399
+ def erase
400
+ if self.validCDKObject
401
+ CDK.eraseCursesWindow(@win)
402
+ CDK.eraseCursesWindow(@shadow_win)
403
+ end
404
+ end
405
+
406
+ def object_type
407
+ :HISTOGRAM
408
+ end
409
+ end
410
+ end