Chromium Code Reviews| 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(IDS_EDIT, IDS_EDIT); | |
|
Evan Stade
2010/11/15 18:57:04
shouldn't one of these things be IDC_EDIT
tfarina
2010/11/15 19:50:08
Nope, we don't have IDC_EDIT.
Evan Stade
2010/11/15 20:41:42
nevertheless, overloading the string ids seems lik
tfarina
2010/11/15 21:09:26
Done.
| |
| 50 menu_model_->AddItemWithStringId( | |
| 51 IDS_BOOMARK_EDITOR_NEW_FOLDER_MENU_ITEM, | |
|
Evan Stade
2010/11/15 18:57:04
ditto
tfarina
2010/11/15 19:50:08
Same thing here.
| |
| 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 // Overridden from menus::SimpleMenuModel::Delegate: | |
| 71 virtual bool IsCommandIdEnabled(int command_id) const { | |
| 72 return !(command_id == IDS_EDIT && running_menu_for_root_) && | |
| 73 (editor_ != NULL); | |
| 74 } | |
| 75 | |
| 76 virtual bool IsCommandIdChecked(int command_id) const { | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 virtual bool GetAcceleratorForCommandId(int command_id, | |
| 81 menus::Accelerator* accelerator) { | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 virtual void ExecuteCommand(int command_id) { | |
| 86 if (!editor_) | |
| 87 return; | |
|
Evan Stade
2010/11/15 18:57:04
insert blank line
tfarina
2010/11/15 19:50:08
Done.
| |
| 88 if (command_id == IDS_EDIT) { | |
|
Evan Stade
2010/11/15 20:41:42
prefer switch/case here
tfarina
2010/11/15 21:09:26
Done.
| |
| 89 GtkTreeIter iter; | |
| 90 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, | |
| 91 NULL, | |
| 92 &iter)) { | |
| 93 return; | |
| 94 } | |
|
Evan Stade
2010/11/15 18:57:04
insert blank line
tfarina
2010/11/15 19:50:08
Done.
| |
| 95 GtkTreePath* path = gtk_tree_model_get_path( | |
| 96 GTK_TREE_MODEL(editor_->tree_store_), &iter); | |
| 97 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(editor_->tree_view_), path); | |
| 98 | |
| 99 // Make the folder name editable. | |
| 100 gtk_tree_view_set_cursor(GTK_TREE_VIEW(editor_->tree_view_), path, | |
| 101 gtk_tree_view_get_column(GTK_TREE_VIEW(editor_->tree_view_), 0), | |
| 102 TRUE); | |
| 103 | |
| 104 gtk_tree_path_free(path); | |
| 105 } else { | |
| 106 DCHECK(command_id == IDS_BOOMARK_EDITOR_NEW_FOLDER_MENU_ITEM); | |
| 107 editor_->NewFolder(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 int64 GetRowIdAt(GtkTreeModel* model, GtkTreeIter* iter) { | |
| 112 GValue value = { 0, }; | |
| 113 gtk_tree_model_get_value(model, iter, bookmark_utils::ITEM_ID, &value); | |
| 114 int64 id = g_value_get_int64(&value); | |
| 115 g_value_unset(&value); | |
| 116 return id; | |
| 117 } | |
| 118 | |
| 119 const BookmarkNode* GetNodeAt(GtkTreeModel* model, GtkTreeIter* iter) { | |
| 120 int64 id = GetRowIdAt(model, iter); | |
| 121 return (id > 0) ? editor_->bb_model_->GetNodeByID(id) : NULL; | |
| 122 } | |
| 123 | |
| 124 const BookmarkNode* GetSelectedNode() { | |
| 125 GtkTreeModel* model; | |
| 126 GtkTreeIter iter; | |
| 127 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, | |
| 128 &model, | |
| 129 &iter)) { | |
| 130 return NULL; | |
| 131 } | |
|
Evan Stade
2010/11/15 18:57:04
insert blank line
tfarina
2010/11/15 19:50:08
Done.
| |
| 132 return GetNodeAt(model, &iter); | |
| 133 } | |
| 134 | |
| 135 // The model and view for the right click context menu. | |
| 136 scoped_ptr<menus::SimpleMenuModel> menu_model_; | |
| 137 scoped_ptr<MenuGtk> menu_; | |
| 138 | |
| 139 // The context menu was brought up for. Set to NULL when the menu is canceled. | |
| 140 BookmarkEditorGtk* editor_; | |
| 141 | |
| 142 // If true, we're running the menu for the bookmark bar or other bookmarks | |
| 143 // nodes. | |
| 144 bool running_menu_for_root_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(ContextMenuController); | |
| 147 }; | |
| 148 | |
| 40 // static | 149 // static |
| 41 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, | 150 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, |
| 42 Profile* profile, | 151 Profile* profile, |
| 43 const BookmarkNode* parent, | 152 const BookmarkNode* parent, |
| 44 const EditDetails& details, | 153 const EditDetails& details, |
| 45 Configuration configuration) { | 154 Configuration configuration) { |
| 46 DCHECK(profile); | 155 DCHECK(profile); |
| 47 BookmarkEditorGtk* editor = | 156 BookmarkEditorGtk* editor = |
| 48 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, | 157 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, |
| 49 configuration); | 158 configuration); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 if (details_.type == EditDetails::EXISTING_NODE) | 283 if (details_.type == EditDetails::EXISTING_NODE) |
| 175 selected_id = details_.existing_node->GetParent()->id(); | 284 selected_id = details_.existing_node->GetParent()->id(); |
| 176 else if (parent_) | 285 else if (parent_) |
| 177 selected_id = parent_->id(); | 286 selected_id = parent_->id(); |
| 178 tree_store_ = bookmark_utils::MakeFolderTreeStore(); | 287 tree_store_ = bookmark_utils::MakeFolderTreeStore(); |
| 179 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, | 288 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, |
| 180 &selected_iter); | 289 &selected_iter); |
| 181 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); | 290 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); |
| 182 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); | 291 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); |
| 183 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); | 292 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); |
| 293 g_signal_connect(tree_view_, "button-press-event", | |
| 294 G_CALLBACK(OnTreeViewButtonPressEventThunk), this); | |
| 184 | 295 |
| 185 GtkTreePath* path = NULL; | 296 GtkTreePath* path = NULL; |
| 186 if (selected_id) { | 297 if (selected_id) { |
| 187 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), | 298 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), |
| 188 &selected_iter); | 299 &selected_iter); |
| 189 } else { | 300 } else { |
| 190 // We don't have a selected parent (Probably because we're making a new | 301 // We don't have a selected parent (Probably because we're making a new |
| 191 // bookmark). Select the first item in the list. | 302 // bookmark). Select the first item in the list. |
| 192 path = gtk_tree_path_new_from_string("0"); | 303 path = gtk_tree_path_new_from_string("0"); |
| 193 } | 304 } |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 can_close = FALSE; | 500 can_close = FALSE; |
| 390 } else { | 501 } else { |
| 391 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); | 502 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); |
| 392 } | 503 } |
| 393 } | 504 } |
| 394 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), | 505 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), |
| 395 GTK_RESPONSE_ACCEPT, can_close); | 506 GTK_RESPONSE_ACCEPT, can_close); |
| 396 } | 507 } |
| 397 | 508 |
| 398 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { | 509 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { |
| 510 NewFolder(); | |
| 511 } | |
| 512 | |
| 513 gboolean BookmarkEditorGtk::OnTreeViewButtonPressEvent(GtkWidget* widget, | |
| 514 GdkEventButton* event) { | |
| 515 if (event->button == 3) | |
| 516 ShowContextMenu(); | |
| 517 | |
| 518 return FALSE; | |
| 519 } | |
| 520 | |
| 521 void BookmarkEditorGtk::ShowContextMenu() { | |
| 522 if (!menu_controller_.get()) | |
| 523 menu_controller_.reset(new ContextMenuController(this)); | |
| 524 | |
| 525 menu_controller_->RunMenu(); | |
| 526 } | |
| 527 | |
| 528 void BookmarkEditorGtk::NewFolder() { | |
| 399 GtkTreeIter iter; | 529 GtkTreeIter iter; |
| 400 if (!gtk_tree_selection_get_selected(tree_selection_, | 530 if (!gtk_tree_selection_get_selected(tree_selection_, |
| 401 NULL, | 531 NULL, |
| 402 &iter)) { | 532 &iter)) { |
| 403 NOTREACHED() << "Something should always be selected if New Folder " << | 533 NOTREACHED() << "Something should always be selected if New Folder " << |
| 404 "is clicked"; | 534 "is clicked"; |
| 405 return; | 535 return; |
| 406 } | 536 } |
| 407 | 537 |
| 408 GtkTreeIter new_item_iter; | 538 GtkTreeIter new_item_iter; |
| 409 AddNewGroup(&iter, &new_item_iter); | 539 AddNewGroup(&iter, &new_item_iter); |
| 410 | 540 |
| 411 GtkTreePath* path = gtk_tree_model_get_path( | 541 GtkTreePath* path = gtk_tree_model_get_path( |
| 412 GTK_TREE_MODEL(tree_store_), &new_item_iter); | 542 GTK_TREE_MODEL(tree_store_), &new_item_iter); |
| 413 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); | 543 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); |
| 414 | 544 |
| 415 // Make the folder name editable. | 545 // Make the folder name editable. |
| 416 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, | 546 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, |
| 417 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), | 547 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), |
| 418 TRUE); | 548 TRUE); |
| 419 | 549 |
| 420 gtk_tree_path_free(path); | 550 gtk_tree_path_free(path); |
| 421 } | 551 } |
| OLD | NEW |