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