Class: Luck::ListBox

Inherits:
Control show all
Defined in:
lib/luck/listbox.rb

Instance Attribute Summary collapse

Attributes inherited from Control

#display, #pane, #x1, #x2, #y1, #y2

Instance Method Summary collapse

Methods inherited from Control

#focus!, #height, #width

Constructor Details

#initialize(*args) ⇒ ListBox

Returns a new instance of ListBox.



13
14
15
16
17
18
19
20
# File 'lib/luck/listbox.rb', line 13

def initialize *args
  @data = []
  @hanging_indent = 0
  @offset = 0
  @index = 0
  @lastclick = Time.at 0
  super
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/luck/listbox.rb', line 3

def data
  @data
end

#hanging_indentObject

Returns the value of attribute hanging_indent.



3
4
5
# File 'lib/luck/listbox.rb', line 3

def hanging_indent
  @hanging_indent
end

#indexObject

Returns the value of attribute index.



3
4
5
# File 'lib/luck/listbox.rb', line 3

def index
  @index
end

#lastclickObject

Returns the value of attribute lastclick.



3
4
5
# File 'lib/luck/listbox.rb', line 3

def lastclick
  @lastclick
end

#numberingObject

Returns the value of attribute numbering.



3
4
5
# File 'lib/luck/listbox.rb', line 3

def numbering
  @numbering
end

#offsetObject

Returns the value of attribute offset.



3
4
5
# File 'lib/luck/listbox.rb', line 3

def offset
  @offset
end

Instance Method Details

#fit_data(offset = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/luck/listbox.rb', line 27

def fit_data offset=nil
  lines = 0
  items = 0
  
  data[offset || @offset, height].each do |line|
    _height = line_height(line)
    if _height + lines > height
      break
    else
      lines += _height
      items += 1
    end
  end
  
  [lines, items]
end

#fit_data_back(offset = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/luck/listbox.rb', line 60

def fit_data_back offset=nil
  lines = -1
  items = 0
  
  offset ||= @offset
  data[([offset - height,0].max)..([offset,0].max)].reverse.each do |line|
    _height = line_height(line)
    if _height + lines >= height
      break
    else
      lines += _height
      items += 1
    end
  end
  
  [lines, items]
end

#handle_char(char) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/luck/listbox.rb', line 109

def handle_char char
  if char == "\n"
    handler.call self, @data[@index] if handler
  elsif char == :up
    @index -= 1 if @index > 0
    @offset -= 1 if @offset > @index
  elsif char == :down
    if @index < @data.size - 1
      @index += 1 
      @offset += 1 if @offset + fit_data[1] <= @index
    end
  elsif char == :pageup
    @offset = [0, @offset - fit_data_back[1]].max
    @index = [@offset + fit_data[1] - 1, @index].min
  elsif char == :pagedown
    @offset = [@data.size - 1, @offset + fit_data[1]].min
    @offset = [@offset, @data.size - fit_data_back(@data.size)[1]].min
    @index = [@offset, @index].max
  end
  redraw
end

#handle_click(button, modifiers, x, y) ⇒ Object



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
# File 'lib/luck/listbox.rb', line 131

def handle_click button, modifiers, x, y
  if button == :scrollup
    @offset -= 1 if @offset > 0
    @index = [@offset + fit_data[1] - 1, @index].min
  elsif button == :scrolldown
    @offset += 1 if @offset < (@data.size - fit_data_back(@data.size)[1])
    @index = [@offset, @index].max
  
  elsif button == :left
    if Time.now - @lastclick < 0.5
      button = :double
    end
    @lastclick = Time.now
    
    previous = @index
    
    row = y1
    data[@offset, height].each_with_index do |line, index|
      _height = line_height(line)
      if _height + row > height
        break
      else
        row += _height
      end
      if row > y
        @index = index + @offset
        break
      end
    end
    
    handler.call self, @data[@index] if button == :double && previous == @index && handler
    
  end
  redraw
end

#handlerObject



9
10
11
# File 'lib/luck/listbox.rb', line 9

def handler
  @handler || @pane.handler
end

#line_height(line) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/luck/listbox.rb', line 44

def line_height line
  lines = 0
  
  line = line.to_s
  line = "##. #{line}" if @numbering
  length = line.size
  return 1 if length < 1
  offset = 0
  while offset < length
    lines += 1
    offset += width - ((offset > 0) ? @hanging_indent : 0)
  end
  
  lines
end

#number!Object



22
23
24
25
# File 'lib/luck/listbox.rb', line 22

def number!
  @numbering = true
  @hanging_indent = 4
end

#on_submit(&blck) ⇒ Object



5
6
7
# File 'lib/luck/listbox.rb', line 5

def on_submit &blck
  @handler = blck
end

#redrawObject



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
# File 'lib/luck/listbox.rb', line 78

def redraw
  @display.driver.cursor = false if @display.active_control == self
  
  row = y1
  return unless data
  data[@offset, height].each_with_index do |line, index|
    line = line.to_s
    number = index + @offset + 1
    line = "#{number.to_s.rjust 2}. #{line}" if @numbering
    print @display.color '1;34' if number == @index + 1
    length = line.size
    offset = 0
    while offset < length || offset == 0
      @display.place row, x1, ((' ' * ((offset > 0) ? @hanging_indent : 0)) + line[offset, width - ((offset > 0) ? @hanging_indent : 0)]).ljust(width, ' ')
      row += 1
      offset += width - ((offset > 0) ? @hanging_indent : 0)
      if row >= y2
        print @display.color '0' if number == @index + 1
        break
      end
    end
    print @display.color '0' if number == @index + 1
    break if row >= y2
  end
  
  until row >= y2
    @display.place row, x1, ' ' * width
    row += 1
  end
end