Chromium Code Reviews

Side by Side Diff: chrome/browser/gtk/options/content_settings_window_gtk.cc

Issue 6251001: Move chrome/browser/gtk/ to chrome/browser/ui/gtk/... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
(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 "chrome/browser/gtk/options/content_settings_window_gtk.h"
6
7 #include <string>
8
9 #include "app/l10n_util.h"
10 #include "base/command_line.h"
11 #include "base/message_loop.h"
12 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h"
13 #include "chrome/browser/gtk/browser_window_gtk.h"
14 #include "chrome/browser/gtk/gtk_util.h"
15 #include "chrome/browser/gtk/gtk_tree.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/common/content_settings_types.h"
21 #include "chrome/common/pref_names.h"
22 #include "grit/chromium_strings.h"
23 #include "grit/generated_resources.h"
24 #include "grit/locale_settings.h"
25
26 namespace {
27
28 // The singleton options window object.
29 ContentSettingsWindowGtk* settings_window = NULL;
30
31 } // namespace
32
33 // static
34 void ContentSettingsWindowGtk::Show(GtkWindow* parent,
35 ContentSettingsType page,
36 Profile* profile) {
37 DCHECK(profile);
38
39 if (!settings_window) {
40 // Creating and initializing a bunch of controls generates a bunch of
41 // spurious events as control values change. Temporarily suppress
42 // accessibility events until the window is created.
43 profile->PauseAccessibilityEvents();
44
45 // Create the options window.
46 settings_window = new ContentSettingsWindowGtk(parent, profile);
47
48 // Resume accessibility events.
49 profile->ResumeAccessibilityEvents();
50 }
51 settings_window->ShowContentSettingsTab(page);
52 }
53
54 ContentSettingsWindowGtk::ContentSettingsWindowGtk(GtkWindow* parent,
55 Profile* profile)
56 : profile_(profile),
57 cookie_page_(profile),
58 image_page_(profile, CONTENT_SETTINGS_TYPE_IMAGES),
59 javascript_page_(profile, CONTENT_SETTINGS_TYPE_JAVASCRIPT),
60 plugin_page_(profile, CONTENT_SETTINGS_TYPE_PLUGINS),
61 popup_page_(profile, CONTENT_SETTINGS_TYPE_POPUPS),
62 geolocation_page_(profile, CONTENT_SETTINGS_TYPE_GEOLOCATION),
63 notifications_page_(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
64 const struct {
65 int message_id;
66 GtkWidget* widget;
67 } kNotebookPages[] = {
68 { IDS_COOKIES_TAB_LABEL, cookie_page_.get_page_widget() },
69 { IDS_IMAGES_TAB_LABEL, image_page_.get_page_widget() },
70 { IDS_JAVASCRIPT_TAB_LABEL, javascript_page_.get_page_widget() },
71 { IDS_PLUGIN_TAB_LABEL, plugin_page_.get_page_widget() },
72 { IDS_POPUP_TAB_LABEL, popup_page_.get_page_widget() },
73 { IDS_GEOLOCATION_TAB_LABEL, geolocation_page_.get_page_widget() },
74 { IDS_NOTIFICATIONS_TAB_LABEL, notifications_page_.get_page_widget() },
75 };
76
77 // We don't need to observe changes in this value.
78 last_selected_page_.Init(prefs::kContentSettingsWindowLastTabIndex,
79 profile->GetPrefs(), NULL);
80
81 std::string dialog_name = l10n_util::GetStringUTF8(
82 IDS_CONTENT_SETTINGS_TITLE);
83 dialog_ = gtk_dialog_new_with_buttons(
84 dialog_name.c_str(),
85 parent,
86 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR),
87 GTK_STOCK_CLOSE,
88 GTK_RESPONSE_CLOSE,
89 NULL);
90 gtk_window_set_policy(GTK_WINDOW(dialog_), FALSE, FALSE, TRUE);
91
92 accessible_widget_helper_.reset(new AccessibleWidgetHelper(
93 dialog_, profile_));
94 accessible_widget_helper_->SendOpenWindowNotification(dialog_name);
95
96 gtk_window_set_default_size(GTK_WINDOW(dialog_), 500, -1);
97 // Allow browser windows to go in front of the options dialog in metacity.
98 gtk_window_set_type_hint(GTK_WINDOW(dialog_), GDK_WINDOW_TYPE_HINT_NORMAL);
99 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox),
100 gtk_util::kContentAreaSpacing);
101 gtk_util::SetWindowIcon(GTK_WINDOW(dialog_));
102
103 // Create hbox with list view and notebook.
104 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kContentAreaSpacing);
105
106 list_ = gtk_tree_view_new();
107 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list_), FALSE);
108
109 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
110 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(
111 "List Item", renderer, "text", 0, NULL);
112 gtk_tree_view_append_column(GTK_TREE_VIEW(list_), column);
113
114 const int kColumnCount = 1;
115 GtkListStore* store = gtk_list_store_new(kColumnCount, G_TYPE_STRING);
116 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNotebookPages); ++i) {
117 GtkTreeIter iter;
118 gtk_list_store_append(store, &iter);
119 std::string label = l10n_util::GetStringUTF8(kNotebookPages[i].message_id);
120 gtk_list_store_set(store, &iter, 0, label.c_str(), -1);
121 }
122 gtk_tree_view_set_model(GTK_TREE_VIEW(list_), GTK_TREE_MODEL(store));
123 g_object_unref(store);
124
125 // Needs to happen after the model is all set up.
126 GtkTreeSelection* selection = gtk_tree_view_get_selection(
127 GTK_TREE_VIEW(list_));
128 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
129
130 // Wrap the list widget in a scrolled window in order to have a frame.
131 GtkWidget* scrolled = gtk_scrolled_window_new(NULL, NULL);
132 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled),
133 GTK_SHADOW_ETCHED_IN);
134 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
135 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
136 gtk_container_add(GTK_CONTAINER(scrolled), list_);
137 gtk_box_pack_start(GTK_BOX(hbox), scrolled, FALSE, FALSE, 0);
138
139 notebook_ = gtk_notebook_new();
140 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNotebookPages); ++i) {
141 gtk_notebook_append_page(GTK_NOTEBOOK(notebook_),
142 kNotebookPages[i].widget,
143 NULL);
144 }
145 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook_), FALSE);
146 gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook_), FALSE);
147 gtk_box_pack_start(GTK_BOX(hbox), notebook_, FALSE, FALSE, 0);
148 DCHECK_EQ(gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_)),
149 CONTENT_SETTINGS_NUM_TYPES);
150
151 // Create vbox with "Features:" text and hbox below.
152 GtkWidget* vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing);
153 GtkWidget* label = gtk_label_new(
154 l10n_util::GetStringUTF8(IDS_CONTENT_SETTINGS_FEATURES_LABEL).c_str());
155 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
156 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
157 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
158 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), vbox);
159
160 // Need to show the notebook before connecting switch-page signal, otherwise
161 // we'll immediately get a signal switching to page 0 and overwrite our
162 // last_selected_page_ value.
163 gtk_util::ShowDialogWithLocalizedSize(dialog_, -1, -1, true);
164
165 g_signal_connect(notebook_, "switch-page",
166 G_CALLBACK(OnSwitchPageThunk), this);
167 g_signal_connect(selection, "changed",
168 G_CALLBACK(OnListSelectionChangedThunk), this);
169
170 // We only have one button and don't do any special handling, so just hook it
171 // directly to gtk_widget_destroy.
172 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL);
173
174 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this);
175 }
176
177 ContentSettingsWindowGtk::~ContentSettingsWindowGtk() {
178 }
179
180 void ContentSettingsWindowGtk::ShowContentSettingsTab(
181 ContentSettingsType page) {
182 // Bring options window to front if it already existed and isn't already
183 // in front
184 gtk_util::PresentWindow(dialog_, gtk_get_current_event_time());
185
186 if (page == CONTENT_SETTINGS_TYPE_DEFAULT) {
187 // Remember the last visited page from local state.
188 page = static_cast<ContentSettingsType>(last_selected_page_.GetValue());
189 if (page == CONTENT_SETTINGS_TYPE_DEFAULT)
190 page = CONTENT_SETTINGS_TYPE_COOKIES;
191 }
192 // If the page number is out of bounds, reset to the first tab.
193 if (page < 0 || page >= gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_)))
194 page = CONTENT_SETTINGS_TYPE_COOKIES;
195
196 gtk_tree::SelectAndFocusRowNum(page, GTK_TREE_VIEW(list_));
197 }
198
199 void ContentSettingsWindowGtk::OnSwitchPage(
200 GtkWidget* notebook,
201 GtkNotebookPage* page,
202 guint page_num) {
203 int index = page_num;
204 DCHECK(index > CONTENT_SETTINGS_TYPE_DEFAULT &&
205 index < CONTENT_SETTINGS_NUM_TYPES);
206
207 // Keep list in sync.
208 GtkTreeModel* model;
209 GtkTreeIter iter;
210 if (gtk_tree_selection_get_selected(
211 gtk_tree_view_get_selection(GTK_TREE_VIEW(list_)), &model, &iter)) {
212 gint row_index = gtk_tree::GetRowNumForIter(model, &iter);
213 if (row_index == index)
214 return;
215 }
216 gtk_tree::SelectAndFocusRowNum(index, GTK_TREE_VIEW(list_));
217 }
218
219 void ContentSettingsWindowGtk::OnWindowDestroy(GtkWidget* widget) {
220 settings_window = NULL;
221 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
222 }
223
224 void ContentSettingsWindowGtk::OnListSelectionChanged(
225 GtkTreeSelection* selection) {
226 GtkTreeModel* model;
227 GtkTreeIter iter;
228 if (!gtk_tree_selection_get_selected(selection, &model, &iter)) {
229 NOTREACHED();
230 return;
231 }
232 gint row_index = gtk_tree::GetRowNumForIter(model, &iter);
233 DCHECK(row_index > CONTENT_SETTINGS_TYPE_DEFAULT &&
234 row_index < CONTENT_SETTINGS_NUM_TYPES);
235
236 last_selected_page_.SetValue(row_index);
237
238 if (row_index != gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_)))
239 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook_), row_index);
240 }
OLDNEW
« no previous file with comments | « chrome/browser/gtk/options/content_settings_window_gtk.h ('k') | chrome/browser/gtk/options/cookie_filter_page_gtk.h » ('j') | no next file with comments »

Powered by Google App Engine