sptk2 logo
SPTK Home Page
CTreeControl.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <FL/Fl_Pixmap.H>
30#include <FL/Fl_Widget.H>
31#include <sptk5/gui/CGroup.h>
32#include <sptk5/gui/CLayoutClient.h>
33#include <sptk5/gui/CScroll.h>
34
35#include <string>
36#include <vector>
37
38namespace sptk {
39
45class CTreeItem;
46
47class CTreeControl;
48
57
64class SP_EXPORT CTreeItem
65 : public CGroup
66{
70 int m_itemHeight {0};
71
75 int m_indent {0};
76
80 bool m_selected {false};
81
85 Fl_Color m_itemColor[2] {};
86
90 CLayoutClient* m_body {nullptr};
91
95 const Fl_Image* m_openedImage {nullptr};
96
100 const Fl_Image* m_closedImage {nullptr};
101
105 bool m_opened {false};
106
110 CTreeControl* m_tree {nullptr};
111
112protected:
123 CTreeItem* addPathOffset(const std::vector<String>& pathFolders, uint32_t offset, const Fl_Image* openedImage,
124 const Fl_Image* closedImage, const Fl_Image* itemImage = 0L, void* data = 0L);
125
129 static const Fl_Image* treeOpened;
130
134 static const Fl_Image* treeClosed;
135
139 static const Fl_Image* folderOpened;
140
144 static const Fl_Image* folderClosed;
145
149 static const Fl_Image* document;
150
151public:
158 CTreeItem(const char* label, const Fl_Image* openedImage = nullptr, const Fl_Image* closedImage = nullptr,
159 void* data = nullptr);
160
168 CTreeItem* addItem(const char* label, const Fl_Image* openedImage = nullptr, const Fl_Image* closedImage = nullptr,
169 void* data = nullptr);
170
179 CTreeItem* addPath(const std::vector<String>& path, const Fl_Image* openedImage, const Fl_Image* closedImage,
180 const Fl_Image* itemImage = 0L, void* data = 0L);
181
189 CTreeItem* addPath(const std::vector<String>& path, const Fl_Image* itemImage = 0L, void* data = 0L);
190
195 {
196 return m_body;
197 }
198
203 void label(const String& lbl) override;
204
208 const String& label() const override
209 {
210 return m_body->label();
211 }
212
216 void clear() override;
217
222 CTreeItem* findItem(const char* label) const;
223
229 CTreeItem* findData(const void* data) const;
230
238
244
249 {
250 return m_tree;
251 }
252
260 void moveItem(CTreeItem* item, CTreeItem* beforeItem = nullptr);
261
267 void moveItem(CTreeItem* item, int direction);
268
273 void visibleChildren(bool vc);
274
278 void open()
279 {
280 visibleChildren(true);
281 }
282
286 void close()
287 {
288 visibleChildren(false);
289 }
290
294 bool opened() const
295 {
296 return m_opened;
297 }
298
302 uint32_t indent() const
303 {
304 return m_indent;
305 }
306
312 void setImage(const Fl_Image* openedImage, const Fl_Image* closedImage = nullptr)
313 {
314 m_openedImage = openedImage;
315 m_closedImage = closedImage;
316 }
317
323 int handle(int event) override;
324
328 void draw() override;
329
336 bool preferredSize(int& w, int& h) override;
337
341 void resize(int xx, int yy, int ww, int hh) override;
342
347 virtual void select(bool flag);
348
352 bool selected() const
353 {
354 return m_selected;
355 }
356
361
366
371
376 CTreeItem* findLast(bool recursive) const;
377
382 CTreeItem* findNext(bool recursive) const;
383
388 CTreeItem* findPrior(bool recursive) const;
389
393 String className() const override
394 {
395 return "treeitem";
396 }
397
401 static const Fl_Image* getTreeOpened();
402
406 static void setTreeOpened(const Fl_Image* image);
407
411 static const Fl_Image* getTreeClosed();
412
416 static void setTreeClosed(const Fl_Image* image);
417
421 static const Fl_Image* getFolderOpened();
422
426 static void setFolderOpened(const Fl_Image* image);
427
431 static const Fl_Image* getFolderClosed();
432
436 static void setFolderClosed(const Fl_Image* image);
437
441 static const Fl_Image* getDocument();
442
446 static void setDocument(const Fl_Image* image);
447};
448
452using CTreeItemVector = std::vector<CTreeItem*>;
453
461class SP_EXPORT CTreeControl
462 : public CScroll
463{
464 friend class CTreeItem;
465
469 CTreeItemVector m_selectedItems;
470
474 CTreeItem* m_root;
475
479 bool m_tabPressed;
480
484 CTreeItemCreator m_itemCreator;
485
486protected:
493
494public:
501 CTreeControl(const char* label, int layoutSize = 50, CLayoutAlign align = CLayoutAlign::TOP);
502
511 CTreeItem* addItem(const char* label, const Fl_Image* openedImage = nullptr, const Fl_Image* closedImage = nullptr,
512 void* data = nullptr);
513
526 CTreeItem* addPath(const std::vector<String>& path, const Fl_Image* openedImage, const Fl_Image* closedImage,
527 const Fl_Image* itemImage = 0L, void* data = 0L);
528
536 CTreeItem* addPath(const std::vector<String>& path, const Fl_Image* itemImage = 0L, void* data = 0L);
537
543 CTreeItem* findItem(const char* item) const;
544
550 CTreeItem* findData(const void* data) const;
551
555 void clear() override
556 {
557 m_selectedItems.clear();
558 m_root->clear();
559 }
560
566 static void moveItem(CTreeItem* item, CTreeItem* beforeItem = nullptr);
567
573
578 {
579 return m_root;
580 }
581
586 {
587 if (m_selectedItems.empty())
588 {
589 return 0;
590 }
591
592 return m_selectedItems[0];
593 }
594
600 void selectOnly(CTreeItem* item, bool giveFocus = false);
601
607
613 int handle(int event) override;
614
619 {
620 m_itemCreator = ic;
621 }
622
629 void load(const xdoc::SNode& node, CLayoutXMLmode xmlMode) override;
630
636 void save(const xdoc::SNode& node, CLayoutXMLmode xmlMode) const override;
637
641 String className() const override
642 {
643 return "tree";
644 }
645};
649} // namespace sptk
SPTK group widget.
Definition: CGroup.h:50
Definition: CLayoutClient.h:82
Scroll area widget.
Definition: CScroll.h:65
Tree widget.
Definition: CTreeControl.h:463
CTreeItem * addItem(const char *label, const Fl_Image *openedImage=nullptr, const Fl_Image *closedImage=nullptr, void *data=nullptr)
Adds a child item to the root item. If the closedImage parameter is omitted the openedImage is used i...
CTreeItem * findItem(const char *item) const
Finds an item by the item label in the whole tree.
void load(const xdoc::SNode &node, CLayoutXMLmode xmlMode) override
Loads group controls data from XML node.
CTreeControl(const char *label, int layoutSize=50, CLayoutAlign align=CLayoutAlign::TOP)
The constructor.
CTreeItem * addPath(const std::vector< String > &path, const Fl_Image *openedImage, const Fl_Image *closedImage, const Fl_Image *itemImage=0L, void *data=0L)
Adds a child item to the root item using the path.
CTreeItem * root() const
Reports the root item.
Definition: CTreeControl.h:577
void clear() override
Removes all the items in the tree.
Definition: CTreeControl.h:555
void removeItem(CTreeItem *item)
Removes all the item and underlying structure from the tree.
int handle(int event) override
Special handle() function.
CTreeItem * findData(const void *data) const
Finds an item by the item user data in the whole tree.
void save(const xdoc::SNode &node, CLayoutXMLmode xmlMode) const override
Saves group controls data into XML node.
void selectOnly(CTreeItem *item, bool giveFocus=false)
Selects only one item.
void itemCreator(CTreeItemCreator ic)
The tree item creator creates a tree item' body,.
Definition: CTreeControl.h:618
CTreeItem * selected() const
Reports the currently selected item.
Definition: CTreeControl.h:585
static void moveItem(CTreeItem *item, CTreeItem *beforeItem=nullptr)
Moves the item in the tree.
void makeVisible(CTreeItem *item)
Makes the item visible if it's outside the visible area.
String className() const override
Returns widget class name (internal SPTK RTTI).
Definition: CTreeControl.h:641
CTreeItem * addPath(const std::vector< String > &path, const Fl_Image *itemImage=0L, void *data=0L)
static CLayoutClient * defaultItemCreator(CTreeItem *item)
The default tree item creator.
Tree widget item.
Definition: CTreeControl.h:66
void open()
Shows all the child items. Changes the item state accordingly.
Definition: CTreeControl.h:278
bool selected() const
Returns selection state of the item.
Definition: CTreeControl.h:352
static const Fl_Image * getTreeClosed()
void draw() override
The draw function. Internal. See Fl_Widget for details.
void label(const String &lbl) override
Sets the item's caption.
void close()
Hides all the child items. Changes the item state accordingly.
Definition: CTreeControl.h:286
virtual void select(bool flag)
Selects or unselects item.
const String & label() const override
Returns the item's caption.
Definition: CTreeControl.h:208
CTreeItem * findItem(const char *label) const
Finds an item among the child items.
CTreeItem * addPathOffset(const std::vector< String > &pathFolders, uint32_t offset, const Fl_Image *openedImage, const Fl_Image *closedImage, const Fl_Image *itemImage=0L, void *data=0L)
static const Fl_Image * treeClosed
Definition: CTreeControl.h:134
static const Fl_Image * folderOpened
Definition: CTreeControl.h:139
static void setDocument(const Fl_Image *image)
void moveItem(CTreeItem *item, CTreeItem *beforeItem=nullptr)
Moves the immediate child before another immediate child.
static const Fl_Image * document
Definition: CTreeControl.h:149
static const Fl_Image * getFolderClosed()
void setImage(const Fl_Image *openedImage, const Fl_Image *closedImage=nullptr)
Sets image(s) for the item.
Definition: CTreeControl.h:312
CLayoutClient * body()
The widget that represents the item without children.
Definition: CTreeControl.h:194
String className() const override
Returns widget class name (internal SPTK RTTI).
Definition: CTreeControl.h:393
CTreeItem * findFirst() const
Selects the first visible item in the child tree.
CTreeItem * addItem(const char *label, const Fl_Image *openedImage=nullptr, const Fl_Image *closedImage=nullptr, void *data=nullptr)
void removeItem(CTreeItem *item)
Removes an item from the child items.
CTreeItem * findData(const void *data) const
Finds an item by the item user data in the whole tree.
bool preferredSize(int &w, int &h) override
Computes the preferred size of the item based on the font of the parent widget, the image size,...
static const Fl_Image * getFolderOpened()
void moveItem(CTreeItem *item, int direction)
Moves the immediate child up or down in the list of items.
static const Fl_Image * treeOpened
Definition: CTreeControl.h:129
static const Fl_Image * getDocument()
CTreeItem * parentItem() const
Returns the parent item.
int handle(int event) override
The event handle function. Internal. See Fl_Widget for details.
CTreeItem * findPrior(bool recursive) const
Selects the prior visible item in the child tree.
void clear() override
Removes all the child items of the item.
CTreeItem * addPath(const std::vector< String > &path, const Fl_Image *itemImage=0L, void *data=0L)
void resize(int xx, int yy, int ww, int hh) override
Resizes item and sub items.
CTreeControl * tree() const
Returns the tree control this item belongs to.
Definition: CTreeControl.h:248
void visibleChildren(bool vc)
Shows or hides all the child items. Changes the item state accordingly.
CTreeItem * findNext(bool recursive) const
Selects the next visible item in the child tree.
bool selectNext()
Selects the next item in the tree.
static void setFolderClosed(const Fl_Image *image)
static const Fl_Image * folderClosed
Definition: CTreeControl.h:144
static void setFolderOpened(const Fl_Image *image)
CTreeItem * findLast(bool recursive) const
Selects the last visible item in the child tree.
bool opened() const
Reports the item state - opened or closed.
Definition: CTreeControl.h:294
static void setTreeClosed(const Fl_Image *image)
bool selectPrior()
Selects the prior item in the tree.
uint32_t indent() const
Reports the item indent from the left.
Definition: CTreeControl.h:302
CTreeItem * addPath(const std::vector< String > &path, const Fl_Image *openedImage, const Fl_Image *closedImage, const Fl_Image *itemImage=0L, void *data=0L)
static const Fl_Image * getTreeOpened()
static void setTreeOpened(const Fl_Image *image)
CTreeItem(const char *label, const Fl_Image *openedImage=nullptr, const Fl_Image *closedImage=nullptr, void *data=nullptr)
Definition: String.h:49
std::vector< CTreeItem * > CTreeItemVector
Vector of CTreeItem pointers.
Definition: CTreeControl.h:452
CLayoutAlign
Definition: CLayoutClient.h:44
CLayoutXMLmode
Definition: CLayoutClient.h:57
CLayoutClient *(*)(CTreeItem *item) CTreeItemCreator
A callback that allows to create a widget of required type and use it as an item body.
Definition: CTreeControl.h:56
@ TOP
Align to the right.

Fri Oct 14 2022 09:58:31: SPTK 5.4.1