FLTK 1.3.4
Fl_Tree_Item.H
Go to the documentation of this file.
1 //
2 // "$Id: Fl_Tree_Item.H 12034 2016-10-17 12:45:55Z AlbrechtS $"
3 //
4 
5 #ifndef FL_TREE_ITEM_H
6 #define FL_TREE_ITEM_H
7 
8 #include <FL/Fl.H>
9 #include <FL/Fl_Widget.H>
10 #include <FL/Fl_Image.H>
11 #include <FL/fl_draw.H>
12 
13 #include <FL/Fl_Tree_Item_Array.H>
14 #include <FL/Fl_Tree_Prefs.H>
15 
17 // FL/Fl_Tree_Item.H
19 //
20 // Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
21 // Copyright (C) 2009-2010 by Greg Ercolano.
22 //
23 // This library is free software. Distribution and use rights are outlined in
24 // the file "COPYING" which should have been included with this file. If this
25 // file is missing or damaged, see the license at:
26 //
27 // http://www.fltk.org/COPYING.php
28 //
29 // Please report all bugs and problems on the following page:
30 //
31 // http://www.fltk.org/str.php
32 //
33 
38 
66 class Fl_Tree;
67 class FL_EXPORT Fl_Tree_Item {
68 #if FLTK_ABI_VERSION >= 10303
69  Fl_Tree *_tree; // parent tree
70 #endif
71  const char *_label; // label (memory managed)
72  Fl_Font _labelfont; // label's font face
73  Fl_Fontsize _labelsize; // label's font size
74  Fl_Color _labelfgcolor; // label's fg color
75  Fl_Color _labelbgcolor; // label's bg color (0xffffffff is 'transparent')
76 #if FLTK_ABI_VERSION >= 10303
77  enum Fl_Tree_Item_Flags {
79 #else
80  enum {
82 #endif
83  OPEN = 1<<0,
84  VISIBLE = 1<<1,
85  ACTIVE = 1<<2,
86  SELECTED = 1<<3
87  };
88 #if FLTK_ABI_VERSION >= 10301
89  // NEW
90  unsigned short _flags; // misc flags
91 #else /*FLTK_ABI_VERSION*/
92  // OLD: this will go away after 1.3.x
93  char _open; // item is open?
94  char _visible; // item is visible?
95  char _active; // item activated?
96  char _selected; // item selected?
97 #endif /*FLTK_ABI_VERSION*/
98  int _xywh[4]; // xywh of this widget (if visible)
99  int _collapse_xywh[4]; // xywh of collapse icon (if visible)
100  int _label_xywh[4]; // xywh of label
101  Fl_Widget *_widget; // item's label widget (optional)
102  Fl_Image *_usericon; // item's user-specific icon (optional)
103 #if FLTK_ABI_VERSION >= 10304
104  Fl_Image *_userdeicon; // deactivated usericon
105 #endif
106  Fl_Tree_Item_Array _children; // array of child items
107  Fl_Tree_Item *_parent; // parent item (=0 if root)
108  void *_userdata; // user data that can be associated with an item
109 #if FLTK_ABI_VERSION >= 10301
110  Fl_Tree_Item *_prev_sibling; // previous sibling (same level)
111  Fl_Tree_Item *_next_sibling; // next sibling (same level)
112 #endif /*FLTK_ABI_VERSION*/
113  // Protected methods
114 protected:
115  void _Init(const Fl_Tree_Prefs &prefs, Fl_Tree *tree);
116  void show_widgets();
117  void hide_widgets();
118  void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
119  void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
120  void recalc_tree();
121  int calc_item_height(const Fl_Tree_Prefs &prefs) const;
122 #if FLTK_ABI_VERSION >= 10303
123  Fl_Color drawfgcolor() const;
124  Fl_Color drawbgcolor() const;
125 #endif
126 
127 public:
128  Fl_Tree_Item(const Fl_Tree_Prefs &prefs); // CTOR -- backwards compatible
129 #if FLTK_ABI_VERSION >= 10303
130  Fl_Tree_Item(Fl_Tree *tree); // CTOR -- ABI 1.3.3+
131  virtual ~Fl_Tree_Item(); // DTOR -- ABI 1.3.3+
132 #else
133  ~Fl_Tree_Item(); // DTOR -- backwards compatible
134 #endif
135  Fl_Tree_Item(const Fl_Tree_Item *o); // COPY CTOR
137  int x() const { return(_xywh[0]); }
139  int y() const { return(_xywh[1]); }
142  int w() const { return(_xywh[2]); }
144  int h() const { return(_xywh[3]); }
147  int label_x() const { return(_label_xywh[0]); }
150  int label_y() const { return(_label_xywh[1]); }
154  int label_w() const { return(_label_xywh[2]); }
157  int label_h() const { return(_label_xywh[3]); }
158 #if FLTK_ABI_VERSION >= 10303
159  virtual int draw_item_content(int render);
160  void draw(int X, int &Y, int W, Fl_Tree_Item *itemfocus,
161  int &tree_item_xmax, int lastchild=1, int render=1);
162 #else
163  void draw(int X, int &Y, int W, Fl_Widget *tree,
164  Fl_Tree_Item *itemfocus, const Fl_Tree_Prefs &prefs, int lastchild=1);
165 #endif
166  void show_self(const char *indent = "") const;
167  void label(const char *val);
168  const char *label() const;
169 
171  inline void user_data( void* data ) { _userdata = data; }
172 
174  inline void* user_data() const { return _userdata; }
175 
177  void labelfont(Fl_Font val) {
178  _labelfont = val;
179  recalc_tree(); // may change tree geometry
180  }
182  Fl_Font labelfont() const {
183  return(_labelfont);
184  }
186  void labelsize(Fl_Fontsize val) {
187  _labelsize = val;
188  recalc_tree(); // may change tree geometry
189  }
192  return(_labelsize);
193  }
195  void labelfgcolor(Fl_Color val) {
196  _labelfgcolor = val;
197  }
200  return(_labelfgcolor);
201  }
203  void labelcolor(Fl_Color val) {
204  labelfgcolor(val);
205  }
208  return labelfgcolor();
209  }
212  void labelbgcolor(Fl_Color val) {
213  _labelbgcolor = val;
214  }
220  return(_labelbgcolor);
221  }
223  void widget(Fl_Widget *val) {
224  _widget = val;
225  recalc_tree(); // may change tree geometry
226  }
228  Fl_Widget *widget() const {
229  return(_widget);
230  }
232  int children() const {
233  return(_children.total());
234  }
236  Fl_Tree_Item *child(int index) {
237  return(_children[index]);
238  }
240  const Fl_Tree_Item *child(int t) const;
242  int has_children() const {
243  return(children());
244  }
245  int find_child(const char *name);
246  int find_child(Fl_Tree_Item *item);
247  int remove_child(Fl_Tree_Item *item);
248  int remove_child(const char *new_label);
249  void clear_children();
250  void swap_children(int ax, int bx);
251  int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
252  const Fl_Tree_Item *find_child_item(const char *name) const;
253  Fl_Tree_Item *find_child_item(const char *name);
254  const Fl_Tree_Item *find_child_item(char **arr) const;
255  Fl_Tree_Item *find_child_item(char **arr);
256  const Fl_Tree_Item *find_item(char **arr) const;
257  Fl_Tree_Item *find_item(char **arr);
259  // Adding items
261  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
262  const char *new_label,
263  Fl_Tree_Item *newitem);
264  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
265  const char *new_label);
266  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
267  char **arr,
268  Fl_Tree_Item *newitem);
269  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
270  char **arr);
271 #if FLTK_ABI_VERSION >= 10303
272  Fl_Tree_Item *replace(Fl_Tree_Item *new_item);
273  Fl_Tree_Item *replace_child(Fl_Tree_Item *olditem, Fl_Tree_Item *newitem);
274 #endif
275  Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
276  Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
277  Fl_Tree_Item* deparent(int index);
278  int reparent(Fl_Tree_Item *newchild, int index);
279  int move(int to, int from);
280  int move(Fl_Tree_Item *item, int op=0, int pos=0);
281  int move_above(Fl_Tree_Item *item);
282  int move_below(Fl_Tree_Item *item);
283  int move_into(Fl_Tree_Item *item, int pos=0);
284  int depth() const;
285  Fl_Tree_Item *prev();
286  Fl_Tree_Item *next();
287  Fl_Tree_Item *next_sibling();
288  Fl_Tree_Item *prev_sibling();
289  void update_prev_next(int index);
290  Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs); // deprecated
291  Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs); // deprecated
292  Fl_Tree_Item *next_visible(Fl_Tree_Prefs &prefs);
293  Fl_Tree_Item *prev_visible(Fl_Tree_Prefs &prefs);
294 
297  return(_parent);
298  }
300  const Fl_Tree_Item *parent() const {
301  return(_parent);
302  }
306  void parent(Fl_Tree_Item *val) {
307  _parent = val;
308  }
309 #if FLTK_ABI_VERSION >= 10303
310  const Fl_Tree_Prefs& prefs() const;
313  const Fl_Tree *tree() const {
314  return(_tree);
315  }
316 #endif
317 #if FLTK_ABI_VERSION >= 10304
318  Fl_Tree *tree() {
321  return(_tree);
322  }
323 #endif
324  // State
327  void open();
328  void close();
330  int is_open() const {
331  return(is_flag(OPEN));
332  }
334  int is_close() const {
335  return(is_flag(OPEN)?0:1);
336  }
338  void open_toggle() {
339  is_open()?close():open(); // handles calling recalc_tree()
340  }
344  void select(int val=1) {
345  set_flag(SELECTED, val);
346  }
348  void select_toggle() {
349  if ( is_selected() ) {
350  deselect(); // deselect if selected
351  } else {
352  select(); // select if deselected
353  }
354  }
359  int select_all() {
360  int count = 0;
361  if ( ! is_selected() ) {
362  select();
363  ++count;
364  }
365  for ( int t=0; t<children(); t++ ) {
366  count += child(t)->select_all();
367  }
368  return(count);
369  }
371  void deselect() {
372  set_flag(SELECTED, 0);
373  }
378  int deselect_all() {
379  int count = 0;
380  if ( is_selected() ) {
381  deselect();
382  ++count;
383  }
384  for ( int t=0; t<children(); t++ ) {
385  count += child(t)->deselect_all();
386  }
387  return(count);
388  }
390  char is_selected() const {
391  return(is_flag(SELECTED));
392  }
401  void activate(int val=1) {
402  set_flag(ACTIVE,val);
403  if ( _widget && val != (int)_widget->active() ) {
404  if ( val ) {
405  _widget->activate();
406  } else {
407  _widget->deactivate();
408  }
409  _widget->redraw();
410  }
411  }
415  void deactivate() {
416  activate(0);
417  }
419  char is_activated() const {
420  return(is_flag(ACTIVE));
421  }
423  char is_active() const {
424  return(is_activated());
425  }
427  int visible() const {
428  return(is_visible());
429  }
431  int is_visible() const {
432  return(is_flag(VISIBLE));
433  }
434  int visible_r() const;
435 
445  void usericon(Fl_Image *val) {
446  _usericon = val;
447  recalc_tree(); // may change tree geometry
448  }
450  Fl_Image *usericon() const {
451  return(_usericon);
452  }
479 #if FLTK_ABI_VERSION >= 10304
480  void userdeicon(Fl_Image* val) {
481  _userdeicon = val;
482  }
485  Fl_Image* userdeicon() const {
486  return _userdeicon;
487  }
488 #endif
489  // Events
492 #if FLTK_ABI_VERSION >= 10303
493  const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0) const;
494  Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0);
495 #else
496  const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs) const;
497  Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs);
498 #endif
499  int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
500  int event_on_label(const Fl_Tree_Prefs &prefs) const;
502  int is_root() const {
503  return(_parent==0?1:0);
504  }
505 
506  // Protected methods
507  // TODO: move these to top 'protected:' section
508 protected:
509 #if FLTK_ABI_VERSION >= 10301
510  inline void set_flag(unsigned short flag,int val) {
512  if ( flag==OPEN || flag==VISIBLE ) {
513  recalc_tree(); // may change tree geometry
514  }
515  if ( val ) _flags |= flag; else _flags &= ~flag;
516  }
518  inline int is_flag(unsigned short val) const {
519  return(_flags & val ? 1 : 0);
520  }
521 #else /*FLTK_ABI_VERSION*/
522  void set_flag(unsigned short flag,int val) {
524  switch (flag) {
525  case OPEN: _open = val; break;
526  case VISIBLE: _visible = val; break;
527  case ACTIVE: _active = val; break;
528  case SELECTED: _selected = val; break;
529  }
530  }
532  int is_flag(unsigned short flag) const {
533  switch (flag) {
534  case OPEN: return(_open ? 1 : 0);
535  case VISIBLE: return(_visible ? 1 : 0);
536  case ACTIVE: return(_active ? 1 : 0);
537  case SELECTED: return(_selected ? 1 : 0);
538  default: return(0);
539  }
540  }
541 #endif /*FLTK_ABI_VERSION*/
542 
543 };
544 
545 #endif /*FL_TREE_ITEM_H*/
546 
547 //
548 // End of "$Id: Fl_Tree_Item.H 12034 2016-10-17 12:45:55Z AlbrechtS $".
549 //
Fl_Widget is the base class for all widgets in FLTK.
Definition: Fl_Widget.H:101
int is_root() const
Is this item the root of the tree?
Definition: Fl_Tree_Item.H:502
void deactivate()
Deactivates the widget.
Definition: Fl_Widget.cxx:253
void select(int val=1)
Change the item's selection state to the optionally specified 'val'.
Definition: Fl_Tree_Item.H:344
int label_h() const
The item's label height.
Definition: Fl_Tree_Item.H:157
int visible() const
See if the item is visible. Alias for is_visible().
Definition: Fl_Tree_Item.H:427
void redraw()
Schedules the drawing of the widget.
Definition: Fl.cxx:1766
Tree widget.
Definition: Fl_Tree.H:323
int deselect_all()
Deselect item and all its children.
Definition: Fl_Tree_Item.H:378
Fl static class.
void parent(Fl_Tree_Item *val)
Set the parent for this item.
Definition: Fl_Tree_Item.H:306
Fl_Color labelcolor() const
Return item's label text color. Alias for labelfgcolor() const).
Definition: Fl_Tree_Item.H:207
void deselect()
Disable the item's selection state.
Definition: Fl_Tree_Item.H:371
void userdeicon(Fl_Image *val)
Set the usericon to draw when the item is deactivated.
Definition: Fl_Tree_Item.H:480
Base class for image caching and drawing.
Definition: Fl_Image.H:55
Fl_Image, Fl_RGB_Image classes.
void user_data(void *data)
Set a user-data value for the item.
Definition: Fl_Tree_Item.H:171
int x() const
The item's x position relative to the window.
Definition: Fl_Tree_Item.H:137
void labelfont(Fl_Font val)
Set item's label font face.
Definition: Fl_Tree_Item.H:177
int select_all()
Select item and all its children.
Definition: Fl_Tree_Item.H:359
Fl_Widget * widget() const
Return FLTK widget assigned to this item.
Definition: Fl_Tree_Item.H:228
void labelsize(Fl_Fontsize val)
Set item's label font size.
Definition: Fl_Tree_Item.H:186
void labelbgcolor(Fl_Color val)
Set item's label background color.
Definition: Fl_Tree_Item.H:212
int is_flag(unsigned short val) const
See if flag set. Returns 0 or 1.
Definition: Fl_Tree_Item.H:518
void usericon(Fl_Image *val)
Set the item's user icon to an Fl_Image.
Definition: Fl_Tree_Item.H:445
void activate(int val=1)
Change the item's activation state to the optionally specified 'val'.
Definition: Fl_Tree_Item.H:401
Fl_Font labelfont() const
Get item's label font face.
Definition: Fl_Tree_Item.H:182
void select_toggle()
Toggle the item's selection state.
Definition: Fl_Tree_Item.H:348
char is_active() const
See if the item is activated. Alias for is_activated().
Definition: Fl_Tree_Item.H:423
Fl_Color labelbgcolor() const
Return item's label background text color.
Definition: Fl_Tree_Item.H:219
void activate()
Activates the widget.
Definition: Fl_Widget.cxx:241
Fl_Widget, Fl_Label classes .
int has_children() const
See if this item has children.
Definition: Fl_Tree_Item.H:242
unsigned int active() const
Returns whether the widget is active.
Definition: Fl_Widget.H:708
void labelcolor(Fl_Color val)
Set item's label text color. Alias for labelfgcolor(Fl_Color)).
Definition: Fl_Tree_Item.H:203
int w() const
The entire item's width to right edge of Fl_Tree's inner width within scrollbars. ...
Definition: Fl_Tree_Item.H:142
int y() const
The item's y position relative to the window.
Definition: Fl_Tree_Item.H:139
int label_x() const
The item's label x position relative to the window.
Definition: Fl_Tree_Item.H:147
int is_close() const
See if the item is 'closed'.
Definition: Fl_Tree_Item.H:334
Fl_Image * usericon() const
Get the item's user icon as an Fl_Image. Returns '0' if disabled.
Definition: Fl_Tree_Item.H:450
void open_toggle()
Toggle the item's open/closed state.
Definition: Fl_Tree_Item.H:338
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:906
Fl_Image * userdeicon() const
Return the deactivated version of the user icon, if any.
Definition: Fl_Tree_Item.H:485
Fl_Tree_Item * child(int index)
Return the child item for the given 'index'.
Definition: Fl_Tree_Item.H:236
const Fl_Tree * tree() const
Return the tree for this item.
Definition: Fl_Tree_Item.H:313
const Fl_Tree_Item * parent() const
Return the const parent for this item. Returns NULL if we are the root.
Definition: Fl_Tree_Item.H:300
utility header to pull drawing functions together
unsigned int Fl_Color
An FLTK color value; see also Colors.
Definition: Enumerations.H:934
Tree widget item.
Definition: Fl_Tree_Item.H:67
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:877
This file defines a class that manages an array of Fl_Tree_Item pointers.
int total() const
Return the total items in the array, or 0 if empty.
Definition: Fl_Tree_Item_Array.H:72
This file contains the definitions for Fl_Tree's preferences.
int label_y() const
The item's label y position relative to the window.
Definition: Fl_Tree_Item.H:150
int children() const
Return the number of children this item has.
Definition: Fl_Tree_Item.H:232
Manages an array of Fl_Tree_Item pointers.
Definition: Fl_Tree_Item_Array.H:47
Fl_Tree_Item * parent()
Return the parent for this item. Returns NULL if we are the root.
Definition: Fl_Tree_Item.H:296
int h() const
The item's height.
Definition: Fl_Tree_Item.H:144
Fl_Fontsize labelsize() const
Get item's label font size.
Definition: Fl_Tree_Item.H:191
int is_visible() const
See if the item is visible.
Definition: Fl_Tree_Item.H:431
Tree widget's preferences.
Definition: Fl_Tree_Prefs.H:111
void widget(Fl_Widget *val)
Assign an FLTK widget to this item.
Definition: Fl_Tree_Item.H:223
Fl_Color labelfgcolor() const
Return item's label foreground text color.
Definition: Fl_Tree_Item.H:199
void * user_data() const
Retrieve the user-data value that has been assigned to the item.
Definition: Fl_Tree_Item.H:174
char is_selected() const
See if the item is selected.
Definition: Fl_Tree_Item.H:390
void labelfgcolor(Fl_Color val)
Set item's label foreground text color.
Definition: Fl_Tree_Item.H:195
int label_w() const
The item's maximum label width to right edge of Fl_Tree's inner width within scrollbars.
Definition: Fl_Tree_Item.H:154
char is_activated() const
See if the item is activated.
Definition: Fl_Tree_Item.H:419
int is_open() const
See if the item is 'open'.
Definition: Fl_Tree_Item.H:330
void deactivate()
Deactivate the item; the callback() won't be invoked when clicked.
Definition: Fl_Tree_Item.H:415