FLTK 1.4.4

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
11#include <FL/Fl_Tree_Item_Array.H>
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
64 class Fl_Tree;
65 class 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
92 protected:
93 void _Init(const Fl_Tree_Prefs &prefs, Fl_Tree *tree);
94 void show_widgets();
95 void hide_widgets();
96#if FL_ABI_VERSION >= 10403
97 virtual void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
98 virtual void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
99#else
100 void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
101 void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
102#endif
103 void recalc_tree();
104 int calc_item_height(const Fl_Tree_Prefs &prefs) const;
105 Fl_Color drawfgcolor() const;
106 Fl_Color drawbgcolor() const;
107
108 public:
109 Fl_Tree_Item(const Fl_Tree_Prefs &prefs); // CTOR -- backwards compatible
110 Fl_Tree_Item(Fl_Tree *tree); // CTOR -- ABI 1.3.3+
111 virtual ~Fl_Tree_Item(); // DTOR -- ABI 1.3.3+
112 Fl_Tree_Item(const Fl_Tree_Item *o); // COPY CTOR
114 int x() const { return(_xywh[0]); }
116 int y() const { return(_xywh[1]); }
119 int w() const { return(_xywh[2]); }
121 int h() const { return(_xywh[3]); }
124 int label_x() const { return(_label_xywh[0]); }
127 int label_y() const { return(_label_xywh[1]); }
131 int label_w() const { return(_label_xywh[2]); }
134 int label_h() const { return(_label_xywh[3]); }
135 virtual int draw_item_content(int render);
136 void draw(int X, int &Y, int W, Fl_Tree_Item *itemfocus,
137 int &tree_item_xmax, int lastchild=1, int render=1);
138 void show_self(const char *indent = "") const;
139 void label(const char *val);
140 const char *label() const;
141
143 inline void user_data( void* data ) { _userdata = data; }
144
146 inline void* user_data() const { return _userdata; }
147
149 void labelfont(Fl_Font val) {
150 _labelfont = val;
151 recalc_tree(); // may change tree geometry
152 }
153
154 Fl_Font labelfont() const {
155 return(_labelfont);
156 }
157
158 void labelsize(Fl_Fontsize val) {
159 _labelsize = val;
160 recalc_tree(); // may change tree geometry
161 }
162
163 Fl_Fontsize labelsize() const {
164 return(_labelsize);
165 }
166
167 void labelfgcolor(Fl_Color val) {
168 _labelfgcolor = val;
169 }
170
171 Fl_Color labelfgcolor() const {
172 return(_labelfgcolor);
173 }
174
175 void labelcolor(Fl_Color val) {
176 labelfgcolor(val);
177 }
178
179 Fl_Color labelcolor() const {
180 return labelfgcolor();
181 }
182
184 void labelbgcolor(Fl_Color val) {
185 _labelbgcolor = val;
186 }
187
191 Fl_Color labelbgcolor() const {
192 return(_labelbgcolor);
193 }
194
195 void widget(Fl_Widget *val) {
196 _widget = val;
197 recalc_tree(); // may change tree geometry
198 }
199
200 Fl_Widget *widget() const {
201 return(_widget);
202 }
203
204 int children() const {
205 return(_children.total());
206 }
207
208 Fl_Tree_Item *child(int index) {
209 return(_children[index]);
210 }
211
212 const Fl_Tree_Item *child(int t) const;
214 int has_children() const {
215 return(children());
216 }
217 int find_child(const char *name);
218 int find_child(Fl_Tree_Item *item);
219 int remove_child(Fl_Tree_Item *item);
220 int remove_child(const char *new_label);
221 void clear_children();
222 void swap_children(int ax, int bx);
223 int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
224 const Fl_Tree_Item *find_child_item(const char *name) const;
225 Fl_Tree_Item *find_child_item(const char *name);
226 const Fl_Tree_Item *find_child_item(char **arr) const;
227 Fl_Tree_Item *find_child_item(char **arr);
228 const Fl_Tree_Item *find_item(char **arr) const;
229 Fl_Tree_Item *find_item(char **arr);
231 // Adding items
233 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
234 const char *new_label,
235 Fl_Tree_Item *newitem);
236 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
237 const char *new_label);
238 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
239 char **arr,
240 Fl_Tree_Item *newitem);
241 Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs,
242 char **arr);
243 Fl_Tree_Item *replace(Fl_Tree_Item *new_item);
244 Fl_Tree_Item *replace_child(Fl_Tree_Item *olditem, Fl_Tree_Item *newitem);
245 Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
246 Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
247 Fl_Tree_Item* deparent(int index);
248 int reparent(Fl_Tree_Item *newchild, int index);
249 int move(int to, int from);
250 int move(Fl_Tree_Item *item, int op=0, int pos=0);
251 int move_above(Fl_Tree_Item *item);
252 int move_below(Fl_Tree_Item *item);
253 int move_into(Fl_Tree_Item *item, int pos=0);
254 int depth() const;
255 Fl_Tree_Item *prev();
256 Fl_Tree_Item *next();
257 Fl_Tree_Item *next_sibling();
258 Fl_Tree_Item *prev_sibling();
259 void update_prev_next(int index);
260 Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs); // deprecated
261 Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs); // deprecated
262 Fl_Tree_Item *next_visible(Fl_Tree_Prefs &prefs);
263 Fl_Tree_Item *prev_visible(Fl_Tree_Prefs &prefs);
264
266 Fl_Tree_Item *parent() {
267 return(_parent);
268 }
269
270 const Fl_Tree_Item *parent() const {
271 return(_parent);
272 }
273
276 void parent(Fl_Tree_Item *val) {
277 _parent = val;
278 }
279 const Fl_Tree_Prefs& prefs() const;
282 const Fl_Tree *tree() const {
283 return(_tree);
284 }
285
287 Fl_Tree *tree() {
288 return(_tree);
289 }
290
291 // State
293 void open();
294 void close();
296 int is_open() const {
297 return(is_flag(OPEN));
298 }
299
300 int is_close() const {
301 return(is_flag(OPEN)?0:1);
302 }
303
304 void open_toggle() {
305 is_open()?close():open(); // handles calling recalc_tree()
306 }
307
310 void select(int val=1) {
311 set_flag(SELECTED, val);
312 }
313
314 void select_toggle() {
315 if ( is_selected() ) {
316 deselect(); // deselect if selected
317 } else {
318 select(); // select if deselected
319 }
320 }
321
325 int select_all() {
326 int count = 0;
327 if ( ! is_selected() ) {
328 select();
329 ++count;
330 }
331 for ( int t=0; t<children(); t++ ) {
332 count += child(t)->select_all();
333 }
334 return(count);
335 }
336
337 void deselect() {
338 set_flag(SELECTED, 0);
339 }
340
344 int deselect_all() {
345 int count = 0;
346 if ( is_selected() ) {
347 deselect();
348 ++count;
349 }
350 for ( int t=0; t<children(); t++ ) {
351 count += child(t)->deselect_all();
352 }
353 return(count);
354 }
355
356 char is_selected() const {
357 return(is_flag(SELECTED));
358 }
359
367 void activate(int val=1) {
368 set_flag(ACTIVE,val);
369 if ( _widget && val != (int)_widget->active() ) {
370 if ( val ) {
371 _widget->activate();
372 } else {
373 _widget->deactivate();
374 }
375 _widget->redraw();
376 }
377 }
378
381 void deactivate() {
382 activate(0);
383 }
384
385 char is_activated() const {
386 return(is_flag(ACTIVE));
387 }
388
389 char is_active() const {
390 return(is_activated());
391 }
392
393 int visible() const {
394 return(is_visible());
395 }
396
397 int is_visible() const {
398 return(is_flag(VISIBLE));
399 }
400
406 int visible_r() const {
407 return(is_visible_r());
408 }
409 int is_visible_r() const;
410
420 void usericon(Fl_Image *val) {
421 _usericon = val;
422 recalc_tree(); // may change tree geometry
423 }
424
425 Fl_Image *usericon() const {
426 return(_usericon);
427 }
428
454 void userdeicon(Fl_Image* val) {
455 _userdeicon = val;
456 }
457
459 Fl_Image* userdeicon() const {
460 return _userdeicon;
461 }
462
463 // Events
465 const Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0) const;
466 Fl_Tree_Item* find_clicked(const Fl_Tree_Prefs &prefs, int yonly=0);
467 int event_on_item(const Fl_Tree_Prefs &prefs) const;
468 int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
469 int event_on_user_icon(const Fl_Tree_Prefs &prefs) const;
470 int event_on_label(const Fl_Tree_Prefs &prefs) const;
472 int is_root() const {
473 return(_parent==0?1:0);
474 }
475
476 // Protected methods
477 // TODO: move these to top 'protected:' section
478 protected:
480 inline void set_flag(unsigned short flag,int val) {
481 if ( flag==OPEN || flag==VISIBLE ) {
482 recalc_tree(); // may change tree geometry
483 }
484 if ( val ) _flags |= flag; else _flags &= ~flag;
485 }
486
487 inline int is_flag(unsigned short val) const {
488 return(_flags & val ? 1 : 0);
489 }
490
491};
492
493#endif /*FL_TREE_ITEM_H*/
Fl_Font
int Fl_Font
A font number is an index into the internal font table.
Definition Enumerations.H:1063
Fl_Color
unsigned int Fl_Color
An FLTK color value; see also Colors.
Definition Enumerations.H:1120
Fl_Fontsize
int Fl_Fontsize
Size of a font in pixels.
Definition Enumerations.H:1092
Fl.H
Fl static class.
Fl_Image.H
Fl_Image, Fl_RGB_Image classes.
Fl_Tree_Item_Array.H
This file defines a class that manages an array of Fl_Tree_Item pointers.
Fl_Tree_Prefs.H
This file contains the definitions for Fl_Tree's preferences.
Fl_Widget.H
Fl_Widget and Fl_Label classes.
Fl_Image
Base class for image caching, scaling and drawing.
Definition Fl_Image.H:60
Fl_Tree_Item_Array
Manages an array of Fl_Tree_Item pointers.
Definition Fl_Tree_Item_Array.H:45
Fl_Tree_Item
Tree widget item.
Definition Fl_Tree_Item.H:65
Fl_Tree_Item::has_children
int has_children() const
See if this item has children.
Definition Fl_Tree_Item.H:214
Fl_Tree_Item::w
int w() const
The entire item's width to right edge of Fl_Tree's inner width within scrollbars.
Definition Fl_Tree_Item.H:119
Fl_Tree_Item::is_visible
int is_visible() const
See if the item is visible.
Definition Fl_Tree_Item.H:397
Fl_Tree_Item::usericon
void usericon(Fl_Image *val)
Set the item's user icon to an Fl_Image.
Definition Fl_Tree_Item.H:420
Fl_Tree_Item::visible
int visible() const
See if the item is visible. Alias for is_visible().
Definition Fl_Tree_Item.H:393
Fl_Tree_Item::tree
Fl_Tree * tree()
Return the tree for this item.
Definition Fl_Tree_Item.H:287
Fl_Tree_Item::userdeicon
void userdeicon(Fl_Image *val)
Set the usericon to draw when the item is deactivated.
Definition Fl_Tree_Item.H:454
Fl_Tree_Item::deactivate
void deactivate()
Deactivate the item; the callback() won't be invoked when clicked.
Definition Fl_Tree_Item.H:381
Fl_Tree_Item::draw_horizontal_connector
virtual void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs)
Horizontal connector line based on preference settings.
Definition Fl_Tree_Item.cxx:715
Fl_Tree_Item::widget
Fl_Widget * widget() const
Return FLTK widget assigned to this item.
Definition Fl_Tree_Item.H:200
Fl_Tree_Item::drawbgcolor
Fl_Color drawbgcolor() const
Returns the recommended background color used for drawing this item.
Definition Fl_Tree_Item.cxx:849
Fl_Tree_Item::deselect
void deselect()
Disable the item's selection state.
Definition Fl_Tree_Item.H:337
Fl_Tree_Item::userdeicon
Fl_Image * userdeicon() const
Return the deactivated version of the user icon, if any.
Definition Fl_Tree_Item.H:459
Fl_Tree_Item::prefs
const Fl_Tree_Prefs & prefs() const
Return the parent tree's prefs.
Definition Fl_Tree_Item.cxx:588
Fl_Tree_Item::open_toggle
void open_toggle()
Toggle the item's open/closed state.
Definition Fl_Tree_Item.H:304
Fl_Tree_Item::hide_widgets
void hide_widgets()
Internal: Hide the FLTK widget() for this item and all children.
Definition Fl_Tree_Item.cxx:1248
Fl_Tree_Item::visible_r
int visible_r() const
See if item and all its parents are open() and visible().
Definition Fl_Tree_Item.H:406
Fl_Tree_Item::parent
Fl_Tree_Item * parent()
Return the parent for this item. Returns NULL if we are the root.
Definition Fl_Tree_Item.H:266
Fl_Tree_Item::open
void open()
Open this item and all its children.
Definition Fl_Tree_Item.cxx:1256
Fl_Tree_Item::label_y
int label_y() const
The item's label y position relative to the window.
Definition Fl_Tree_Item.H:127
Fl_Tree_Item::child
Fl_Tree_Item * child(int index)
Return the child item for the given 'index'.
Definition Fl_Tree_Item.H:208
Fl_Tree_Item::select_all
int select_all()
Select item and all its children.
Definition Fl_Tree_Item.H:325
Fl_Tree_Item::labelfgcolor
Fl_Color labelfgcolor() const
Return item's label foreground text color.
Definition Fl_Tree_Item.H:171
Fl_Tree_Item::is_close
int is_close() const
See if the item is 'closed'.
Definition Fl_Tree_Item.H:300
Fl_Tree_Item::labelsize
void labelsize(Fl_Fontsize val)
Set item's label font size.
Definition Fl_Tree_Item.H:158
Fl_Tree_Item::user_data
void user_data(void *data)
Set a user-data value for the item.
Definition Fl_Tree_Item.H:143
Fl_Tree_Item::Fl_Tree_Item
Fl_Tree_Item(const Fl_Tree_Prefs &prefs)
Constructor.
Definition Fl_Tree_Item.cxx:42
Fl_Tree_Item::user_data
void * user_data() const
Retrieve the user-data value that has been assigned to the item.
Definition Fl_Tree_Item.H:146
Fl_Tree_Item::recalc_tree
void recalc_tree()
Call this when our geometry is changed.
Definition Fl_Tree_Item.cxx:1478
Fl_Tree_Item::select
void select(int val=1)
Change the item's selection state to the optionally specified 'val'.
Definition Fl_Tree_Item.H:310
Fl_Tree_Item::show_widgets
void show_widgets()
Internal: Show the FLTK widget() for this item and all children.
Definition Fl_Tree_Item.cxx:1236
Fl_Tree_Item::draw_vertical_connector
virtual void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs)
Vertical connector line based on preference settings.
Definition Fl_Tree_Item.cxx:741
Fl_Tree_Item::parent
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:270
Fl_Tree_Item::tree
const Fl_Tree * tree() const
Return the tree for this item.
Definition Fl_Tree_Item.H:282
Fl_Tree_Item::y
int y() const
The item's y position relative to the window.
Definition Fl_Tree_Item.H:116
Fl_Tree_Item::labelbgcolor
Fl_Color labelbgcolor() const
Return item's label background text color.
Definition Fl_Tree_Item.H:191
Fl_Tree_Item::label_h
int label_h() const
The item's label height.
Definition Fl_Tree_Item.H:134
Fl_Tree_Item::is_selected
char is_selected() const
See if the item is selected.
Definition Fl_Tree_Item.H:356
Fl_Tree_Item::labelcolor
Fl_Color labelcolor() const
Return item's label text color. Alias for labelfgcolor() const).
Definition Fl_Tree_Item.H:179
Fl_Tree_Item::close
void close()
Close this item and all its children.
Definition Fl_Tree_Item.cxx:1266
Fl_Tree_Item::labelfont
void labelfont(Fl_Font val)
Set item's label font face.
Definition Fl_Tree_Item.H:149
Fl_Tree_Item::labelfont
Fl_Font labelfont() const
Get item's label font face.
Definition Fl_Tree_Item.H:154
Fl_Tree_Item::label_w
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:131
Fl_Tree_Item::select_toggle
void select_toggle()
Toggle the item's selection state.
Definition Fl_Tree_Item.H:314
Fl_Tree_Item::labelcolor
void labelcolor(Fl_Color val)
Set item's label text color. Alias for labelfgcolor(Fl_Color)).
Definition Fl_Tree_Item.H:175
Fl_Tree_Item::is_flag
int is_flag(unsigned short val) const
See if flag set. Returns 0 or 1.
Definition Fl_Tree_Item.H:487
Fl_Tree_Item::activate
void activate(int val=1)
Change the item's activation state to the optionally specified 'val'.
Definition Fl_Tree_Item.H:367
Fl_Tree_Item::h
int h() const
The item's height.
Definition Fl_Tree_Item.H:121
Fl_Tree_Item::calc_item_height
int calc_item_height(const Fl_Tree_Prefs &prefs) const
Return the item's 'visible' height.
Definition Fl_Tree_Item.cxx:814
Fl_Tree_Item::is_open
int is_open() const
See if the item is 'open'.
Definition Fl_Tree_Item.H:296
Fl_Tree_Item::set_flag
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:480
Fl_Tree_Item::is_root
int is_root() const
Is this item the root of the tree?
Definition Fl_Tree_Item.H:472
Fl_Tree_Item::labelsize
Fl_Fontsize labelsize() const
Get item's label font size.
Definition Fl_Tree_Item.H:163
Fl_Tree_Item::drawfgcolor
Fl_Color drawfgcolor() const
Returns the recommended foreground color used for drawing this item.
Definition Fl_Tree_Item.cxx:839
Fl_Tree_Item::widget
void widget(Fl_Widget *val)
Assign an FLTK widget to this item.
Definition Fl_Tree_Item.H:195
Fl_Tree_Item::parent
void parent(Fl_Tree_Item *val)
Set the parent for this item.
Definition Fl_Tree_Item.H:276
Fl_Tree_Item::is_active
char is_active() const
See if the item is activated. Alias for is_activated().
Definition Fl_Tree_Item.H:389
Fl_Tree_Item::children
int children() const
Return the number of children this item has.
Definition Fl_Tree_Item.H:204
Fl_Tree_Item::labelfgcolor
void labelfgcolor(Fl_Color val)
Set item's label foreground text color.
Definition Fl_Tree_Item.H:167
Fl_Tree_Item::is_visible_r
int is_visible_r() const
See if item and all its parents are open() and visible().
Definition Fl_Tree_Item.cxx:1466
Fl_Tree_Item::x
int x() const
The item's x position relative to the window.
Definition Fl_Tree_Item.H:114
Fl_Tree_Item::label_x
int label_x() const
The item's label x position relative to the window.
Definition Fl_Tree_Item.H:124
Fl_Tree_Item::is_activated
char is_activated() const
See if the item is activated.
Definition Fl_Tree_Item.H:385
Fl_Tree_Item::labelbgcolor
void labelbgcolor(Fl_Color val)
Set item's label background color.
Definition Fl_Tree_Item.H:184
Fl_Tree_Item::deselect_all
int deselect_all()
Deselect item and all its children.
Definition Fl_Tree_Item.H:344
Fl_Tree_Item::usericon
Fl_Image * usericon() const
Get the item's user icon as an Fl_Image. Returns '0' if disabled.
Definition Fl_Tree_Item.H:425
Fl_Tree_Prefs
Tree widget's preferences.
Definition Fl_Tree_Prefs.H:105
Fl_Tree
Tree widget.
Definition Fl_Tree.H:286
Fl_Widget
Fl_Widget is the base class for all widgets in FLTK.
Definition Fl_Widget.H:112
fl_draw.H
utility header to pull drawing functions together