| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/table/table_view.h" | 5 #include "ui/views/controls/table/table_view.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 ui::NativeTheme::kColorId_TableSelectionBackgroundFocused : | 68 ui::NativeTheme::kColorId_TableSelectionBackgroundFocused : |
| 69 ui::NativeTheme::kColorId_TableSelectionBackgroundUnfocused; | 69 ui::NativeTheme::kColorId_TableSelectionBackgroundUnfocused; |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Returns the color id for text. |has_focus| indicates if the table has focus. | 72 // Returns the color id for text. |has_focus| indicates if the table has focus. |
| 73 ui::NativeTheme::ColorId selected_text_color_id(bool has_focus) { | 73 ui::NativeTheme::ColorId selected_text_color_id(bool has_focus) { |
| 74 return has_focus ? ui::NativeTheme::kColorId_TableSelectedText : | 74 return has_focus ? ui::NativeTheme::kColorId_TableSelectedText : |
| 75 ui::NativeTheme::kColorId_TableSelectedTextUnfocused; | 75 ui::NativeTheme::kColorId_TableSelectedTextUnfocused; |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Whether the platform "command" key is down. |
| 79 bool IsCmdOrCtrl(const ui::Event& event) { |
| 80 #if defined(OS_MACOSX) |
| 81 return event.IsCommandDown(); |
| 82 #else |
| 83 return event.IsControlDown(); |
| 84 #endif |
| 85 } |
| 86 |
| 78 } // namespace | 87 } // namespace |
| 79 | 88 |
| 80 // Used as the comparator to sort the contents of the table. | 89 // Used as the comparator to sort the contents of the table. |
| 81 struct TableView::SortHelper { | 90 struct TableView::SortHelper { |
| 82 explicit SortHelper(TableView* table) : table(table) {} | 91 explicit SortHelper(TableView* table) : table(table) {} |
| 83 | 92 |
| 84 bool operator()(int model_index1, int model_index2) { | 93 bool operator()(int model_index1, int model_index2) { |
| 85 return table->CompareRows(model_index1, model_index2) < 0; | 94 return table->CompareRows(model_index1, model_index2) < 0; |
| 86 } | 95 } |
| 87 | 96 |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 return gfx::Size(width, RowCount() * row_height_); | 352 return gfx::Size(width, RowCount() * row_height_); |
| 344 } | 353 } |
| 345 | 354 |
| 346 bool TableView::OnKeyPressed(const ui::KeyEvent& event) { | 355 bool TableView::OnKeyPressed(const ui::KeyEvent& event) { |
| 347 if (!HasFocus()) | 356 if (!HasFocus()) |
| 348 return false; | 357 return false; |
| 349 | 358 |
| 350 switch (event.key_code()) { | 359 switch (event.key_code()) { |
| 351 case ui::VKEY_A: | 360 case ui::VKEY_A: |
| 352 // control-a selects all. | 361 // control-a selects all. |
| 353 if (event.IsControlDown() && !single_selection_ && RowCount()) { | 362 if (IsCmdOrCtrl(event) && !single_selection_ && RowCount()) { |
| 354 ui::ListSelectionModel selection_model; | 363 ui::ListSelectionModel selection_model; |
| 355 selection_model.SetSelectedIndex(selection_model_.active()); | 364 selection_model.SetSelectedIndex(selection_model_.active()); |
| 356 for (int i = 0; i < RowCount(); ++i) | 365 for (int i = 0; i < RowCount(); ++i) |
| 357 selection_model.AddIndexToSelection(i); | 366 selection_model.AddIndexToSelection(i); |
| 358 SetSelectionModel(selection_model); | 367 SetSelectionModel(selection_model); |
| 359 return true; | 368 return true; |
| 360 } | 369 } |
| 361 break; | 370 break; |
| 362 | 371 |
| 363 case ui::VKEY_HOME: | 372 case ui::VKEY_HOME: |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 view_index = std::min(RowCount() - 1, view_index + 1); | 862 view_index = std::min(RowCount() - 1, view_index + 1); |
| 854 SelectByViewIndex(view_index); | 863 SelectByViewIndex(view_index); |
| 855 } | 864 } |
| 856 | 865 |
| 857 void TableView::ConfigureSelectionModelForEvent( | 866 void TableView::ConfigureSelectionModelForEvent( |
| 858 const ui::LocatedEvent& event, | 867 const ui::LocatedEvent& event, |
| 859 ui::ListSelectionModel* model) const { | 868 ui::ListSelectionModel* model) const { |
| 860 const int view_index = event.y() / row_height_; | 869 const int view_index = event.y() / row_height_; |
| 861 DCHECK(view_index >= 0 && view_index < RowCount()); | 870 DCHECK(view_index >= 0 && view_index < RowCount()); |
| 862 | 871 |
| 863 if (selection_model_.anchor() == -1 || | 872 if (selection_model_.anchor() == -1 || single_selection_ || |
| 864 single_selection_ || | 873 (!IsCmdOrCtrl(event) && !event.IsShiftDown())) { |
| 865 (!event.IsControlDown() && !event.IsShiftDown())) { | |
| 866 SelectRowsInRangeFrom(view_index, true, model); | 874 SelectRowsInRangeFrom(view_index, true, model); |
| 867 model->set_anchor(ViewToModel(view_index)); | 875 model->set_anchor(ViewToModel(view_index)); |
| 868 model->set_active(ViewToModel(view_index)); | 876 model->set_active(ViewToModel(view_index)); |
| 869 return; | 877 return; |
| 870 } | 878 } |
| 871 if ((event.IsControlDown() && event.IsShiftDown()) || event.IsShiftDown()) { | 879 if ((IsCmdOrCtrl(event) && event.IsShiftDown()) || event.IsShiftDown()) { |
| 872 // control-shift: copy existing model and make sure rows between anchor and | 880 // control-shift: copy existing model and make sure rows between anchor and |
| 873 // |view_index| are selected. | 881 // |view_index| are selected. |
| 874 // shift: reset selection so that only rows between anchor and |view_index| | 882 // shift: reset selection so that only rows between anchor and |view_index| |
| 875 // are selected. | 883 // are selected. |
| 876 if (event.IsControlDown() && event.IsShiftDown()) | 884 if (IsCmdOrCtrl(event) && event.IsShiftDown()) |
| 877 model->Copy(selection_model_); | 885 model->Copy(selection_model_); |
| 878 else | 886 else |
| 879 model->set_anchor(selection_model_.anchor()); | 887 model->set_anchor(selection_model_.anchor()); |
| 880 for (int i = std::min(view_index, ModelToView(model->anchor())), | 888 for (int i = std::min(view_index, ModelToView(model->anchor())), |
| 881 end = std::max(view_index, ModelToView(model->anchor())); | 889 end = std::max(view_index, ModelToView(model->anchor())); |
| 882 i <= end; ++i) { | 890 i <= end; ++i) { |
| 883 SelectRowsInRangeFrom(i, true, model); | 891 SelectRowsInRangeFrom(i, true, model); |
| 884 } | 892 } |
| 885 model->set_active(ViewToModel(view_index)); | 893 model->set_active(ViewToModel(view_index)); |
| 886 } else { | 894 } else { |
| 887 DCHECK(event.IsControlDown()); | 895 DCHECK(IsCmdOrCtrl(event)); |
| 888 // Toggle the selection state of |view_index| and set the anchor/active to | 896 // Toggle the selection state of |view_index| and set the anchor/active to |
| 889 // it and don't change the state of any other rows. | 897 // it and don't change the state of any other rows. |
| 890 model->Copy(selection_model_); | 898 model->Copy(selection_model_); |
| 891 model->set_anchor(ViewToModel(view_index)); | 899 model->set_anchor(ViewToModel(view_index)); |
| 892 model->set_active(ViewToModel(view_index)); | 900 model->set_active(ViewToModel(view_index)); |
| 893 SelectRowsInRangeFrom(view_index, | 901 SelectRowsInRangeFrom(view_index, |
| 894 !model->IsSelected(ViewToModel(view_index)), | 902 !model->IsSelected(ViewToModel(view_index)), |
| 895 model); | 903 model); |
| 896 } | 904 } |
| 897 } | 905 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 947 if (tooltip) | 955 if (tooltip) |
| 948 *tooltip = text; | 956 *tooltip = text; |
| 949 if (tooltip_origin) { | 957 if (tooltip_origin) { |
| 950 tooltip_origin->SetPoint(cell_bounds.x(), | 958 tooltip_origin->SetPoint(cell_bounds.x(), |
| 951 cell_bounds.y() + kTextVerticalPadding); | 959 cell_bounds.y() + kTextVerticalPadding); |
| 952 } | 960 } |
| 953 return true; | 961 return true; |
| 954 } | 962 } |
| 955 | 963 |
| 956 } // namespace views | 964 } // namespace views |
| OLD | NEW |