FLTK 1.4.0
Loading...
Searching...
No Matches
Fl_Tree_Item.H
Go to the documentation of this file.
1//
2
3#ifndef FL_TREE_ITEM_H
4#define FL_TREE_ITEM_H
5
6#include <FL/Fl.H>
7#include <FL/Fl_Widget.H>
8#include <FL/Fl_Image.H>
9#include <FL/fl_draw.H>
10
12#include <FL/Fl_Tree_Prefs.H>
13
15// FL/Fl_Tree_Item.H
17//
18// Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
19// Copyright (C) 2009-2010 by Greg Ercolano.
20//
21// This library is free software. Distribution and use rights are outlined in
22// the file "COPYING" which should have been included with this file. If this
23// file is missing or damaged, see the license at:
24//
25// https://www.fltk.org/COPYING.php
26//
27// Please see the following page on how to report bugs and issues:
28//
29// https://www.fltk.org/bugs.php
30//
31
36
64class Fl_Tree;
65class FL_EXPORT Fl_Tree_Item {
66 Fl_Tree *_tree; // parent tree
67 const char *_label; // label (memory managed)
68 Fl_Font _labelfont; // label's font face
69 Fl_Fontsize _labelsize; // label's font size
70 Fl_Color _labelfgcolor; // label's fg color
71 Fl_Color _labelbgcolor; // label's bg color (0xffffffff is 'transparent')
73 enum Fl_Tree_Item_Flags {
74 OPEN = 1<<0,
75 VISIBLE = 1<<1,
76 ACTIVE = 1<<2,
77 SELECTED = 1<<3
78 };
79 unsigned short _flags; // misc flags
80 int _xywh[4]; // xywh of this widget (if visible)
81 int _collapse_xywh[4]; // xywh of collapse icon (if visible)
82 int _label_xywh[4]; // xywh of label
83 Fl_Widget *_widget; // item's label widget (optional)
84 Fl_Image *_usericon; // item's user-specific icon (optional)
85 Fl_Image *_userdeicon; // deactivated usericon
86 Fl_Tree_Item_Array _children; // array of child items
87 Fl_Tree_Item *_parent; // parent item (=0 if root)
88 void *_userdata; // user data that can be associated with an item
89 Fl_Tree_Item *_prev_sibling; // previous sibling (same level)
90 Fl_Tree_Item *_next_sibling; // next sibling (same level)
91 // Protected methods
92protected:
93 void _Init(const Fl_Tree_Prefs &prefs, Fl_Tree *tree);
94 void show_widgets();
95 void hide_widgets();
96 void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
97 void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
98 void recalc_tree();
99 int calc_item_height(const Fl_Tree_Prefs &prefs) const;
100 Fl_Color drawfgcolor() const;
101 Fl_Color drawbgcolor() const;
102
103public:
104 Fl_Tree_Item(const Fl_Tree_Prefs &prefs); // CTOR -- backwards compatible
105 Fl_Tree_Item(Fl_Tree *tree); // CTOR -- ABI 1.3.3+
106 virtual ~Fl_Tree_Item(); // DTOR -- ABI 1.3.3+
107 Fl_Tree_Item(const Fl_Tree_Item *o); // COPY CTOR
109 int x() const { return(_xywh[0]); }
111 int y() const { return(_xywh[1]); }
114 int w() const { return(_xywh[2]); }
116 int h() const { return(_xywh[3]); }
119 int label_x() const { return(_label_xywh[0]); }
122 int label_y() const { return(_label_xywh[1]); }
126 int label_w() const { return(_label_xywh[2]); }
129 int label_h() const { return(_label_xywh[3]); }
130 virtual int draw_item_content(int render);
131 void draw(int X, int &Y, int W, Fl_Tree_Item *itemfocus,
132 int &tree_item_xmax, int lastchild=1, int render=1);
133 void show_self(const char *indent = "") const;
134 void label(const char *val);
135 const char *label() const;
136
138 inline void user_data( void* data ) { _userdata = data; }
139
141 inline void* user_data() const { return _userdata; }
142
144 void labelfont(Fl_Font val) {
145 _labelfont = val;
146 recalc_tree(); // may change tree geometry
147 }
150 return(_labelfont);
151 }
154 _labelsize = val;
155 recalc_tree(); // may change tree geometry
156 }
159 return(_labelsize);
160 }
163 _labelfgcolor = val;
164 }
167 return(_labelfgcolor);
168 }
171 labelfgcolor(val);
172 }
175 return labelfgcolor();
176 }
180 _labelbgcolor = val;
181 }
187 return(_labelbgcolor);
188 }
190 void widget(Fl_Widget *val) {
191 _widget = val;
192 recalc_tree(); // may change tree geometry
193 }
195 Fl_Widget *widget() const {
196 return(_widget);
197 }
199 int children() const {
200 return(_children.total());
201 }
203 Fl_Tree_Item *child(int index) {
204 return(_children[index]);
205 }
207 const Fl_Tree_Item *child(int t) const;
209 int has_children() const {
210 return(children());
211 }
212 int find_child(const char *name);
213 int find_child(Fl_Tree_Item *item);
214 int remove_child(Fl_Tree_Item *item);
215 int remove_child(const char *new_label);
216 void clear_children();
217 void swap_children(int ax, int bx);
218 int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
219 const Fl_Tree_Item *find_child_item(const char *name) const;
220 Fl_Tree_Item *find_child_item(const char *name);
221 const Fl_Tree_Item *find_child_item(char **arr) const;
222 Fl_Tree_Item *find_child_item(char **arr);
223 const Fl_Tree_Item *find_item(char **arr) const;
224 Fl_Tree_Item *find_item(char **arr);
226 // Adding items
228 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
229 const char *new_label,
230 Fl_Tree_Item *newitem);
231 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
232 const char *new_label);
233 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
234 char **arr,
235 Fl_Tree_Item *newitem);
236 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
237 char **arr);
238 Fl_Tree_Item *replace(Fl_Tree_Item *new_item);
239 Fl_Tree_Item *replace_child(Fl_Tree_Item *olditem, Fl_Tree_Item *newitem);
240 Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
241 Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
242 Fl_Tree_Item* deparent(int index);
243 int reparent(Fl_Tree_Item *newchild, int index);
244 int move(int to, int from);
245 int move(Fl_Tree_Item *item, int op=0, int pos=0);
246 int move_above(Fl_Tree_Item *item);
247 int move_below(Fl_Tree_Item *item);
248 int move_into(Fl_Tree_Item *item, int pos=0);
249 int depth() const;
250 Fl_Tree_Item *prev();
251 Fl_Tree_Item *next();
252 Fl_Tree_Item *next_sibling();
253 Fl_Tree_Item *prev_sibling();
254 void update_prev_next(int index);
255 Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs); // deprecated
256 Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs); // deprecated
257 Fl_Tree_Item *next_visible(Fl_Tree_Prefs &prefs);
258 Fl_Tree_Item *prev_visible(Fl_Tree_Prefs &prefs);
259
262 return(_parent);
263 }
265 const Fl_Tree_Item *parent() const {
266 return(_parent);
267 }
271 void parent(Fl_Tree_Item *val) {
272 _parent = val;
273 }
274 const Fl_Tree_Prefs& prefs() const;
277 const Fl_Tree *tree() const {
278 return(_tree);
279 }
283 return(_tree);
284 }
286 // State
288 void open();
289 void close();
291 int is_open() const {
292 return(is_flag(OPEN));
293 }
295 int is_close() const {
296 return(is_flag(OPEN)?0:1);
297 }
299 void open_toggle() {
300 is_open()?close():open(); // handles calling recalc_tree()
301 }
305 void select(int val=1) {
306 set_flag(SELECTED, val);
307 }
310 if ( is_selected() ) {
311 deselect(); // deselect if selected
312 } else {
313 select(); // select if deselected
314 }
315 }
321 int count = 0;
322 if ( ! is_selected() ) {
323 select();
324 ++count;
325 }
326 for ( int t=0; t<children(); t++ ) {
327 count += child(t)->select_all();
328 }
329 return(count);
330 }
332 void deselect() {
333 set_flag(SELECTED, 0);
334 }
340 int count = 0;
341 if ( is_selected() ) {
342 deselect();
343 ++count;
344 }
345 for ( int t=0; t<children(); t++ ) {
346 count += child(t)->deselect_all();
347 }
348 return(count);
349 }
351 char is_selected() const {
352 return(is_flag(SELECTED));
353 }
362 void activate(int val=1) {
363 set_flag(ACTIVE,val);
364 if ( _widget && val != (int)_widget->active() ) {
365 if ( val ) {
366 _widget->activate();
367 } else {
368 _widget->deactivate();
369 }
370 _widget->redraw();
371 }
372 }
376 void deactivate() {
377 activate(0);
378 }
380 char is_activated() const {
381 return(is_flag(ACTIVE));
382 }
384 char is_active() const {
385 return(is_activated());
386 }
388 int visible() const {
389 return(is_visible());
390 }
392 int is_visible() const {
393 return(is_flag(VISIBLE));
394 }
395 int visible_r() const;
396
406 void usericon(Fl_Image *val) {
407 _usericon = val;
408 recalc_tree(); // may change tree geometry
409 }
412 return(_usericon);
413 }
440 void userdeicon(Fl_Image* val) {
441 _userdeicon = val;
442 }
446 return _userdeicon;
447 }
449 // Events
451 const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0) const;
452 Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0);
453 int event_on_item(const Fl_Tree_Prefs &prefs) const;
454 int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
455 int event_on_user_icon(const Fl_Tree_Prefs &prefs) const;
456 int event_on_label(const Fl_Tree_Prefs &prefs) const;
458 int is_root() const {
459 return(_parent==0?1:0);
460 }
461
462 // Protected methods
463 // TODO: move these to top 'protected:' section
464protected:
466 inline void set_flag(unsigned short flag,int val) {
467 if ( flag==OPEN || flag==VISIBLE ) {
468 recalc_tree(); // may change tree geometry
469 }
470 if ( val ) _flags |= flag; else _flags &= ~flag;
471 }
473 inline int is_flag(unsigned short val) const {
474 return(_flags & val ? 1 : 0);
475 }
476
477};
478
479#endif /*FL_TREE_ITEM_H*/
int Fl_Font
A font number is an index into the internal font table.
Definition Enumerations.H:1044
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1101
int Fl_Fontsize
Size of a font in pixels.
Definition Enumerations.H:1073
Fl static class.
Fl_Image, Fl_RGB_Image classes.
This file defines a class that manages an array of Fl_Tree_Item pointers.
This file contains the definitions for Fl_Tree's preferences.
Fl_Widget and Fl_Label classes.
Base class for image caching, scaling and drawing.
Definition Fl_Image.H:60
Manages an array of Fl_Tree_Item pointers.
Definition Fl_Tree_Item_Array.H:45
int total() const
Return the total items in the array, or 0 if empty.
Definition Fl_Tree_Item_Array.H:68
Tree widget item.
Definition Fl_Tree_Item.H:65
int has_children() const
See if this item has children.
Definition Fl_Tree_Item.H:209
int w() const
The entire item's width to right edge of Fl_Tree's inner width within scrollbars.
Definition Fl_Tree_Item.H:114
int is_visible() const
See if the item is visible.
Definition Fl_Tree_Item.H:392
void usericon(Fl_Image *val)
Set the item's user icon to an Fl_Image.
Definition Fl_Tree_Item.H:406
int visible() const
See if the item is visible. Alias for is_visible().
Definition Fl_Tree_Item.H:388
Fl_Tree * tree()
Return the tree for this item.
Definition Fl_Tree_Item.H:282
void userdeicon(Fl_Image *val)
Set the usericon to draw when the item is deactivated.
Definition Fl_Tree_Item.H:440
void deactivate()
Deactivate the item; the callback() won't be invoked when clicked.
Definition Fl_Tree_Item.H:376
Fl_Widget * widget() const
Return FLTK widget assigned to this item.
Definition Fl_Tree_Item.H:195
void deselect()
Disable the item's selection state.
Definition Fl_Tree_Item.H:332
Fl_Image * userdeicon() const
Return the deactivated version of the user icon, if any.
Definition Fl_Tree_Item.H:445
void open_toggle()
Toggle the item's open/closed state.
Definition Fl_Tree_Item.H:299
Fl_Tree_Item * parent()
Return the parent for this item. Returns NULL if we are the root.
Definition Fl_Tree_Item.H:261
int label_y() const
The item's label y position relative to the window.
Definition Fl_Tree_Item.H:122
Fl_Tree_Item * child(int index)
Return the child item for the given 'index'.
Definition Fl_Tree_Item.H:203
int select_all()
Select item and all its children.
Definition Fl_Tree_Item.H:320
Fl_Color labelfgcolor() const
Return item's label foreground text color.
Definition Fl_Tree_Item.H:166
int is_close() const
See if the item is 'closed'.
Definition Fl_Tree_Item.H:295
void labelsize(Fl_Fontsize val)
Set item's label font size.
Definition Fl_Tree_Item.H:153
void user_data(void *data)
Set a user-data value for the item.
Definition Fl_Tree_Item.H:138
void * user_data() const
Retrieve the user-data value that has been assigned to the item.
Definition Fl_Tree_Item.H:141
void select(int val=1)
Change the item's selection state to the optionally specified 'val'.
Definition Fl_Tree_Item.H:305
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:265
const Fl_Tree * tree() const
Return the tree for this item.
Definition Fl_Tree_Item.H:277
int y() const
The item's y position relative to the window.
Definition Fl_Tree_Item.H:111
Fl_Color labelbgcolor() const
Return item's label background text color.
Definition Fl_Tree_Item.H:186
int label_h() const
The item's label height.
Definition Fl_Tree_Item.H:129
char is_selected() const
See if the item is selected.
Definition Fl_Tree_Item.H:351
Fl_Color labelcolor() const
Return item's label text color. Alias for labelfgcolor() const).
Definition Fl_Tree_Item.H:174
void labelfont(Fl_Font val)
Set item's label font face.
Definition Fl_Tree_Item.H:144
Fl_Font labelfont() const
Get item's label font face.
Definition Fl_Tree_Item.H:149
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:126
void select_toggle()
Toggle the item's selection state.
Definition Fl_Tree_Item.H:309
void labelcolor(Fl_Color val)
Set item's label text color. Alias for labelfgcolor(Fl_Color)).
Definition Fl_Tree_Item.H:170
int is_flag(unsigned short val) const
See if flag set. Returns 0 or 1.
Definition Fl_Tree_Item.H:473
void activate(int val=1)
Change the item's activation state to the optionally specified 'val'.
Definition Fl_Tree_Item.H:362
int h() const
The item's height.
Definition Fl_Tree_Item.H:116
int is_open() const
See if the item is 'open'.
Definition Fl_Tree_Item.H:291
void set_flag(unsigned short flag, int val)
Set a flag to an on or off value. val is 0 or 1.
Definition Fl_Tree_Item.H:466
int is_root() const
Is this item the root of the tree?
Definition Fl_Tree_Item.H:458
Fl_Fontsize labelsize() const
Get item's label font size.
Definition Fl_Tree_Item.H:158
void widget(Fl_Widget *val)
Assign an FLTK widget to this item.
Definition Fl_Tree_Item.H:190
void parent(Fl_Tree_Item *val)
Set the parent for this item.
Definition Fl_Tree_Item.H:271
char is_active() const
See if the item is activated. Alias for is_activated().
Definition Fl_Tree_Item.H:384
int children() const
Return the number of children this item has.
Definition Fl_Tree_Item.H:199
void labelfgcolor(Fl_Color val)
Set item's label foreground text color.
Definition Fl_Tree_Item.H:162
int x() const
The item's x position relative to the window.
Definition Fl_Tree_Item.H:109
int label_x() const
The item's label x position relative to the window.
Definition Fl_Tree_Item.H:119
char is_activated() const
See if the item is activated.
Definition Fl_Tree_Item.H:380
void labelbgcolor(Fl_Color val)
Set item's label background color.
Definition Fl_Tree_Item.H:179
int deselect_all()
Deselect item and all its children.
Definition Fl_Tree_Item.H:339
Fl_Image * usericon() const
Get the item's user icon as an Fl_Image. Returns '0' if disabled.
Definition Fl_Tree_Item.H:411
Tree widget's preferences.
Definition Fl_Tree_Prefs.H:105
Tree widget.
Definition Fl_Tree.H:286
Fl_Widget is the base class for all widgets in FLTK.
Definition Fl_Widget.H:104
void deactivate()
Deactivates the widget.
Definition Fl_Widget.cxx:249
void redraw()
Schedules the drawing of the widget.
Definition Fl.cxx:1586
void activate()
Activates the widget.
Definition Fl_Widget.cxx:237
unsigned int active() const
Returns whether the widget is active.
Definition Fl_Widget.H:881
utility header to pull drawing functions together