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/bookmark_editor_gtk.h" | 5 #include "chrome/browser/gtk/bookmark_editor_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "app/menus/simple_menu_model.h" |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
14 #include "chrome/browser/bookmarks/bookmark_model.h" | 15 #include "chrome/browser/bookmarks/bookmark_model.h" |
15 #include "chrome/browser/bookmarks/bookmark_utils.h" | 16 #include "chrome/browser/bookmarks/bookmark_utils.h" |
16 #include "chrome/browser/gtk/bookmark_tree_model.h" | 17 #include "chrome/browser/gtk/bookmark_tree_model.h" |
17 #include "chrome/browser/gtk/bookmark_utils_gtk.h" | 18 #include "chrome/browser/gtk/bookmark_utils_gtk.h" |
18 #include "chrome/browser/gtk/gtk_theme_provider.h" | 19 #include "chrome/browser/gtk/gtk_theme_provider.h" |
19 #include "chrome/browser/gtk/gtk_util.h" | 20 #include "chrome/browser/gtk/gtk_util.h" |
| 21 #include "chrome/browser/gtk/menu_gtk.h" |
20 #include "chrome/browser/history/history.h" | 22 #include "chrome/browser/history/history.h" |
21 #include "chrome/browser/net/url_fixer_upper.h" | 23 #include "chrome/browser/net/url_fixer_upper.h" |
22 #include "chrome/browser/profile.h" | 24 #include "chrome/browser/profile.h" |
23 #include "gfx/gtk_util.h" | 25 #include "gfx/gtk_util.h" |
24 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
25 #include "grit/chromium_strings.h" | 27 #include "grit/chromium_strings.h" |
26 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
27 #include "grit/locale_settings.h" | 29 #include "grit/locale_settings.h" |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 // Background color of text field when URL is invalid. | 33 // Background color of text field when URL is invalid. |
32 const GdkColor kErrorColor = GDK_COLOR_RGB(0xFF, 0xBC, 0xBC); | 34 const GdkColor kErrorColor = GDK_COLOR_RGB(0xFF, 0xBC, 0xBC); |
33 | 35 |
34 // Preferred initial dimensions, in pixels, of the folder tree. | 36 // Preferred initial dimensions, in pixels, of the folder tree. |
35 static const int kTreeWidth = 300; | 37 static const int kTreeWidth = 300; |
36 static const int kTreeHeight = 150; | 38 static const int kTreeHeight = 150; |
37 | 39 |
38 } // namespace | 40 } // namespace |
39 | 41 |
| 42 class BookmarkEditorGtk::ContextMenuController |
| 43 : public menus::SimpleMenuModel::Delegate { |
| 44 public: |
| 45 explicit ContextMenuController(BookmarkEditorGtk* editor) |
| 46 : editor_(editor), |
| 47 running_menu_for_root_(false) { |
| 48 menu_model_.reset(new menus::SimpleMenuModel(this)); |
| 49 menu_model_->AddItemWithStringId(COMMAND_EDIT, IDS_EDIT); |
| 50 menu_model_->AddItemWithStringId( |
| 51 COMMAND_NEW_FOLDER, |
| 52 IDS_BOOMARK_EDITOR_NEW_FOLDER_MENU_ITEM); |
| 53 menu_.reset(new MenuGtk(NULL, menu_model_.get())); |
| 54 } |
| 55 virtual ~ContextMenuController() {} |
| 56 |
| 57 void RunMenu() { |
| 58 const BookmarkNode* selected_node = GetSelectedNode(); |
| 59 if (selected_node) |
| 60 running_menu_for_root_ = selected_node->GetParent()->IsRoot(); |
| 61 menu_->PopupAsContext(gtk_get_current_event_time()); |
| 62 } |
| 63 |
| 64 void Cancel() { |
| 65 editor_ = NULL; |
| 66 menu_->Cancel(); |
| 67 } |
| 68 |
| 69 private: |
| 70 enum ContextMenuCommand { |
| 71 COMMAND_EDIT, |
| 72 COMMAND_NEW_FOLDER |
| 73 }; |
| 74 |
| 75 // Overridden from menus::SimpleMenuModel::Delegate: |
| 76 virtual bool IsCommandIdEnabled(int command_id) const { |
| 77 return !(command_id == COMMAND_EDIT && running_menu_for_root_) && |
| 78 (editor_ != NULL); |
| 79 } |
| 80 |
| 81 virtual bool IsCommandIdChecked(int command_id) const { |
| 82 return false; |
| 83 } |
| 84 |
| 85 virtual bool GetAcceleratorForCommandId(int command_id, |
| 86 menus::Accelerator* accelerator) { |
| 87 return false; |
| 88 } |
| 89 |
| 90 virtual void ExecuteCommand(int command_id) { |
| 91 if (!editor_) |
| 92 return; |
| 93 |
| 94 switch (command_id) { |
| 95 case COMMAND_EDIT: { |
| 96 GtkTreeIter iter; |
| 97 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, |
| 98 NULL, |
| 99 &iter)) { |
| 100 return; |
| 101 } |
| 102 |
| 103 GtkTreePath* path = gtk_tree_model_get_path( |
| 104 GTK_TREE_MODEL(editor_->tree_store_), &iter); |
| 105 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(editor_->tree_view_), path); |
| 106 |
| 107 // Make the folder name editable. |
| 108 gtk_tree_view_set_cursor(GTK_TREE_VIEW(editor_->tree_view_), path, |
| 109 gtk_tree_view_get_column(GTK_TREE_VIEW(editor_->tree_view_), 0), |
| 110 TRUE); |
| 111 |
| 112 gtk_tree_path_free(path); |
| 113 break; |
| 114 } |
| 115 case COMMAND_NEW_FOLDER: |
| 116 editor_->NewFolder(); |
| 117 break; |
| 118 default: |
| 119 NOTREACHED(); |
| 120 break; |
| 121 } |
| 122 } |
| 123 |
| 124 int64 GetRowIdAt(GtkTreeModel* model, GtkTreeIter* iter) { |
| 125 GValue value = { 0, }; |
| 126 gtk_tree_model_get_value(model, iter, bookmark_utils::ITEM_ID, &value); |
| 127 int64 id = g_value_get_int64(&value); |
| 128 g_value_unset(&value); |
| 129 return id; |
| 130 } |
| 131 |
| 132 const BookmarkNode* GetNodeAt(GtkTreeModel* model, GtkTreeIter* iter) { |
| 133 int64 id = GetRowIdAt(model, iter); |
| 134 return (id > 0) ? editor_->bb_model_->GetNodeByID(id) : NULL; |
| 135 } |
| 136 |
| 137 const BookmarkNode* GetSelectedNode() { |
| 138 GtkTreeModel* model; |
| 139 GtkTreeIter iter; |
| 140 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, |
| 141 &model, |
| 142 &iter)) { |
| 143 return NULL; |
| 144 } |
| 145 |
| 146 return GetNodeAt(model, &iter); |
| 147 } |
| 148 |
| 149 // The model and view for the right click context menu. |
| 150 scoped_ptr<menus::SimpleMenuModel> menu_model_; |
| 151 scoped_ptr<MenuGtk> menu_; |
| 152 |
| 153 // The context menu was brought up for. Set to NULL when the menu is canceled. |
| 154 BookmarkEditorGtk* editor_; |
| 155 |
| 156 // If true, we're running the menu for the bookmark bar or other bookmarks |
| 157 // nodes. |
| 158 bool running_menu_for_root_; |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(ContextMenuController); |
| 161 }; |
| 162 |
40 // static | 163 // static |
41 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, | 164 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, |
42 Profile* profile, | 165 Profile* profile, |
43 const BookmarkNode* parent, | 166 const BookmarkNode* parent, |
44 const EditDetails& details, | 167 const EditDetails& details, |
45 Configuration configuration) { | 168 Configuration configuration) { |
46 DCHECK(profile); | 169 DCHECK(profile); |
47 BookmarkEditorGtk* editor = | 170 BookmarkEditorGtk* editor = |
48 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, | 171 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, |
49 configuration); | 172 configuration); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 if (details_.type == EditDetails::EXISTING_NODE) | 297 if (details_.type == EditDetails::EXISTING_NODE) |
175 selected_id = details_.existing_node->GetParent()->id(); | 298 selected_id = details_.existing_node->GetParent()->id(); |
176 else if (parent_) | 299 else if (parent_) |
177 selected_id = parent_->id(); | 300 selected_id = parent_->id(); |
178 tree_store_ = bookmark_utils::MakeFolderTreeStore(); | 301 tree_store_ = bookmark_utils::MakeFolderTreeStore(); |
179 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, | 302 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, |
180 &selected_iter); | 303 &selected_iter); |
181 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); | 304 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); |
182 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); | 305 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); |
183 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); | 306 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); |
| 307 g_signal_connect(tree_view_, "button-press-event", |
| 308 G_CALLBACK(OnTreeViewButtonPressEventThunk), this); |
184 | 309 |
185 GtkTreePath* path = NULL; | 310 GtkTreePath* path = NULL; |
186 if (selected_id) { | 311 if (selected_id) { |
187 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), | 312 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), |
188 &selected_iter); | 313 &selected_iter); |
189 } else { | 314 } else { |
190 // We don't have a selected parent (Probably because we're making a new | 315 // We don't have a selected parent (Probably because we're making a new |
191 // bookmark). Select the first item in the list. | 316 // bookmark). Select the first item in the list. |
192 path = gtk_tree_path_new_from_string("0"); | 317 path = gtk_tree_path_new_from_string("0"); |
193 } | 318 } |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 can_close = FALSE; | 514 can_close = FALSE; |
390 } else { | 515 } else { |
391 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); | 516 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); |
392 } | 517 } |
393 } | 518 } |
394 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), | 519 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), |
395 GTK_RESPONSE_ACCEPT, can_close); | 520 GTK_RESPONSE_ACCEPT, can_close); |
396 } | 521 } |
397 | 522 |
398 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { | 523 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { |
| 524 NewFolder(); |
| 525 } |
| 526 |
| 527 gboolean BookmarkEditorGtk::OnTreeViewButtonPressEvent(GtkWidget* widget, |
| 528 GdkEventButton* event) { |
| 529 if (event->button == 3) |
| 530 ShowContextMenu(); |
| 531 |
| 532 return FALSE; |
| 533 } |
| 534 |
| 535 void BookmarkEditorGtk::ShowContextMenu() { |
| 536 if (!menu_controller_.get()) |
| 537 menu_controller_.reset(new ContextMenuController(this)); |
| 538 |
| 539 menu_controller_->RunMenu(); |
| 540 } |
| 541 |
| 542 void BookmarkEditorGtk::NewFolder() { |
399 GtkTreeIter iter; | 543 GtkTreeIter iter; |
400 if (!gtk_tree_selection_get_selected(tree_selection_, | 544 if (!gtk_tree_selection_get_selected(tree_selection_, |
401 NULL, | 545 NULL, |
402 &iter)) { | 546 &iter)) { |
403 NOTREACHED() << "Something should always be selected if New Folder " << | 547 NOTREACHED() << "Something should always be selected if New Folder " << |
404 "is clicked"; | 548 "is clicked"; |
405 return; | 549 return; |
406 } | 550 } |
407 | 551 |
408 GtkTreeIter new_item_iter; | 552 GtkTreeIter new_item_iter; |
409 AddNewGroup(&iter, &new_item_iter); | 553 AddNewGroup(&iter, &new_item_iter); |
410 | 554 |
411 GtkTreePath* path = gtk_tree_model_get_path( | 555 GtkTreePath* path = gtk_tree_model_get_path( |
412 GTK_TREE_MODEL(tree_store_), &new_item_iter); | 556 GTK_TREE_MODEL(tree_store_), &new_item_iter); |
413 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); | 557 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); |
414 | 558 |
415 // Make the folder name editable. | 559 // Make the folder name editable. |
416 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, | 560 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, |
417 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), | 561 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), |
418 TRUE); | 562 TRUE); |
419 | 563 |
420 gtk_tree_path_free(path); | 564 gtk_tree_path_free(path); |
421 } | 565 } |
OLD | NEW |