Method: CDK::ALPHALIST#initialize

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

#initialize(cdkscreen, xplace, yplace, height, width, title, label, list, list_size, filler_char, highlight, box, shadow) ⇒ ALPHALIST

Returns a new instance of ALPHALIST.



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
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
281
282
# File 'lib/cdk/components/alphalist.rb', line 10

def initialize(cdkscreen, xplace, yplace, height, width, title, label,
    list, list_size, filler_char, highlight, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy
  box_width = width
  box_height = height
  label_len = 0
  bindings = {
    CDK::BACKCHAR => Ncurses::KEY_PPAGE,
    CDK::FORCHAR  => Ncurses::KEY_NPAGE,
  }

  if !self.createList(list, list_size)
    self.destroy
    return nil
  end

  self.setBox(box)

  # If the height is a negative value, the height will be ROWS-height,
  # otherwise the height will be the given height.
  box_height = CDK.setWidgetDimension(parent_height, height, 0)

  # If the width is a negative value, the width will be COLS-width,
  # otherwise the width will be the given width.
  box_width = CDK.setWidgetDimension(parent_width, width, 0)

  # Translate the label string to a chtype array
  if label.size > 0
    lentmp = []
    chtype_label = char2Chtype(label, lentmp, [])
    label_len = lentmp[0]
  end

  # 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 file selector window.
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)

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

  # Set some variables.
  @screen = cdkscreen
  @parent = cdkscreen.window
  @highlight = highlight
  @filler_char = filler_char
  @box_height = box_height
  @box_width = box_width
  @shadow = shadow
  @shadow_win = nil

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

  # Create the entry field.
  temp_width =  if CDK::ALPHALIST.isFullWidth(width)
                then CDK::FULL
                else box_width - 2 - label_len
                end
  @entry_field = CDK::ENTRY.new(cdkscreen, @win.getbegx, @win.getbegy,
      title, label, Ncurses::A_NORMAL, filler_char, :MIXED, temp_width,
      0, 512, box, false)
  if @entry_field.nil?
    self.destroy
    return nil
  end
  @entry_field.setLLchar(Ncurses::ACS_LTEE)
  @entry_field.setLRchar(Ncurses::ACS_RTEE)

  # Callback functions
  adjust_alphalist_cb = lambda do |object_type, object, alphalist, key|
    scrollp = alphalist.scroll_field
    entry = alphalist.entry_field

    if scrollp.list_size > 0
      # Adjust the scrolling list.
      alphalist.injectMyScroller(key)

      # Set the value in the entry field.
      current = chtype2Char(scrollp.item[scrollp.current_item])
      entry.setValue(current)
      entry.draw(entry.box)
      return true
    end
    CDK.Beep
    return false
  end

  complete_word_cb = lambda do |object_type, object, alphalist, key|
    entry = alphalist.entry_field
    scrollp = nil
    selected = -1
    ret = 0
    alt_words = []

    if entry.info.size == 0
      CDK.Beep
      return true
    end
    
    # Look for a unique word match.
    index = search_list(alphalist.list, alphalist.list.size, entry.info)

    # if the index is less than zero, return we didn't find a match
    if index < 0
      CDK.Beep
      return true
    end

    # Did we find the last word in the list?
    if index == alphalist.list.size - 1
      entry.setValue(alphalist.list[index])
      entry.draw(entry.box)
      return true
    end


    # Ok, we found a match, is the next item similar?
    len = [entry.info.size, alphalist.list[index + 1].size].min
    ret = alphalist.list[index + 1][0...len] <=> entry.info
    if ret == 0
      current_index = index
      match = 0
      selected = -1

      # Start looking for alternate words
      # FIXME(original): bsearch would be more suitable.
      while current_index < alphalist.list.size &&
          (alphalist.list[current_index][0...len] <=> entry.info) == 0
        alt_words << alphalist.list[current_index]
        current_index += 1
      end

      # Determine the height of the scrolling list.
      height = if alt_words.size < 8 then alt_words.size + 3 else 11 end

      # Create a scrolling list of close matches.
      scrollp = CDK::SCROLL.new(entry.screen,
          CDK::CENTER, CDK::CENTER, CDK::RIGHT, height, -30,
          "<C></B/5>Possible Matches.", alt_words, alt_words.size,
          true, Ncurses::A_REVERSE, true, false)

      # Allow them to select a close match.
      match = scrollp.activate([])
      selected = scrollp.current_item

      # Check how they exited the list.
      if scrollp.exit_type == :ESCAPE_HIT
        # Destroy the scrolling list.
        scrollp.destroy

        # Beep at the user.
        CDK.Beep

        # Redraw the alphalist and return.
        alphalist.draw(alphalist.box)
        return true
      end

      # Destroy the scrolling list.
      scrollp.destroy

      # Set the entry field to the selected value.
      entry.set(alt_words[match], entry.min, entry.max, entry.box)

      # Move the highlight bar down to the selected value.
      (0...selected).each do |x|
        alphalist.injectMyScroller(Ncurses::KEY_DOWN)
      end

      # Redraw the alphalist.
      alphalist.draw(alphalist.box)
    else
      # Set the entry field with the found item.
      entry.set(alphalist.list[index], entry.min, entry.max, entry.box)
      entry.draw(entry.box)
    end
    return true
  end

  pre_process_entry_field = lambda do |cdktype, object, alphalist, input|
    scrollp = alphalist.scroll_field
    entry = alphalist.entry_field
    info_len = entry.info.size
    result = 1
    empty = false

    if alphalist.isBind(:ALPHALIST, input)
      result = 1  # Don't try to use this key in editing
    elsif (CDK.isChar(input) &&
        input.chr.match(/^[[:alnum:][:punct:]]$/)) ||
        [Ncurses::KEY_BACKSPACE, Ncurses::KEY_DC].include?(input)
      index = 0
      curr_pos = entry.screen_col + entry.left_char
      pattern = entry.info.clone
      if [Ncurses::KEY_BACKSPACE, Ncurses::KEY_DC].include?(input)
        if input == Ncurses::KEY_BACKSPACE
          curr_pos -= 1
        end
        if curr_pos >= 0
          pattern.slice!(curr_pos)
        end
      else
        front = (pattern[0...curr_pos] or '')
        back = (pattern[curr_pos..-1] or '')
        pattern = front + input.chr + back
      end

      if pattern.size == 0
        empty = true
      elsif (index = search_list(alphalist.list,
          alphalist.list.size, pattern)) >= 0
        # XXX: original uses n scroll downs/ups for <10 positions change
          scrollp.setPosition(index)
        alphalist.drawMyScroller
      else
        CDK.Beep
        result = 0
      end
    end

    if empty
      scrollp.setPosition(0)
      alphalist.drawMyScroller
    end

    return result
  end

  # Set the key bindings for the entry field.
  @entry_field.bind(:ENTRY, Ncurses::KEY_UP, adjust_alphalist_cb, self)
  @entry_field.bind(:ENTRY, Ncurses::KEY_DOWN, adjust_alphalist_cb, self)
  @entry_field.bind(:ENTRY, Ncurses::KEY_NPAGE, adjust_alphalist_cb, self)
  @entry_field.bind(:ENTRY, Ncurses::KEY_PPAGE, adjust_alphalist_cb, self)
  @entry_field.bind(:ENTRY, CDK::KEY_TAB, complete_word_cb, self)

  # Set up the post-process function for the entry field.
  @entry_field.setPreProcess(pre_process_entry_field, self)

  # Create the scrolling list.  It overlaps the entry field by one line if
  # we are using box-borders.
  temp_height = @entry_field.win.getmaxy - @border_size
  temp_width = if CDK::ALPHALIST.isFullWidth(width)
               then CDK::FULL
               else box_width - 1
               end
  @scroll_field = CDK::SCROLL.new(cdkscreen, @win.getbegx,
      @entry_field.win.getbegy + temp_height, CDK::RIGHT,
      box_height - temp_height, temp_width, '', list, list_size,
      false, Ncurses::A_REVERSE, box, false)
  @scroll_field.setULchar(Ncurses::ACS_LTEE)
  @scroll_field.setURchar(Ncurses::ACS_RTEE)

  # Setup the key bindings.
  bindings.each do |from, to|
    self.bind(:ALPHALIST, from, :getc, to)
  end

  cdkscreen.register(:ALPHALIST, self)
end