FLTK 1.3.x

Fl_Tree.H

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Tree.H 7981 2010-12-08 23:53:04Z 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 
00121 
00125 enum Fl_Tree_Reason {
00126   FL_TREE_REASON_NONE=0,        
00127   FL_TREE_REASON_SELECTED,      
00128   FL_TREE_REASON_DESELECTED,    
00129   FL_TREE_REASON_OPENED,        
00130   FL_TREE_REASON_CLOSED         
00131 };
00132 
00133 
00134 class FL_EXPORT Fl_Tree : public Fl_Group {
00135   Fl_Tree_Item  *_root;                         // can be null!
00136   Fl_Tree_Item  *_item_focus;                   // item that has focus box
00137   Fl_Tree_Item  *_callback_item;                // item invoked during callback (can be NULL)
00138   Fl_Tree_Reason _callback_reason;              // reason for the callback
00139   Fl_Tree_Prefs  _prefs;                        // all the tree's settings
00140   int            _scrollbar_size;               // size of scrollbar trough
00141 
00142 protected:
00144   Fl_Scrollbar *_vscroll;
00145   
00146 protected:
00147   void item_clicked(Fl_Tree_Item* val);
00149   void do_callback_for_item(Fl_Tree_Item* item, Fl_Tree_Reason reason) {
00150     callback_reason(reason);
00151     callback_item(item);
00152     do_callback((Fl_Widget*)this, user_data());
00153   }
00154   Fl_Tree_Item *next_visible_item(Fl_Tree_Item *start, int dir);
00155 
00156 public:
00157   Fl_Tree(int X, int Y, int W, int H, const char *L=0);
00158   ~Fl_Tree();
00159   int handle(int e);
00160   void draw();
00161   
00163   // root methods
00165   
00170   void root_label(const char *new_label) {
00171     if ( ! _root ) return;
00172     _root->label(new_label);
00173   }
00175   Fl_Tree_Item* root() {
00176     return(_root);
00177   }
00178   
00180   // Item creation/removal methods
00182   Fl_Tree_Item *add(const char *path);
00183   Fl_Tree_Item* add(Fl_Tree_Item *item, const char *name);
00184   Fl_Tree_Item *insert_above(Fl_Tree_Item *above, const char *name);
00185   Fl_Tree_Item* insert(Fl_Tree_Item *item, const char *name, int pos);
00186   
00191   int remove(Fl_Tree_Item *item) {
00192     if ( !item ) return(0);
00193     if ( item == _root ) {
00194       clear();
00195     } else {
00196       Fl_Tree_Item *parent = item->parent();            // find item's parent
00197       if ( ! parent ) return(-1);
00198       parent->remove_child(item);                       // remove child + children
00199     }
00200     return(0);
00201   } 
00205   void clear() {
00206     if ( ! _root ) return;
00207     _root->clear_children();
00208     delete _root; _root = 0;
00209   } 
00211   void clear_children(Fl_Tree_Item *item) {
00212     if ( item->has_children() ) {
00213       item->clear_children();
00214       redraw();                         // redraw only if there were children to clear
00215     }
00216   } 
00217   
00219   // Item lookup methods
00221   Fl_Tree_Item *find_item(const char *path);
00222   const Fl_Tree_Item *find_item(const char *path) const;
00223   int item_pathname(char *pathname, int pathnamelen, const Fl_Tree_Item *item) const;
00224 
00225   const Fl_Tree_Item *find_clicked() const;
00226   
00236   Fl_Tree_Item *item_clicked() {
00237     return(_callback_item);
00238   }
00239   Fl_Tree_Item *first();
00240   Fl_Tree_Item *next(Fl_Tree_Item *item=0);
00241   Fl_Tree_Item *prev(Fl_Tree_Item *item=0);
00242   Fl_Tree_Item *last();
00243   Fl_Tree_Item *first_selected_item();
00244   Fl_Tree_Item *next_selected_item(Fl_Tree_Item *item=0);
00245 
00247   // Item open/close methods
00249   
00270   int open(Fl_Tree_Item *item, int docallback=1) {
00271     if ( item->is_open() ) return(0);
00272     item->open();
00273     redraw();
00274     if ( docallback ) {
00275       do_callback_for_item(item, FL_TREE_REASON_OPENED);
00276     }
00277     return(1);
00278   }
00300   int open(const char *path, int docallback=1) {
00301     Fl_Tree_Item *item = find_item(path);
00302     if ( ! item ) return(-1);
00303     return(open(item, docallback));
00304   }
00320   void open_toggle(Fl_Tree_Item *item, int docallback=1) {
00321     if ( item->is_open() ) {
00322       close(item, docallback);
00323     } else {
00324       open(item, docallback);
00325     }
00326   }
00346   int close(Fl_Tree_Item *item, int docallback=1) {
00347     if ( item->is_close() ) return(0);
00348     item->close();
00349     redraw();
00350     if ( docallback ) {
00351       do_callback_for_item(item, FL_TREE_REASON_CLOSED);
00352     }
00353     return(1);
00354   }
00375   int close(const char *path, int docallback=1) {
00376     Fl_Tree_Item *item = find_item(path);
00377     if ( ! item ) return(-1);
00378     return(close(item, docallback));
00379   }
00391   int is_open(Fl_Tree_Item *item) const {
00392     return(item->is_open()?1:0);
00393   }
00406   int is_open(const char *path) const {
00407     const Fl_Tree_Item *item = find_item(path);
00408     if ( ! item ) return(-1);
00409     return(item->is_open()?1:0);
00410   }
00419   int is_close(Fl_Tree_Item *item) const {
00420     return(item->is_close());
00421   }
00431   int is_close(const char *path) const {
00432     const Fl_Tree_Item *item = find_item(path);
00433     if ( ! item ) return(-1);
00434     return(item->is_close()?1:0);
00435   }
00436   
00454   int select(Fl_Tree_Item *item, int docallback=1) {
00455     if ( ! item->is_selected() ) {
00456       item->select();
00457       set_changed();
00458       if ( docallback ) {
00459         do_callback_for_item(item, FL_TREE_REASON_SELECTED);
00460       }
00461       redraw();
00462       return(1);
00463     }
00464     return(0);
00465   }
00484   int select(const char *path, int docallback=1) {
00485     Fl_Tree_Item *item = find_item(path);
00486     if ( ! item ) return(-1);
00487     return(select(item, docallback));
00488   }
00502   void select_toggle(Fl_Tree_Item *item, int docallback=1) {
00503     item->select_toggle();
00504     set_changed();
00505     if ( docallback ) {
00506       do_callback_for_item(item, item->is_selected() ? FL_TREE_REASON_SELECTED
00507                                                      : FL_TREE_REASON_DESELECTED);
00508     }
00509     redraw();
00510   }
00528   int deselect(Fl_Tree_Item *item, int docallback=1) {
00529     if ( item->is_selected() ) {
00530       item->deselect();
00531       set_changed();
00532       if ( docallback ) {
00533         do_callback_for_item(item, FL_TREE_REASON_DESELECTED);
00534       }
00535       redraw();
00536       return(1);
00537     }
00538     return(0);
00539   }
00558   int deselect(const char *path, int docallback=1) {
00559     Fl_Tree_Item *item = find_item(path);
00560     if ( ! item ) return(-1);
00561     return(deselect(item, docallback));
00562   }
00563   
00564   int deselect_all(Fl_Tree_Item *item=0, int docallback=1);
00565   int select_only(Fl_Tree_Item *selitem, int docallback=1);
00566   int select_all(Fl_Tree_Item *item=0, int docallback=1);
00567   void set_item_focus(Fl_Tree_Item *o);
00568   
00577   int is_selected(Fl_Tree_Item *item) const {
00578     return(item->is_selected()?1:0);
00579   }
00589   int is_selected(const char *path) {
00590     Fl_Tree_Item *item = find_item(path);
00591     if ( ! item ) return(-1);
00592     return(is_selected(item));
00593   }
00597   void show_self() {
00598     if ( ! _root ) return;
00599     _root->show_self();
00600   }
00601   
00603   // Item attribute related methods
00605   
00607   int labelsize() const {
00608     return(_prefs.labelsize());
00609   }
00613   void labelsize(int val) {
00614     _prefs.labelsize(val);
00615   }
00616   
00622   int labelfont() const {
00623     return(_prefs.labelfont());
00624   }
00630   void labelfont(int val) {
00631     _prefs.labelfont(val);
00632   }
00636   int  marginleft() const {
00637     return(_prefs.marginleft());
00638   }
00642   void marginleft(int val) {
00643     _prefs.marginleft(val);
00644     redraw();
00645   }
00649   int  margintop() const {
00650     return(_prefs.margintop());
00651   }
00655   void margintop(int val) {
00656     _prefs.margintop(val);
00657     redraw();
00658   }
00662   int  openchild_marginbottom() const {
00663     return(_prefs.openchild_marginbottom());
00664   }
00668   void openchild_marginbottom(int val) {
00669     _prefs.openchild_marginbottom(val);
00670     redraw();
00671   }
00675   int  connectorwidth() const {
00676     return(_prefs.connectorwidth());
00677   }
00681   void connectorwidth(int val) {
00682     _prefs.connectorwidth(val);
00683     redraw();
00684   }
00688   Fl_Image *usericon() const {
00689     return(_prefs.usericon());
00690   }
00700   void usericon(Fl_Image *val) {
00701     _prefs.usericon(val);
00702     redraw();
00703   }
00708   Fl_Image *openicon() const {
00709     return(_prefs.openicon());
00710   }
00716   void openicon(Fl_Image *val) {
00717     _prefs.openicon(val);
00718     redraw();
00719   }
00724   Fl_Image *closeicon() const {
00725     return(_prefs.closeicon());
00726   }
00732   void closeicon(Fl_Image *val) {
00733     _prefs.closeicon(val);
00734     redraw();
00735   }
00737   int showcollapse() const {
00738     return(_prefs.showcollapse());
00739   }
00748   void showcollapse(int val) {
00749     _prefs.showcollapse(val);
00750     redraw();
00751   }
00753   int showroot() const {
00754     return(_prefs.showroot());
00755   }
00760   void showroot(int val) {
00761     _prefs.showroot(val);
00762     redraw();
00763   }
00765   Fl_Tree_Connector connectorstyle() const {
00766     return(_prefs.connectorstyle());
00767   }
00769   void connectorstyle(Fl_Tree_Connector val) {
00770     _prefs.connectorstyle(val);
00771     redraw();
00772   }
00776   Fl_Tree_Sort sortorder() const {
00777     return(_prefs.sortorder());
00778   }
00780   void sortorder(Fl_Tree_Sort val) {
00781     _prefs.sortorder(val);
00782     // no redraw().. only affects new add()itions
00783   }
00788   Fl_Boxtype selectbox() const {
00789     return(_prefs.selectbox());
00790   }
00795   void selectbox(Fl_Boxtype val) {
00796     _prefs.selectbox(val);
00797     redraw();
00798   }
00800   Fl_Tree_Select selectmode() const {
00801     return(_prefs.selectmode());
00802   }
00804   void selectmode(Fl_Tree_Select val) {
00805     _prefs.selectmode(val);
00806   }
00807   int displayed(Fl_Tree_Item *item);
00808   void show_item(Fl_Tree_Item *item, int yoff);
00809   void show_item(Fl_Tree_Item *item);
00810   void show_item_bottom(Fl_Tree_Item *item);
00811   void show_item_middle(Fl_Tree_Item *item);
00812   void show_item_top(Fl_Tree_Item *item);
00813   void display(Fl_Tree_Item *item);
00814   int  vposition() const;
00815   void vposition(int ypos);
00816 
00829   int is_scrollbar(Fl_Widget *w) {
00830       return( ( w == _vscroll ) ? 1 : 0 );
00831   }
00840   int scrollbar_size() const {
00841       return(_scrollbar_size);
00842   }
00861   void scrollbar_size(int size) {
00862       _scrollbar_size = size;
00863       int scrollsize = _scrollbar_size ? _scrollbar_size : Fl::scrollbar_size();
00864       if ( _vscroll->w() != scrollsize ) {
00865         _vscroll->resize(x()+w()-scrollsize, h(), scrollsize, _vscroll->h());
00866       }
00867   }   
00868 
00870   // callback related
00872 
00876   void callback_item(Fl_Tree_Item* item) {
00877     _callback_item = item;
00878   }
00882   Fl_Tree_Item* callback_item() {
00883     return(_callback_item);
00884   }
00888   void callback_reason(Fl_Tree_Reason reason) {
00889     _callback_reason = reason;
00890   }
00907   Fl_Tree_Reason callback_reason() const {
00908     return(_callback_reason);
00909   }
00910 
00912   void load(class Fl_Preferences&);
00913 };
00914 
00915 #endif /*FL_TREE_H*/
00916 
00917 //
00918 // End of "$Id: Fl_Tree.H 7981 2010-12-08 23:53:04Z greg.ercolano $".
00919 //