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 virtual void TableCellMouseDown(Table* table, int32 rowIndex, 100 int32 columnIndex, BPoint screenWhere, 101 uint32 buttons); 102 virtual void TableCellMouseUp(Table* table, int32 rowIndex, 103 int32 columnIndex, BPoint screenWhere, 104 uint32 buttons); 105 }; 106 107 108 class Table : public AbstractTable, private TableModelListener { 109 public: 110 Table(const char* name, uint32 flags, 111 border_style borderStyle = B_NO_BORDER, 112 bool showHorizontalScrollbar = true); 113 Table(TableModel* model, const char* name, 114 uint32 flags, 115 border_style borderStyle = B_NO_BORDER, 116 bool showHorizontalScrollbar = true); 117 virtual ~Table(); 118 119 void SetTableModel(TableModel* model); 120 TableModel* GetTableModel() const { return fModel; } 121 122 void SetToolTipProvider( 123 TableToolTipProvider* toolTipProvider); 124 TableToolTipProvider* ToolTipProvider() const 125 { return fToolTipProvider; } 126 127 TableSelectionModel* SelectionModel(); 128 129 void SelectRow(int32 rowIndex, bool extendSelection); 130 void DeselectRow(int32 rowIndex); 131 void DeselectAllRows(); 132 133 bool AddTableListener(TableListener* listener); 134 void RemoveTableListener(TableListener* listener); 135 136 protected: 137 virtual bool GetToolTipAt(BPoint point, BToolTip** _tip); 138 139 virtual void SelectionChanged(); 140 141 virtual AbstractColumn* CreateColumn(TableColumn* column); 142 143 virtual void ColumnMouseDown(AbstractColumn* column, 144 BRow* row, BField* field, 145 BPoint screenWhere, uint32 buttons); 146 virtual void ColumnMouseUp(AbstractColumn* column, 147 BRow* row, BField* field, 148 BPoint screenWhere, uint32 buttons); 149 150 private: 151 virtual void TableRowsAdded(TableModel* model, 152 int32 rowIndex, int32 count); 153 virtual void TableRowsRemoved(TableModel* model, 154 int32 rowIndex, int32 count); 155 virtual void TableRowsChanged(TableModel* model, 156 int32 rowIndex, int32 count); 157 158 private: 159 class Column; 160 161 friend class TableSelectionModel; 162 163 typedef BObjectList<TableListener> ListenerList; 164 typedef BObjectList<BRow> RowList; 165 166 private: 167 virtual void ItemInvoked(); 168 169 void _UpdateRowIndices(int32 fromIndex); 170 int32 _ModelIndexOfRow(BRow* row); 171 172 private: 173 TableModel* fModel; 174 TableToolTipProvider* fToolTipProvider; 175 RowList fRows; 176 TableSelectionModel fSelectionModel; 177 ListenerList fListeners; 178 int32 fIgnoreSelectionChange; 179 }; 180 181 182 #endif // TABLE_H 183