Class: Umbra::Listbox

Inherits:
Multiline show all
Defined in:
lib/umbra/listbox.rb

Overview

Display a list of items. Adds selection capability to the Multiline widget. Adds event :SELECT_ROW which fires on selection and unselection.

Instance Attribute Summary collapse

Attributes inherited from Multiline

#current_index, #list, #panned_cols, #search_offset

Attributes inherited from Widget

#col, #col_offset, #curpos, #focusable, #graphic, #handler, #key_label, #modified, #name, #repaint_required, #row, #row_offset, #state

Instance Method Summary collapse

Methods inherited from Multiline

#_truncate_to_width, #ask_search, #command, #current_row, #cursor_backward, #cursor_down, #cursor_end, #cursor_forward, #cursor_home, #cursor_up, #delete_at, #ensure_visible, #find_more, #fire_action_event, #getvalue, #goto_end, #goto_line, #goto_start, #handle_key, #insert, #is_visible?, #next_match, #on_enter, #on_enter_row, #on_leave, #on_leave_row, #page_backward, #page_forward, #print_row, #repaint, #row_count, #scroll_down, #scroll_left, #scroll_right, #scroll_up, #to_searchable, #value_of_row

Methods inherited from Widget

#_form=, #color_pair, #command, #getvalue, #getvalue_for_paint, #handle_key, #height, #highlight_attr, #init_vars, #modified?, #on_enter, #on_leave, #repaint, #repaint_all, #rowcol, #set_form_col, #set_form_row, #text, #touch, #variable_set, #visible, #width

Methods included from KeyMappingHandler

#_process_key, #bind_key, #bind_keys, #process_key, #unbind_key

Methods included from EventHandler

#bind_event, #event?, #fire_handler, #fire_property_change, #register_events

Constructor Details

#initialize(config = {}, &block) ⇒ Listbox

Returns a new instance of Listbox.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Attribute Details

#current_markObject

row current character (default is >)



34
35
36
# File 'lib/umbra/listbox.rb', line 34

def current_mark
  @current_mark
end

#selected_indexObject (readonly)

row selected, may change to plural



27
28
29
# File 'lib/umbra/listbox.rb', line 27

def selected_index
  @selected_index
end

#selected_markObject

row selected character



32
33
34
# File 'lib/umbra/listbox.rb', line 32

def selected_mark
  @selected_mark
end

#selection_allowedObject

does this class allow row selection (should be class level)



25
26
27
# File 'lib/umbra/listbox.rb', line 25

def selection_allowed
  @selection_allowed
end

#selection_keyObject

key used to select a row



26
27
28
# File 'lib/umbra/listbox.rb', line 26

def selection_key
  @selection_key
end

#unselected_markObject

row unselected character (usually blank)



33
34
35
# File 'lib/umbra/listbox.rb', line 33

def unselected_mark
  @unselected_mark
end

Instance Method Details

#clear_selectionObject

clear selected index/indices



62
63
64
# File 'lib/umbra/listbox.rb', line 62

def clear_selection
  @selected_index = nil
end

#color_of_row(index, state) ⇒ Array

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.

Parameters:

  • offset (Integer)

    of row in data

  • state (Symbol)

    state of current row

Returns:

  • (Array)

    color_pair and attrib constant



163
164
165
166
167
168
169
# 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) ⇒ Object

set the list

Parameters:

  • alist (Array<String>)

    string array to display as a list



55
56
57
58
# File 'lib/umbra/listbox.rb', line 55

def list=(alist)
  super
  clear_selection
end

#map_keysObject

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



67
68
69
70
71
72
73
# 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) ⇒ String Also known as: _format_mark

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.

Parameters:

  • index (Integer)

    offset of row in data

  • state (Symbol)

    state of current row

Returns:

  • (String)

    aracter to be displayed inside left margin



143
144
145
146
147
148
149
150
151
152
# 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_row(win, row, col, line, index) ⇒ Object

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.

Parameters:

  • win (Window)
    • window pointer for printing

  • row (Integer)
    • row offset on screen

  • col (Integer)
    • col offset on screen

  • line (String)
    • line to print

  • index (Integer)
    • offset in List array



111
112
113
114
115
116
117
118
119
120
121
122
123
# 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) ⇒ Object

select given row, and fire SELECT_ROW handler

Parameters:

  • _row (Integer) (defaults to: @current_index)

    row to select, default to current row



88
89
90
91
# 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) ⇒ Object

Determine state of the row Listbox adds :SELECTED state to Multiline.

Parameters:

  • offset (Integer)

    of row in data



128
129
130
131
132
133
134
# 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) ⇒ Object

Toggle current row’s selection status.

Parameters:

  • _row (Integer) (defaults to: @current_index)

    row to toggle, default to current row



77
78
79
80
81
82
83
84
# 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) ⇒ Object

unselect given row, and fire SELECT_ROW handler

Parameters:

  • _row (Integer) (defaults to: @current_index)

    row to unselect, default to current row



95
96
97
98
99
100
# 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