| OLD | NEW |
| 1 | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 3 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 5 | 4 |
| 6 #include "views/controls/table/table_view2.h" | 5 #include "views/controls/table/table_view2.h" |
| 7 | 6 |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "ui/base/models/table_model.h" | 8 #include "ui/base/models/table_model.h" |
| 10 #include "views/controls/native/native_view_host.h" | 9 #include "views/controls/native/native_view_host.h" |
| 11 #include "views/controls/table/table_view_observer.h" | 10 #include "views/controls/table/table_view_observer.h" |
| 12 | 11 |
| 13 using ui::TableColumn; | |
| 14 | |
| 15 namespace views { | 12 namespace views { |
| 16 | 13 |
| 17 // TableView2 ------------------------------------------------------------------ | 14 // TableView2 ------------------------------------------------------------------ |
| 18 | 15 |
| 19 TableView2::TableView2(TableModel* model, | 16 TableView2::TableView2(ui::TableModel* model, |
| 20 const std::vector<TableColumn>& columns, | 17 const std::vector<ui::TableColumn>& columns, |
| 21 TableTypes table_type, | 18 TableTypes table_type, |
| 22 int options) | 19 int options) |
| 23 : model_(model), | 20 : model_(model), |
| 24 table_type_(table_type), | 21 table_type_(table_type), |
| 25 table_view_observer_(NULL), | 22 table_view_observer_(NULL), |
| 26 visible_columns_(), | 23 visible_columns_(), |
| 27 all_columns_(), | 24 all_columns_(), |
| 28 column_count_(static_cast<int>(columns.size())), | 25 column_count_(static_cast<int>(columns.size())), |
| 29 single_selection_((options & SINGLE_SELECTION) != 0), | 26 single_selection_((options & SINGLE_SELECTION) != 0), |
| 30 resizable_columns_((options & RESIZABLE_COLUMNS) != 0), | 27 resizable_columns_((options & RESIZABLE_COLUMNS) != 0), |
| 31 autosize_columns_((options & AUTOSIZE_COLUMNS) != 0), | 28 autosize_columns_((options & AUTOSIZE_COLUMNS) != 0), |
| 32 horizontal_lines_((options & HORIZONTAL_LINES) != 0), | 29 horizontal_lines_((options & HORIZONTAL_LINES) != 0), |
| 33 vertical_lines_((options & VERTICAL_LINES) != 0), | 30 vertical_lines_((options & VERTICAL_LINES) != 0), |
| 34 native_wrapper_(NULL) { | 31 native_wrapper_(NULL) { |
| 35 Init(columns); | 32 Init(columns); |
| 36 } | 33 } |
| 37 | 34 |
| 38 TableView2::~TableView2() { | 35 TableView2::~TableView2() { |
| 39 if (model_) | 36 if (model_) |
| 40 model_->SetObserver(NULL); | 37 model_->SetObserver(NULL); |
| 41 } | 38 } |
| 42 | 39 |
| 43 void TableView2::SetModel(TableModel* model) { | 40 void TableView2::SetModel(ui::TableModel* model) { |
| 44 if (model == model_) | 41 if (model == model_) |
| 45 return; | 42 return; |
| 46 | 43 |
| 47 if (model_) | 44 if (model_) |
| 48 model_->SetObserver(NULL); | 45 model_->SetObserver(NULL); |
| 49 model_ = model; | 46 model_ = model; |
| 50 if (model_) | 47 if (model_) |
| 51 model_->SetObserver(this); | 48 model_->SetObserver(this); |
| 52 if (native_wrapper_) | 49 if (native_wrapper_) |
| 53 OnModelChanged(); | 50 OnModelChanged(); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 void TableView2::OnItemsRemoved(int start, int length) { | 149 void TableView2::OnItemsRemoved(int start, int length) { |
| 153 if (!native_wrapper_) | 150 if (!native_wrapper_) |
| 154 return; | 151 return; |
| 155 | 152 |
| 156 DCHECK(start >= 0 && length >= 0 && | 153 DCHECK(start >= 0 && length >= 0 && |
| 157 start + length <= native_wrapper_->GetRowCount()); | 154 start + length <= native_wrapper_->GetRowCount()); |
| 158 | 155 |
| 159 native_wrapper_->OnRowsRemoved(start, length); | 156 native_wrapper_->OnRowsRemoved(start, length); |
| 160 } | 157 } |
| 161 | 158 |
| 162 void TableView2::AddColumn(const TableColumn& col) { | 159 void TableView2::AddColumn(const ui::TableColumn& col) { |
| 163 DCHECK_EQ(0U, all_columns_.count(col.id)); | 160 DCHECK_EQ(0U, all_columns_.count(col.id)); |
| 164 all_columns_[col.id] = col; | 161 all_columns_[col.id] = col; |
| 165 } | 162 } |
| 166 | 163 |
| 167 void TableView2::SetColumns(const std::vector<TableColumn>& columns) { | 164 void TableView2::SetColumns(const std::vector<ui::TableColumn>& columns) { |
| 168 // Remove the currently visible columns. | 165 // Remove the currently visible columns. |
| 169 while (!visible_columns_.empty()) | 166 while (!visible_columns_.empty()) |
| 170 SetColumnVisibility(visible_columns_.front(), false); | 167 SetColumnVisibility(visible_columns_.front(), false); |
| 171 | 168 |
| 172 all_columns_.clear(); | 169 all_columns_.clear(); |
| 173 for (std::vector<TableColumn>::const_iterator i = columns.begin(); | 170 for (std::vector<ui::TableColumn>::const_iterator i = columns.begin(); |
| 174 i != columns.end(); ++i) { | 171 i != columns.end(); ++i) { |
| 175 AddColumn(*i); | 172 AddColumn(*i); |
| 176 } | 173 } |
| 177 } | 174 } |
| 178 | 175 |
| 179 void TableView2::OnColumnsChanged() { | 176 void TableView2::OnColumnsChanged() { |
| 180 column_count_ = static_cast<int>(visible_columns_.size()); | 177 column_count_ = static_cast<int>(visible_columns_.size()); |
| 181 ResetColumnSizes(); | 178 ResetColumnSizes(); |
| 182 } | 179 } |
| 183 | 180 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 203 native_wrapper_->RemoveColumn(index); | 200 native_wrapper_->RemoveColumn(index); |
| 204 visible_columns_.erase(i); | 201 visible_columns_.erase(i); |
| 205 changed = true; | 202 changed = true; |
| 206 break; | 203 break; |
| 207 } | 204 } |
| 208 } | 205 } |
| 209 } | 206 } |
| 210 if (is_visible) { | 207 if (is_visible) { |
| 211 DCHECK(native_wrapper_); | 208 DCHECK(native_wrapper_); |
| 212 visible_columns_.push_back(id); | 209 visible_columns_.push_back(id); |
| 213 TableColumn& column = all_columns_[id]; | 210 ui::TableColumn& column = all_columns_[id]; |
| 214 native_wrapper_->InsertColumn(column, column_count_); | 211 native_wrapper_->InsertColumn(column, column_count_); |
| 215 changed = true; | 212 changed = true; |
| 216 } | 213 } |
| 217 if (changed) | 214 if (changed) |
| 218 OnColumnsChanged(); | 215 OnColumnsChanged(); |
| 219 | 216 |
| 220 } | 217 } |
| 221 | 218 |
| 222 bool TableView2::IsColumnVisible(int id) const { | 219 bool TableView2::IsColumnVisible(int id) const { |
| 223 for (std::vector<int>::const_iterator i = visible_columns_.begin(); | 220 for (std::vector<int>::const_iterator i = visible_columns_.begin(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 242 width = native_bounds.width(); | 239 width = native_bounds.width(); |
| 243 } | 240 } |
| 244 } | 241 } |
| 245 | 242 |
| 246 float percent = 0; | 243 float percent = 0; |
| 247 int fixed_width = 0; | 244 int fixed_width = 0; |
| 248 int autosize_width = 0; | 245 int autosize_width = 0; |
| 249 | 246 |
| 250 for (std::vector<int>::const_iterator i = visible_columns_.begin(); | 247 for (std::vector<int>::const_iterator i = visible_columns_.begin(); |
| 251 i != visible_columns_.end(); ++i) { | 248 i != visible_columns_.end(); ++i) { |
| 252 TableColumn& col = all_columns_[*i]; | 249 ui::TableColumn& col = all_columns_[*i]; |
| 253 int col_index = static_cast<int>(i - visible_columns_.begin()); | 250 int col_index = static_cast<int>(i - visible_columns_.begin()); |
| 254 if (col.width == -1) { | 251 if (col.width == -1) { |
| 255 if (col.percent > 0) { | 252 if (col.percent > 0) { |
| 256 percent += col.percent; | 253 percent += col.percent; |
| 257 } else { | 254 } else { |
| 258 autosize_width += col.min_visible_width; | 255 autosize_width += col.min_visible_width; |
| 259 } | 256 } |
| 260 } else { | 257 } else { |
| 261 fixed_width += native_wrapper_->GetColumnWidth(col_index); | 258 fixed_width += native_wrapper_->GetColumnWidth(col_index); |
| 262 } | 259 } |
| 263 } | 260 } |
| 264 | 261 |
| 265 // Now do a pass to set the actual sizes of auto-sized and | 262 // Now do a pass to set the actual sizes of auto-sized and |
| 266 // percent-sized columns. | 263 // percent-sized columns. |
| 267 int available_width = width - fixed_width - autosize_width; | 264 int available_width = width - fixed_width - autosize_width; |
| 268 for (std::vector<int>::const_iterator i = visible_columns_.begin(); | 265 for (std::vector<int>::const_iterator i = visible_columns_.begin(); |
| 269 i != visible_columns_.end(); ++i) { | 266 i != visible_columns_.end(); ++i) { |
| 270 TableColumn& col = all_columns_[*i]; | 267 ui::TableColumn& col = all_columns_[*i]; |
| 271 if (col.width == -1) { | 268 if (col.width == -1) { |
| 272 int col_index = static_cast<int>(i - visible_columns_.begin()); | 269 int col_index = static_cast<int>(i - visible_columns_.begin()); |
| 273 if (col.percent > 0) { | 270 if (col.percent > 0) { |
| 274 if (available_width > 0) { | 271 if (available_width > 0) { |
| 275 int col_width = | 272 int col_width = |
| 276 static_cast<int>(available_width * (col.percent / percent)); | 273 static_cast<int>(available_width * (col.percent / percent)); |
| 277 available_width -= col_width; | 274 available_width -= col_width; |
| 278 percent -= col.percent; | 275 percent -= col.percent; |
| 279 native_wrapper_->SetColumnWidth(col_index, col_width); | 276 native_wrapper_->SetColumnWidth(col_index, col_width); |
| 280 } | 277 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 309 | 306 |
| 310 void TableView2::ViewHierarchyChanged(bool is_add, View* parent, View* child) { | 307 void TableView2::ViewHierarchyChanged(bool is_add, View* parent, View* child) { |
| 311 if (is_add && !native_wrapper_ && GetWidget()) { | 308 if (is_add && !native_wrapper_ && GetWidget()) { |
| 312 // The native wrapper's lifetime will be managed by the view hierarchy after | 309 // The native wrapper's lifetime will be managed by the view hierarchy after |
| 313 // we call AddChildView. | 310 // we call AddChildView. |
| 314 native_wrapper_ = CreateWrapper(); | 311 native_wrapper_ = CreateWrapper(); |
| 315 AddChildView(native_wrapper_->GetView()); | 312 AddChildView(native_wrapper_->GetView()); |
| 316 } | 313 } |
| 317 } | 314 } |
| 318 | 315 |
| 319 void TableView2::Init(const std::vector<TableColumn>& columns) { | 316 void TableView2::Init(const std::vector<ui::TableColumn>& columns) { |
| 320 for (std::vector<TableColumn>::const_iterator i = columns.begin(); | 317 for (std::vector<ui::TableColumn>::const_iterator i = columns.begin(); |
| 321 i != columns.end(); ++i) { | 318 i != columns.end(); ++i) { |
| 322 AddColumn(*i); | 319 AddColumn(*i); |
| 323 visible_columns_.push_back(i->id); | 320 visible_columns_.push_back(i->id); |
| 324 } | 321 } |
| 325 } | 322 } |
| 326 | 323 |
| 327 gfx::NativeView TableView2::GetTestingHandle() { | 324 gfx::NativeView TableView2::GetTestingHandle() { |
| 328 return native_wrapper_->GetTestingHandle(); | 325 return native_wrapper_->GetTestingHandle(); |
| 329 } | 326 } |
| 330 | 327 |
| 331 TableColumn TableView2::GetVisibleColumnAt(int index) { | 328 ui::TableColumn TableView2::GetVisibleColumnAt(int index) { |
| 332 DCHECK(index < static_cast<int>(visible_columns_.size())); | 329 DCHECK(index < static_cast<int>(visible_columns_.size())); |
| 333 std::map<int, TableColumn>::iterator iter = | 330 std::map<int, ui::TableColumn>::iterator iter = |
| 334 all_columns_.find(index); | 331 all_columns_.find(index); |
| 335 DCHECK(iter != all_columns_.end()); | 332 DCHECK(iter != all_columns_.end()); |
| 336 return iter->second; | 333 return iter->second; |
| 337 } | 334 } |
| 338 | 335 |
| 339 //////////////////////////////////////////////////////////////////////////////// | 336 //////////////////////////////////////////////////////////////////////////////// |
| 340 // NativeTable2, protected: | 337 // NativeTable2, protected: |
| 341 | 338 |
| 342 NativeTableWrapper* TableView2::CreateWrapper() { | 339 NativeTableWrapper* TableView2::CreateWrapper() { |
| 343 return NativeTableWrapper::CreateNativeWrapper(this); | 340 return NativeTableWrapper::CreateNativeWrapper(this); |
| 344 } | 341 } |
| 345 | 342 |
| 346 } // namespace views | 343 } // namespace views |
| OLD | NEW |