Class: CDK::TEMPLATE

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/components/template.rb

Instance Attribute Summary

Attributes included from HasTitle

#title_attrib

Attributes included from HasScreen

#is_visible, #screen, #screen_index

Attributes included from ExitConditions

#exit_type

Attributes included from Bindings

#binding_list

Attributes included from Focusable

#accepts_focus, #has_focus

Attributes included from Borders

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #border_size, #box

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CDKOBJS

#setBackgroundColor, #timeout, #validCDKObject, #validObjType

Methods included from WindowHooks

#refreshData, #saveData

Methods included from WindowInput

#getc, #getch, #setPostProcess, #setPreProcess

Methods included from HasTitle

#cleanTitle, #drawTitle, #init_title, #setTitle

Methods included from HasScreen

#SCREEN_XPOS, #SCREEN_YPOS, #init_screen, #wrefresh

Methods included from ExitConditions

#init_exit_conditions, #resetExitType, #setExitType

Methods included from Bindings

#bind, #bindableObject, #checkBind, #cleanBindings, #init_bindings, #isBind, #unbind

Methods included from Focusable

#init_focus

Methods included from Borders

#getBox, #init_borders, #setBXattr, #setBox, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar

Methods included from Movement

#move_specific

Methods included from Converters

#char2Chtype, #charOf, #chtype2Char, #chtype2String, #decode_attribute, #encode_attribute

Methods included from Justifications

#justify_string

Methods included from Alignments

#alignxy

Constructor Details

#initialize(cdkscreen, xplace, yplace, title, label, plate, overlay, box, shadow) ⇒ TEMPLATE

Returns a new instance of TEMPLATE.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cdk/components/template.rb', line 5

def initialize(cdkscreen, xplace, yplace, title, label, plate,
    overlay, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = 0
  box_height = if box then 3 else 1 end
  plate_len = 0

  if plate.nil? || plate.size == 0
    return nil
  end

  self.setBox(box)

  field_width = plate.size + 2 * @border_size

  # Set some basic values of the template field.
  @label = []
  @label_len = 0
  @label_win = nil

  # Translate the label string to achtype array
  if !(label.nil?) && label.size > 0
    label_len  = []
    @label     = char2Chtype(label, label_len, [])
    @label_len = label_len[0]
  end

  # Translate the char * overlay to a chtype array
  if !(overlay.nil?) && overlay.size > 0
    overlay_len  = []
    @overlay     = char2Chtype(overlay, overlay_len, [])
    @overlay_len = overlay_len[0]
    @field_attr  = @overlay[0] & Ncurses::A_ATTRIBUTES
  else
    @overlay = []
    @overlay_len = 0
    @field_attr = Ncurses::A_NORMAL
  end

  # Set the box width.
  box_width = field_width + @label_len + 2 * @border_size

  old_width = box_width
  box_width = self.setTitle(title, box_width)
  horizontal_adjust = (box_width - old_width) / 2

  box_height += @title_lines

  # Make sure we didn't extend beyond the dimensions of the window.
  box_width = [box_width, parent_width].min
  box_height = [box_height, parent_height].min
  field_width = [field_width,
      box_width - @label_len - 2 * @border_size].min

  # Rejustify the x and y positions if we need to.
  xtmp = [xplace]
  ytmp = [yplace]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Make the template window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)

  # Is the template window nil?
  if @win.nil?
    self.destroy
    return nil
  end
  @win.keypad(true)

  # Make the label window.
  if label.size > 0
    @label_win = @win.subwin(1, @label_len,
        ypos + @title_lines + @border_size,
        xpos + horizontal_adjust + @border_size)
  end

  # Make the field window
  @field_win = @win.subwin(1, field_width,
        ypos + @title_lines + @border_size,
        xpos + @label_len + horizontal_adjust + @border_size)
  @field_win.keypad(true)

  # Set up the info field.
  @plate_len = plate.size
  @info = ''
  # Copy the plate to the template
  @plate = plate.clone

  # Set up the rest of the structure.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @shadow_win = nil
  @field_width = field_width
  @box_height = box_height
  @box_width = box_width
  @plate_pos = 0
  @screen_pos = 0
  @info_pos = 0
  @min = 0
  @input_window = @win
  @accepts_focus = true
  @shadow = shadow
  @callbackfn = lambda do |template, input|
    failed = false
    change = false
    moveby = false
    amount = 0
    mark = @info_pos
    have = @info.size

    if input == Ncurses::KEY_LEFT
      if mark != 0
        moveby = true
        amount = -1
      else
        failed = true
      end
    elsif input == Ncurses::KEY_RIGHT
      if mark < @info.size
        moveby = true
        amount = 1
      else
        failed = true
      end
    else
      test = @info.clone
      if input == Ncurses::KEY_BACKSPACE
        if mark != 0
          front = @info[0...mark-1] || ''
          back = @info[mark..-1] || ''
          test = front + back
          change = true
          amount = -1
        else
          failed = true
        end
      elsif input == Ncurses::KEY_DC
        if mark < @info.size
          front = @info[0...mark] || ''
          back = @info[mark+1..-1] || ''
          test = front + back
          change = true
          amount = 0
        else
          failed = true
        end
      elsif CDK.isChar(input) && @plate_pos < @plate.size
        test[mark] = input.chr
        change = true
        amount = 1
      else
        failed = true
      end

      if change
        if self.validTemplate(test)
          @info = test
          self.drawField
        else
          failed = true
        end
      end
    end

    if failed
      CDK.Beep
    elsif change || moveby
      @info_pos += amount
      @plate_pos += amount
      @screen_pos += amount

      self.adjustCursor(amount)
    end
  end

  # Do we need to create a shadow?
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  cdkscreen.register(:TEMPLATE, self)
end

Class Method Details

.isPlateChar(c) ⇒ Object



550
551
552
# File 'lib/cdk/components/template.rb', line 550

def self.isPlateChar(c)
  '#ACcMXz'.include?(c.chr)
end

Instance Method Details

#activate(actions) ⇒ Object

This actually manages the tempalte widget



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cdk/components/template.rb', line 194

def activate(actions)
  self.draw(@box)

  if actions.nil? || actions.size == 0
    while true
      input = self.getch([])

      # Inject each character into the widget.
      ret = self.inject(input)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  else
    # Inject each character one at a time.
    actions.each do |action|
      ret = self.inject(action)
      if @exit_type != :EARLY_EXIT
        return ret
      end
    end
  end

  # Set the exit type and return.
  self.setExitType(0)
  return ret
end

#adjustCursor(direction) ⇒ Object

Adjust the cursor for the template



442
443
444
445
446
447
448
449
450
# File 'lib/cdk/components/template.rb', line 442

def adjustCursor(direction)
  while @plate_pos < [@field_width, @plate.size].min &&
      !CDK::TEMPLATE.isPlateChar(@plate[@plate_pos])
    @plate_pos += direction
    @screen_pos += direction
  end
  @field_win.wmove(0, @screen_pos)
  wrefresh(@field_win)
end

#cleanObject

Erase the information in the template widget.



530
531
532
533
534
535
# File 'lib/cdk/components/template.rb', line 530

def clean
  @info = ''
  @screen_pos = 0
  @info_pos = 0
  @plaste_pos = 0
end

#destroyObject

Destroy this widget.



462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/cdk/components/template.rb', line 462

def destroy
  self.cleanTitle

  # Delete the windows
  CDK.deleteCursesWindow(@field_win)
  CDK.deleteCursesWindow(@label_win)
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  # Clean the key bindings.
  self.cleanBindings(:TEMPLATE)

  CDK::SCREEN.unregister(:TEMPLATE, self)
end

#draw(box) ⇒ Object

Draw the template widget.



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/cdk/components/template.rb', line 389

def draw(box)
  # Do we need to draw the shadow.
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end

  # Box it if needed
  if box
    Draw.drawObjBox(@win, self)
  end

  self.drawTitle(@win)

  wrefresh

  self.drawField
end

#drawFieldObject

Draw the template field



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
# File 'lib/cdk/components/template.rb', line 408

def drawField
  field_color = 0
  
  # Draw in the label and the template object.
  unless @label_win.nil?
    Draw.writeChtype(@label_win, 0, 0, @label, CDK::HORIZONTAL,
        0, @label_len)
    wrefresh(@label_win)
  end

  # Draw in the template
  if @overlay.size > 0
    Draw.writeChtype(@field_win, 0, 0, @overlay, CDK::HORIZONTAL,
        0, @overlay_len)
  end

  # Adjust the cursor.
  if @info.size > 0
    pos = 0
    (0...[@field_width, @plate.size].min).each do |x|
      if CDK::TEMPLATE.isPlateChar(@plate[x]) && pos < @info.size
        field_color = @overlay[x] & Ncurses::A_ATTRIBUTES
        @field_win.mvwaddch(0, x, @info[pos].ord | field_color)
        pos += 1
      end
    end
    @field_win.wmove(0, @screen_pos)
  else
    self.adjustCursor(1)
  end
  wrefresh(@field_win)
end

#eraseObject

Erase the widget.



478
479
480
481
482
483
484
485
# File 'lib/cdk/components/template.rb', line 478

def erase
  if self.validCDKObject
    CDK.eraseCursesWindow(@field_win)
    CDK.eraseCursesWindow(@label_win)
    CDK.eraseCursesWindow(@shadow_win)
    CDK.eraseCursesWindow(@win)
  end
end

#focusObject



542
543
544
# File 'lib/cdk/components/template.rb', line 542

def focus
  self.draw(@box)
end

#getMinObject



525
526
527
# File 'lib/cdk/components/template.rb', line 525

def getMin
  return @min
end

#getValueObject



514
515
516
# File 'lib/cdk/components/template.rb', line 514

def getValue
  return @info
end

#inject(input) ⇒ Object

This injects a character into the widget.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
304
305
306
307
308
309
310
311
# File 'lib/cdk/components/template.rb', line 223

def inject(input)
  pp_return = 1
  @complete = false
  ret = -1

  self.setExitType(0)

  # Move the cursor.
  self.drawField

  # Check if there is a pre-process function to be called.
  unless @pre_process_func.nil?
    pp_return = @pre_process_func.call(:TEMPLATE, self,
        @pre_process_data, input)
  end

  # Should we continue?
  if pp_return != 0
    # Check a predefined binding
    if self.checkBind(:TEMPLATE, input)
      @complete = true
    else
      case input
      when CDK::ERASE
        if @info.size > 0
          self.clean
          self.drawField
        end
      when CDK::CUT
        if @info.size > 0
          @@g_paste_buffer = @info.clone
          self.clean
          self.drawField
        else
          CDK.Beep
        end
      when CDK::COPY
        if @info.size > 0
          @@g_paste_buffer = @info.clone
        else
          CDK.Beep
        end
      when CDK::PASTE
        if @@g_paste_buffer.size > 0
          self.clean

          # Start inserting each character one at a time.
          (0...@@g_paste_buffer.size).each do |x|
            @callbackfn.call(self, @@g_paste_buffer[x])
          end
          self.drawField
        else
          CDK.Beep
        end
      when CDK::KEY_TAB, CDK::KEY_RETURN, Ncurses::KEY_ENTER
        if @info.size < @min
          CDK.Beep
        else
          self.setExitType(input)
          ret = @info
          @complete = true
        end
      when CDK::KEY_ESC
        self.setExitType(input)
        @complete = true
      when Ncurses::ERR
        self.setExitType(input)
        @complete = true
      when CDK::REFRESH
        @screen.erase
        @screen.refresh
      else
        @callbackfn.call(self, input)
      end
    end

    # Should we call a post-process?
    if !@complete && !(@post_process_func.nil?)
      @post_process_func.call(:TEMPLATE, self, @post_process_data, input)
    end
  end

  if !@complete
    self.setExitType(0)
  end

  @return_data = ret
  return ret
end

#mixObject

Return a mixture of the plate-overlay and field-info



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/cdk/components/template.rb', line 347

def mix
  mixed_string = ''
  plate_pos = 0
  info_pos = 0

  if @info.size > 0
    mixed_string = ''
    while plate_pos < @plate_len && info_pos < @info.size
      mixed_string << if CDK::TEMPLATE.isPlateChar(@plate[plate_pos])
                      then info_pos += 1; @info[info_pos - 1]
                      else @plate[plate_pos]
                      end
      plate_pos += 1
    end
  end

  return mixed_string
end

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

Move the template field to the given location.



382
383
384
385
386
# File 'lib/cdk/components/template.rb', line 382

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

#object_typeObject



558
559
560
# File 'lib/cdk/components/template.rb', line 558

def object_type
  :TEMPLATE
end

#positionObject



554
555
556
# File 'lib/cdk/components/template.rb', line 554

def position
  super(@win)
end

#set(new_value, box) ⇒ Object

Set the value given to the template



488
489
490
491
# File 'lib/cdk/components/template.rb', line 488

def set(new_value, box)
  self.setValue(new_value)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

Set the background attribute of the widget.



453
454
455
456
457
458
459
# File 'lib/cdk/components/template.rb', line 453

def setBKattr(attrib)
  @win.wbkgd(attrib)
  @field_win.wbkgd(attrib)
  unless @label_win.nil?
    @label_win.wbkgd(attrib)
  end
end

#setCB(callback) ⇒ Object

Set the callback function for the widget.



538
539
540
# File 'lib/cdk/components/template.rb', line 538

def setCB(callback)
  @callbackfn = callback
end

#setMin(min) ⇒ Object

Set the minimum number of characters to enter into the widget.



519
520
521
522
523
# File 'lib/cdk/components/template.rb', line 519

def setMin(min)
  if min >= 0
    @min = min
  end
end

#setValue(new_value) ⇒ Object

Set the value given to the template.



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/cdk/components/template.rb', line 494

def setValue(new_value)
  len = 0

  # Just to be sure, let's make sure the new value isn't nil
  if new_value.nil?
    self.clean
    return
  end

  # Determine how many characters we need to copy.
  copychars = [@new_value.size, @field_width, @plate.size].min

  @info = new_value[0...copychars]

  # Use the function which handles the input of the characters.
  (0...new_value.size).each do |x|
    @callbackfn.call(self, new_value[x].ord)
  end
end

#unfocusObject



546
547
548
# File 'lib/cdk/components/template.rb', line 546

def unfocus
  self.draw(@box)
end

#unmix(info) ⇒ Object

Return the field_info from the mixed string.



367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/cdk/components/template.rb', line 367

def unmix(info)
  pos = 0
  unmixed_string = ''

  while pos < @info.size
    if CDK::TEMPLATE.isPlateChar(@plate[pos])
      unmixed_string << info[pos]
    end
    pos += 1
  end

  return unmixed_string
end

#validTemplate(input) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/cdk/components/template.rb', line 313

def validTemplate(input)
  pp = 0
  ip = 0
  while ip < input.size && pp < @plate.size
    newchar = input[ip]
    while pp < @plate.size && !CDK::TEMPLATE.isPlateChar(@plate[pp])
      pp += 1
    end
    if pp == @plate.size
      return false
    end

    # Check if the input matches the plate
    if CDK.digit?(newchar) && 'ACc'.include?(@plate[pp])
      return false
    end
    if !CDK.digit?(newchar) && @plate[pp] == '#'
      return false
    end

    # Do we need to convert the case?
    if @plate[pp] == 'C' || @plate[pp] == 'X'
      newchar = newchar.upcase
    elsif @plate[pp] == 'c' || @plate[pp] == 'x'
      newchar = newchar.downcase
    end
    input[ip] = newchar
    ip += 1
    pp += 1
  end
  return true
end