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