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