Method: CDK::ENTRY#initialize

Defined in:
lib/cdk/entry.rb

#initialize(cdkscreen, xplace, yplace, title, label, field_attr, filler, disp_type, f_width, min, max, box, shadow) ⇒ ENTRY

Returns a new instance of ENTRY.



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
# File 'lib/cdk/entry.rb', line 9

def initialize(cdkscreen, xplace, yplace, title, label, field_attr, filler,
    disp_type, f_width, min, max, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  field_width = f_width
  box_width = 0
  xpos = xplace
  ypos = yplace
  
  self.setBox(box)
  box_height = @border_size * 2 + 1

  # 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)
  box_width = field_width + 2 * @border_size

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

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

  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 dimensinos 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 = [xpos]
  ytmp = [ypos]
  CDK.alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Make the label window.
  @win = cdkscreen.window.subwin(box_height, box_width, ypos, xpos)
  if @win.nil?
    self.destroy
    return nil
  end
  @win.keypad(true)

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

  if @field_win.nil?
    self.destroy
    return nil
  end
  @field_win.keypad(true)

  # make the label win, if we need to
  if !(label.nil?) && label.size > 0
    @label_win = @win.subwin(1, @label_len,
        ypos + @title_lines + @border_size,
        xpos + horizontal_adjust + @border_size)
  end

  # cleanChar (entry->info, max + 3, '\0');
  @info = ''
  @info_width = max + 3

  # Set up the rest of the structure.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @shadow_win = nil
  @field_attr = field_attr
  @field_width = field_width
  @filler = filler
  @hidden = filler
  @input_window = @field_win
  @accepts_focus = true
  @data_ptr = nil
  @shadow = shadow
  @screen_col = 0
  @left_char = 0
  @min = min
  @max = max
  @box_width = box_width
  @box_height = box_height
  @disp_type = disp_type
  @callbackfn = lambda do |entry, character|
    plainchar = Display.filterByDisplayType(entry, character)

    if plainchar == Ncurses::ERR || entry.info.size >= entry.max
      CDK.Beep
    else
      # Update the screen and pointer
      if entry.screen_col != entry.field_width - 1
        front = (entry.info[0...(entry.screen_col + entry.left_char)] or '')
        back = (entry.info[(entry.screen_col + entry.left_char)..-1] or '')
        entry.info = front + plainchar.chr + back
        entry.screen_col += 1
      else
        # Update the character pointer.
        entry.info << plainchar
        # Do not update the pointer if it's the last character
        if entry.info.size < entry.max
          entry.left_char += 1
        end
      end

      # Update the entry field.
      entry.drawField
    end
  end

  # Do we want a shadow?
  if shadow
    @shadow_win = cdkscreen.window.subwin(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  cdkscreen.register(:ENTRY, self)
end