FLTK 1.3.0
Fl_Tree.H
Go to the documentation of this file.
00001 //
00002 // "$Id$"
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 
00167 
00171 enum Fl_Tree_Reason {
00172   FL_TREE_REASON_NONE=0,        
00173   FL_TREE_REASON_SELECTED,      
00174   FL_TREE_REASON_DESELECTED,    
00175   FL_TREE_REASON_OPENED,        
00176   FL_TREE_REASON_CLOSED         
00177 };
00178 
00179 
00180 class FL_EXPORT Fl_Tree : public Fl_Group {
00181   Fl_Tree_Item  *_root;                         // can be null!
00182   Fl_Tree_Item  *_item_focus;                   // item that has focus box
00183   Fl_Tree_Item  *_callback_item;                // item invoked during callback (can be NULL)
00184   Fl_Tree_Reason _callback_reason;              // reason for the callback
00185   Fl_Tree_Prefs  _prefs;                        // all the tree's settings
00186   int            _scrollbar_size;               // size of scrollbar trough
00187 
00188 protected:
00190   Fl_Scrollbar *_vscroll;
00191   
00192 protected:
00193   void item_clicked(Fl_Tree_Item* val);
00195   void do_callback_for_item(Fl_Tree_Item* item, Fl_Tree_Reason reason) {
00196     callback_reason(reason);
00197     callback_item(item);
00198     do_callback((Fl_Widget*)this, user_data());
00199   }
00200   Fl_Tree_Item *next_visible_item(Fl_Tree_Item *start, int dir);
00201 
00202 public:
00203   Fl_Tree(int X, int Y, int W, int H, const char *L=0);
00204   ~Fl_Tree();
00205   int handle(int e);
00206   void draw();
00207   
00209   // root methods
00211   
00216   void root_label(const char *new_label) {
00217     if ( ! _root ) return;
00218     _root->label(new_label);
00219   }
00221   Fl_Tree_Item* root() {
00222     return(_root);
00223   }
00224   
00226   // Item creation/removal methods
00228   Fl_Tree_Item *add(const char *path);
00229   Fl_Tree_Item* add(Fl_Tree_Item *item, const char *name);
00230   Fl_Tree_Item *insert_above(Fl_Tree_Item *above, const char *name);
00231   Fl_Tree_Item* insert(Fl_Tree_Item *item, const char *name, int pos);
00232   
00238   int remove(Fl_Tree_Item *item) {
00239     if ( item == _root ) {
00240       clear();
00241     } else {
00242       Fl_Tree_Item *parent = item->parent();            // find item's parent
00243       if ( ! parent ) return(-1);
00244       parent->remove_child(item);                       // remove child + children
00245     }
00246     return(0);
00247   } 
00251   void clear() {
00252     if ( ! _root ) return;
00253     _root->clear_children();
00254     delete _root; _root = 0;
00255   } 
00259   void clear_children(Fl_Tree_Item *item) {
00260     if ( item->has_children() ) {
00261       item->clear_children();
00262       redraw();                         // redraw only if there were children to clear
00263     }
00264   } 
00265   
00267   // Item lookup methods
00269   Fl_Tree_Item *find_item(const char *path);
00270   const Fl_Tree_Item *find_item(const char *path) const;
00271   int item_pathname(char *pathname, int pathnamelen, const Fl_Tree_Item *item) const;
00272 
00273   const Fl_Tree_Item *find_clicked() const;
00274   
00284   Fl_Tree_Item *item_clicked() {
00285     return(_callback_item);
00286   }
00287   Fl_Tree_Item *first();
00288   Fl_Tree_Item *next(Fl_Tree_Item *item=0);
00289   Fl_Tree_Item *prev(Fl_Tree_Item *item=0);
00290   Fl_Tree_Item *last();
00291   Fl_Tree_Item *first_selected_item();
00292   Fl_Tree_Item *next_selected_item(Fl_Tree_Item *item=0);
00293 
00295   // Item open/close methods
00297   
00317   int open(Fl_Tree_Item *item, int docallback=1) {
00318     if ( item->is_open() ) return(0);
00319     item->open();
00320     redraw();
00321     if ( docallback ) {
00322       do_callback_for_item(item, FL_TREE_REASON_OPENED);
00323     }
00324     return(1);
00325   }
00346   int open(const char *path, int docallback=1) {
00347     Fl_Tree_Item *item = find_item(path);
00348     if ( ! item ) return(-1);
00349     return(open(item, docallback));
00350   }
00366   void open_toggle(Fl_Tree_Item *item, int docallback=1) {
00367     if ( item->is_open() ) {
00368       close(item, docallback);
00369     } else {
00370       open(item, docallback);
00371     }
00372   }
00391   int close(Fl_Tree_Item *item, int docallback=1) {
00392     if ( item->is_close() ) return(0);
00393     item->close();
00394     redraw();
00395     if ( docallback ) {
00396       do_callback_for_item(item, FL_TREE_REASON_CLOSED);
00397     }
00398     return(1);
00399   }
00419   int close(const char *path, int docallback=1) {
00420     Fl_Tree_Item *item = find_item(path);
00421     if ( ! item ) return(-1);
00422     return(close(item, docallback));
00423   }
00434   int is_open(Fl_Tree_Item *item) const {
00435     return(item->is_open()?1:0);
00436   }
00448   int is_open(const char *path) const {
00449     const Fl_Tree_Item *item = find_item(path);
00450     if ( ! item ) return(-1);
00451     return(item->is_open()?1:0);
00452   }
00460   int is_close(Fl_Tree_Item *item) const {
00461     return(item->is_close());
00462   }
00471   int is_close(const char *path) const {
00472     const Fl_Tree_Item *item = find_item(path);
00473     if ( ! item ) return(-1);
00474     return(item->is_close()?1:0);
00475   }
00476   
00493   int select(Fl_Tree_Item *item, int docallback=1) {
00494     if ( ! item->is_selected() ) {
00495       item->select();
00496       set_changed();
00497       if ( docallback ) {
00498         do_callback_for_item(item, FL_TREE_REASON_SELECTED);
00499       }
00500       redraw();
00501       return(1);
00502     }
00503     return(0);
00504   }
00522   int select(const char *path, int docallback=1) {
00523     Fl_Tree_Item *item = find_item(path);
00524     if ( ! item ) return(-1);
00525     return(select(item, docallback));
00526   }
00540   void select_toggle(Fl_Tree_Item *item, int docallback=1) {
00541     item->select_toggle();
00542     set_changed();
00543     if ( docallback ) {
00544       do_callback_for_item(item, item->is_selected() ? FL_TREE_REASON_SELECTED
00545                                                      : FL_TREE_REASON_DESELECTED);
00546     }
00547     redraw();
00548   }
00565   int deselect(Fl_Tree_Item *item, int docallback=1) {
00566     if ( item->is_selected() ) {
00567       item->deselect();
00568       set_changed();
00569       if ( docallback ) {
00570         do_callback_for_item(item, FL_TREE_REASON_DESELECTED);
00571       }
00572       redraw();
00573       return(1);
00574     }
00575     return(0);
00576   }
00594   int deselect(const char *path, int docallback=1) {
00595     Fl_Tree_Item *item = find_item(path);
00596     if ( ! item ) return(-1);
00597     return(deselect(item, docallback));
00598   }
00599   
00600   int deselect_all(Fl_Tree_Item *item=0, int docallback=1);
00601   int select_only(Fl_Tree_Item *selitem, int docallback=1);
00602   int select_all(Fl_Tree_Item *item=0, int docallback=1);
00603   void set_item_focus(Fl_Tree_Item *o);
00604   
00613   int is_selected(Fl_Tree_Item *item) const {
00614     return(item->is_selected()?1:0);
00615   }
00624   int is_selected(const char *path) {
00625     Fl_Tree_Item *item = find_item(path);
00626     if ( ! item ) return(-1);
00627     return(is_selected(item));
00628   }
00632   void show_self() {
00633     if ( ! _root ) return;
00634     _root->show_self();
00635   }
00636   
00638   // Item attribute related methods
00640   
00642   Fl_Fontsize item_labelsize() const {
00643     return(_prefs.labelsize());
00644   }
00648   void item_labelsize(Fl_Fontsize val) {
00649     _prefs.labelsize(val);
00650   }
00652   Fl_Font item_labelfont() const {
00653     return(_prefs.labelfont());
00654   }
00658   void item_labelfont(Fl_Font val) {
00659     _prefs.labelfont(val);
00660   }
00662   Fl_Color item_labelfgcolor(void) const {
00663     return(_prefs.labelfgcolor());
00664   }
00668   void item_labelfgcolor(Fl_Color val) {
00669     _prefs.labelfgcolor(val);
00670   }
00672   Fl_Color item_labelbgcolor(void) const {
00673     return(_prefs.labelbgcolor());
00674   }
00678   void item_labelbgcolor(Fl_Color val) {
00679     _prefs.labelbgcolor(val);
00680   }
00682   Fl_Color connectorcolor() const {
00683     return(_prefs.connectorcolor());
00684   }
00686   void connectorcolor(Fl_Color val) {
00687     _prefs.connectorcolor(val);
00688   }
00692   int marginleft() const {
00693     return(_prefs.marginleft());
00694   }
00698   void marginleft(int val) {
00699     _prefs.marginleft(val);
00700     redraw();
00701   }
00705   int margintop() const {
00706     return(_prefs.margintop());
00707   }
00711   void margintop(int val) {
00712     _prefs.margintop(val);
00713     redraw();
00714   }
00718   int openchild_marginbottom() const {
00719     return(_prefs.openchild_marginbottom());
00720   }
00724   void openchild_marginbottom(int val) {
00725     _prefs.openchild_marginbottom(val);
00726     redraw();
00727   }
00731   int connectorwidth() const {
00732     return(_prefs.connectorwidth());
00733   }
00737   void connectorwidth(int val) {
00738     _prefs.connectorwidth(val);
00739     redraw();
00740   }
00745   Fl_Image *usericon() const {
00746     return(_prefs.usericon());
00747   }
00757   void usericon(Fl_Image *val) {
00758     _prefs.usericon(val);
00759     redraw();
00760   }
00765   Fl_Image *openicon() const {
00766     return(_prefs.openicon());
00767   }
00773   void openicon(Fl_Image *val) {
00774     _prefs.openicon(val);
00775     redraw();
00776   }
00781   Fl_Image *closeicon() const {
00782     return(_prefs.closeicon());
00783   }
00789   void closeicon(Fl_Image *val) {
00790     _prefs.closeicon(val);
00791     redraw();
00792   }
00794   int showcollapse() const {
00795     return(_prefs.showcollapse());
00796   }
00805   void showcollapse(int val) {
00806     _prefs.showcollapse(val);
00807     redraw();
00808   }
00810   int showroot() const {
00811     return(_prefs.showroot());
00812   }
00817   void showroot(int val) {
00818     _prefs.showroot(val);
00819     redraw();
00820   }
00822   Fl_Tree_Connector connectorstyle() const {
00823     return(_prefs.connectorstyle());
00824   }
00826   void connectorstyle(Fl_Tree_Connector val) {
00827     _prefs.connectorstyle(val);
00828     redraw();
00829   }
00833   Fl_Tree_Sort sortorder() const {
00834     return(_prefs.sortorder());
00835   }
00837   void sortorder(Fl_Tree_Sort val) {
00838     _prefs.sortorder(val);
00839     // no redraw().. only affects new add()itions
00840   }
00845   Fl_Boxtype selectbox() const {
00846     return(_prefs.selectbox());
00847   }
00852   void selectbox(Fl_Boxtype val) {
00853     _prefs.selectbox(val);
00854     redraw();
00855   }
00857   Fl_Tree_Select selectmode() const {
00858     return(_prefs.selectmode());
00859   }
00861   void selectmode(Fl_Tree_Select val) {
00862     _prefs.selectmode(val);
00863   }
00864   int displayed(Fl_Tree_Item *item);
00865   void show_item(Fl_Tree_Item *item, int yoff);
00866   void show_item(Fl_Tree_Item *item);
00867   void show_item_bottom(Fl_Tree_Item *item);
00868   void show_item_middle(Fl_Tree_Item *item);
00869   void show_item_top(Fl_Tree_Item *item);
00870   void display(Fl_Tree_Item *item);
00871   int  vposition() const;
00872   void vposition(int ypos);
00873 
00886   int is_scrollbar(Fl_Widget *w) {
00887       return( ( w == _vscroll ) ? 1 : 0 );
00888   }
00897   int scrollbar_size() const {
00898       return(_scrollbar_size);
00899   }
00918   void scrollbar_size(int size) {
00919       _scrollbar_size = size;
00920       int scrollsize = _scrollbar_size ? _scrollbar_size : Fl::scrollbar_size();
00921       if ( _vscroll->w() != scrollsize ) {
00922         _vscroll->resize(x()+w()-scrollsize, h(), scrollsize, _vscroll->h());
00923       }
00924   }   
00925 
00927   // callback related
00929 
00933   void callback_item(Fl_Tree_Item* item) {
00934     _callback_item = item;
00935   }
00939   Fl_Tree_Item* callback_item() {
00940     return(_callback_item);
00941   }
00945   void callback_reason(Fl_Tree_Reason reason) {
00946     _callback_reason = reason;
00947   }
00964   Fl_Tree_Reason callback_reason() const {
00965     return(_callback_reason);
00966   }
00967 
00969   void load(class Fl_Preferences&);
00970 };
00971 
00972 #endif /*FL_TREE_H*/
00973 
00974 //
00975 // End of "$Id$".
00976 //