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 //