class Canis::List
scrollable, selectable list of items
- @selected contains indices of selected objects.
A readonly control for displaying a list of data or values. Although user editing is not allowed, but the list may be repopulated as in a directory listing, or a list dependent on some other control's value. This is not a drop-in replacement for Listbox
as it drops many methods that are redundant. Default selection is single, as opposed to Listbox
.
Attributes
dsl_accessor :default_values # array of default values dsl_accessor :is_popup # if it is in a popup and single select, selection closes
please set these in he constructor block. Settin them later will have no effect since i would have bound them to actions
index of selected row
dsl_accessor :height # << widget already has this dsl_accessor :title dsl_property :title_attrib # bold, reverse, normal
dsl_accessor :list # the array of data to be sent by user
Public Class Methods
basic listbox constructor
# File lib/canis/core/widgets/deprecated/rlist.rb, line 81 def initialize form=nil, config={}, &block @focusable = true @editable = false @sanitization_required = true # cleanup control and non print chars @truncation_required = true @suppress_borders = false #to_print_borders = 1 #@row_selected_symbol = '' # thi sprevents default value from being set @row = 0 @col = 0 # data of listbox this is not an array, its a pointer to the listdatamodel @list = nil # any special attribs such as status to be printed in col1, or color (selection) @list_attribs = {} @current_index = 0 @selected_indices = [] @selected_index = nil @row_offset = @col_offset = 1 @should_show_focus = true # Here's its on since the cellrenderer will show it on repaint @one_key_selection = false # use vim keys @selection_mode = :multiple # default is multiple, anything else given becomes single super @_events.push(*[:ENTER_ROW, :LEAVE_ROW, :LIST_SELECTION_EVENT, :PRESS]) # I have moved this here so user can override keys. map_keys unless @keys_mapped @win = @graphic # 2010-01-04 12:36 BUFFERED replace form.window with graphic @win_left = 0 @win_top = 0 init_vars @internal_width = 2 #@internal_width = 0 if @suppress_borders # NOTE it is 1 in bordertitle bordertitle_init if @list && !@selected_index.nil? # XXX set_focus_on @selected_index # the new version end init_actions end
Public Instance Methods
get element at @param [Fixnum] index for element @return [Object] element @since 1.2.0 2010-09-06 14:33 making life easier for others.
# File lib/canis/core/widgets/deprecated/rlist.rb, line 209 def [](off0) @list[off0] end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 234 def append text @list.push text @widget_scrolled = true @repaint_required = true end
gets string to search and calls data models find prev
# File lib/canis/core/widgets/deprecated/rlist.rb, line 360 def ask_search_backward regex = get_string("Enter regex to search (backward)") @last_regex = regex ix = @list.find_prev regex, @current_index if ix.nil? alert("No matching data for: #{regex}") else set_focus_on(ix) end end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 350 def ask_search_forward regex = get_string("Enter regex to search") ix = @list.find_match regex if ix.nil? alert("No matching data for: #{regex}") else set_focus_on(ix) end end
get a keystroke from user and go to first item starting with that key
# File lib/canis/core/widgets/deprecated/rlist.rb, line 343 def ask_selection_for_char ch = @graphic.getch if ch < 0 || ch > 255 return :UNHANDLED end ret = set_selection_for_char ch.chr end
getter and setter for cell_renderer
NOTE: due to our optimization of not repainting unless data has changed or scrolling has happened, highlighting focus will repaint the row if you've colored it. In that case, set show_show_focus
to false
# File lib/canis/core/widgets/deprecated/rlist.rb, line 400 def cell_renderer(*val) if val.empty? @cell_renderer ||= create_default_cell_renderer else @cell_renderer = val[0] end end
the idea here is to allow users who subclass Listbox
to easily override parts of the cumbersome repaint method. This assumes your List
has some data, but you print a lot more. Now you don't need to change the data in the renderer, or keep formatted data in the list itself. e.g. @list contains file names, or File objects, and this converts to a long listing. If the renderer did that, the truncation would be on wrong data. @since 1.2.0
# File lib/canis/core/widgets/deprecated/rlist.rb, line 526 def convert_value_to_text value, crow case value when TrueClass, FalseClass value else value.to_s if value end end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 407 def create_default_cell_renderer return ListCellRenderer.new "", {"color"=>@color, "bgcolor"=>@bgcolor, "parent" => self, "display_length"=> @width-@internal_width-@left_margin} #return BasicListCellRenderer.new "", {"color"=>@color, "bgcolor"=>@bgcolor, "parent" => self, "display_length"=> @width-2-@left_margin} end
avoid using “row”, i'd rather stick with “index” and “value”.
return object under cursor Note: this should not be confused with selected row/s. User may not have selected this. This is only useful since in some demos we like to change a status bar as a user scrolls down @since 1.2.0 2010-09-06 14:33 making life easier for others.
# File lib/canis/core/widgets/deprecated/rlist.rb, line 216 def current_value @list[@current_index] end
triggered on hitting ENTER button
# File lib/canis/core/widgets/deprecated/rlist.rb, line 338 def fire_action_event require 'canis/core/include/ractionevent' fire_handler :PRESS, ActionEvent.new(self, :PRESS, text) end
START FOR scrollable ###
# File lib/canis/core/widgets/deprecated/rlist.rb, line 244 def get_content @list end
Returns selected indices Indices are often required since the renderer may modify the values displayed
# File lib/canis/core/widgets/deprecated/rlist.rb, line 612 def get_selected_indices; @selected_indices; end
Returns selected values
# File lib/canis/core/widgets/deprecated/rlist.rb, line 616 def get_selected_values selected = [] @selected_indices.each { |i| selected << @list[i] } return selected end
END FOR scrollable ###
override widgets text returns indices of selected rows
# File lib/canis/core/widgets/deprecated/rlist.rb, line 253 def getvalue selected_rows end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 595 def goto_next_selection return if selected_rows().length == 0 row = selected_rows().sort.find { |i| i > @current_index } row ||= @current_index @current_index = row @repaint_required = true # fire list_select XXX end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 602 def goto_prev_selection return if selected_rows().length == 0 row = selected_rows().sort{|a,b| b <=> a}.find { |i| i < @current_index } row ||= @current_index @current_index = row @repaint_required = true # fire list_select XXX end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 496 def highlight_selected_row r=nil, c=nil, acolor=nil return unless @selected_index # no selection r = _convert_index_to_printable_row(@selected_index) unless r return unless r # not on screen unless c _r, c = rowcol end acolor ||= get_color $promptcolor, @selected_color, @selected_bgcolor att = FFI::NCurses::A_REVERSE att = get_attrib(@selected_attrib) if @selected_attrib @graphic.mvchgat(y=r, x=c, @width-@internal_width-@left_margin, att , acolor , nil) end
Define actions that can be popped up by PromptMenu
or other menubar Currently, only PromptMenu
, but we can start contextually appending to Menubar or others
# File lib/canis/core/widgets/deprecated/rlist.rb, line 625 def init_actions am = action_manager() am.add_action(Action.new("&Disable selection") { @selection_mode = :none; unbind_key(32); bind_key(32, :scroll_forward); } ) am.add_action(Action.new("&Edit Toggle") { @edit_toggle = !@edit_toggle; $status_message.value = "Edit toggle is #{@edit_toggle}" }) end
this is called several times, from constructor and when list data changed, so only put relevant resets here.
# File lib/canis/core/widgets/deprecated/rlist.rb, line 121 def init_vars @repaint_required = true @widget_scrolled = true # 2011-10-15 @toprow = @pcol = 0 if @show_selector @row_selected_symbol ||= '>' @row_unselected_symbol ||= ' ' @left_margin ||= @row_selected_symbol.length end @row_selected_symbol ||= '' #@left_margin ||= 0 @one_key_selection = false if @one_key_selection.nil? @row_offset = @col_offset = 0 if @suppress_borders end
provide data to List
in the form of an Array or Variable
or ListDataModel. This will create a default ListSelectionModel.
CHANGE as on 2010-09-21 12:53: If explicit nil passed then dummy datamodel and selection model created From now on, constructor will call this, so this can always happen.
NOTE: sometimes this can be added much after its painted. Do not expect this to be called from constructor, although that is the usual case. it can be dependent on some other list or tree. @param [Array, Variable
, ListDataModel] data to populate list with @return [ListDataModel] just created or assigned
# File lib/canis/core/widgets/deprecated/rlist.rb, line 175 def list *val return @list if val.empty? alist = val[0] case alist when Array @list = alist # I possibly should call init_vars in these 3 cases but am doing the minimal 2013-04-10 - 18:27 # Based no issue: https://github.com/mare-imbrium/canis/issues/15 @current_index = @toprow = @pcol = 0 when NilClass @list = [] # or nil ? @current_index = @toprow = @pcol = 0 when Variable @list = alist.value @current_index = @toprow = @pcol = 0 else raise ArgumentError, "Listbox list(): do not know how to handle #{alist.class} " end clear_selection @repaint_required = true @widget_scrolled = true # 2011-10-15 @list end
be informed when data has changed. required here, was being called by listdatamodel earlier
# File lib/canis/core/widgets/deprecated/rlist.rb, line 572 def list_data_changed if row_count == 0 # added on 2009-02-02 17:13 so cursor not hanging on last row which could be empty init_vars @current_index = 0 set_form_row end @widget_scrolled = true # 2011-10-15 @repaint_required = true end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 199 def list_data_model; @list; end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 136 def map_keys return if @keys_mapped require 'canis/core/include/deprecated/listbindings' bindings() bind_key(?f, 'next row starting with char'){ ask_selection_for_char() } bind_key(?\M-v, 'toggle one_key_selection'){ @one_key_selection = !@one_key_selection } bind_key(13, 'fire action event'){ fire_action_event } list_bindings unless @selection_mode == :none # listselectable @keys_mapped = true end
please check for error before proceeding @return [Boolean] false if no data
# File lib/canis/core/widgets/deprecated/rlist.rb, line 372 def on_enter if @list.nil? || @list.size == 0 Ncurses.beep return :UNHANDLED end super # forgot this 2011-10-9 that's why events not firign on_enter_row @current_index set_form_row # added 2009-01-11 23:41 true end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 382 def on_enter_row arow # copied from resultsettextview, can this not be in one place like listscrollable ? FIXME if @should_show_focus highlight_focussed_row :FOCUSSED unless @oldrow == @selected_index highlight_focussed_row :UNFOCUSSED end end fire_handler :ENTER_ROW, self @repaint_required = true end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 393 def on_leave_row arow fire_handler :LEAVE_ROW, self end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 219 def remove_all return if @list.nil? || @list.empty? @list = [] init_vars end
returns count of row, needed by scrollbar and others.
# File lib/canis/core/widgets/deprecated/rlist.rb, line 149 def row_count return 0 if @list.nil? @list.length end
# File lib/canis/core/widgets/deprecated/rlist.rb, line 508 def unhighlight_row index, r=nil, c=nil, acolor=nil return unless index # no selection r = _convert_index_to_printable_row(index) unless r return unless r # not on screen unless c _r, c = rowcol end acolor ||= get_color $datacolor att = FFI::NCurses::A_NORMAL att = get_attrib(@normal_attrib) if @normal_attrib @graphic.mvchgat(y=r, x=c, @width-@internal_width-@left_margin, att , acolor , nil) end