Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: chrome/browser/gtk/options/url_picker_dialog_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. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | 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 <gtk/gtk.h>
6
7 #include "app/l10n_util.h"
8 #include "base/message_loop.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/gtk/accessible_widget_helper_gtk.h"
11 #include "chrome/browser/gtk/gtk_tree.h"
12 #include "chrome/browser/gtk/gtk_util.h"
13 #include "chrome/browser/gtk/options/url_picker_dialog_gtk.h"
14 #include "chrome/browser/net/url_fixer_upper.h"
15 #include "chrome/browser/possible_url_model.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/pref_names.h"
19 #include "gfx/gtk_util.h"
20 #include "googleurl/src/gurl.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "grit/locale_settings.h"
24 #include "net/base/net_util.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26
27 namespace {
28
29 // Column ids for |history_list_store_|.
30 enum {
31 COL_FAVICON,
32 COL_TITLE,
33 COL_DISPLAY_URL,
34 COL_COUNT,
35 };
36
37 } // anonymous namespace
38
39 UrlPickerDialogGtk::UrlPickerDialogGtk(UrlPickerCallback* callback,
40 Profile* profile,
41 GtkWindow* parent)
42 : profile_(profile),
43 callback_(callback) {
44 std::string dialog_name = l10n_util::GetStringUTF8(IDS_ASI_ADD_TITLE);
45 dialog_ = gtk_dialog_new_with_buttons(
46 dialog_name.c_str(),
47 parent,
48 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR),
49 GTK_STOCK_CANCEL,
50 GTK_RESPONSE_CANCEL,
51 NULL);
52 accessible_widget_helper_.reset(new AccessibleWidgetHelper(
53 dialog_, profile));
54 accessible_widget_helper_->SendOpenWindowNotification(dialog_name);
55
56 add_button_ = gtk_dialog_add_button(GTK_DIALOG(dialog_),
57 GTK_STOCK_ADD, GTK_RESPONSE_OK);
58 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_OK);
59 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox),
60 gtk_util::kContentAreaSpacing);
61
62 // URL entry.
63 GtkWidget* url_hbox = gtk_hbox_new(FALSE, gtk_util::kLabelSpacing);
64 GtkWidget* url_label = gtk_label_new(
65 l10n_util::GetStringUTF8(IDS_ASI_URL).c_str());
66 gtk_box_pack_start(GTK_BOX(url_hbox), url_label,
67 FALSE, FALSE, 0);
68 url_entry_ = gtk_entry_new();
69 accessible_widget_helper_->SetWidgetName(url_entry_, IDS_ASI_URL);
70 gtk_entry_set_activates_default(GTK_ENTRY(url_entry_), TRUE);
71 g_signal_connect(url_entry_, "changed",
72 G_CALLBACK(OnUrlEntryChangedThunk), this);
73 gtk_box_pack_start(GTK_BOX(url_hbox), url_entry_,
74 TRUE, TRUE, 0);
75 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), url_hbox,
76 FALSE, FALSE, 0);
77
78 // Recent history description label.
79 GtkWidget* history_vbox = gtk_vbox_new(FALSE, gtk_util::kLabelSpacing);
80 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), history_vbox);
81 GtkWidget* history_label = gtk_util::CreateBoldLabel(
82 l10n_util::GetStringUTF8(IDS_ASI_DESCRIPTION));
83 gtk_box_pack_start(GTK_BOX(history_vbox), history_label, FALSE, FALSE, 0);
84
85 // Recent history list.
86 GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL);
87 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
88 GTK_POLICY_AUTOMATIC,
89 GTK_POLICY_AUTOMATIC);
90 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window),
91 GTK_SHADOW_ETCHED_IN);
92 gtk_container_add(GTK_CONTAINER(history_vbox), scroll_window);
93
94 history_list_store_ = gtk_list_store_new(COL_COUNT,
95 GDK_TYPE_PIXBUF,
96 G_TYPE_STRING,
97 G_TYPE_STRING);
98 history_list_sort_ = gtk_tree_model_sort_new_with_model(
99 GTK_TREE_MODEL(history_list_store_));
100 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(history_list_sort_),
101 COL_TITLE, CompareTitle, this, NULL);
102 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(history_list_sort_),
103 COL_DISPLAY_URL, CompareURL, this, NULL);
104 history_tree_ = gtk_tree_view_new_with_model(history_list_sort_);
105 accessible_widget_helper_->SetWidgetName(
106 history_tree_, IDS_ASI_DESCRIPTION);
107 g_object_unref(history_list_store_);
108 g_object_unref(history_list_sort_);
109 gtk_container_add(GTK_CONTAINER(scroll_window), history_tree_);
110 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(history_tree_),
111 TRUE);
112 g_signal_connect(history_tree_, "row-activated",
113 G_CALLBACK(OnHistoryRowActivatedThunk), this);
114
115 history_selection_ = gtk_tree_view_get_selection(
116 GTK_TREE_VIEW(history_tree_));
117 gtk_tree_selection_set_mode(history_selection_,
118 GTK_SELECTION_SINGLE);
119 g_signal_connect(history_selection_, "changed",
120 G_CALLBACK(OnHistorySelectionChangedThunk), this);
121
122 // History list columns.
123 GtkTreeViewColumn* column = gtk_tree_view_column_new();
124 GtkCellRenderer* renderer = gtk_cell_renderer_pixbuf_new();
125 gtk_tree_view_column_pack_start(column, renderer, FALSE);
126 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", COL_FAVICON);
127 renderer = gtk_cell_renderer_text_new();
128 gtk_tree_view_column_pack_start(column, renderer, TRUE);
129 gtk_tree_view_column_add_attribute(column, renderer, "text", COL_TITLE);
130 gtk_tree_view_append_column(GTK_TREE_VIEW(history_tree_),
131 column);
132 gtk_tree_view_column_set_title(
133 column, l10n_util::GetStringUTF8(IDS_ASI_PAGE_COLUMN).c_str());
134 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
135 gtk_tree_view_column_set_resizable(column, TRUE);
136 gtk_tree_view_column_set_sort_column_id(column, COL_TITLE);
137
138 GtkTreeViewColumn* url_column = gtk_tree_view_column_new_with_attributes(
139 l10n_util::GetStringUTF8(IDS_ASI_URL_COLUMN).c_str(),
140 gtk_cell_renderer_text_new(),
141 "text", COL_DISPLAY_URL,
142 NULL);
143 gtk_tree_view_append_column(GTK_TREE_VIEW(history_tree_), url_column);
144 gtk_tree_view_column_set_sort_column_id(url_column, COL_DISPLAY_URL);
145
146 // Loading data, showing dialog.
147 url_table_model_.reset(new PossibleURLModel());
148 url_table_adapter_.reset(new gtk_tree::TableAdapter(this, history_list_store_,
149 url_table_model_.get()));
150 url_table_model_->Reload(profile_);
151
152 EnableControls();
153
154 // Set the size of the dialog.
155 gtk_widget_realize(dialog_);
156 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(dialog_),
157 IDS_URLPICKER_DIALOG_WIDTH_CHARS,
158 IDS_URLPICKER_DIALOG_HEIGHT_LINES,
159 true);
160
161 // Set the width of the first column as well.
162 int width;
163 gtk_util::GetWidgetSizeFromResources(
164 dialog_,
165 IDS_URLPICKER_DIALOG_LEFT_COLUMN_WIDTH_CHARS, 0,
166 &width, NULL);
167 gtk_tree_view_column_set_fixed_width(column, width);
168
169 gtk_util::ShowDialogWithLocalizedSize(dialog_,
170 IDS_URLPICKER_DIALOG_WIDTH_CHARS,
171 IDS_URLPICKER_DIALOG_HEIGHT_LINES,
172 false);
173
174 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this);
175 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this);
176 }
177
178 UrlPickerDialogGtk::~UrlPickerDialogGtk() {
179 delete callback_;
180 }
181
182 void UrlPickerDialogGtk::AddURL() {
183 callback_->Run(URLFixerUpper::FixupURL(
184 gtk_entry_get_text(GTK_ENTRY(url_entry_)), std::string()));
185 }
186
187 void UrlPickerDialogGtk::EnableControls() {
188 const gchar* text = gtk_entry_get_text(GTK_ENTRY(url_entry_));
189 gtk_widget_set_sensitive(add_button_, text && *text);
190 }
191
192 std::string UrlPickerDialogGtk::GetURLForPath(GtkTreePath* path) const {
193 gint row = gtk_tree::GetTreeSortChildRowNumForPath(history_list_sort_, path);
194 if (row < 0) {
195 NOTREACHED();
196 return std::string();
197 }
198 std::string languages =
199 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
200 // Because this gets parsed by FixupURL(), it's safe to omit the scheme or
201 // trailing slash, and unescape most characters, but we need to not drop any
202 // username/password, or unescape anything that changes the meaning.
203 return UTF16ToUTF8(net::FormatUrl(url_table_model_->GetURL(row),
204 languages, net::kFormatUrlOmitAll & ~net::kFormatUrlOmitUsernamePassword,
205 UnescapeRule::SPACES, NULL, NULL, NULL));
206 }
207
208 void UrlPickerDialogGtk::SetColumnValues(int row, GtkTreeIter* iter) {
209 SkBitmap bitmap = url_table_model_->GetIcon(row);
210 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap);
211 string16 title = url_table_model_->GetText(row, IDS_ASI_PAGE_COLUMN);
212 string16 url = url_table_model_->GetText(row, IDS_ASI_URL_COLUMN);
213 gtk_list_store_set(history_list_store_, iter,
214 COL_FAVICON, pixbuf,
215 COL_TITLE, UTF16ToUTF8(title).c_str(),
216 COL_DISPLAY_URL, UTF16ToUTF8(url).c_str(),
217 -1);
218 g_object_unref(pixbuf);
219 }
220
221 // static
222 gint UrlPickerDialogGtk::CompareTitle(GtkTreeModel* model,
223 GtkTreeIter* a,
224 GtkTreeIter* b,
225 gpointer window) {
226 int row1 = gtk_tree::GetRowNumForIter(model, a);
227 int row2 = gtk_tree::GetRowNumForIter(model, b);
228 return reinterpret_cast<UrlPickerDialogGtk*>(window)->url_table_model_->
229 CompareValues(row1, row2, IDS_ASI_PAGE_COLUMN);
230 }
231
232 // static
233 gint UrlPickerDialogGtk::CompareURL(GtkTreeModel* model,
234 GtkTreeIter* a,
235 GtkTreeIter* b,
236 gpointer window) {
237 int row1 = gtk_tree::GetRowNumForIter(model, a);
238 int row2 = gtk_tree::GetRowNumForIter(model, b);
239 return reinterpret_cast<UrlPickerDialogGtk*>(window)->url_table_model_->
240 CompareValues(row1, row2, IDS_ASI_URL_COLUMN);
241 }
242
243 void UrlPickerDialogGtk::OnUrlEntryChanged(GtkWidget* editable) {
244 EnableControls();
245 }
246
247 void UrlPickerDialogGtk::OnHistorySelectionChanged(
248 GtkTreeSelection* selection) {
249 GtkTreeIter iter;
250 if (!gtk_tree_selection_get_selected(selection, NULL, &iter)) {
251 // The user has unselected the history element, nothing to do.
252 return;
253 }
254 GtkTreePath* path = gtk_tree_model_get_path(
255 GTK_TREE_MODEL(history_list_sort_), &iter);
256 gtk_entry_set_text(GTK_ENTRY(url_entry_),
257 GetURLForPath(path).c_str());
258 gtk_tree_path_free(path);
259 }
260
261 void UrlPickerDialogGtk::OnHistoryRowActivated(GtkWidget* tree_view,
262 GtkTreePath* path,
263 GtkTreeViewColumn* column) {
264 callback_->Run(URLFixerUpper::FixupURL(GetURLForPath(path), std::string()));
265 gtk_widget_destroy(dialog_);
266 }
267
268 void UrlPickerDialogGtk::OnResponse(GtkWidget* dialog, int response_id) {
269 if (response_id == GTK_RESPONSE_OK)
270 AddURL();
271 gtk_widget_destroy(dialog_);
272 }
273
274 void UrlPickerDialogGtk::OnWindowDestroy(GtkWidget* widget) {
275 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
276 }
OLDNEW
« no previous file with comments | « chrome/browser/gtk/options/url_picker_dialog_gtk.h ('k') | chrome/browser/gtk/overflow_button.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698