xref: /haiku/src/apps/debuganalyzer/gui/table/Table.h (revision 857b0c2bef2de29d8f02f6daf7c8379bf782dccb)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef TABLE_H
6 #define TABLE_H
7 
8 #include <ColumnTypes.h>
9 
10 #include <ObjectList.h>
11 #include <util/DoublyLinkedList.h>
12 #include <Variant.h>
13 
14 #include "table/AbstractTable.h"
15 #include "table/TableColumn.h"
16 
17 
18 class Table;
19 class TableModel;
20 
21 
22 class TableModelListener {
23 public:
24 	virtual						~TableModelListener();
25 
26 	virtual	void				TableRowsAdded(TableModel* model,
27 									int32 rowIndex, int32 count);
28 	virtual	void				TableRowsRemoved(TableModel* model,
29 									int32 rowIndex, int32 count);
30 	virtual	void				TableRowsChanged(TableModel* model,
31 									int32 rowIndex, int32 count);
32 };
33 
34 
35 class TableModel : public AbstractTableModelBase {
36 public:
37 	virtual						~TableModel();
38 
39 	virtual	int32				CountRows() const = 0;
40 
41 	virtual	bool				GetValueAt(int32 rowIndex, int32 columnIndex,
42 									BVariant& value) = 0;
43 
44 	virtual	bool				AddListener(TableModelListener* listener);
45 	virtual	void				RemoveListener(TableModelListener* listener);
46 
47 protected:
48 			typedef BObjectList<TableModelListener> ListenerList;
49 
50 protected:
51 			void				NotifyRowsAdded(int32 rowIndex, int32 count);
52 			void				NotifyRowsRemoved(int32 rowIndex, int32 count);
53 			void				NotifyRowsChanged(int32 rowIndex, int32 count);
54 
55 protected:
56 			ListenerList		fListeners;
57 };
58 
59 
60 class TableSelectionModel {
61 public:
62 								TableSelectionModel(Table* table);
63 								~TableSelectionModel();
64 
65 			int32				CountRows();
66 			int32				RowAt(int32 index);
67 
68 private:
69 			friend class Table;
70 
71 private:
72 			void				_SelectionChanged();
73 			void				_Update();
74 
75 private:
76 			Table*				fTable;
77 			int32*				fRows;
78 			int32				fRowCount;
79 };
80 
81 
82 class TableToolTipProvider {
83 public:
84 	virtual						~TableToolTipProvider();
85 
86 	virtual	bool				GetToolTipForTableCell(int32 rowIndex,
87 									int32 columnIndex, BToolTip** _tip) = 0;
88 									// columnIndex can be -1, if not in a column
89 };
90 
91 
92 class TableListener {
93 public:
94 	virtual						~TableListener();
95 
96 	virtual	void				TableSelectionChanged(Table* table);
97 	virtual	void				TableRowInvoked(Table* table, int32 rowIndex);
98 };
99 
100 
101 class Table : public AbstractTable, private TableModelListener {
102 public:
103 								Table(const char* name, uint32 flags,
104 									border_style borderStyle = B_NO_BORDER,
105 									bool showHorizontalScrollbar = true);
106 								Table(TableModel* model, const char* name,
107 									uint32 flags,
108 									border_style borderStyle = B_NO_BORDER,
109 									bool showHorizontalScrollbar = true);
110 	virtual						~Table();
111 
112 			void				SetTableModel(TableModel* model);
113 			TableModel*			GetTableModel() const	{ return fModel; }
114 
115 			void				SetToolTipProvider(
116 									TableToolTipProvider* toolTipProvider);
117 			TableToolTipProvider* ToolTipProvider() const
118 									{ return fToolTipProvider; }
119 
120 			TableSelectionModel* SelectionModel();
121 
122 			void				SelectRow(int32 rowIndex, bool extendSelection);
123 			void				DeselectRow(int32 rowIndex);
124 			void				DeselectAllRows();
125 
126 			bool				AddTableListener(TableListener* listener);
127 			void				RemoveTableListener(TableListener* listener);
128 
129 protected:
130 	virtual bool				GetToolTipAt(BPoint point, BToolTip** _tip);
131 
132 	virtual	void				SelectionChanged();
133 
134 	virtual	AbstractColumn*		CreateColumn(TableColumn* column);
135 
136 private:
137 	virtual	void				TableRowsAdded(TableModel* model,
138 									int32 rowIndex, int32 count);
139 	virtual	void				TableRowsRemoved(TableModel* model,
140 									int32 rowIndex, int32 count);
141 	virtual	void				TableRowsChanged(TableModel* model,
142 									int32 rowIndex, int32 count);
143 
144 private:
145 			class Column;
146 
147 			friend class TableSelectionModel;
148 
149 			typedef BObjectList<TableListener>	ListenerList;
150 			typedef BObjectList<BRow> RowList;
151 
152 private:
153 	virtual	void				ItemInvoked();
154 
155 			void				_UpdateRowIndices(int32 fromIndex);
156 			int32				_ModelIndexOfRow(BRow* row);
157 
158 private:
159 			TableModel*			fModel;
160 			TableToolTipProvider* fToolTipProvider;
161 			RowList				fRows;
162 			TableSelectionModel	fSelectionModel;
163 			ListenerList		fListeners;
164 			int32				fIgnoreSelectionChange;
165 };
166 
167 
168 #endif	// TABLE_H
169