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

current_mark[RW]
selected_index[R]
selected_mark[RW]
selection_allowed[RW]
selection_key[RW]
unselected_mark[RW]

Public Class Methods

new(config={}) click to toggle source
Calls superclass method
# 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

_format_mark(index, state)
Alias for: mark_of_row
clear_selection() click to toggle source

clear selected index/indices

# File lib/umbra/listbox.rb, line 62
def clear_selection
  @selected_index = nil
end
color_of_row(index, state) click to toggle source

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

Calls superclass method
# 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
list=(alist) click to toggle source

set the list @param alist [Array<String>] string array to display as a list

Calls superclass method
# File lib/umbra/listbox.rb, line 55
def list=(alist)
  super
  clear_selection
end
map_keys() click to toggle source

Binds selection key to toggle_selection if selection enabled. All others pass to parent class.

Calls superclass method
# 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
mark_of_row(index, state) click to toggle source

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
Also aliased as: _format_mark
paint_row(win, row, col, line, index) click to toggle source

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_row(_row=@current_index) click to toggle source

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
state_of_row(index) click to toggle source

Determine state of the row Listbox adds :SELECTED state to Multiline. @param [Integer] offset of row in data

Calls superclass method
# File lib/umbra/listbox.rb, line 128
def state_of_row index
  _st = super
  if index == @selected_index
    _st = :SELECTED
  end #
  _st
end
toggle_selection(_row=@current_index) click to toggle source

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_row(_row=@current_index) click to toggle source

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