| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/views/options/geolocation_exceptions_view.h" | 5 #include "chrome/browser/views/options/simple_content_exceptions_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
| 12 #include "grit/locale_settings.h" | 12 #include "grit/locale_settings.h" |
| 13 #include "gfx/rect.h" | 13 #include "gfx/rect.h" |
| 14 #include "views/controls/button/native_button.h" | 14 #include "views/controls/button/native_button.h" |
| 15 #include "views/controls/table/table_view.h" | 15 #include "views/controls/table/table_view.h" |
| 16 #include "views/grid_layout.h" | 16 #include "views/grid_layout.h" |
| 17 #include "views/standard_layout.h" | 17 #include "views/standard_layout.h" |
| 18 #include "views/window/window.h" | 18 #include "views/window/window.h" |
| 19 | 19 |
| 20 static const int kExceptionsViewInsetSize = 5; | 20 static const int kExceptionsViewInsetSize = 5; |
| 21 static GeolocationExceptionsView* instance = NULL; | 21 static SimpleContentExceptionsView* instance = NULL; |
| 22 | 22 |
| 23 // static | 23 // static |
| 24 void GeolocationExceptionsView::ShowExceptionsWindow( | 24 void SimpleContentExceptionsView::ShowExceptionsWindow( |
| 25 gfx::NativeWindow parent, | 25 gfx::NativeWindow parent, |
| 26 GeolocationContentSettingsMap* map) { | 26 RemoveRowsTableModel* model) { |
| 27 scoped_ptr<RemoveRowsTableModel> owned_model(model); |
| 27 if (!instance) { | 28 if (!instance) { |
| 28 instance = new GeolocationExceptionsView(map); | 29 instance = new SimpleContentExceptionsView(owned_model.release()); |
| 29 views::Window::CreateChromeWindow(parent, gfx::Rect(), instance); | 30 views::Window::CreateChromeWindow(parent, gfx::Rect(), instance); |
| 30 } | 31 } |
| 31 | 32 |
| 32 // This will show invisible windows and bring visible windows to the front. | 33 // This will show invisible windows and bring visible windows to the front. |
| 33 instance->window()->Show(); | 34 instance->window()->Show(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 GeolocationExceptionsView::~GeolocationExceptionsView() { | 37 SimpleContentExceptionsView::~SimpleContentExceptionsView() { |
| 37 instance = NULL; | 38 instance = NULL; |
| 38 table_->SetModel(NULL); | 39 table_->SetModel(NULL); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void GeolocationExceptionsView::OnSelectionChanged() { | 42 void SimpleContentExceptionsView::OnSelectionChanged() { |
| 42 UpdateButtonState(); | 43 UpdateButtonState(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 void GeolocationExceptionsView::OnTableViewDelete( | 46 void SimpleContentExceptionsView::OnTableViewDelete( |
| 46 views::TableView* table_view) { | 47 views::TableView* table_view) { |
| 47 Remove(); | 48 Remove(); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void GeolocationExceptionsView::ButtonPressed(views::Button* sender, | 51 void SimpleContentExceptionsView::ButtonPressed(views::Button* sender, |
| 51 const views::Event& event) { | 52 const views::Event& event) { |
| 52 switch (sender->tag()) { | 53 switch (sender->tag()) { |
| 53 case IDS_EXCEPTIONS_REMOVEALL_BUTTON: | 54 case IDS_EXCEPTIONS_REMOVEALL_BUTTON: |
| 54 RemoveAll(); | 55 RemoveAll(); |
| 55 break; | 56 break; |
| 56 case IDS_EXCEPTIONS_REMOVE_BUTTON: | 57 case IDS_EXCEPTIONS_REMOVE_BUTTON: |
| 57 Remove(); | 58 Remove(); |
| 58 break; | 59 break; |
| 59 default: | 60 default: |
| 60 NOTREACHED(); | 61 NOTREACHED(); |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 | 64 |
| 64 void GeolocationExceptionsView::Layout() { | 65 void SimpleContentExceptionsView::Layout() { |
| 65 views::NativeButton* buttons[] = { remove_button_, remove_all_button_ }; | 66 views::NativeButton* buttons[] = { remove_button_, remove_all_button_ }; |
| 66 | 67 |
| 67 // The buttons are placed in the parent, but we need to lay them out. | 68 // The buttons are placed in the parent, but we need to lay them out. |
| 68 int max_y = GetParent()->GetLocalBounds(false).bottom() - kButtonVEdgeMargin; | 69 int max_y = GetParent()->GetLocalBounds(false).bottom() - kButtonVEdgeMargin; |
| 69 int x = kPanelHorizMargin; | 70 int x = kPanelHorizMargin; |
| 70 | 71 |
| 71 for (size_t i = 0; i < arraysize(buttons); ++i) { | 72 for (size_t i = 0; i < arraysize(buttons); ++i) { |
| 72 gfx::Size pref = buttons[i]->GetPreferredSize(); | 73 gfx::Size pref = buttons[i]->GetPreferredSize(); |
| 73 buttons[i]->SetBounds(x, max_y - pref.height(), pref.width(), | 74 buttons[i]->SetBounds(x, max_y - pref.height(), pref.width(), |
| 74 pref.height()); | 75 pref.height()); |
| 75 x += pref.width() + kRelatedControlHorizontalSpacing; | 76 x += pref.width() + kRelatedControlHorizontalSpacing; |
| 76 } | 77 } |
| 77 | 78 |
| 78 // Lay out the rest of this view. | 79 // Lay out the rest of this view. |
| 79 View::Layout(); | 80 View::Layout(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 gfx::Size GeolocationExceptionsView::GetPreferredSize() { | 83 gfx::Size SimpleContentExceptionsView::GetPreferredSize() { |
| 83 return gfx::Size(views::Window::GetLocalizedContentsSize( | 84 return gfx::Size(views::Window::GetLocalizedContentsSize( |
| 84 IDS_GEOLOCATION_EXCEPTION_DIALOG_WIDTH_CHARS, | 85 IDS_GEOLOCATION_EXCEPTION_DIALOG_WIDTH_CHARS, |
| 85 IDS_GEOLOCATION_EXCEPTION_DIALOG_HEIGHT_LINES)); | 86 IDS_GEOLOCATION_EXCEPTION_DIALOG_HEIGHT_LINES)); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void GeolocationExceptionsView::ViewHierarchyChanged(bool is_add, | 89 void SimpleContentExceptionsView::ViewHierarchyChanged(bool is_add, |
| 89 views::View* parent, | 90 views::View* parent, |
| 90 views::View* child) { | 91 views::View* child) { |
| 91 if (is_add && child == this) | 92 if (is_add && child == this) |
| 92 Init(); | 93 Init(); |
| 93 } | 94 } |
| 94 | 95 |
| 95 std::wstring GeolocationExceptionsView::GetWindowTitle() const { | 96 std::wstring SimpleContentExceptionsView::GetWindowTitle() const { |
| 96 return l10n_util::GetString(IDS_GEOLOCATION_EXCEPTION_TITLE); | 97 return l10n_util::GetString(IDS_GEOLOCATION_EXCEPTION_TITLE); |
| 97 } | 98 } |
| 98 | 99 |
| 99 GeolocationExceptionsView::GeolocationExceptionsView( | 100 SimpleContentExceptionsView::SimpleContentExceptionsView( |
| 100 GeolocationContentSettingsMap* map) | 101 RemoveRowsTableModel* model) |
| 101 : model_(map), | 102 : model_(model), |
| 102 table_(NULL), | 103 table_(NULL), |
| 103 remove_button_(NULL), | 104 remove_button_(NULL), |
| 104 remove_all_button_(NULL) { | 105 remove_all_button_(NULL) { |
| 105 } | 106 } |
| 106 | 107 |
| 107 void GeolocationExceptionsView::Init() { | 108 void SimpleContentExceptionsView::Init() { |
| 108 if (table_) | 109 if (table_) |
| 109 return; // We've already Init'd. | 110 return; // We've already Init'd. |
| 110 | 111 |
| 111 using views::GridLayout; | 112 using views::GridLayout; |
| 112 | 113 |
| 113 std::vector<TableColumn> columns; | 114 std::vector<TableColumn> columns; |
| 114 columns.push_back( | 115 columns.push_back( |
| 115 TableColumn(IDS_EXCEPTIONS_HOSTNAME_HEADER, TableColumn::LEFT, -1, .75)); | 116 TableColumn(IDS_EXCEPTIONS_HOSTNAME_HEADER, TableColumn::LEFT, -1, .75)); |
| 116 columns.back().sortable = true; | 117 columns.back().sortable = true; |
| 117 columns.push_back( | 118 columns.push_back( |
| 118 TableColumn(IDS_EXCEPTIONS_ACTION_HEADER, TableColumn::LEFT, -1, .25)); | 119 TableColumn(IDS_EXCEPTIONS_ACTION_HEADER, TableColumn::LEFT, -1, .25)); |
| 119 columns.back().sortable = true; | 120 columns.back().sortable = true; |
| 120 table_ = new views::TableView(&model_, columns, views::TEXT_ONLY, false, true, | 121 table_ = new views::TableView(model_.get(), columns, views::TEXT_ONLY, |
| 121 false); | 122 false, true, false); |
| 122 views::TableView::SortDescriptors sort; | 123 views::TableView::SortDescriptors sort; |
| 123 sort.push_back( | 124 sort.push_back( |
| 124 views::TableView::SortDescriptor(IDS_EXCEPTIONS_HOSTNAME_HEADER, true)); | 125 views::TableView::SortDescriptor(IDS_EXCEPTIONS_HOSTNAME_HEADER, true)); |
| 125 table_->SetSortDescriptors(sort); | 126 table_->SetSortDescriptors(sort); |
| 126 table_->SetObserver(this); | 127 table_->SetObserver(this); |
| 127 | 128 |
| 128 remove_button_ = new views::NativeButton( | 129 remove_button_ = new views::NativeButton( |
| 129 this, l10n_util::GetString(IDS_EXCEPTIONS_REMOVE_BUTTON)); | 130 this, l10n_util::GetString(IDS_EXCEPTIONS_REMOVE_BUTTON)); |
| 130 remove_button_->set_tag(IDS_EXCEPTIONS_REMOVE_BUTTON); | 131 remove_button_->set_tag(IDS_EXCEPTIONS_REMOVE_BUTTON); |
| 131 remove_all_button_ = new views::NativeButton( | 132 remove_all_button_ = new views::NativeButton( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 148 GridLayout::USE_PREF, 0, 0); | 149 GridLayout::USE_PREF, 0, 0); |
| 149 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | 150 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); |
| 150 | 151 |
| 151 layout->StartRow(1, single_column_layout_id); | 152 layout->StartRow(1, single_column_layout_id); |
| 152 layout->AddView(table_); | 153 layout->AddView(table_); |
| 153 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | 154 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); |
| 154 | 155 |
| 155 UpdateButtonState(); | 156 UpdateButtonState(); |
| 156 } | 157 } |
| 157 | 158 |
| 158 GeolocationExceptionsTableModel::Rows | 159 RemoveRowsTableModel::Rows |
| 159 GeolocationExceptionsView::GetSelectedRows() const { | 160 SimpleContentExceptionsView::GetSelectedRows() const { |
| 160 GeolocationExceptionsTableModel::Rows rows; | 161 RemoveRowsTableModel::Rows rows; |
| 161 for (views::TableView::iterator i(table_->SelectionBegin()); | 162 for (views::TableView::iterator i(table_->SelectionBegin()); |
| 162 i != table_->SelectionEnd(); ++i) | 163 i != table_->SelectionEnd(); ++i) |
| 163 rows.insert(*i); | 164 rows.insert(*i); |
| 164 return rows; | 165 return rows; |
| 165 } | 166 } |
| 166 | 167 |
| 167 void GeolocationExceptionsView::UpdateButtonState() { | 168 void SimpleContentExceptionsView::UpdateButtonState() { |
| 168 remove_button_->SetEnabled(model_.CanRemoveRows(GetSelectedRows())); | 169 remove_button_->SetEnabled(model_->CanRemoveRows(GetSelectedRows())); |
| 169 remove_all_button_->SetEnabled(model_.RowCount() > 0); | 170 remove_all_button_->SetEnabled(model_->RowCount() > 0); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void GeolocationExceptionsView::Remove() { | 173 void SimpleContentExceptionsView::Remove() { |
| 173 model_.RemoveRows(GetSelectedRows()); | 174 model_->RemoveRows(GetSelectedRows()); |
| 174 UpdateButtonState(); | 175 UpdateButtonState(); |
| 175 } | 176 } |
| 176 | 177 |
| 177 void GeolocationExceptionsView::RemoveAll() { | 178 void SimpleContentExceptionsView::RemoveAll() { |
| 178 model_.RemoveAll(); | 179 model_->RemoveAll(); |
| 179 UpdateButtonState(); | 180 UpdateButtonState(); |
| 180 } | 181 } |
| OLD | NEW |