00001 #ifndef FL_TREE_H 00002 #define FL_TREE_H 00003 00004 #include <FL/Fl.H> 00005 #include <FL/Fl_Group.H> 00006 #include <FL/Fl_Scrollbar.H> 00007 #include <FL/fl_draw.H> 00008 00009 #include <FL/Fl_Tree_Item.H> 00010 #include <FL/Fl_Tree_Prefs.H> 00011 00012 //INSERT-LGPL-HEADER-HERE// 00013 00018 00077 00078 class Fl_Tree : public Fl_Group { 00079 Fl_Tree_Item *_root; // can be null! 00080 Fl_Tree_Item *_item_clicked; 00081 Fl_Tree_Prefs _prefs; // all the tree's settings 00082 Fl_Scrollbar *_vscroll; 00083 00084 protected: 00096 const Fl_Tree_Item *find_clicked() const { 00097 if ( ! _root ) return(0); 00098 return(_root->find_clicked()); 00099 } 00104 void item_clicked(Fl_Tree_Item* val) { 00105 _item_clicked = val; 00106 } 00107 00108 public: 00109 Fl_Tree(int X, int Y, int W, int H, const char *L=0); 00110 ~Fl_Tree(); 00111 int handle(int e); 00112 void draw(); 00113 00115 // root methods 00117 00122 void root_label(const char *new_label) { 00123 if ( ! _root ) return; 00124 _root->label(new_label); 00125 } 00127 Fl_Tree_Item* root() { 00128 return(_root); 00129 } 00130 00132 // Item creation/removal methods 00134 Fl_Tree_Item *add(const char *path); 00135 Fl_Tree_Item *insert_above(Fl_Tree_Item *above, const char *name); 00136 00141 int remove(Fl_Tree_Item *item) { 00142 if ( !item ) return(0); 00143 if ( item == _root ) { 00144 clear(); 00145 } else { 00146 Fl_Tree_Item *parent = item->parent(); // find item's parent 00147 if ( ! parent ) return(-1); 00148 parent->remove_child(item); // remove child + children 00149 } 00150 return(0); 00151 } 00155 void clear() { 00156 if ( ! _root ) return; 00157 _root->clear_children(); 00158 delete _root; _root = 0; 00159 } 00161 void clear_children(Fl_Tree_Item *item) { 00162 if ( item->has_children() ) { 00163 item->clear_children(); 00164 redraw(); // redraw only if there were children to clear 00165 } 00166 } 00167 00169 // Item lookup methods 00171 Fl_Tree_Item *find_item(const char *path); 00172 const Fl_Tree_Item *find_item(const char *path) const; 00173 00178 Fl_Tree_Item *parent(Fl_Tree_Item *item) { 00179 return(item->parent()); 00180 } 00187 Fl_Tree_Item *item_clicked() { 00188 return(_item_clicked); 00189 } 00201 Fl_Tree_Item *first() { 00202 return(_root); // first item always root 00203 } 00216 Fl_Tree_Item *last() { 00217 if ( ! _root ) return(0); 00218 Fl_Tree_Item *item = _root; 00219 while ( item->has_children() ) { 00220 item = item->child(item->children()-1); 00221 } 00222 return(item); 00223 } 00224 00226 // Item open/close methods 00228 00233 void open(Fl_Tree_Item *item) { 00234 if ( ! item->is_open() ) { 00235 item->open(); 00236 redraw(); 00237 } 00238 } 00247 int open(const char *path) { 00248 Fl_Tree_Item *item = find_item(path); 00249 if ( item ) { 00250 open(item); 00251 return(0); 00252 } 00253 return(-1); 00254 } 00258 void close(Fl_Tree_Item *item) { 00259 if ( ! item->is_close() ) { 00260 item->close(); 00261 redraw(); 00262 } 00263 } 00272 int close(const char *path) { 00273 Fl_Tree_Item *item = find_item(path); 00274 if ( item ) { 00275 close(item); 00276 return(0); 00277 } 00278 return(-1); 00279 } 00289 int is_open(Fl_Tree_Item *item) const { 00290 return(item->is_open()?1:0); 00291 } 00302 int is_open(const char *path) const { 00303 const Fl_Tree_Item *item = find_item(path); 00304 if ( item ) return(item->is_open()?1:0); 00305 return(-1); 00306 } 00312 int is_close(Fl_Tree_Item *item) const { 00313 return(item->is_close()); 00314 } 00322 int is_close(const char *path) const { 00323 const Fl_Tree_Item *item = find_item(path); 00324 if ( item ) return(item->is_close()?1:0); 00325 return(-1); 00326 } 00327 00329 // Item selection methods 00331 00335 void select(Fl_Tree_Item *item) { 00336 if ( ! item->is_selected() ) { 00337 item->select(); 00338 redraw(); 00339 } 00340 } 00348 int select(const char *path) { 00349 Fl_Tree_Item *item = find_item(path); 00350 if ( item ) { 00351 select(item); 00352 return(0); 00353 } 00354 return(-1); 00355 } 00359 void select_toggle(Fl_Tree_Item *item) { 00360 item->select_toggle(); 00361 redraw(); 00362 } 00366 void deselect(Fl_Tree_Item *item) { 00367 if ( item->is_selected() ) { 00368 item->deselect(); 00369 redraw(); 00370 } 00371 } 00379 int deselect(const char *path) { 00380 Fl_Tree_Item *item = find_item(path); 00381 if ( item ) { 00382 deselect(item); 00383 return(0); 00384 } 00385 return(-1); 00386 } 00387 00388 int deselect_all(Fl_Tree_Item *item=0); 00389 int select_only(Fl_Tree_Item *selitem); 00390 00396 int is_selected(Fl_Tree_Item *item) const { 00397 return(item->is_selected()?1:0); 00398 } 00406 int is_selected(const char *path) { 00407 Fl_Tree_Item *item = find_item(path); 00408 if ( item ) return(is_selected(item)); 00409 return(-1); 00410 } 00414 void show_self() { 00415 if ( ! _root ) return; 00416 _root->show_self(); 00417 } 00418 00420 // Item attribute related methods 00422 00424 int labelsize() const { 00425 return(_prefs.labelsize()); 00426 } 00430 void labelsize(int val) { 00431 _prefs.labelsize(val); 00432 } 00433 00439 int labelfont() const { 00440 return(_prefs.labelfont()); 00441 } 00447 void labelfont(int val) { 00448 _prefs.labelfont(val); 00449 } 00453 int marginleft() const { 00454 return(_prefs.marginleft()); 00455 } 00459 void marginleft(int val) { 00460 _prefs.marginleft(val); 00461 redraw(); 00462 } 00466 int margintop() const { 00467 return(_prefs.margintop()); 00468 } 00472 void margintop(int val) { 00473 _prefs.margintop(val); 00474 redraw(); 00475 } 00479 int connectorwidth() const { 00480 return(_prefs.connectorwidth()); 00481 } 00485 void connectorwidth(int val) { 00486 _prefs.connectorwidth(val); 00487 redraw(); 00488 } 00492 Fl_Pixmap *usericon() const { 00493 return(_prefs.usericon()); 00494 } 00504 void usericon(Fl_Pixmap *val) { 00505 _prefs.usericon(val); 00506 redraw(); 00507 } 00512 Fl_Pixmap *openicon() const { 00513 return(_prefs.openicon()); 00514 } 00520 void openicon(Fl_Pixmap *val) { 00521 _prefs.openicon(val); 00522 redraw(); 00523 } 00528 Fl_Pixmap *closeicon() const { 00529 return(_prefs.closeicon()); 00530 } 00536 void closeicon(Fl_Pixmap *val) { 00537 _prefs.closeicon(val); 00538 redraw(); 00539 } 00541 int showcollapse() const { 00542 return(_prefs.showcollapse()); 00543 } 00552 void showcollapse(int val) { 00553 _prefs.showcollapse(val); 00554 redraw(); 00555 } 00557 int showroot() const { 00558 return(_prefs.showroot()); 00559 } 00564 void showroot(int val) { 00565 _prefs.showroot(val); 00566 redraw(); 00567 } 00569 Fl_Tree_Connector connectorstyle() const { 00570 return(_prefs.connectorstyle()); 00571 } 00573 void connectorstyle(Fl_Tree_Connector val) { 00574 _prefs.connectorstyle(val); 00575 redraw(); 00576 } 00580 Fl_Tree_Sort sortorder() const { 00581 return(_prefs.sortorder()); 00582 } 00584 void sortorder(Fl_Tree_Sort val) { 00585 _prefs.sortorder(val); 00586 // no redraw().. only affects new add()itions 00587 } 00592 Fl_Boxtype selectbox() const { 00593 return(_prefs.selectbox()); 00594 } 00599 void selectbox(Fl_Boxtype val) { 00600 _prefs.selectbox(val); 00601 redraw(); 00602 } 00604 Fl_Tree_Select selectmode() const { 00605 return(_prefs.selectmode()); 00606 } 00608 void selectmode(Fl_Tree_Select val) { 00609 _prefs.selectmode(val); 00610 } 00611 }; 00612 00613 #endif /*FL_TREE_H*/