FLTK 1.3.0
Fl_Tree_Item.H
Go to the documentation of this file.
1 //
2 // "$Id: Fl_Tree_Item.H 8340 2011-01-30 20:22:06Z 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; you can redistribute it and/or
24 // modify it under the terms of the GNU Library General Public
25 // License as published by the Free Software Foundation; either
26 // version 2 of the License, or (at your option) any later version.
27 //
28 // This library is distributed in the hope that it will be useful,
29 // but WITHOUT ANY WARRANTY; without even the implied warranty of
30 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 // Library General Public License for more details.
32 //
33 // You should have received a copy of the GNU Library General Public
34 // License along with this library; if not, write to the Free Software
35 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 // USA.
37 //
38 
43 
59 class FL_EXPORT Fl_Tree_Item {
60  const char *_label; // label (memory managed)
61  Fl_Font _labelfont; // label's font face
62  Fl_Fontsize _labelsize; // label's font size
63  Fl_Color _labelfgcolor; // label's fg color
64  Fl_Color _labelbgcolor; // label's bg color
65  char _open; // item is open?
66  char _visible; // item is visible?
67  char _active; // item activated?
68  char _selected; // item selected?
69  int _xywh[4]; // xywh of this widget (if visible)
70  int _collapse_xywh[4]; // xywh of collapse icon (if any)
71  int _label_xywh[4]; // xywh of label
72  Fl_Widget *_widget; // item's label widget (optional)
73  Fl_Image *_usericon; // item's user-specific icon (optional)
74  Fl_Tree_Item_Array _children; // array of child items
75  Fl_Tree_Item *_parent; // parent item (=0 if root)
76  void *_userdata; // user data that can be associated with an item
77 protected:
78  void show_widgets();
79  void hide_widgets();
80  void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
81  void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
82 public:
83  Fl_Tree_Item(const Fl_Tree_Prefs &prefs); // CTOR
84  ~Fl_Tree_Item(); // DTOR
85  Fl_Tree_Item(const Fl_Tree_Item *o); // COPY CTOR
86  int x() const { return(_xywh[0]); }
87  int y() const { return(_xywh[1]); }
88  int w() const { return(_xywh[2]); }
89  int h() const { return(_xywh[3]); }
90  void draw(int X, int &Y, int W, Fl_Widget *tree, Fl_Tree_Item *itemfocus, const Fl_Tree_Prefs &prefs, int lastchild=1);
91  void show_self(const char *indent = "") const;
92  void label(const char *val);
93  const char *label() const;
94 
96  inline void user_data( void* data ) { _userdata = data; }
97 
99  inline void* user_data() const { return _userdata; }
100 
102  void labelfont(Fl_Font val) {
103  _labelfont = val;
104  }
106  Fl_Font labelfont() const {
107  return(_labelfont);
108  }
110  void labelsize(Fl_Fontsize val) {
111  _labelsize = val;
112  }
115  return(_labelsize);
116  }
118  void labelfgcolor(Fl_Color val) {
119  _labelfgcolor = val;
120  }
122  void labelcolor(Fl_Color val) {
123  _labelfgcolor = val;
124  }
127  return(_labelfgcolor);
128  }
131  return(_labelfgcolor);
132  }
134  void labelbgcolor(Fl_Color val) {
135  _labelbgcolor = val;
136  }
139  return(_labelbgcolor);
140  }
142  void widget(Fl_Widget *val) {
143  _widget = val;
144  }
146  Fl_Widget *widget() const {
147  return(_widget);
148  }
150  int children() const {
151  return(_children.total());
152  }
154  Fl_Tree_Item *child(int index) {
155  return(_children[index]);
156  }
158  const Fl_Tree_Item *child(int t) const;
160  int has_children() const {
161  return(children());
162  }
163  int find_child(const char *name);
164  int find_child(Fl_Tree_Item *item);
165  int remove_child(Fl_Tree_Item *item);
166  int remove_child(const char *new_label);
167  void clear_children();
168  void swap_children(int ax, int bx);
169  int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
170  const Fl_Tree_Item *find_child_item(char **arr) const; // const
171  Fl_Tree_Item *find_child_item(char **arr); // non-const
172  const Fl_Tree_Item *find_item(char **arr) const; // const
173  Fl_Tree_Item *find_item(char **arr); // non-const
175  // Adding items
177  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs, const char *new_label);
178  Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs, char **arr);
179  Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
180  Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
181  int depth() const;
182  Fl_Tree_Item *prev();
183  Fl_Tree_Item *next();
184  Fl_Tree_Item *next_sibling();
185  Fl_Tree_Item *prev_sibling();
186  Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs);
187  Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs);
188 
191  return(_parent);
192  }
194  const Fl_Tree_Item *parent() const {
195  return(_parent);
196  }
200  void parent(Fl_Tree_Item *val) {
201  _parent = val;
202  }
204  // State
206  void open();
207  void close();
209  int is_open() const {
210  return(_open?1:0);
211  }
213  int is_close() const {
214  return(_open?0:1);
215  }
217  void open_toggle() {
218  _open?close():open();
219  }
223  void select(int val=1) {
224  _selected = val;
225  }
227  void select_toggle() {
228  if ( is_selected() ) {
229  deselect(); // deselect if selected
230  } else {
231  select(); // select if deselected
232  }
233  }
238  int select_all() {
239  int count = 0;
240  if ( ! is_selected() ) {
241  select();
242  ++count;
243  }
244  for ( int t=0; t<children(); t++ ) {
245  count += child(t)->select_all();
246  }
247  return(count);
248  }
250  void deselect() {
251  _selected = 0;
252  }
257  int deselect_all() {
258  int count = 0;
259  if ( is_selected() ) {
260  deselect();
261  ++count;
262  }
263  for ( int t=0; t<children(); t++ ) {
264  count += child(t)->deselect_all();
265  }
266  return(count);
267  }
269  char is_selected() const {
270  return(_selected);
271  }
281  void activate(int val=1) {
282  _active = val;
283  if ( _widget && val != (int)_widget->active() ) {
284  if ( val ) {
285  _widget->activate();
286  } else {
287  _widget->deactivate();
288  }
289  _widget->redraw();
290  }
291  }
295  void deactivate() {
296  activate(0);
297  }
299  char is_activated() const {
300  return(_active);
301  }
303  char is_active() const {
304  return(_active);
305  }
307  int visible() const {
308  return(_visible ? 1 : 0);
309  }
310  int visible_r() const;
311 
313  void usericon(Fl_Image *val) {
314  _usericon = val;
315  }
317  Fl_Image *usericon() const {
318  return(_usericon);
319  }
321  // Events
323  const Fl_Tree_Item *find_clicked(const Fl_Tree_Prefs &prefs) const;
324  Fl_Tree_Item *find_clicked(const Fl_Tree_Prefs &prefs);
325  int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
326  int event_on_label(const Fl_Tree_Prefs &prefs) const;
328  int is_root() const {
329  return(_parent==0?1:0);
330  }
331 };
332 
333 #endif /*FL_TREE_ITEM_H*/
334 
335 //
336 // End of "$Id: Fl_Tree_Item.H 8340 2011-01-30 20:22:06Z greg.ercolano $".
337 //