Class: CDK::MENTRY

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

Instance Attribute Summary collapse

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

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, field_attr, filler, disp_type, f_width, f_rows, logical_rows, min, box, shadow) ⇒ MENTRY

Returns a new instance of MENTRY.



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

def initialize(cdkscreen, xplace, yplace, title, label, field_attr,
    filler, disp_type, f_width, f_rows, logical_rows, min, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  field_width = f_width
  field_rows = f_rows

  self.setBox(box)

  # If the field_width is a negative value, the field_width will be
  # COLS-field_width, otherwise the field_width will be the given width.
  field_width = CDK.setWidgetDimension(parent_width, field_width, 0)
 
  # If the field_rows is a negative value, the field_rows will be
  # ROWS-field_rows, otherwise the field_rows will be the given rows.
  field_rows = CDK.setWidgetDimension(parent_width, field_rows, 0)
  box_height = field_rows + 2

  # Set some basic values of the mentry field
  @label = ''
  @label_len = 0
  @label_win = nil
  
  # We need to translate the string label to a chtype array
  if label.size > 0
    label_len = []
    @label = char2Chtype(label, label_len, [])
    @label_len = label_len[0]
  end
  box_width = @label_len + field_width + 2
  
  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 parent window.
  box_width = [box_width, parent_width].min
  box_height = [box_height, parent_height].min
  field_width = [box_width - @label_len - 2, field_width].min
  field_rows = [box_height - @title_lines - 2, field_rows].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 label window.
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)

  # Is the window nil?
  if @win.nil?
    self.destroy
    return nil
  end

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

  # make the field window.
  @field_win = @win.subwin(field_rows, field_width,
      ypos + @title_lines + 1, xpos + @label_len + horizontal_adjust + 1)

  # Turn on the keypad.
  @field_win.keypad(true)
  @win.keypad(true)

  # Set up the rest of the structure.
  @parent = cdkscreen.window
  @total_width = (field_width * logical_rows) + 1

  # Create the info string
  @info = ''

  # Set up the rest of the widget information.
  @screen = cdkscreen
  @shadow_win = nil
  @field_attr = field_attr
  @field_width = field_width
  @rows = field_rows
  @box_height = box_height
  @box_width = box_width
  @filler = filler.ord
  @hidden = filler.ord
  @input_window = @win
  @accepts_focus = true
  @current_row = 0
  @current_col = 0
  @top_row = 0
  @shadow = shadow
  @disp_type = disp_type
  @min = min
  @logical_rows = logical_rows

  # This is a generic character parser for the mentry field. It is used as
  # a callback function, so any personal modifications can be made by
  # creating a new function and calling that one the mentry activation.
  mentry_callback = lambda do |mentry, character|
    cursor_pos = mentry.getCursorPos
    newchar = Display.filterByDisplayType(mentry.disp_type, character)

    if newchar == Ncurses::ERR
      CDK.Beep
    else
      mentry.info = mentry.info[0...cursor_pos] + newchar.chr +
          mentry.info[cursor_pos..-1]
      mentry.current_col += 1

      mentry.drawField

      # Have we gone out of bounds
      if mentry.current_col >= mentry.field_width
        # Update the row and col values.
        mentry.current_col = 0
        mentry.current_row += 1

        # If we have gone outside of the visual boundaries, we
        # need to scroll the window.
        if mentry.current_row == mentry.rows
          # We have to redraw the screen
          mentry.current_row -= 1
          mentry.top_row += 1
          mentry.drawField
        end
        mentry.field_win.wmove(mentry.current_row, mentry.current_col)
        wrefresh(mentry.field_win)
      end
    end
  end
  @callbackfn = mentry_callback

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

  # Register
  cdkscreen.register(:MENTRY, self)
end

Instance Attribute Details

#current_colObject

Returns the value of attribute current_col.



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

def current_col
  @current_col
end

#current_rowObject

Returns the value of attribute current_row.



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

def current_row
  @current_row
end

#disp_typeObject (readonly)

Returns the value of attribute disp_type.



6
7
8
# File 'lib/cdk/components/mentry.rb', line 6

def disp_type
  @disp_type
end

#field_widthObject (readonly)

Returns the value of attribute field_width.



6
7
8
# File 'lib/cdk/components/mentry.rb', line 6

def field_width
  @field_width
end

#field_winObject (readonly)

Returns the value of attribute field_win.



6
7
8
# File 'lib/cdk/components/mentry.rb', line 6

def field_win
  @field_win
end

#infoObject

Returns the value of attribute info.



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

def info
  @info
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/cdk/components/mentry.rb', line 6

def rows
  @rows
end

#top_rowObject

Returns the value of attribute top_row.



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

def top_row
  @top_row
end

Instance Method Details

#activate(actions) ⇒ Object

This actually activates the mentry widget…



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

def activate(actions)
  input = 0

  # Draw the mentry widget.
  self.draw(@box)

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

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

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

#cleanObject

This erases the information in the multiple line entry widget



585
586
587
588
589
590
# File 'lib/cdk/components/mentry.rb', line 585

def clean
  @info = ''
  @current_row = 0
  @current_col = 0
  @top_row = 0
end

#destroyObject

This function destroys a multiple line entry field widget.



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/cdk/components/mentry.rb', line 507

def destroy
  self.cleanTitle
  
  # Clean up the windows.
  CDK.deleteCursesWindow(@field_win)
  CDK.deleteCursesWindow(@label_win)
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)
  
  # Clean the key bindings.
  self.cleanBindings(:MENTRY)
  
  # Unregister this object.
  CDK::SCREEN.unregister(:MENTRY, self)
end

#draw(box) ⇒ Object

This function draws the multiple line entry field.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/cdk/components/mentry.rb', line 464

def draw(box)
  # Box the widget if asked.
  if box
    Draw.drawObjBox(@win, self)
    wrefresh
  end
  
  # Do we need to draw in the shadow?
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end
  
  # Draw in the label to the widget.
  unless @label_win.nil?
    Draw.writeChtype(@label_win, 0, 0, @label, CDK::HORIZONTAL,
        0, @label_len)
    wrefresh(@label_win)
  end
  
  # Draw the mentry field
  self.drawField
end

#drawFieldObject

This function redraws the multiple line entry field.



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/components/mentry.rb', line 434

def drawField
  currchar = @field_width * @top_row
  
  self.drawTitle(@win)
  wrefresh
  
  lastpos = @info.size
  
  # Start redrawing the fields.
  (0...@rows).each do |x|
    (0...@field_width).each do |y|
      if currchar < lastpos
        if Display.isHiddenDisplayType(@disp_type)
          @field_win.mvwaddch(x, y, @filler)
        else
          @field_win.mvwaddch(x, y, @info[currchar].ord | @field_attr)
          currchar += 1
        end
      else
        @field_win.mvwaddch(x, y, @filler)
      end
    end
  end
  
  # Refresh the screen.
  @field_win.wmove(@current_row, @current_col)
  wrefresh(@field_win)
end

#eraseObject

This function erases the multiple line entry field from the screen.



497
498
499
500
501
502
503
504
# File 'lib/cdk/components/mentry.rb', line 497

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

#focusObject



597
598
599
600
# File 'lib/cdk/components/mentry.rb', line 597

def focus
  @field_win.wmove(0, @current_col)
  wrefresh(@field_win)
end

#getCursorPosObject



224
225
226
# File 'lib/cdk/components/mentry.rb', line 224

def getCursorPos
  return (@current_row + @top_row) * @field_width + @current_col
end

#getFillerCharObject



562
563
564
# File 'lib/cdk/components/mentry.rb', line 562

def getFillerChar
  return @filler
end

#getHiddenCharObject



571
572
573
# File 'lib/cdk/components/mentry.rb', line 571

def getHiddenChar
  return @hidden
end

#getMinObject



580
581
582
# File 'lib/cdk/components/mentry.rb', line 580

def getMin
  return @min
end

#getValueObject



553
554
555
# File 'lib/cdk/components/mentry.rb', line 553

def getValue
  return @info
end

#inject(input) ⇒ Object

This injects a character into the widget.



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
312
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
# File 'lib/cdk/components/mentry.rb', line 229

def inject(input)
  cursor_pos = self.getCursorPos
  pp_return = 1
  ret = -1
  @complete = false

  # Set the exit type.
  self.setExitType(0)

  # Refresh the field.
  self.drawField

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

  # Should we continue?
  if pp_return != 0
    # Check for a key binding...
    if self.checkBind(:MENTRY, input)
      @complete = true
    else
      moved = false
      redraw = false

      case input
      when Ncurses::KEY_HOME
        moved = self.setCurPos(0, 0)
        redraw = self.setTopRow(0)
      when Ncurses::KEY_END
        field_characters = @rows * @field_width
        if @info.size < field_characters
          redraw = self.setTopRow(0)
          moved = self.setCurPos(
              @info.size / @field_width, @info.size % @field_width)
        else
          redraw = self.setTopRow(@info.size / @field_width, @rows + 1)
          moved = self.setCurPos(@rows - 1, @info.size % @field_width)
        end
      when Ncurses::KEY_LEFT
        mtmp = [moved]
        rtmp = [redraw]
        self.KEY_LEFT(mtmp, rtmp)
        moved = mtmp[0]
        redraw = rtmp[0]
      when Ncurses::KEY_RIGHT
        if @current_col < @field_width - 1
          if self.getCursorPos + 1 <= @info.size
            moved = self.setCurPos(@current_row, @current_col + 1)
          end
        elsif @current_row == @rows - 1
          if @top_row + @current_row + 1 < @logical_rows
            moved = self.setCurPos(@current_row, 0)
            redraw = self.setTopRow(@top_row + 1)
          end
        else
          moved = self.setCurPos(@current_row + 1, 0)
        end
        if !moved && !redraw
          CDK.Beep
        end
      when Ncurses::KEY_DOWN
        if @current_row != @rows - 1
          if self.getCursorPos + @field_width + 1 <= @info.size
            moved = self.setCurPos(@current_row + 1, @current_col)
          end
        elsif @top_row < @logical_rows - @rows
          if (@top_row + @current_row + 1) * @field_width <= @info.size
            redraw = self.setTopRow(@top_row + 1)
          end
        end
        if !moved && !redraw
          CDK.Beep
        end
      when Ncurses::KEY_UP
        if @current_row != 0
          moved = self.setCurPos(@current_row - 1, @current_col)
        elsif @top_row != 0
          redraw = self.setTopRow(@top_row - 1)
        end
        if !moved && !redraw
          CDK.Beep
        end
      when Ncurses::KEY_BACKSPACE, Ncurses::KEY_DC
        if @disp_type == :VIEWONLY
          CDK.Beep
        elsif @info.length == 0
          CDK.Beep
        elsif input == Ncurses::KEY_DC
          cursor_pos = self.getCursorPos
          if cursor_pos < @info.size
            @info = @info[0...cursor_pos] + @info[cursor_pos + 1..-1]
            self.drawField
          else
            CDK.Beep
          end
        else
          mtmp = [moved]
          rtmp = [redraw]
          hKL = self.KEY_LEFT(mtmp, rtmp)
          moved = mtmp[0]
          rtmp = [redraw]
          if hKL
            cursor_pos = self.getCursorPos
            if cursor_pos < @info.size
              @info = @info[0...cursor_pos] + @info[cursor_pos + 1..-1]
              self.drawField
            else
              CDK.Beep
            end
          end
        end
      when CDK::TRANSPOSE
        if cursor_pos >= @info.size - 1
          CDK.Beep
        else
          holder = @info[cursor_pos]
          @info[cursor_pos] = @info[cursor_pos + 1]
          @info[cursor_pos + 1] = holder
          self.drawField
        end
      when CDK::ERASE
        if @info.size != 0
          self.clean
          self.drawField
        end
      when CDK::CUT
        if @info.size == 0
          CDK.Beep
        else
          @@g_paste_buffer = @info.clone
          self.clean
          self.drawField
        end
      when CDK::COPY
        if @info.size == 0
          CDK.Beep
        else
          @@g_paste_buffer = @info.clone
        end
      when CDK::PASTE
        if @@g_paste_buffer.size == 0
          CDK.Beep
        else
          self.setValue(@@g_paste_buffer)
          self.draw(@box)
        end
      when CDK::KEY_TAB, CDK::KEY_RETURN, Ncurses::KEY_ENTER
        if @info.size < @min + 1
          CDK.Beep
        else
          self.setExitType(input)
          ret = @info
          @complete = true
        end
      when Ncurses::ERR
        self.setExitType(input)
        @complete = true
      when CDK::KEY_ESC
        self.setExitType(input)
        @complete = true
      when CDK::REFRESH
        @screen.erase
        @screen.refresh
      else
        if @disp_type == :VIEWONLY || @info.size >= @total_width
          CDK.Beep
        else
          @callbackfn.call(self, input)
        end
      end

      if redraw
        self.drawField
      elsif moved
        @field_win.wmove(@current_row, @current_col)
        wrefresh(@field_win)
      end
    end

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

  if !@complete
    self.setExitType(0)
  end

  @result_data = ret
  return ret
end

#KEY_LEFT(moved, redraw) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/cdk/components/mentry.rb', line 204

def KEY_LEFT(moved, redraw)
  result = true
  if @current_col != 0
    moved[0] = self.setCurPos(@current_row, @current_col - 1)
  elsif @current_row == 0
    if @top_row != 0
      moved[0] = self.setCurPos(@current_row, @field_width - 1)
      redraw[0] = self.setTopRow(@top_row - 1)
    end
  else
    moved[0] = self.setCurPos(@current_row - 1, @field_width - 1)
  end

  if !moved[0] && !redraw[0]
    CDK.Beep
    result = false
  end
  return result
end

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

This moves the mentry field to the given location.



427
428
429
430
431
# File 'lib/cdk/components/mentry.rb', line 427

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

#object_typeObject



610
611
612
# File 'lib/cdk/components/mentry.rb', line 610

def object_type
  :MENTRY
end

#positionObject



606
607
608
# File 'lib/cdk/components/mentry.rb', line 606

def position
  super(@win)
end

#set(value, min, box) ⇒ Object

This sets multiple attributes of the widget.



524
525
526
527
528
# File 'lib/cdk/components/mentry.rb', line 524

def set(value, min, box)
  self.setValue(value)
  self.setMin(min)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



488
489
490
491
492
493
494
# File 'lib/cdk/components/mentry.rb', line 488

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

#setCB(callback) ⇒ Object

This sets the callback function.



593
594
595
# File 'lib/cdk/components/mentry.rb', line 593

def setCB(callback)
  @callbackfn = callback
end

#setCurPos(row, col) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/cdk/components/mentry.rb', line 195

def setCurPos(row, col)
  if @current_row != row || @current_col != col
    @current_row = row
    @current_col = col
    return true
  end
  return false
end

#setFillerChar(filler) ⇒ Object

This sets the filler character to use when drawing the widget.



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

def setFillerChar(filler)
  @filler = filler.ord
end

#setHiddenChar(character) ⇒ Object

This sets the character to use when a hidden character type is used



567
568
569
# File 'lib/cdk/components/mentry.rb', line 567

def setHiddenChar(character)
  @hidden = character
end

#setMin(min) ⇒ Object

This sets a minimum length of the widget.



576
577
578
# File 'lib/cdk/components/mentry.rb', line 576

def setMin(min)
  @min = min
end

#setTopRow(row) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/cdk/components/mentry.rb', line 187

def setTopRow(row)
  if @top_row != row
    @top_row = row
    return true
  end
  return false
end

#setValue(new_value) ⇒ Object

This removes the old information in the entry field and keeps the new information given.



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/cdk/components/mentry.rb', line 532

def setValue(new_value)
  field_characters = @rows * @field_width
  
  @info = new_value
  
  # Set the cursor/row info
  if new_value.size < field_characters
    @top_row = 0
    @current_row = new_value.size / @field_width
    @current_col = new_value.size % @field_width
  else
    row_used = new_value.size / @field_width
    @top_row = row_used - @rows + 1
    @current_row = @rows - 1
    @current_col = new_value.size % @field_width
  end
  
  # Redraw the widget.
  self.drawField
end

#unfocusObject



602
603
604
# File 'lib/cdk/components/mentry.rb', line 602

def unfocus
  wrefresh(@field_win)
end