class Umbra::Listbox
Display a list of items. Adds selection capability to the Multiline
widget. Adds event :SELECT_ROW which fires on selection and unselection.
Attributes
Public Class Methods
# File lib/umbra/listbox.rb, line 36 def initialize config={}, &block @selection_allowed = true # does this class allow selection of row @selected_index = nil # index of row selected @selection_key = ?s.getbyte(0) # 's' used to select/deselect @selected_color_pair = CP_RED @selected_attr = REVERSE @selected_mark = 'x' # row selected character @unselected_mark = ' ' # row unselected character (usually blank) @current_mark = '>' # row current character (default is >) #register_events([:LIST_SELECTION_EVENT]) register_events([:SELECT_ROW]) super @search_offset = 1 # search has offset of 1, due to added mark end
Public Instance Methods
clear selected index/indices
# File lib/umbra/listbox.rb, line 62 def clear_selection @selected_index = nil end
Determine color and attribute of row. Overriding this allows application to have customized row colors based on data
which can be determined using +index+.
Listbox
adds :SELECTED state to Multiline
. @param [Integer] offset of row in data @param state [Symbol] state of current row @return [Array] color_pair and attrib constant
# File lib/umbra/listbox.rb, line 163 def color_of_row index, state arr = super if state == :SELECTED arr = [@selected_color_pair, @selected_attr] end arr end
set the list @param alist [Array<String>] string array to display as a list
# File lib/umbra/listbox.rb, line 55 def list=(alist) super clear_selection end
Binds selection key to toggle_selection
if selection enabled. All others pass to parent class.
# File lib/umbra/listbox.rb, line 67 def map_keys return if @keys_mapped if @selection_allowed and @selection_key bind_key(@selection_key, 'toggle_selection') { toggle_selection } end super end
Determine the mark on the left of the row. The mark depends on the state: :SELECTED :HIGHLIGHTED :CURRENT :NORMAL Listbox
adds :SELECTED state to Multiline
. @param index [Integer] offset of row in data @param state [Symbol] state of current row @return [String] aracter to be displayed inside left margin
# File lib/umbra/listbox.rb, line 143 def mark_of_row index, state mark = case state when :SELECTED @selected_mark when :HIGHLIGHTED, :CURRENT @current_mark else @unselected_mark end end
Paint the row. For any major customization of Listbox
output, this method would be overridden. This method determines state, mark, slice of line item to show. listbox adds a mark on the side, whether a row is selected or not, and whether it is current. @param win [Window] - window pointer for printing @param row [Integer] - row offset on screen @param col [Integer] - col offset on screen @param line [String] - line to print @param index [Integer] - offset in List array
# File lib/umbra/listbox.rb, line 111 def paint_row(win, row, col, line, index) state = state_of_row(index) f = value_of_row(line, index, state) mark = mark_of_row(index, state) ff = "#{mark}#{f}" ff = _truncate_to_width( ff ) ## truncate and handle panning print_row(win, row, col, ff, index, state) end
select given row, and fire SELECT_ROW handler @param _row [Integer] row to select, default to current row
# File lib/umbra/listbox.rb, line 88 def select_row _row=@current_index @selected_index = _row fire_handler :SELECT_ROW, self # use selected_index to know which one end
Toggle current row's selection status. @param _row [Integer] row to toggle, default to current row
# File lib/umbra/listbox.rb, line 77 def toggle_selection _row=@current_index @repaint_required = true if @selected_index == _row unselect_row _row else select_row _row end end
unselect given row, and fire SELECT_ROW handler @param _row [Integer] row to unselect, default to current row
# File lib/umbra/listbox.rb, line 95 def unselect_row _row=@current_index if _row == @selected_index @selected_index = nil fire_handler :SELECT_ROW, self # use selected_index to know which one end end