Class: RubyCurses::HorizList
- Inherits:
-
Widget
- Object
- Widget
- RubyCurses::HorizList
- Defined in:
- lib/rbcurse/extras/widgets/horizlist.rb
Overview
Horizontal list scrolling and selection.
== Example
require 'rbcurse/extras/widgets/horizlist'
require 'fileutils'
l = HorizList.new @form, :row => 5, :col => 5, :width => 80
list = Dir.glob("*")
l.list(list)
Instance Method Summary collapse
- #add(item) ⇒ Object
- #bounds_check ⇒ Object
- #current_value ⇒ Object (also: #text)
- #fire_action_event ⇒ Object
- #handle_key(ch) ⇒ Object
- #init_vars ⇒ Object
-
#initialize(form, config = {}, &block) ⇒ HorizList
constructor
A new instance of HorizList.
- #list(strings) ⇒ Object
-
#map_keys ⇒ Object
alias :text :getvalue # NEXT VERSION.
- #next_item(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object
- #on_enter ⇒ Object
- #on_enter_row(arow) ⇒ Object
- #on_leave_row(arow) ⇒ Object
- #previous_item(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object
- #repaint ⇒ Object
- #row_count ⇒ Object
- #scroll_left ⇒ Object
- #scroll_right ⇒ Object
Constructor Details
#initialize(form, config = {}, &block) ⇒ HorizList
Returns a new instance of HorizList.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 26 def initialize form, config={}, &block super @_events.push(*[:ENTER_ROW, :LEAVE_ROW, :PRESS]) @focusable = true init_vars @list = [] @toprow = 0 # last printed index @last_index = 0 # item on which cursor is @current_index = 0 end |
Instance Method Details
#add(item) ⇒ Object
55 56 57 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 55 def add item @list << item end |
#bounds_check ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 145 def bounds_check @row_changed = false if @oldrow != @current_index on_leave_row @oldrow if respond_to? :on_leave_row # to be defined by widget that has included this on_enter_row @current_index if respond_to? :on_enter_row # to be defined by widget that has included this set_form_row @row_changed = true end if @old_toprow != @toprow # only if scrolling has happened should we repaint @repaint_required = true @widget_scrolled = true end end |
#current_value ⇒ Object Also known as: text
179 180 181 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 179 def current_value @list[@current_index] end |
#fire_action_event ⇒ Object
175 176 177 178 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 175 def fire_action_event require 'rbcurse/core/include/ractionevent' fire_handler :PRESS, ActionEvent.new(self, :PRESS, text) end |
#handle_key(ch) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 83 def handle_key ch map_keys unless @keys_mapped ret = process_key ch, self @multiplier = 0 return :UNHANDLED if ret == :UNHANDLED end |
#init_vars ⇒ Object
40 41 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 40 def init_vars end |
#list(strings) ⇒ Object
52 53 54 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 52 def list(strings) @list = strings end |
#map_keys ⇒ Object
alias :text :getvalue # NEXT VERSION
44 45 46 47 48 49 50 51 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 44 def map_keys @keys_mapped = true bind_keys([KEY_RIGHT, ?l, ?.], :next_item) bind_keys([KEY_LEFT, ?h,?,], :previous_item) bind_keys([10,13,32]){ fire_action_event } bind_keys([?\M-l,?>, KEY_DOWN], :scroll_right) bind_keys([?\M-h,?<, KEY_UP], :scroll_left) end |
#next_item(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 104 def next_item num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier) rc = row_count return :NO_NEXT_ROW if @current_index == rc-1 # changed 2011-10-5 so process can do something @old_toprow = @toprow @oldrow = @current_index @current_index += num @current_index = rc - 1 if @current_index >= rc @toprow = @current_index if @current_index > @last_index bounds_check $multiplier = 0 @repaint_required = true end |
#on_enter ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 158 def on_enter if @list.nil? || @list.size == 0 Ncurses.beep return :UNHANDLED end super on_enter_row @current_index set_form_row true end |
#on_enter_row(arow) ⇒ Object
168 169 170 171 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 168 def on_enter_row arow fire_handler :ENTER_ROW, self @repaint_required = true end |
#on_leave_row(arow) ⇒ Object
172 173 174 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 172 def on_leave_row arow fire_handler :LEAVE_ROW, self end |
#previous_item(num = (($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier)) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 89 def previous_item num=(($multiplier.nil? or $multiplier == 0) ? 1 : $multiplier) return :NO_PREVIOUS_ROW if @current_index == 0 @old_toprow = @toprow @oldrow = @current_index @current_index -= num @current_index = 0 if @current_index < 0 bounds_check $multiplier = 0 if @current_index < @toprow @toprow = @current_index end @toprow = 0 if @toprow < 0 @repaint_required = true end |
#repaint ⇒ Object
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 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 58 def repaint return unless @repaint_required @window ||= @form.window $log.debug "XXX:HORIZ REPAINT Ci #{@current_index}, TR #{@toprow} " $status_message.value = " #{@current_index}, TR #{@toprow} " @window.printstring @row, @col, " "* @width, @color_pair || $reversecolor c = @col + 1 t = @toprow @list.each_with_index { |e, i| next if i < t # sucks for large lists break if c + e.length >= @width + @col att = @attrib || FFI::NCurses::A_NORMAL if i == @current_index att = FFI::NCurses::A_REVERSE if i == @current_index acolor = @focussed_color_pair || get_color($datacolor,'green', 'white') else acolor = @color_pair end @window.printstring @row, c, e, acolor || $reversecolor, att c += e.length + 2 @last_index = i break if c >= @width + @col } @repaint_required = false end |
#row_count ⇒ Object
144 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 144 def row_count; @list.size; end |
#scroll_left ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 130 def scroll_left return :NO_PREVIOUS_ROW if @current_index == 0 @old_toprow = @toprow @oldrow = @current_index @current_index = @toprow - 4 @current_index = 0 if @current_index < 0 bounds_check $multiplier = 0 if @current_index < @toprow @toprow = @current_index end @toprow = 0 if @toprow < 0 @repaint_required = true end |
#scroll_right ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rbcurse/extras/widgets/horizlist.rb', line 116 def scroll_right rc = row_count return :NO_NEXT_ROW if @current_index == rc-1 # changed 2011-10-5 so process can do something @old_toprow = @toprow @oldrow = @current_index @current_index = @last_index + 1 @current_index = rc - 1 if @current_index >= rc @toprow = @current_index if @current_index > @last_index bounds_check $multiplier = 0 @repaint_required = true end |