1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
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/>.
23 #include "Container.h"
29 class TreeView
: public Container
{
34 class ToggleCollapseButton
: public Button
{
36 ToggleCollapseButton(int w
, int h
, const char *text_
= nullptr);
37 explicit ToggleCollapseButton(const char *text_
= nullptr);
38 virtual ~ToggleCollapseButton() override
{}
41 virtual void setParent(Container
&parent
) override
;
44 CONSUI_DISABLE_COPY(ToggleCollapseButton
);
47 /// Node drawing 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
;
61 virtual int draw(Curses::ViewPort area
, Error
&error
) override
;
62 virtual void cleanFocus() override
;
63 virtual bool grabFocus() override
;
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
91 virtual NodeReference
insertNode(NodeReference position
, Widget
&widget
);
93 /// Inserts a widget after a specified position. TreeView takes ownership of
95 virtual NodeReference
insertNodeAfter(NodeReference position
, Widget
&widget
);
97 /// Prepends a widget to a specified parent. TreeView takes ownership of the
99 virtual NodeReference
prependNode(NodeReference parent
, Widget
&widget
);
101 /// Appends a widget to a specified parent. TreeView takes ownership of the
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
119 virtual void moveNodeBefore(NodeReference node
, NodeReference position
);
121 /// Detaches a given node from its current location and moves it after a given
123 virtual void moveNodeAfter(NodeReference node
, NodeReference position
);
125 /// Detaches a given node from its current location and appents it to a new
127 virtual void setNodeParent(NodeReference node
, NodeReference newparent
);
129 virtual void setNodeStyle(NodeReference node
, Style s
);
130 virtual Style
getNodeStyle(NodeReference node
) const;
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
;
139 TreeView
*getTreeView() const { return treeview
; }
140 bool isCollapsed() const { return collapsed
; }
141 Style
getStyle() const { return style
; }
142 Widget
*getWidget() const { return widget
; }
145 /// Pointer to TreeView this node belongs to.
148 /// Flag whether the subtree is folded or not.
151 /// Selected node drawing 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.
160 NodeReference focus_node_
;
163 virtual void updateArea() override
;
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
);
187 CONSUI_DISABLE_COPY(TreeView
);
189 void actionCollapse();
192 void declareBindables();
195 } // namespace CppConsUI
199 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: