xref: /haiku/src/apps/debuganalyzer/gui/table/TreeTable.h (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef TREE_TABLE_H
6 #define TREE_TABLE_H
7 
8 #include <vector>
9 
10 #include <ColumnTypes.h>
11 #include <Variant.h>
12 
13 #include "table/AbstractTable.h"
14 #include "table/TableColumn.h"
15 
16 
17 class TreeTable;
18 class TreeTableModel;
19 class TreeTableNode;
20 class TreeTableRow;
21 
22 
23 class TreeTablePath {
24 public:
25 								TreeTablePath();
26 								TreeTablePath(const TreeTablePath& other);
27 								TreeTablePath(const TreeTablePath& other,
28 									int32 childIndex);
29 								~TreeTablePath();
30 
31 		bool					AddComponent(int32 childIndex);
32 		int32					RemoveLastComponent();
33 		void					Clear();
34 
35 		int32					CountComponents() const;
36 		int32					ComponentAt(int32 index) const;
37 
38 		TreeTablePath&			operator=(const TreeTablePath& other);
39 		bool					operator==(const TreeTablePath& other) const;
40 		bool					operator!=(const TreeTablePath& other) const;
41 
42 private:
43 		typedef std::vector<int32> ComponentVector;
44 
45 private:
46 		ComponentVector			fComponents;
47 };
48 
49 
50 class TreeTableModelListener {
51 public:
52 	virtual						~TreeTableModelListener();
53 
54 	virtual	void				TableNodesAdded(TreeTableModel* model,
55 									const TreeTablePath& path, int32 childIndex,
56 									int32 count);
57 	virtual	void				TableNodesRemoved(TreeTableModel* model,
58 									const TreeTablePath& path, int32 childIndex,
59 									int32 count);
60 	virtual	void				TableNodesChanged(TreeTableModel* model,
61 									const TreeTablePath& path, int32 childIndex,
62 									int32 count);
63 };
64 
65 
66 class TreeTableModel : public AbstractTableModelBase {
67 public:
68 	virtual						~TreeTableModel();
69 
70 	virtual	void*				Root() const = 0;
71 									// the root itself isn't shown
72 
73 	virtual	int32				CountChildren(void* parent) const = 0;
74 	virtual	void*				ChildAt(void* parent, int32 index) const = 0;
75 
76 	virtual	bool				GetValueAt(void* object, int32 columnIndex,
77 									BVariant& value) = 0;
78 
79 	virtual	void*				NodeForPath(const TreeTablePath& path) const;
80 
81 	virtual	bool				AddListener(TreeTableModelListener* listener);
82 	virtual	void				RemoveListener(
83 									TreeTableModelListener* listener);
84 
85 protected:
86 			typedef BObjectList<TreeTableModelListener> ListenerList;
87 
88 protected:
89 			void				NotifyNodesAdded(const TreeTablePath& path,
90 									int32 childIndex, int32 count);
91 			void				NotifyNodesRemoved(const TreeTablePath& path,
92 									int32 childIndex, int32 count);
93 			void				NotifyNodesChanged(const TreeTablePath& path,
94 									int32 childIndex, int32 count);
95 
96 protected:
97 			ListenerList		fListeners;
98 };
99 
100 
101 class TreeTableSelectionModel {
102 public:
103 								TreeTableSelectionModel(TreeTable* table);
104 								~TreeTableSelectionModel();
105 
106 			int32				CountNodes();
107 			void*				NodeAt(int32 index);
108 			bool				GetPathAt(int32 index, TreeTablePath& _path);
109 
110 private:
111 			friend class TreeTable;
112 
113 private:
114 			void				_SelectionChanged();
115 			void				_Update();
116 			TreeTableNode*		_NodeAt(int32 index);
117 
118 private:
119 			TreeTable*			fTreeTable;
120 			TreeTableNode**		fNodes;
121 			int32				fNodeCount;
122 };
123 
124 
125 class TreeTableListener {
126 public:
127 	virtual						~TreeTableListener();
128 
129 	virtual	void				TreeTableSelectionChanged(TreeTable* table);
130 	virtual	void				TreeTableNodeInvoked(TreeTable* table,
131 									const TreeTablePath& path);
132 	virtual	void				TreeTableNodeExpandedChanged(TreeTable* table,
133 									const TreeTablePath& path, bool expanded);
134 
135 	virtual	void				TreeTableCellMouseDown(TreeTable* table,
136 									const TreeTablePath& path,
137 									int32 columnIndex, BPoint screenWhere,
138 									uint32 buttons);
139 	virtual	void				TreeTableCellMouseUp(TreeTable* table,
140 									const TreeTablePath& path,
141 									int32 columnIndex, BPoint screenWhere,
142 									uint32 buttons);
143 };
144 
145 
146 class TreeTable : public AbstractTable, private TreeTableModelListener {
147 public:
148 								TreeTable(const char* name, uint32 flags,
149 									border_style borderStyle = B_NO_BORDER,
150 									bool showHorizontalScrollbar = true);
151 								TreeTable(TreeTableModel* model,
152 									const char* name, uint32 flags,
153 									border_style borderStyle = B_NO_BORDER,
154 									bool showHorizontalScrollbar = true);
155 	virtual						~TreeTable();
156 
157 			bool				SetTreeTableModel(TreeTableModel* model);
158 			TreeTableModel*		GetTreeTableModel() const	{ return fModel; }
159 
160 			TreeTableSelectionModel* SelectionModel();
161 
162 			void				SelectNode(const TreeTablePath& path,
163 									bool extendSelection);
164 			void				DeselectNode(const TreeTablePath& path);
165 			void				DeselectAllNodes();
166 
167 			bool				IsNodeExpanded(const TreeTablePath& path) const;
168 			void				SetNodeExpanded(const TreeTablePath& path,
169 									bool expanded,
170 									bool expandAncestors = false);
171 
172 			void				ScrollToNode(const TreeTablePath& path);
173 
174 			bool				AddTreeTableListener(
175 									TreeTableListener* listener);
176 			void				RemoveTreeTableListener(
177 									TreeTableListener* listener);
178 
179 protected:
180 	virtual	void				SelectionChanged();
181 
182 	virtual	AbstractColumn*		CreateColumn(TableColumn* column);
183 
184 	virtual	void				ColumnMouseDown(AbstractColumn* column,
185 									BRow* row, BField* field,
186 									BPoint screenWhere, uint32 buttons);
187 	virtual	void				ColumnMouseUp(AbstractColumn* column,
188 									BRow* row, BField* field,
189 									BPoint screenWhere, uint32 buttons);
190 
191 private:
192 	virtual	void				TableNodesAdded(TreeTableModel* model,
193 									const TreeTablePath& path, int32 childIndex,
194 									int32 count);
195 	virtual	void				TableNodesRemoved(TreeTableModel* model,
196 									const TreeTablePath& path, int32 childIndex,
197 									int32 count);
198 	virtual	void				TableNodesChanged(TreeTableModel* model,
199 									const TreeTablePath& path, int32 childIndex,
200 									int32 count);
201 
202 private:
203 			class Column;
204 
205 			friend class TreeTableSelectionModel;
206 
207 			typedef BObjectList<TreeTableListener>	ListenerList;
208 
209 private:
210 	virtual	void				ExpandOrCollapse(BRow* row, bool expand);
211 	virtual	void				ItemInvoked();
212 
213 			bool				_AddChildRows(TreeTableNode* parentNode,
214 									int32 childIndex, int32 count,
215 									int32 columnCount);
216 			void				_RemoveChildRows(TreeTableNode* parentNode,
217 									int32 childIndex, int32 count);
218 
219 			void				_SetNodeExpanded(TreeTableNode* node,
220 									bool expanded,
221 									bool expandAncestors = false);
222 
223 			TreeTableNode*		_NodeForPath(const TreeTablePath& path) const;
224 			void				_GetPathForNode(TreeTableNode* node,
225 									TreeTablePath& _path) const;
226 
227 private:
228 			TreeTableModel*		fModel;
229 			TreeTableNode*		fRootNode;
230 			TreeTableSelectionModel fSelectionModel;
231 			ListenerList		fListeners;
232 			int32				fIgnoreSelectionChange;
233 };
234 
235 
236 #endif	// TREE_TABLE_H
237