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 <gtk/gtk.h> | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/scoped_ptr.h" | |
10 #include "chrome/browser/accessibility_events.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" | |
13 #include "chrome/browser/gtk/gtk_util.h" | |
14 #include "chrome/browser/gtk/options/advanced_page_gtk.h" | |
15 #include "chrome/browser/gtk/options/content_page_gtk.h" | |
16 #include "chrome/browser/gtk/options/general_page_gtk.h" | |
17 #include "chrome/browser/prefs/pref_member.h" | |
18 #include "chrome/browser/prefs/pref_service.h" | |
19 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/browser/ui/browser_list.h" | |
21 #include "chrome/browser/ui/browser_window.h" | |
22 #include "chrome/browser/ui/options/options_window.h" | |
23 #include "chrome/browser/ui/window_sizer.h" | |
24 #include "chrome/common/pref_names.h" | |
25 #include "grit/chromium_strings.h" | |
26 #include "grit/generated_resources.h" | |
27 | |
28 #if defined(OS_CHROMEOS) | |
29 #include "chrome/browser/chromeos/options/internet_page_view.h" | |
30 #include "chrome/browser/chromeos/options/system_page_view.h" | |
31 #endif | |
32 | |
33 /////////////////////////////////////////////////////////////////////////////// | |
34 // OptionsWindowGtk | |
35 // | |
36 // The contents of the Options dialog window. | |
37 | |
38 class OptionsWindowGtk { | |
39 public: | |
40 explicit OptionsWindowGtk(Profile* profile); | |
41 ~OptionsWindowGtk(); | |
42 | |
43 // Shows the Tab corresponding to the specified OptionsPage. | |
44 void ShowOptionsPage(OptionsPage page, OptionsGroup highlight_group); | |
45 | |
46 private: | |
47 static void OnSwitchPage(GtkNotebook* notebook, GtkNotebookPage* page, | |
48 guint page_num, OptionsWindowGtk* window); | |
49 | |
50 static void OnWindowDestroy(GtkWidget* widget, OptionsWindowGtk* window); | |
51 | |
52 // The options dialog. | |
53 GtkWidget* dialog_; | |
54 | |
55 // The container of the option pages. | |
56 GtkWidget* notebook_; | |
57 | |
58 // The Profile associated with these options. | |
59 Profile* profile_; | |
60 | |
61 // The general page. | |
62 GeneralPageGtk general_page_; | |
63 | |
64 // The content page. | |
65 ContentPageGtk content_page_; | |
66 | |
67 // The advanced (user data) page. | |
68 AdvancedPageGtk advanced_page_; | |
69 | |
70 // The last page the user was on when they opened the Options window. | |
71 IntegerPrefMember last_selected_page_; | |
72 | |
73 scoped_ptr<AccessibleWidgetHelper> accessible_widget_helper_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(OptionsWindowGtk); | |
76 }; | |
77 | |
78 // The singleton options window object. | |
79 static OptionsWindowGtk* options_window = NULL; | |
80 | |
81 /////////////////////////////////////////////////////////////////////////////// | |
82 // OptionsWindowGtk, public: | |
83 | |
84 OptionsWindowGtk::OptionsWindowGtk(Profile* profile) | |
85 // Always show preferences for the original profile. Most state when off | |
86 // the record comes from the original profile, but we explicitly use | |
87 // the original profile to avoid potential problems. | |
88 : profile_(profile->GetOriginalProfile()), | |
89 general_page_(profile_), | |
90 content_page_(profile_), | |
91 advanced_page_(profile_) { | |
92 | |
93 // We don't need to observe changes in this value. | |
94 last_selected_page_.Init(prefs::kOptionsWindowLastTabIndex, | |
95 g_browser_process->local_state(), NULL); | |
96 | |
97 std::string dialog_name = | |
98 l10n_util::GetStringFUTF8( | |
99 IDS_PREFERENCES_DIALOG_TITLE, | |
100 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); | |
101 dialog_ = gtk_dialog_new_with_buttons( | |
102 dialog_name.c_str(), | |
103 // Prefs window is shared between all browser windows. | |
104 NULL, | |
105 // Non-modal. | |
106 GTK_DIALOG_NO_SEPARATOR, | |
107 GTK_STOCK_CLOSE, | |
108 GTK_RESPONSE_CLOSE, | |
109 NULL); | |
110 | |
111 accessible_widget_helper_.reset(new AccessibleWidgetHelper( | |
112 dialog_, profile)); | |
113 accessible_widget_helper_->SendOpenWindowNotification(dialog_name); | |
114 | |
115 gtk_window_set_default_size(GTK_WINDOW(dialog_), 500, -1); | |
116 // Allow browser windows to go in front of the options dialog in metacity. | |
117 gtk_window_set_type_hint(GTK_WINDOW(dialog_), GDK_WINDOW_TYPE_HINT_NORMAL); | |
118 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | |
119 gtk_util::kContentAreaSpacing); | |
120 | |
121 notebook_ = gtk_notebook_new(); | |
122 | |
123 #if defined(OS_CHROMEOS) | |
124 gtk_notebook_append_page( | |
125 GTK_NOTEBOOK(notebook_), | |
126 (new chromeos::SystemPageView(profile_))->WrapInGtkWidget(), | |
127 gtk_label_new( | |
128 l10n_util::GetStringUTF8(IDS_OPTIONS_SYSTEM_TAB_LABEL).c_str())); | |
129 | |
130 gtk_notebook_append_page( | |
131 GTK_NOTEBOOK(notebook_), | |
132 (new chromeos::InternetPageView(profile_))->WrapInGtkWidget(), | |
133 gtk_label_new( | |
134 l10n_util::GetStringUTF8(IDS_OPTIONS_INTERNET_TAB_LABEL).c_str())); | |
135 #endif | |
136 | |
137 gtk_notebook_append_page( | |
138 GTK_NOTEBOOK(notebook_), | |
139 general_page_.get_page_widget(), | |
140 gtk_label_new( | |
141 l10n_util::GetStringUTF8(IDS_OPTIONS_GENERAL_TAB_LABEL).c_str())); | |
142 | |
143 gtk_notebook_append_page( | |
144 GTK_NOTEBOOK(notebook_), | |
145 content_page_.get_page_widget(), | |
146 gtk_label_new( | |
147 l10n_util::GetStringUTF8(IDS_OPTIONS_CONTENT_TAB_LABEL).c_str())); | |
148 | |
149 gtk_notebook_append_page( | |
150 GTK_NOTEBOOK(notebook_), | |
151 advanced_page_.get_page_widget(), | |
152 gtk_label_new( | |
153 l10n_util::GetStringUTF8(IDS_OPTIONS_ADVANCED_TAB_LABEL).c_str())); | |
154 | |
155 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), notebook_); | |
156 | |
157 DCHECK_EQ( | |
158 gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_)), OPTIONS_PAGE_COUNT); | |
159 | |
160 // Show the content so that we can compute full dialog size, both | |
161 // for centering and because we want to show the notebook before | |
162 // connecting switch-page signal, otherwise we'll immediately get a | |
163 // signal switching to page 0 and overwrite our last_selected_page_ | |
164 // value. | |
165 gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(dialog_))); | |
166 | |
167 if (Browser* b = BrowserList::GetLastActive()) { | |
168 gtk_util::CenterOverWindow(GTK_WINDOW(dialog_), | |
169 b->window()->GetNativeHandle()); | |
170 } | |
171 | |
172 // Now that we're centered over the browser, we add our dialog to its own | |
173 // window group. We don't do anything with the response and we don't want the | |
174 // options window's modal dialogs to be associated with the main browser | |
175 // window because gtk grabs work on a per window group basis. | |
176 gtk_window_group_add_window(gtk_window_group_new(), GTK_WINDOW(dialog_)); | |
177 g_object_unref(gtk_window_get_group(GTK_WINDOW(dialog_))); | |
178 | |
179 g_signal_connect(notebook_, "switch-page", G_CALLBACK(OnSwitchPage), this); | |
180 | |
181 // We only have one button and don't do any special handling, so just hook it | |
182 // directly to gtk_widget_destroy. | |
183 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL); | |
184 | |
185 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroy), this); | |
186 | |
187 gtk_widget_show(dialog_); | |
188 } | |
189 | |
190 OptionsWindowGtk::~OptionsWindowGtk() { | |
191 } | |
192 | |
193 void OptionsWindowGtk::ShowOptionsPage(OptionsPage page, | |
194 OptionsGroup highlight_group) { | |
195 if (Browser* b = BrowserList::GetLastActive()) { | |
196 gtk_util::CenterOverWindow(GTK_WINDOW(dialog_), | |
197 b->window()->GetNativeHandle()); | |
198 } | |
199 | |
200 // Bring options window to front if it already existed and isn't already | |
201 // in front | |
202 gtk_window_present_with_time(GTK_WINDOW(dialog_), | |
203 gtk_get_current_event_time()); | |
204 | |
205 if (page == OPTIONS_PAGE_DEFAULT) { | |
206 // Remember the last visited page from local state. | |
207 page = static_cast<OptionsPage>(last_selected_page_.GetValue()); | |
208 if (page == OPTIONS_PAGE_DEFAULT) | |
209 page = OPTIONS_PAGE_GENERAL; | |
210 } | |
211 // If the page number is out of bounds, reset to the first tab. | |
212 if (page < 0 || page >= gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_))) | |
213 page = OPTIONS_PAGE_GENERAL; | |
214 | |
215 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook_), page); | |
216 | |
217 // TODO(mattm): set highlight_group | |
218 } | |
219 | |
220 /////////////////////////////////////////////////////////////////////////////// | |
221 // OptionsWindowGtk, private: | |
222 | |
223 // static | |
224 void OptionsWindowGtk::OnSwitchPage(GtkNotebook* notebook, | |
225 GtkNotebookPage* page, | |
226 guint page_num, | |
227 OptionsWindowGtk* window) { | |
228 int index = page_num; | |
229 DCHECK(index > OPTIONS_PAGE_DEFAULT && index < OPTIONS_PAGE_COUNT); | |
230 window->last_selected_page_.SetValue(index); | |
231 } | |
232 | |
233 // static | |
234 void OptionsWindowGtk::OnWindowDestroy(GtkWidget* widget, | |
235 OptionsWindowGtk* window) { | |
236 options_window = NULL; | |
237 MessageLoop::current()->DeleteSoon(FROM_HERE, window); | |
238 } | |
239 | |
240 /////////////////////////////////////////////////////////////////////////////// | |
241 // Factory/finder method: | |
242 | |
243 #if !defined(OS_CHROMEOS) | |
244 // ShowOptionsWindow for non ChromeOS build. For ChromeOS build, see | |
245 // chrome/browser/chromeos/options/options_window_view.h | |
246 void ShowOptionsWindow(OptionsPage page, | |
247 OptionsGroup highlight_group, | |
248 Profile* profile) { | |
249 DCHECK(profile); | |
250 | |
251 // If there's already an existing options window, activate it and switch to | |
252 // the specified page. | |
253 if (!options_window) { | |
254 // Creating and initializing a bunch of controls generates a bunch of | |
255 // spurious events as control values change. Temporarily suppress | |
256 // accessibility events until the window is created. | |
257 profile->PauseAccessibilityEvents(); | |
258 | |
259 // Create the options window. | |
260 options_window = new OptionsWindowGtk(profile); | |
261 | |
262 // Resume accessibility events. | |
263 profile->ResumeAccessibilityEvents(); | |
264 } | |
265 options_window->ShowOptionsPage(page, highlight_group); | |
266 } | |
267 #endif // !defined(OS_CHROMEOS) | |
OLD | NEW |