1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/controls/table/table_view.h"
9 #include "base/auto_reset.h"
10 #include "base/i18n/rtl.h"
11 #include "ui/events/event.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/rect_conversions.h"
15 #include "ui/gfx/skia_util.h"
16 #include "ui/native_theme/native_theme.h"
17 #include "ui/views/controls/scroll_view.h"
18 #include "ui/views/controls/table/table_grouper.h"
19 #include "ui/views/controls/table/table_header.h"
20 #include "ui/views/controls/table/table_utils.h"
21 #include "ui/views/controls/table/table_view_observer.h"
22 #include "ui/views/controls/table/table_view_row_background_painter.h"
24 // Padding around the text (on each side).
25 static const int kTextVerticalPadding = 3;
26 static const int kTextHorizontalPadding = 6;
29 static const int kImageSize = 16;
31 static const int kGroupingIndicatorSize = 6;
37 // Returns result, unless ascending is false in which case -result is returned.
38 int SwapCompareResult(int result, bool ascending) {
39 return ascending ? result : -result;
42 // Populates |model_index_to_range_start| based on the |grouper|.
43 void GetModelIndexToRangeStart(TableGrouper* grouper,
45 std::map<int, int>* model_index_to_range_start) {
46 for (int model_index = 0; model_index < row_count;) {
48 grouper->GetGroupRange(model_index, &range);
49 DCHECK_GT(range.length, 0);
50 for (int range_counter = 0; range_counter < range.length; range_counter++)
51 (*model_index_to_range_start)[range_counter + model_index] = model_index;
52 model_index += range.length;
56 // Returns the color id for the background of selected text. |has_focus|
57 // indicates if the table has focus.
58 ui::NativeTheme::ColorId text_background_color_id(bool has_focus) {
60 ui::NativeTheme::kColorId_TableSelectionBackgroundFocused :
61 ui::NativeTheme::kColorId_TableSelectionBackgroundUnfocused;
64 // Returns the color id for text. |has_focus| indicates if the table has focus.
65 ui::NativeTheme::ColorId selected_text_color_id(bool has_focus) {
66 return has_focus ? ui::NativeTheme::kColorId_TableSelectedText :
67 ui::NativeTheme::kColorId_TableSelectedTextUnfocused;
72 // Used as the comparator to sort the contents of the table.
73 struct TableView::SortHelper {
74 explicit SortHelper(TableView* table) : table(table) {}
76 bool operator()(int model_index1, int model_index2) {
77 return table->CompareRows(model_index1, model_index2) < 0;
83 // Used as the comparator to sort the contents of the table when a TableGrouper
84 // is present. When groups are present we sort the groups based on the first row
85 // in the group and within the groups we keep the same order as the model.
86 struct TableView::GroupSortHelper {
87 explicit GroupSortHelper(TableView* table) : table(table) {}
89 bool operator()(int model_index1, int model_index2) {
90 const int range1 = model_index_to_range_start[model_index1];
91 const int range2 = model_index_to_range_start[model_index2];
92 if (range1 == range2) {
93 // The two rows are in the same group, sort so that items in the same
94 // group always appear in the same order.
95 return model_index1 < model_index2;
97 return table->CompareRows(range1, range2) < 0;
101 std::map<int, int> model_index_to_range_start;
104 TableView::VisibleColumn::VisibleColumn() : x(0), width(0) {}
106 TableView::VisibleColumn::~VisibleColumn() {}
108 TableView::PaintRegion::PaintRegion()
115 TableView::PaintRegion::~PaintRegion() {}
117 TableView::TableView(ui::TableModel* model,
118 const std::vector<ui::TableColumn>& columns,
119 TableTypes table_type,
120 bool single_selection)
124 table_type_(table_type),
125 single_selection_(single_selection),
126 table_view_observer_(NULL),
127 row_height_(font_.GetHeight() + kTextVerticalPadding * 2),
128 last_parent_width_(0),
131 in_set_visible_column_width_(false) {
132 for (size_t i = 0; i < columns.size(); ++i) {
133 VisibleColumn visible_column;
134 visible_column.column = columns[i];
135 visible_columns_.push_back(visible_column);
141 TableView::~TableView() {
143 model_->SetObserver(NULL);
146 // TODO: this doesn't support arbitrarily changing the model, rename this to
147 // ClearModel() or something.
148 void TableView::SetModel(ui::TableModel* model) {
153 model_->SetObserver(NULL);
155 selection_model_.Clear();
157 model_->SetObserver(this);
160 View* TableView::CreateParentIfNecessary() {
161 ScrollView* scroll_view = ScrollView::CreateScrollViewWithBorder();
162 scroll_view->SetContents(this);
163 CreateHeaderIfNecessary();
165 scroll_view->SetHeader(header_);
169 void TableView::SetRowBackgroundPainter(
170 scoped_ptr<TableViewRowBackgroundPainter> painter) {
171 row_background_painter_ = painter.Pass();
174 void TableView::SetGrouper(TableGrouper* grouper) {
176 SortItemsAndUpdateMapping();
179 int TableView::RowCount() const {
180 return model_ ? model_->RowCount() : 0;
183 int TableView::SelectedRowCount() {
184 return static_cast<int>(selection_model_.size());
187 void TableView::Select(int model_row) {
191 SelectByViewIndex(model_row == -1 ? -1 : ModelToView(model_row));
194 int TableView::FirstSelectedRow() {
195 return SelectedRowCount() == 0 ? -1 : selection_model_.selected_indices()[0];
198 void TableView::SetColumnVisibility(int id, bool is_visible) {
199 if (is_visible == IsColumnVisible(id))
203 VisibleColumn visible_column;
204 visible_column.column = FindColumnByID(id);
205 visible_columns_.push_back(visible_column);
207 for (size_t i = 0; i < visible_columns_.size(); ++i) {
208 if (visible_columns_[i].column.id == id) {
209 visible_columns_.erase(visible_columns_.begin() + i);
214 UpdateVisibleColumnSizes();
215 PreferredSizeChanged();
218 header_->SchedulePaint();
221 void TableView::ToggleSortOrder(int visible_column_index) {
222 DCHECK(visible_column_index >= 0 &&
223 visible_column_index < static_cast<int>(visible_columns_.size()));
224 if (!visible_columns_[visible_column_index].column.sortable)
226 const int column_id = visible_columns_[visible_column_index].column.id;
227 SortDescriptors sort(sort_descriptors_);
228 if (!sort.empty() && sort[0].column_id == column_id) {
229 sort[0].ascending = !sort[0].ascending;
231 SortDescriptor descriptor(column_id, true);
232 sort.insert(sort.begin(), descriptor);
233 // Only persist two sort descriptors.
237 SetSortDescriptors(sort);
240 bool TableView::IsColumnVisible(int id) const {
241 for (size_t i = 0; i < visible_columns_.size(); ++i) {
242 if (visible_columns_[i].column.id == id)
248 void TableView::AddColumn(const ui::TableColumn& col) {
249 DCHECK(!HasColumn(col.id));
250 columns_.push_back(col);
253 bool TableView::HasColumn(int id) const {
254 for (size_t i = 0; i < columns_.size(); ++i) {
255 if (columns_[i].id == id)
261 void TableView::SetVisibleColumnWidth(int index, int width) {
262 DCHECK(index >= 0 && index < static_cast<int>(visible_columns_.size()));
263 if (visible_columns_[index].width == width)
265 base::AutoReset<bool> reseter(&in_set_visible_column_width_, true);
266 visible_columns_[index].width = width;
267 for (size_t i = index + 1; i < visible_columns_.size(); ++i) {
268 visible_columns_[i].x =
269 visible_columns_[i - 1].x + visible_columns_[i - 1].width;
271 PreferredSizeChanged();
275 int TableView::ModelToView(int model_index) const {
278 DCHECK_GE(model_index, 0) << " negative model_index " << model_index;
279 DCHECK_LT(model_index, RowCount()) << " out of bounds model_index " <<
281 return model_to_view_[model_index];
284 int TableView::ViewToModel(int view_index) const {
287 DCHECK_GE(view_index, 0) << " negative view_index " << view_index;
288 DCHECK_LT(view_index, RowCount()) << " out of bounds view_index " <<
290 return view_to_model_[view_index];
293 void TableView::Layout() {
294 // parent()->parent() is the scrollview. When its width changes we force
295 // recalculating column sizes.
296 View* scroll_view = parent() ? parent()->parent() : NULL;
298 const int scroll_view_width = scroll_view->GetContentsBounds().width();
299 if (scroll_view_width != last_parent_width_) {
300 last_parent_width_ = scroll_view_width;
301 if (!in_set_visible_column_width_) {
302 // Layout to the parent (the Viewport), which differs from
303 // |scroll_view_width| when scrollbars are present.
304 layout_width_ = parent()->width();
305 UpdateVisibleColumnSizes();
309 // We have to override Layout like this since we're contained in a ScrollView.
310 gfx::Size pref = GetPreferredSize();
311 int width = pref.width();
312 int height = pref.height();
314 width = std::max(parent()->width(), width);
315 height = std::max(parent()->height(), height);
317 SetBounds(x(), y(), width, height);
320 gfx::Size TableView::GetPreferredSize() {
322 if (header_ && !visible_columns_.empty())
323 width = visible_columns_.back().x + visible_columns_.back().width;
324 return gfx::Size(width, RowCount() * row_height_);
327 bool TableView::OnKeyPressed(const ui::KeyEvent& event) {
331 switch (event.key_code()) {
333 // control-a selects all.
334 if (event.IsControlDown() && !single_selection_ && RowCount()) {
335 ui::ListSelectionModel selection_model;
336 selection_model.SetSelectedIndex(selection_model_.active());
337 for (int i = 0; i < RowCount(); ++i)
338 selection_model.AddIndexToSelection(i);
339 SetSelectionModel(selection_model);
346 SelectByViewIndex(0);
351 SelectByViewIndex(RowCount() - 1);
355 AdvanceSelection(ADVANCE_DECREMENT);
359 AdvanceSelection(ADVANCE_INCREMENT);
365 if (table_view_observer_)
366 table_view_observer_->OnKeyDown(event.key_code());
370 bool TableView::OnMousePressed(const ui::MouseEvent& event) {
372 if (!event.IsOnlyLeftMouseButton())
375 const int row = event.y() / row_height_;
376 if (row < 0 || row >= RowCount())
379 if (event.GetClickCount() == 2) {
380 SelectByViewIndex(row);
381 if (table_view_observer_)
382 table_view_observer_->OnDoubleClick();
383 } else if (event.GetClickCount() == 1) {
384 ui::ListSelectionModel new_model;
385 ConfigureSelectionModelForEvent(event, &new_model);
386 SetSelectionModel(new_model);
392 void TableView::OnGestureEvent(ui::GestureEvent* event) {
393 if (event->type() != ui::ET_GESTURE_TAP)
396 const int row = event->y() / row_height_;
397 if (row < 0 || row >= RowCount())
400 event->StopPropagation();
401 ui::ListSelectionModel new_model;
402 ConfigureSelectionModelForEvent(*event, &new_model);
403 SetSelectionModel(new_model);
406 bool TableView::GetTooltipText(const gfx::Point& p,
407 string16* tooltip) const {
408 return GetTooltipImpl(p, tooltip, NULL);
411 bool TableView::GetTooltipTextOrigin(const gfx::Point& p,
412 gfx::Point* loc) const {
413 return GetTooltipImpl(p, NULL, loc);
416 void TableView::OnModelChanged() {
417 selection_model_.Clear();
421 void TableView::OnItemsChanged(int start, int length) {
422 SortItemsAndUpdateMapping();
425 void TableView::OnItemsAdded(int start, int length) {
426 for (int i = 0; i < length; ++i)
427 selection_model_.IncrementFrom(start);
431 void TableView::OnItemsRemoved(int start, int length) {
432 // Determine the currently selected index in terms of the view. We inline the
433 // implementation here since ViewToModel() has DCHECKs that fail since the
434 // model has changed but |model_to_view_| has not been updated yet.
435 const int previously_selected_model_index = FirstSelectedRow();
436 int previously_selected_view_index = previously_selected_model_index;
437 if (previously_selected_model_index != -1 && is_sorted())
438 previously_selected_view_index =
439 model_to_view_[previously_selected_model_index];
440 for (int i = 0; i < length; ++i)
441 selection_model_.DecrementFrom(start);
443 // If the selection was empty and is no longer empty select the same visual
445 if (selection_model_.empty() && previously_selected_view_index != -1 &&
447 selection_model_.SetSelectedIndex(
448 ViewToModel(std::min(RowCount() - 1, previously_selected_view_index)));
450 if (table_view_observer_)
451 table_view_observer_->OnSelectionChanged();
454 gfx::Point TableView::GetKeyboardContextMenuLocation() {
455 int first_selected = FirstSelectedRow();
456 gfx::Rect vis_bounds(GetVisibleBounds());
457 int y = vis_bounds.height() / 2;
458 if (first_selected != -1) {
459 gfx::Rect cell_bounds(GetRowBounds(first_selected));
460 if (cell_bounds.bottom() >= vis_bounds.y() &&
461 cell_bounds.bottom() < vis_bounds.bottom()) {
462 y = cell_bounds.bottom();
465 gfx::Point screen_loc(0, y);
466 if (base::i18n::IsRTL())
467 screen_loc.set_x(width());
468 ConvertPointToScreen(this, &screen_loc);
472 void TableView::OnPaint(gfx::Canvas* canvas) {
473 // Don't invoke View::OnPaint so that we can render our own focus border.
475 canvas->DrawColor(GetNativeTheme()->GetSystemColor(
476 ui::NativeTheme::kColorId_TableBackground));
478 if (!RowCount() || visible_columns_.empty())
481 const PaintRegion region(GetPaintRegion(GetPaintBounds(canvas)));
482 if (region.min_column == -1)
483 return; // No need to paint anything.
485 const SkColor selected_bg_color = GetNativeTheme()->GetSystemColor(
486 text_background_color_id(HasFocus()));
487 const SkColor fg_color = GetNativeTheme()->GetSystemColor(
488 ui::NativeTheme::kColorId_TableText);
489 const SkColor selected_fg_color = GetNativeTheme()->GetSystemColor(
490 selected_text_color_id(HasFocus()));
491 for (int i = region.min_row; i < region.max_row; ++i) {
492 const int model_index = ViewToModel(i);
493 const bool is_selected = selection_model_.IsSelected(model_index);
495 canvas->FillRect(GetRowBounds(i), selected_bg_color);
496 } else if (row_background_painter_) {
497 row_background_painter_->PaintRowBackground(model_index,
501 if (selection_model_.active() == i && HasFocus())
502 canvas->DrawFocusRect(GetRowBounds(i));
503 for (int j = region.min_column; j < region.max_column; ++j) {
504 const gfx::Rect cell_bounds(GetCellBounds(i, j));
505 int text_x = kTextHorizontalPadding + cell_bounds.x();
507 // Provide space for the grouping indicator, but draw it separately.
508 if (j == 0 && grouper_)
509 text_x += kGroupingIndicatorSize + kTextHorizontalPadding;
511 // Always paint the icon in the first visible column.
512 if (j == 0 && table_type_ == ICON_AND_TEXT) {
513 gfx::ImageSkia image = model_->GetIcon(model_index);
514 if (!image.isNull()) {
515 int image_x = GetMirroredXWithWidthInView(text_x, kImageSize);
516 canvas->DrawImageInt(
517 image, 0, 0, image.width(), image.height(),
519 cell_bounds.y() + (cell_bounds.height() - kImageSize) / 2,
520 kImageSize, kImageSize, true);
522 text_x += kImageSize + kTextHorizontalPadding;
524 if (text_x < cell_bounds.right() - kTextHorizontalPadding) {
525 canvas->DrawStringInt(
526 model_->GetText(model_index, visible_columns_[j].column.id), font_,
527 is_selected ? selected_fg_color : fg_color,
528 GetMirroredXWithWidthInView(text_x, cell_bounds.right() - text_x -
529 kTextHorizontalPadding),
530 cell_bounds.y() + kTextVerticalPadding,
531 cell_bounds.right() - text_x,
532 cell_bounds.height() - kTextVerticalPadding * 2,
533 TableColumnAlignmentToCanvasAlignment(
534 visible_columns_[j].column.alignment));
539 if (!grouper_ || region.min_column > 0)
542 const SkColor grouping_color = GetNativeTheme()->GetSystemColor(
543 ui::NativeTheme::kColorId_TableGroupingIndicatorColor);
544 SkPaint grouping_paint;
545 grouping_paint.setColor(grouping_color);
546 grouping_paint.setStyle(SkPaint::kFill_Style);
547 grouping_paint.setAntiAlias(true);
548 const int group_indicator_x = GetMirroredXInView(GetCellBounds(0, 0).x() +
549 kTextHorizontalPadding + kGroupingIndicatorSize / 2);
550 for (int i = region.min_row; i < region.max_row; ) {
551 const int model_index = ViewToModel(i);
553 grouper_->GetGroupRange(model_index, &range);
554 DCHECK_GT(range.length, 0);
555 // The order of rows in a group is consistent regardless of sort, so it's ok
556 // to do this calculation.
557 const int start = i - (model_index - range.start);
558 const int last = start + range.length - 1;
559 const gfx::Rect start_cell_bounds(GetCellBounds(start, 0));
561 const gfx::Rect last_cell_bounds(GetCellBounds(last, 0));
562 canvas->FillRect(gfx::Rect(
563 group_indicator_x - kGroupingIndicatorSize / 2,
564 start_cell_bounds.CenterPoint().y(),
565 kGroupingIndicatorSize,
566 last_cell_bounds.y() - start_cell_bounds.y()),
568 canvas->DrawCircle(gfx::Point(group_indicator_x,
569 last_cell_bounds.CenterPoint().y()),
570 kGroupingIndicatorSize / 2, grouping_paint);
572 canvas->DrawCircle(gfx::Point(group_indicator_x,
573 start_cell_bounds.CenterPoint().y()),
574 kGroupingIndicatorSize / 2, grouping_paint);
579 void TableView::OnFocus() {
580 SchedulePaintForSelection();
583 void TableView::OnBlur() {
584 SchedulePaintForSelection();
587 void TableView::NumRowsChanged() {
588 SortItemsAndUpdateMapping();
589 PreferredSizeChanged();
593 void TableView::SetSortDescriptors(const SortDescriptors& sort_descriptors) {
594 sort_descriptors_ = sort_descriptors;
595 SortItemsAndUpdateMapping();
597 header_->SchedulePaint();
600 void TableView::SortItemsAndUpdateMapping() {
602 view_to_model_.clear();
603 model_to_view_.clear();
605 const int row_count = RowCount();
606 view_to_model_.resize(row_count);
607 model_to_view_.resize(row_count);
608 for (int i = 0; i < row_count; ++i)
609 view_to_model_[i] = i;
611 GroupSortHelper sort_helper(this);
612 GetModelIndexToRangeStart(grouper_, RowCount(),
613 &sort_helper.model_index_to_range_start);
614 std::sort(view_to_model_.begin(), view_to_model_.end(), sort_helper);
616 std::sort(view_to_model_.begin(), view_to_model_.end(), SortHelper(this));
618 for (int i = 0; i < row_count; ++i)
619 model_to_view_[view_to_model_[i]] = i;
620 model_->ClearCollator();
625 int TableView::CompareRows(int model_row1, int model_row2) {
626 const int sort_result = model_->CompareValues(
627 model_row1, model_row2, sort_descriptors_[0].column_id);
628 if (sort_result == 0 && sort_descriptors_.size() > 1) {
629 // Try the secondary sort.
630 return SwapCompareResult(
631 model_->CompareValues(model_row1, model_row2,
632 sort_descriptors_[1].column_id),
633 sort_descriptors_[1].ascending);
635 return SwapCompareResult(sort_result, sort_descriptors_[0].ascending);
638 gfx::Rect TableView::GetRowBounds(int row) const {
639 return gfx::Rect(0, row * row_height_, width(), row_height_);
642 gfx::Rect TableView::GetCellBounds(int row, int visible_column_index) const {
644 return GetRowBounds(row);
645 const VisibleColumn& vis_col(visible_columns_[visible_column_index]);
646 return gfx::Rect(vis_col.x, row * row_height_, vis_col.width, row_height_);
649 void TableView::AdjustCellBoundsForText(int visible_column_index,
650 gfx::Rect* bounds) const {
651 int text_x = kTextHorizontalPadding + bounds->x();
652 if (visible_column_index == 0) {
654 text_x += kGroupingIndicatorSize + kTextHorizontalPadding;
655 if (table_type_ == ICON_AND_TEXT)
656 text_x += kImageSize + kTextHorizontalPadding;
658 bounds->set_x(text_x);
660 std::max(0, bounds->right() - kTextHorizontalPadding - text_x));
663 void TableView::CreateHeaderIfNecessary() {
664 // Only create a header if there is more than one column or the title of the
665 // only column is not empty.
666 if (header_ || (columns_.size() == 1 && columns_[0].title.empty()))
669 header_ = new TableHeader(this);
672 void TableView::UpdateVisibleColumnSizes() {
676 std::vector<ui::TableColumn> columns;
677 for (size_t i = 0; i < visible_columns_.size(); ++i)
678 columns.push_back(visible_columns_[i].column);
680 int first_column_padding = 0;
681 if (table_type_ == ICON_AND_TEXT && header_)
682 first_column_padding += kImageSize + kTextHorizontalPadding;
684 first_column_padding += kGroupingIndicatorSize + kTextHorizontalPadding;
686 std::vector<int> sizes = views::CalculateTableColumnSizes(
687 layout_width_, first_column_padding, header_->font(), font_,
688 std::max(kTextHorizontalPadding, TableHeader::kHorizontalPadding) * 2,
689 TableHeader::kSortIndicatorWidth, columns, model_);
690 DCHECK_EQ(visible_columns_.size(), sizes.size());
692 for (size_t i = 0; i < visible_columns_.size(); ++i) {
693 visible_columns_[i].x = x;
694 visible_columns_[i].width = sizes[i];
699 TableView::PaintRegion TableView::GetPaintRegion(
700 const gfx::Rect& bounds) const {
701 DCHECK(!visible_columns_.empty());
705 region.min_row = std::min(RowCount() - 1,
706 std::max(0, bounds.y() / row_height_));
707 region.max_row = bounds.bottom() / row_height_;
708 if (bounds.bottom() % row_height_ != 0)
710 region.max_row = std::min(region.max_row, RowCount());
713 region.max_column = 1;
717 const int paint_x = GetMirroredXForRect(bounds);
718 const int paint_max_x = paint_x + bounds.width();
719 region.min_column = -1;
720 region.max_column = visible_columns_.size();
721 for (size_t i = 0; i < visible_columns_.size(); ++i) {
722 int max_x = visible_columns_[i].x + visible_columns_[i].width;
723 if (region.min_column == -1 && max_x >= paint_x)
724 region.min_column = static_cast<int>(i);
725 if (region.min_column != -1 && visible_columns_[i].x >= paint_max_x) {
726 region.max_column = i;
733 gfx::Rect TableView::GetPaintBounds(gfx::Canvas* canvas) const {
735 if (canvas->sk_canvas()->getClipBounds(&sk_clip_rect))
736 return gfx::ToEnclosingRect(gfx::SkRectToRectF(sk_clip_rect));
737 return GetVisibleBounds();
740 void TableView::SchedulePaintForSelection() {
741 if (selection_model_.size() == 1) {
742 const int first_model_row = FirstSelectedRow();
743 SchedulePaintInRect(GetRowBounds(ModelToView(first_model_row)));
744 if (first_model_row != selection_model_.active())
745 SchedulePaintInRect(GetRowBounds(ModelToView(selection_model_.active())));
746 } else if (selection_model_.size() > 1) {
751 ui::TableColumn TableView::FindColumnByID(int id) const {
752 for (size_t i = 0; i < columns_.size(); ++i) {
753 if (columns_[i].id == id)
757 return ui::TableColumn();
760 void TableView::SelectByViewIndex(int view_index) {
761 ui::ListSelectionModel new_selection;
762 if (view_index != -1) {
763 SelectRowsInRangeFrom(view_index, true, &new_selection);
764 new_selection.set_anchor(ViewToModel(view_index));
765 new_selection.set_active(ViewToModel(view_index));
768 SetSelectionModel(new_selection);
771 void TableView::SetSelectionModel(const ui::ListSelectionModel& new_selection) {
772 if (new_selection.Equals(selection_model_))
775 SchedulePaintForSelection();
776 selection_model_.Copy(new_selection);
777 SchedulePaintForSelection();
779 // Scroll the group for the active item to visible.
780 if (selection_model_.active() != -1) {
781 gfx::Rect vis_rect(GetVisibleBounds());
782 const GroupRange range(GetGroupRange(selection_model_.active()));
783 const int start_y = GetRowBounds(ModelToView(range.start)).y();
785 GetRowBounds(ModelToView(range.start + range.length - 1)).bottom();
786 vis_rect.set_y(start_y);
787 vis_rect.set_height(end_y - start_y);
788 ScrollRectToVisible(vis_rect);
791 if (table_view_observer_)
792 table_view_observer_->OnSelectionChanged();
795 void TableView::AdvanceSelection(AdvanceDirection direction) {
796 if (selection_model_.active() == -1) {
797 SelectByViewIndex(0);
800 int view_index = ModelToView(selection_model_.active());
801 if (direction == ADVANCE_DECREMENT)
802 view_index = std::max(0, view_index - 1);
804 view_index = std::min(RowCount() - 1, view_index + 1);
805 SelectByViewIndex(view_index);
808 void TableView::ConfigureSelectionModelForEvent(
809 const ui::LocatedEvent& event,
810 ui::ListSelectionModel* model) const {
811 const int view_index = event.y() / row_height_;
812 DCHECK(view_index >= 0 && view_index < RowCount());
814 if (selection_model_.anchor() == -1 ||
816 (!event.IsControlDown() && !event.IsShiftDown())) {
817 SelectRowsInRangeFrom(view_index, true, model);
818 model->set_anchor(ViewToModel(view_index));
819 model->set_active(ViewToModel(view_index));
822 if ((event.IsControlDown() && event.IsShiftDown()) || event.IsShiftDown()) {
823 // control-shift: copy existing model and make sure rows between anchor and
824 // |view_index| are selected.
825 // shift: reset selection so that only rows between anchor and |view_index|
827 if (event.IsControlDown() && event.IsShiftDown())
828 model->Copy(selection_model_);
830 model->set_anchor(selection_model_.anchor());
831 for (int i = std::min(view_index, ModelToView(model->anchor())),
832 end = std::max(view_index, ModelToView(model->anchor()));
834 SelectRowsInRangeFrom(i, true, model);
836 model->set_active(ViewToModel(view_index));
838 DCHECK(event.IsControlDown());
839 // Toggle the selection state of |view_index| and set the anchor/active to
840 // it and don't change the state of any other rows.
841 model->Copy(selection_model_);
842 model->set_anchor(ViewToModel(view_index));
843 model->set_active(ViewToModel(view_index));
844 SelectRowsInRangeFrom(view_index,
845 !model->IsSelected(ViewToModel(view_index)),
850 void TableView::SelectRowsInRangeFrom(int view_index,
852 ui::ListSelectionModel* model) const {
853 const GroupRange range(GetGroupRange(ViewToModel(view_index)));
854 for (int i = 0; i < range.length; ++i) {
856 model->AddIndexToSelection(range.start + i);
858 model->RemoveIndexFromSelection(range.start + i);
862 GroupRange TableView::GetGroupRange(int model_index) const {
865 grouper_->GetGroupRange(model_index, &range);
867 range.start = model_index;
873 bool TableView::GetTooltipImpl(const gfx::Point& location,
875 gfx::Point* tooltip_origin) const {
876 const int row = location.y() / row_height_;
877 if (row < 0 || row >= RowCount() || visible_columns_.empty())
880 const int x = GetMirroredXInView(location.x());
881 const int column = GetClosestVisibleColumnIndex(this, x);
882 if (x < visible_columns_[column].x ||
883 x > (visible_columns_[column].x + visible_columns_[column].width))
886 const string16 text(model_->GetText(ViewToModel(row),
887 visible_columns_[column].column.id));
891 gfx::Rect cell_bounds(GetCellBounds(row, column));
892 AdjustCellBoundsForText(column, &cell_bounds);
893 const int right = std::min(GetVisibleBounds().right(), cell_bounds.right());
894 if (right > cell_bounds.x() &&
895 font_.GetStringWidth(text) <= (right - cell_bounds.x()))
900 if (tooltip_origin) {
901 tooltip_origin->SetPoint(cell_bounds.x(),
902 cell_bounds.y() + kTextVerticalPadding);