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/content_settings_window_view.h" | |
6 | |
7 #include "base/stl_util-inl.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/prefs/pref_service.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/ui/views/options/advanced_page_view.h" | |
12 #include "chrome/browser/ui/views/options/content_filter_page_view.h" | |
13 #include "chrome/browser/ui/views/options/cookie_filter_page_view.h" | |
14 #include "chrome/browser/ui/views/options/general_page_view.h" | |
15 #include "chrome/browser/ui/views/options/plugin_filter_page_view.h" | |
16 #include "chrome/common/chrome_constants.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "grit/chromium_strings.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "grit/locale_settings.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "views/controls/label.h" | |
23 #include "views/widget/root_view.h" | |
24 #include "views/window/dialog_delegate.h" | |
25 #include "views/window/window.h" | |
26 | |
27 static ContentSettingsWindowView* instance_ = NULL; | |
28 // Content setting dialog bounds padding. | |
29 const int kDialogPadding = 7; | |
30 | |
31 namespace browser { | |
32 | |
33 // Declared in browser_dialogs.h so others don't have to depend on our header. | |
34 void ShowContentSettingsWindow(gfx::NativeWindow parent_window, | |
35 ContentSettingsType content_type, | |
36 Profile* profile) { | |
37 DCHECK(profile); | |
38 // If there's already an existing options window, activate it and switch to | |
39 // the specified page. | |
40 // TODO(beng): note this is not multi-simultaneous-profile-safe. When we care | |
41 // about this case this will have to be fixed. | |
42 if (!instance_) { | |
43 instance_ = new ContentSettingsWindowView(profile); | |
44 views::Window::CreateChromeWindow(parent_window, gfx::Rect(), instance_); | |
45 } | |
46 instance_->ShowContentSettingsTab(content_type); | |
47 } | |
48 | |
49 } // namespace browser | |
50 | |
51 ContentSettingsWindowView::ContentSettingsWindowView(Profile* profile) | |
52 // Always show preferences for the original profile. Most state when off | |
53 // the record comes from the original profile, but we explicitly use | |
54 // the original profile to avoid potential problems. | |
55 : profile_(profile->GetOriginalProfile()), | |
56 label_(NULL), | |
57 listbox_(NULL), | |
58 current_page_(0) { | |
59 // We don't need to observe changes in this value. | |
60 last_selected_page_.Init(prefs::kContentSettingsWindowLastTabIndex, | |
61 profile->GetPrefs(), NULL); | |
62 } | |
63 | |
64 ContentSettingsWindowView::~ContentSettingsWindowView() { | |
65 STLDeleteElements(&pages_); | |
66 } | |
67 | |
68 void ContentSettingsWindowView::ShowContentSettingsTab( | |
69 ContentSettingsType page) { | |
70 // This will show invisible windows and bring visible windows to the front. | |
71 window()->Show(); | |
72 | |
73 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) { | |
74 // Remember the last visited page from local state. | |
75 page = static_cast<ContentSettingsType>(last_selected_page_.GetValue()); | |
76 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) | |
77 page = CONTENT_SETTINGS_TYPE_COOKIES; | |
78 } | |
79 // If the page number is out of bounds, reset to the first tab. | |
80 if (page < 0 || page >= listbox_->GetRowCount()) | |
81 page = CONTENT_SETTINGS_TYPE_COOKIES; | |
82 | |
83 listbox_->SelectRow(static_cast<int>(page)); | |
84 ShowSettingsPage(listbox_->SelectedRow()); | |
85 } | |
86 | |
87 /////////////////////////////////////////////////////////////////////////////// | |
88 // ContentSettingsWindowView, views::DialogDelegate implementation: | |
89 | |
90 std::wstring ContentSettingsWindowView::GetWindowTitle() const { | |
91 return UTF16ToWide(l10n_util::GetStringUTF16(IDS_CONTENT_SETTINGS_TITLE)); | |
92 } | |
93 | |
94 void ContentSettingsWindowView::WindowClosing() { | |
95 instance_ = NULL; | |
96 } | |
97 | |
98 bool ContentSettingsWindowView::Cancel() { | |
99 return GetCurrentContentSettingsTabView()->CanClose(); | |
100 } | |
101 | |
102 views::View* ContentSettingsWindowView::GetContentsView() { | |
103 return this; | |
104 } | |
105 | |
106 /////////////////////////////////////////////////////////////////////////////// | |
107 // ContentSettingsWindowView, views::Listbox::Listener implementation: | |
108 | |
109 void ContentSettingsWindowView::ListboxSelectionChanged( | |
110 views::Listbox* sender) { | |
111 DCHECK_EQ(listbox_, sender); | |
112 ShowSettingsPage(listbox_->SelectedRow()); | |
113 last_selected_page_.SetValue(current_page_); | |
114 } | |
115 | |
116 /////////////////////////////////////////////////////////////////////////////// | |
117 // ContentSettingsWindowView, views::View overrides: | |
118 | |
119 void ContentSettingsWindowView::Layout() { | |
120 int listbox_width = views::Window::GetLocalizedContentsWidth( | |
121 IDS_CONTENT_SETTINGS_DIALOG_LISTBOX_WIDTH_CHARS); | |
122 label_->SetBounds(kDialogPadding, | |
123 kDialogPadding, | |
124 listbox_width, | |
125 label_->GetPreferredSize().height()); | |
126 | |
127 listbox_->SetBounds(kDialogPadding, | |
128 2 * kDialogPadding + label_->height(), | |
129 listbox_width, | |
130 height() - (3 * kDialogPadding) - label_->height()); | |
131 | |
132 if (pages_[current_page_]->parent()) { | |
133 pages_[current_page_]->SetBounds( | |
134 2 * kDialogPadding + listbox_width, | |
135 2 * kDialogPadding + label_->height(), | |
136 width() - (3 * kDialogPadding) - listbox_width, | |
137 height() - (2 * kDialogPadding)); | |
138 } | |
139 } | |
140 | |
141 gfx::Size ContentSettingsWindowView::GetPreferredSize() { | |
142 return gfx::Size(views::Window::GetLocalizedContentsSize( | |
143 IDS_CONTENT_SETTINGS_DIALOG_WIDTH_CHARS, | |
144 IDS_CONTENT_SETTINGS_DIALOG_HEIGHT_LINES)); | |
145 } | |
146 | |
147 void ContentSettingsWindowView::ViewHierarchyChanged(bool is_add, | |
148 views::View* parent, | |
149 views::View* child) { | |
150 // Can't init before we're inserted into a Container, because we require a | |
151 // HWND to parent native child controls to. | |
152 if (is_add && child == this) | |
153 Init(); | |
154 } | |
155 | |
156 /////////////////////////////////////////////////////////////////////////////// | |
157 // ContentSettingsWindowView, private: | |
158 | |
159 void ContentSettingsWindowView::Init() { | |
160 // Make sure we don't leak memory by calling this twice. | |
161 DCHECK(!listbox_); | |
162 | |
163 label_ = new views::Label( | |
164 l10n_util::GetStringUTF16(IDS_CONTENT_SETTINGS_FEATURES_LABEL)); | |
165 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
166 AddChildView(label_); | |
167 | |
168 pages_.push_back(new CookieFilterPageView(profile_)); | |
169 pages_.push_back( | |
170 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_IMAGES)); | |
171 pages_.push_back( | |
172 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); | |
173 pages_.push_back(new PluginFilterPageView(profile_)); | |
174 pages_.push_back( | |
175 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_POPUPS)); | |
176 pages_.push_back( | |
177 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | |
178 pages_.push_back( | |
179 new ContentFilterPageView(profile_, CONTENT_SETTINGS_TYPE_NOTIFICATIONS)); | |
180 for (size_t i = 0; i < pages_.size(); ++i) { | |
181 pages_[i]->set_parent_owned(false); | |
182 } | |
183 DCHECK_EQ(static_cast<int>(pages_.size()), CONTENT_SETTINGS_NUM_TYPES); | |
184 | |
185 std::vector<string16> strings; | |
186 strings.push_back(l10n_util::GetStringUTF16(IDS_COOKIES_TAB_LABEL)); | |
187 strings.push_back(l10n_util::GetStringUTF16(IDS_IMAGES_TAB_LABEL)); | |
188 strings.push_back(l10n_util::GetStringUTF16(IDS_JAVASCRIPT_TAB_LABEL)); | |
189 strings.push_back(l10n_util::GetStringUTF16(IDS_PLUGIN_TAB_LABEL)); | |
190 strings.push_back(l10n_util::GetStringUTF16(IDS_POPUP_TAB_LABEL)); | |
191 strings.push_back(l10n_util::GetStringUTF16(IDS_GEOLOCATION_TAB_LABEL)); | |
192 strings.push_back(l10n_util::GetStringUTF16(IDS_NOTIFICATIONS_TAB_LABEL)); | |
193 listbox_ = new views::Listbox(strings, this); | |
194 AddChildView(listbox_); | |
195 CHECK_EQ(strings.size(), pages_.size()); | |
196 } | |
197 | |
198 void ContentSettingsWindowView::ShowSettingsPage(int page) { | |
199 if (pages_[current_page_]->parent()) | |
200 RemoveChildView(pages_[current_page_]); | |
201 current_page_ = page; | |
202 AddChildView(pages_[current_page_]); | |
203 Layout(); | |
204 SchedulePaint(); | |
205 } | |
206 | |
207 const OptionsPageView* | |
208 ContentSettingsWindowView::GetCurrentContentSettingsTabView() const { | |
209 return static_cast<OptionsPageView*>(pages_[current_page_]); | |
210 } | |
OLD | NEW |