You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
11KB

  1. //
  2. // "$Id: Fl_Tree_Item.H 8340 2011-01-30 20:22:06Z greg.ercolano $"
  3. //
  4. #ifndef FL_TREE_ITEM_H
  5. #define FL_TREE_ITEM_H
  6. #include <FL/Fl.H>
  7. #include <FL/Fl_Widget.H>
  8. #include <FL/Fl_Image.H>
  9. #include <FL/fl_draw.H>
  10. #include <FL/Fl_Tree_Item_Array.H>
  11. #include <FL/Fl_Tree_Prefs.H>
  12. //////////////////////
  13. // FL/Fl_Tree_Item.H
  14. //////////////////////
  15. //
  16. // Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
  17. // Copyright (C) 2009-2010 by Greg Ercolano.
  18. //
  19. // This library is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Library General Public
  21. // License as published by the Free Software Foundation; either
  22. // version 2 of the License, or (at your option) any later version.
  23. //
  24. // This library is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. // Library General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU Library General Public
  30. // License along with this library; if not, write to the Free Software
  31. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  32. // USA.
  33. //
  34. ///
  35. /// \file
  36. /// \brief This file contains the definitions for Fl_Tree_Item
  37. ///
  38. /// \brief Tree item
  39. ///
  40. /// This class is a single tree item, and manages all of the item's attributes.
  41. /// Fl_Tree_Item is used by Fl_Tree, which is comprised of many instances of Fl_Tree_Item.
  42. ///
  43. /// Fl_Tree_Item is hierarchical; it dynamically manages an Fl_Tree_Item_Array of children
  44. /// that are themselves instances of Fl_Tree_Item. Each item can have zero or more children.
  45. /// When an item has children, close() and open() can be used to hide or show them.
  46. ///
  47. /// Items have their own attributes; font size, face, color.
  48. /// Items maintain their own hierarchy of children.
  49. ///
  50. /// When you make changes to items, you'll need to tell the tree to redraw()
  51. /// for the changes to show up.
  52. ///
  53. class FL_EXPORT Fl_Tree_Item {
  54. const char *_label; // label (memory managed)
  55. Fl_Font _labelfont; // label's font face
  56. Fl_Fontsize _labelsize; // label's font size
  57. Fl_Color _labelfgcolor; // label's fg color
  58. Fl_Color _labelbgcolor; // label's bg color
  59. char _open; // item is open?
  60. char _visible; // item is visible?
  61. char _active; // item activated?
  62. char _selected; // item selected?
  63. int _xywh[4]; // xywh of this widget (if visible)
  64. int _collapse_xywh[4]; // xywh of collapse icon (if any)
  65. int _label_xywh[4]; // xywh of label
  66. Fl_Widget *_widget; // item's label widget (optional)
  67. Fl_Image *_usericon; // item's user-specific icon (optional)
  68. Fl_Tree_Item_Array _children; // array of child items
  69. Fl_Tree_Item *_parent; // parent item (=0 if root)
  70. void *_userdata; // user data that can be associated with an item
  71. protected:
  72. void show_widgets();
  73. void hide_widgets();
  74. void draw_vertical_connector(int x, int y1, int y2, const Fl_Tree_Prefs &prefs);
  75. void draw_horizontal_connector(int x1, int x2, int y, const Fl_Tree_Prefs &prefs);
  76. public:
  77. Fl_Tree_Item(const Fl_Tree_Prefs &prefs); // CTOR
  78. ~Fl_Tree_Item(); // DTOR
  79. Fl_Tree_Item(const Fl_Tree_Item *o); // COPY CTOR
  80. int x() const { return(_xywh[0]); }
  81. int y() const { return(_xywh[1]); }
  82. int w() const { return(_xywh[2]); }
  83. int h() const { return(_xywh[3]); }
  84. void draw(int X, int &Y, int W, Fl_Widget *tree, Fl_Tree_Item *itemfocus, const Fl_Tree_Prefs &prefs, int lastchild=1);
  85. void show_self(const char *indent = "") const;
  86. void label(const char *val);
  87. const char *label() const;
  88. /// Set a user-data value for the item.
  89. inline void user_data( void* data ) { _userdata = data; }
  90. /// Retrieve the user-data value that has been assigned to the item.
  91. inline void* user_data() const { return _userdata; }
  92. /// Set item's label font face.
  93. void labelfont(Fl_Font val) {
  94. _labelfont = val;
  95. }
  96. /// Get item's label font face.
  97. Fl_Font labelfont() const {
  98. return(_labelfont);
  99. }
  100. /// Set item's label font size.
  101. void labelsize(Fl_Fontsize val) {
  102. _labelsize = val;
  103. }
  104. /// Get item's label font size.
  105. Fl_Fontsize labelsize() const {
  106. return(_labelsize);
  107. }
  108. /// Set item's label foreground text color.
  109. void labelfgcolor(Fl_Color val) {
  110. _labelfgcolor = val;
  111. }
  112. /// Set item's label text color.
  113. void labelcolor(Fl_Color val) {
  114. _labelfgcolor = val;
  115. }
  116. /// Return item's label text color.
  117. Fl_Color labelcolor() const {
  118. return(_labelfgcolor);
  119. }
  120. /// Return item's label foreground text color.
  121. Fl_Color labelfgcolor() const {
  122. return(_labelfgcolor);
  123. }
  124. /// Set item's label background color.
  125. void labelbgcolor(Fl_Color val) {
  126. _labelbgcolor = val;
  127. }
  128. /// Return item's background text color.
  129. Fl_Color labelbgcolor() const {
  130. return(_labelbgcolor);
  131. }
  132. /// Assign an FLTK widget to this item.
  133. void widget(Fl_Widget *val) {
  134. _widget = val;
  135. }
  136. /// Return FLTK widget assigned to this item.
  137. Fl_Widget *widget() const {
  138. return(_widget);
  139. }
  140. /// Return the number of children this item has.
  141. int children() const {
  142. return(_children.total());
  143. }
  144. /// Return the child item for the given 'index'.
  145. Fl_Tree_Item *child(int index) {
  146. return(_children[index]);
  147. }
  148. /// Return the const child item for the given 'index'.
  149. const Fl_Tree_Item *child(int t) const;
  150. /// See if this item has children.
  151. int has_children() const {
  152. return(children());
  153. }
  154. int find_child(const char *name);
  155. int find_child(Fl_Tree_Item *item);
  156. int remove_child(Fl_Tree_Item *item);
  157. int remove_child(const char *new_label);
  158. void clear_children();
  159. void swap_children(int ax, int bx);
  160. int swap_children(Fl_Tree_Item *a, Fl_Tree_Item *b);
  161. const Fl_Tree_Item *find_child_item(char **arr) const; // const
  162. Fl_Tree_Item *find_child_item(char **arr); // non-const
  163. const Fl_Tree_Item *find_item(char **arr) const; // const
  164. Fl_Tree_Item *find_item(char **arr); // non-const
  165. //////////////////
  166. // Adding items
  167. //////////////////
  168. Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs, const char *new_label);
  169. Fl_Tree_Item *add(const Fl_Tree_Prefs &prefs, char **arr);
  170. Fl_Tree_Item *insert(const Fl_Tree_Prefs &prefs, const char *new_label, int pos=0);
  171. Fl_Tree_Item *insert_above(const Fl_Tree_Prefs &prefs, const char *new_label);
  172. int depth() const;
  173. Fl_Tree_Item *prev();
  174. Fl_Tree_Item *next();
  175. Fl_Tree_Item *next_sibling();
  176. Fl_Tree_Item *prev_sibling();
  177. Fl_Tree_Item *next_displayed(Fl_Tree_Prefs &prefs);
  178. Fl_Tree_Item *prev_displayed(Fl_Tree_Prefs &prefs);
  179. /// Return the parent for this item. Returns NULL if we are the root.
  180. Fl_Tree_Item *parent() {
  181. return(_parent);
  182. }
  183. /// Return the const parent for this item. Returns NULL if we are the root.
  184. const Fl_Tree_Item *parent() const {
  185. return(_parent);
  186. }
  187. /// Set the parent for this item.
  188. /// Should only be used by Fl_Tree's internals.
  189. ///
  190. void parent(Fl_Tree_Item *val) {
  191. _parent = val;
  192. }
  193. //////////////////
  194. // State
  195. //////////////////
  196. void open();
  197. void close();
  198. /// See if the item is 'open'.
  199. int is_open() const {
  200. return(_open?1:0);
  201. }
  202. /// See if the item is 'closed'.
  203. int is_close() const {
  204. return(_open?0:1);
  205. }
  206. /// Toggle the item's open/closed state.
  207. void open_toggle() {
  208. _open?close():open();
  209. }
  210. /// Change the item's selection state to the optionally specified 'val'.
  211. /// If 'val' is not specified, the item will be selected.
  212. ///
  213. void select(int val=1) {
  214. _selected = val;
  215. }
  216. /// Toggle the item's selection state.
  217. void select_toggle() {
  218. if ( is_selected() ) {
  219. deselect(); // deselect if selected
  220. } else {
  221. select(); // select if deselected
  222. }
  223. }
  224. /// Select self and all children
  225. /// Returns count of how many items were in the 'deselected' state,
  226. /// ie. how many items were "changed".
  227. ///
  228. int select_all() {
  229. int count = 0;
  230. if ( ! is_selected() ) {
  231. select();
  232. ++count;
  233. }
  234. for ( int t=0; t<children(); t++ ) {
  235. count += child(t)->select_all();
  236. }
  237. return(count);
  238. }
  239. /// Disable the item's selection state.
  240. void deselect() {
  241. _selected = 0;
  242. }
  243. /// Deselect self and all children
  244. /// Returns count of how many items were in the 'selected' state,
  245. /// ie. how many items were "changed".
  246. ///
  247. int deselect_all() {
  248. int count = 0;
  249. if ( is_selected() ) {
  250. deselect();
  251. ++count;
  252. }
  253. for ( int t=0; t<children(); t++ ) {
  254. count += child(t)->deselect_all();
  255. }
  256. return(count);
  257. }
  258. /// See if the item is selected.
  259. char is_selected() const {
  260. return(_selected);
  261. }
  262. /// Change the item's activation state to the optionally specified 'val'.
  263. ///
  264. /// When deactivated, the item will be 'grayed out'; the callback()
  265. /// won't be invoked if the user clicks on the label. If the item
  266. /// has a widget() associated with the item, its activation state
  267. /// will be changed as well.
  268. ///
  269. /// If 'val' is not specified, the item will be activated.
  270. ///
  271. void activate(int val=1) {
  272. _active = val;
  273. if ( _widget && val != (int)_widget->active() ) {
  274. if ( val ) {
  275. _widget->activate();
  276. } else {
  277. _widget->deactivate();
  278. }
  279. _widget->redraw();
  280. }
  281. }
  282. /// Deactivate the item; the callback() won't be invoked when clicked.
  283. /// Same as activate(0)
  284. ///
  285. void deactivate() {
  286. activate(0);
  287. }
  288. /// See if the item is activated.
  289. char is_activated() const {
  290. return(_active);
  291. }
  292. /// See if the item is activated.
  293. char is_active() const {
  294. return(_active);
  295. }
  296. /// See if the item is visible.
  297. int visible() const {
  298. return(_visible ? 1 : 0);
  299. }
  300. int visible_r() const;
  301. /// Set the user icon's image. '0' will disable.
  302. void usericon(Fl_Image *val) {
  303. _usericon = val;
  304. }
  305. /// Get the user icon. Returns '0' if disabled.
  306. Fl_Image *usericon() const {
  307. return(_usericon);
  308. }
  309. //////////////////
  310. // Events
  311. //////////////////
  312. const Fl_Tree_Item *find_clicked(const Fl_Tree_Prefs &prefs) const;
  313. Fl_Tree_Item *find_clicked(const Fl_Tree_Prefs &prefs);
  314. int event_on_collapse_icon(const Fl_Tree_Prefs &prefs) const;
  315. int event_on_label(const Fl_Tree_Prefs &prefs) const;
  316. /// Is this item the root of the tree?
  317. int is_root() const {
  318. return(_parent==0?1:0);
  319. }
  320. };
  321. #endif /*FL_TREE_ITEM_H*/
  322. //
  323. // End of "$Id: Fl_Tree_Item.H 8340 2011-01-30 20:22:06Z greg.ercolano $".
  324. //