| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/keyword_editor_view.h" | 5 #include "chrome/browser/gtk/keyword_editor_view.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/gfx/gtk_util.h" |
| 8 #include "chrome/browser/gtk/edit_search_engine_dialog.h" | 9 #include "chrome/browser/gtk/edit_search_engine_dialog.h" |
| 9 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| 10 #include "chrome/browser/metrics/user_metrics.h" | 11 #include "chrome/browser/metrics/user_metrics.h" |
| 12 #include "chrome/browser/search_engines/keyword_editor_controller.h" |
| 11 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 12 #include "chrome/browser/search_engines/template_url_model.h" | 14 #include "chrome/browser/search_engines/template_url_model.h" |
| 15 #include "chrome/browser/search_engines/template_url_table_model.h" |
| 13 #include "chrome/common/gtk_util.h" | 16 #include "chrome/common/gtk_util.h" |
| 14 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 // Initial size for dialog. | 21 // Initial size for dialog. |
| 19 const int kDialogDefaultWidth = 450; | 22 const int kDialogDefaultWidth = 450; |
| 20 const int kDialogDefaultHeight = 450; | 23 const int kDialogDefaultHeight = 450; |
| 21 | 24 |
| 25 // How many rows should be added to an index into the |table_model_| to get the |
| 26 // corresponding row in |list_store_| |
| 27 const int kFirstGroupRowOffset = 2; |
| 28 const int kSecondGroupRowOffset = 5; |
| 29 |
| 22 // Column ids for |list_store_|. | 30 // Column ids for |list_store_|. |
| 23 enum { | 31 enum { |
| 24 COL_FAVICON, | 32 COL_FAVICON, |
| 25 COL_TITLE, | 33 COL_TITLE, |
| 26 COL_KEYWORD, | 34 COL_KEYWORD, |
| 35 COL_IS_HEADER, |
| 36 COL_IS_SEPARATOR, |
| 37 COL_WEIGHT, |
| 38 COL_WEIGHT_SET, |
| 27 COL_COUNT, | 39 COL_COUNT, |
| 28 }; | 40 }; |
| 29 | 41 |
| 30 KeywordEditorView* instance_ = NULL; | 42 KeywordEditorView* instance_ = NULL; |
| 31 | 43 |
| 32 } | 44 } |
| 33 | 45 |
| 34 // static | 46 // static |
| 35 void KeywordEditorView::Show(Profile* profile) { | 47 void KeywordEditorView::Show(Profile* profile) { |
| 36 DCHECK(profile); | 48 DCHECK(profile); |
| 37 if (!profile->GetTemplateURLModel()) | 49 if (!profile->GetTemplateURLModel()) |
| 38 return; | 50 return; |
| 39 | 51 |
| 40 // If there's already an existing editor window, activate it. | 52 // If there's already an existing editor window, activate it. |
| 41 if (instance_) { | 53 if (instance_) { |
| 42 gtk_window_present(GTK_WINDOW(instance_->dialog_)); | 54 gtk_window_present(GTK_WINDOW(instance_->dialog_)); |
| 43 } else { | 55 } else { |
| 44 instance_ = new KeywordEditorView(profile); | 56 instance_ = new KeywordEditorView(profile); |
| 45 } | 57 } |
| 46 } | 58 } |
| 47 | 59 |
| 48 void KeywordEditorView::OnEditedKeyword(const TemplateURL* template_url, | 60 void KeywordEditorView::OnEditedKeyword(const TemplateURL* template_url, |
| 49 const std::wstring& title, | 61 const std::wstring& title, |
| 50 const std::wstring& keyword, | 62 const std::wstring& keyword, |
| 51 const std::wstring& url) { | 63 const std::wstring& url) { |
| 52 NOTIMPLEMENTED(); | 64 if (template_url) { |
| 65 controller_->ModifyTemplateURL(template_url, title, keyword, url); |
| 66 |
| 67 // Force the make default button to update. |
| 68 EnableControls(); |
| 69 } else { |
| 70 SelectModelRow(controller_->AddTemplateURL(title, keyword, url)); |
| 71 } |
| 53 } | 72 } |
| 54 | 73 |
| 55 KeywordEditorView::~KeywordEditorView() { | 74 KeywordEditorView::~KeywordEditorView() { |
| 56 url_model_->RemoveObserver(this); | 75 controller_->url_model()->RemoveObserver(this); |
| 57 } | 76 } |
| 58 | 77 |
| 59 KeywordEditorView::KeywordEditorView(Profile* profile) | 78 KeywordEditorView::KeywordEditorView(Profile* profile) |
| 60 : profile_(profile), | 79 : profile_(profile), |
| 61 url_model_(profile->GetTemplateURLModel()) { | 80 controller_(new KeywordEditorController(profile)), |
| 81 table_model_(controller_->table_model()) { |
| 62 Init(); | 82 Init(); |
| 63 } | 83 } |
| 64 | 84 |
| 65 void KeywordEditorView::Init() { | 85 void KeywordEditorView::Init() { |
| 66 DCHECK(url_model_); | |
| 67 url_model_->Load(); | |
| 68 url_model_->AddObserver(this); | |
| 69 | |
| 70 dialog_ = gtk_dialog_new_with_buttons( | 86 dialog_ = gtk_dialog_new_with_buttons( |
| 71 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE).c_str(), | 87 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE).c_str(), |
| 72 NULL, | 88 NULL, |
| 73 // Non-modal. | 89 // Non-modal. |
| 74 GTK_DIALOG_NO_SEPARATOR, | 90 GTK_DIALOG_NO_SEPARATOR, |
| 75 GTK_STOCK_CLOSE, | 91 GTK_STOCK_CLOSE, |
| 76 GTK_RESPONSE_CLOSE, | 92 GTK_RESPONSE_CLOSE, |
| 77 NULL); | 93 NULL); |
| 78 | 94 |
| 79 gtk_window_set_default_size(GTK_WINDOW(dialog_), kDialogDefaultWidth, | 95 gtk_window_set_default_size(GTK_WINDOW(dialog_), kDialogDefaultWidth, |
| 80 kDialogDefaultHeight); | 96 kDialogDefaultHeight); |
| 81 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | 97 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), |
| 82 gtk_util::kContentAreaSpacing); | 98 gtk_util::kContentAreaSpacing); |
| 83 | 99 |
| 84 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing); | 100 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing); |
| 85 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), hbox); | 101 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), hbox); |
| 86 | 102 |
| 87 GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL); | 103 GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL); |
| 88 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), | 104 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), |
| 89 GTK_POLICY_AUTOMATIC, | 105 GTK_POLICY_AUTOMATIC, |
| 90 GTK_POLICY_AUTOMATIC); | 106 GTK_POLICY_AUTOMATIC); |
| 91 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window), | 107 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window), |
| 92 GTK_SHADOW_ETCHED_IN); | 108 GTK_SHADOW_ETCHED_IN); |
| 93 gtk_box_pack_start(GTK_BOX(hbox), scroll_window, TRUE, TRUE, 0); | 109 gtk_box_pack_start(GTK_BOX(hbox), scroll_window, TRUE, TRUE, 0); |
| 94 | 110 |
| 95 list_store_ = gtk_list_store_new(COL_COUNT, | 111 list_store_ = gtk_list_store_new(COL_COUNT, |
| 96 GDK_TYPE_PIXBUF, | 112 GDK_TYPE_PIXBUF, |
| 97 G_TYPE_STRING, | 113 G_TYPE_STRING, |
| 98 G_TYPE_STRING); | 114 G_TYPE_STRING, |
| 115 G_TYPE_BOOLEAN, |
| 116 G_TYPE_BOOLEAN, |
| 117 G_TYPE_INT, |
| 118 G_TYPE_BOOLEAN); |
| 99 tree_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store_)); | 119 tree_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store_)); |
| 100 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_), TRUE); | 120 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_), TRUE); |
| 121 gtk_tree_view_set_row_separator_func(GTK_TREE_VIEW(tree_), |
| 122 OnCheckRowIsSeparator, |
| 123 NULL, NULL); |
| 124 g_signal_connect(G_OBJECT(tree_), "row-activated", |
| 125 G_CALLBACK(OnRowActivated), this); |
| 101 gtk_container_add(GTK_CONTAINER(scroll_window), tree_); | 126 gtk_container_add(GTK_CONTAINER(scroll_window), tree_); |
| 102 | 127 |
| 103 GtkTreeViewColumn* title_column = gtk_tree_view_column_new(); | 128 GtkTreeViewColumn* title_column = gtk_tree_view_column_new(); |
| 104 GtkCellRenderer* pixbuf_renderer = gtk_cell_renderer_pixbuf_new(); | 129 GtkCellRenderer* pixbuf_renderer = gtk_cell_renderer_pixbuf_new(); |
| 105 gtk_tree_view_column_pack_start(title_column, pixbuf_renderer, FALSE); | 130 gtk_tree_view_column_pack_start(title_column, pixbuf_renderer, FALSE); |
| 106 gtk_tree_view_column_add_attribute(title_column, pixbuf_renderer, "pixbuf", | 131 gtk_tree_view_column_add_attribute(title_column, pixbuf_renderer, "pixbuf", |
| 107 COL_FAVICON); | 132 COL_FAVICON); |
| 108 GtkCellRenderer* title_renderer = gtk_cell_renderer_text_new(); | 133 GtkCellRenderer* title_renderer = gtk_cell_renderer_text_new(); |
| 109 gtk_tree_view_column_pack_start(title_column, title_renderer, TRUE); | 134 gtk_tree_view_column_pack_start(title_column, title_renderer, TRUE); |
| 110 gtk_tree_view_column_add_attribute(title_column, title_renderer, "text", | 135 gtk_tree_view_column_add_attribute(title_column, title_renderer, "text", |
| 111 COL_TITLE); | 136 COL_TITLE); |
| 137 gtk_tree_view_column_add_attribute(title_column, title_renderer, "weight", |
| 138 COL_WEIGHT); |
| 139 gtk_tree_view_column_add_attribute(title_column, title_renderer, "weight-set", |
| 140 COL_WEIGHT_SET); |
| 112 gtk_tree_view_column_set_title( | 141 gtk_tree_view_column_set_title( |
| 113 title_column, l10n_util::GetStringUTF8( | 142 title_column, l10n_util::GetStringUTF8( |
| 114 IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN).c_str()); | 143 IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN).c_str()); |
| 115 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), title_column); | 144 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), title_column); |
| 116 | 145 |
| 117 GtkTreeViewColumn* keyword_column = gtk_tree_view_column_new_with_attributes( | 146 GtkTreeViewColumn* keyword_column = gtk_tree_view_column_new_with_attributes( |
| 118 l10n_util::GetStringUTF8( | 147 l10n_util::GetStringUTF8( |
| 119 IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN).c_str(), | 148 IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN).c_str(), |
| 120 gtk_cell_renderer_text_new(), | 149 gtk_cell_renderer_text_new(), |
| 121 "text", COL_KEYWORD, | 150 "text", COL_KEYWORD, |
| 122 NULL); | 151 NULL); |
| 123 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), keyword_column); | 152 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), keyword_column); |
| 124 | 153 |
| 125 selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_)); | 154 selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_)); |
| 126 gtk_tree_selection_set_mode(selection_, GTK_SELECTION_SINGLE); | 155 gtk_tree_selection_set_mode(selection_, GTK_SELECTION_SINGLE); |
| 156 gtk_tree_selection_set_select_function(selection_, OnSelectionFilter, |
| 157 NULL, NULL); |
| 127 g_signal_connect(G_OBJECT(selection_), "changed", | 158 g_signal_connect(G_OBJECT(selection_), "changed", |
| 128 G_CALLBACK(OnSelectionChanged), this); | 159 G_CALLBACK(OnSelectionChanged), this); |
| 129 | 160 |
| 130 GtkWidget* button_box = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | 161 GtkWidget* button_box = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); |
| 131 gtk_box_pack_start(GTK_BOX(hbox), button_box, FALSE, FALSE, 0); | 162 gtk_box_pack_start(GTK_BOX(hbox), button_box, FALSE, FALSE, 0); |
| 132 | 163 |
| 133 add_button_ = gtk_button_new_with_label( | 164 add_button_ = gtk_button_new_with_label( |
| 134 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_NEW_BUTTON).c_str()); | 165 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_NEW_BUTTON).c_str()); |
| 135 g_signal_connect(G_OBJECT(add_button_), "clicked", | 166 g_signal_connect(G_OBJECT(add_button_), "clicked", |
| 136 G_CALLBACK(OnAddButtonClicked), this); | 167 G_CALLBACK(OnAddButtonClicked), this); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 150 gtk_box_pack_start(GTK_BOX(button_box), remove_button_, FALSE, FALSE, 0); | 181 gtk_box_pack_start(GTK_BOX(button_box), remove_button_, FALSE, FALSE, 0); |
| 151 | 182 |
| 152 make_default_button_ = gtk_button_new_with_label( | 183 make_default_button_ = gtk_button_new_with_label( |
| 153 l10n_util::GetStringUTF8( | 184 l10n_util::GetStringUTF8( |
| 154 IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON).c_str()); | 185 IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON).c_str()); |
| 155 g_signal_connect(G_OBJECT(make_default_button_), "clicked", | 186 g_signal_connect(G_OBJECT(make_default_button_), "clicked", |
| 156 G_CALLBACK(OnMakeDefaultButtonClicked), this); | 187 G_CALLBACK(OnMakeDefaultButtonClicked), this); |
| 157 gtk_box_pack_start(GTK_BOX(button_box), make_default_button_, FALSE, FALSE, | 188 gtk_box_pack_start(GTK_BOX(button_box), make_default_button_, FALSE, FALSE, |
| 158 0); | 189 0); |
| 159 | 190 |
| 191 controller_->url_model()->AddObserver(this); |
| 192 table_model_->SetObserver(this); |
| 193 table_model_->Reload(); |
| 194 |
| 160 EnableControls(); | 195 EnableControls(); |
| 161 | 196 |
| 162 gtk_widget_show_all(dialog_); | 197 gtk_widget_show_all(dialog_); |
| 163 | 198 |
| 164 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponse), this); | 199 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponse), this); |
| 165 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroy), this); | 200 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroy), this); |
| 166 } | 201 } |
| 167 | 202 |
| 168 void KeywordEditorView::EnableControls() { | 203 void KeywordEditorView::EnableControls() { |
| 169 bool enable = gtk_tree_selection_count_selected_rows(selection_) == 1; | 204 bool can_edit = false; |
| 170 bool can_make_default = false; | 205 bool can_make_default = false; |
| 171 bool can_remove = false; | 206 bool can_remove = false; |
| 172 // TODO(mattm) | 207 int model_row = GetSelectedModelRow(); |
| 173 gtk_widget_set_sensitive(add_button_, url_model_->loaded()); | 208 if (model_row != -1) { |
| 174 gtk_widget_set_sensitive(edit_button_, enable); | 209 can_edit = true; |
| 175 gtk_widget_set_sensitive(remove_button_, can_make_default); | 210 const TemplateURL* selected_url = controller_->GetTemplateURL(model_row); |
| 176 gtk_widget_set_sensitive(make_default_button_, can_remove); | 211 can_make_default = controller_->CanMakeDefault(selected_url); |
| 212 can_remove = controller_->CanRemove(selected_url); |
| 213 } |
| 214 gtk_widget_set_sensitive(add_button_, controller_->loaded()); |
| 215 gtk_widget_set_sensitive(edit_button_, can_edit); |
| 216 gtk_widget_set_sensitive(remove_button_, can_remove); |
| 217 gtk_widget_set_sensitive(make_default_button_, can_make_default); |
| 218 } |
| 219 |
| 220 void KeywordEditorView::SetColumnValues(int model_row, GtkTreeIter* iter) { |
| 221 SkBitmap bitmap = table_model_->GetIcon(model_row); |
| 222 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap); |
| 223 gtk_list_store_set( |
| 224 list_store_, iter, |
| 225 COL_FAVICON, pixbuf, |
| 226 // Dunno why, even with COL_WEIGHT_SET to FALSE here, the weight still |
| 227 // has an effect. So we just set it to normal. |
| 228 COL_WEIGHT, PANGO_WEIGHT_NORMAL, |
| 229 COL_WEIGHT_SET, TRUE, |
| 230 COL_TITLE, WideToUTF8(table_model_->GetText( |
| 231 model_row, IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN)).c_str(), |
| 232 COL_KEYWORD, WideToUTF8(table_model_->GetText( |
| 233 model_row, IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN)).c_str(), |
| 234 -1); |
| 235 } |
| 236 |
| 237 int KeywordEditorView::GetListStoreRowForModelRow(int model_row) const { |
| 238 if (model_row < model_second_group_index_) |
| 239 return model_row + kFirstGroupRowOffset; |
| 240 else |
| 241 return model_row + kSecondGroupRowOffset; |
| 242 } |
| 243 |
| 244 int KeywordEditorView::GetModelRowForPath(GtkTreePath* path) const { |
| 245 gint* indices = gtk_tree_path_get_indices(path); |
| 246 if (!indices) { |
| 247 NOTREACHED(); |
| 248 return -1; |
| 249 } |
| 250 if (indices[0] >= model_second_group_index_ + kSecondGroupRowOffset) |
| 251 return indices[0] - kSecondGroupRowOffset; |
| 252 return indices[0] - kFirstGroupRowOffset; |
| 253 } |
| 254 |
| 255 int KeywordEditorView::GetModelRowForIter(GtkTreeIter* iter) const { |
| 256 GtkTreePath* path = gtk_tree_model_get_path(GTK_TREE_MODEL(list_store_), |
| 257 iter); |
| 258 int model_row = GetModelRowForPath(path); |
| 259 gtk_tree_path_free(path); |
| 260 return model_row; |
| 261 } |
| 262 |
| 263 int KeywordEditorView::GetSelectedModelRow() const { |
| 264 GtkTreeIter iter; |
| 265 if (!gtk_tree_selection_get_selected(selection_, NULL, &iter)) |
| 266 return -1; |
| 267 return GetModelRowForIter(&iter); |
| 268 } |
| 269 |
| 270 void KeywordEditorView::SelectModelRow(int model_row) { |
| 271 int row = GetListStoreRowForModelRow(model_row); |
| 272 GtkTreeIter iter; |
| 273 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), |
| 274 &iter, NULL, row)) { |
| 275 NOTREACHED(); |
| 276 return; |
| 277 } |
| 278 GtkTreePath* path = gtk_tree_model_get_path(GTK_TREE_MODEL(list_store_), |
| 279 &iter); |
| 280 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_), path, NULL, FALSE); |
| 281 gtk_tree_path_free(path); |
| 282 } |
| 283 |
| 284 void KeywordEditorView::AddNodeToList(int model_row) { |
| 285 GtkTreeIter iter; |
| 286 int row = GetListStoreRowForModelRow(model_row); |
| 287 if (row == 0) { |
| 288 gtk_list_store_prepend(list_store_, &iter); |
| 289 } else { |
| 290 GtkTreeIter sibling; |
| 291 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &sibling, |
| 292 NULL, row - 1); |
| 293 gtk_list_store_insert_after(list_store_, &iter, &sibling); |
| 294 } |
| 295 |
| 296 SetColumnValues(model_row, &iter); |
| 297 } |
| 298 |
| 299 void KeywordEditorView::OnModelChanged() { |
| 300 model_second_group_index_ = table_model_->last_search_engine_index(); |
| 301 gtk_list_store_clear(list_store_); |
| 302 |
| 303 TableModel::Groups groups(table_model_->GetGroups()); |
| 304 if (groups.size() != 2) { |
| 305 NOTREACHED(); |
| 306 return; |
| 307 } |
| 308 |
| 309 GtkTreeIter iter; |
| 310 // First group title. |
| 311 gtk_list_store_append(list_store_, &iter); |
| 312 gtk_list_store_set( |
| 313 list_store_, &iter, |
| 314 COL_WEIGHT, PANGO_WEIGHT_BOLD, |
| 315 COL_WEIGHT_SET, TRUE, |
| 316 COL_TITLE, WideToUTF8(groups[0].title).c_str(), |
| 317 COL_IS_HEADER, TRUE, |
| 318 -1); |
| 319 // First group separator. |
| 320 gtk_list_store_append(list_store_, &iter); |
| 321 gtk_list_store_set( |
| 322 list_store_, &iter, |
| 323 COL_IS_HEADER, TRUE, |
| 324 COL_IS_SEPARATOR, TRUE, |
| 325 -1); |
| 326 |
| 327 // Blank row between groups. |
| 328 gtk_list_store_append(list_store_, &iter); |
| 329 gtk_list_store_set( |
| 330 list_store_, &iter, |
| 331 COL_IS_HEADER, TRUE, |
| 332 -1); |
| 333 // Second group title. |
| 334 gtk_list_store_append(list_store_, &iter); |
| 335 gtk_list_store_set( |
| 336 list_store_, &iter, |
| 337 COL_WEIGHT, PANGO_WEIGHT_BOLD, |
| 338 COL_WEIGHT_SET, TRUE, |
| 339 COL_TITLE, WideToUTF8(groups[1].title).c_str(), |
| 340 COL_IS_HEADER, TRUE, |
| 341 -1); |
| 342 // Second group separator. |
| 343 gtk_list_store_append(list_store_, &iter); |
| 344 gtk_list_store_set( |
| 345 list_store_, &iter, |
| 346 COL_IS_HEADER, TRUE, |
| 347 COL_IS_SEPARATOR, TRUE, |
| 348 -1); |
| 349 |
| 350 for (int i = 0; i < table_model_->RowCount(); ++i) |
| 351 AddNodeToList(i); |
| 352 } |
| 353 |
| 354 void KeywordEditorView::OnItemsChanged(int start, int length) { |
| 355 DCHECK(model_second_group_index_ == table_model_->last_search_engine_index()); |
| 356 GtkTreeIter iter; |
| 357 for (int i = 0; i < length; ++i) { |
| 358 int row = GetListStoreRowForModelRow(start + i); |
| 359 bool rv = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), |
| 360 &iter, NULL, row); |
| 361 if (!rv) { |
| 362 NOTREACHED(); |
| 363 return; |
| 364 } |
| 365 SetColumnValues(start + i, &iter); |
| 366 rv = gtk_tree_model_iter_next(GTK_TREE_MODEL(list_store_), &iter); |
| 367 } |
| 368 } |
| 369 |
| 370 void KeywordEditorView::OnItemsAdded(int start, int length) { |
| 371 model_second_group_index_ = table_model_->last_search_engine_index(); |
| 372 for (int i = 0; i < length; ++i) { |
| 373 AddNodeToList(start + i); |
| 374 } |
| 375 } |
| 376 |
| 377 void KeywordEditorView::OnItemsRemoved(int start, int length) { |
| 378 // This is quite likely not correct with removing multiple in one call, but |
| 379 // that shouldn't happen since we only can select and modify/remove one at a |
| 380 // time. |
| 381 DCHECK(length == 1); |
| 382 for (int i = 0; i < length; ++i) { |
| 383 int row = GetListStoreRowForModelRow(start + i); |
| 384 GtkTreeIter iter; |
| 385 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &iter, |
| 386 NULL, row)) { |
| 387 NOTREACHED(); |
| 388 return; |
| 389 } |
| 390 gtk_list_store_remove(list_store_, &iter); |
| 391 } |
| 392 model_second_group_index_ = table_model_->last_search_engine_index(); |
| 177 } | 393 } |
| 178 | 394 |
| 179 void KeywordEditorView::OnTemplateURLModelChanged() { | 395 void KeywordEditorView::OnTemplateURLModelChanged() { |
| 180 // TODO(mattm): repopulate table | |
| 181 EnableControls(); | 396 EnableControls(); |
| 182 } | 397 } |
| 183 | 398 |
| 184 // static | 399 // static |
| 185 void KeywordEditorView::OnWindowDestroy(GtkWidget* widget, | 400 void KeywordEditorView::OnWindowDestroy(GtkWidget* widget, |
| 186 KeywordEditorView* window) { | 401 KeywordEditorView* window) { |
| 187 instance_ = NULL; | 402 instance_ = NULL; |
| 188 MessageLoop::current()->DeleteSoon(FROM_HERE, window); | 403 MessageLoop::current()->DeleteSoon(FROM_HERE, window); |
| 189 } | 404 } |
| 190 | 405 |
| 191 // static | 406 // static |
| 192 void KeywordEditorView::OnResponse(GtkDialog* dialog, int response_id, | 407 void KeywordEditorView::OnResponse(GtkDialog* dialog, int response_id, |
| 193 KeywordEditorView* window) { | 408 KeywordEditorView* window) { |
| 194 gtk_widget_destroy(window->dialog_); | 409 gtk_widget_destroy(window->dialog_); |
| 195 } | 410 } |
| 196 | 411 |
| 197 // static | 412 // static |
| 413 gboolean KeywordEditorView::OnCheckRowIsSeparator(GtkTreeModel* model, |
| 414 GtkTreeIter* iter, |
| 415 gpointer user_data) { |
| 416 gboolean is_separator; |
| 417 gtk_tree_model_get(model, iter, COL_IS_SEPARATOR, &is_separator, -1); |
| 418 return is_separator; |
| 419 } |
| 420 |
| 421 //static |
| 422 gboolean KeywordEditorView::OnSelectionFilter(GtkTreeSelection *selection, |
| 423 GtkTreeModel *model, |
| 424 GtkTreePath *path, |
| 425 gboolean path_currently_selected, |
| 426 gpointer user_data) { |
| 427 GtkTreeIter iter; |
| 428 if (!gtk_tree_model_get_iter(model, &iter, path)) { |
| 429 NOTREACHED(); |
| 430 return TRUE; |
| 431 } |
| 432 gboolean is_header; |
| 433 gtk_tree_model_get(model, &iter, COL_IS_HEADER, &is_header, -1); |
| 434 return !is_header; |
| 435 } |
| 436 |
| 437 // static |
| 198 void KeywordEditorView::OnSelectionChanged( | 438 void KeywordEditorView::OnSelectionChanged( |
| 199 GtkTreeSelection *selection, KeywordEditorView* editor) { | 439 GtkTreeSelection *selection, KeywordEditorView* editor) { |
| 200 editor->EnableControls(); | 440 editor->EnableControls(); |
| 201 } | 441 } |
| 202 | 442 |
| 203 // static | 443 // static |
| 444 void KeywordEditorView::OnRowActivated(GtkTreeView* tree_view, |
| 445 GtkTreePath* path, |
| 446 GtkTreeViewColumn* column, |
| 447 KeywordEditorView* editor) { |
| 448 OnEditButtonClicked(NULL, editor); |
| 449 } |
| 450 |
| 451 // static |
| 204 void KeywordEditorView::OnAddButtonClicked(GtkButton* button, | 452 void KeywordEditorView::OnAddButtonClicked(GtkButton* button, |
| 205 KeywordEditorView* editor) { | 453 KeywordEditorView* editor) { |
| 206 new EditSearchEngineDialog( | 454 new EditSearchEngineDialog( |
| 207 GTK_WINDOW(gtk_widget_get_toplevel(editor->dialog_)), | 455 GTK_WINDOW(gtk_widget_get_toplevel(editor->dialog_)), |
| 208 NULL, | 456 NULL, |
| 209 editor, | 457 editor, |
| 210 editor->profile_); | 458 editor->profile_); |
| 211 } | 459 } |
| 212 | 460 |
| 213 // static | 461 // static |
| 214 void KeywordEditorView::OnEditButtonClicked(GtkButton* button, | 462 void KeywordEditorView::OnEditButtonClicked(GtkButton* button, |
| 215 KeywordEditorView* editor) { | 463 KeywordEditorView* editor) { |
| 216 // TODO(mattm) | 464 int model_row = editor->GetSelectedModelRow(); |
| 465 if (model_row == -1) { |
| 466 NOTREACHED(); |
| 467 return; |
| 468 } |
| 469 new EditSearchEngineDialog( |
| 470 GTK_WINDOW(gtk_widget_get_toplevel(editor->dialog_)), |
| 471 editor->controller_->GetTemplateURL(model_row), |
| 472 editor, |
| 473 editor->profile_); |
| 217 } | 474 } |
| 218 | 475 |
| 219 // static | 476 // static |
| 220 void KeywordEditorView::OnRemoveButtonClicked(GtkButton* button, | 477 void KeywordEditorView::OnRemoveButtonClicked(GtkButton* button, |
| 221 KeywordEditorView* editor) { | 478 KeywordEditorView* editor) { |
| 222 // TODO(mattm) | 479 int model_row = editor->GetSelectedModelRow(); |
| 480 if (model_row == -1) { |
| 481 NOTREACHED(); |
| 482 return; |
| 483 } |
| 484 editor->controller_->RemoveTemplateURL(model_row); |
| 485 if (model_row >= editor->table_model_->RowCount()) |
| 486 model_row = editor->table_model_->RowCount() - 1; |
| 487 if (model_row >= 0) |
| 488 editor->SelectModelRow(model_row); |
| 223 } | 489 } |
| 224 | 490 |
| 225 // static | 491 // static |
| 226 void KeywordEditorView::OnMakeDefaultButtonClicked(GtkButton* button, | 492 void KeywordEditorView::OnMakeDefaultButtonClicked(GtkButton* button, |
| 227 KeywordEditorView* editor) { | 493 KeywordEditorView* editor) { |
| 228 // TODO(mattm) | 494 int model_row = editor->GetSelectedModelRow(); |
| 495 if (model_row == -1) { |
| 496 NOTREACHED(); |
| 497 return; |
| 498 } |
| 499 int new_index = editor->controller_->MakeDefaultTemplateURL(model_row); |
| 500 if (new_index > 0) { |
| 501 editor->SelectModelRow(new_index); |
| 502 } |
| 229 } | 503 } |
| OLD | NEW |