cdk 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,463 @@
1
+ module CDK
2
+ class CDKOBJS
3
+ attr_accessor :screen_index, :screen, :has_focus, :is_visible, :box
4
+ attr_accessor :ULChar, :URChar, :LLChar, :LRChar, :HZChar, :VTChar, :BXAttr
5
+ attr_reader :binding_list, :accepts_focus, :exit_type, :border_size
6
+
7
+ @@g_paste_buffer = ''
8
+
9
+ def initialize
10
+ @has_focus = true
11
+ @is_visible = true
12
+
13
+ CDK::ALL_OBJECTS << self
14
+
15
+ # set default line-drawing characters
16
+ @ULChar = Ncurses::ACS_ULCORNER
17
+ @URChar = Ncurses::ACS_URCORNER
18
+ @LLChar = Ncurses::ACS_LLCORNER
19
+ @LRChar = Ncurses::ACS_LRCORNER
20
+ @HZChar = Ncurses::ACS_HLINE
21
+ @VTChar = Ncurses::ACS_VLINE
22
+ @BXAttr = Ncurses::A_NORMAL
23
+
24
+ # set default exit-types
25
+ @exit_type = :NEVER_ACTIVATED
26
+ @early_exit = :NEVER_ACTIVATED
27
+
28
+ @accepts_focus = false
29
+
30
+ # Bound functions
31
+ @binding_list = {}
32
+ end
33
+
34
+ def object_type
35
+ # no type by default
36
+ :NULL
37
+ end
38
+
39
+ def validObjType(type)
40
+ # dummy version for now
41
+ true
42
+ end
43
+
44
+ def SCREEN_XPOS(n)
45
+ n + @border_size
46
+ end
47
+
48
+ def SCREEN_YPOS(n)
49
+ n + @border_size + @title_lines
50
+ end
51
+
52
+ def draw(a)
53
+ end
54
+
55
+ def erase
56
+ end
57
+
58
+ def move(xplace, yplace, relative, refresh_flag)
59
+ self.move_specific(xplace, yplace, relative, refresh_flag,
60
+ [@win, @shadow_win], [])
61
+ end
62
+
63
+ def move_specific(xplace, yplace, relative, refresh_flag,
64
+ windows, subwidgets)
65
+ current_x = @win.getbegx
66
+ current_y = @win.getbegy
67
+ xpos = xplace
68
+ ypos = yplace
69
+
70
+ # If this is a relative move, then we will adjust where we want
71
+ # to move to.
72
+ if relative
73
+ xpos = @win.getbegx + xplace
74
+ ypos = @win.getbegy + yplace
75
+ end
76
+
77
+ # Adjust the window if we need to
78
+ xtmp = [xpos]
79
+ ytmp = [ypos]
80
+ CDK.alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
81
+ xpos = xtmp[0]
82
+ ypos = ytmp[0]
83
+
84
+ # Get the difference
85
+ xdiff = current_x - xpos
86
+ ydiff = current_y - ypos
87
+
88
+ # Move the window to the new location.
89
+ windows.each do |window|
90
+ CDK.moveCursesWindow(window, -xdiff, -ydiff)
91
+ end
92
+
93
+ subwidgets.each do |subwidget|
94
+ subwidget.move(xplace, yplace, relative, false)
95
+ end
96
+
97
+ # Touch the windows so they 'move'
98
+ CDK::SCREEN.refreshCDKWindow(@screen.window)
99
+
100
+ # Redraw the window, if they asked for it
101
+ if refresh_flag
102
+ self.draw(@box)
103
+ end
104
+ end
105
+
106
+ def inject(a)
107
+ end
108
+
109
+ def setBox(box)
110
+ @box = box
111
+ @border_size = if @box then 1 else 0 end
112
+ end
113
+
114
+ def getBox
115
+ return @box
116
+ end
117
+
118
+ def focus
119
+ end
120
+
121
+ def unfocus
122
+ end
123
+
124
+ def saveData
125
+ end
126
+
127
+ def refreshData
128
+ end
129
+
130
+ def destroy
131
+ end
132
+
133
+ # Set the object's upper-left-corner line-drawing character.
134
+ def setULchar(ch)
135
+ @ULChar = ch
136
+ end
137
+
138
+ # Set the object's upper-right-corner line-drawing character.
139
+ def setURchar(ch)
140
+ @URChar = ch
141
+ end
142
+
143
+ # Set the object's lower-left-corner line-drawing character.
144
+ def setLLchar(ch)
145
+ @LLChar = ch
146
+ end
147
+
148
+ # Set the object's upper-right-corner line-drawing character.
149
+ def setLRchar(ch)
150
+ @LRChar = ch
151
+ end
152
+
153
+ # Set the object's horizontal line-drawing character
154
+ def setHZchar(ch)
155
+ @HZChar = ch
156
+ end
157
+
158
+ # Set the object's vertical line-drawing character
159
+ def setVTchar(ch)
160
+ @VTChar = ch
161
+ end
162
+
163
+ # Set the object's box-attributes.
164
+ def setBXattr(ch)
165
+ @BXAttr = ch
166
+ end
167
+
168
+ # This sets the background color of the widget.
169
+ def setBackgroundColor(color)
170
+ return if color.nil? || color == ''
171
+
172
+ junk1 = []
173
+ junk2 = []
174
+
175
+ # Convert the value of the environment variable to a chtype
176
+ holder = CDK.char2Chtype(color, junk1, junk2)
177
+
178
+ # Set the widget's background color
179
+ self.setBKattr(holder[0])
180
+ end
181
+
182
+ # Set the widget's title.
183
+ def setTitle (title, box_width)
184
+ if !title.nil?
185
+ temp = title.split("\n")
186
+ @title_lines = temp.size
187
+
188
+ if box_width >= 0
189
+ max_width = 0
190
+ temp.each do |line|
191
+ len = []
192
+ align = []
193
+ holder = CDK.char2Chtype(line, len, align)
194
+ max_width = [len[0], max_width].max
195
+ end
196
+ box_width = [box_width, max_width + 2 * @border_size].max
197
+ else
198
+ box_width = -(box_width - 1)
199
+ end
200
+
201
+ # For each line in the title convert from string to chtype array
202
+ title_width = box_width - (2 * @border_size)
203
+ @title = []
204
+ @title_pos = []
205
+ @title_len = []
206
+ (0...@title_lines).each do |x|
207
+ len_x = []
208
+ pos_x = []
209
+ @title << CDK.char2Chtype(temp[x], len_x, pos_x)
210
+ @title_len.concat(len_x)
211
+ @title_pos << CDK.justifyString(title_width, len_x[0], pos_x[0])
212
+ end
213
+ end
214
+
215
+ return box_width
216
+ end
217
+
218
+ # Draw the widget's title
219
+ def drawTitle(win)
220
+ (0...@title_lines).each do |x|
221
+ Draw.writeChtype(@win, @title_pos[x] + @border_size,
222
+ x + @border_size, @title[x], CDK::HORIZONTAL, 0,
223
+ @title_len[x])
224
+ end
225
+ end
226
+
227
+ # Remove storage for the widget's title.
228
+ def cleanTitle
229
+ @title_lines = ''
230
+ end
231
+
232
+ # Set data for preprocessing
233
+ def setPreProcess (fn, data)
234
+ @pre_process_func = fn
235
+ @pre_process_data = data
236
+ end
237
+
238
+ # Set data for postprocessing
239
+ def setPostProcess (fn, data)
240
+ @post_process_func = fn
241
+ @post_process_data = data
242
+ end
243
+
244
+ # Set the object's exit-type based on the input.
245
+ # The .exitType field should have been part of the CDKOBJS struct, but it
246
+ # is used too pervasively in older applications to move (yet).
247
+ def setExitType(ch)
248
+ case ch
249
+ when Ncurses::ERR
250
+ @exit_type = :ERROR
251
+ when CDK::KEY_ESC
252
+ @exit_type = :ESCAPE_HIT
253
+ when CDK::KEY_TAB, Ncurses::KEY_ENTER, CDK::KEY_RETURN
254
+ @exit_type = :NORMAL
255
+ when 0
256
+ @exit_type = :EARLY_EXIT
257
+ end
258
+ end
259
+
260
+ def validCDKObject
261
+ result = false
262
+ if CDK::ALL_OBJECTS.include?(self)
263
+ result = self.validObjType(self.object_type)
264
+ end
265
+ return result
266
+ end
267
+
268
+ def getc
269
+ cdktype = self.object_type
270
+ test = self.bindableObject(cdktype)
271
+ result = @input_window.wgetch
272
+
273
+ if result >= 0 && !(test.nil?) && test.binding_list.include?(result) &&
274
+ test.binding_list[result][0] == :getc
275
+ result = test.binding_list[result][1]
276
+ elsif test.nil? || !(test.binding_list.include?(result)) ||
277
+ test.binding_list[result][0].nil?
278
+ case result
279
+ when "\r".ord, "\n".ord
280
+ result = Ncurses::KEY_ENTER
281
+ when "\t".ord
282
+ result = CDK::KEY_TAB
283
+ when CDK::DELETE
284
+ result = Ncurses::KEY_DC
285
+ when "\b".ord
286
+ result = Ncurses::KEY_BACKSPACE
287
+ when CDK::BEGOFLINE
288
+ result = Ncurses::KEY_HOME
289
+ when CDK::ENDOFLINE
290
+ result = Ncurses::KEY_END
291
+ when CDK::FORCHAR
292
+ result = Ncurses::KEY_RIGHT
293
+ when CDK::BACKCHAR
294
+ result = Ncurses::KEY_LEFT
295
+ when CDK::NEXT
296
+ result = CDK::KEY_TAB
297
+ when CDK::PREV
298
+ result = Ncurses::KEY_BTAB
299
+ end
300
+ end
301
+
302
+ return result
303
+ end
304
+
305
+ def getch(function_key)
306
+ key = self.getc
307
+ function_key << (key >= Ncurses::KEY_MIN && key <= Ncurses::KEY_MAX)
308
+ return key
309
+ end
310
+
311
+ def bindableObject(cdktype)
312
+ if cdktype != self.object_type
313
+ return nil
314
+ elsif [:FSELECT, :ALPHALIST].include?(self.object_type)
315
+ return @entry_field
316
+ else
317
+ return self
318
+ end
319
+ end
320
+
321
+ def bind(type, key, function, data)
322
+ obj = self.bindableObject(type)
323
+ if key.ord < Ncurses::KEY_MAX && !(obj.nil?)
324
+ if key.ord != 0
325
+ obj.binding_list[key.ord] = [function, data]
326
+ end
327
+ end
328
+ end
329
+
330
+ def unbind(type, key)
331
+ obj = self.bindableObject(type)
332
+ unless obj.nil?
333
+ obj.binding_list.delete(key)
334
+ end
335
+ end
336
+
337
+ def cleanBindings(type)
338
+ obj = self.bindableObject(type)
339
+ if !(obj.nil?) && !(obj.binding_list.nil?)
340
+ obj.binding_list.clear
341
+ end
342
+ end
343
+
344
+ # This checks to see if the binding for the key exists:
345
+ # If it does then it runs the command and returns its value, normally true
346
+ # If it doesn't it returns a false. This way we can 'overwrite' coded
347
+ # bindings.
348
+ def checkBind(type, key)
349
+ obj = self.bindableObject(type)
350
+ if !(obj.nil?) && obj.binding_list.include?(key)
351
+ function = obj.binding_list[key][0]
352
+ data = obj.binding_list[key][1]
353
+
354
+ if function == :getc
355
+ return data
356
+ else
357
+ return function.call(type, obj, data, key)
358
+ end
359
+ end
360
+ return false
361
+ end
362
+
363
+ # This checks to see if the binding for the key exists.
364
+ def isBind(type, key)
365
+ result = false
366
+ obj = self.bindableObject(type)
367
+ unless obj.nil?
368
+ result = obj.binding_list.include?(key)
369
+ end
370
+
371
+ return result
372
+ end
373
+
374
+ # This allows the user to use the cursor keys to adjust the
375
+ # postion of the widget.
376
+ def position(win)
377
+ parent = @screen.window
378
+ orig_x = win.getbegx
379
+ orig_y = win.getbegy
380
+ beg_x = parent.getbegx
381
+ beg_y = parent.getbegy
382
+ end_x = beg_x + @screen.window.getmaxx
383
+ end_y = beg_y + @screen.window.getmaxy
384
+
385
+ # Let them move the widget around until they hit return.
386
+ while !([CDK::KEY_RETURN, Ncurses::KEY_ENTER].include?(
387
+ key = self.getch([])))
388
+ case key
389
+ when Ncurses::KEY_UP, '8'.ord
390
+ if win.getbegy > beg_y
391
+ self.move(0, -1, true, true)
392
+ else
393
+ CDK.Beep
394
+ end
395
+ when Ncurses::KEY_DOWN, '2'.ord
396
+ if (win.getbegy + win.getmaxy) < end_y
397
+ self.move(0, 1, true, true)
398
+ else
399
+ CDK.Beep
400
+ end
401
+ when Ncurses::KEY_LEFT, '4'.ord
402
+ if win.getbegx > beg_x
403
+ self.move(-1, 0, true, true)
404
+ else
405
+ CDK.Beep
406
+ end
407
+ when Ncurses::KEY_RIGHT, '6'.ord
408
+ if (win.getbegx + win.getmaxx) < end_x
409
+ self.move(1, 0, true, true)
410
+ else
411
+ CDK.Beep
412
+ end
413
+ when '7'.ord
414
+ if win.getbegy > beg_y && win.getbegx > beg_x
415
+ self.move(-1, -1, true, true)
416
+ else
417
+ CDK.Beep
418
+ end
419
+ when '9'.ord
420
+ if (win.getbegx + win.getmaxx) < end_x && win.getbegy > beg_y
421
+ self.move(1, -1, true, true)
422
+ else
423
+ CDK.Beep
424
+ end
425
+ when '1'.ord
426
+ if win.getbegx > beg_x && (win.getbegy + win.getmaxy) < end_y
427
+ self.move(-1, 1, true, true)
428
+ else
429
+ CDK.Beep
430
+ end
431
+ when '3'.ord
432
+ if (win.getbegx + win.getmaxx) < end_x &&
433
+ (win.getbegy + win.getmaxy) < end_y
434
+ self.move(1, 1, true, true)
435
+ else
436
+ CDK.Beep
437
+ end
438
+ when '5'.ord
439
+ self.move(CDK::CENTER, CDK::CENTER, false, true)
440
+ when 't'.ord
441
+ self.move(win.getbegx, CDK::TOP, false, true)
442
+ when 'b'.ord
443
+ self.move(win.getbegx, CDK::BOTTOM, false, true)
444
+ when 'l'.ord
445
+ self.move(CDK::LEFT, win.getbegy, false, true)
446
+ when 'r'.ord
447
+ self.move(CDK::RIGHT, win.getbegy, false, true)
448
+ when 'c'.ord
449
+ self.move(CDK::CENTER, win.getbegy, false, true)
450
+ when 'C'.ord
451
+ self.move(win.getbegx, CDK::CENTER, false, true)
452
+ when CDK::REFRESH
453
+ @screen.erase
454
+ @screen.refresh
455
+ when CDK::KEY_ESC
456
+ self.move(orig_x, orig_y, false, true)
457
+ else
458
+ CDK.Beep
459
+ end
460
+ end
461
+ end
462
+ end
463
+ end