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,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 = CDK::ACS_ULCORNER
17
+ @URChar = CDK::ACS_URCORNER
18
+ @LLChar = CDK::ACS_LLCORNER
19
+ @LRChar = CDK::ACS_LRCORNER
20
+ @HZChar = CDK::ACS_HLINE
21
+ @VTChar = CDK::ACS_VLINE
22
+ @BXAttr = Curses::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.begx
66
+ current_y = @win.begy
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.begx + xplace
74
+ ypos = @win.begy + 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.SetBackAttrObj(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 Curses::Error
250
+ @exit_type = :ERROR
251
+ when CDK::KEY_ESC
252
+ @exit_type = :ESCAPE_HIT
253
+ when CDK::KEY_TAB, Curses::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.getch
272
+
273
+ if result.ord >= 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", "\n"
280
+ result = Curses::KEY_ENTER
281
+ when "\t"
282
+ result = CDK::KEY_TAB
283
+ when CDK::DELETE
284
+ result = Curses::KEY_DC
285
+ when "\b"
286
+ result = Curses::KEY_BACKSPACE
287
+ when CDK::BEGOFLINE
288
+ result = Curses::KEY_HOME
289
+ when CDK::ENDOFLINE
290
+ result = Curses::KEY_END
291
+ when CDK::FORCHAR
292
+ result = Curses::KEY_RIGHT
293
+ when CDK::BACKCHAR
294
+ result = Curses::KEY_LEFT
295
+ when CDK::NEXT
296
+ result = CDK::KEY_TAB
297
+ when CDK::PREV
298
+ result = Curses::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.ord >= Curses::KEY_MIN && key.ord <= Curses::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 < Curses::KEY_MAX && !(obj.nil?)
324
+ if key.ord != 0
325
+ obj.binding_list[key] = [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.begx
379
+ orig_y = win.begy
380
+ beg_x = parent.begx
381
+ beg_y = parent.begy
382
+ end_x = beg_x + @screen.window.maxx
383
+ end_y = beg_y + @screen.window.maxy
384
+
385
+ # Let them move the widget around until they hit return.
386
+ while !([CDK::KEY_RETURN, Curses::KEY_ENTER].include?(
387
+ key = self.getch([])))
388
+ case key
389
+ when Curses::KEY_UP, '8'
390
+ if win.begy > beg_y
391
+ self.move(0, -1, true, true)
392
+ else
393
+ CDK.Beep
394
+ end
395
+ when Curses::KEY_DOWN, '2'
396
+ if (win.begy + win.maxy) < end_y
397
+ self.move(0, 1, true, true)
398
+ else
399
+ CDK.Beep
400
+ end
401
+ when Curses::KEY_LEFT, '4'
402
+ if win.begx > beg_x
403
+ self.move(-1, 0, true, true)
404
+ else
405
+ CDK.Beep
406
+ end
407
+ when Curses::KEY_RIGHT, '6'
408
+ if (win.begx + win.maxx) < end_x
409
+ self.move(1, 0, true, true)
410
+ else
411
+ CDK.Beep
412
+ end
413
+ when '7'
414
+ if win.begy > beg_y && win.begx > beg_x
415
+ self.move(-1, -1, true, true)
416
+ else
417
+ CDK.Beep
418
+ end
419
+ when '9'
420
+ if (win.begx + win.maxx) < end_x && win.begy > beg_y
421
+ self.move(1, -1, true, true)
422
+ else
423
+ CDK.Beep
424
+ end
425
+ when '1'
426
+ if win.begx > beg_x && (win.begy + win.maxy) < end_y
427
+ self.move(-1, 1, true, true)
428
+ else
429
+ CDK.Beep
430
+ end
431
+ when '3'
432
+ if (win.begx + win.maxx) < end_x &&
433
+ (win.begy + win.maxy) < end_y
434
+ self.move(1, 1, true, true)
435
+ else
436
+ CDK.Beep
437
+ end
438
+ when '5'
439
+ self.move(CDK::CENTER, CDK::CENTER, false, true)
440
+ when 't'
441
+ self.move(win.begx, CDK::TOP, false, true)
442
+ when 'b'
443
+ self.move(win.begx, CDK::BOTTOM, false, true)
444
+ when 'l'
445
+ self.move(CDK::LEFT, win.begy, false, true)
446
+ when 'r'
447
+ self.move(CDK::RIGHT, win.begy, false, true)
448
+ when 'c'
449
+ self.move(CDK::CENTER, win.begy, false, true)
450
+ when 'C'
451
+ self.move(win.begx, 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