Update list of wide characters
[centerim5.git] / cppconsui / TreeView.h
blob1ecaf2e89252515b97335dfdd995e48cdc97fc66
1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 //
4 // This file is part of CenterIM.
5 //
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef TREEVIEW_H
20 #define TREEVIEW_H
22 #include "Button.h"
23 #include "Container.h"
25 #include "tree.hh"
27 namespace CppConsUI {
29 class TreeView : public Container {
30 protected:
31 class TreeNode;
33 public:
34 class ToggleCollapseButton : public Button {
35 public:
36 ToggleCollapseButton(int w, int h, const char *text_ = nullptr);
37 explicit ToggleCollapseButton(const char *text_ = nullptr);
38 virtual ~ToggleCollapseButton() override {}
40 // Widget
41 virtual void setParent(Container &parent) override;
43 private:
44 CONSUI_DISABLE_COPY(ToggleCollapseButton);
47 /// Node drawing style.
48 enum Style {
49 STYLE_NORMAL, ///< Draw "[+]" if the node is collapsed.
50 STYLE_VOID, ///< Don't draw any extra information.
53 typedef tree<TreeNode> TheTree;
54 typedef TheTree::pre_order_iterator NodeReference;
55 typedef TheTree::sibling_iterator SiblingIterator;
57 TreeView(int w, int h);
58 virtual ~TreeView() override;
60 // Widget
61 virtual int draw(Curses::ViewPort area, Error &error) override;
62 virtual void cleanFocus() override;
63 virtual bool grabFocus() override;
65 // Container
66 virtual void clear() override;
67 virtual bool isWidgetVisible(const Widget &widget) const override;
68 virtual bool setFocusChild(Widget &child) override;
69 virtual void getFocusChain(
70 FocusChain &focus_chain, FocusChain::iterator parent) override;
71 virtual void onChildMoveResize(
72 Widget &activator, const Rect &oldsize, const Rect &newsize) override;
73 virtual void onChildWishSizeChange(
74 Widget &activator, const Size &oldsize, const Size &newsize) override;
75 virtual void onChildVisible(Widget &activator, bool visible) override;
77 /// Folds/unfolds given node.
78 virtual void setCollapsed(NodeReference node, bool collapsed);
80 /// Toggles folding for given node.
81 virtual void toggleCollapsed(NodeReference node);
83 /// Convenient method to toggle folding of the current active node.
84 virtual void actionToggleCollapsed();
86 /// Returns root node reference.
87 virtual NodeReference getRootNode() const { return thetree_.begin(); }
89 /// Inserts a widget before a specified position. TreeView takes ownership of
90 /// the widget.
91 virtual NodeReference insertNode(NodeReference position, Widget &widget);
93 /// Inserts a widget after a specified position. TreeView takes ownership of
94 /// the widget.
95 virtual NodeReference insertNodeAfter(NodeReference position, Widget &widget);
97 /// Prepends a widget to a specified parent. TreeView takes ownership of the
98 /// widget.
99 virtual NodeReference prependNode(NodeReference parent, Widget &widget);
101 /// Appends a widget to a specified parent. TreeView takes ownership of the
102 /// widget.
103 virtual NodeReference appendNode(NodeReference parent, Widget &widget);
105 /// Deletes given node.
106 virtual void deleteNode(NodeReference node, bool keepchildren);
108 /// Deletes all children of given node.
109 virtual void deleteNodeChildren(NodeReference node, bool keepchildren);
111 /// Returns reference to currently focused node/widget.
112 virtual NodeReference getSelectedNode() const;
114 /// Returns node depth.
115 virtual int getNodeDepth(NodeReference node) const;
117 /// Detaches a given node from its current location and moves it before a
118 /// given position.
119 virtual void moveNodeBefore(NodeReference node, NodeReference position);
121 /// Detaches a given node from its current location and moves it after a given
122 /// position.
123 virtual void moveNodeAfter(NodeReference node, NodeReference position);
125 /// Detaches a given node from its current location and appents it to a new
126 /// parent.
127 virtual void setNodeParent(NodeReference node, NodeReference newparent);
129 virtual void setNodeStyle(NodeReference node, Style s);
130 virtual Style getNodeStyle(NodeReference node) const;
132 protected:
133 class TreeNode {
134 // Note: If TreeNode is just protected/private and all its variables are
135 // public, then variables can be accessed from outside using NodeReference.
136 friend class TreeView;
138 public:
139 TreeView *getTreeView() const { return treeview; }
140 bool isCollapsed() const { return collapsed; }
141 Style getStyle() const { return style; }
142 Widget *getWidget() const { return widget; }
144 private:
145 /// Pointer to TreeView this node belongs to.
146 TreeView *treeview;
148 /// Flag whether the subtree is folded or not.
149 bool collapsed;
151 /// Selected node drawing style.
152 Style style;
154 /// Widget to show. Not const because width is changed to fit. E.g. labels
155 /// can show '...' when the text does not fit in the given space.
156 Widget *widget;
159 TheTree thetree_;
160 NodeReference focus_node_;
162 // Widget
163 virtual void updateArea() override;
165 // Container
166 using Container::addWidget;
167 using Container::removeWidget;
168 using Container::moveWidgetBefore;
169 using Container::moveWidgetAfter;
171 virtual int drawNode(SiblingIterator node, int *out_height,
172 Curses::ViewPort &area, Error &error);
174 virtual TreeNode addNode(Widget &widget);
176 virtual void fixFocus();
178 virtual NodeReference findNode(const Widget &child) const;
180 virtual bool isNodeOpenable(SiblingIterator &node) const;
181 virtual bool isNodeVisible(NodeReference &node) const;
183 virtual int repositionChildren(
184 SiblingIterator node, int top, bool in_visibility);
186 private:
187 CONSUI_DISABLE_COPY(TreeView);
189 void actionCollapse();
190 void actionExpand();
192 void declareBindables();
195 } // namespace CppConsUI
197 #endif // TREEVIEW_H
199 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: