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_exceptions_window_gtk.h" | 5 #include "chrome/browser/gtk/options/content_exceptions_window_gtk.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 ContentExceptionsWindowGtk::~ContentExceptionsWindowGtk() { | 43 ContentExceptionsWindowGtk::~ContentExceptionsWindowGtk() { |
44 } | 44 } |
45 | 45 |
46 ContentExceptionsWindowGtk::ContentExceptionsWindowGtk( | 46 ContentExceptionsWindowGtk::ContentExceptionsWindowGtk( |
47 GtkWindow* parent, | 47 GtkWindow* parent, |
48 HostContentSettingsMap* map, | 48 HostContentSettingsMap* map, |
49 ContentSettingsType type) { | 49 ContentSettingsType type) { |
50 // Build the model adapters that translate views and TableModels into | 50 // Build the list backing that GTK uses, along with an adapter which will |
51 // something GTK can use. | 51 // sort stuff without changing the underlying backing store. |
52 list_store_ = gtk_list_store_new(COL_COUNT, G_TYPE_STRING, G_TYPE_STRING); | 52 list_store_ = gtk_list_store_new(COL_COUNT, G_TYPE_STRING, G_TYPE_STRING); |
53 treeview_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store_)); | 53 sort_list_store_ = gtk_tree_model_sort_new_with_model( |
| 54 GTK_TREE_MODEL(list_store_)); |
| 55 treeview_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(sort_list_store_)); |
54 g_object_unref(list_store_); | 56 g_object_unref(list_store_); |
| 57 g_object_unref(sort_list_store_); |
55 | 58 |
56 // Set up the properties of the treeview | 59 // Set up the properties of the treeview |
57 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_), TRUE); | 60 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_), TRUE); |
58 g_signal_connect(treeview_, "row-activated", | 61 g_signal_connect(treeview_, "row-activated", |
59 G_CALLBACK(OnTreeViewRowActivateThunk), this); | 62 G_CALLBACK(OnTreeViewRowActivateThunk), this); |
60 | 63 |
61 GtkTreeViewColumn* pattern_column = gtk_tree_view_column_new_with_attributes( | 64 GtkTreeViewColumn* pattern_column = gtk_tree_view_column_new_with_attributes( |
62 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_PATTERN_HEADER).c_str(), | 65 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_PATTERN_HEADER).c_str(), |
63 gtk_cell_renderer_text_new(), | 66 gtk_cell_renderer_text_new(), |
64 "text", COL_PATTERN, | 67 "text", COL_PATTERN, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 181 |
179 gtk_tree::SelectAndFocusRowNum(new_index, GTK_TREE_VIEW(treeview_)); | 182 gtk_tree::SelectAndFocusRowNum(new_index, GTK_TREE_VIEW(treeview_)); |
180 | 183 |
181 UpdateButtonState(); | 184 UpdateButtonState(); |
182 } | 185 } |
183 | 186 |
184 void ContentExceptionsWindowGtk::UpdateButtonState() { | 187 void ContentExceptionsWindowGtk::UpdateButtonState() { |
185 int num_selected = gtk_tree_selection_count_selected_rows( | 188 int num_selected = gtk_tree_selection_count_selected_rows( |
186 treeview_selection_); | 189 treeview_selection_); |
187 int row_count = gtk_tree_model_iter_n_children( | 190 int row_count = gtk_tree_model_iter_n_children( |
188 GTK_TREE_MODEL(list_store_), NULL); | 191 GTK_TREE_MODEL(sort_list_store_), NULL); |
189 | 192 |
190 // TODO(erg): http://crbug.com/34177 , support editing of more than one entry | 193 // TODO(erg): http://crbug.com/34177 , support editing of more than one entry |
191 // at a time. | 194 // at a time. |
192 gtk_widget_set_sensitive(edit_button_, num_selected == 1); | 195 gtk_widget_set_sensitive(edit_button_, num_selected == 1); |
193 gtk_widget_set_sensitive(remove_button_, num_selected >= 1); | 196 gtk_widget_set_sensitive(remove_button_, num_selected >= 1); |
194 gtk_widget_set_sensitive(remove_all_button_, row_count > 0); | 197 gtk_widget_set_sensitive(remove_all_button_, row_count > 0); |
195 } | 198 } |
196 | 199 |
197 void ContentExceptionsWindowGtk::Add(GtkWidget* widget) { | 200 void ContentExceptionsWindowGtk::Add(GtkWidget* widget) { |
198 new ContentExceptionEditor(GTK_WINDOW(dialog_), | 201 new ContentExceptionEditor(GTK_WINDOW(dialog_), |
199 this, model_.get(), -1, | 202 this, model_.get(), -1, |
200 HostContentSettingsMap::Pattern(), | 203 HostContentSettingsMap::Pattern(), |
201 CONTENT_SETTING_BLOCK); | 204 CONTENT_SETTING_BLOCK); |
202 } | 205 } |
203 | 206 |
204 void ContentExceptionsWindowGtk::Edit(GtkWidget* widget) { | 207 void ContentExceptionsWindowGtk::Edit(GtkWidget* widget) { |
205 std::set<int> indices; | 208 std::set<std::pair<int, int> > indices; |
206 gtk_tree::GetSelectedIndices(treeview_selection_, &indices); | 209 GetSelectedModelIndices(&indices); |
207 DCHECK_GT(indices.size(), 0u); | 210 DCHECK_GT(indices.size(), 0u); |
208 int index = *indices.begin(); | 211 int index = indices.begin()->first; |
209 const HostContentSettingsMap::PatternSettingPair& entry = | 212 const HostContentSettingsMap::PatternSettingPair& entry = |
210 model_->entry_at(index); | 213 model_->entry_at(index); |
211 new ContentExceptionEditor(GTK_WINDOW(dialog_), this, model_.get(), index, | 214 new ContentExceptionEditor(GTK_WINDOW(dialog_), this, model_.get(), index, |
212 entry.first, entry.second); | 215 entry.first, entry.second); |
213 } | 216 } |
214 | 217 |
215 void ContentExceptionsWindowGtk::Remove(GtkWidget* widget) { | 218 void ContentExceptionsWindowGtk::Remove(GtkWidget* widget) { |
216 std::set<int> selected_indices; | 219 std::set<std::pair<int, int> > model_selected_indices; |
217 gtk_tree::GetSelectedIndices(treeview_selection_, &selected_indices); | 220 GetSelectedModelIndices(&model_selected_indices); |
218 | 221 |
219 int selected_row = 0; | 222 int selected_row = gtk_tree_model_iter_n_children( |
220 for (std::set<int>::reverse_iterator i = selected_indices.rbegin(); | 223 GTK_TREE_MODEL(sort_list_store_), NULL); |
221 i != selected_indices.rend(); ++i) { | 224 for (std::set<std::pair<int, int> >::reverse_iterator i = |
222 model_->RemoveException(*i); | 225 model_selected_indices.rbegin(); |
223 selected_row = *i; | 226 i != model_selected_indices.rend(); ++i) { |
| 227 model_->RemoveException(i->first); |
| 228 selected_row = std::min(selected_row, i->second); |
224 } | 229 } |
225 | 230 |
226 int row_count = model_->RowCount(); | 231 int row_count = model_->RowCount(); |
227 if (row_count <= 0) | 232 if (row_count <= 0) |
228 return; | 233 return; |
229 if (selected_row >= row_count) | 234 if (selected_row >= row_count) |
230 selected_row = row_count - 1; | 235 selected_row = row_count - 1; |
231 gtk_tree::SelectAndFocusRowNum(selected_row, | 236 gtk_tree::SelectAndFocusRowNum(selected_row, |
232 GTK_TREE_VIEW(treeview_)); | 237 GTK_TREE_VIEW(treeview_)); |
233 | 238 |
(...skipping 16 matching lines...) Expand all Loading... |
250 case CONTENT_SETTINGS_TYPE_PLUGINS: | 255 case CONTENT_SETTINGS_TYPE_PLUGINS: |
251 return l10n_util::GetStringUTF8(IDS_PLUGINS_EXCEPTION_TITLE); | 256 return l10n_util::GetStringUTF8(IDS_PLUGINS_EXCEPTION_TITLE); |
252 case CONTENT_SETTINGS_TYPE_POPUPS: | 257 case CONTENT_SETTINGS_TYPE_POPUPS: |
253 return l10n_util::GetStringUTF8(IDS_POPUP_EXCEPTION_TITLE); | 258 return l10n_util::GetStringUTF8(IDS_POPUP_EXCEPTION_TITLE); |
254 default: | 259 default: |
255 NOTREACHED(); | 260 NOTREACHED(); |
256 } | 261 } |
257 return std::string(); | 262 return std::string(); |
258 } | 263 } |
259 | 264 |
| 265 void ContentExceptionsWindowGtk::GetSelectedModelIndices( |
| 266 std::set<std::pair<int, int> >* indices) { |
| 267 GtkTreeModel* model; |
| 268 GList* paths = gtk_tree_selection_get_selected_rows(treeview_selection_, |
| 269 &model); |
| 270 for (GList* item = paths; item; item = item->next) { |
| 271 GtkTreePath* sorted_path = reinterpret_cast<GtkTreePath*>(item->data); |
| 272 int sorted_row = gtk_tree::GetRowNumForPath(sorted_path); |
| 273 GtkTreePath* path = gtk_tree_model_sort_convert_path_to_child_path( |
| 274 GTK_TREE_MODEL_SORT(sort_list_store_), sorted_path); |
| 275 int row = gtk_tree::GetRowNumForPath(path); |
| 276 gtk_tree_path_free(path); |
| 277 indices->insert(std::make_pair(row, sorted_row)); |
| 278 } |
| 279 |
| 280 g_list_foreach(paths, (GFunc)gtk_tree_path_free, NULL); |
| 281 g_list_free(paths); |
| 282 } |
| 283 |
260 void ContentExceptionsWindowGtk::OnTreeViewRowActivate( | 284 void ContentExceptionsWindowGtk::OnTreeViewRowActivate( |
261 GtkWidget* sender, | 285 GtkWidget* sender, |
262 GtkTreePath* path, | 286 GtkTreePath* path, |
263 GtkTreeViewColumn* column) { | 287 GtkTreeViewColumn* column) { |
264 Edit(sender); | 288 Edit(sender); |
265 } | 289 } |
266 | 290 |
267 void ContentExceptionsWindowGtk::OnWindowDestroy(GtkWidget* widget) { | 291 void ContentExceptionsWindowGtk::OnWindowDestroy(GtkWidget* widget) { |
268 instances[model_->content_type()] = NULL; | 292 instances[model_->content_type()] = NULL; |
269 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 293 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
270 } | 294 } |
271 | 295 |
272 void ContentExceptionsWindowGtk::OnTreeSelectionChanged( | 296 void ContentExceptionsWindowGtk::OnTreeSelectionChanged( |
273 GtkWidget* selection) { | 297 GtkWidget* selection) { |
274 UpdateButtonState(); | 298 UpdateButtonState(); |
275 } | 299 } |
OLD | NEW |