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 virtual void TableModelReset(TableModel* model); 33 }; 34 35 36 class TableModel : public AbstractTableModelBase { 37 public: 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 48 protected: 49 typedef BObjectList<TableModelListener> ListenerList; 50 51 protected: 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 57 protected: 58 ListenerList fListeners; 59 }; 60 61 62 class TableSelectionModel { 63 public: 64 TableSelectionModel(Table* table); 65 ~TableSelectionModel(); 66 67 int32 CountRows(); 68 int32 RowAt(int32 index); 69 70 private: 71 friend class Table; 72 73 private: 74 void _SelectionChanged(); 75 void _Update(); 76 77 private: 78 Table* fTable; 79 int32* fRows; 80 int32 fRowCount; 81 }; 82 83 84 class TableToolTipProvider { 85 public: 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 94 class TableListener { 95 public: 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 110 class Table : public AbstractTable, private TableModelListener { 111 public: 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 protected: 139 virtual bool GetToolTipAt(BPoint point, BToolTip** _tip); 140 141 virtual void SelectionChanged(); 142 143 virtual AbstractColumn* CreateColumn(TableColumn* column); 144 145 virtual void ColumnMouseDown(AbstractColumn* column, 146 BRow* row, BField* field, 147 BPoint screenWhere, uint32 buttons); 148 virtual void ColumnMouseUp(AbstractColumn* column, 149 BRow* row, BField* field, 150 BPoint screenWhere, uint32 buttons); 151 152 private: 153 virtual void TableRowsAdded(TableModel* model, 154 int32 rowIndex, int32 count); 155 virtual void TableRowsRemoved(TableModel* model, 156 int32 rowIndex, int32 count); 157 virtual void TableRowsChanged(TableModel* model, 158 int32 rowIndex, int32 count); 159 virtual void TableModelReset(TableModel* model); 160 161 private: 162 class Column; 163 164 friend class TableSelectionModel; 165 166 typedef BObjectList<TableListener> ListenerList; 167 typedef BObjectList<BRow> RowList; 168 169 private: 170 virtual void ItemInvoked(); 171 172 void _UpdateRowIndices(int32 fromIndex); 173 int32 _ModelIndexOfRow(BRow* row); 174 175 private: 176 TableModel* fModel; 177 TableToolTipProvider* fToolTipProvider; 178 RowList fRows; 179 TableSelectionModel fSelectionModel; 180 ListenerList fListeners; 181 int32 fIgnoreSelectionChange; 182 }; 183 184 185 #endif // TABLE_H 186