Class: Canis::Tree
- Includes:
- ListOperations, ListScrollable
- Defined in:
- lib/canis/core/widgets/deprecated/rtree.rb,
lib/canis/core/widgets/tree.rb
Overview
a representation of heirarchical data in outline form Currently supports only single selection, and does not allow editing.
Constant Summary collapse
- TREE_EVENTS =
[:ENTER_ROW, :LEAVE_ROW, :TREE_COLLAPSED_EVENT, :TREE_EXPANDED_EVENT, :TREE_SELECTION_EVENT, :TREE_WILL_COLLAPSE_EVENT, :TREE_WILL_EXPAND_EVENT]
Instance Attribute Summary collapse
-
#current_index ⇒ Object
Returns the value of attribute current_index.
-
#one_key_selection ⇒ Object
dsl_accessor :valign # popup related.
-
#selected_index ⇒ Object
readonly
index of row selected, relates to internal representation, not tree.
-
#toprow ⇒ Object
readonly
dsl_accessor :title dsl_property :title_attrib # bold, reverse, normal dsl_accessor :border_attrib, :border_color # FIXME not used currently.
-
#treemodel ⇒ Object
readonly
returns treemodel for further actions 2011-10-2.
Attributes included from ListScrollable
#find_offset, #find_offset1, #search_found_ix, #show_caret
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
-
#<<(array) ⇒ Object
add a row to the table The name add will be removed soon, pls use << instead.
-
#_list ⇒ Object
private, for use by repaint.
-
#ask_search_backward ⇒ Object
gets string to search and calls data models find prev.
- #ask_search_forward ⇒ Object
-
#ask_selection_for_char ⇒ Object
get a keystroke from user and go to first item starting with that key.
-
#cell_renderer(*val) ⇒ Object
getter and setter for cell_renderer.
- #collapse_children(node = :current_index) ⇒ Object
- #collapse_node(node) ⇒ Object
-
#collapse_parent(node = :current_index) ⇒ Object
collapse parent can use multiplier.
-
#command(*args, &block) ⇒ Object
default block.
- #convert_to_list(tree) ⇒ Object
- #create_default_cell_renderer ⇒ Object
- #create_default_renderer ⇒ Object
-
#current_row ⇒ Object
(also: #text)
return object under cursor Note: this should not be confused with selected row/s.
-
#current_value ⇒ Object
return object under cursor Note: this should not be confused with selected row/s.
-
#data(alist = nil) ⇒ Object
pass data to create this tree model used to be list.
-
#delete_at(ix) ⇒ Object
delete a data row at index .
-
#expand_children(node = :current_index) ⇒ Object
this expands all the children of a node, recursively we can’t use multiplier concept here since we are doing a preorder enumeration we need to do a breadth first enumeration to use a multiplier.
- #expand_node(node) ⇒ Object
-
#expand_parents(node) ⇒ Object
goes up to root of this node, and expands down to this node this is often required to make a specific node visible such as in a dir listing when current dir is deep in heirarchy.
-
#get_content ⇒ Object
START FOR scrollable ###.
- #get_expanded_descendants(node) ⇒ Object
-
#get_node_for_path(user_path) ⇒ Object
To retrieve the node corresponding to a path specified as an array or string Do not mention the root.
- #get_window ⇒ Object
-
#getvalue ⇒ Object
END FOR scrollable ### override widgets text.
- #goto_parent(node = :current_index) ⇒ Object
-
#handle_key(ch) ⇒ Object
Listbox.
- #has_been_expanded(node) ⇒ Object
- #init_vars ⇒ Object
-
#initialize(form, config = {}, &block) ⇒ Tree
constructor
A new instance of Tree.
- #is_row_selected?(row = @current_index) ⇒ Boolean
- #list_data_changed ⇒ Object
-
#map_keys ⇒ Object
maps keys to methods checks @key_map can be :emacs or :vim.
-
#mark_parents_expanded(node) ⇒ Object
this is required to make a node visible, if you wish to start from a node that is not root e.g.
- #node_collapsed?(node) ⇒ Boolean
- #node_expanded?(node) ⇒ Boolean
-
#node_to_row(node) ⇒ Object
convert a given node to row.
- #OLDprint_borders ⇒ Object
- #OLDprint_title ⇒ Object
-
#on_enter ⇒ Boolean
please check for error before proceeding.
- #on_enter_row(arow) ⇒ Object
- #on_leave_row(arow) ⇒ Object
-
#print_foot ⇒ Object
print footer containing line and total, overriding textpad which prints column offset also This is called internally by repaint() but can be overridden for more complex printing.
-
#renderer(*val) ⇒ Object
supply a custom renderer that implements render().
-
#repaint ⇒ Object
this method chops the data to length before giving it to the renderer, this can cause problems if the renderer does some processing.
-
#root(node = nil, asks_allow_children = false, &block) ⇒ Object
Sets the given node as root and returns treemodel.
- #row_collapsed?(row) ⇒ Boolean
- #row_count ⇒ Object
-
#row_expanded?(row) ⇒ TreeNode?
Returns selected node or nil.
-
#row_selected?(row) ⇒ Boolean
private related to index in representation, not tree.
- #row_to_node(row = @current_index) ⇒ Object
-
#scrollatrow ⇒ Object
at what row should scrolling begin.
-
#select_default_values ⇒ Object
show default value as selected and fire handler for it This is called in repaint, so can raise an error if called on creation or before repaint.
- #selected_row ⇒ Object
-
#set_default_selection_model ⇒ Object
set the default selection model as the operational one.
- #set_expanded_state(node, state) ⇒ Object
- #set_form_col(col1 = 0) ⇒ Object
- #toggle_expanded_state(row = @current_index) ⇒ Object
-
#toggle_row_selection ⇒ Object
An event is thrown when a row is selected or deselected.
-
#traverse(node, level = 0) {|node, level| ... } ⇒ Object
$log.debug “XXX convert #treetree.roottree.root.childrentree.root.children.size ” $log.debug “ converted tree to list.
- #traverse_expanded(node, nodes) ⇒ Object
-
#XXXpadrefresh ⇒ Object
refresh pad onto window overrides super due to header_adjustment and the header too.
Methods included from ListScrollable
#_convert_index_to_printable_row, #_convert_index_to_visible_row, #_find_next, #_find_prev, #ask_search, #backward_word, #bounds_check, #find_more, #find_next, #find_prev, #focussed_index, #forward_char, #forward_word, #goto_bottom, #goto_last_position, #goto_top, #highlight_focussed_row, #install_keys, #is_visible?, #next_match, #next_row, #previous_row, #sanitize, #scroll_backward, #scroll_forward, #scroll_left, #scroll_right, #selected_item, #set_focus_on, #set_form_row, #set_selection_for_char, #show_caret_func, #truncate
Methods included from ListOperations
#next_regex, #set_selection_for_char
Methods inherited from Widget
#action_manager, #bgcolor, #color, #color_pair, #destroy, #focus, #focusable, #focusable?, #getvalue_for_paint, #hide, #modified?, #move, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint_all, #repaint_required, #rowcol, #set_form, #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
Methods included from EventHandler
#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events
Constructor Details
#initialize(form, config = {}, &block) ⇒ Tree
Returns a new instance of Tree.
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/canis/core/widgets/tree.rb', line 173 def initialize form = nil, config={}, &block @_header_adjustment = 0 #1 @col_min_width = 3 @expanded_state = {} register_events(TREE_EVENTS) super #@_events.push(*[:ENTER_ROW, :LEAVE_ROW, :TREE_COLLAPSED_EVENT, :TREE_EXPANDED_EVENT, :TREE_SELECTION_EVENT, :TREE_WILL_COLLAPSE_EVENT, :TREE_WILL_EXPAND_EVENT]) create_default_renderer unless @renderer # 2014-04-10 - 11:01 init_vars #set_default_selection_model unless @list_selection_model end |
Instance Attribute Details
#current_index ⇒ Object
Returns the value of attribute current_index.
44 45 46 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 44 def current_index @current_index end |
#one_key_selection ⇒ Object
dsl_accessor :valign # popup related
will pressing a single key move to first matching row. setting it to false lets us use vim keys
57 58 59 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 57 def one_key_selection @one_key_selection end |
#selected_index ⇒ Object (readonly)
index of row selected, relates to internal representation, not tree. @see selected_row
59 60 61 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 59 def selected_index @selected_index end |
#toprow ⇒ Object (readonly)
dsl_accessor :title dsl_property :title_attrib # bold, reverse, normal dsl_accessor :border_attrib, :border_color # FIXME not used currently
40 41 42 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 40 def toprow @toprow end |
#treemodel ⇒ Object (readonly)
returns treemodel for further actions 2011-10-2
60 61 62 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 60 def treemodel @treemodel end |
Instance Method Details
#<<(array) ⇒ Object
add a row to the table The name add will be removed soon, pls use << instead.
387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/canis/core/widgets/tree.rb', line 387 def <<( array) unless @list # columns were not added, this most likely is the title @list ||= [] _init_model array end @list << array @native_text = @list # 2014-05-27 - 16:34 fire_dimension_changed self end |
#_list ⇒ Object
private, for use by repaint
288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/canis/core/widgets/tree.rb', line 288 def _list if @_structure_changed @list = nil @_structure_changed = false end unless @list $log.debug " XXX recreating _list" convert_to_list @treemodel #$log.debug " XXXX list: #{@list.size} : #{@list} " $log.debug " XXXX list: #{@list.size} " end return @list end |
#ask_search_backward ⇒ Object
gets string to search and calls data models find prev
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 365 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 |
#ask_search_forward ⇒ Object
355 356 357 358 359 360 361 362 363 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 355 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 |
#ask_selection_for_char ⇒ Object
get a keystroke from user and go to first item starting with that key
348 349 350 351 352 353 354 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 348 def ask_selection_for_char ch = @graphic.getch if ch < 0 || ch > 255 return :UNHANDLED end ret = set_selection_for_char ch.chr end |
#cell_renderer(*val) ⇒ Object
getter and setter for cell_renderer
409 410 411 412 413 414 415 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 409 def cell_renderer(*val) if val.empty? @cell_renderer ||= create_default_cell_renderer else @cell_renderer = val[0] end end |
#collapse_children(node = :current_index) ⇒ Object
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/canis/core/widgets/tree.rb', line 582 def collapse_children node=:current_index $multiplier = 999 if !$multiplier || $multiplier == 0 $log.debug " CCCC IINSIDE COLLLAPSE" node = row_to_node if node == :current_index return if node.children.empty? # or node.is_leaf? #node.children.each do |e| #expand_node e # this will keep expanding parents #expand_children e #end node.breadth_each($multiplier) do |e| $log.debug "CCC collapsing #{e.user_object} " collapse_node e end $multiplier = 0 _structure_changed true end |
#collapse_node(node) ⇒ Object
535 536 537 538 539 540 541 |
# File 'lib/canis/core/widgets/tree.rb', line 535 def collapse_node(node) $log.debug " collapse called on #{node.user_object} " state = false fire_handler :TREE_WILL_COLLAPSE_EVENT, node (node, state) fire_handler :TREE_COLLAPSED_EVENT, node end |
#collapse_parent(node = :current_index) ⇒ Object
collapse parent can use multiplier. # we need to move up also
601 602 603 604 605 606 607 |
# File 'lib/canis/core/widgets/tree.rb', line 601 def collapse_parent node=:current_index node = row_to_node if node == :current_index parent = node.parent return if parent.nil? goto_parent node collapse_node parent end |
#command(*args, &block) ⇒ Object
default block
693 694 695 |
# File 'lib/canis/core/widgets/tree.rb', line 693 def command *args, &block bind :TREE_WILL_EXPAND_EVENT, *args, &block end |
#convert_to_list(tree) ⇒ Object
311 312 313 314 315 |
# File 'lib/canis/core/widgets/tree.rb', line 311 def convert_to_list tree @list = @native_text = (tree.root) #$log.debug "XXX convert #{tree.root.children.size} " #$log.debug " converted tree to list. #{@list.size} " end |
#create_default_cell_renderer ⇒ Object
416 417 418 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 416 def create_default_cell_renderer return Canis::TreeCellRenderer.new "", {"color"=>@color, "bgcolor"=>@bgcolor, "parent" => self, "display_length"=> @width-@internal_width-@left_margin} end |
#create_default_renderer ⇒ Object
192 193 194 |
# File 'lib/canis/core/widgets/tree.rb', line 192 def create_default_renderer renderer DefaultTreeRenderer.new(self) end |
#current_row ⇒ Object Also known as: text
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
331 332 333 |
# File 'lib/canis/core/widgets/tree.rb', line 331 def current_row @list[@current_index] end |
#current_value ⇒ Object
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 commendte off on 2014-05-27 - 14:27 alias :text :current_row # thanks to shoes, not sure how this will impact since widget has text.
336 337 338 |
# File 'lib/canis/core/widgets/tree.rb', line 336 def current_row @list[@current_index] end |
#data(alist = nil) ⇒ Object
pass data to create this tree model used to be list
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/canis/core/widgets/tree.rb', line 252 def data alist=nil # if nothing passed, print an empty root, rather than crashing alist = [] if alist.nil? @data = alist # data given by user case alist when Array @treemodel = Canis::DefaultTreeModel.new("/") @treemodel.root.add alist when Hash @treemodel = Canis::DefaultTreeModel.new("/") @treemodel.root.add alist when TreeNode # this is a root node @treemodel = Canis::DefaultTreeModel.new(alist) when DefaultTreeModel @treemodel = alist else if alist.is_a? DefaultTreeModel @treemodel = alist else raise ArgumentError, "Tree does not know how to handle #{alist.class} " end end # we now have a tree raise "I still don't have a tree" unless @treemodel (@treemodel.root, true) convert_to_list @treemodel # added on 2009-01-13 23:19 since updates are not automatic now #@list.bind(:LIST_DATA_EVENT) { |e| list_data_changed() } #create_default_list_selection_model TODO fire_dimension_changed self end |
#delete_at(ix) ⇒ Object
delete a data row at index
NOTE : This does not adjust for header_adjustment. So zero will refer to the header if there is one.
This is to keep consistent with textpad which does not know of header_adjustment and uses the actual
index. Usually, programmers will be dealing with +@current_index+
405 406 407 408 409 410 411 412 413 |
# File 'lib/canis/core/widgets/tree.rb', line 405 def delete_at ix return unless @list raise ArgumentError, "Argument must be within 0 and #{@list.length}" if ix < 0 or ix >= @list.length fire_dimension_changed #@list.delete_at(ix + @_header_adjustment) _ret = @list.delete_at(ix) @native_text = @list # 2014-05-27 - 16:34 return _ret end |
#expand_children(node = :current_index) ⇒ Object
this expands all the children of a node, recursively we can’t use multiplier concept here since we are doing a preorder enumeration we need to do a breadth first enumeration to use a multiplier
568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/canis/core/widgets/tree.rb', line 568 def node=:current_index $multiplier = 999 if !$multiplier || $multiplier == 0 node = row_to_node if node == :current_index return if node.children.empty? # or node.is_leaf? #node.children.each do |e| #expand_node e # this will keep expanding parents #expand_children e #end node.breadth_each($multiplier) do |e| e end $multiplier = 0 _structure_changed true end |
#expand_node(node) ⇒ Object
528 529 530 531 532 533 534 |
# File 'lib/canis/core/widgets/tree.rb', line 528 def (node) #$log.debug " expand called on #{node.user_object} " state = true fire_handler :TREE_WILL_EXPAND_EVENT, node (node, state) fire_handler :TREE_EXPANDED_EVENT, node end |
#expand_parents(node) ⇒ Object
goes up to root of this node, and expands down to this node this is often required to make a specific node visible such as in a dir listing when current dir is deep in heirarchy.
556 557 558 559 560 561 562 563 |
# File 'lib/canis/core/widgets/tree.rb', line 556 def node _path = node.tree_path _path.each do |e| # if already expanded parent then break we should break #set_expanded_state(e, true) (e) end end |
#get_content ⇒ Object
START FOR scrollable ###
355 356 357 358 359 360 |
# File 'lib/canis/core/widgets/tree.rb', line 355 def get_content #@list 2008-12-01 23:13 #@list_variable && @list_variable.value || @list # called by next_match in listscrollable @list end |
#get_expanded_descendants(node) ⇒ Object
636 637 638 639 640 641 642 643 644 645 |
# File 'lib/canis/core/widgets/tree.rb', line 636 def (node) nodes = [] # 2014-07-04 - 11:55 trying out making the root invisible, we don't insert it into the list if @treemodel.root_visible nodes << node end node, nodes $log.debug " def get_expanded_descendants(node) #{nodes.size} " return nodes end |
#get_node_for_path(user_path) ⇒ Object
To retrieve the node corresponding to a path specified as an array or string Do not mention the root. e.g. “ruby/1.9.2/io/console” or %w[ ruby 1.9.3 io console ]
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/canis/core/widgets/tree.rb', line 665 def get_node_for_path(user_path) case user_path when String user_path = user_path.split "/" when Array else raise ArgumentError, "Should be Array or String delimited with /" end $log.debug "TREE #{user_path} " if $log.debug? root = @treemodel.root found = nil user_path.each do |e| success = false root.children.each do |c| if c.user_object == e found = c success = true root = c break end end return false unless success end return found end |
#get_window ⇒ Object
362 363 364 |
# File 'lib/canis/core/widgets/tree.rb', line 362 def get_window @graphic end |
#getvalue ⇒ Object
END FOR scrollable ### override widgets text
368 369 370 |
# File 'lib/canis/core/widgets/tree.rb', line 368 def getvalue selected_row() end |
#goto_parent(node = :current_index) ⇒ Object
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/canis/core/widgets/tree.rb', line 609 def goto_parent node=:current_index node = row_to_node if node == :current_index parent = node.parent return if parent.nil? crow = @current_index # TODO we have beautiful ruby stuff to do that! (find) @list.each_with_index do |e,i| if e == parent crow = i break end end @repaint_required = true #set_form_row # will not work if off form #set_focus_on crow goto_line crow end |
#handle_key(ch) ⇒ Object
Listbox
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 294 def handle_key(ch) return if @list.nil? || @list.empty? @current_index ||= 0 @toprow ||= 0 h = scrollatrow() rc = row_count $log.debug " tree got ch #{ch}" case ch when 27, ?\C-c.getbyte(0) #editing_canceled @current_index if @cell_editing_allowed #cancel_block # block $multiplier = 0 return 0 #when ?\C-u.getbyte(0) # multiplier. Series is 4 16 64 # TESTING @multiplier = (@multiplier == 0 ? 4 : @multiplier *= 4) # return 0 when ?\C-c.getbyte(0) @multiplier = 0 return 0 else ret = :UNHANDLED if ret == :UNHANDLED # beware one-key eats up numbers. we'll be wondering why if @one_key_selection case ch #when ?A.getbyte(0)..?Z.getbyte(0), ?a.getbyte(0)..?z.getbyte(0), ?0.getbyte(0)..?9.getbyte(0) when ?A.getbyte(0)..?Z.getbyte(0), ?a.getbyte(0)..?z.getbyte(0) # simple motion, key press defines motion ret = set_selection_for_char ch.chr else ret = process_key ch, self @multiplier = 0 return :UNHANDLED if ret == :UNHANDLED end else # no motion on single key, we can freak out like in vim, pref f <char> for set_selection case ch when ?0.getbyte(0)..?9.getbyte(0) $multiplier *= 10 ; $multiplier += (ch-48) #$log.debug " setting mult to #{$multiplier} in list " return 0 end $log.debug " TREE before process key #{ch} " ret = process_key ch, self $log.debug " TREE after process key #{ch} #{ret} " #$multiplier = 0 # 2010-09-02 22:35 this prevents parent from using mult return :UNHANDLED if ret == :UNHANDLED end end end $multiplier = 0 end |
#has_been_expanded(node) ⇒ Object
627 628 629 |
# File 'lib/canis/core/widgets/tree.rb', line 627 def node @expanded_state.has_key? node end |
#init_vars ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/canis/core/widgets/tree.rb', line 196 def init_vars # show_selector and symbol etc unused if @show_selector @row_selected_symbol ||= '>' @row_unselected_symbol ||= ' ' @left_margin ||= @row_selected_symbol.length end @left_margin ||= 0 #@one_key_selection = true if @one_key_selection.nil? @row_offset = @col_offset = 0 if @suppress_borders @internal_width = 2 # taking into account borders accounting for 2 cols @internal_width = 0 if @suppress_borders # should it be 0 ??? super end |
#is_row_selected?(row = @current_index) ⇒ Boolean
443 444 445 |
# File 'lib/canis/core/widgets/tree.rb', line 443 def is_row_selected? row=@current_index row == @selected_index end |
#list_data_changed ⇒ Object
503 504 505 506 507 508 509 510 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 503 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 @repaint_required = true end |
#map_keys ⇒ Object
maps keys to methods checks @key_map can be :emacs or :vim.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/canis/core/widgets/tree.rb', line 213 def map_keys super @keys_mapped = true bind_key($row_selector, 'toggle row selection'){ toggle_row_selection() } bind_key(KEY_ENTER, 'toggle expanded state') { () } bind_key(?o, 'toggle expanded state') { () } bind_key(?f, 'first row starting with char'){ set_selection_for_char() } #bind_key(?\M-v, 'one key selection toggle'){ @one_key_selection = !@one_key_selection } bind_key(?O, 'expand children'){ () } bind_key(?X, 'collapse children'){ collapse_children() } bind_key(?>, :scroll_right) bind_key(?<, :scroll_left) # TODO bind_key(?x, 'collapse parent'){ collapse_parent() } bind_key(?p, 'goto parent'){ goto_parent() } # # commented since textpad is now calling this # this can be brought back into include and used from other textpad too. #require 'canis/core/include/listbindings' #bindings end |
#mark_parents_expanded(node) ⇒ Object
this is required to make a node visible, if you wish to start from a node that is not root e.g. you are loading app in a dir somewhere but want to show path from root down. NOTE this sucks since you have to click 2 times to expand it.
545 546 547 548 549 550 551 552 |
# File 'lib/canis/core/widgets/tree.rb', line 545 def node # i am setting parents as expanded, but NOT firing handlers - XXX separate this into expand_parents _path = node.tree_path _path.each do |e| # if already expanded parent then break we should break (e, true) end end |
#node_collapsed?(node) ⇒ Boolean
633 634 635 |
# File 'lib/canis/core/widgets/tree.rb', line 633 def node_collapsed? node !(node) end |
#node_expanded?(node) ⇒ Boolean
630 631 632 |
# File 'lib/canis/core/widgets/tree.rb', line 630 def node @expanded_state[node] == true end |
#node_to_row(node) ⇒ Object
convert a given node to row
499 500 501 502 503 504 505 506 507 508 |
# File 'lib/canis/core/widgets/tree.rb', line 499 def node_to_row node crow = nil @list.each_with_index do |e,i| if e == node crow = i break end end crow end |
#OLDprint_borders ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 255 def OLDprint_borders width = @width height = @height-1 # 2010-01-04 15:30 BUFFERED HEIGHT window = @graphic # 2010-01-04 12:37 BUFFERED startcol = @col startrow = @row @color_pair = get_color($datacolor) # bordercolor = @border_color || $datacolor # changed 2011 dts bordercolor = @border_color || @color_pair # 2011-09-28 V1.3.1 borderatt = @border_attrib || Ncurses::A_NORMAL window.print_border startrow, startcol, height, width, bordercolor, borderatt print_title end |
#OLDprint_title ⇒ Object
269 270 271 272 273 274 275 276 277 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 269 def OLDprint_title return unless @title _title = @title if @title.length > @width - 2 _title = @title[0..@width-2] end @color_pair ||= get_color($datacolor) @graphic.printstring( @row, @col+(@width-_title.length)/2, _title, @color_pair, @title_attrib) unless @title.nil? end |
#on_enter ⇒ Boolean
please check for error before proceeding
377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 377 def on_enter if @list.size < 1 Ncurses.beep return false end on_enter_row @current_index set_form_row # added 2009-01-11 23:41 #$log.debug " ONE ENTER LIST #{@current_index}, #{@form.row}" @repaint_required = true super #fire_handler :ENTER, self true end |
#on_enter_row(arow) ⇒ Object
390 391 392 393 394 395 396 397 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 390 def on_enter_row arow #$log.debug " Listbox #{self} ENTER_ROW with curr #{@current_index}. row: #{arow} H: #{@handler.keys}" #fire_handler :ENTER_ROW, arow fire_handler :ENTER_ROW, self #@list.on_enter_row self TODO #edit_row_at arow @repaint_required = true end |
#on_leave_row(arow) ⇒ Object
399 400 401 402 403 404 405 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 399 def on_leave_row arow #$log.debug " Listbox #{self} leave with (cr: #{@current_index}) #{arow}: list[row]:#{@list[arow]}" #$log.debug " Listbox #{self} leave with (cr: #{@current_index}) #{arow}: " #fire_handler :LEAVE_ROW, arow fire_handler :LEAVE_ROW, self #editing_completed arow end |
#print_foot ⇒ Object
print footer containing line and total, overriding textpad which prints column offset also This is called internally by repaint() but can be overridden for more complex printing.
435 436 437 438 439 440 441 442 |
# File 'lib/canis/core/widgets/tree.rb', line 435 def print_foot return unless @print_footer ha = @_header_adjustment # ha takes into account whether there are headers or not = "#{@current_index+1-ha} of #{@list.length-ha} " @graphic.printstring(@row + @height -1 , @col+2, , @color_pair || $datacolor, @footer_attrib) @repaint_footer_required = false end |
#renderer(*val) ⇒ Object
supply a custom renderer that implements render()
374 375 376 377 378 379 |
# File 'lib/canis/core/widgets/tree.rb', line 374 def renderer *val if val.empty? return @renderer end @renderer = val[0] end |
#repaint ⇒ Object
this method chops the data to length before giving it to the renderer, this can cause problems if the renderer does some processing. also, it pans the data horizontally giving the renderer a section of it. FIXME: tree may not be clearing till end see appdirtree after divider movement
305 306 307 308 309 |
# File 'lib/canis/core/widgets/tree.rb', line 305 def repaint # we need to see if structure changed then regenerate @list _list() super end |
#root(node = nil, asks_allow_children = false, &block) ⇒ Object
Sets the given node as root and returns treemodel. Returns root if no argument given. Now we return root if already set Made node nillable so we can return root.
240 241 242 243 244 245 246 247 248 |
# File 'lib/canis/core/widgets/tree.rb', line 240 def root node=nil, asks_allow_children=false, &block if @treemodel return @treemodel.root unless node raise ArgumentError, "Root already set" end raise ArgumentError, "root: node cannot be nil" unless node @treemodel = Canis::DefaultTreeModel.new(node, asks_allow_children, &block) end |
#row_collapsed?(row) ⇒ Boolean
520 521 522 |
# File 'lib/canis/core/widgets/tree.rb', line 520 def row_collapsed? row ! row end |
#row_count ⇒ Object
139 140 141 142 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 139 def row_count return 0 if @list.nil? @list.length end |
#row_expanded?(row) ⇒ TreeNode?
Returns selected node or nil
516 517 518 519 |
# File 'lib/canis/core/widgets/tree.rb', line 516 def row node = @list[row] node end |
#row_selected?(row) ⇒ Boolean
private related to index in representation, not tree
511 512 513 |
# File 'lib/canis/core/widgets/tree.rb', line 511 def row_selected? row @selected_index == row end |
#row_to_node(row = @current_index) ⇒ Object
494 495 496 |
# File 'lib/canis/core/widgets/tree.rb', line 494 def row_to_node row=@current_index @list[row] end |
#scrollatrow ⇒ Object
at what row should scrolling begin
144 145 146 147 148 149 150 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 144 def scrollatrow if @suppress_borders return @height - 1 else return @height - 3 end end |
#select_default_values ⇒ Object
show default value as selected and fire handler for it This is called in repaint, so can raise an error if called on creation or before repaint. Just set @default_value, and let us handle the rest. Suggestions are welcome.
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/canis/core/widgets/tree.rb', line 342 def select_default_values return if @default_value.nil? # NOTE list not yet created raise "list has not yet been created" unless @list index = node_to_row @default_value raise "could not find node #{@default_value}, #{@list} " unless index return unless index @current_index = index toggle_row_selection @default_value = nil end |
#selected_row ⇒ Object
446 447 448 |
# File 'lib/canis/core/widgets/tree.rb', line 446 def selected_row @list[@selected_index].node end |
#set_default_selection_model ⇒ Object
set the default selection model as the operational one
187 188 189 190 |
# File 'lib/canis/core/widgets/tree.rb', line 187 def set_default_selection_model @list_selection_model = nil @list_selection_model = Canis::DefaultListSelectionModel.new self end |
#set_expanded_state(node, state) ⇒ Object
523 524 525 526 527 |
# File 'lib/canis/core/widgets/tree.rb', line 523 def (node, state) @expanded_state[node] = state @repaint_required = true _structure_changed true end |
#set_form_col(col1 = 0) ⇒ Object
511 512 513 514 515 516 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 511 def set_form_col col1=0 @cols_panned ||= 0 # RFED16 2010-02-17 23:40 win_col = 0 col2 = win_col + @col + @col_offset + col1 + @cols_panned + @left_margin setrowcol nil, col2 # 2010-02-17 23:19 RFED16 end |
#toggle_expanded_state(row = @current_index) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/canis/core/widgets/tree.rb', line 478 def row=@current_index state = row node = row_to_node if node.nil? Ncurses.beep $log.debug " No such node on row #{row} " return end $log.debug " toggle XXX state #{state} #{node} " if state collapse_node node else node end end |
#toggle_row_selection ⇒ Object
An event is thrown when a row is selected or deselected. Please note that when a row is selected, another one is automatically deselected. An event is not thrown for that since your may not want to collapse that. Only clicking on a selected row, will send a DESELECT on it since you may want to collapse it. However, the previous selection is also present in the event object, so you can act upon it. This is not used for expanding or collapsing, only for application to show some data in another window or pane based on selection. Maybe there should not be a deselect for current row ?
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/canis/core/widgets/tree.rb', line 457 def toggle_row_selection node = @list[@current_index] previous_node = nil previous_node = @list[@selected_index] if @selected_index previous_index = nil if @selected_index == @current_index @selected_index = nil previous_index = @current_index else previous_index = @selected_index @selected_index = @current_index end state = @selected_index.nil? ? :DESELECTED : :SELECTED #TreeSelectionEvent = Struct.new(:node, :tree, :state, :previous_node, :row_first) @tree_selection_event = TreeSelectionEvent.new(node, self, state, previous_node, @current_index) #if @item_event.nil? fire_handler :TREE_SELECTION_EVENT, @tree_selection_event # should the event itself be ITEM_EVENT $log.debug " XXX tree selected #{@selected_index}/ #{@current_index} , #{state} " fire_row_changed @current_index if @current_index fire_row_changed previous_index if previous_index @repaint_required = true end |
#traverse(node, level = 0) {|node, level| ... } ⇒ Object
$log.debug “XXX convert #Canis::Tree.treetree.roottree.root.childrentree.root.children.size ” $log.debug “ converted tree to list. #Canis::Tree.@[email protected] ”
222 223 224 225 226 227 228 229 230 |
# File 'lib/canis/core/widgets/deprecated/rtree.rb', line 222 def traverse node, level=0, &block raise "disuse" #icon = node.is_leaf? ? "-" : "+" #puts "%*s %s" % [ level+1, icon, node.user_object ] yield node, level if block_given? node.children.each do |e| traverse e, level+1, &block end end |
#traverse_expanded(node, nodes) ⇒ Object
646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/canis/core/widgets/tree.rb', line 646 def node, nodes return if ! node #nodes << node node.children.each do |e| nodes << e if e e, nodes else next end end end |
#XXXpadrefresh ⇒ Object
refresh pad onto window overrides super due to header_adjustment and the header too
418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/canis/core/widgets/tree.rb', line 418 def XXXpadrefresh top = @window.top left = @window.left sr = @startrow + top sc = @startcol + left # first do header always in first row retval = FFI::NCurses.prefresh(@pad,0,@pcol, sr , sc , 2 , @cols+ sc ); # now print rest of data # h is header_adjustment h = 1 retval = FFI::NCurses.prefresh(@pad,@prow + h,@pcol, sr + h , sc , @rows + sr , @cols+ sc ); $log.warn "XXX: PADREFRESH #{retval}, #{@prow}, #{@pcol}, #{sr}, #{sc}, #{@rows+sr}, #{@cols+sc}." if retval == -1 # padrefresh can fail if width is greater than NCurses.COLS end |