1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include "table/AbstractTable.h" 7 8 #include <new> 9 10 #include <Looper.h> 11 #include <Message.h> 12 13 #include "table/TableColumn.h" 14 15 16 // #pragma mark - AbstractTableModelBase 17 18 19 AbstractTableModelBase::~AbstractTableModelBase() 20 { 21 } 22 23 24 // #pragma mark - AbstractColumn 25 26 27 AbstractTable::AbstractColumn::AbstractColumn(TableColumn* tableColumn) 28 : 29 BColumn(tableColumn->Width(), tableColumn->MinWidth(), 30 tableColumn->MaxWidth(), tableColumn->Alignment()), 31 fTableColumn(tableColumn), 32 fTable(NULL) 33 { 34 SetWantsEvents(true); 35 } 36 37 38 AbstractTable::AbstractColumn::~AbstractColumn() 39 { 40 delete fTableColumn; 41 } 42 43 44 void 45 AbstractTable::AbstractColumn::SetTable(AbstractTable* table) 46 { 47 fTable = table; 48 } 49 50 51 void 52 AbstractTable::AbstractColumn::MouseDown(BColumnListView* parent, BRow* row, 53 BField* field, BRect fieldRect, BPoint point, uint32 _buttons) 54 { 55 if (fTable == NULL) 56 return; 57 58 if (fTable == NULL) 59 return; 60 BLooper* window = fTable->Looper(); 61 if (window == NULL) 62 return; 63 64 BMessage* message = window->CurrentMessage(); 65 if (message == NULL) 66 return; 67 68 int32 buttons; 69 // Note: The _buttons parameter cannot be trusted. 70 BPoint screenWhere; 71 if (message->FindInt32("buttons", &buttons) != B_OK 72 || message->FindPoint("screen_where", &screenWhere) != B_OK) { 73 return; 74 } 75 76 fTable->ColumnMouseDown(this, row, field, screenWhere, buttons); 77 } 78 79 80 void 81 AbstractTable::AbstractColumn::MouseUp(BColumnListView* parent, BRow* row, 82 BField* field) 83 { 84 if (fTable == NULL) 85 return; 86 BLooper* window = fTable->Looper(); 87 if (window == NULL) 88 return; 89 90 BMessage* message = window->CurrentMessage(); 91 if (message == NULL) 92 return; 93 94 int32 buttons; 95 BPoint screenWhere; 96 if (message->FindInt32("buttons", &buttons) != B_OK 97 || message->FindPoint("screen_where", &screenWhere) != B_OK) { 98 return; 99 } 100 101 fTable->ColumnMouseUp(this, row, field, screenWhere, buttons); 102 } 103 104 105 // #pragma mark - AbstractTable 106 107 108 AbstractTable::AbstractTable(const char* name, uint32 flags, 109 border_style borderStyle, bool showHorizontalScrollbar) 110 : 111 BColumnListView(name, flags, borderStyle, showHorizontalScrollbar), 112 fColumns(20, false) 113 { 114 } 115 116 117 AbstractTable::~AbstractTable() 118 { 119 } 120 121 122 void 123 AbstractTable::AddColumn(TableColumn* column) 124 { 125 if (column == NULL) 126 return; 127 128 AbstractColumn* privateColumn = CreateColumn(column); 129 privateColumn->SetTable(this); 130 131 if (!fColumns.AddItem(privateColumn)) { 132 delete privateColumn; 133 throw std::bad_alloc(); 134 } 135 136 BColumnListView::AddColumn(privateColumn, column->ModelIndex()); 137 138 // TODO: The derived classes need to be notified, so that they can create 139 // fields for the existing rows. 140 } 141 142 143 void 144 AbstractTable::ResizeColumnToPreferred(int32 index) 145 { 146 BColumnListView::ResizeColumnToPreferred(index); 147 } 148 149 150 void 151 AbstractTable::ResizeAllColumnsToPreferred() 152 { 153 BColumnListView::ResizeAllColumnsToPreferred(); 154 } 155 156 157 list_view_type 158 AbstractTable::SelectionMode() const 159 { 160 return BColumnListView::SelectionMode(); 161 } 162 163 164 void 165 AbstractTable::SetSelectionMode(list_view_type type) 166 { 167 BColumnListView::SetSelectionMode(type); 168 } 169 170 171 void 172 AbstractTable::DeselectAll() 173 { 174 BColumnListView::DeselectAll(); 175 } 176 177 178 void 179 AbstractTable::SetSortingEnabled(bool enabled) 180 { 181 BColumnListView::SetSortingEnabled(enabled); 182 } 183 184 185 bool 186 AbstractTable::SortingEnabled() const 187 { 188 return BColumnListView::SortingEnabled(); 189 } 190 191 192 void 193 AbstractTable::SetSortColumn(TableColumn* column, bool add, bool ascending) 194 { 195 if (AbstractColumn* privateColumn = GetColumn(column)) 196 BColumnListView::SetSortColumn(privateColumn, add, ascending); 197 } 198 199 200 void 201 AbstractTable::ClearSortColumns() 202 { 203 BColumnListView::ClearSortColumns(); 204 } 205 206 207 AbstractTable::AbstractColumn* 208 AbstractTable::GetColumn(TableColumn* column) const 209 { 210 for (int32 i = 0; AbstractColumn* privateColumn = fColumns.ItemAt(i); i++) { 211 if (privateColumn->GetTableColumn() == column) 212 return privateColumn; 213 } 214 215 return NULL; 216 } 217