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.
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.
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/canis/core/widgets/tree.rb', line 167 def initialize form = nil, config={}, &block @_header_adjustment = 0 #1 @col_min_width = 3 @expanded_state = {} register_events([:ENTER_ROW, :LEAVE_ROW, :TREE_COLLAPSED_EVENT, :TREE_EXPANDED_EVENT, :TREE_SELECTION_EVENT, :TREE_WILL_COLLAPSE_EVENT, :TREE_WILL_EXPAND_EVENT]) 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
164 165 166 |
# File 'lib/canis/core/widgets/tree.rb', line 164 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.
374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/canis/core/widgets/tree.rb', line 374 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
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/canis/core/widgets/tree.rb', line 280 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
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/canis/core/widgets/tree.rb', line 567 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
520 521 522 523 524 525 526 |
# File 'lib/canis/core/widgets/tree.rb', line 520 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
586 587 588 589 590 591 592 |
# File 'lib/canis/core/widgets/tree.rb', line 586 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
676 677 678 |
# File 'lib/canis/core/widgets/tree.rb', line 676 def command *args, &block bind :TREE_WILL_EXPAND_EVENT, *args, &block end |
#convert_to_list(tree) ⇒ Object
301 302 303 304 305 |
# File 'lib/canis/core/widgets/tree.rb', line 301 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
186 187 188 |
# File 'lib/canis/core/widgets/tree.rb', line 186 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
319 320 321 |
# File 'lib/canis/core/widgets/tree.rb', line 319 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.
324 325 326 |
# File 'lib/canis/core/widgets/tree.rb', line 324 def current_row @list[@current_index] end |
#data(alist = nil) ⇒ Object
pass data to create this tree model used to be list
244 245 246 247 248 249 250 251 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 |
# File 'lib/canis/core/widgets/tree.rb', line 244 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+
392 393 394 395 396 397 398 399 400 |
# File 'lib/canis/core/widgets/tree.rb', line 392 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
553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/canis/core/widgets/tree.rb', line 553 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
513 514 515 516 517 518 519 |
# File 'lib/canis/core/widgets/tree.rb', line 513 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.
541 542 543 544 545 546 547 548 |
# File 'lib/canis/core/widgets/tree.rb', line 541 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 ###
342 343 344 345 346 347 |
# File 'lib/canis/core/widgets/tree.rb', line 342 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
619 620 621 622 623 624 625 626 627 628 |
# File 'lib/canis/core/widgets/tree.rb', line 619 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 ]
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'lib/canis/core/widgets/tree.rb', line 648 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 { |e| success = false root.children.each { |c| if c.user_object == e found = c success = true root = c break end } return false unless success } return found end |
#get_window ⇒ Object
348 349 350 |
# File 'lib/canis/core/widgets/tree.rb', line 348 def get_window @graphic end |
#getvalue ⇒ Object
END FOR scrollable ### override widgets text
353 354 355 |
# File 'lib/canis/core/widgets/tree.rb', line 353 def getvalue selected_row() end |
#goto_parent(node = :current_index) ⇒ Object
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/canis/core/widgets/tree.rb', line 593 def goto_parent node=:current_index node = row_to_node if node == :current_index parent = node.parent return if parent.nil? crow = @current_index @list.each_with_index { |e,i| if e == parent crow = i break 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
610 611 612 |
# File 'lib/canis/core/widgets/tree.rb', line 610 def node @expanded_state.has_key? node end |
#init_vars ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/canis/core/widgets/tree.rb', line 189 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
430 431 432 |
# File 'lib/canis/core/widgets/tree.rb', line 430 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.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/canis/core/widgets/tree.rb', line 206 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.
530 531 532 533 534 535 536 537 |
# File 'lib/canis/core/widgets/tree.rb', line 530 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
616 617 618 |
# File 'lib/canis/core/widgets/tree.rb', line 616 def node_collapsed? node !(node) end |
#node_expanded?(node) ⇒ Boolean
613 614 615 |
# File 'lib/canis/core/widgets/tree.rb', line 613 def node @expanded_state[node] == true end |
#node_to_row(node) ⇒ Object
convert a given node to row
484 485 486 487 488 489 490 491 492 493 |
# File 'lib/canis/core/widgets/tree.rb', line 484 def node_to_row node crow = nil @list.each_with_index { |e,i| if e == node crow = i break 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.
422 423 424 425 426 427 428 429 |
# File 'lib/canis/core/widgets/tree.rb', line 422 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()
359 360 361 362 363 364 |
# File 'lib/canis/core/widgets/tree.rb', line 359 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
296 297 298 299 300 |
# File 'lib/canis/core/widgets/tree.rb', line 296 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.
232 233 234 235 236 237 238 239 240 |
# File 'lib/canis/core/widgets/tree.rb', line 232 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
505 506 507 |
# File 'lib/canis/core/widgets/tree.rb', line 505 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
501 502 503 504 |
# File 'lib/canis/core/widgets/tree.rb', line 501 def row node = @list[row] node end |
#row_selected?(row) ⇒ Boolean
private related to index in representation, not tree
496 497 498 |
# File 'lib/canis/core/widgets/tree.rb', line 496 def row_selected? row @selected_index == row end |
#row_to_node(row = @current_index) ⇒ Object
480 481 482 |
# File 'lib/canis/core/widgets/tree.rb', line 480 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.
330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/canis/core/widgets/tree.rb', line 330 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
433 434 435 |
# File 'lib/canis/core/widgets/tree.rb', line 433 def selected_row @list[@selected_index].node end |
#set_default_selection_model ⇒ Object
set the default selection model as the operational one
182 183 184 185 |
# File 'lib/canis/core/widgets/tree.rb', line 182 def set_default_selection_model @list_selection_model = nil @list_selection_model = Canis::DefaultListSelectionModel.new self end |
#set_expanded_state(node, state) ⇒ Object
508 509 510 511 512 |
# File 'lib/canis/core/widgets/tree.rb', line 508 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
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/canis/core/widgets/tree.rb', line 465 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 ?
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/canis/core/widgets/tree.rb', line 444 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] ”
306 307 308 309 310 311 312 313 314 |
# File 'lib/canis/core/widgets/tree.rb', line 306 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
629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/canis/core/widgets/tree.rb', line 629 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
405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/canis/core/widgets/tree.rb', line 405 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 |