Class: Canis::Listbox

Inherits:
TextPad show all
Extended by:
Forwardable
Defined in:
lib/canis/core/widgets/listbox.rb

Overview

A scrollable, selectable array of strings. Delegates display to ListRenderer Delegates selection to Defaultlistselection (/include/listselectionmodel.rb) Due to extending Defaultlistselection, methods are not visible here. Selection methods are (the first three are what programmers will use the most):

-  `selected_values` : returns values selecteda (multiple selection)
-  `selected_value`  : returns value of row selected (single selection)
-  `selected_rows`   : same as selected_indices, indices of selected items

-  `toggle_row_selection` : toggles current row, called by key $row_selector
-  `select`               : select given or current row
-  `unselect`             : unselects given or current row
-  `is_row_selected?`     : determine if given row is selected
-  `is_selection_empty?`  : has anything been selected
-  `clear_selection`      : clear selection
-  `select_all`           : select all rows

Listbox also fires a ListSelectionEvent whose type can be:

- :INSERT , a row or rows added to selection
- :DELETE , a row or rows removed from selection
- :CLEAR , all selection cleared

Examples

mylist = %w[john tim matz shougo _why sean aaron]
l = Listbox.new @form, :row => 5, :col => 4, :height => 10, :width => 20, :list => mylist

Inside a Flow:

lb = listbox :list => mylist, :title => 'Contacts', :width_pc => 50, :selection_mode => :single

Since:

  • 1.2.0

Instance Attribute Summary

Attributes inherited from TextPad

#cols, #current_index, #document, #key_handler, #lastcol, #lastrow, #pad, #text_patterns

Attributes inherited from Widget

#_object_created, #col_offset, #config, #curpos, #focussed, #form, #handler, #id, #key_label, #parent_component, #row_offset, #state

Instance Method Summary collapse

Methods inherited from TextPad

#DEADhighlight_row, #ORIG_text, #ORIGnext_match, #[]=, #__calc_dimensions, #_do_borders, #_getarray, #_handle_key, #append, #ask_search, #backward_regex, #backward_word, #bottom_of_window, #bounds_check, #check_prow, #clear_pad, #clear_row, #content, #content_cols, #create_default_keyhandler, #current_value, #cursor_backward, #cursor_bol, #cursor_eol, #cursor_forward, #destroy, #down, #ensure_visible, #filename, #find_more, #fire_action_event, #fire_dimension_changed, #fire_row_changed, #forward_regex, #forward_word, #goto_end, #goto_last_position, #goto_line, #goto_start, #handle_key, #height=, #init_vars, #is_visible?, #lastcurpos, #map_keys, #middle_of_window, #next_match, #on_enter, #pad_cols, #pad_rows, #padrefresh, #preprocess_text, #print_foot, #render, #render_all, #repaint, #row_count, #rowcol, #scroll_backward, #scroll_forward, #scroll_left, #scroll_right, #scroll_window_down, #scroll_window_up, #scrollatrows, #set_form_col, #set_form_row, #text, #text_action_event, #to_searchable, #top_of_window, #up, #visual_index, #width=

Methods included from BorderTitle

#bordertitle_init, #print_borders, #print_title

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #command, #destroy, #focus, #focusable, #focusable?, #getvalue, #getvalue_for_paint, #handle_key, #hide, #init_vars, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint, #repaint_all, #repaint_required, #rowcol, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rb_getstr, #warn

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

Methods included from ConfigSetup

#config_setup, #variable_set

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Constructor Details

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

Returns a new instance of Listbox.

Since:

  • 1.2.0



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/canis/core/widgets/listbox.rb', line 85

def initialize form = nil, config={}, &block

  @left_margin = 0
  @should_show_focus = true

  register_events([:LEAVE_ROW, :LIST_SELECTION_EVENT])
  self.extend DefaultListSelection
  super
  # textpad takes care of enter_row and press
  #@_events.push(*[:LEAVE_ROW, :LIST_SELECTION_EVENT])
  bind_key(?f, 'next row starting with char'){ set_selection_for_char(nil) }

  # if user has not specified a selection model, install default
  unless @selection_mode == :none
    unless @list_selection_model
      create_default_selection_model
    end
  end
  # if user has not specified a renderer, install default
  unless @renderer
    create_default_renderer
  end
end

Instance Method Details

#clearObject Also known as: remove_all

Since:

  • 1.2.0



131
132
133
134
# File 'lib/canis/core/widgets/listbox.rb', line 131

def clear
  @selected_indices.clear
  super
end

#create_default_rendererObject

create a default renderer since user has not specified Widgets inheriting this with a differernt rendering such as tree can overrider this.

Since:

  • 1.2.0



111
112
113
114
# File 'lib/canis/core/widgets/listbox.rb', line 111

def create_default_renderer
  r = ListRenderer.new self
  renderer(r)
end

#create_default_selection_modelObject

create a default selection model Widgets inheriting this may override this

Since:

  • 1.2.0



123
124
125
# File 'lib/canis/core/widgets/listbox.rb', line 123

def create_default_selection_model
  list_selection_model(Canis::DefaultListSelectionModel.new self)
end

#next_regex(str) ⇒ Object

Find the next row that contains given string

Parameters:

  • String

    to find

Returns:

  • row and col offset of match, or nil

Since:

  • 1.2.0



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/canis/core/widgets/listbox.rb', line 195

def  next_regex str
  first = nil
  ## content can be string or Chunkline, so we had to write <tt>index</tt> for this.
  ## =~ does not give an error, but it does not work.
  @list.each_with_index do |line, ix|
    col = line =~ /#{str}/
    if col
      first ||= [ ix, col ]
      if ix > @current_index
        return [ix, col]
      end
    end
  end
  return first
end

#on_enter_row(arow) ⇒ Object

This is called whenever user enters a row

Since:

  • 1.2.0



147
148
149
150
151
152
153
154
155
156
# File 'lib/canis/core/widgets/listbox.rb', line 147

def on_enter_row arow
  super
  # TODO check if user wants focus to be showed
  ## this results in the row being entered and left being evaluated and repainted
  # which means that the focussed row can be bolded. The renderer's +render+ method will be triggered
  if @should_show_focus
    fire_row_changed @oldindex
    fire_row_changed arow
  end
end

#on_leave_row(arow) ⇒ Object

This is called whenever user leaves a row Fires handler for leave_row

Since:

  • 1.2.0



139
140
141
142
143
144
145
# File 'lib/canis/core/widgets/listbox.rb', line 139

def on_leave_row arow
  # leave this out, since we are not painting on exit of widget 2014-07-02 - 17:51 
  #if @should_show_focus
    #fire_row_changed arow
  #end
  fire_handler :LEAVE_ROW, self
end

#renderer(*val) ⇒ Object

Since:

  • 1.2.0



115
116
117
118
119
120
# File 'lib/canis/core/widgets/listbox.rb', line 115

def renderer *val
  if val.empty?
    return @renderer
  end
  @renderer = val[0]
end

#set_selection_for_char(char = nil) ⇒ Object

sets the selection to the next row starting with char Trying to return unhandled is having no effect right now. if only we could pop it into a stack or unget it.

Since:

  • 1.2.0



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/canis/core/widgets/listbox.rb', line 177

def set_selection_for_char char=nil
  char = _ask_a_char unless char
  return :UNHANDLED if char == :UNHANDLED
  #alert "got #{char}"
  @oldrow = @current_index
  @last_regex = /^#{char}/i
  ix = next_regex @last_regex
  return unless ix
  @current_index = ix[0] 
  #alert "curr ind #{@current_index} "
  @search_found_ix = @current_index
  @curpos = ix[1]
  ensure_visible
  return @current_index
end