FLTK 1.3.3
Fl_Tree_Item.H
Go to the documentation of this file.
1 //
2 // "$Id: Fl_Tree_Item.H 10271 2014-09-04 18:56:52Z greg.ercolano $"
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 
78  enum Fl_Tree_Item_Flags {
79 #else
80 
81  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  Fl_Tree_Item_Array _children; // array of child items
104  Fl_Tree_Item *_parent; // parent item (=0 if root)
105  void *_userdata; // user data that can be associated with an item
106 #if FLTK_ABI_VERSION >= 10301
107  Fl_Tree_Item *_prev_sibling; // previous sibling (same level)
108  Fl_Tree_Item *_next_sibling; // next sibling (same level)
109 #endif /*FLTK_ABI_VERSION*/
110  // Protected methods
111 protected:
112  void _Init(const Fl_Tree_Prefs &prefs, Fl_Tree *tree);
113  void show_widgets();
114  void hide_widgets();
115  void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
116  void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
117  void recalc_tree();
118  int calc_item_height(const Fl_Tree_Prefs &prefs) const;
119 #if FLTK_ABI_VERSION >= 10303
120  Fl_Color drawfgcolor() const;
121  Fl_Color drawbgcolor() const;
122 #endif
123 
124 public:
125  Fl_Tree_Item(const Fl_Tree_Prefs &prefs); // CTOR -- backwards compatible
126 #if FLTK_ABI_VERSION >= 10303
127  Fl_Tree_Item(Fl_Tree *tree); // CTOR -- ABI 1.3.3+
128  virtual ~Fl_Tree_Item(); // DTOR -- ABI 1.3.3+
129 #else
130  ~Fl_Tree_Item(); // DTOR -- backwards compatible
131 #endif
132  Fl_Tree_Item(const Fl_Tree_Item *o); // COPY CTOR
134  int x() const { return(_xywh[0]); }
136  int y() const { return(_xywh[1]); }
139  int w() const { return(_xywh[2]); }
141  int h() const { return(_xywh[3]); }
144  int label_x() const { return(_label_xywh[0]); }
147  int label_y() const { return(_label_xywh[1]); }
151  int label_w() const { return(_label_xywh[2]); }
154  int label_h() const { return(_label_xywh[3]); }
155 #if FLTK_ABI_VERSION >= 10303
156  virtual int draw_item_content(int render);
157  void draw(int X, int &Y, int W, Fl_Tree_Item *itemfocus,
158  int &tree_item_xmax, int lastchild=1, int render=1);
159 #else
160  void draw(int X, int &Y, int W, Fl_Widget *tree,
161  Fl_Tree_Item *itemfocus, const Fl_Tree_Prefs &prefs, int lastchild=1);
162 #endif
163  void show_self(const char *indent = "") const;
164  void label(const char *val);
165  const char *label() const;
166 
168  inline void user_data( void* data ) { _userdata = data; }
169 
171  inline void* user_data() const { return _userdata; }
172 
174  void labelfont(Fl_Font val) {
175  _labelfont = val;
176  recalc_tree(); // may change tree geometry
177  }
179  Fl_Font labelfont() const {
180  return(_labelfont);
181  }
183  void labelsize(Fl_Fontsize val) {
184  _labelsize = val;
185  recalc_tree(); // may change tree geometry
186  }
189  return(_labelsize);
190  }
192  void labelfgcolor(Fl_Color val) {
193  _labelfgcolor = val;
194  }
197  return(_labelfgcolor);
198  }
200  void labelcolor(Fl_Color val) {
201  labelfgcolor(val);
202  }
205  return labelfgcolor();
206  }
209  void labelbgcolor(Fl_Color val) {
210  _labelbgcolor = val;
211  }
217  return(_labelbgcolor);
218  }
220  void widget(Fl_Widget *val) {
221  _widget = val;
222  recalc_tree(); // may change tree geometry
223  }
225  Fl_Widget *widget() const {
226  return(_widget);
227  }
229  int children() const {
230  return(_children.total());
231  }
233  Fl_Tree_Item *child(int index) {
234  return(_children[index]);
235  }
237  const Fl_Tree_Item *child(int t) const;
239  int has_children() const {
240  return(children());
241  }
242  int find_child(const char *name);
243  int find_child(Fl_Tree_Item *item);
244  int remove_child(Fl_Tree_Item *item);
245  int remove_child(const char *new_label);
246  void clear_children();
247  void swap_children(int ax, int bx);
248  int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
249  const Fl_Tree_Item *find_child_item(const char *name) const;
250  Fl_Tree_Item *find_child_item(const char *name);
251  const Fl_Tree_Item *find_child_item(char **arr) const;
252  Fl_Tree_Item *find_child_item(char **arr);
253  const Fl_Tree_Item *find_item(char **arr) const;
254  Fl_Tree_Item *find_item(char **arr);
256  // Adding items
258  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
259  const char *new_label,
260  Fl_Tree_Item *newitem);
261  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
262  const char *new_label);
263  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
264  char **arr,
265  Fl_Tree_Item *newitem);
266  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
267  char **arr);
268 #if FLTK_ABI_VERSION >= 10303
269  Fl_Tree_Item *replace(Fl_Tree_Item *new_item);
270  Fl_Tree_Item *replace_child(Fl_Tree_Item *olditem, Fl_Tree_Item *newitem);
271 #endif
272  Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
273  Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
274  Fl_Tree_Item* deparent(int index);
275  int reparent(Fl_Tree_Item *newchild, int index);
276  int move(int to, int from);
277  int move(Fl_Tree_Item *item, int op=0, int pos=0);
278  int move_above(Fl_Tree_Item *item);
279  int move_below(Fl_Tree_Item *item);
280  int move_into(Fl_Tree_Item *item, int pos=0);
281  int depth() const;
282  Fl_Tree_Item *prev();
283  Fl_Tree_Item *next();
284  Fl_Tree_Item *next_sibling();
285  Fl_Tree_Item *prev_sibling();
286  void update_prev_next(int index);
287  Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs); // deprecated
288  Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs); // deprecated
289  Fl_Tree_Item *next_visible(Fl_Tree_Prefs &prefs);
290  Fl_Tree_Item *prev_visible(Fl_Tree_Prefs &prefs);
291 
294  return(_parent);
295  }
297  const Fl_Tree_Item *parent() const {
298  return(_parent);
299  }
303  void parent(Fl_Tree_Item *val) {
304  _parent = val;
305  }
306 #if FLTK_ABI_VERSION >= 10303
307  const Fl_Tree_Prefs& prefs() const;
309  const Fl_Tree *tree() const {
310  return(_tree);
311  }
312 #endif
313 
314  // State
316  void open();
317  void close();
319  int is_open() const {
320  return(is_flag(OPEN));
321  }
323  int is_close() const {
324  return(is_flag(OPEN)?0:1);
325  }
327  void open_toggle() {
328  is_open()?close():open(); // handles calling recalc_tree()
329  }
333  void select(int val=1) {
334  set_flag(SELECTED, val);
335  }
337  void select_toggle() {
338  if ( is_selected() ) {
339  deselect(); // deselect if selected
340  } else {
341  select(); // select if deselected
342  }
343  }
348  int select_all() {
349  int count = 0;
350  if ( ! is_selected() ) {
351  select();
352  ++count;
353  }
354  for ( int t=0; t<children(); t++ ) {
355  count += child(t)->select_all();
356  }
357  return(count);
358  }
360  void deselect() {
361  set_flag(SELECTED, 0);
362  }
367  int deselect_all() {
368  int count = 0;
369  if ( is_selected() ) {
370  deselect();
371  ++count;
372  }
373  for ( int t=0; t<children(); t++ ) {
374  count += child(t)->deselect_all();
375  }
376  return(count);
377  }
379  char is_selected() const {
380  return(is_flag(SELECTED));
381  }
390  void activate(int val=1) {
391  set_flag(ACTIVE,val);
392  if ( _widget && val != (int)_widget->active() ) {
393  if ( val ) {
394  _widget->activate();
395  } else {
396  _widget->deactivate();
397  }
398  _widget->redraw();
399  }
400  }
404  void deactivate() {
405  activate(0);
406  }
408  char is_activated() const {
409  return(is_flag(ACTIVE));
410  }
412  char is_active() const {
413  return(is_activated());
414  }
416  int visible() const {
417  return(is_visible());
418  }
420  int is_visible() const {
421  return(is_flag(VISIBLE));
422  }
423  int visible_r() const;
424 
426  void usericon(Fl_Image *val) {
427  _usericon = val;
428  recalc_tree(); // may change tree geometry
429  }
431  Fl_Image *usericon() const {
432  return(_usericon);
433  }
435  // Events
437 #if FLTK_ABI_VERSION >= 10303
438  const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0) const;
439  Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0);
440 #else
441  const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs) const;
442  Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs);
443 #endif
444  int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
445  int event_on_label(const Fl_Tree_Prefs &prefs) const;
447  int is_root() const {
448  return(_parent==0?1:0);
449  }
450 
451  // Protected methods
452  // TODO: move these to top 'protected:' section
453 protected:
454 #if FLTK_ABI_VERSION >= 10301
455 
456  inline void set_flag(unsigned short flag,int val) {
457  if ( flag==OPEN || flag==VISIBLE ) {
458  recalc_tree(); // may change tree geometry
459  }
460  if ( val ) _flags |= flag; else _flags &= ~flag;
461  }
463  inline int is_flag(unsigned short val) const {
464  return(_flags & val ? 1 : 0);
465  }
466 #else /*FLTK_ABI_VERSION*/
467 
468  void set_flag(unsigned short flag,int val) {
469  switch (flag) {
470  case OPEN: _open = val; break;
471  case VISIBLE: _visible = val; break;
472  case ACTIVE: _active = val; break;
473  case SELECTED: _selected = val; break;
474  }
475  }
477  int is_flag(unsigned short flag) const {
478  switch (flag) {
479  case OPEN: return(_open ? 1 : 0);
480  case VISIBLE: return(_visible ? 1 : 0);
481  case ACTIVE: return(_active ? 1 : 0);
482  case SELECTED: return(_selected ? 1 : 0);
483  default: return(0);
484  }
485  }
486 #endif /*FLTK_ABI_VERSION*/
487 
488 };
489 
490 #endif /*FL_TREE_ITEM_H*/
491 
492 //
493 // End of "$Id: Fl_Tree_Item.H 10271 2014-09-04 18:56:52Z greg.ercolano $".
494 //