Class: CDK::MATRIX

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

Constant Summary collapse

MAX_MATRIX_ROWS =
1000
MAX_MATRIX_COLS =
1000
@@g_paste_buffer =
''

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, rows, cols, vrows, vcols, title, rowtitles, coltitles, colwidths, colvalues, rspace, cspace, filler, dominant, box, box_cell, shadow) ⇒ MATRIX

Returns a new instance of MATRIX.



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
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
217
218
219
220
221
222
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
# File 'lib/cdk/components/matrix.rb', line 13

def initialize(cdkscreen, xplace, yplace, rows, cols, vrows, vcols,
    title, rowtitles, coltitles, colwidths, colvalues, rspace, cspace,
    filler, dominant, box, box_cell, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_height = 0
  box_width = 0
  max_row_title_width = 0
  row_space = [0, rspace].max
  col_space = [0, cspace].max
  begx = 0
  begy = 0
  cell_width = 0
  have_rowtitles = false
  have_coltitles = false
  bindings = {
      CDK::FORCHAR  => Ncurses::KEY_NPAGE,
      CDK::BACKCHAR => Ncurses::KEY_PPAGE,
  }

  self.setBox(box)
  borderw = if @box then 1 else 0 end

  # Make sure that the number of rows/cols/vrows/vcols is not zero.
  if rows <= 0 || cols <= 0 || vrows <= 0 || vcols <= 0
    self.destroy
    return nil
  end

  @cell = Array.new(rows + 1) { |i| Array.new(cols + 1)}
  @info = Array.new(rows + 1) { |i| Array.new(cols + 1) { |i| '' }}

  # Make sure the number of virtual cells is not larger than the
  # physical size.
  vrows = [vrows, rows].min
  vcols = [vcols, cols].min

  @rows = rows
  @cols = cols
  @colwidths = [0] * (cols + 1)
  @colvalues = [0] * (cols + 1)
  @coltitle = Array.new(cols + 1) {|i| []}
  @coltitle_len = [0] * (cols + 1)
  @coltitle_pos = [0] * (cols + 1)
  @rowtitle = Array.new(rows + 1) {|i| []}
  @rowtitle_len = [0] * (rows + 1)
  @rowtitle_pos = [0] * (rows + 1)

  # Count the number of lines in the title
  temp = title.split("\n")
  @title_lines = temp.size

  # Determine the height of the box.
  if vrows == 1
    box_height = 6 + @title_lines
  else
    if row_space == 0
      box_height = 6 + @title_lines + (vrows - 1) * 2
    else
      box_height = 3 + @title_lines + vrows * 3 +
          (vrows - 1) * (row_space - 1)
    end
  end

  # Determine the maximum row title width.
  (1..rows).each do |x|
    if !(rowtitles.nil?) && x < rowtitles.size && rowtitles[x].size > 0
      have_rowtitles = true
    end
    rowtitle_len = []
    rowtitle_pos = []
    @rowtitle[x] = char2Chtype((rowtitles[x] || ''),
        rowtitle_len, rowtitle_pos)
    @rowtitle_len[x] = rowtitle_len[0]
    @rowtitle_pos[x] = rowtitle_pos[0]
    max_row_title_width = [max_row_title_width, @rowtitle_len[x]].max
  end

  if have_rowtitles
    @maxrt = max_row_title_width + 2

    # We need to rejustify the row title cell info.
    (1..rows).each do |x|
      @rowtitle_pos[x] = justify_string(@maxrt,
          @rowtitle_len[x], @rowtitle_pos[x])
    end
  else
    @maxrt = 0
  end

  # Determine the width of the matrix.
  max_width = 2 + @maxrt
  (1..vcols).each do |x|
    max_width += colwidths[x] + 2 + col_space
  end
  max_width -= (col_space - 1)
  box_width = [max_width, box_width].max
  box_width = self.setTitle(title, box_width)

  # Make sure the dimensions of the window didn't extend
  # beyond the dimensions of the parent window
  box_width = [box_width, parent_width].min
  box_height = [box_height, parent_height].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 pop-up window.
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)

  if @win.nil?
    self.destroy
    return nil
  end

  # Make the subwindows in the pop-up.
  begx = xpos
  begy = ypos + borderw + @title_lines

  # Make the 'empty' 0x0 cell.
  @cell[0][0] = @win.subwin(3, @maxrt, begy, begx)

  begx += @maxrt + 1

  # Copy the titles into the structrue.
  (1..cols).each do |x|
    if !(coltitles.nil?) && x < coltitles.size && coltitles[x].size > 0
      have_coltitles = true
    end
    coltitle_len = []
    coltitle_pos = []
    @coltitle[x] = char2Chtype(coltitles[x] || '',
        coltitle_len, coltitle_pos)
    @coltitle_len[x] = coltitle_len[0]
    @coltitle_pos[x] = @border_size + justify_string(
        colwidths[x], @coltitle_len[x], coltitle_pos[0])
    @colwidths[x] = colwidths[x]
  end

  if have_coltitles
    # Make the column titles.
    (1..vcols).each do |x|
      cell_width = colwidths[x] + 3
      @cell[0][x] = @win.subwin(borderw, cell_width, begy, begx)
      if @cell[0][x].nil?
        self.destroy
        return nil
      end
      begx += cell_width + col_space - 1
    end
    begy += 1
  end

  # Make the main cell body
  (1..vrows).each do |x|
    if have_rowtitles
      # Make the row titles
      @cell[x][0] = @win.subwin(3, @maxrt, begy, xpos + borderw)

      if @cell[x][0].nil?
        self.destroy
        return nil
      end
    end

    # Set the start of the x position.
    begx = xpos + @maxrt + borderw

    # Make the cells
    (1..vcols).each do |y|
      cell_width = colwidths[y] + 3
      @cell[x][y] = @win.subwin(3, cell_width, begy, begx)

      if @cell[x][y].nil?
        self.destroy
        return nil
      end
      begx += cell_width + col_space - 1
      @cell[x][y].keypad(true)
    end
    begy += row_space + 2
  end
  @win.keypad(true)

  # Keep the rest of the info.
  @screen = cdkscreen
  @accepts_focus = true
  @input_window = @win
  @parent = cdkscreen.window
  @vrows = vrows
  @vcols = vcols
  @box_width = box_width
  @box_height = box_height
  @row_space = row_space
  @col_space = col_space
  @filler = filler.ord
  @dominant = dominant
  @row = 1
  @col = 1
  @crow = 1
  @ccol = 1
  @trow = 1
  @lcol = 1
  @oldcrow = 1
  @oldccol = 1
  @oldvrow = 1
  @oldvcol = 1
  @box_cell = box_cell
  @shadow = shadow
  @highlight = Ncurses::A_REVERSE
  @shadow_win = nil
  @callbackfn = lambda do |matrix, input|
    disptype = matrix.colvalues[matrix.col]
    plainchar = Display.filterByDisplayType(disptype, input)
    charcount = matrix.info[matrix.row][matrix.col].size

    if plainchar == Ncurses::ERR
      CDK.Beep
    elsif charcount == matrix.colwidths[matrix.col]
      CDK.Beep
    else
      # Update the screen.
      matrix.CurMatrixCell.wmove(1,
          matrix.info[matrix.row][matrix.col].size + 1)
      matrix.CurMatrixCell.waddch(
          if Display.isHiddenDisplayType(disptype)
          then matrix.filler
          else plainchar
          end)
      wrefresh(matrix.CurMatrixCell)

      # Update the info string
      matrix.info[matrix.row][matrix.col] =
          matrix.info[matrix.row][matrix.col][0...charcount] +
          plainchar.chr
    end
  end

  # Make room for the cell information.
  (1..rows).each do |x|
    (1..cols).each do |y|
      @colvalues[y] = colvalues[y]
      @colwidths[y] = colwidths[y]
    end
  end

  @colvalues = colvalues.clone
  @colwidths = colwidths.clone

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

  # Set up the key bindings.
  bindings.each do |from, to|
    self.bind(:MATRIX, from, :getc, to)
  end

  # Register this baby.
  cdkscreen.register(:MATRIX, self)
end

Instance Attribute Details

#ccolObject (readonly)

Returns the value of attribute ccol.



7
8
9
# File 'lib/cdk/components/matrix.rb', line 7

def ccol
  @ccol
end

#colObject (readonly)

Returns the value of attribute col.



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

def col
  @col
end

#colvaluesObject (readonly)

Returns the value of attribute colvalues.



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

def colvalues
  @colvalues
end

#colwidthsObject (readonly)

Returns the value of attribute colwidths.



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

def colwidths
  @colwidths
end

#crowObject (readonly)

Returns the value of attribute crow.



7
8
9
# File 'lib/cdk/components/matrix.rb', line 7

def crow
  @crow
end

#fillerObject (readonly)

Returns the value of attribute filler.



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

def filler
  @filler
end

#infoObject

Returns the value of attribute info.



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

def info
  @info
end

#rowObject (readonly)

Returns the value of attribute row.



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

def row
  @row
end

Instance Method Details

#activate(actions) ⇒ Object

This activates the matrix.



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/matrix.rb', line 283

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

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

      # Inject the 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 exit.
  self.setExitType(0)
  return -1
end

#cleanObject

This cleans out the information cells in the matrix widget.



939
940
941
942
943
944
945
# File 'lib/cdk/components/matrix.rb', line 939

def clean
  (1..@rows).each do |x|
    (1..@cols).each do |y|
      self.cleanCell(x, y)
    end
  end
end

#cleanCell(row, col) ⇒ Object

This cleans one cell in the matrix widget.



948
949
950
951
952
# File 'lib/cdk/components/matrix.rb', line 948

def cleanCell(row, col)
  if row > 0 && row <= @rows && col > col <= @cols
    @info[row][col] = ''
  end
end

#CurMatrixCellObject



1124
1125
1126
# File 'lib/cdk/components/matrix.rb', line 1124

def CurMatrixCell
  return @cell[@crow][@ccol]
end

#CurMatrixInfoObject



1128
1129
1130
# File 'lib/cdk/components/matrix.rb', line 1128

def CurMatrixInfo
  return @info[@trow + @crow - 1][@lcol + @ccol - 1]
end

#destroyObject

This function destroys the matrix widget.



867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/cdk/components/matrix.rb', line 867

def destroy
  self.cleanTitle

  # Clear the matrix windows.
  CDK.deleteCursesWindow(@cell[0][0])
  (1..@vrows).each do |x|
    CDK.deleteCursesWindow(@cell[x][0])
  end
  (1..@vcols).each do |x|
    CDK.deleteCursesWindow(@cell[0][x])
  end
  (1..@vrows).each do |x|
    (1..@vcols).each do |y|
      CDK.deleteCursesWindow(@cell[x][y])
    end
  end

  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

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

  # Unregister this object.
  CDK::SCREEN.unregister(:MATRIX, self)
end

#draw(box) ⇒ Object

This function draws the matrix widget.



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/cdk/components/matrix.rb', line 845

def draw(box)
  # Did we ask for a shadow?
  unless @shadow_win.nil?
    Draw.drawShadow(@shadow_win)
  end

  # Should we box the matrix?
  if box
    Draw.drawObjBox(@win, self)
  end

  self.drawTitle(@win)

  wrefresh

  self.drawEachColTitle
  self.drawEachRowTitle
  self.drawEachCell
  self.focusCurrent
end

#drawCell(row, col, vrow, vcol, attr, box) ⇒ Object

This draws a cell within a matrix.



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/cdk/components/matrix.rb', line 663

def drawCell(row, col, vrow, vcol, attr, box)
  disptype = @colvalues[@col]
  highlight = @filler & Ncurses::A_ATTRIBUTES
  rows = @vrows
  cols = @vcols
  infolen = @info[vrow][vcol].size

  # Given the dominance of the colors/attributes, we need to set the
  # current cell attribute.
  if @dominant == CDK::ROW
    highlight = (@rowtitle[row][0] || 0) & Ncurses::A_ATTRIBUTES
  elsif @dominant == CDK::COL
    highlight = (@coltitle[col][0] || 0) & Ncurses::A_ATTRIBUTES
  end

  # Draw in the cell info.
  (1..@colwidths[col]).each do |x|
    ch = if x <= infolen && !Display.isHiddenDisplayType(disptype)
         then charOf(@info[vrow][vcol][x-1]).ord | highlight
         else @filler
         end
    @cell[row][col].mvwaddch(1, x, ch.ord | highlight)
  end

  @cell[row][col].wmove(1, infolen + 1)
  wrefresh(@cell[row][col])

  # Only draw the box iff the user asked for a box.
  if !box
    return
  end

  # If the value of the column spacing is greater than 0 then these
  # are independent boxes
  if @col_space != 0 && @row_space != 0
    Draw.attrbox(@cell[row][col], Ncurses::ACS_ULCORNER,
        Ncurses::ACS_URCORNER, Ncurses::ACS_LLCORNER,
        Ncurses::ACS_LRCORNER, Ncurses::ACS_HLINE,
        Ncurses::ACS_VLINE, attr)
    return
  end
  if @col_space != 0 && @row_space == 0
    if row == 1
      Draw.attrbox(@cell[row][col], Ncurses::ACS_ULCORNER,
          Ncurses::ACS_URCORNER, Ncurses::ACS_LTEE,
          Ncurses::ACS_RTEE, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
      return
    elsif row > 1 && row < rows
      Draw.attrbox(@cell[row][col], Ncurses::ACS_LTEE, Ncurses::ACS_RTEE,
          Ncurses::ACS_LTEE, Ncurses::ACS_RTEE, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
      return
    elsif row == rows
      Draw.attrbox(@cell[row][col], Ncurses::ACS_LTEE, Ncurses::ACS_RTEE,
          Ncurses::ACS_LLCORNER, Ncurses::ACS_LRCORNER, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
      return
    end
  end
  if @col_space == 0 && @row_space != 0
    if col == 1
      Draw.attrbox(@cell[row][col], Ncurses::ACS_ULCORNER,
          Ncurses::ACS_TTEE, Ncurses::ACS_LLCORNER, Ncurses::ACS_BTEE,
          Ncurses::ACS_HLINE, Ncurses::ACS_VLINE, attr)
      return
    elsif col > 1 && col < cols
      Draw.attrbox(@cell[row][col], Ncurses::ACS_TTEE, Ncurses::ACS_TTEE,
          Ncurses::ACS_BTEE, Ncurses::ACS_BTEE, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
      return
    elsif col == cols
      Draw.attrbox(@cell[row][col], Ncurses::ACS_TTEE,
          Ncurses::ACS_URCORNER,Ncurses::ACS_BTEE, Ncurses::ACS_LRCORNER,
          Ncurses::ACS_HLINE, Ncurses::ACS_VLINE, attr)
      return
    end
  end

  # Start drawing the matrix.
  if row == 1
    if col == 1
      # Draw the top left corner
      Draw.attrbox(@cell[row][col], Ncurses::ACS_ULCORNER,
          Ncurses::ACS_TTEE, Ncurses::ACS_LTEE, Ncurses::ACS_PLUS,
          Ncurses::ACS_HLINE, Ncurses::ACS_VLINE, attr)
    elsif col > 1 && col < cols
      # Draw the top middle box
      Draw.attrbox(@cell[row][col], Ncurses::ACS_TTEE, Ncurses::ACS_TTEE,
          Ncurses::ACS_PLUS, Ncurses::ACS_PLUS, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    elsif col == cols
      # Draw the top right corner
      Draw.attrbox(@cell[row][col], Ncurses::ACS_TTEE,
          Ncurses::ACS_URCORNER, Ncurses::ACS_PLUS, Ncurses::ACS_RTEE,
          Ncurses::ACS_HLINE, Ncurses::ACS_VLINE, attr)
    end
  elsif row > 1 && row < rows
    if col == 1
      # Draw the middle left box
      Draw.attrbox(@cell[row][col], Ncurses::ACS_LTEE, Ncurses::ACS_PLUS,
          Ncurses::ACS_LTEE, Ncurses::ACS_PLUS, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    elsif col > 1 && col < cols
      # Draw the middle box
      Draw.attrbox(@cell[row][col], Ncurses::ACS_PLUS, Ncurses::ACS_PLUS,
          Ncurses::ACS_PLUS, Ncurses::ACS_PLUS, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    elsif col == cols
      # Draw the middle right box
      Draw.attrbox(@cell[row][col], Ncurses::ACS_PLUS, Ncurses::ACS_RTEE,
          Ncurses::ACS_PLUS, Ncurses::ACS_RTEE, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    end
  elsif row == rows
    if col == 1
      # Draw the bottom left corner
      Draw.attrbox(@cell[row][col], Ncurses::ACS_LTEE, Ncurses::ACS_PLUS,
          Ncurses::ACS_LLCORNER, Ncurses::ACS_BTEE, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    elsif col > 1 && col < cols
      # Draw the bottom middle box
      Draw.attrbox(@cell[row][col], Ncurses::ACS_PLUS, Ncurses::ACS_PLUS,
          Ncurses::ACS_BTEE, Ncurses::ACS_BTEE, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    elsif col == cols
      # Draw the bottom right corner
      Draw.attrbox(@cell[row][col], Ncurses::ACS_PLUS, Ncurses::ACS_RTEE,
          Ncurses::ACS_BTEE, Ncurses::ACS_LRCORNER, Ncurses::ACS_HLINE,
          Ncurses::ACS_VLINE, attr)
    end
  end

  self.focusCurrent
end

#drawCurCellObject



835
836
837
# File 'lib/cdk/components/matrix.rb', line 835

def drawCurCell
  self.drawCell(@crow, @ccol, @row, @col, Ncurses::A_NORMAL, @box_cell)
end

#drawEachCellObject



825
826
827
828
829
830
831
832
833
# File 'lib/cdk/components/matrix.rb', line 825

def drawEachCell
  # Fill in the cells.
  (1..@vrows).each do |x|
    (1..@vcols).each do |y|
      self.drawCell(x, y, @trow + x - 1, @lcol + y - 1,
          Ncurses::A_NORMAL, @box_cell)
    end
  end
end

#drawEachColTitleObject



799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/cdk/components/matrix.rb', line 799

def drawEachColTitle
  (1..@vcols).each do |x|
    unless @cell[0][x].nil?
      @cell[0][x].werase
      Draw.writeChtype(@cell[0][x],
          @coltitle_pos[@lcol + x - 1], 0,
          @coltitle[@lcol + x - 1], CDK::HORIZONTAL, 0,
          @coltitle_len[@lcol + x - 1])
      wrefresh(@cell[0][x])
    end
  end
end

#drawEachRowTitleObject



812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/cdk/components/matrix.rb', line 812

def drawEachRowTitle
  (1..@vrows).each do |x|
    unless @cell[x][0].nil?
      @cell[x][0].werase
      Draw.writeChtype(@cell[x][0],
          @rowtitle_pos[@trow + x - 1], 1,
          @rowtitle[@trow + x - 1], CDK::HORIZONTAL, 0,
          @rowtitle_len[@trow + x - 1])
      wrefresh(@cell[x][0])
    end
  end
end

#drawOldCellObject



839
840
841
842
# File 'lib/cdk/components/matrix.rb', line 839

def drawOldCell
  self.drawCell(@oldcrow, @oldccol, @oldvrow, @oldvcol,
      Ncurses::A_NORMAL, @box_cell)
end

#eraseObject

This function erases the matrix widget from the screen.



895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'lib/cdk/components/matrix.rb', line 895

def erase
  if self.validCDKObject
    # Clear the matrix cells.
    CDK.eraseCursesWindow(@cell[0][0])
    (1..@vrows).each do |x|
      CDK.eraseCursesWindow(@cell[x][0])
    end
    (1..@vcols).each do |x|
      CDK.eraseCursesWindow(@cell[0][x])
    end
    (1..@vrows).each do |x|
      (1..@vcols).each do |y|
        CDK.eraseCursesWindow(@cell[x][y])
      end
    end
    CDK.eraseCursesWindow(@shadow_win)
    CDK.eraseCursesWindow(@win)
  end
end

#focusObject



1160
1161
1162
# File 'lib/cdk/components/matrix.rb', line 1160

def focus
  self.draw(@box)
end

#focusCurrentObject



1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/cdk/components/matrix.rb', line 1132

def focusCurrent
  Draw.attrbox(self.CurMatrixCell, Ncurses::ACS_ULCORNER,
      Ncurses::ACS_URCORNER, Ncurses::ACS_LLCORNER,
      Ncurses::ACS_LRCORNER, Ncurses::ACS_HLINE,
      Ncurses::ACS_VLINE, Ncurses::A_BOLD)
  wrefresh(self.CurMatrixCell)
  self.highlightCell
end

#getCell(row, col) ⇒ Object

This gets the value of a matrix cell.



1116
1117
1118
1119
1120
1121
1122
# File 'lib/cdk/components/matrix.rb', line 1116

def getCell(row, col)
  # Make sure the row/col combination is within the matrix.
  if row > @rows || col > @cols || row <= 0 || col <= 0
    return 0
  end
  return @info[row][col]
end

#getColObject

This returns the current row/col cell



1142
1143
1144
# File 'lib/cdk/components/matrix.rb', line 1142

def getCol
  return @col
end

#getRowObject



1146
1147
1148
# File 'lib/cdk/components/matrix.rb', line 1146

def getRow
  return @row
end

#highlightCellObject

Highlight the new field.



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/cdk/components/matrix.rb', line 622

def highlightCell
  disptype = @colvalues[@col]
  highlight = @highlight
  infolen = @info[@row][@col].size
  
  # Given the dominance of the color/attributes, we need to set the
  # current cell attribute.
  if @dominant == CDK::ROW
    highlight = (@rowtitle[@crow][0] || 0) & Ncurses::A_ATTRIBUTES
  elsif @dominant == CDK::COL
    highlight = (@coltitle[@ccol][0] || 0) & Ncurses::A_ATTRIBUTES
  end

  # If the column is only one char.
  (1..@colwidths[@ccol]).each do |x|
    ch = if x <= infolen && !Display.isHiddenDisplayType(disptype)
         then charOf(@info[@row][@col][x - 1])
         else @filler
         end
    self.CurMatrixCell.mvwaddch(1, x, ch.ord | highlight)
  end
  self.CurMatrixCell.wmove(1, infolen + 1)
  wrefresh(self.CurMatrixCell)
end

#inject(input) ⇒ Object

This injects a single character into the matrix widget.



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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/cdk/components/matrix.rb', line 314

def inject(input)
  refresh_cells = false
  moved_cell = false
  charcount = @info[@row][@col].size
  pp_return = 1
  ret = -1
  @complete = false

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

  # Move the cursor to the correct position within the cell.
  if @colwidths[@ccol] == 1
    self.CurMatrixCell.wmove(1, 1)
  else
    self.CurMatrixCell.wmove(1, @info[@row][@col].size + 1)
  end

  # Put the focus on the current cell.
  self.focusCurrent

  # 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(:MATRIX, self,
        @pre_process_data, input)
  end

  # Should we continue?
  if pp_return != 0
    # Check the key bindings.
    if self.checkBind(:MATRIX, input)
      @complete = true
    else
      case input
      when CDK::TRANSPOSE
      when Ncurses::KEY_HOME
      when Ncurses::KEY_END
      when Ncurses::KEY_BACKSPACE, Ncurses::KEY_DC
        if @colvalues[@col] == :VIEWONLY || charcount <= 0
          CDK.Beep
        else
          charcount -= 1
          self.CurMatrixCell.mvwdelch(1, charcount + 1)
          self.CurMatrixCell.mvwinsch(1, charcount + 1, @filler)

          wrefresh(self.CurMatrixCell)
          @info[@row][@col] = @info[@row][@col][0...charcount]
        end
      when Ncurses::KEY_RIGHT, CDK::KEY_TAB
        if @ccol != @vcols
          # We are moving to the right...
          @col += 1
          @ccol += 1
          moved_cell = true
        else
          # We have to shift the columns to the right.
          if @col != @cols
            @lcol += 1
            @col += 1

            # Redraw the column titles.
            if @rows > @vrows
              self.redrawTitles(false, true)
            end
            refresh_cells = true
            moved_cell = true
          else
            # We are at the far right column, we need to shift
            # down one row, if we can.
            if @row == @rows
              CDK.Beep
            else
              # Set up the columns info.
              @col = 1
              @lcol = 1
              @ccol = 1

              # Shift the rows...
              if @crow != @vrows
                @row += 1
                @crow += 1
              else
                @row += 1
                @trow += 1
              end
              self.redrawTitles(true, true)
              refresh_cells = true
              moved_cell = true
            end
          end
        end
      when Ncurses::KEY_LEFT, Ncurses::KEY_BTAB
        if @ccol != 1
          # We are moving to the left...
          @col -= 1
          @ccol -= 1
          moved_cell = true
        else
          # Are we at the far left?
          if @lcol != 1
            @lcol -= 1
            @col -= 1

            # Redraw the column titles.
            if @cols > @vcols
              self.redrawTitles(false, true)
            end
            refresh_cells = true
            moved_cell = true
          else
            # Shift up one line if we can...
            if @row == 1
              CDK.Beep
            else
              # Set up the columns info.
              @col = @cols
              @lcol = @cols - @vcols + 1
              @ccol = @vcols

              # Shift the rows...
              if @crow != 1
                @row -= 1
                @crow -= 1
              else
                @row -= 1
                @trow -= 1
              end
              self.redrawTitles(true, true)
              refresh_cells = true
              moved_cell = true
            end
          end
        end
      when Ncurses::KEY_UP
        if @crow != 1
          @row -= 1
          @crow -= 1
          moved_cell = true
        else
          if @trow != 1
            @trow -= 1
            @row -= 1

            # Redraw the row titles.
            if @rows > @vrows
              self.redrawTitles(true, false)
            end
            refresh_cells = true
            moved_cell = true
          else
            CDK.Beep
          end
        end
      when Ncurses::KEY_DOWN
        if @crow != @vrows
          @row += 1
          @crow += 1
          moved_cell = true
        else
          if @trow + @vrows - 1 != @rows
            @trow += 1
            @row += 1

            # Redraw the titles.
            if @rows > @vrows
              self.redrawTitles(true, false)
            end
            refresh_cells = true
            moved_cell = true
          else
            CDK.Beep
          end
        end
      when Ncurses::KEY_NPAGE
        if @rows > @vrows
          if @trow + (@vrows - 1) * 2 <= @rows
            @trow += @vrows - 1
            @row += @vrows - 1
            self.redrawTitles(true, false)
            refresh_cells = true
            moved_cell = true
          else
            CDK.Beep
          end
        else
          CDK.Beep
        end
      when Ncurses::KEY_PPAGE
        if @rows > @vrows
          if @trow - (@vrows - 1) * 2 >= 1
            @trow -= @vrows - 1
            @row -= @vrows - 1
            self.redrawTitles(true, false)
            refresh_cells = true
            moved_cell = true
          else
            CDK.Beep
          end
        else
          CDK.Beep
        end
      when CDK.CTRL('G')
        self.jumpToCell(-1, -1)
        self.draw(@box)
      when CDK::PASTE
        if @@g_paste_buffer.size == 0 ||
            @@g_paste_buffer.size > @colwidths[@ccol]
          CDK.Beep
        else
          self.CurMatrixInfo = @@g_paste_buffer.clone
          self.drawCurCell
        end
      when CDK::COPY
        @@g_paste_buffer = self.CurMatrixInfo.clone
      when CDK::CUT
        @@g_paste_buffer = self.CurMatrixInfo.clone
        self.cleanCell(@trow + @crow - 1, @lcol + @ccol - 1)
        self.drawCurCell
      when CDK::ERASE
        self.cleanCell(@trow + @crow - 1, @lcol + @ccol - 1)
        self.drawCurCell
      when Ncurses::KEY_ENTER, CDK::KEY_RETURN
        if !@box_cell
          Draw.attrbox(@cell[@oldcrow][@oldccol], ' '.ord, ' '.ord,
              ' '.ord, ' '.ord, ' '.ord, ' '.ord, Ncurses::A_NORMAL)
        else
          self.drawOldCell
        end
        wrefresh(self.CurMatrixCell)
        self.setExitType(input)
        ret = 1
        @complete = true
      when Ncurses::ERR
        self.setExitType(input)
        @complete = true
      when CDK::KEY_ESC
        if !@box_cell
          Draw.attrbox(@cell[@oldcrow][@oldccol], ' '.ord, ' '.ord,
              ' '.ord, ' '.ord, ' '.ord, ' '.ord, Ncurses::A_NORMAL)
        else
          self.drawOldCell
        end
        wrefresh(self.CurMatrixCell)
        self.setExitType(input)
        @complete = true
      when CDK::REFRESH
        @screen.erase
        @screen.refresh
      else
        @callbackfn.call(self, input)
      end
    end

    if !@complete
      # Did we change cells?
      if moved_cell
        # un-highlight the old box
        if !@box_cell
          Draw.attrbox(@cell[@oldcrow][@oldccol], ' '.ord, ' '.ord,
              ' '.ord, ' '.ord, ' '.ord, ' '.ord, Ncurses::A_NORMAL)
        else
          self.drawOldCell
        end
        wrefresh(@cell[@oldcrow][@oldccol])

        self.focusCurrent
      end

      # Redraw each cell
      if refresh_cells
        self.drawEachCell
        self.focusCurrent
      end

      # Move to the correct position in the cell.
      if refresh_cells || moved_cell
        if @colwidths[@ccol] == 1
          self.CurMatrixCell.wmove(1, 1)
        else
          self.CurMatrixCell.wmove(1, self.CurMatrixInfo.size + 1)
        end
        wrefresh(self.CurMatrixCell)
      end

      # Should we call a post-process?
      unless @post_process_func.nil?
        @post_process_func.call(:MATRIX, self, @post_process_data, input)
      end
    end
  end

  if !@complete
    # Set the variables we need.
    @oldcrow = @crow
    @oldccol = @ccol
    @oldvrow = @row
    @oldvcol = @col

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

  @result_data = ret
  return ret
end

#jumpToCell(row, col) ⇒ Object

This allows us to hyper-warp to a cell



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/cdk/components/matrix.rb', line 955

def jumpToCell(row, col)
  new_row = row
  new_col = col

  # Only create the row scale if needed.
  if (row == -1) || (row > @rows)
    # Create the row scale widget.
    scale = CDK::SCALE.new(@screen, CDK::CENTER, CDK::CENTER,
        '<C>Jump to which row.', '</5/B>Row: ', Ncurses::A_NORMAL,
        5, 1, 1, @rows, 1, 1, true, false)

    # Activate the scale and get the row.
    new_row = scale.activate([])
    scale.destroy
  end

  # Only create the column scale if needed.
  if (col == -1) || (col > @cols)
    # Create the column scale widget.
    scale = CDK::SCALE.new(@screen, CDK::CENTER, CDK::CENTER,
        '<C>Jump to which column', '</5/B>Col: ', Ncurses::A_NORMAL,
        5, 1, 1, @cols, 1, 1, true, false)

    # Activate the scale and get the column.
    new_col = scale.activate([])
    scale.destroy
  end

  # Hyper-warp....
  if new_row != @row || @new_col != @col
    return self.moveToCell(new_row, new_col)
  else
    return 1
  end
end

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

This moves the matrix field to the given location.



648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/cdk/components/matrix.rb', line 648

def move(xplace, yplace, relative, refresh_flag)
  windows = [@win]

  (0..@vrows).each do |x|
    (0..@vcols).each do |y|
      windows << @cell[x][y]
    end
  end

  windows << @shadow_win
  self.move_specific(xplace, yplace, relative, refresh_flag,
     windows, [])
end

#moveToCell(newrow, newcol) ⇒ Object

This allows us to move to a given cell.



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/cdk/components/matrix.rb', line 992

def moveToCell(newrow, newcol)
  row_shift = newrow - @row
  col_shift = newcol - @col

  # Make sure we aren't asking to move out of the matrix.
  if newrow > @rows || newcol > @cols || newrow <= 0 || newcol <= 0
    return 0
  end

  # Did we move up/down?
  if row_shift > 0
    # We are moving down
    if @vrows == @cols
      @trow = 1
      @crow = newrow
      @row = newrow
    else
      if row_shift + @vrows < @rows
        # Just shift down by row_shift
        @trow += row_shift
        @crow = 1
        @row += row_shift
      else
        # We need to munge the values
        @trow = @rows - @vrows + 1
        @crow = row_shift + @vrows - @rows + 1
        @row = newrow
      end
    end
  elsif row_shift < 0
    # We are moving up.
    if @vrows == @rows
      @trow = 1
      @row = newrow
      @crow = newrow
    else
      if row_shift + @vrows > 1
        # Just shift up by row_shift...
        @trow += row_shift
        @row += row_shift
        @crow = 1
      else
        # We need to munge the values
        @trow = 1
        @crow = 1
        @row = 1
      end
    end
  end

  # Did we move left/right?
  if col_shift > 0
    # We are moving right.
    if @vcols == @cols
      @lcol = 1
      @ccol = newcol
      @col = newcol
    else
      if col_shift + @vcols < @cols
        @lcol += col_shift
        @ccol = 1
        @col += col_shift
      else
        # We need to munge with the values
        @lcol = @cols - @vcols + 1
        @ccol = col_shift + @vcols - @cols + 1
        @col = newcol
      end
    end
  elsif col_shift < 0
    # We are moving left.
    if @vcols == @cols
      @lcol = 1
      @col = newcol
      @ccol = newcol
    else
      if col_shift + @vcols > 1
        # Just shift left by col_shift
        @lcol += col_shift
        @col += col_shift
        @ccol = 1
      else
        @lcol = 1
        @col = 1
        @ccol = 1
      end
    end
  end

  # Keep the 'old' values around for redrawing sake.
  @oldcrow = @crow
  @oldccol = @ccol
  @oldvrow = @row
  @oldvcol = @col

  return 1
end

#object_typeObject



1172
1173
1174
# File 'lib/cdk/components/matrix.rb', line 1172

def object_type
  :MATRIX
end

#positionObject



1168
1169
1170
# File 'lib/cdk/components/matrix.rb', line 1168

def position
  super(@win)
end

#redrawTitles(row_titles, col_titles) ⇒ Object

This redraws the titles indicated…



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'lib/cdk/components/matrix.rb', line 1091

def redrawTitles(row_titles, col_titles)
  # Redraw the row titles.
  if row_titles
    self.drawEachRowTitle
  end

  # Redraw the column titles.
  if col_titles
    self.drawEachColTitle
  end
end

#setBKattr(attrib) ⇒ Object

This sets the background attribute of the widget.



1151
1152
1153
1154
1155
1156
1157
1158
# File 'lib/cdk/components/matrix.rb', line 1151

def setBKattr(attrib)
  @win.wbkgd(attrib)
  (0..@vrows).each do |x|
    (0..@vcols).each do |y|
      # wbkgd (MATRIX_CELL (widget, x, y), attrib);
    end
  end
end

#setCB(callback) ⇒ Object

Set the callback function



916
917
918
# File 'lib/cdk/components/matrix.rb', line 916

def setCB(callback)
  @callbackfn = callback
end

#setCell(row, col, value) ⇒ Object

This sets the value of a matrix cell.



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'lib/cdk/components/matrix.rb', line 1104

def setCell(row, col, value)
  # Make sure the row/col combination is within the matrix.
  if row > @rows || cols > @cols || row <= 0 || col <= 0
    return -1
  end

  self.cleanCell(row, col)
  @info[row][col] = value[0...[@colwidths[col], value.size].min]
  return 1
end

#setCells(info, rows, maxcols, sub_size) ⇒ Object

This function sets the values of the matrix widget.



921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'lib/cdk/components/matrix.rb', line 921

def setCells(info, rows, maxcols, sub_size)
  if rows > @rows
    rows = @rows
  end

  # Copy in the new info.
  (1..rows).each do |x|
    (1..@cols).each do |y|
      if x <= rows && y <= sub_size[x]
        @info[x][y] = @info[x][y][0..[@colwidths[y], @info[x][y].size].min]
      else
        self.cleanCell(x, y)
      end
    end
  end
end

#unfocusObject



1164
1165
1166
# File 'lib/cdk/components/matrix.rb', line 1164

def unfocus
  self.draw(@box)
end