Class: CDK::CDKOBJS

Inherits:
Object
  • Object
show all
Defined in:
lib/cdk/cdk_objs.rb

Constant Summary collapse

@@g_paste_buffer =
''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCDKOBJS

Returns a new instance of CDKOBJS.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cdk/cdk_objs.rb', line 9

def initialize
  @has_focus = true
  @is_visible = true

  CDK::ALL_OBJECTS << self

  # set default line-drawing characters
  @ULChar = Ncurses::ACS_ULCORNER
  @URChar = Ncurses::ACS_URCORNER
  @LLChar = Ncurses::ACS_LLCORNER
  @LRChar = Ncurses::ACS_LRCORNER
  @HZChar = Ncurses::ACS_HLINE
  @VTChar = Ncurses::ACS_VLINE
  @BXAttr = Ncurses::A_NORMAL

  # set default exit-types
  @exit_type = :NEVER_ACTIVATED
  @early_exit = :NEVER_ACTIVATED

  @accepts_focus = false

  # Bound functions
  @binding_list = {}
end

Instance Attribute Details

#accepts_focusObject (readonly)

Returns the value of attribute accepts_focus.



5
6
7
# File 'lib/cdk/cdk_objs.rb', line 5

def accepts_focus
  @accepts_focus
end

#binding_listObject (readonly)

Returns the value of attribute binding_list.



5
6
7
# File 'lib/cdk/cdk_objs.rb', line 5

def binding_list
  @binding_list
end

#border_sizeObject (readonly)

Returns the value of attribute border_size.



5
6
7
# File 'lib/cdk/cdk_objs.rb', line 5

def border_size
  @border_size
end

#boxObject

Returns the value of attribute box.



3
4
5
# File 'lib/cdk/cdk_objs.rb', line 3

def box
  @box
end

#BXAttrObject

Returns the value of attribute BXAttr.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def BXAttr
  @BXAttr
end

#exit_typeObject (readonly)

Returns the value of attribute exit_type.



5
6
7
# File 'lib/cdk/cdk_objs.rb', line 5

def exit_type
  @exit_type
end

#has_focusObject

Returns the value of attribute has_focus.



3
4
5
# File 'lib/cdk/cdk_objs.rb', line 3

def has_focus
  @has_focus
end

#HZCharObject

Returns the value of attribute HZChar.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def HZChar
  @HZChar
end

#is_visibleObject

Returns the value of attribute is_visible.



3
4
5
# File 'lib/cdk/cdk_objs.rb', line 3

def is_visible
  @is_visible
end

#LLCharObject

Returns the value of attribute LLChar.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def LLChar
  @LLChar
end

#LRCharObject

Returns the value of attribute LRChar.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def LRChar
  @LRChar
end

#screenObject

Returns the value of attribute screen.



3
4
5
# File 'lib/cdk/cdk_objs.rb', line 3

def screen
  @screen
end

#screen_indexObject

Returns the value of attribute screen_index.



3
4
5
# File 'lib/cdk/cdk_objs.rb', line 3

def screen_index
  @screen_index
end

#ULCharObject

Returns the value of attribute ULChar.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def ULChar
  @ULChar
end

#URCharObject

Returns the value of attribute URChar.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def URChar
  @URChar
end

#VTCharObject

Returns the value of attribute VTChar.



4
5
6
# File 'lib/cdk/cdk_objs.rb', line 4

def VTChar
  @VTChar
end

Instance Method Details

#bind(type, key, function, data) ⇒ Object



321
322
323
324
325
326
327
328
# File 'lib/cdk/cdk_objs.rb', line 321

def bind(type, key, function, data)
  obj = self.bindableObject(type)
  if key.ord < Ncurses::KEY_MAX && !(obj.nil?)
    if key.ord != 0
      obj.binding_list[key.ord] = [function, data]
    end
  end
end

#bindableObject(cdktype) ⇒ Object



311
312
313
314
315
316
317
318
319
# File 'lib/cdk/cdk_objs.rb', line 311

def bindableObject(cdktype)
  if cdktype != self.object_type
    return nil
  elsif [:FSELECT, :ALPHALIST].include?(self.object_type)
    return @entry_field
  else
    return self
  end
end

#checkBind(type, key) ⇒ Object

This checks to see if the binding for the key exists: If it does then it runs the command and returns its value, normally true If it doesn’t it returns a false. This way we can ‘overwrite’ coded bindings.



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/cdk/cdk_objs.rb', line 348

def checkBind(type, key)
  obj = self.bindableObject(type)
  if !(obj.nil?) && obj.binding_list.include?(key)
    function = obj.binding_list[key][0]
    data = obj.binding_list[key][1]

    if function == :getc
      return data
    else
      return function.call(type, obj, data, key)
    end
  end
  return false
end

#cleanBindings(type) ⇒ Object



337
338
339
340
341
342
# File 'lib/cdk/cdk_objs.rb', line 337

def cleanBindings(type)
  obj = self.bindableObject(type)
  if !(obj.nil?) && !(obj.binding_list.nil?)
    obj.binding_list.clear
  end
end

#cleanTitleObject

Remove storage for the widget’s title.



228
229
230
# File 'lib/cdk/cdk_objs.rb', line 228

def cleanTitle
  @title_lines = ''
end

#destroyObject



130
131
# File 'lib/cdk/cdk_objs.rb', line 130

def destroy
end

#draw(a) ⇒ Object



52
53
# File 'lib/cdk/cdk_objs.rb', line 52

def draw(a)
end

#drawTitle(win) ⇒ Object

Draw the widget’s title



219
220
221
222
223
224
225
# File 'lib/cdk/cdk_objs.rb', line 219

def drawTitle(win)
  (0...@title_lines).each do |x|
    Draw.writeChtype(@win, @title_pos[x] + @border_size,
        x + @border_size, @title[x], CDK::HORIZONTAL, 0,
        @title_len[x])
  end
end

#eraseObject



55
56
# File 'lib/cdk/cdk_objs.rb', line 55

def erase
end

#focusObject



118
119
# File 'lib/cdk/cdk_objs.rb', line 118

def focus
end

#getBoxObject



114
115
116
# File 'lib/cdk/cdk_objs.rb', line 114

def getBox
  return @box
end

#getcObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/cdk/cdk_objs.rb', line 268

def getc
  cdktype = self.object_type
  test = self.bindableObject(cdktype)
  result = @input_window.wgetch

  if result >= 0 && !(test.nil?) && test.binding_list.include?(result) &&
      test.binding_list[result][0] == :getc
    result = test.binding_list[result][1]
  elsif test.nil? || !(test.binding_list.include?(result)) ||
      test.binding_list[result][0].nil?
    case result
    when "\r".ord, "\n".ord
      result = Ncurses::KEY_ENTER
    when "\t".ord
      result = CDK::KEY_TAB
    when CDK::DELETE
      result = Ncurses::KEY_DC
    when "\b".ord
      result = Ncurses::KEY_BACKSPACE
    when CDK::BEGOFLINE
      result = Ncurses::KEY_HOME
    when CDK::ENDOFLINE
      result = Ncurses::KEY_END
    when CDK::FORCHAR
      result = Ncurses::KEY_RIGHT
    when CDK::BACKCHAR
      result = Ncurses::KEY_LEFT
    when CDK::NEXT
      result = CDK::KEY_TAB
    when CDK::PREV
      result = Ncurses::KEY_BTAB
    end
  end

  return result
end

#getch(function_key) ⇒ Object



305
306
307
308
309
# File 'lib/cdk/cdk_objs.rb', line 305

def getch(function_key)
  key = self.getc
  function_key << (key >= Ncurses::KEY_MIN && key <= Ncurses::KEY_MAX)
  return key
end

#inject(a) ⇒ Object



106
107
# File 'lib/cdk/cdk_objs.rb', line 106

def inject(a)
end

#isBind(type, key) ⇒ Object

This checks to see if the binding for the key exists.



364
365
366
367
368
369
370
371
372
# File 'lib/cdk/cdk_objs.rb', line 364

def isBind(type, key)
  result = false
  obj = self.bindableObject(type)
  unless obj.nil?
    result = obj.binding_list.include?(key)
  end

  return result
end

#move(xplace, yplace, relative, refresh_flag) ⇒ Object



58
59
60
61
# File 'lib/cdk/cdk_objs.rb', line 58

def move(xplace, yplace, relative, refresh_flag)
  self.move_specific(xplace, yplace, relative, refresh_flag,
      [@win, @shadow_win], [])
end

#move_specific(xplace, yplace, relative, refresh_flag, windows, subwidgets) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cdk/cdk_objs.rb', line 63

def move_specific(xplace, yplace, relative, refresh_flag,
    windows, subwidgets)
  current_x = @win.getbegx
  current_y = @win.getbegy
  xpos = xplace
  ypos = yplace

  # If this is a relative move, then we will adjust where we want
  # to move to.
  if relative
    xpos = @win.getbegx + xplace
    ypos = @win.getbegy + yplace
  end

  # Adjust the window if we need to
  xtmp = [xpos]
  ytmp = [ypos]
  CDK.alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Get the difference
  xdiff = current_x - xpos
  ydiff = current_y - ypos

  # Move the window to the new location.
  windows.each do |window|
    CDK.moveCursesWindow(window, -xdiff, -ydiff)
  end

  subwidgets.each do |subwidget|
    subwidget.move(xplace, yplace, relative, false)
  end

  # Touch the windows so they 'move'
  CDK::SCREEN.refreshCDKWindow(@screen.window)

  # Redraw the window, if they asked for it
  if refresh_flag
    self.draw(@box)
  end
end

#object_typeObject



34
35
36
37
# File 'lib/cdk/cdk_objs.rb', line 34

def object_type
  # no type by default
  :NULL
end

#position(win) ⇒ Object

This allows the user to use the cursor keys to adjust the postion of the widget.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/cdk/cdk_objs.rb', line 376

def position(win)
  parent = @screen.window
  orig_x = win.getbegx
  orig_y = win.getbegy
  beg_x = parent.getbegx
  beg_y = parent.getbegy
  end_x = beg_x + @screen.window.getmaxx
  end_y = beg_y + @screen.window.getmaxy

  # Let them move the widget around until they hit return.
  while !([CDK::KEY_RETURN, Ncurses::KEY_ENTER].include?(
      key = self.getch([])))
    case key
    when Ncurses::KEY_UP, '8'.ord
      if win.getbegy > beg_y
        self.move(0, -1, true, true)
      else
        CDK.Beep
      end
    when Ncurses::KEY_DOWN, '2'.ord
      if (win.getbegy + win.getmaxy) < end_y
        self.move(0, 1, true, true)
      else
        CDK.Beep
      end
    when Ncurses::KEY_LEFT, '4'.ord
      if win.getbegx > beg_x
        self.move(-1, 0, true, true)
      else
        CDK.Beep
      end
    when Ncurses::KEY_RIGHT, '6'.ord
      if (win.getbegx + win.getmaxx) < end_x
        self.move(1, 0, true, true)
      else
        CDK.Beep
      end
    when '7'.ord
      if win.getbegy > beg_y && win.getbegx > beg_x
        self.move(-1, -1, true, true)
      else
        CDK.Beep
      end
    when '9'.ord
      if (win.getbegx + win.getmaxx) < end_x && win.getbegy > beg_y
        self.move(1, -1, true, true)
      else
        CDK.Beep
      end
    when '1'.ord
      if win.getbegx > beg_x && (win.getbegy + win.getmaxy) < end_y
        self.move(-1, 1, true, true)
      else
        CDK.Beep
      end
    when '3'.ord
      if (win.getbegx + win.getmaxx) < end_x &&
          (win.getbegy + win.getmaxy) < end_y
        self.move(1, 1, true, true)
      else
        CDK.Beep
      end
    when '5'.ord
      self.move(CDK::CENTER, CDK::CENTER, false, true)
    when 't'.ord
      self.move(win.getbegx, CDK::TOP, false, true)
    when 'b'.ord
      self.move(win.getbegx, CDK::BOTTOM, false, true)
    when 'l'.ord
      self.move(CDK::LEFT, win.getbegy, false, true)
    when 'r'.ord
      self.move(CDK::RIGHT, win.getbegy, false, true)
    when 'c'.ord
      self.move(CDK::CENTER, win.getbegy, false, true)
    when 'C'.ord
      self.move(win.getbegx, CDK::CENTER, false, true)
    when CDK::REFRESH
      @screen.erase
      @screen.refresh
    when CDK::KEY_ESC
      self.move(orig_x, orig_y, false, true)
    else
      CDK.Beep
    end
  end
end

#refreshDataObject



127
128
# File 'lib/cdk/cdk_objs.rb', line 127

def refreshData
end

#saveDataObject



124
125
# File 'lib/cdk/cdk_objs.rb', line 124

def saveData
end

#SCREEN_XPOS(n) ⇒ Object



44
45
46
# File 'lib/cdk/cdk_objs.rb', line 44

def SCREEN_XPOS(n)
  n + @border_size
end

#SCREEN_YPOS(n) ⇒ Object



48
49
50
# File 'lib/cdk/cdk_objs.rb', line 48

def SCREEN_YPOS(n)
  n + @border_size + @title_lines
end

#setBackgroundColor(color) ⇒ Object

This sets the background color of the widget.



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cdk/cdk_objs.rb', line 169

def setBackgroundColor(color)
  return if color.nil? || color == ''

  junk1 = []
  junk2 = []
  
  # Convert the value of the environment variable to a chtype
  holder = CDK.char2Chtype(color, junk1, junk2)

  # Set the widget's background color
  self.setBKattr(holder[0])
end

#setBox(box) ⇒ Object



109
110
111
112
# File 'lib/cdk/cdk_objs.rb', line 109

def setBox(box)
  @box = box
  @border_size = if @box then 1 else 0 end
end

#setBXattr(ch) ⇒ Object

Set the object’s box-attributes.



164
165
166
# File 'lib/cdk/cdk_objs.rb', line 164

def setBXattr(ch)
  @BXAttr = ch
end

#setExitType(ch) ⇒ Object

Set the object’s exit-type based on the input. The .exitType field should have been part of the CDKOBJS struct, but it is used too pervasively in older applications to move (yet).



247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/cdk/cdk_objs.rb', line 247

def setExitType(ch)
  case ch
  when Ncurses::ERR
    @exit_type = :ERROR
  when CDK::KEY_ESC
    @exit_type = :ESCAPE_HIT
  when CDK::KEY_TAB, Ncurses::KEY_ENTER, CDK::KEY_RETURN
    @exit_type = :NORMAL
  when 0
    @exit_type = :EARLY_EXIT
  end
end

#setHZchar(ch) ⇒ Object

Set the object’s horizontal line-drawing character



154
155
156
# File 'lib/cdk/cdk_objs.rb', line 154

def setHZchar(ch)
  @HZChar = ch
end

#setLLchar(ch) ⇒ Object

Set the object’s lower-left-corner line-drawing character.



144
145
146
# File 'lib/cdk/cdk_objs.rb', line 144

def setLLchar(ch)
  @LLChar = ch
end

#setLRchar(ch) ⇒ Object

Set the object’s upper-right-corner line-drawing character.



149
150
151
# File 'lib/cdk/cdk_objs.rb', line 149

def setLRchar(ch)
  @LRChar = ch
end

#setPostProcess(fn, data) ⇒ Object

Set data for postprocessing



239
240
241
242
# File 'lib/cdk/cdk_objs.rb', line 239

def setPostProcess (fn, data)
  @post_process_func = fn
  @post_process_data = data
end

#setPreProcess(fn, data) ⇒ Object

Set data for preprocessing



233
234
235
236
# File 'lib/cdk/cdk_objs.rb', line 233

def setPreProcess (fn, data)
  @pre_process_func = fn
  @pre_process_data = data
end

#setTitle(title, box_width) ⇒ Object

Set the widget’s title.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cdk/cdk_objs.rb', line 183

def setTitle (title, box_width)
  if !title.nil? 
    temp = title.split("\n")
    @title_lines = temp.size
    
    if box_width >= 0
      max_width = 0
      temp.each do |line|
        len = []
        align = []
        holder = CDK.char2Chtype(line, len, align)
        max_width = [len[0], max_width].max
      end
      box_width = [box_width, max_width + 2 * @border_size].max
    else
      box_width = -(box_width - 1)
    end

    # For each line in the title convert from string to chtype array
    title_width = box_width - (2 * @border_size)
    @title = []
    @title_pos = []
    @title_len = []
    (0...@title_lines).each do |x|
      len_x = []
      pos_x = []
      @title << CDK.char2Chtype(temp[x], len_x, pos_x)
      @title_len.concat(len_x)
      @title_pos << CDK.justifyString(title_width, len_x[0], pos_x[0])
    end
  end

  return box_width
end

#setULchar(ch) ⇒ Object

Set the object’s upper-left-corner line-drawing character.



134
135
136
# File 'lib/cdk/cdk_objs.rb', line 134

def setULchar(ch)
  @ULChar = ch
end

#setURchar(ch) ⇒ Object

Set the object’s upper-right-corner line-drawing character.



139
140
141
# File 'lib/cdk/cdk_objs.rb', line 139

def setURchar(ch)
  @URChar = ch
end

#setVTchar(ch) ⇒ Object

Set the object’s vertical line-drawing character



159
160
161
# File 'lib/cdk/cdk_objs.rb', line 159

def setVTchar(ch)
  @VTChar = ch
end

#unbind(type, key) ⇒ Object



330
331
332
333
334
335
# File 'lib/cdk/cdk_objs.rb', line 330

def unbind(type, key)
  obj = self.bindableObject(type)
  unless obj.nil?
    obj.binding_list.delete(key)
  end
end

#unfocusObject



121
122
# File 'lib/cdk/cdk_objs.rb', line 121

def unfocus
end

#validCDKObjectObject



260
261
262
263
264
265
266
# File 'lib/cdk/cdk_objs.rb', line 260

def validCDKObject
  result = false
  if CDK::ALL_OBJECTS.include?(self)
    result = self.validObjType(self.object_type)
  end
  return result
end

#validObjType(type) ⇒ Object



39
40
41
42
# File 'lib/cdk/cdk_objs.rb', line 39

def validObjType(type)
  # dummy version for now
  true
end