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
18class Table;
19class TableModel;
20
21
22class TableModelListener {
23public:
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	virtual	void				TableModelReset(TableModel* model);
33};
34
35
36class TableModel : public AbstractTableModelBase {
37public:
38	virtual						~TableModel();
39
40	virtual	int32				CountRows() const = 0;
41
42	virtual	bool				GetValueAt(int32 rowIndex, int32 columnIndex,
43									BVariant& value) = 0;
44
45	virtual	bool				AddListener(TableModelListener* listener);
46	virtual	void				RemoveListener(TableModelListener* listener);
47
48protected:
49			typedef BObjectList<TableModelListener> ListenerList;
50
51protected:
52			void				NotifyRowsAdded(int32 rowIndex, int32 count);
53			void				NotifyRowsRemoved(int32 rowIndex, int32 count);
54			void				NotifyRowsChanged(int32 rowIndex, int32 count);
55			void				NotifyTableModelReset();
56
57protected:
58			ListenerList		fListeners;
59};
60
61
62class TableSelectionModel {
63public:
64								TableSelectionModel(Table* table);
65								~TableSelectionModel();
66
67			int32				CountRows();
68			int32				RowAt(int32 index);
69
70private:
71			friend class Table;
72
73private:
74			void				_SelectionChanged();
75			void				_Update();
76
77private:
78			Table*				fTable;
79			int32*				fRows;
80			int32				fRowCount;
81};
82
83
84class TableToolTipProvider {
85public:
86	virtual						~TableToolTipProvider();
87
88	virtual	bool				GetToolTipForTableCell(int32 rowIndex,
89									int32 columnIndex, BToolTip** _tip) = 0;
90									// columnIndex can be -1, if not in a column
91};
92
93
94class TableListener {
95public:
96	virtual						~TableListener();
97
98	virtual	void				TableSelectionChanged(Table* table);
99	virtual	void				TableRowInvoked(Table* table, int32 rowIndex);
100
101	virtual	void				TableCellMouseDown(Table* table, int32 rowIndex,
102									int32 columnIndex, BPoint screenWhere,
103									uint32 buttons);
104	virtual	void				TableCellMouseUp(Table* table, int32 rowIndex,
105									int32 columnIndex, BPoint screenWhere,
106									uint32 buttons);
107};
108
109
110class Table : public AbstractTable, private TableModelListener {
111public:
112								Table(const char* name, uint32 flags,
113									border_style borderStyle = B_NO_BORDER,
114									bool showHorizontalScrollbar = true);
115								Table(TableModel* model, const char* name,
116									uint32 flags,
117									border_style borderStyle = B_NO_BORDER,
118									bool showHorizontalScrollbar = true);
119	virtual						~Table();
120
121			void				SetTableModel(TableModel* model);
122			TableModel*			GetTableModel() const	{ return fModel; }
123
124			void				SetToolTipProvider(
125									TableToolTipProvider* toolTipProvider);
126			TableToolTipProvider* ToolTipProvider() const
127									{ return fToolTipProvider; }
128
129			TableSelectionModel* SelectionModel();
130
131			void				SelectRow(int32 rowIndex, bool extendSelection);
132			void				DeselectRow(int32 rowIndex);
133			void				DeselectAllRows();
134
135			bool				AddTableListener(TableListener* listener);
136			void				RemoveTableListener(TableListener* listener);
137
138	virtual	status_t			GetCellRectAt(int32 rowIndex, int32 colIndex,
139									BRect& _output) const;
140
141protected:
142	virtual bool				GetToolTipAt(BPoint point, BToolTip** _tip);
143
144	virtual	void				SelectionChanged();
145
146	virtual	AbstractColumn*		CreateColumn(TableColumn* column);
147
148	virtual	void				ColumnMouseDown(AbstractColumn* column,
149									BRow* row, BField* field,
150									BPoint screenWhere, uint32 buttons);
151	virtual	void				ColumnMouseUp(AbstractColumn* column,
152									BRow* row, BField* field,
153									BPoint screenWhere, uint32 buttons);
154
155private:
156	virtual	void				TableRowsAdded(TableModel* model,
157									int32 rowIndex, int32 count);
158	virtual	void				TableRowsRemoved(TableModel* model,
159									int32 rowIndex, int32 count);
160	virtual	void				TableRowsChanged(TableModel* model,
161									int32 rowIndex, int32 count);
162	virtual	void				TableModelReset(TableModel* model);
163
164private:
165			class Column;
166
167			friend class TableSelectionModel;
168
169			typedef BObjectList<TableListener>	ListenerList;
170			typedef BObjectList<BRow> RowList;
171
172private:
173	virtual	void				ItemInvoked();
174
175			void				_UpdateRowIndices(int32 fromIndex);
176			int32				_ModelIndexOfRow(BRow* row);
177
178private:
179			TableModel*			fModel;
180			TableToolTipProvider* fToolTipProvider;
181			RowList				fRows;
182			TableSelectionModel	fSelectionModel;
183			ListenerList		fListeners;
184			int32				fIgnoreSelectionChange;
185};
186
187
188#endif	// TABLE_H
189