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 "chrome/browser/ui/views/website_settings/permission_selector_view.h" | 5 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/ui/website_settings/website_settings_ui.h" | 8 #include "chrome/browser/ui/website_settings/website_settings_ui.h" |
9 #include "grit/generated_resources.h" | 9 #include "grit/generated_resources.h" |
10 #include "ui/base/l10n/l10n_util.h" | 10 #include "ui/base/l10n/l10n_util.h" |
11 #include "ui/base/models/simple_menu_model.h" | 11 #include "ui/base/models/simple_menu_model.h" |
12 #include "ui/gfx/image/image.h" | 12 #include "ui/gfx/image/image.h" |
13 #include "ui/views/controls/button/menu_button.h" | 13 #include "ui/views/controls/button/menu_button.h" |
14 #include "ui/views/controls/image_view.h" | 14 #include "ui/views/controls/image_view.h" |
15 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
16 #include "ui/views/controls/menu/menu_model_adapter.h" | 16 #include "ui/views/controls/menu/menu_model_adapter.h" |
17 #include "ui/views/controls/menu/menu_runner.h" | 17 #include "ui/views/controls/menu/menu_runner.h" |
18 #include "ui/views/layout/grid_layout.h" | 18 #include "ui/views/layout/grid_layout.h" |
19 #include "ui/views/view.h" | 19 #include "ui/views/view.h" |
20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Left icon margin. | 24 // Left icon margin. |
25 const int kPermissionIconMarginLeft = 6; | 25 static const int kPermissionIconMarginLeft = 6; |
msw
2012/07/30 15:48:42
nit: you don't have to explicitly mark these two a
markusheintz_
2012/07/30 16:32:06
Done.
| |
26 // The width of the column that contains the permissions icons. | 26 // The width of the column that contains the permissions icons. |
27 const int kPermissionIconColumnWidth = 20; | 27 static const int kPermissionIconColumnWidth = 20; |
28 | 28 |
29 // An array with |ContentSetting|s ordered by CommandID. The array is used to | 29 // An array with |ContentSetting|s ordered by CommandID. The array is used to |
30 // lookup a content setting for a given command id. | 30 // lookup a content setting for a given command id. |
31 const ContentSetting kSettingsForCommandIDs[] = { | 31 const ContentSetting kSettingsForCommandIDs[] = { |
32 CONTENT_SETTING_DEFAULT, // COMMAND_SET_TO_DEFAULT | 32 CONTENT_SETTING_DEFAULT, // COMMAND_SET_TO_DEFAULT |
33 CONTENT_SETTING_ALLOW, // COMMAND_SET_TO_ALLOW | 33 CONTENT_SETTING_ALLOW, // COMMAND_SET_TO_ALLOW |
34 CONTENT_SETTING_BLOCK, // COMMAND_SET_TO_BLOCK | 34 CONTENT_SETTING_BLOCK, // COMMAND_SET_TO_BLOCK |
35 }; | 35 }; |
36 | 36 |
37 } // namespace | 37 } // namespace |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 class PermissionMenuButton : public views::MenuButton, | 87 class PermissionMenuButton : public views::MenuButton, |
88 public views::MenuButtonListener { | 88 public views::MenuButtonListener { |
89 public: | 89 public: |
90 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership | 90 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership |
91 // of the |model| remains with the caller and is not transfered to the | 91 // of the |model| remains with the caller and is not transfered to the |
92 // |PermissionMenuButton|. | 92 // |PermissionMenuButton|. |
93 PermissionMenuButton(const string16& text, | 93 PermissionMenuButton(const string16& text, |
94 PermissionMenuModel* model); | 94 PermissionMenuModel* model); |
95 virtual ~PermissionMenuButton(); | 95 virtual ~PermissionMenuButton(); |
96 | 96 |
97 // Overridden from views::MenuButton. | |
98 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
99 | |
100 // Overridden from views::TextButton. | |
101 virtual void SetText(const string16& text) OVERRIDE; | |
102 | |
97 private: | 103 private: |
98 // Overridden from views::MenuButtonListener: | 104 // Overridden from views::MenuButtonListener. |
99 virtual void OnMenuButtonClicked(View* source, | 105 virtual void OnMenuButtonClicked(View* source, |
100 const gfx::Point& point) OVERRIDE; | 106 const gfx::Point& point) OVERRIDE; |
101 | 107 |
102 PermissionMenuModel* menu_model_; // Owned by |PermissionSelectorView|. | 108 PermissionMenuModel* menu_model_; // Owned by |PermissionSelectorView|. |
103 scoped_ptr<views::MenuRunner> menu_runner_; | 109 scoped_ptr<views::MenuRunner> menu_runner_; |
104 | 110 |
105 DISALLOW_COPY_AND_ASSIGN(PermissionMenuButton); | 111 DISALLOW_COPY_AND_ASSIGN(PermissionMenuButton); |
106 }; | 112 }; |
107 | 113 |
108 /////////////////////////////////////////////////////////////////////////////// | 114 /////////////////////////////////////////////////////////////////////////////// |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 // Accelerators are not supported. | 157 // Accelerators are not supported. |
152 return false; | 158 return false; |
153 } | 159 } |
154 | 160 |
155 void PermissionMenuModel::ExecuteCommand(int command_id) { | 161 void PermissionMenuModel::ExecuteCommand(int command_id) { |
156 current_setting_ = kSettingsForCommandIDs[command_id]; | 162 current_setting_ = kSettingsForCommandIDs[command_id]; |
157 permission_selector_->SelectionChanged(); | 163 permission_selector_->SelectionChanged(); |
158 } | 164 } |
159 | 165 |
160 /////////////////////////////////////////////////////////////////////////////// | 166 /////////////////////////////////////////////////////////////////////////////// |
161 // PermissionMenuModel | 167 // PermissionMenuButton |
162 /////////////////////////////////////////////////////////////////////////////// | 168 /////////////////////////////////////////////////////////////////////////////// |
163 | 169 |
164 PermissionMenuButton::PermissionMenuButton(const string16& text, | 170 PermissionMenuButton::PermissionMenuButton(const string16& text, |
165 PermissionMenuModel* model) | 171 PermissionMenuModel* model) |
166 : ALLOW_THIS_IN_INITIALIZER_LIST(MenuButton(NULL, text, this, true)), | 172 : ALLOW_THIS_IN_INITIALIZER_LIST(MenuButton(NULL, text, this, true)), |
167 menu_model_(model) { | 173 menu_model_(model) { |
168 } | 174 } |
169 | 175 |
170 PermissionMenuButton::~PermissionMenuButton() { | 176 PermissionMenuButton::~PermissionMenuButton() { |
171 } | 177 } |
172 | 178 |
179 gfx::Size PermissionMenuButton::GetPreferredSize() { | |
180 gfx::Insets insets = GetInsets(); | |
181 // Scale the button to the current text size. | |
182 gfx::Size prefsize(text_size_.width() + insets.width(), | |
183 text_size_.height() + insets.height()); | |
184 if (max_width_ > 0) | |
185 prefsize.set_width(std::min(max_width_, prefsize.width())); | |
186 if (show_menu_marker()) { | |
187 prefsize.Enlarge(menu_marker()->width() + | |
188 views::MenuButton::kMenuMarkerPaddingLeft + | |
msw
2012/07/30 15:48:42
nit: Should these indent to match the paren above?
markusheintz_
2012/07/30 16:32:06
I'm not sure either. I copied that style from menu
| |
189 views::MenuButton::kMenuMarkerPaddingRight, | |
190 0); | |
191 } | |
192 return prefsize; | |
193 } | |
194 | |
195 void PermissionMenuButton::SetText(const string16& text) { | |
196 MenuButton::SetText(text); | |
197 SizeToPreferredSize(); | |
198 } | |
199 | |
173 void PermissionMenuButton::OnMenuButtonClicked(View* source, | 200 void PermissionMenuButton::OnMenuButtonClicked(View* source, |
174 const gfx::Point& point) { | 201 const gfx::Point& point) { |
175 views::MenuModelAdapter menu_model_adapter(menu_model_); | 202 views::MenuModelAdapter menu_model_adapter(menu_model_); |
176 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); | 203 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); |
177 | 204 |
178 gfx::Point p(point); | 205 gfx::Point p(point); |
179 p.Offset(-source->width(), 0); | 206 p.Offset(-source->width(), 0); |
180 if (menu_runner_->RunMenuAt( | 207 if (menu_runner_->RunMenuAt( |
181 source->GetWidget()->GetTopLevelWidget(), | 208 source->GetWidget()->GetTopLevelWidget(), |
182 this, | 209 this, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 } | 306 } |
280 | 307 |
281 ContentSetting PermissionSelectorView::GetSelectedSetting() const { | 308 ContentSetting PermissionSelectorView::GetSelectedSetting() const { |
282 return menu_button_model_->current_setting(); | 309 return menu_button_model_->current_setting(); |
283 } | 310 } |
284 | 311 |
285 ContentSettingsType PermissionSelectorView::GetPermissionType() const { | 312 ContentSettingsType PermissionSelectorView::GetPermissionType() const { |
286 return menu_button_model_->site_permission(); | 313 return menu_button_model_->site_permission(); |
287 } | 314 } |
288 | 315 |
316 void PermissionSelectorView::ChildPreferredSizeChanged(View* child) { | |
317 SizeToPreferredSize(); | |
318 // FIXME: The parent is only a plain |View| that is used as a | |
319 // container/box/panel. The SizeToPreferredSize method of the parent is | |
320 // called here directly in order not to implement a custom |View| class with | |
321 // its own implementation of the ChildPreferredSizeChanged method. | |
322 parent()->SizeToPreferredSize(); | |
323 } | |
324 | |
289 PermissionSelectorView::~PermissionSelectorView() { | 325 PermissionSelectorView::~PermissionSelectorView() { |
290 } | 326 } |
OLD | NEW |