2
// "$Id: Fl_Tree_Item.H 8340 2011-01-30 20:22:06Z greg.ercolano $"
9
#include <FL/Fl_Widget.H>
10
#include <FL/Fl_Image.H>
11
#include <FL/fl_draw.H>
13
#include <FL/Fl_Tree_Item_Array.H>
14
#include <FL/Fl_Tree_Prefs.H>
16
//////////////////////
18
//////////////////////
20
// Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
21
// Copyright (C) 2009-2010 by Greg Ercolano.
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.
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.
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
41
/// \brief This file contains the definitions for Fl_Tree_Item
46
/// This class is a single tree item, and manages all of the item's attributes.
47
/// Fl_Tree_Item is used by Fl_Tree, which is comprised of many instances of Fl_Tree_Item.
49
/// Fl_Tree_Item is hierarchical; it dynamically manages an Fl_Tree_Item_Array of children
50
/// that are themselves instances of Fl_Tree_Item. Each item can have zero or more children.
51
/// When an item has children, close() and open() can be used to hide or show them.
53
/// Items have their own attributes; font size, face, color.
54
/// Items maintain their own hierarchy of children.
56
/// When you make changes to items, you'll need to tell the tree to redraw()
57
/// for the changes to show up.
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
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);
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;
95
/// Set a user-data value for the item.
96
inline void user_data( void* data ) { _userdata = data; }
98
/// Retrieve the user-data value that has been assigned to the item.
99
inline void* user_data() const { return _userdata; }
101
/// Set item's label font face.
102
void labelfont(Fl_Font val) {
105
/// Get item's label font face.
106
Fl_Font labelfont() const {
109
/// Set item's label font size.
110
void labelsize(Fl_Fontsize val) {
113
/// Get item's label font size.
114
Fl_Fontsize labelsize() const {
117
/// Set item's label foreground text color.
118
void labelfgcolor(Fl_Color val) {
121
/// Set item's label text color.
122
void labelcolor(Fl_Color val) {
125
/// Return item's label text color.
126
Fl_Color labelcolor() const {
127
return(_labelfgcolor);
129
/// Return item's label foreground text color.
130
Fl_Color labelfgcolor() const {
131
return(_labelfgcolor);
133
/// Set item's label background color.
134
void labelbgcolor(Fl_Color val) {
137
/// Return item's background text color.
138
Fl_Color labelbgcolor() const {
139
return(_labelbgcolor);
141
/// Assign an FLTK widget to this item.
142
void widget(Fl_Widget *val) {
145
/// Return FLTK widget assigned to this item.
146
Fl_Widget *widget() const {
149
/// Return the number of children this item has.
150
int children() const {
151
return(_children.total());
153
/// Return the child item for the given 'index'.
154
Fl_Tree_Item *child(int index) {
155
return(_children[index]);
157
/// Return the const child item for the given 'index'.
158
const Fl_Tree_Item *child(int t) const;
159
/// See if this item has children.
160
int has_children() const {
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
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);
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);
189
/// Return the parent for this item. Returns NULL if we are the root.
190
Fl_Tree_Item *parent() {
193
/// Return the const parent for this item. Returns NULL if we are the root.
194
const Fl_Tree_Item *parent() const {
197
/// Set the parent for this item.
198
/// Should only be used by Fl_Tree's internals.
200
void parent(Fl_Tree_Item *val) {
208
/// See if the item is 'open'.
209
int is_open() const {
212
/// See if the item is 'closed'.
213
int is_close() const {
216
/// Toggle the item's open/closed state.
218
_open?close():open();
220
/// Change the item's selection state to the optionally specified 'val'.
221
/// If 'val' is not specified, the item will be selected.
223
void select(int val=1) {
226
/// Toggle the item's selection state.
227
void select_toggle() {
228
if ( is_selected() ) {
229
deselect(); // deselect if selected
231
select(); // select if deselected
234
/// Select self and all children
235
/// Returns count of how many items were in the 'deselected' state,
236
/// ie. how many items were "changed".
240
if ( ! is_selected() ) {
244
for ( int t=0; t<children(); t++ ) {
245
count += child(t)->select_all();
249
/// Disable the item's selection state.
253
/// Deselect self and all children
254
/// Returns count of how many items were in the 'selected' state,
255
/// ie. how many items were "changed".
259
if ( is_selected() ) {
263
for ( int t=0; t<children(); t++ ) {
264
count += child(t)->deselect_all();
268
/// See if the item is selected.
269
char is_selected() const {
272
/// Change the item's activation state to the optionally specified 'val'.
274
/// When deactivated, the item will be 'grayed out'; the callback()
275
/// won't be invoked if the user clicks on the label. If the item
276
/// has a widget() associated with the item, its activation state
277
/// will be changed as well.
279
/// If 'val' is not specified, the item will be activated.
281
void activate(int val=1) {
283
if ( _widget && val != (int)_widget->active() ) {
287
_widget->deactivate();
292
/// Deactivate the item; the callback() won't be invoked when clicked.
293
/// Same as activate(0)
298
/// See if the item is activated.
299
char is_activated() const {
302
/// See if the item is activated.
303
char is_active() const {
306
/// See if the item is visible.
307
int visible() const {
308
return(_visible ? 1 : 0);
310
int visible_r() const;
312
/// Set the user icon's image. '0' will disable.
313
void usericon(Fl_Image *val) {
316
/// Get the user icon. Returns '0' if disabled.
317
Fl_Image *usericon() const {
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;
327
/// Is this item the root of the tree?
328
int is_root() const {
329
return(_parent==0?1:0);
333
#endif /*FL_TREE_ITEM_H*/
336
// End of "$Id: Fl_Tree_Item.H 8340 2011-01-30 20:22:06Z greg.ercolano $".