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 TableListener { 83 public: 84 virtual ~TableListener(); 85 86 virtual void TableSelectionChanged(Table* table); 87 virtual void TableRowInvoked(Table* table, int32 rowIndex); 88 }; 89 90 91 class Table : public AbstractTable, private TableModelListener { 92 public: 93 Table(const char* name, uint32 flags, 94 border_style borderStyle = B_NO_BORDER, 95 bool showHorizontalScrollbar = true); 96 Table(TableModel* model, const char* name, 97 uint32 flags, 98 border_style borderStyle = B_NO_BORDER, 99 bool showHorizontalScrollbar = true); 100 virtual ~Table(); 101 102 void SetTableModel(TableModel* model); 103 TableModel* GetTableModel() const { return fModel; } 104 105 TableSelectionModel* SelectionModel(); 106 107 void SelectRow(int32 rowIndex, bool extendSelection); 108 void DeselectRow(int32 rowIndex); 109 void DeselectAllRows(); 110 111 bool AddTableListener(TableListener* listener); 112 void RemoveTableListener(TableListener* listener); 113 114 protected: 115 virtual void SelectionChanged(); 116 117 virtual AbstractColumn* CreateColumn(TableColumn* column); 118 119 private: 120 virtual void TableRowsAdded(TableModel* model, 121 int32 rowIndex, int32 count); 122 virtual void TableRowsRemoved(TableModel* model, 123 int32 rowIndex, int32 count); 124 virtual void TableRowsChanged(TableModel* model, 125 int32 rowIndex, int32 count); 126 127 private: 128 class Column; 129 130 friend class TableSelectionModel; 131 132 typedef BObjectList<TableListener> ListenerList; 133 typedef BObjectList<BRow> RowList; 134 135 private: 136 virtual void ItemInvoked(); 137 138 int32 _ModelIndexOfRow(BRow* row); 139 140 private: 141 TableModel* fModel; 142 RowList fRows; 143 TableSelectionModel fSelectionModel; 144 ListenerList fListeners; 145 int32 fIgnoreSelectionChange; 146 }; 147 148 149 #endif // TABLE_H 150