FLTK 1.3.0
Fl_Tree.H
Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Tree.H 8632 2011-05-04 02:59:50Z greg.ercolano $"
00003 //
00004 
00005 #ifndef FL_TREE_H
00006 #define FL_TREE_H
00007 
00008 #include <FL/Fl.H>
00009 #include <FL/Fl_Group.H>
00010 #include <FL/Fl_Scrollbar.H>
00011 #include <FL/fl_draw.H>
00012 
00013 #include <FL/Fl_Tree_Item.H>
00014 #include <FL/Fl_Tree_Prefs.H>
00015 
00017 // FL/Fl_Tree.H
00019 //
00020 // Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
00021 // Copyright (C) 2009-2010 by Greg Ercolano.
00022 //
00023 // This library is free software; you can redistribute it and/or
00024 // modify it under the terms of the GNU Library General Public
00025 // License as published by the Free Software Foundation; either
00026 // version 2 of the License, or (at your option) any later version.
00027 //
00028 // This library is distributed in the hope that it will be useful,
00029 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00030 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00031 // Library General Public License for more details.
00032 //
00033 // You should have received a copy of the GNU Library General Public
00034 // License along with this library; if not, write to the Free Software
00035 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00036 // USA.
00037 //
00038 
00043 
00186 
00190 enum Fl_Tree_Reason {
00191   FL_TREE_REASON_NONE=0,        
00192   FL_TREE_REASON_SELECTED,      
00193   FL_TREE_REASON_DESELECTED,    
00194   FL_TREE_REASON_OPENED,        
00195   FL_TREE_REASON_CLOSED         
00196 };
00197 
00198 
00199 class FL_EXPORT Fl_Tree : public Fl_Group {
00200   Fl_Tree_Item  *_root;                         // can be null!
00201   Fl_Tree_Item  *_item_focus;                   // item that has focus box
00202   Fl_Tree_Item  *_callback_item;                // item invoked during callback (can be NULL)
00203   Fl_Tree_Reason _callback_reason;              // reason for the callback
00204   Fl_Tree_Prefs  _prefs;                        // all the tree's settings
00205   int            _scrollbar_size;               // size of scrollbar trough
00206 
00207 protected:
00209   Fl_Scrollbar *_vscroll;
00210   
00211 protected:
00212   void item_clicked(Fl_Tree_Item* val);
00214   void do_callback_for_item(Fl_Tree_Item* item, Fl_Tree_Reason reason) {
00215     callback_reason(reason);
00216     callback_item(item);
00217     do_callback((Fl_Widget*)this, user_data());
00218   }
00219   Fl_Tree_Item *next_visible_item(Fl_Tree_Item *start, int dir);
00220 
00221 public:
00222   Fl_Tree(int X, int Y, int W, int H, const char *L=0);
00223   ~Fl_Tree();
00224   int handle(int e);
00225   void draw();
00226   
00228   // root methods
00230   
00235   void root_label(const char *new_label) {
00236     if ( ! _root ) return;
00237     _root->label(new_label);
00238   }
00240   Fl_Tree_Item* root() {
00241     return(_root);
00242   }
00243   
00245   // Item creation/removal methods
00247   Fl_Tree_Item *add(const char *path);
00248   Fl_Tree_Item* add(Fl_Tree_Item *item, const char *name);
00249   Fl_Tree_Item *insert_above(Fl_Tree_Item *above, const char *name);
00250   Fl_Tree_Item* insert(Fl_Tree_Item *item, const char *name, int pos);
00251   
00257   int remove(Fl_Tree_Item *item) {
00258     if ( item == _root ) {
00259       clear();
00260     } else {
00261       Fl_Tree_Item *parent = item->parent();            // find item's parent
00262       if ( ! parent ) return(-1);
00263       parent->remove_child(item);                       // remove child + children
00264     }
00265     return(0);
00266   } 
00270   void clear() {
00271     if ( ! _root ) return;
00272     _root->clear_children();
00273     delete _root; _root = 0;
00274   } 
00278   void clear_children(Fl_Tree_Item *item) {
00279     if ( item->has_children() ) {
00280       item->clear_children();
00281       redraw();                         // redraw only if there were children to clear
00282     }
00283   } 
00284   
00286   // Item lookup methods
00288   Fl_Tree_Item *find_item(const char *path);
00289   const Fl_Tree_Item *find_item(const char *path) const;
00290   int item_pathname(char *pathname, int pathnamelen, const Fl_Tree_Item *item) const;
00291 
00292   const Fl_Tree_Item *find_clicked() const;
00293   
00303   Fl_Tree_Item *item_clicked() {
00304     return(_callback_item);
00305   }
00306   Fl_Tree_Item *first();
00307   Fl_Tree_Item *next(Fl_Tree_Item *item=0);
00308   Fl_Tree_Item *prev(Fl_Tree_Item *item=0);
00309   Fl_Tree_Item *last();
00310   Fl_Tree_Item *first_selected_item();
00311   Fl_Tree_Item *next_selected_item(Fl_Tree_Item *item=0);
00312 
00314   // Item open/close methods
00316   
00336   int open(Fl_Tree_Item *item, int docallback=1) {
00337     if ( item->is_open() ) return(0);
00338     item->open();
00339     redraw();
00340     if ( docallback ) {
00341       do_callback_for_item(item, FL_TREE_REASON_OPENED);
00342     }
00343     return(1);
00344   }
00368   int open(const char *path, int docallback=1) {
00369     Fl_Tree_Item *item = find_item(path);
00370     if ( ! item ) return(-1);
00371     return(open(item, docallback));
00372   }
00388   void open_toggle(Fl_Tree_Item *item, int docallback=1) {
00389     if ( item->is_open() ) {
00390       close(item, docallback);
00391     } else {
00392       open(item, docallback);
00393     }
00394   }
00413   int close(Fl_Tree_Item *item, int docallback=1) {
00414     if ( item->is_close() ) return(0);
00415     item->close();
00416     redraw();
00417     if ( docallback ) {
00418       do_callback_for_item(item, FL_TREE_REASON_CLOSED);
00419     }
00420     return(1);
00421   }
00444   int close(const char *path, int docallback=1) {
00445     Fl_Tree_Item *item = find_item(path);
00446     if ( ! item ) return(-1);
00447     return(close(item, docallback));
00448   }
00459   int is_open(Fl_Tree_Item *item) const {
00460     return(item->is_open()?1:0);
00461   }
00476   int is_open(const char *path) const {
00477     const Fl_Tree_Item *item = find_item(path);
00478     if ( ! item ) return(-1);
00479     return(item->is_open()?1:0);
00480   }
00488   int is_close(Fl_Tree_Item *item) const {
00489     return(item->is_close());
00490   }
00502   int is_close(const char *path) const {
00503     const Fl_Tree_Item *item = find_item(path);
00504     if ( ! item ) return(-1);
00505     return(item->is_close()?1:0);
00506   }
00507   
00524   int select(Fl_Tree_Item *item, int docallback=1) {
00525     if ( ! item->is_selected() ) {
00526       item->select();
00527       set_changed();
00528       if ( docallback ) {
00529         do_callback_for_item(item, FL_TREE_REASON_SELECTED);
00530       }
00531       redraw();
00532       return(1);
00533     }
00534     return(0);
00535   }
00556   int select(const char *path, int docallback=1) {
00557     Fl_Tree_Item *item = find_item(path);
00558     if ( ! item ) return(-1);
00559     return(select(item, docallback));
00560   }
00574   void select_toggle(Fl_Tree_Item *item, int docallback=1) {
00575     item->select_toggle();
00576     set_changed();
00577     if ( docallback ) {
00578       do_callback_for_item(item, item->is_selected() ? FL_TREE_REASON_SELECTED
00579                                                      : FL_TREE_REASON_DESELECTED);
00580     }
00581     redraw();
00582   }
00599   int deselect(Fl_Tree_Item *item, int docallback=1) {
00600     if ( item->is_selected() ) {
00601       item->deselect();
00602       set_changed();
00603       if ( docallback ) {
00604         do_callback_for_item(item, FL_TREE_REASON_DESELECTED);
00605       }
00606       redraw();
00607       return(1);
00608     }
00609     return(0);
00610   }
00631   int deselect(const char *path, int docallback=1) {
00632     Fl_Tree_Item *item = find_item(path);
00633     if ( ! item ) return(-1);
00634     return(deselect(item, docallback));
00635   }
00636   
00637   int deselect_all(Fl_Tree_Item *item=0, int docallback=1);
00638   int select_only(Fl_Tree_Item *selitem, int docallback=1);
00639   int select_all(Fl_Tree_Item *item=0, int docallback=1);
00640   void set_item_focus(Fl_Tree_Item *o);
00641   
00650   int is_selected(Fl_Tree_Item *item) const {
00651     return(item->is_selected()?1:0);
00652   }
00664   int is_selected(const char *path) {
00665     Fl_Tree_Item *item = find_item(path);
00666     if ( ! item ) return(-1);
00667     return(is_selected(item));
00668   }
00672   void show_self() {
00673     if ( ! _root ) return;
00674     _root->show_self();
00675   }
00676   
00678   // Item attribute related methods
00680   
00682   Fl_Fontsize item_labelsize() const {
00683     return(_prefs.labelsize());
00684   }
00688   void item_labelsize(Fl_Fontsize val) {
00689     _prefs.labelsize(val);
00690   }
00692   Fl_Font item_labelfont() const {
00693     return(_prefs.labelfont());
00694   }
00698   void item_labelfont(Fl_Font val) {
00699     _prefs.labelfont(val);
00700   }
00702   Fl_Color item_labelfgcolor(void) const {
00703     return(_prefs.labelfgcolor());
00704   }
00708   void item_labelfgcolor(Fl_Color val) {
00709     _prefs.labelfgcolor(val);
00710   }
00712   Fl_Color item_labelbgcolor(void) const {
00713     return(_prefs.labelbgcolor());
00714   }
00718   void item_labelbgcolor(Fl_Color val) {
00719     _prefs.labelbgcolor(val);
00720   }
00722   Fl_Color connectorcolor() const {
00723     return(_prefs.connectorcolor());
00724   }
00726   void connectorcolor(Fl_Color val) {
00727     _prefs.connectorcolor(val);
00728   }
00732   int marginleft() const {
00733     return(_prefs.marginleft());
00734   }
00738   void marginleft(int val) {
00739     _prefs.marginleft(val);
00740     redraw();
00741   }
00745   int margintop() const {
00746     return(_prefs.margintop());
00747   }
00751   void margintop(int val) {
00752     _prefs.margintop(val);
00753     redraw();
00754   }
00758   int openchild_marginbottom() const {
00759     return(_prefs.openchild_marginbottom());
00760   }
00764   void openchild_marginbottom(int val) {
00765     _prefs.openchild_marginbottom(val);
00766     redraw();
00767   }
00771   int connectorwidth() const {
00772     return(_prefs.connectorwidth());
00773   }
00777   void connectorwidth(int val) {
00778     _prefs.connectorwidth(val);
00779     redraw();
00780   }
00785   Fl_Image *usericon() const {
00786     return(_prefs.usericon());
00787   }
00797   void usericon(Fl_Image *val) {
00798     _prefs.usericon(val);
00799     redraw();
00800   }
00805   Fl_Image *openicon() const {
00806     return(_prefs.openicon());
00807   }
00813   void openicon(Fl_Image *val) {
00814     _prefs.openicon(val);
00815     redraw();
00816   }
00821   Fl_Image *closeicon() const {
00822     return(_prefs.closeicon());
00823   }
00829   void closeicon(Fl_Image *val) {
00830     _prefs.closeicon(val);
00831     redraw();
00832   }
00834   int showcollapse() const {
00835     return(_prefs.showcollapse());
00836   }
00845   void showcollapse(int val) {
00846     _prefs.showcollapse(val);
00847     redraw();
00848   }
00850   int showroot() const {
00851     return(_prefs.showroot());
00852   }
00857   void showroot(int val) {
00858     _prefs.showroot(val);
00859     redraw();
00860   }
00862   Fl_Tree_Connector connectorstyle() const {
00863     return(_prefs.connectorstyle());
00864   }
00866   void connectorstyle(Fl_Tree_Connector val) {
00867     _prefs.connectorstyle(val);
00868     redraw();
00869   }
00873   Fl_Tree_Sort sortorder() const {
00874     return(_prefs.sortorder());
00875   }
00877   void sortorder(Fl_Tree_Sort val) {
00878     _prefs.sortorder(val);
00879     // no redraw().. only affects new add()itions
00880   }
00885   Fl_Boxtype selectbox() const {
00886     return(_prefs.selectbox());
00887   }
00892   void selectbox(Fl_Boxtype val) {
00893     _prefs.selectbox(val);
00894     redraw();
00895   }
00897   Fl_Tree_Select selectmode() const {
00898     return(_prefs.selectmode());
00899   }
00901   void selectmode(Fl_Tree_Select val) {
00902     _prefs.selectmode(val);
00903   }
00904   int displayed(Fl_Tree_Item *item);
00905   void show_item(Fl_Tree_Item *item, int yoff);
00906   void show_item(Fl_Tree_Item *item);
00907   void show_item_bottom(Fl_Tree_Item *item);
00908   void show_item_middle(Fl_Tree_Item *item);
00909   void show_item_top(Fl_Tree_Item *item);
00910   void display(Fl_Tree_Item *item);
00911   int  vposition() const;
00912   void vposition(int ypos);
00913 
00926   int is_scrollbar(Fl_Widget *w) {
00927       return( ( w == _vscroll ) ? 1 : 0 );
00928   }
00937   int scrollbar_size() const {
00938       return(_scrollbar_size);
00939   }
00958   void scrollbar_size(int size) {
00959       _scrollbar_size = size;
00960       int scrollsize = _scrollbar_size ? _scrollbar_size : Fl::scrollbar_size();
00961       if ( _vscroll->w() != scrollsize ) {
00962         _vscroll->resize(x()+w()-scrollsize, h(), scrollsize, _vscroll->h());
00963       }
00964   }   
00965 
00967   // callback related
00969 
00973   void callback_item(Fl_Tree_Item* item) {
00974     _callback_item = item;
00975   }
00979   Fl_Tree_Item* callback_item() {
00980     return(_callback_item);
00981   }
00985   void callback_reason(Fl_Tree_Reason reason) {
00986     _callback_reason = reason;
00987   }
01004   Fl_Tree_Reason callback_reason() const {
01005     return(_callback_reason);
01006   }
01007 
01009   void load(class Fl_Preferences&);
01010 };
01011 
01012 #endif /*FL_TREE_H*/
01013 
01014 //
01015 // End of "$Id: Fl_Tree.H 8632 2011-05-04 02:59:50Z greg.ercolano $".
01016 //