Method: CDK::MENTRY#initialize

Defined in:
lib/cdk/components/mentry.rb

#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