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