OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/gtk/options/content_settings_window_gtk.h" | 5 #include "chrome/browser/gtk/options/content_settings_window_gtk.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "chrome/browser/browser.h" | 12 #include "chrome/browser/browser.h" |
13 #include "chrome/browser/browser_list.h" | 13 #include "chrome/browser/browser_list.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" | 15 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" |
16 #include "chrome/browser/gtk/browser_window_gtk.h" | 16 #include "chrome/browser/gtk/browser_window_gtk.h" |
17 #include "chrome/browser/gtk/gtk_util.h" | 17 #include "chrome/browser/gtk/gtk_util.h" |
18 #include "chrome/browser/gtk/gtk_tree.h" | |
18 #include "chrome/browser/pref_service.h" | 19 #include "chrome/browser/pref_service.h" |
19 #include "chrome/browser/profile.h" | 20 #include "chrome/browser/profile.h" |
20 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/content_settings_types.h" | 22 #include "chrome/common/content_settings_types.h" |
22 #include "chrome/common/notification_service.h" | 23 #include "chrome/common/notification_service.h" |
23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
24 #include "grit/chromium_strings.h" | 25 #include "grit/chromium_strings.h" |
25 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
26 #include "grit/locale_settings.h" | 27 #include "grit/locale_settings.h" |
27 | 28 |
(...skipping 28 matching lines...) Expand all Loading... | |
56 ContentSettingsWindowGtk::ContentSettingsWindowGtk(GtkWindow* parent, | 57 ContentSettingsWindowGtk::ContentSettingsWindowGtk(GtkWindow* parent, |
57 Profile* profile) | 58 Profile* profile) |
58 : profile_(profile), | 59 : profile_(profile), |
59 cookie_page_(profile), | 60 cookie_page_(profile), |
60 image_page_(profile, CONTENT_SETTINGS_TYPE_IMAGES), | 61 image_page_(profile, CONTENT_SETTINGS_TYPE_IMAGES), |
61 javascript_page_(profile, CONTENT_SETTINGS_TYPE_JAVASCRIPT), | 62 javascript_page_(profile, CONTENT_SETTINGS_TYPE_JAVASCRIPT), |
62 plugin_page_(profile, CONTENT_SETTINGS_TYPE_PLUGINS), | 63 plugin_page_(profile, CONTENT_SETTINGS_TYPE_PLUGINS), |
63 popup_page_(profile, CONTENT_SETTINGS_TYPE_POPUPS), | 64 popup_page_(profile, CONTENT_SETTINGS_TYPE_POPUPS), |
64 geolocation_page_(profile, CONTENT_SETTINGS_TYPE_GEOLOCATION), | 65 geolocation_page_(profile, CONTENT_SETTINGS_TYPE_GEOLOCATION), |
65 notifications_page_(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { | 66 notifications_page_(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { |
67 const struct { | |
68 int message_id; | |
69 GtkWidget* widget; | |
70 } kNotebookPages[] = { | |
71 { IDS_COOKIES_TAB_LABEL, cookie_page_.get_page_widget() }, | |
72 { IDS_IMAGES_TAB_LABEL, image_page_.get_page_widget() }, | |
73 { IDS_JAVASCRIPT_TAB_LABEL, javascript_page_.get_page_widget() }, | |
74 { IDS_PLUGIN_TAB_LABEL, plugin_page_.get_page_widget() }, | |
75 { IDS_POPUP_TAB_LABEL, popup_page_.get_page_widget() }, | |
76 { IDS_GEOLOCATION_TAB_LABEL, geolocation_page_.get_page_widget() }, | |
77 { IDS_NOTIFICATIONS_TAB_LABEL, notifications_page_.get_page_widget() }, | |
78 }; | |
79 | |
66 // We don't need to observe changes in this value. | 80 // We don't need to observe changes in this value. |
67 last_selected_page_.Init(prefs::kContentSettingsWindowLastTabIndex, | 81 last_selected_page_.Init(prefs::kContentSettingsWindowLastTabIndex, |
68 profile->GetPrefs(), NULL); | 82 profile->GetPrefs(), NULL); |
69 | 83 |
70 std::string dialog_name = l10n_util::GetStringUTF8( | 84 std::string dialog_name = l10n_util::GetStringUTF8( |
71 IDS_CONTENT_SETTINGS_TITLE); | 85 IDS_CONTENT_SETTINGS_TITLE); |
72 dialog_ = gtk_dialog_new_with_buttons( | 86 dialog_ = gtk_dialog_new_with_buttons( |
73 dialog_name.c_str(), | 87 dialog_name.c_str(), |
74 parent, | 88 parent, |
75 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | 89 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), |
76 GTK_STOCK_CLOSE, | 90 GTK_STOCK_CLOSE, |
77 GTK_RESPONSE_CLOSE, | 91 GTK_RESPONSE_CLOSE, |
78 NULL); | 92 NULL); |
79 gtk_window_set_policy(GTK_WINDOW(dialog_), FALSE, FALSE, TRUE); | 93 gtk_window_set_policy(GTK_WINDOW(dialog_), FALSE, FALSE, TRUE); |
80 | 94 |
81 accessible_widget_helper_.reset(new AccessibleWidgetHelper( | 95 accessible_widget_helper_.reset(new AccessibleWidgetHelper( |
82 dialog_, profile_)); | 96 dialog_, profile_)); |
83 accessible_widget_helper_->SendOpenWindowNotification(dialog_name); | 97 accessible_widget_helper_->SendOpenWindowNotification(dialog_name); |
84 | 98 |
85 gtk_window_set_default_size(GTK_WINDOW(dialog_), 500, -1); | 99 gtk_window_set_default_size(GTK_WINDOW(dialog_), 500, -1); |
86 // Allow browser windows to go in front of the options dialog in metacity. | 100 // Allow browser windows to go in front of the options dialog in metacity. |
87 gtk_window_set_type_hint(GTK_WINDOW(dialog_), GDK_WINDOW_TYPE_HINT_NORMAL); | 101 gtk_window_set_type_hint(GTK_WINDOW(dialog_), GDK_WINDOW_TYPE_HINT_NORMAL); |
88 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | 102 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), |
89 gtk_util::kContentAreaSpacing); | 103 gtk_util::kContentAreaSpacing); |
90 gtk_util::SetWindowIcon(GTK_WINDOW(dialog_)); | 104 gtk_util::SetWindowIcon(GTK_WINDOW(dialog_)); |
91 | 105 |
106 // Create hbox with list view and notebook. | |
107 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kContentAreaSpacing); | |
108 | |
109 list_ = gtk_tree_view_new(); | |
110 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list_), FALSE); | |
111 | |
112 GtkCellRenderer* renderer = gtk_cell_renderer_text_new(); | |
113 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( | |
114 "List Item", renderer, "text", 0, NULL); | |
115 gtk_tree_view_append_column(GTK_TREE_VIEW(list_), column); | |
116 | |
117 const int kColumnCount = 1; | |
118 GtkListStore* store = gtk_list_store_new(kColumnCount, G_TYPE_STRING); | |
119 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNotebookPages); ++i) { | |
120 GtkTreeIter iter; | |
121 gtk_list_store_append(store, &iter); | |
122 std::string label = l10n_util::GetStringUTF8(kNotebookPages[i].message_id); | |
123 gtk_list_store_set(store, &iter, 0, label.c_str(), -1); | |
124 } | |
125 gtk_tree_view_set_model(GTK_TREE_VIEW(list_), GTK_TREE_MODEL(store)); | |
126 g_object_unref(store); | |
127 | |
128 // Need to show the list before connecting switch-page signal, otherwise | |
129 // we'll immediately get a signal switching to page 0 and overwrite our | |
130 // last_selected_page_ value. | |
131 gtk_util::ShowDialogWithLocalizedSize(dialog_, -1, -1, true); | |
Evan Martin
2010/07/08 17:43:22
BTW, this function is completely crazy. I kind of
| |
132 | |
133 // Needs to happen after the model is all set up. | |
134 GtkTreeSelection* selection = gtk_tree_view_get_selection( | |
135 GTK_TREE_VIEW(list_)); | |
136 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE); | |
Evan Martin
2010/07/08 17:43:22
I am happy you got this.
| |
137 | |
138 // Wrap the list widget in a scrolled window in order to have a frame. | |
139 GtkWidget* scrolled = gtk_scrolled_window_new(NULL, NULL); | |
140 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), | |
141 GTK_SHADOW_ETCHED_IN); | |
142 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), | |
143 GTK_POLICY_NEVER, GTK_POLICY_NEVER); | |
144 gtk_container_add(GTK_CONTAINER(scrolled), list_); | |
145 gtk_box_pack_start(GTK_BOX(hbox), scrolled, FALSE, FALSE, 0); | |
146 | |
92 notebook_ = gtk_notebook_new(); | 147 notebook_ = gtk_notebook_new(); |
93 | |
94 const struct { | |
95 int message_id; | |
96 GtkWidget* widget; | |
97 } kNotebookPages[] = { | |
98 { IDS_COOKIES_TAB_LABEL, cookie_page_.get_page_widget() }, | |
99 { IDS_IMAGES_TAB_LABEL, image_page_.get_page_widget() }, | |
100 { IDS_JAVASCRIPT_TAB_LABEL, javascript_page_.get_page_widget() }, | |
101 { IDS_PLUGIN_TAB_LABEL, plugin_page_.get_page_widget() }, | |
102 { IDS_POPUP_TAB_LABEL, popup_page_.get_page_widget() }, | |
103 { IDS_GEOLOCATION_TAB_LABEL, geolocation_page_.get_page_widget() }, | |
104 { IDS_NOTIFICATIONS_TAB_LABEL, notifications_page_.get_page_widget() }, | |
105 }; | |
106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNotebookPages); ++i) { | 148 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNotebookPages); ++i) { |
107 std::string label = l10n_util::GetStringUTF8(kNotebookPages[i].message_id); | 149 gtk_notebook_append_page(GTK_NOTEBOOK(notebook_), |
108 // Since the tabs are on the side, add some padding space to the label. | 150 kNotebookPages[i].widget, |
109 gtk_notebook_append_page( | 151 NULL); |
110 GTK_NOTEBOOK(notebook_), | |
111 kNotebookPages[i].widget, | |
112 gtk_label_new((" " + label + " ").c_str())); | |
113 } | 152 } |
114 | 153 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook_), FALSE); |
115 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook_), GTK_POS_LEFT); | 154 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook_), FALSE); |
116 | 155 gtk_box_pack_start(GTK_BOX(hbox), notebook_, FALSE, FALSE, 0); |
117 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), notebook_); | |
118 | |
119 DCHECK_EQ(gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_)), | 156 DCHECK_EQ(gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_)), |
120 CONTENT_SETTINGS_NUM_TYPES); | 157 CONTENT_SETTINGS_NUM_TYPES); |
121 | 158 |
159 // Create vbox with "Features:" text and hbox below. | |
160 GtkWidget* vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | |
161 GtkWidget* label = gtk_label_new( | |
162 l10n_util::GetStringUTF8(IDS_CONTENT_SETTINGS_FEATURES_LABEL).c_str()); | |
163 gtk_misc_set_alignment(GTK_MISC(label), 0, .5); | |
164 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); | |
165 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); | |
166 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), vbox); | |
167 | |
122 // Need to show the notebook before connecting switch-page signal, otherwise | 168 // Need to show the notebook before connecting switch-page signal, otherwise |
123 // we'll immediately get a signal switching to page 0 and overwrite our | 169 // we'll immediately get a signal switching to page 0 and overwrite our |
124 // last_selected_page_ value. | 170 // last_selected_page_ value. |
125 gtk_util::ShowDialogWithLocalizedSize(dialog_, -1, -1, true); | 171 gtk_util::ShowDialogWithLocalizedSize(dialog_, -1, -1, true); |
126 | 172 |
127 g_signal_connect(notebook_, "switch-page", | 173 g_signal_connect(notebook_, "switch-page", |
128 G_CALLBACK(OnSwitchPageThunk), this); | 174 G_CALLBACK(OnSwitchPageThunk), this); |
175 g_signal_connect(selection, "changed", | |
176 G_CALLBACK(OnListSelectionChangedThunk), this); | |
129 | 177 |
130 // We only have one button and don't do any special handling, so just hook it | 178 // We only have one button and don't do any special handling, so just hook it |
131 // directly to gtk_widget_destroy. | 179 // directly to gtk_widget_destroy. |
132 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL); | 180 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL); |
133 | 181 |
134 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this); | 182 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this); |
135 } | 183 } |
136 | 184 |
137 ContentSettingsWindowGtk::~ContentSettingsWindowGtk() { | 185 ContentSettingsWindowGtk::~ContentSettingsWindowGtk() { |
138 } | 186 } |
139 | 187 |
140 void ContentSettingsWindowGtk::ShowContentSettingsTab( | 188 void ContentSettingsWindowGtk::ShowContentSettingsTab( |
141 ContentSettingsType page) { | 189 ContentSettingsType page) { |
142 // Bring options window to front if it already existed and isn't already | 190 // Bring options window to front if it already existed and isn't already |
143 // in front | 191 // in front |
144 gtk_util::PresentWindow(dialog_, gtk_get_current_event_time()); | 192 gtk_util::PresentWindow(dialog_, gtk_get_current_event_time()); |
145 | 193 |
146 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) { | 194 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) { |
147 // Remember the last visited page from local state. | 195 // Remember the last visited page from local state. |
148 page = static_cast<ContentSettingsType>(last_selected_page_.GetValue()); | 196 page = static_cast<ContentSettingsType>(last_selected_page_.GetValue()); |
149 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) | 197 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) |
150 page = CONTENT_SETTINGS_TYPE_COOKIES; | 198 page = CONTENT_SETTINGS_TYPE_COOKIES; |
151 } | 199 } |
152 // If the page number is out of bounds, reset to the first tab. | 200 // If the page number is out of bounds, reset to the first tab. |
153 if (page < 0 || page >= gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_))) | 201 if (page < 0 || page >= gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_))) |
154 page = CONTENT_SETTINGS_TYPE_COOKIES; | 202 page = CONTENT_SETTINGS_TYPE_COOKIES; |
155 | 203 |
156 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook_), page); | 204 gtk_tree::SelectAndFocusRowNum(page, GTK_TREE_VIEW(list_)); |
157 } | 205 } |
158 | 206 |
159 void ContentSettingsWindowGtk::OnSwitchPage( | 207 void ContentSettingsWindowGtk::OnSwitchPage( |
160 GtkWidget* notebook, | 208 GtkWidget* notebook, |
161 GtkNotebookPage* page, | 209 GtkNotebookPage* page, |
162 guint page_num) { | 210 guint page_num) { |
163 int index = page_num; | 211 int index = page_num; |
164 DCHECK(index > CONTENT_SETTINGS_TYPE_DEFAULT && | 212 DCHECK(index > CONTENT_SETTINGS_TYPE_DEFAULT && |
165 index < CONTENT_SETTINGS_NUM_TYPES); | 213 index < CONTENT_SETTINGS_NUM_TYPES); |
166 last_selected_page_.SetValue(index); | 214 |
215 // Keep list in sync. | |
216 GtkTreeModel* model; | |
217 GtkTreeIter iter; | |
218 if (gtk_tree_selection_get_selected( | |
219 gtk_tree_view_get_selection(GTK_TREE_VIEW(list_)), &model, &iter)) { | |
220 gint row_index = gtk_tree::GetRowNumForIter(model, &iter); | |
221 if (row_index == index) | |
222 return; | |
223 } | |
224 gtk_tree::SelectAndFocusRowNum(index, GTK_TREE_VIEW(list_)); | |
167 } | 225 } |
168 | 226 |
169 void ContentSettingsWindowGtk::OnWindowDestroy(GtkWidget* widget) { | 227 void ContentSettingsWindowGtk::OnWindowDestroy(GtkWidget* widget) { |
170 settings_window = NULL; | 228 settings_window = NULL; |
171 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 229 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
172 } | 230 } |
231 | |
232 void ContentSettingsWindowGtk::OnListSelectionChanged( | |
233 GtkTreeSelection* selection) { | |
234 GtkTreeModel* model; | |
235 GtkTreeIter iter; | |
236 if (!gtk_tree_selection_get_selected(selection, &model, &iter)) { | |
237 NOTREACHED(); | |
238 return; | |
239 } | |
240 gint row_index = gtk_tree::GetRowNumForIter(model, &iter); | |
241 DCHECK(row_index > CONTENT_SETTINGS_TYPE_DEFAULT && | |
242 row_index < CONTENT_SETTINGS_NUM_TYPES); | |
243 | |
244 last_selected_page_.SetValue(row_index); | |
245 | |
246 if (row_index != | |
247 gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_))) | |
Evan Martin
2010/07/08 17:43:22
curlies, or fit on one line
| |
248 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook_), row_index); | |
249 } | |
OLD | NEW |