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); | |
| 50 menu_model_->AddItemWithStringId( | |
| 51 IDS_BOOMARK_EDITOR_NEW_FOLDER_MENU_ITEM, | |
| 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 running_menu_for_root_ = GetSelectedNode()->GetParent()->IsRoot(); | |
|
Evan Stade
2010/11/15 04:01:13
GetSelectedNode() can return null. this segfaults
tfarina
2010/11/15 12:43:00
Done.
| |
| 59 menu_->PopupAsContext(gtk_get_current_event_time()); | |
| 60 } | |
| 61 void Cancel() { | |
| 62 editor_ = NULL; | |
| 63 menu_->Cancel(); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 // Overridden from menus::SimpleMenuModel::Delegate: | |
| 68 virtual bool IsCommandIdEnabled(int command_id) const { | |
| 69 return (command_id != IDS_EDIT || !running_menu_for_root_) && | |
|
Evan Stade
2010/11/15 04:01:13
clearer if you change this from !a || !b to !(a an
tfarina
2010/11/15 12:43:00
Done.
| |
| 70 (editor_ != NULL); | |
| 71 } | |
| 72 virtual bool IsCommandIdChecked(int command_id) const { | |
| 73 return false; | |
| 74 } | |
|
Evan Stade
2010/11/15 04:01:13
blank lines between functions
tfarina
2010/11/15 12:43:00
Done.
| |
| 75 virtual bool GetAcceleratorForCommandId(int command_id, | |
| 76 menus::Accelerator* accelerator) { | |
| 77 return false; | |
| 78 } | |
| 79 virtual void ExecuteCommand(int command_id) { | |
| 80 if (!editor_) | |
| 81 return; | |
| 82 if (command_id == IDS_EDIT) { | |
| 83 GtkTreeIter iter; | |
| 84 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, | |
| 85 NULL, | |
| 86 &iter)) { | |
| 87 return; | |
| 88 } | |
| 89 GtkTreePath* path = gtk_tree_model_get_path( | |
| 90 GTK_TREE_MODEL(editor_->tree_store_), &iter); | |
| 91 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(editor_->tree_view_), path); | |
| 92 | |
| 93 // Make the folder name editable. | |
| 94 gtk_tree_view_set_cursor(GTK_TREE_VIEW(editor_->tree_view_), path, | |
| 95 gtk_tree_view_get_column(GTK_TREE_VIEW(editor_->tree_view_), 0), | |
| 96 TRUE); | |
| 97 | |
| 98 gtk_tree_path_free(path); | |
| 99 } else { | |
| 100 DCHECK(command_id == IDS_BOOMARK_EDITOR_NEW_FOLDER_MENU_ITEM); | |
| 101 editor_->NewFolder(); | |
| 102 } | |
| 103 } | |
| 104 int64 GetRowIdAt(GtkTreeModel* model, GtkTreeIter* iter) { | |
| 105 GValue value = { 0, }; | |
| 106 gtk_tree_model_get_value(model, iter, bookmark_utils::ITEM_ID, &value); | |
| 107 int64 id = g_value_get_int64(&value); | |
| 108 g_value_unset(&value); | |
| 109 return id; | |
| 110 } | |
| 111 const BookmarkNode* GetNodeAt(GtkTreeModel* model, GtkTreeIter* iter) { | |
| 112 int64 id = GetRowIdAt(model, iter); | |
| 113 return (id > 0) ? editor_->bb_model_->GetNodeByID(id) : NULL; | |
| 114 } | |
| 115 const BookmarkNode* GetSelectedNode() { | |
| 116 GtkTreeModel* model; | |
| 117 GtkTreeIter iter; | |
| 118 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, | |
| 119 &model, | |
| 120 &iter)) { | |
| 121 return NULL; | |
| 122 } | |
| 123 return GetNodeAt(model, &iter); | |
| 124 } | |
| 125 | |
| 126 // The model and view for the right click context menu. | |
| 127 scoped_ptr<menus::SimpleMenuModel> menu_model_; | |
| 128 scoped_ptr<MenuGtk> menu_; | |
| 129 | |
| 130 // The context menu was brought up for. Set to NULL when the menu is canceled. | |
| 131 BookmarkEditorGtk* editor_; | |
| 132 | |
| 133 // If true, we're running the menu for the bookmark bar or other bookmarks | |
| 134 // nodes. | |
| 135 bool running_menu_for_root_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(ContextMenuController); | |
| 138 }; | |
| 139 | |
| 40 // static | 140 // static |
| 41 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, | 141 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, |
| 42 Profile* profile, | 142 Profile* profile, |
| 43 const BookmarkNode* parent, | 143 const BookmarkNode* parent, |
| 44 const EditDetails& details, | 144 const EditDetails& details, |
| 45 Configuration configuration) { | 145 Configuration configuration) { |
| 46 DCHECK(profile); | 146 DCHECK(profile); |
| 47 BookmarkEditorGtk* editor = | 147 BookmarkEditorGtk* editor = |
| 48 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, | 148 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, |
| 49 configuration); | 149 configuration); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 if (details_.type == EditDetails::EXISTING_NODE) | 274 if (details_.type == EditDetails::EXISTING_NODE) |
| 175 selected_id = details_.existing_node->GetParent()->id(); | 275 selected_id = details_.existing_node->GetParent()->id(); |
| 176 else if (parent_) | 276 else if (parent_) |
| 177 selected_id = parent_->id(); | 277 selected_id = parent_->id(); |
| 178 tree_store_ = bookmark_utils::MakeFolderTreeStore(); | 278 tree_store_ = bookmark_utils::MakeFolderTreeStore(); |
| 179 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, | 279 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, |
| 180 &selected_iter); | 280 &selected_iter); |
| 181 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); | 281 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); |
| 182 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); | 282 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); |
| 183 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); | 283 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); |
| 284 g_signal_connect(tree_view_, "button-press-event", | |
| 285 G_CALLBACK(OnTreeViewButtonPressEventThunk), this); | |
| 184 | 286 |
| 185 GtkTreePath* path = NULL; | 287 GtkTreePath* path = NULL; |
| 186 if (selected_id) { | 288 if (selected_id) { |
| 187 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), | 289 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), |
| 188 &selected_iter); | 290 &selected_iter); |
| 189 } else { | 291 } else { |
| 190 // We don't have a selected parent (Probably because we're making a new | 292 // We don't have a selected parent (Probably because we're making a new |
| 191 // bookmark). Select the first item in the list. | 293 // bookmark). Select the first item in the list. |
| 192 path = gtk_tree_path_new_from_string("0"); | 294 path = gtk_tree_path_new_from_string("0"); |
| 193 } | 295 } |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 can_close = FALSE; | 491 can_close = FALSE; |
| 390 } else { | 492 } else { |
| 391 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); | 493 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); |
| 392 } | 494 } |
| 393 } | 495 } |
| 394 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), | 496 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), |
| 395 GTK_RESPONSE_ACCEPT, can_close); | 497 GTK_RESPONSE_ACCEPT, can_close); |
| 396 } | 498 } |
| 397 | 499 |
| 398 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { | 500 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { |
| 501 NewFolder(); | |
| 502 } | |
| 503 | |
| 504 gboolean BookmarkEditorGtk::OnTreeViewButtonPressEvent(GtkWidget* widget, | |
| 505 GdkEventButton* event) { | |
| 506 if (event->button == 3) | |
| 507 ShowContextMenu(); | |
| 508 | |
| 509 return FALSE; | |
| 510 } | |
| 511 | |
| 512 void BookmarkEditorGtk::ShowContextMenu() { | |
| 513 if (!menu_controller_.get()) | |
| 514 menu_controller_.reset(new ContextMenuController(this)); | |
| 515 | |
| 516 menu_controller_->RunMenu(); | |
| 517 } | |
| 518 | |
| 519 void BookmarkEditorGtk::NewFolder() { | |
| 399 GtkTreeIter iter; | 520 GtkTreeIter iter; |
| 400 if (!gtk_tree_selection_get_selected(tree_selection_, | 521 if (!gtk_tree_selection_get_selected(tree_selection_, |
| 401 NULL, | 522 NULL, |
| 402 &iter)) { | 523 &iter)) { |
| 403 NOTREACHED() << "Something should always be selected if New Folder " << | 524 NOTREACHED() << "Something should always be selected if New Folder " << |
| 404 "is clicked"; | 525 "is clicked"; |
| 405 return; | 526 return; |
| 406 } | 527 } |
| 407 | 528 |
| 408 GtkTreeIter new_item_iter; | 529 GtkTreeIter new_item_iter; |
| 409 AddNewGroup(&iter, &new_item_iter); | 530 AddNewGroup(&iter, &new_item_iter); |
| 410 | 531 |
| 411 GtkTreePath* path = gtk_tree_model_get_path( | 532 GtkTreePath* path = gtk_tree_model_get_path( |
| 412 GTK_TREE_MODEL(tree_store_), &new_item_iter); | 533 GTK_TREE_MODEL(tree_store_), &new_item_iter); |
| 413 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); | 534 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); |
| 414 | 535 |
| 415 // Make the folder name editable. | 536 // Make the folder name editable. |
| 416 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, | 537 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, |
| 417 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), | 538 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), |
| 418 TRUE); | 539 TRUE); |
| 419 | 540 |
| 420 gtk_tree_path_free(path); | 541 gtk_tree_path_free(path); |
| 421 } | 542 } |
| OLD | NEW |