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