| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/gtk/keyword_editor_view.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "chrome/browser/gtk/edit_keyword_controller.h" |
| 9 #include "chrome/browser/profile.h" |
| 10 #include "chrome/browser/metrics/user_metrics.h" |
| 11 #include "chrome/browser/search_engines/template_url.h" |
| 12 #include "chrome/browser/search_engines/template_url_model.h" |
| 13 #include "chrome/common/gtk_util.h" |
| 14 #include "grit/generated_resources.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Initial size for dialog. |
| 19 const int kDialogDefaultWidth = 450; |
| 20 const int kDialogDefaultHeight = 450; |
| 21 |
| 22 // Column ids for |list_store_|. |
| 23 enum { |
| 24 COL_FAVICON, |
| 25 COL_TITLE, |
| 26 COL_KEYWORD, |
| 27 COL_COUNT, |
| 28 }; |
| 29 |
| 30 KeywordEditorView* instance_ = NULL; |
| 31 |
| 32 } |
| 33 |
| 34 // static |
| 35 void KeywordEditorView::Show(Profile* profile) { |
| 36 DCHECK(profile); |
| 37 if (!profile->GetTemplateURLModel()) |
| 38 return; |
| 39 |
| 40 // If there's already an existing editor window, activate it. |
| 41 if (instance_) { |
| 42 gtk_window_present(GTK_WINDOW(instance_->dialog_)); |
| 43 } else { |
| 44 instance_ = new KeywordEditorView(profile); |
| 45 } |
| 46 } |
| 47 |
| 48 void KeywordEditorView::OnEditedKeyword(const TemplateURL* template_url, |
| 49 const std::wstring& title, |
| 50 const std::wstring& keyword, |
| 51 const std::wstring& url) { |
| 52 NOTIMPLEMENTED(); |
| 53 } |
| 54 |
| 55 KeywordEditorView::~KeywordEditorView() { |
| 56 url_model_->RemoveObserver(this); |
| 57 } |
| 58 |
| 59 KeywordEditorView::KeywordEditorView(Profile* profile) |
| 60 : profile_(profile), |
| 61 url_model_(profile->GetTemplateURLModel()) { |
| 62 Init(); |
| 63 } |
| 64 |
| 65 void KeywordEditorView::Init() { |
| 66 DCHECK(url_model_); |
| 67 url_model_->Load(); |
| 68 url_model_->AddObserver(this); |
| 69 |
| 70 dialog_ = gtk_dialog_new_with_buttons( |
| 71 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE).c_str(), |
| 72 NULL, |
| 73 // Non-modal. |
| 74 GTK_DIALOG_NO_SEPARATOR, |
| 75 GTK_STOCK_CLOSE, |
| 76 GTK_RESPONSE_CLOSE, |
| 77 NULL); |
| 78 |
| 79 gtk_window_set_default_size(GTK_WINDOW(dialog_), kDialogDefaultWidth, |
| 80 kDialogDefaultHeight); |
| 81 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), |
| 82 gtk_util::kContentAreaSpacing); |
| 83 |
| 84 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing); |
| 85 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), hbox); |
| 86 |
| 87 GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL); |
| 88 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), |
| 89 GTK_POLICY_AUTOMATIC, |
| 90 GTK_POLICY_AUTOMATIC); |
| 91 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window), |
| 92 GTK_SHADOW_ETCHED_IN); |
| 93 gtk_box_pack_start(GTK_BOX(hbox), scroll_window, TRUE, TRUE, 0); |
| 94 |
| 95 list_store_ = gtk_list_store_new(COL_COUNT, |
| 96 GDK_TYPE_PIXBUF, |
| 97 G_TYPE_STRING, |
| 98 G_TYPE_STRING); |
| 99 tree_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store_)); |
| 100 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_), TRUE); |
| 101 gtk_container_add(GTK_CONTAINER(scroll_window), tree_); |
| 102 |
| 103 GtkTreeViewColumn* title_column = gtk_tree_view_column_new(); |
| 104 GtkCellRenderer* pixbuf_renderer = gtk_cell_renderer_pixbuf_new(); |
| 105 gtk_tree_view_column_pack_start(title_column, pixbuf_renderer, FALSE); |
| 106 gtk_tree_view_column_add_attribute(title_column, pixbuf_renderer, "pixbuf", |
| 107 COL_FAVICON); |
| 108 GtkCellRenderer* title_renderer = gtk_cell_renderer_text_new(); |
| 109 gtk_tree_view_column_pack_start(title_column, title_renderer, TRUE); |
| 110 gtk_tree_view_column_add_attribute(title_column, title_renderer, "text", |
| 111 COL_TITLE); |
| 112 gtk_tree_view_column_set_title( |
| 113 title_column, l10n_util::GetStringUTF8( |
| 114 IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN).c_str()); |
| 115 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), title_column); |
| 116 |
| 117 GtkTreeViewColumn* keyword_column = gtk_tree_view_column_new_with_attributes( |
| 118 l10n_util::GetStringUTF8( |
| 119 IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN).c_str(), |
| 120 gtk_cell_renderer_text_new(), |
| 121 "text", COL_KEYWORD, |
| 122 NULL); |
| 123 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_), keyword_column); |
| 124 |
| 125 selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_)); |
| 126 gtk_tree_selection_set_mode(selection_, GTK_SELECTION_SINGLE); |
| 127 g_signal_connect(G_OBJECT(selection_), "changed", |
| 128 G_CALLBACK(OnSelectionChanged), this); |
| 129 |
| 130 GtkWidget* button_box = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); |
| 131 gtk_box_pack_start(GTK_BOX(hbox), button_box, FALSE, FALSE, 0); |
| 132 |
| 133 add_button_ = gtk_button_new_with_label( |
| 134 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_NEW_BUTTON).c_str()); |
| 135 g_signal_connect(G_OBJECT(add_button_), "clicked", |
| 136 G_CALLBACK(OnAddButtonClicked), this); |
| 137 gtk_box_pack_start(GTK_BOX(button_box), add_button_, FALSE, FALSE, 0); |
| 138 |
| 139 edit_button_ = gtk_button_new_with_label( |
| 140 l10n_util::GetStringUTF8(IDS_SEARCH_ENGINES_EDITOR_EDIT_BUTTON).c_str()); |
| 141 g_signal_connect(G_OBJECT(edit_button_), "clicked", |
| 142 G_CALLBACK(OnEditButtonClicked), this); |
| 143 gtk_box_pack_start(GTK_BOX(button_box), edit_button_, FALSE, FALSE, 0); |
| 144 |
| 145 remove_button_ = gtk_button_new_with_label( |
| 146 l10n_util::GetStringUTF8( |
| 147 IDS_SEARCH_ENGINES_EDITOR_REMOVE_BUTTON).c_str()); |
| 148 g_signal_connect(G_OBJECT(remove_button_), "clicked", |
| 149 G_CALLBACK(OnRemoveButtonClicked), this); |
| 150 gtk_box_pack_start(GTK_BOX(button_box), remove_button_, FALSE, FALSE, 0); |
| 151 |
| 152 make_default_button_ = gtk_button_new_with_label( |
| 153 l10n_util::GetStringUTF8( |
| 154 IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON).c_str()); |
| 155 g_signal_connect(G_OBJECT(make_default_button_), "clicked", |
| 156 G_CALLBACK(OnMakeDefaultButtonClicked), this); |
| 157 gtk_box_pack_start(GTK_BOX(button_box), make_default_button_, FALSE, FALSE, |
| 158 0); |
| 159 |
| 160 EnableControls(); |
| 161 |
| 162 gtk_widget_show_all(dialog_); |
| 163 |
| 164 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponse), this); |
| 165 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroy), this); |
| 166 } |
| 167 |
| 168 void KeywordEditorView::EnableControls() { |
| 169 bool enable = gtk_tree_selection_count_selected_rows(selection_) == 1; |
| 170 bool can_make_default = false; |
| 171 bool can_remove = false; |
| 172 // TODO(mattm) |
| 173 gtk_widget_set_sensitive(add_button_, url_model_->loaded()); |
| 174 gtk_widget_set_sensitive(edit_button_, enable); |
| 175 gtk_widget_set_sensitive(remove_button_, can_make_default); |
| 176 gtk_widget_set_sensitive(make_default_button_, can_remove); |
| 177 } |
| 178 |
| 179 void KeywordEditorView::OnTemplateURLModelChanged() { |
| 180 // TODO(mattm): repopulate table |
| 181 EnableControls(); |
| 182 } |
| 183 |
| 184 // static |
| 185 void KeywordEditorView::OnWindowDestroy(GtkWidget* widget, |
| 186 KeywordEditorView* window) { |
| 187 instance_ = NULL; |
| 188 MessageLoop::current()->DeleteSoon(FROM_HERE, window); |
| 189 } |
| 190 |
| 191 // static |
| 192 void KeywordEditorView::OnResponse(GtkDialog* dialog, int response_id, |
| 193 KeywordEditorView* window) { |
| 194 gtk_widget_destroy(window->dialog_); |
| 195 } |
| 196 |
| 197 // static |
| 198 void KeywordEditorView::OnSelectionChanged( |
| 199 GtkTreeSelection *selection, KeywordEditorView* editor) { |
| 200 editor->EnableControls(); |
| 201 } |
| 202 |
| 203 // static |
| 204 void KeywordEditorView::OnAddButtonClicked(GtkButton* button, |
| 205 KeywordEditorView* editor) { |
| 206 EditKeywordControllerBase::Create( |
| 207 GTK_WINDOW(gtk_widget_get_toplevel(editor->dialog_)), |
| 208 NULL, |
| 209 editor, |
| 210 editor->profile_); |
| 211 } |
| 212 |
| 213 // static |
| 214 void KeywordEditorView::OnEditButtonClicked(GtkButton* button, |
| 215 KeywordEditorView* editor) { |
| 216 // TODO(mattm) |
| 217 } |
| 218 |
| 219 // static |
| 220 void KeywordEditorView::OnRemoveButtonClicked(GtkButton* button, |
| 221 KeywordEditorView* editor) { |
| 222 // TODO(mattm) |
| 223 } |
| 224 |
| 225 // static |
| 226 void KeywordEditorView::OnMakeDefaultButtonClicked(GtkButton* button, |
| 227 KeywordEditorView* editor) { |
| 228 // TODO(mattm) |
| 229 } |
| OLD | NEW |