Class: Iup::Tree

Inherits:
Widget show all
Includes:
ButtonCallback, DragDropAttributes
Defined in:
lib/wrapped/tree.rb

Overview

A tree displays a hierarchy of branch and leaf nodes. Each node may display some text and an optional image.

Example

tree = Iup::Tree.new

dlg = Iup::Dialog.new(tree) do |d|
  d.title = 'Tree Example'
end.map

tree.size = '80x80'
tree.font = 'Courier, Normal 10'
tree.addbranch(0, '3D')
tree.addbranch(0, '2D')
tree.addleaf(1, 'trapeze')
tree.addbranch(1, 'parallelogram')
tree.addleaf(2, 'diamond')
tree.addleaf(2, 'square')
tree.addbranch(4, 'triangle')
tree.addleaf(5, 'scalenus')
tree.addleaf(5, 'isoceles')
tree.addleaf(5, 'equilateral')
tree.value = 6
tree.addexpanded = 'no'

dlg.show

Note: the contents of the tree can only be created after its containing dialog has been mapped.

Instance Attribute Summary

Attributes inherited from Widget

#handle

Instance Method Summary collapse

Methods included from ButtonCallback

#button_cb=

Methods included from DragDropAttributes

#dragbegin_cb=, #dragdata_cb=, #dragdatasize_cb=, #dragend_cb=, #dragsource, #dragsourcemove, #dragtypes, #dropdata_cb=, #dropmotion_cb=, #droptarget, #droptypes

Methods included from AttributeBuilders

#define_attribute, #define_id_attribute, #define_id_reader, #define_id_writer, #define_property_attribute, #define_property_reader, #define_property_writer, #define_reader, #define_writer

Methods inherited from Widget

#active, #assign_handle, #bgcolor, #destroy, #enterwindow_cb=, #fgcolor, #font, #getfocus_cb=, #help_cb=, #k_any=, #killfocus_cb=, #leavewindow_cb=, #map_cb=, #maxsize, #minsize, #open_controls, #size, #unmap_cb=, #visible, #wid, #zorder

Methods included from CallbackSetter

#define_callback

Constructor Details

#initializeTree

Creates an instance of Tree. Unusually, does not accept an initialization block, as tree must be placed and mapped within a dialog before setup can take place - see Example.



41
42
43
# File 'lib/wrapped/tree.rb', line 41

def initialize
  @handle = IupLib.IupTree
end

Instance Method Details

#addbranch(id, val) ⇒ Object

Adds new branch after specified node.

  • id - identifier of a node

  • val - text label



407
408
409
# File 'lib/wrapped/tree.rb', line 407

def addbranch id, val
  IupLib.IupSetAttribute @handle, "ADDBRANCH#{id}", val.to_s
end

#addexpandedObject

:attr: addexpanded ‘yes’ / ‘no’, to expand branches when created.



52
# File 'lib/wrapped/tree.rb', line 52

define_attribute :addexpanded

#addleaf(id, val) ⇒ Object

Adds new leaf after specified node.

  • id - identifier of a node

  • val - text label



414
415
416
# File 'lib/wrapped/tree.rb', line 414

def addleaf id, val
  IupLib.IupSetAttribute @handle, "ADDLEAF#{id}", val.to_s
end

#addrootObject

:attr: addroot ‘yes’ / ‘no’, automatically adds an empty branch as the first node, on creation.



58
# File 'lib/wrapped/tree.rb', line 58

define_attribute :addroot

#branchclose_cb=(callback) ⇒ Object



506
507
508
509
510
511
512
513
514
# File 'lib/wrapped/tree.rb', line 506

def branchclose_cb= callback
  unless callback.arity == 1
    raise ArgumentError, 'branchclose_cb callback must take 1 argument: node_id'
  end
  cb = Proc.new do |ih, id|
    callback.call id
  end
  define_callback cb, 'BRANCHCLOSE_CB', :i_i
end

#branchopen_cb=(callback) ⇒ Object



489
490
491
492
493
494
495
496
497
# File 'lib/wrapped/tree.rb', line 489

def branchopen_cb= callback
  unless callback.arity == 1
    raise ArgumentError, 'branchopen_cb callback must take 1 argument: node_id'
  end
  cb = Proc.new do |ih, id|
    callback.call id
  end
  define_callback cb, 'BRANCHOPEN_CB', :i_i
end

#canfocusObject

:attr: canfocus Enables the control to gain focus. Values ‘yes’ / ‘no’.



63
# File 'lib/wrapped/tree.rb', line 63

define_attribute :canfocus

#childcount(id) ⇒ Object

Returns the number of immediate child of given node.

  • id - identifier of a node



147
148
149
# File 'lib/wrapped/tree.rb', line 147

def childcount id
  IupLib.IupGetAttribute(@handle, "CHILDCOUNT#{id}").first
end

#color(id, val = nil) ⇒ Object

:call-seq:

tree.color(id) # retrieves
tree.color(id, val) # sets

Accesses text foreground color:

  • id - identifier of a node

  • val - “r g b” color



158
159
160
161
162
163
164
# File 'lib/wrapped/tree.rb', line 158

def color id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "COLOR#{id}").first
  else
    IupLib.IupSetAttribute @handle, "COLOR#{id}", val
  end
end

#copynode(source_id, target_id) ⇒ Object

Copies source node and its children to target.

  • source_id - identifier of a node

  • target_id - identifier of a node



421
422
423
# File 'lib/wrapped/tree.rb', line 421

def copynode source_id, target_id
  IupLib.IupSetAttribute @handle, "COPYNODE#{id}", target_id.to_s
end

#countObject

:attr_reader: count Returns total number of nodes in the tree.



68
# File 'lib/wrapped/tree.rb', line 68

define_reader :count

#delnode(id, val) ⇒ Object

Delete one or more nodes:

  • id - identifier of a node

  • val - one of

    • ‘all’: ignores id and deletes all nodes in tree, including root.

    • ‘selected’: deletes selected node and its children.

    • ‘children’: deleted only the children of selected node.

    • ‘marked’: ignores id and deletes all selected node.



432
433
434
# File 'lib/wrapped/tree.rb', line 432

def delnode id, val
  IupLib.IupSetAttribute @handle, "DELNODE#{id}", val.to_s
end

#depth(id) ⇒ Object

Returns the depth of the given node.

  • id - identifier of a node



168
169
170
# File 'lib/wrapped/tree.rb', line 168

def depth id
  IupLib.IupGetAttribute(@handle, "DEPTH#{id}").first
end

#dragdrop_cb=(callback) ⇒ Object



583
584
585
586
587
588
589
590
591
# File 'lib/wrapped/tree.rb', line 583

def dragdrop_cb= callback
  unless callback.arity == 4
    raise ArgumentError, 'dragdrop_cb callback must take 4 arguments: (drag_id, drop_id, isshift, iscontrol)'
  end
  cb = Proc.new do |ih, drag_id, drop_id, isshift, iscontrol|
    callback.call drag_id.to_i, drop_id.to_i, (isshift != 0), (iscontrol != 0)
  end
  define_callback cb, 'DRAGDROP_CB', :iiii_i
end

#dragdroptreeObject

:attr: dragdroptree Determines if tree supports dragdrop operations. Values ‘yes’ / ‘no’.



73
# File 'lib/wrapped/tree.rb', line 73

define_attribute :dragdroptree

#dropequaldragObject

:attr: dropequaldrag ‘yes’ / ‘no’, if set, allows a drop node to equal drag node.



78
# File 'lib/wrapped/tree.rb', line 78

define_attribute :dropequaldrag

#executeleaf_cb=(callback) ⇒ Object



523
524
525
526
527
528
529
530
531
# File 'lib/wrapped/tree.rb', line 523

def executeleaf_cb= callback
  unless callback.arity == 1
    raise ArgumentError, 'executeleaf_cb callback must take 1 argument: node_id'
  end
  cb = Proc.new do |ih, id|
    callback.call id
  end
  define_callback cb, 'EXECUTELEAF_CB', :i_i
end

#expandObject

:attr: expand Allows control to fill available space in indicated direction. Values ‘no’ / ‘horizontal’ / ‘vertical’ / ‘yes’.



84
# File 'lib/wrapped/tree.rb', line 84

define_attribute :expand

#expandallObject

:attr: expandall Expands or contracts all nodes, values ‘yes’ / ‘no’.



439
# File 'lib/wrapped/tree.rb', line 439

define_attribute :expandall

#hidebuttonsObject

:attr: hidebuttons ‘yes’ / ‘no’, to hide expand and create buttons.



89
# File 'lib/wrapped/tree.rb', line 89

define_attribute :hidebuttons

#hidelinesObject

:attr: hidelines ‘yes’ / ‘no’, the lines connecting nodes in hierarchy.



94
# File 'lib/wrapped/tree.rb', line 94

define_attribute :hidelines

#image(id, img) ⇒ Object

Sets image to use for given node.

  • id - identifier of a node

  • img - image name or reference. This can use an actual image object, or the name of an image from IupImageLib.



285
286
287
# File 'lib/wrapped/tree.rb', line 285

def image id, img
  attribute_reference "IMAGE#{id}", ImageWidget, img
end

#imagebranchcollapsedObject



318
319
320
# File 'lib/wrapped/tree.rb', line 318

def imagebranchcollapsed 
  attribute_reference("IMAGEBRANCHCOLLAPSED", ImageWidget, nil)
end

#imagebranchcollapsed=(img) ⇒ Object

:nodoc:



322
323
324
# File 'lib/wrapped/tree.rb', line 322

def imagebranchcollapsed= img # :nodoc:
  attribute_reference("IMAGEBRANCHCOLLAPSED", ImageWidget, img)
end

#imagebranchexpandedObject



333
334
335
# File 'lib/wrapped/tree.rb', line 333

def imagebranchexpanded 
  attribute_reference("IMAGEBRANCHEXPANDED", ImageWidget, nil)
end

#imagebranchexpanded=(img) ⇒ Object

:nodoc:



337
338
339
# File 'lib/wrapped/tree.rb', line 337

def imagebranchexpanded= img # :nodoc:
  attribute_reference("IMAGEBRANCHEXPANDED", ImageWidget, img)
end

#imageexpanded(id, img) ⇒ Object

Sets image to use for given node, for expanded branches.

  • id - identifier of a node

  • img - image name or reference. This can use an actual image object, or the name of an image from IupImageLib.



292
293
294
# File 'lib/wrapped/tree.rb', line 292

def imageexpanded id, img
  attribute_reference "IMAGEEXPANDED#{id}", ImageWidget, img
end

#imageleafObject



303
304
305
# File 'lib/wrapped/tree.rb', line 303

def imageleaf
  attribute_reference("IMAGELEAF", ImageWidget, nil)
end

#imageleaf=(img) ⇒ Object

:nodoc:



307
308
309
# File 'lib/wrapped/tree.rb', line 307

def imageleaf= img # :nodoc:
  attribute_reference("IMAGELEAF", ImageWidget, img)
end

#indentationObject

:attr: indentation level of indentation in pixels, defaults to 5.



99
# File 'lib/wrapped/tree.rb', line 99

define_attribute :indentation

#insertbranch(id, val) ⇒ Object

Inserts new branch after specified node, preserving depth.

  • id - identifier of a node

  • val - text label



444
445
446
# File 'lib/wrapped/tree.rb', line 444

def insertbranch id, val
  IupLib.IupSetAttribute @handle, "INSERTBRANCH#{id}", val.to_s
end

#insertleaf(id, val) ⇒ Object

Inserts new leaf after specified node, preserving depth.

  • id - identifier of a node

  • val - text label



451
452
453
# File 'lib/wrapped/tree.rb', line 451

def insertleaf id, val
  IupLib.IupSetAttribute @handle, "INSERTLEAF#{id}", val.to_s
end

#kind(id) ⇒ Object

Returns kind of given node, as ‘leaf’ or ‘branch’.

  • id - identifier of a node



174
175
176
# File 'lib/wrapped/tree.rb', line 174

def kind id
  IupLib.IupGetAttribute(@handle, "KIND#{id}").first
end

#markObject

:attr_writer: mark selects a range of nodes: ‘start-end’ / ‘INVERTid’ / ‘block’ / ‘clearall’ / ‘markall’ / ‘invertall’



347
# File 'lib/wrapped/tree.rb', line 347

define_writer :mark

#marked(id, val = nil) ⇒ Object

:call-seq:

tree.marked(id)  # retrieves
tree.marked(id, val) # sets

Accesses marked state of node:

  • id - identity of node

  • val - ‘yes’ / ‘no’



356
357
358
359
360
361
362
# File 'lib/wrapped/tree.rb', line 356

def marked id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "MARKED#{id}").first
  else
    IupLib.IupSetAttribute @handle, "MARKED#{id}", val
  end
end

#markednodesObject



369
370
371
372
373
374
375
376
377
378
# File 'lib/wrapped/tree.rb', line 369

def markednodes 
  mns = IupLib.IupGetAttribute(@handle, 'MARKEDNODES').first
  result = []

  mns.split('').each_with_index do |item, index|
    result << index+1 if item == '+'
  end

  return result
end

#markednodes=(val) ⇒ Object

:nodoc:



380
381
382
383
384
385
386
387
388
389
390
# File 'lib/wrapped/tree.rb', line 380

def markednodes= val # :nodoc:
  result = ""
  count.to_i.times do |i|
    if val.include?(i+1)
      result << '+'
    else
      result << '-'
    end
  end
  IupLib.IupSetAttribute @handle, 'MARKEDNODES', result
end

#markmodeObject

:attr: markmode ‘single’ / ‘multiple’, for selection of nodes.



395
# File 'lib/wrapped/tree.rb', line 395

define_attribute :markmode

#markstartObject

:attr: markstart Initial node for block marking, used when mark=block.



400
# File 'lib/wrapped/tree.rb', line 400

define_attribute :markstart

#motion_cb=(callback) ⇒ Object



602
603
604
605
606
607
608
609
610
# File 'lib/wrapped/tree.rb', line 602

def motion_cb= callback
  unless callback.arity == 3
    raise ArgumentError, 'motion_cb callback must take 3 arguments: (x, y, state)'
  end
  cb = Proc.new do |ih, x, y, state|
    callback.call x, y, state
  end
  define_callback cb, 'MOTION_CB', :iis_i
end

#movenode(source_id, target_id) ⇒ Object

Moves source to target, as a new child or sibling.

  • source_id - identifier of a node

  • target_id - identifier of a node



458
459
460
# File 'lib/wrapped/tree.rb', line 458

def movenode source_id, target_id
  IupLib.IupSetAttribute @handle, "MOVENODE#{id}", target_id.to_s
end

#parent(id) ⇒ Object

Returns identifier of parent of given node.

  • id - identifier of a node



180
181
182
# File 'lib/wrapped/tree.rb', line 180

def parent id
  IupLib.IupGetAttribute(@handle, "PARENT#{id}").first
end

#positionObject

:attr_reader: position returns position in pixels within client window as “x,y”.



104
# File 'lib/wrapped/tree.rb', line 104

define_reader :position

#rastersizeObject

:attr: rastersize Size of the control, in pixels, value as “widthxheight”.



109
# File 'lib/wrapped/tree.rb', line 109

define_attribute :rastersize

#rightclick_cb=(callback) ⇒ Object



540
541
542
543
544
545
546
547
548
# File 'lib/wrapped/tree.rb', line 540

def rightclick_cb= callback
  unless callback.arity == 1
    raise ArgumentError, 'rightclick_cb callback must take 1 argument: node_id'
  end
  cb = Proc.new do |ih, id|
    callback.call id
  end
  define_callback cb, 'RIGHTCLICK_CB', :i_i
end

#screenpositionObject

:attr_reader: screenposition returns position in pixels on screen as “x,y”.



114
# File 'lib/wrapped/tree.rb', line 114

define_reader :screenposition

#selection_cb=(callback) ⇒ Object



472
473
474
475
476
477
478
479
480
# File 'lib/wrapped/tree.rb', line 472

def selection_cb= callback
  unless callback.arity == 2
    raise ArgumentError, 'selection_cb callback must take 2 arguments: node_id, status'
  end
  cb = Proc.new do |ih, id, status|
    callback.call id, status
  end
  define_callback cb, 'SELECTION_CB', :ii_i
end

#showdragdropObject

:attr: showdragdrop ‘yes’ / ‘no’, enables internal drag and drop of nodes.



119
# File 'lib/wrapped/tree.rb', line 119

define_attribute :showdragdrop

#showtoggleObject

:attr: showtoggle ‘yes’ / ‘no’ / ‘3state’, enables use of toggles for all nodes of the tree.



124
# File 'lib/wrapped/tree.rb', line 124

define_attribute :showtoggle

#spacingObject

:attr: spacing vertical, internal padding for each node, defaults to 3 pixels.



129
# File 'lib/wrapped/tree.rb', line 129

define_attribute :spacing

#state(id, val = nil) ⇒ Object

:call-seq:

tree.state(id) # retrieves
tree.state(id, val) # sets

Accesses state as ‘collapsed’ or ‘expanded’:

  • id - identifier of a node

  • val - value ‘collapsed’ or ‘expanded’.



191
192
193
194
195
196
197
# File 'lib/wrapped/tree.rb', line 191

def state id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "STATE#{id}").first
  else
    IupLib.IupSetAttribute @handle, "STATE#{id}", val
  end
end

#title(id, val = nil) ⇒ Object

:call-seq:

tree.title(id) # retrieves
tree.title(id, val) # sets

Accesses title of given node:

  • id - identifier of a node

  • val - title.



206
207
208
209
210
211
212
# File 'lib/wrapped/tree.rb', line 206

def title id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "TITLE#{id}").first
  else
    IupLib.IupSetAttribute @handle, "TITLE#{id}", val
  end
end

#titlefont(id, val = nil) ⇒ Object

:call-seq:

tree.titlefont(id) # retrieves
tree.titlefont(id, val) # sets

Accesses font of node title:

  • id - identifier of a node

  • val - font description.



221
222
223
224
225
226
227
# File 'lib/wrapped/tree.rb', line 221

def titlefont id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "TITLEFONT#{id}").first
  else
    IupLib.IupSetAttribute @handle, "TITLEFONT#{id}", val
  end
end

#togglevalue(id, val = nil) ⇒ Object

:call-seq:

tree.togglevalue(id) # retrieves
tree.togglevalue(id, val) # sets

Accesses toggle state of given node:

  • id - identifier of a node

  • val - state as ‘on’ / ‘off’ / ‘notdef’



236
237
238
239
240
241
242
# File 'lib/wrapped/tree.rb', line 236

def togglevalue id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "TOGGLEVALUE#{id}").first
  else
    IupLib.IupSetAttribute @handle, "TOGGLEVALUE#{id}", val
  end
end

#togglevalue_cb=(callback) ⇒ Object



558
559
560
561
562
563
564
565
566
# File 'lib/wrapped/tree.rb', line 558

def togglevalue_cb= callback
  unless callback.arity == 2
    raise ArgumentError, 'togglevalue_cb callback must take 2 arguments: node_id, status'
  end
  cb = Proc.new do |ih, id, status|
    callback.call id, status
  end
  define_callback cb, 'TOGGLEVALUE_CB', :ii_i
end

#togglevisible(id, val = nil) ⇒ Object

:call-seq:

tree.togglevisible(id) # retrieves
tree.togglevisible(id, val) # sets

Accesses toggle visibility of given node:

  • id - identifier of a node

  • val - visibility as ‘yes’ / ‘no’.



251
252
253
254
255
256
257
# File 'lib/wrapped/tree.rb', line 251

def togglevisible id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "TOGGLEVISIBLE#{id}").first
  else
    IupLib.IupSetAttribute @handle, "TOGGLEVISIBLE#{id}", val
  end
end

#topitemObject

:attr_writer: topitem Positions given node id at the top of tree, or near, to make it visible.



134
# File 'lib/wrapped/tree.rb', line 134

define_writer :topitem

#totalchildcount(id) ⇒ Object

Returns total number of children for given node.

  • id - identifier of a node



261
262
263
# File 'lib/wrapped/tree.rb', line 261

def totalchildcount id
  IupLib.IupGetAttribute(@handle, "TOTALCHILDCOUNT#{id}").first
end

#userdata(id, val = nil) ⇒ Object

:call-seq:

tree.userdata(id) # retrieves
tree.userdata(id, val) # sets

Accesses userdata for given node id.

  • id - identifier of a node

  • val - user data (a string)



272
273
274
275
276
277
278
# File 'lib/wrapped/tree.rb', line 272

def userdata id, val=nil
  if val.nil?
    IupLib.IupGetAttribute(@handle, "USERDATA#{id}").first
  else
    IupLib.IupSetAttribute @handle, "USERDATA#{id}", val
  end
end

#valueObject

:attr:value When retrieved, returns the identifier of the focussed node. When given a value, moves focus appropriately: ‘root’ / ‘last’ / ‘next’ / ‘previous’ / ‘pgdn’ / ‘pgup’



141
# File 'lib/wrapped/tree.rb', line 141

define_attribute :value