• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

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 
00066 class Fl_Tree;
00067 class FL_EXPORT Fl_Tree_Item {
00068 #if FLTK_ABI_VERSION >= 10303
00069   Fl_Tree                *_tree;                // parent tree
00070 #endif
00071   const char             *_label;               // label (memory managed)
00072   Fl_Font                 _labelfont;           // label's font face
00073   Fl_Fontsize             _labelsize;           // label's font size
00074   Fl_Color                _labelfgcolor;        // label's fg color
00075   Fl_Color                _labelbgcolor;        // label's bg color (0xffffffff is 'transparent')
00076 #if FLTK_ABI_VERSION >= 10303
00078   enum Fl_Tree_Item_Flags {
00079 #else
00081   enum {
00082 #endif
00083     OPEN                = 1<<0,         
00084     VISIBLE             = 1<<1,         
00085     ACTIVE              = 1<<2,         
00086     SELECTED            = 1<<3          
00087   };
00088 #if FLTK_ABI_VERSION >= 10301
00089   // NEW
00090   unsigned short _flags;                // misc flags
00091 #else /*FLTK_ABI_VERSION*/
00092   // OLD: this will go away after 1.3.x
00093   char                    _open;                // item is open?
00094   char                    _visible;             // item is visible?
00095   char                    _active;              // item activated?
00096   char                    _selected;            // item selected?
00097 #endif /*FLTK_ABI_VERSION*/
00098   int                     _xywh[4];             // xywh of this widget (if visible)
00099   int                     _collapse_xywh[4];    // xywh of collapse icon (if visible)
00100   int                     _label_xywh[4];       // xywh of label
00101   Fl_Widget              *_widget;              // item's label widget (optional)
00102   Fl_Image               *_usericon;            // item's user-specific icon (optional)
00103 #if FLTK_ABI_VERSION >= 10304
00104   Fl_Image               *_userdeicon;          // deactivated usericon
00105 #endif
00106   Fl_Tree_Item_Array      _children;            // array of child items
00107   Fl_Tree_Item           *_parent;              // parent item (=0 if root)
00108   void                   *_userdata;            // user data that can be associated with an item
00109 #if FLTK_ABI_VERSION >= 10301
00110   Fl_Tree_Item           *_prev_sibling;        // previous sibling (same level)
00111   Fl_Tree_Item           *_next_sibling;        // next sibling (same level)
00112 #endif /*FLTK_ABI_VERSION*/
00113   // Protected methods
00114 protected:
00115   void _Init(const Fl_Tree_Prefs &prefs, Fl_Tree *tree);
00116   void show_widgets();
00117   void hide_widgets();
00118   void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
00119   void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
00120   void recalc_tree();
00121   int calc_item_height(const Fl_Tree_Prefs &prefs) const;
00122 #if FLTK_ABI_VERSION >= 10303
00123   Fl_Color drawfgcolor() const;
00124   Fl_Color drawbgcolor() const;
00125 #endif
00126 
00127 public:
00128   Fl_Tree_Item(const Fl_Tree_Prefs &prefs);     // CTOR -- backwards compatible
00129 #if FLTK_ABI_VERSION >= 10303
00130   Fl_Tree_Item(Fl_Tree *tree);                  // CTOR -- ABI 1.3.3+
00131   virtual ~Fl_Tree_Item();                      // DTOR -- ABI 1.3.3+
00132 #else
00133   ~Fl_Tree_Item();                              // DTOR -- backwards compatible
00134 #endif
00135   Fl_Tree_Item(const Fl_Tree_Item *o);          // COPY CTOR
00137   int x() const { return(_xywh[0]); }
00139   int y() const { return(_xywh[1]); }
00142   int w() const { return(_xywh[2]); }
00144   int h() const { return(_xywh[3]); }
00147   int label_x() const { return(_label_xywh[0]); }
00150   int label_y() const { return(_label_xywh[1]); }
00154   int label_w() const { return(_label_xywh[2]); }
00157   int label_h() const { return(_label_xywh[3]); }
00158 #if FLTK_ABI_VERSION >= 10303
00159   virtual int draw_item_content(int render);
00160   void draw(int X, int &Y, int W, Fl_Tree_Item *itemfocus, 
00161             int &tree_item_xmax, int lastchild=1, int render=1);
00162 #else
00163   void draw(int X, int &Y, int W, Fl_Widget *tree, 
00164             Fl_Tree_Item *itemfocus, const Fl_Tree_Prefs &prefs, int lastchild=1);
00165 #endif
00166   void show_self(const char *indent = "") const;
00167   void label(const char *val);
00168   const char *label() const;
00169 
00171   inline void user_data( void* data ) { _userdata = data; }
00172 
00174   inline void* user_data() const { return _userdata; }
00175   
00177   void labelfont(Fl_Font val) {
00178     _labelfont = val; 
00179     recalc_tree();              // may change tree geometry
00180   }
00182   Fl_Font labelfont() const {
00183     return(_labelfont);
00184   }
00186   void labelsize(Fl_Fontsize val) {
00187     _labelsize = val; 
00188     recalc_tree();              // may change tree geometry
00189   }
00191   Fl_Fontsize labelsize() const {
00192     return(_labelsize);
00193   }
00195   void labelfgcolor(Fl_Color val) {
00196     _labelfgcolor = val; 
00197   }
00199   Fl_Color labelfgcolor() const {
00200     return(_labelfgcolor); 
00201   }
00203   void labelcolor(Fl_Color val) {
00204      labelfgcolor(val);
00205   }
00207   Fl_Color labelcolor() const {
00208     return labelfgcolor(); 
00209   }
00212   void labelbgcolor(Fl_Color val) {
00213     _labelbgcolor = val; 
00214   }
00219   Fl_Color labelbgcolor() const {
00220     return(_labelbgcolor); 
00221   }
00223   void widget(Fl_Widget *val) {
00224     _widget = val; 
00225     recalc_tree();              // may change tree geometry
00226   }
00228   Fl_Widget *widget() const {
00229     return(_widget); 
00230   }
00232   int children() const {
00233     return(_children.total()); 
00234   }
00236   Fl_Tree_Item *child(int index) {
00237     return(_children[index]); 
00238   }
00240   const Fl_Tree_Item *child(int t) const;
00242   int has_children() const {
00243     return(children()); 
00244   }
00245   int find_child(const char *name);
00246   int find_child(Fl_Tree_Item *item);
00247   int remove_child(Fl_Tree_Item *item);
00248   int remove_child(const char *new_label);
00249   void clear_children();
00250   void swap_children(int ax, int bx);
00251   int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
00252   const Fl_Tree_Item *find_child_item(const char *name) const;
00253         Fl_Tree_Item *find_child_item(const char *name);
00254   const Fl_Tree_Item *find_child_item(char **arr) const;
00255         Fl_Tree_Item *find_child_item(char **arr);
00256   const Fl_Tree_Item *find_item(char **arr) const;
00257         Fl_Tree_Item *find_item(char **arr);
00259   // Adding items
00261   Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
00262                     const char *new_label,
00263                     Fl_Tree_Item *newitem);
00264   Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
00265                     const char *new_label);
00266   Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
00267                     char **arr,
00268                     Fl_Tree_Item *newitem);
00269   Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
00270                     char **arr);
00271 #if FLTK_ABI_VERSION >= 10303
00272   Fl_Tree_Item *replace(Fl_Tree_Item *new_item);
00273   Fl_Tree_Item *replace_child(Fl_Tree_Item *olditem, Fl_Tree_Item *newitem);
00274 #endif
00275   Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
00276   Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
00277   Fl_Tree_Item* deparent(int index);
00278   int reparent(Fl_Tree_Item *newchild, int index);
00279   int move(int to, int from);
00280   int move(Fl_Tree_Item *item, int op=0, int pos=0);
00281   int move_above(Fl_Tree_Item *item);
00282   int move_below(Fl_Tree_Item *item);
00283   int move_into(Fl_Tree_Item *item, int pos=0);
00284   int depth() const;
00285   Fl_Tree_Item *prev();
00286   Fl_Tree_Item *next();
00287   Fl_Tree_Item *next_sibling();
00288   Fl_Tree_Item *prev_sibling();
00289   void update_prev_next(int index);
00290   Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs);   // deprecated
00291   Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs);   // deprecated
00292   Fl_Tree_Item *next_visible(Fl_Tree_Prefs &prefs);
00293   Fl_Tree_Item *prev_visible(Fl_Tree_Prefs &prefs);
00294   
00296   Fl_Tree_Item *parent() {
00297     return(_parent);
00298   }
00300   const Fl_Tree_Item *parent() const {
00301     return(_parent);
00302   }
00306   void parent(Fl_Tree_Item *val) {
00307     _parent = val;
00308   }
00309 #if FLTK_ABI_VERSION >= 10303
00310   const Fl_Tree_Prefs& prefs() const;
00313   const Fl_Tree *tree() const {
00314     return(_tree);
00315   }
00316 #endif
00317 #if FLTK_ABI_VERSION >= 10304
00320   Fl_Tree *tree() {
00321     return(_tree);
00322   }
00323 #endif
00325   // State
00327   void open();
00328   void close();
00330   int is_open() const {
00331     return(is_flag(OPEN));
00332   }
00334   int is_close() const {
00335     return(is_flag(OPEN)?0:1);
00336   }
00338   void open_toggle() {
00339     is_open()?close():open();   // handles calling recalc_tree()
00340   }
00344   void select(int val=1) {
00345     set_flag(SELECTED, val);
00346   }
00348   void select_toggle() {
00349     if ( is_selected() ) {
00350       deselect();       // deselect if selected
00351     } else {
00352       select();         // select if deselected
00353     }
00354   }
00359   int select_all() {
00360     int count = 0;
00361     if ( ! is_selected() ) {
00362       select();
00363       ++count;
00364     }
00365     for ( int t=0; t<children(); t++ ) {
00366       count += child(t)->select_all();
00367     }
00368     return(count);
00369   }
00371   void deselect() {
00372     set_flag(SELECTED, 0);
00373   }
00378   int deselect_all() {
00379     int count = 0;
00380     if ( is_selected() ) {
00381       deselect();
00382       ++count;
00383     }
00384     for ( int t=0; t<children(); t++ ) {
00385       count += child(t)->deselect_all();
00386     }
00387     return(count);
00388   }
00390   char is_selected() const {
00391     return(is_flag(SELECTED));
00392   }
00401   void activate(int val=1) {
00402     set_flag(ACTIVE,val);
00403     if ( _widget && val != (int)_widget->active() ) {
00404       if ( val ) {
00405         _widget->activate();
00406       } else {
00407         _widget->deactivate();
00408       }
00409       _widget->redraw();
00410     }
00411   }
00415   void deactivate() {
00416     activate(0);
00417   }
00419   char is_activated() const {
00420     return(is_flag(ACTIVE));
00421   }
00423   char is_active() const {
00424     return(is_activated());
00425   }
00427   int visible() const {
00428     return(is_visible());
00429   }
00431   int is_visible() const {
00432     return(is_flag(VISIBLE));
00433   }
00434   int visible_r() const;
00435 
00445   void usericon(Fl_Image *val) {
00446     _usericon = val;
00447     recalc_tree();              // may change tree geometry
00448   }
00450   Fl_Image *usericon() const {
00451     return(_usericon);
00452   }
00479 #if FLTK_ABI_VERSION >= 10304
00480   void userdeicon(Fl_Image* val) {
00481     _userdeicon = val;
00482   }
00485   Fl_Image* userdeicon() const {
00486     return _userdeicon;
00487   }
00488 #endif
00490   // Events
00492 #if FLTK_ABI_VERSION >= 10303
00493   const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0) const;
00494   Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0);
00495 #else
00496   const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs) const;
00497   Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs);
00498 #endif
00499   int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
00500   int event_on_label(const Fl_Tree_Prefs &prefs) const;
00502   int is_root() const {
00503     return(_parent==0?1:0);
00504   }
00505 
00506   // Protected methods
00507   // TODO: move these to top 'protected:' section
00508 protected:
00509 #if FLTK_ABI_VERSION >= 10301
00511   inline void set_flag(unsigned short flag,int val) {
00512     if ( flag==OPEN || flag==VISIBLE ) {
00513       recalc_tree();            // may change tree geometry
00514     }
00515     if ( val ) _flags |= flag; else _flags &= ~flag;
00516   }
00518   inline int is_flag(unsigned short val) const {
00519     return(_flags & val ? 1 : 0);
00520   }
00521 #else /*FLTK_ABI_VERSION*/
00523   void set_flag(unsigned short flag,int val) {
00524     switch (flag) {
00525       case     OPEN: _open     = val; break;
00526       case  VISIBLE: _visible  = val; break;
00527       case   ACTIVE: _active   = val; break;
00528       case SELECTED: _selected = val; break;
00529     }
00530   }
00532   int is_flag(unsigned short flag) const {
00533     switch (flag) {
00534       case     OPEN: return(_open ? 1 : 0);
00535       case  VISIBLE: return(_visible ? 1 : 0);
00536       case   ACTIVE: return(_active ? 1 : 0);
00537       case SELECTED: return(_selected ? 1 : 0);
00538       default:       return(0);
00539     }
00540   }
00541 #endif /*FLTK_ABI_VERSION*/
00542 
00543 };
00544 
00545 #endif /*FL_TREE_ITEM_H*/
00546 
00547 //
00548 // End of "$Id$".
00549 //
  • © 1998-2016 by Bill Spitzak and others.     FLTK

  • © 1998-2016 by Bill Spitzak and others.     FLTK

    Permission is granted to reproduce this manual or any portion for any purpose, provided this copyright and permission notice are preserved.