FLTK 1.3.0
Fl_Tree.H
Go to the documentation of this file.
1 //
2 // "$Id: Fl_Tree.H 8632 2011-05-04 02:59:50Z greg.ercolano $"
3 //
4 
5 #ifndef FL_TREE_H
6 #define FL_TREE_H
7 
8 #include <FL/Fl.H>
9 #include <FL/Fl_Group.H>
10 #include <FL/Fl_Scrollbar.H>
11 #include <FL/fl_draw.H>
12 
13 #include <FL/Fl_Tree_Item.H>
14 #include <FL/Fl_Tree_Prefs.H>
15 
17 // FL/Fl_Tree.H
19 //
20 // Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
21 // Copyright (C) 2009-2010 by Greg Ercolano.
22 //
23 // This library is free software; you can redistribute it and/or
24 // modify it under the terms of the GNU Library General Public
25 // License as published by the Free Software Foundation; either
26 // version 2 of the License, or (at your option) any later version.
27 //
28 // This library is distributed in the hope that it will be useful,
29 // but WITHOUT ANY WARRANTY; without even the implied warranty of
30 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 // Library General Public License for more details.
32 //
33 // You should have received a copy of the GNU Library General Public
34 // License along with this library; if not, write to the Free Software
35 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 // USA.
37 //
38 
43 
186 
196 };
197 
198 
199 class FL_EXPORT Fl_Tree : public Fl_Group {
200  Fl_Tree_Item *_root; // can be null!
201  Fl_Tree_Item *_item_focus; // item that has focus box
202  Fl_Tree_Item *_callback_item; // item invoked during callback (can be NULL)
203  Fl_Tree_Reason _callback_reason; // reason for the callback
204  Fl_Tree_Prefs _prefs; // all the tree's settings
205  int _scrollbar_size; // size of scrollbar trough
206 
207 protected:
210 
211 protected:
212  void item_clicked(Fl_Tree_Item* val);
215  callback_reason(reason);
216  callback_item(item);
217  do_callback((Fl_Widget*)this, user_data());
218  }
219  Fl_Tree_Item *next_visible_item(Fl_Tree_Item *start, int dir);
220 
221 public:
222  Fl_Tree(int X, int Y, int W, int H, const char *L=0);
223  ~Fl_Tree();
224  int handle(int e);
225  void draw();
226 
228  // root methods
230 
235  void root_label(const char *new_label) {
236  if ( ! _root ) return;
237  _root->label(new_label);
238  }
241  return(_root);
242  }
243 
245  // Item creation/removal methods
247  Fl_Tree_Item *add(const char *path);
248  Fl_Tree_Item* add(Fl_Tree_Item *item, const char *name);
249  Fl_Tree_Item *insert_above(Fl_Tree_Item *above, const char *name);
250  Fl_Tree_Item* insert(Fl_Tree_Item *item, const char *name, int pos);
251 
257  int remove(Fl_Tree_Item *item) {
258  if ( item == _root ) {
259  clear();
260  } else {
261  Fl_Tree_Item *parent = item->parent(); // find item's parent
262  if ( ! parent ) return(-1);
263  parent->remove_child(item); // remove child + children
264  }
265  return(0);
266  }
270  void clear() {
271  if ( ! _root ) return;
272  _root->clear_children();
273  delete _root; _root = 0;
274  }
279  if ( item->has_children() ) {
280  item->clear_children();
281  redraw(); // redraw only if there were children to clear
282  }
283  }
284 
286  // Item lookup methods
288  Fl_Tree_Item *find_item(const char *path);
289  const Fl_Tree_Item *find_item(const char *path) const;
290  int item_pathname(char *pathname, int pathnamelen, const Fl_Tree_Item *item) const;
291 
292  const Fl_Tree_Item *find_clicked() const;
293 
304  return(_callback_item);
305  }
306  Fl_Tree_Item *first();
307  Fl_Tree_Item *next(Fl_Tree_Item *item=0);
308  Fl_Tree_Item *prev(Fl_Tree_Item *item=0);
309  Fl_Tree_Item *last();
310  Fl_Tree_Item *first_selected_item();
311  Fl_Tree_Item *next_selected_item(Fl_Tree_Item *item=0);
312 
314  // Item open/close methods
316 
336  int open(Fl_Tree_Item *item, int docallback=1) {
337  if ( item->is_open() ) return(0);
338  item->open();
339  redraw();
340  if ( docallback ) {
341  do_callback_for_item(item, FL_TREE_REASON_OPENED);
342  }
343  return(1);
344  }
368  int open(const char *path, int docallback=1) {
369  Fl_Tree_Item *item = find_item(path);
370  if ( ! item ) return(-1);
371  return(open(item, docallback));
372  }
388  void open_toggle(Fl_Tree_Item *item, int docallback=1) {
389  if ( item->is_open() ) {
390  close(item, docallback);
391  } else {
392  open(item, docallback);
393  }
394  }
413  int close(Fl_Tree_Item *item, int docallback=1) {
414  if ( item->is_close() ) return(0);
415  item->close();
416  redraw();
417  if ( docallback ) {
418  do_callback_for_item(item, FL_TREE_REASON_CLOSED);
419  }
420  return(1);
421  }
444  int close(const char *path, int docallback=1) {
445  Fl_Tree_Item *item = find_item(path);
446  if ( ! item ) return(-1);
447  return(close(item, docallback));
448  }
459  int is_open(Fl_Tree_Item *item) const {
460  return(item->is_open()?1:0);
461  }
476  int is_open(const char *path) const {
477  const Fl_Tree_Item *item = find_item(path);
478  if ( ! item ) return(-1);
479  return(item->is_open()?1:0);
480  }
488  int is_close(Fl_Tree_Item *item) const {
489  return(item->is_close());
490  }
502  int is_close(const char *path) const {
503  const Fl_Tree_Item *item = find_item(path);
504  if ( ! item ) return(-1);
505  return(item->is_close()?1:0);
506  }
507 
524  int select(Fl_Tree_Item *item, int docallback=1) {
525  if ( ! item->is_selected() ) {
526  item->select();
527  set_changed();
528  if ( docallback ) {
529  do_callback_for_item(item, FL_TREE_REASON_SELECTED);
530  }
531  redraw();
532  return(1);
533  }
534  return(0);
535  }
556  int select(const char *path, int docallback=1) {
557  Fl_Tree_Item *item = find_item(path);
558  if ( ! item ) return(-1);
559  return(select(item, docallback));
560  }
574  void select_toggle(Fl_Tree_Item *item, int docallback=1) {
575  item->select_toggle();
576  set_changed();
577  if ( docallback ) {
578  do_callback_for_item(item, item->is_selected() ? FL_TREE_REASON_SELECTED
580  }
581  redraw();
582  }
599  int deselect(Fl_Tree_Item *item, int docallback=1) {
600  if ( item->is_selected() ) {
601  item->deselect();
602  set_changed();
603  if ( docallback ) {
604  do_callback_for_item(item, FL_TREE_REASON_DESELECTED);
605  }
606  redraw();
607  return(1);
608  }
609  return(0);
610  }
631  int deselect(const char *path, int docallback=1) {
632  Fl_Tree_Item *item = find_item(path);
633  if ( ! item ) return(-1);
634  return(deselect(item, docallback));
635  }
636 
637  int deselect_all(Fl_Tree_Item *item=0, int docallback=1);
638  int select_only(Fl_Tree_Item *selitem, int docallback=1);
639  int select_all(Fl_Tree_Item *item=0, int docallback=1);
640  void set_item_focus(Fl_Tree_Item *o);
641 
650  int is_selected(Fl_Tree_Item *item) const {
651  return(item->is_selected()?1:0);
652  }
664  int is_selected(const char *path) {
665  Fl_Tree_Item *item = find_item(path);
666  if ( ! item ) return(-1);
667  return(is_selected(item));
668  }
672  void show_self() {
673  if ( ! _root ) return;
674  _root->show_self();
675  }
676 
678  // Item attribute related methods
680 
683  return(_prefs.labelsize());
684  }
689  _prefs.labelsize(val);
690  }
693  return(_prefs.labelfont());
694  }
699  _prefs.labelfont(val);
700  }
703  return(_prefs.labelfgcolor());
704  }
709  _prefs.labelfgcolor(val);
710  }
713  return(_prefs.labelbgcolor());
714  }
719  _prefs.labelbgcolor(val);
720  }
723  return(_prefs.connectorcolor());
724  }
727  _prefs.connectorcolor(val);
728  }
732  int marginleft() const {
733  return(_prefs.marginleft());
734  }
738  void marginleft(int val) {
739  _prefs.marginleft(val);
740  redraw();
741  }
745  int margintop() const {
746  return(_prefs.margintop());
747  }
751  void margintop(int val) {
752  _prefs.margintop(val);
753  redraw();
754  }
759  return(_prefs.openchild_marginbottom());
760  }
764  void openchild_marginbottom(int val) {
765  _prefs.openchild_marginbottom(val);
766  redraw();
767  }
771  int connectorwidth() const {
772  return(_prefs.connectorwidth());
773  }
777  void connectorwidth(int val) {
778  _prefs.connectorwidth(val);
779  redraw();
780  }
785  Fl_Image *usericon() const {
786  return(_prefs.usericon());
787  }
797  void usericon(Fl_Image *val) {
798  _prefs.usericon(val);
799  redraw();
800  }
805  Fl_Image *openicon() const {
806  return(_prefs.openicon());
807  }
813  void openicon(Fl_Image *val) {
814  _prefs.openicon(val);
815  redraw();
816  }
821  Fl_Image *closeicon() const {
822  return(_prefs.closeicon());
823  }
829  void closeicon(Fl_Image *val) {
830  _prefs.closeicon(val);
831  redraw();
832  }
834  int showcollapse() const {
835  return(_prefs.showcollapse());
836  }
845  void showcollapse(int val) {
846  _prefs.showcollapse(val);
847  redraw();
848  }
850  int showroot() const {
851  return(_prefs.showroot());
852  }
857  void showroot(int val) {
858  _prefs.showroot(val);
859  redraw();
860  }
863  return(_prefs.connectorstyle());
864  }
867  _prefs.connectorstyle(val);
868  redraw();
869  }
874  return(_prefs.sortorder());
875  }
878  _prefs.sortorder(val);
879  // no redraw().. only affects new add()itions
880  }
886  return(_prefs.selectbox());
887  }
892  void selectbox(Fl_Boxtype val) {
893  _prefs.selectbox(val);
894  redraw();
895  }
898  return(_prefs.selectmode());
899  }
902  _prefs.selectmode(val);
903  }
904  int displayed(Fl_Tree_Item *item);
905  void show_item(Fl_Tree_Item *item, int yoff);
906  void show_item(Fl_Tree_Item *item);
907  void show_item_bottom(Fl_Tree_Item *item);
908  void show_item_middle(Fl_Tree_Item *item);
909  void show_item_top(Fl_Tree_Item *item);
910  void display(Fl_Tree_Item *item);
911  int vposition() const;
912  void vposition(int ypos);
913 
927  return( ( w == _vscroll ) ? 1 : 0 );
928  }
937  int scrollbar_size() const {
938  return(_scrollbar_size);
939  }
958  void scrollbar_size(int size) {
959  _scrollbar_size = size;
960  int scrollsize = _scrollbar_size ? _scrollbar_size : Fl::scrollbar_size();
961  if ( _vscroll->w() != scrollsize ) {
962  _vscroll->resize(x()+w()-scrollsize, h(), scrollsize, _vscroll->h());
963  }
964  }
965 
967  // callback related
969 
974  _callback_item = item;
975  }
980  return(_callback_item);
981  }
986  _callback_reason = reason;
987  }
1005  return(_callback_reason);
1006  }
1007 
1009  void load(class Fl_Preferences&);
1010 };
1011 
1012 #endif /*FL_TREE_H*/
1013 
1014 //
1015 // End of "$Id: Fl_Tree.H 8632 2011-05-04 02:59:50Z greg.ercolano $".
1016 //
int openchild_marginbottom() const
Get the amount of white space (in pixels) that should appear below an open child tree's contents...
Definition: Fl_Tree.H:758
Fl_Widget is the base class for all widgets in FLTK.
Definition: Fl_Widget.H:109
void marginleft(int val)
Set the amount of white space (in pixels) that should appear between the widget's left border and the...
Definition: Fl_Tree.H:738
an item was opened
Definition: Fl_Tree.H:194
void select(int val=1)
Change the item's selection state to the optionally specified 'val'.
Definition: Fl_Tree_Item.H:223
int is_open(const char *path) const
See if item specified by path (eg: "Parent/child/item") is open.
Definition: Fl_Tree.H:476
Fl_Tree_Select selectmode() const
Gets the tree's current selection mode.
Definition: Fl_Tree.H:897
Fl_Tree_Sort
Sort order options for items added to the tree.
Definition: Fl_Tree_Prefs.H:52
Fl_Tree_Connector connectorstyle() const
Returns the line drawing style for inter-connecting items.
Definition: Fl_Tree.H:862
void * user_data() const
Gets the user data for this widget.
Definition: Fl_Widget.H:589
int x() const
Gets the widget position in its window.
Definition: Fl_Widget.H:280
int close(const char *path, int docallback=1)
Closes the item specified by path, eg: "Parent/child/item".
Definition: Fl_Tree.H:444
Fl_Tree_Select
Tree selection style.
Definition: Fl_Tree_Prefs.H:70
void redraw()
Schedules the drawing of the widget.
Definition: Fl.cxx:1568
Fl_Color item_labelbgcolor(void) const
Get the default label background color used for creating new items.
Definition: Fl_Tree.H:712
Tree widget.
Definition: Fl_Tree.H:199
Fl_Font item_labelfont() const
Get the default font face used for creating new items.
Definition: Fl_Tree.H:692
Fl static class.
an item was selected
Definition: Fl_Tree.H:192
Fl_Color item_labelfgcolor(void) const
Get the default label foreground color used for creating new items.
Definition: Fl_Tree.H:702
void open()
Open this item and all its children.
Definition: Fl_Tree_Item.cxx:744
void item_labelbgcolor(Fl_Color val)
Set the default label background color used for creating new items.
Definition: Fl_Tree.H:718
int h() const
Gets the widget height.
Definition: Fl_Widget.H:295
void set_changed()
Marks the value of the widget as changed.
Definition: Fl_Widget.H:780
Fl_Tree_Item * callback_item()
Gets the item that caused the callback.
Definition: Fl_Tree.H:979
void size(int W, int H)
Changes the size of the widget.
Definition: Fl_Widget.H:337
void open_toggle(Fl_Tree_Item *item, int docallback=1)
Toggle the open state of item.
Definition: Fl_Tree.H:388
Fl_Tree_Item * item_clicked()
Return the item that was last clicked.
Definition: Fl_Tree.H:303
void selectmode(Fl_Tree_Select val)
Sets the tree's selection mode.
Definition: Fl_Tree.H:901
void deselect()
Disable the item's selection state.
Definition: Fl_Tree_Item.H:250
int w() const
Gets the widget width.
Definition: Fl_Widget.H:290
void margintop(int val)
Sets the amount of white space (in pixels) that should appear between the widget's top border and the...
Definition: Fl_Tree.H:751
int scrollbar_size() const
Gets the current size of the scrollbars' troughs, in pixels.
Definition: Fl_Tree.H:937
Fl_Image * closeicon() const
Returns the icon to be used as the 'close' icon.
Definition: Fl_Tree.H:821
Fl_Image is the base class used for caching and drawing all kinds of images in FLTK.
Definition: Fl_Image.H:51
int open(Fl_Tree_Item *item, int docallback=1)
Open the specified 'item'.
Definition: Fl_Tree.H:336
int showcollapse() const
Returns 1 if the collapse icon is enabled, 0 if not.
Definition: Fl_Tree.H:834
void select_toggle(Fl_Tree_Item *item, int docallback=1)
Toggle the select state of the specified item.
Definition: Fl_Tree.H:574
int is_scrollbar(Fl_Widget *w)
See if widget w is one of the Fl_Tree widget's scrollbars.
Definition: Fl_Tree.H:926
void connectorcolor(Fl_Color val)
Set the connector color used for tree connection lines.
Definition: Fl_Tree.H:726
Fl_Boxtype selectbox() const
Sets the style of box used to draw selected items.
Definition: Fl_Tree.H:885
int is_close(const char *path) const
See if item specified by path (eg: "Parent/child/item") is closed.
Definition: Fl_Tree.H:502
void close()
Close this item and all its children.
Definition: Fl_Tree_Item.cxx:753
void draw()
Draws the widget.
Definition: Fl_Group.cxx:741
Fl_Boxtype
Definition: Enumerations.H:464
void clear()
Deletes all child widgets from memory recursively.
Definition: Fl_Group.cxx:386
void selectbox(Fl_Boxtype val)
Gets the style of box used to draw selected items.
Definition: Fl_Tree.H:892
Fl_Tree_Reason
The reason the callback was invoked.
Definition: Fl_Tree.H:190
Fl_Scrollbar * _vscroll
Vertical scrollbar.
Definition: Fl_Tree.H:209
int is_selected(Fl_Tree_Item *item) const
See if the specified item is selected.
Definition: Fl_Tree.H:650
void insert(Fl_Widget &, int i)
The widget is removed from its current group (if any) and then inserted into this group...
Definition: Fl_Group.cxx:461
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:45
an item was closed
Definition: Fl_Tree.H:195
Fl_Color connectorcolor() const
Get the connector color used for tree connection lines.
Definition: Fl_Tree.H:722
void select_toggle()
Toggle the item's selection state.
Definition: Fl_Tree_Item.H:227
void add(Fl_Widget &)
The widget is removed from its current group (if any) and then added to the end of this group...
Definition: Fl_Group.cxx:494
void callback_item(Fl_Tree_Item *item)
Sets the item that was changed for this callback.
Definition: Fl_Tree.H:973
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:150
Fl_Image * usericon() const
Returns the Fl_Image being used as the default user icon for all newly created items.
Definition: Fl_Tree.H:785
void do_callback_for_item(Fl_Tree_Item *item, Fl_Tree_Reason reason)
Do the callback for the item, setting the item and reason.
Definition: Fl_Tree.H:214
int close(Fl_Tree_Item *item, int docallback=1)
Closes the specified item.
Definition: Fl_Tree.H:413
int is_selected(const char *path)
See if item specified by path (eg: "Parent/child/item") is selected.
Definition: Fl_Tree.H:664
int marginleft() const
Get the amount of white space (in pixels) that should appear between the widget's left border and the...
Definition: Fl_Tree.H:732
void clear_children()
Clear all the children for this item.
Definition: Fl_Tree_Item.cxx:147
int connectorwidth() const
Gets the width of the horizontal connection lines (in pixels) that appear to the left of each tree it...
Definition: Fl_Tree.H:771
int is_close(Fl_Tree_Item *item) const
See if the specified item is closed.
Definition: Fl_Tree.H:488
int remove_child(Fl_Tree_Item *item)
Remove child by item.
Definition: Fl_Tree_Item.cxx:345
int has_children() const
See if this item has children.
Definition: Fl_Tree_Item.H:160
void sortorder(Fl_Tree_Sort val)
Gets the sort order used to add items to the tree.
Definition: Fl_Tree.H:877
void item_labelsize(Fl_Fontsize val)
Set the default label font size used for creating new items.
Definition: Fl_Tree.H:688
void callback_reason(Fl_Tree_Reason reason)
Sets the reason for this callback.
Definition: Fl_Tree.H:985
int deselect(Fl_Tree_Item *item, int docallback=1)
De-select the specified item.
Definition: Fl_Tree.H:599
void showcollapse(int val)
Set if we should show the collapse icon or not.
Definition: Fl_Tree.H:845
Fl_Preferences provides methods to store user settings between application starts.
Definition: Fl_Preferences.H:69
int showroot() const
Returns 1 if the root item is to be shown, or 0 if not.
Definition: Fl_Tree.H:850
Fl_Tree_Connector
Defines the style of connection lines between items.
Definition: Fl_Tree_Prefs.H:61
int is_close() const
See if the item is 'closed'.
Definition: Fl_Tree_Item.H:213
int deselect(const char *path, int docallback=1)
Deselect an item specified by path (eg: "Parent/child/item").
Definition: Fl_Tree.H:631
int open(const char *path, int docallback=1)
Opens the item specified by path (eg: "Parent/child/item").
Definition: Fl_Tree.H:368
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:741
void showroot(int val)
Set if the root item should be shown or not.
Definition: Fl_Tree.H:857
Fl_Fontsize item_labelsize() const
Get the default label fontsize used for creating new items.
Definition: Fl_Tree.H:682
Fl_Tree_Sort sortorder() const
Set the default sort order used when items are added to the tree.
Definition: Fl_Tree.H:873
void connectorstyle(Fl_Tree_Connector val)
Sets the line drawing style for inter-connecting items.
Definition: Fl_Tree.H:866
void root_label(const char *new_label)
Set the label for the root item.
Definition: Fl_Tree.H:235
void openicon(Fl_Image *val)
Sets the icon to be used as the 'open' icon.
Definition: Fl_Tree.H:813
an item was de-selected
Definition: Fl_Tree.H:193
void item_labelfont(Fl_Font val)
Set the default font face used for creating new items.
Definition: Fl_Tree.H:698
utility header to pull drawing functions together
void connectorwidth(int val)
Sets the width of the horizontal connection lines (in pixels) that appear to the left of each tree it...
Definition: Fl_Tree.H:777
unsigned int Fl_Color
an FLTK color value
Definition: Enumerations.H:769
Tree item.
Definition: Fl_Tree_Item.H:59
static int scrollbar_size()
Gets the default scrollbar size used by Fl_Browser_, Fl_Help_View, Fl_Scroll, and Fl_Text_Display wid...
Definition: Fl.cxx:151
void do_callback()
Calls the widget callback.
Definition: Fl_Widget.H:835
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:712
Fl_Image * openicon() const
Returns the icon to be used as the 'open' icon.
Definition: Fl_Tree.H:805
int select(const char *path, int docallback=1)
Select the item specified by path (eg: "Parent/child/item").
Definition: Fl_Tree.H:556
Fl_Tree_Reason callback_reason() const
Gets the reason for this callback.
Definition: Fl_Tree.H:1004
void item_labelfgcolor(Fl_Color val)
Set the default label foreground color used for creating new items.
Definition: Fl_Tree.H:708
This file contains the definitions for Fl_Tree's preferences.
void show_self()
Print the tree as 'ascii art' to stdout.
Definition: Fl_Tree.H:672
void openchild_marginbottom(int val)
Set the amount of white space (in pixels) that should appear below an open child tree's contents...
Definition: Fl_Tree.H:764
Fl_Tree_Item * parent()
Return the parent for this item. Returns NULL if we are the root.
Definition: Fl_Tree_Item.H:190
void scrollbar_size(int size)
Sets the pixel size of the scrollbars' troughs to the size, in pixels.
Definition: Fl_Tree.H:958
int select(Fl_Tree_Item *item, int docallback=1)
Select the specified item.
Definition: Fl_Tree.H:524
void clear_children(Fl_Tree_Item *item)
Clear all the children of a particular node in the tree specified by item.
Definition: Fl_Tree.H:278
Fl_Tree_Item * root()
Returns the root item.
Definition: Fl_Tree.H:240
Tree widget's preferences.
Definition: Fl_Tree_Prefs.H:85
void usericon(Fl_Image *val)
Sets the Fl_Image to be used as the default user icon for all newly created items.
Definition: Fl_Tree.H:797
void closeicon(Fl_Image *val)
Sets the icon to be used as the 'close' icon.
Definition: Fl_Tree.H:829
This file contains the definitions for Fl_Tree_Item.
char is_selected() const
See if the item is selected.
Definition: Fl_Tree_Item.H:269
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition: Fl_Scrollbar.H:52
void clear()
Clear all children from the tree.
Definition: Fl_Tree.H:270
unknown reason
Definition: Fl_Tree.H:191
int is_open() const
See if the item is 'open'.
Definition: Fl_Tree_Item.H:209
int is_open(Fl_Tree_Item *item) const
See if item is open.
Definition: Fl_Tree.H:459
int margintop() const
Get the amount of white space (in pixels) that should appear between the widget's top border and the ...
Definition: Fl_Tree.H:745