FLTK 1.3.0
Fl_Tree_Item.H
Go to the documentation of this file.
00001 //
00002 // "$Id$"
00003 //
00004 
00005 #ifndef FL_TREE_ITEM_H
00006 #define FL_TREE_ITEM_H
00007 
00008 #include <FL/Fl.H>
00009 #include <FL/Fl_Widget.H>
00010 #include <FL/Fl_Image.H>
00011 #include <FL/fl_draw.H>
00012 
00013 #include <FL/Fl_Tree_Item_Array.H>
00014 #include <FL/Fl_Tree_Prefs.H>
00015 
00017 // FL/Fl_Tree_Item.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. Distribution and use rights are outlined in
00024 // the file "COPYING" which should have been included with this file.  If this
00025 // file is missing or damaged, see the license at:
00026 //
00027 //     http://www.fltk.org/COPYING.php
00028 //
00029 // Please report all bugs and problems on the following page:
00030 //
00031 //     http://www.fltk.org/str.php
00032 //
00033 
00038 
00054 class FL_EXPORT Fl_Tree_Item {
00055   const char             *_label;               // label (memory managed)
00056   Fl_Font                 _labelfont;           // label's font face
00057   Fl_Fontsize             _labelsize;           // label's font size
00058   Fl_Color                _labelfgcolor;        // label's fg color
00059   Fl_Color                _labelbgcolor;        // label's bg color (0xffffffff is 'transparent')
00060   enum {
00061     OPEN                = 1<<0,         
00062     VISIBLE             = 1<<1,         
00063     ACTIVE              = 1<<2,         
00064     SELECTED            = 1<<3,         
00065   };
00066 #if FLTK_ABI_VERSION >= 10302
00067   // NEW
00068   unsigned short _flags;                // misc flags
00069 #else /*FLTK_ABI_VERSION*/
00070   // OLD: this will go away after 1.3.x
00071   char                    _open;                // item is open?
00072   char                    _visible;             // item is visible?
00073   char                    _active;              // item activated?
00074   char                    _selected;            // item selected?
00075 #endif /*FLTK_ABI_VERSION*/
00076   int                     _xywh[4];             // xywh of this widget (if visible)
00077   int                     _collapse_xywh[4];    // xywh of collapse icon (if visible)
00078   int                     _label_xywh[4];       // xywh of label
00079   Fl_Widget              *_widget;              // item's label widget (optional)
00080   Fl_Image               *_usericon;            // item's user-specific icon (optional)
00081   Fl_Tree_Item_Array      _children;            // array of child items
00082   Fl_Tree_Item           *_parent;              // parent item (=0 if root)
00083   void                   *_userdata;            // user data that can be associated with an item
00084 #if FLTK_ABI_VERSION >= 10302
00085   Fl_Tree_Item           *_prev_sibling;        // previous sibling (same level)
00086   Fl_Tree_Item           *_next_sibling;        // next sibling (same level)
00087 #endif /*FLTK_ABI_VERSION*/
00088 protected:
00089   void show_widgets();
00090   void hide_widgets();
00091   void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
00092   void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
00093 public:
00094   Fl_Tree_Item(const Fl_Tree_Prefs &prefs);     // CTOR
00095   ~Fl_Tree_Item();                              // DTOR
00096   Fl_Tree_Item(const Fl_Tree_Item *o);          // COPY CTOR
00097   int x() const { return(_xywh[0]); }
00098   int y() const { return(_xywh[1]); }
00099   int w() const { return(_xywh[2]); }
00100   int h() const { return(_xywh[3]); }
00101   int calc_item_height(const Fl_Tree_Prefs &prefs) const;
00102   void draw(int X, int &Y, int W, Fl_Widget *tree, Fl_Tree_Item *itemfocus, const Fl_Tree_Prefs &prefs, int lastchild=1);
00103   void show_self(const char *indent = "") const;
00104   void label(const char *val);
00105   const char *label() const;
00106 
00108   inline void user_data( void* data ) { _userdata = data; }
00109 
00111   inline void* user_data() const { return _userdata; }
00112   
00114   void labelfont(Fl_Font val) {
00115     _labelfont = val; 
00116   }
00118   Fl_Font labelfont() const {
00119     return(_labelfont);
00120   }
00122   void labelsize(Fl_Fontsize val) {
00123     _labelsize = val; 
00124   }
00126   Fl_Fontsize labelsize() const {
00127     return(_labelsize);
00128   }
00130   void labelfgcolor(Fl_Color val) {
00131     _labelfgcolor = val; 
00132   }
00134   void labelcolor(Fl_Color val) {
00135     _labelfgcolor = val; 
00136   }
00138   Fl_Color labelcolor() const {
00139     return(_labelfgcolor); 
00140   }
00142   Fl_Color labelfgcolor() const {
00143     return(_labelfgcolor); 
00144   }
00147   void labelbgcolor(Fl_Color val) {
00148     _labelbgcolor = val; 
00149   }
00152   Fl_Color labelbgcolor() const {
00153     return(_labelbgcolor); 
00154   }
00156   void widget(Fl_Widget *val) {
00157     _widget = val; 
00158   }
00160   Fl_Widget *widget() const {
00161     return(_widget); 
00162   }
00164   int children() const {
00165     return(_children.total()); 
00166   }
00168   Fl_Tree_Item *child(int index) {
00169     return(_children[index]); 
00170   }
00172   const Fl_Tree_Item *child(int t) const;
00174   int has_children() const {
00175     return(children()); 
00176   }
00177   int find_child(const char *name);
00178   int find_child(Fl_Tree_Item *item);
00179   int remove_child(Fl_Tree_Item *item);
00180   int remove_child(const char *new_label);
00181   void clear_children();
00182   void swap_children(int ax, int bx);
00183   int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
00184   const Fl_Tree_Item *find_child_item(char **arr) const;        // const
00185         Fl_Tree_Item *find_child_item(char **arr);              // non-const
00186   const Fl_Tree_Item *find_item(char **arr) const;              // const
00187         Fl_Tree_Item *find_item(char **arr);                    // non-const
00189   // Adding items
00191   Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs, const char *new_label);
00192   Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs, char **arr);
00193   Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
00194   Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
00195   int depth() const;
00196   Fl_Tree_Item *prev();
00197   Fl_Tree_Item *next();
00198   Fl_Tree_Item *next_sibling();
00199   Fl_Tree_Item *prev_sibling();
00200   void update_prev_next(int index);
00201   Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs);
00202   Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs);
00203   
00205   Fl_Tree_Item *parent() {
00206     return(_parent);
00207   }
00209   const Fl_Tree_Item *parent() const {
00210     return(_parent);
00211   }
00215   void parent(Fl_Tree_Item *val) {
00216     _parent = val;
00217   }
00219   // State
00221   void open();
00222   void close();
00224   int is_open() const {
00225     return(is_flag(OPEN));
00226   }
00228   int is_close() const {
00229     return(is_flag(OPEN)?0:1);
00230   }
00232   void open_toggle() {
00233     is_open()?close():open();
00234   }
00238   void select(int val=1) {
00239     set_flag(SELECTED, val);
00240   }
00242   void select_toggle() {
00243     if ( is_selected() ) {
00244       deselect();       // deselect if selected
00245     } else {
00246       select();         // select if deselected
00247     }
00248   }
00253   int select_all() {
00254     int count = 0;
00255     if ( ! is_selected() ) {
00256       select();
00257       ++count;
00258     }
00259     for ( int t=0; t<children(); t++ ) {
00260       count += child(t)->select_all();
00261     }
00262     return(count);
00263   }
00265   void deselect() {
00266     set_flag(SELECTED, 0);
00267   }
00272   int deselect_all() {
00273     int count = 0;
00274     if ( is_selected() ) {
00275       deselect();
00276       ++count;
00277     }
00278     for ( int t=0; t<children(); t++ ) {
00279       count += child(t)->deselect_all();
00280     }
00281     return(count);
00282   }
00284   char is_selected() const {
00285     return(is_flag(SELECTED));
00286   }
00296   void activate(int val=1) {
00297     set_flag(ACTIVE,val);
00298     if ( _widget && val != (int)_widget->active() ) {
00299       if ( val ) {
00300         _widget->activate();
00301       } else {
00302         _widget->deactivate();
00303       }
00304       _widget->redraw();
00305     }
00306   }
00310   void deactivate() {
00311     activate(0);
00312   }
00314   char is_activated() const {
00315     return(is_flag(ACTIVE));
00316   }
00318   char is_active() const {
00319     return(is_activated());
00320   }
00322   int visible() const {
00323     return(is_visible());
00324   }
00326   int is_visible() const {
00327     return(is_flag(VISIBLE));
00328   }
00329   int visible_r() const;
00330 
00332   void usericon(Fl_Image *val) {
00333     _usericon = val;
00334   }
00336   Fl_Image *usericon() const {
00337     return(_usericon);
00338   }
00340   // Events
00342   const Fl_Tree_Item *find_clicked(const Fl_Tree_Prefs &prefs) const;
00343   Fl_Tree_Item *find_clicked(const Fl_Tree_Prefs &prefs);
00344   int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
00345   int event_on_label(const Fl_Tree_Prefs &prefs) const;
00347   int is_root() const {
00348     return(_parent==0?1:0);
00349   }
00350 
00351   // Protected methods
00352 protected:
00353 #if FLTK_ABI_VERSION >= 10302
00354 
00355   inline void set_flag(unsigned short flag,int val) {
00356     if ( val ) _flags |= flag; else _flags &= ~flag;
00357   }
00359   inline int is_flag(unsigned short val) const {
00360     return(_flags & val ? 1 : 0);
00361   }
00362 #else /*FLTK_ABI_VERSION*/
00363 
00364   void set_flag(unsigned short flag,int val) {
00365     switch (flag) {
00366       case     OPEN: _open     = val; break;
00367       case  VISIBLE: _visible  = val; break;
00368       case   ACTIVE: _active   = val; break;
00369       case SELECTED: _selected = val; break;
00370     }
00371   }
00373   int is_flag(unsigned short flag) const {
00374     switch (flag) {
00375       case     OPEN: return(_open ? 1 : 0);
00376       case  VISIBLE: return(_visible ? 1 : 0);
00377       case   ACTIVE: return(_active ? 1 : 0);
00378       case SELECTED: return(_selected ? 1 : 0);
00379       default:       return(0);
00380     }
00381   }
00382 #endif /*FLTK_ABI_VERSION*/
00383 
00384 };
00385 
00386 #endif /*FL_TREE_ITEM_H*/
00387 
00388 //
00389 // End of "$Id$".
00390 //