diff options
author | chase@chromium.org <chase@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-04 01:44:30 +0000 |
---|---|---|
committer | chase@chromium.org <chase@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-04 01:44:30 +0000 |
commit | f9e8dbbf401d1ceada479461f6e9fb395eab6990 (patch) | |
tree | aba0cbc73c91c92cc017ec71483d588759f92d89 | |
parent | 506f16d32a23d660e473f052564688f05eabfb67 (diff) | |
download | chromium_src-f9e8dbbf401d1ceada479461f6e9fb395eab6990.zip chromium_src-f9e8dbbf401d1ceada479461f6e9fb395eab6990.tar.gz chromium_src-f9e8dbbf401d1ceada479461f6e9fb395eab6990.tar.bz2 |
Revert 38056 - Unfork BookmarkMenuGtk and BookmarkMenuController.
This is step 2 of 3. bookmark_context_menu_controller_gtk will eventually replace bookmark_context_menu_controller; for now it is separate because getting Windows to use it is another huge hassle.
BUG=32830
TEST=manual + trybots
Review URL: http://codereview.chromium.org/563017
TBR=estade@chromium.org
Review URL: http://codereview.chromium.org/567041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38065 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/gtk/bookmark_bar_gtk.cc | 11 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_bar_gtk.h | 10 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_context_menu_gtk.cc (renamed from chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.cc) | 273 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_context_menu_gtk.h (renamed from chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h) | 117 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_manager_gtk.cc | 15 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_manager_gtk.h | 10 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_menu_controller_gtk.cc | 15 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_menu_controller_gtk.h | 9 | ||||
-rwxr-xr-x | chrome/chrome_browser.gypi | 7 |
9 files changed, 219 insertions, 248 deletions
diff --git a/chrome/browser/gtk/bookmark_bar_gtk.cc b/chrome/browser/gtk/bookmark_bar_gtk.cc index 62bac3d..f5a0b8c 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk.cc +++ b/chrome/browser/gtk/bookmark_bar_gtk.cc @@ -17,6 +17,7 @@ #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_utils.h" #include "chrome/browser/browser.h" +#include "chrome/browser/gtk/bookmark_context_menu_gtk.h" #include "chrome/browser/gtk/bookmark_menu_controller_gtk.h" #include "chrome/browser/gtk/bookmark_tree_model.h" #include "chrome/browser/gtk/bookmark_utils_gtk.h" @@ -922,9 +923,9 @@ void BookmarkBarGtk::PopupMenuForNode(GtkWidget* sender, GtkWindow* window = GTK_WINDOW(gtk_widget_get_toplevel(sender)); current_context_menu_controller_.reset( - new BookmarkContextMenuController( - window, this, profile_, page_navigator_, parent, nodes, - BookmarkContextMenuController::BOOKMARK_BAR)); + new BookmarkContextMenuGtk( + window, profile_, browser_, page_navigator_, parent, nodes, + BookmarkContextMenuGtk::BOOKMARK_BAR, NULL)); current_context_menu_.reset( new MenuGtk(NULL, current_context_menu_controller_->menu_model())); current_context_menu_->PopupAsContext(event->time); @@ -1372,7 +1373,3 @@ void BookmarkBarGtk::PopupForButtonNextTo(GtkWidget* button, button_idx = (button_idx + shift + folder_list.size()) % folder_list.size(); PopupForButton(folder_list[button_idx]); } - -void BookmarkBarGtk::CloseMenu() { - current_context_menu_->Cancel(); -} diff --git a/chrome/browser/gtk/bookmark_bar_gtk.h b/chrome/browser/gtk/bookmark_bar_gtk.h index 0c6813e..2770fc5 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk.h +++ b/chrome/browser/gtk/bookmark_bar_gtk.h @@ -13,7 +13,6 @@ #include "app/slide_animation.h" #include "base/gfx/size.h" #include "base/scoped_ptr.h" -#include "chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h" #include "chrome/browser/bookmarks/bookmark_model_observer.h" #include "chrome/browser/gtk/bookmark_bar_instructions_gtk.h" #include "chrome/browser/gtk/menu_bar_helper.h" @@ -24,6 +23,7 @@ #include "chrome/common/owned_widget_gtk.h" #include "testing/gtest/include/gtest/gtest_prod.h" +class BookmarkContextMenuGtk; class BookmarkMenuController; class Browser; class BrowserWindowGtk; @@ -39,8 +39,7 @@ class BookmarkBarGtk : public AnimationDelegate, public BookmarkModelObserver, public MenuBarHelper::Delegate, public NotificationObserver, - public BookmarkBarInstructionsGtk::Delegate, - public BookmarkContextMenuControllerDelegate { + public BookmarkBarInstructionsGtk::Delegate { FRIEND_TEST(BookmarkBarGtkUnittest, DisplaysHelpMessageOnEmpty); FRIEND_TEST(BookmarkBarGtkUnittest, HidesHelpMessageWithBookmark); FRIEND_TEST(BookmarkBarGtkUnittest, BuildsButtons); @@ -107,9 +106,6 @@ class BookmarkBarGtk : public AnimationDelegate, // The NTP needs to have access to this. static const int kBookmarkBarNTPHeight; - // BookmarkContextMenuController::Delegate implementation -------------------- - virtual void CloseMenu(); - private: // Helper function which generates GtkToolItems for |bookmark_toolbar_|. void CreateAllBookmarkButtons(); @@ -356,7 +352,7 @@ class BookmarkBarGtk : public AnimationDelegate, // The last displayed right click menu, or NULL if no menus have been // displayed yet. // The controller. - scoped_ptr<BookmarkContextMenuController> current_context_menu_controller_; + scoped_ptr<BookmarkContextMenuGtk> current_context_menu_controller_; // The view. scoped_ptr<MenuGtk> current_context_menu_; diff --git a/chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.cc b/chrome/browser/gtk/bookmark_context_menu_gtk.cc index 857630e..4c20ff3 100644 --- a/chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.cc +++ b/chrome/browser/gtk/bookmark_context_menu_gtk.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h" +#include "chrome/browser/gtk/bookmark_context_menu_gtk.h" #include "app/l10n_util.h" #include "base/compiler_specific.h" @@ -10,10 +10,13 @@ #include "chrome/browser/bookmarks/bookmark_manager.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_utils.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_list.h" #include "chrome/browser/input_window_dialog.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/profile.h" #include "chrome/browser/tab_contents/page_navigator.h" +#include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "grit/generated_resources.h" @@ -78,7 +81,7 @@ class EditFolderController : public InputWindowDialog::Delegate, l10n_util::GetString(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL); std::wstring contents = is_new_ ? l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME) : - UTF16ToWide(node_->GetTitleAsString16()); + node_->GetTitle(); dialog_ = InputWindowDialog::Create(wnd, title, label, contents, this); model_->AddObserver(this); @@ -192,112 +195,101 @@ class SelectOnCreationHandler : public BookmarkEditor::Handler { } // namespace -BookmarkContextMenuController::BookmarkContextMenuController( - gfx::NativeWindow parent_window, - BookmarkContextMenuControllerDelegate* delegate, +// BookmarkContextMenuGtk ------------------------------------------- + +BookmarkContextMenuGtk::BookmarkContextMenuGtk( + gfx::NativeWindow wnd, Profile* profile, + Browser* browser, PageNavigator* navigator, const BookmarkNode* parent, const std::vector<const BookmarkNode*>& selection, - ConfigurationType configuration) - : parent_window_(parent_window), - delegate_(delegate), + ConfigurationType configuration, + Delegate* delegate) + : wnd_(wnd), profile_(profile), + browser_(browser), navigator_(navigator), parent_(parent), selection_(selection), + model_(profile->GetBookmarkModel()), configuration_(configuration), - model_(profile->GetBookmarkModel()) { + delegate_(delegate), + model_changed_(false) { DCHECK(profile_); DCHECK(model_->IsLoaded()); menu_model_.reset(new menus::SimpleMenuModel(this)); - model_->AddObserver(this); - - BuildMenu(); -} -BookmarkContextMenuController::~BookmarkContextMenuController() { - if (model_) - model_->RemoveObserver(this); -} - -void BookmarkContextMenuController::BuildMenu() { - if (configuration_ != BOOKMARK_MANAGER_ORGANIZE_MENU) { - if (selection_.size() == 1 && selection_[0]->is_url()) { - AddItem(IDS_BOOMARK_BAR_OPEN_ALL, - IDS_BOOMARK_BAR_OPEN_IN_NEW_TAB); - AddItem(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW, - IDS_BOOMARK_BAR_OPEN_IN_NEW_WINDOW); - AddItem(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO, - IDS_BOOMARK_BAR_OPEN_INCOGNITO); + if (configuration != BOOKMARK_MANAGER_ORGANIZE_MENU) { + if (selection.size() == 1 && selection[0]->is_url()) { + AppendItem(IDS_BOOMARK_BAR_OPEN_ALL, IDS_BOOMARK_BAR_OPEN_IN_NEW_TAB); + AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW, + IDS_BOOMARK_BAR_OPEN_IN_NEW_WINDOW); + AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO, + IDS_BOOMARK_BAR_OPEN_INCOGNITO); } else { - AddItem(IDS_BOOMARK_BAR_OPEN_ALL); - AddItem(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW); - AddItem(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO); + AppendItem(IDS_BOOMARK_BAR_OPEN_ALL, IDS_BOOMARK_BAR_OPEN_ALL); + AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW, + IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW); + AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO, + IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO); } - AddSeparator(); + AppendSeparator(); } - if (selection_.size() == 1 && selection_[0]->is_folder()) { - AddItem(IDS_BOOKMARK_BAR_RENAME_FOLDER); + if (selection.size() == 1 && selection[0]->is_folder()) { + AppendItem(IDS_BOOKMARK_BAR_RENAME_FOLDER); } else { - AddItem(IDS_BOOKMARK_BAR_EDIT); + AppendItem(IDS_BOOKMARK_BAR_EDIT); } - AddItem(IDS_BOOKMARK_BAR_REMOVE); - if (configuration_ == BOOKMARK_MANAGER_TABLE || - configuration_ == BOOKMARK_MANAGER_TABLE_OTHER || - configuration_ == BOOKMARK_MANAGER_ORGANIZE_MENU || - configuration_ == BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER) { - AddItem(IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER); + if (configuration == BOOKMARK_MANAGER_TABLE || + configuration == BOOKMARK_MANAGER_TABLE_OTHER || + configuration == BOOKMARK_MANAGER_ORGANIZE_MENU || + configuration == BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER) { + AppendItem(IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER); } - if (configuration_ == BOOKMARK_MANAGER_TABLE || - configuration_ == BOOKMARK_MANAGER_TABLE_OTHER || - configuration_ == BOOKMARK_MANAGER_TREE || - configuration_ == BOOKMARK_MANAGER_ORGANIZE_MENU || - configuration_ == BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER) { - AddSeparator(); - AddItem(IDS_CUT); - AddItem(IDS_COPY); - AddItem(IDS_PASTE); - } + AppendSeparator(); + AppendItem(IDS_CUT); + AppendItem(IDS_COPY); + AppendItem(IDS_PASTE); + + AppendSeparator(); + AppendItem(IDS_BOOKMARK_BAR_REMOVE); - if (configuration_ == BOOKMARK_MANAGER_ORGANIZE_MENU) { - AddSeparator(); - AddItem(IDS_BOOKMARK_MANAGER_SORT); + if (configuration == BOOKMARK_MANAGER_ORGANIZE_MENU) { + AppendSeparator(); + AppendItem(IDS_BOOKMARK_MANAGER_SORT); } - AddSeparator(); + AppendSeparator(); - AddItem(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK); - AddItem(IDS_BOOMARK_BAR_NEW_FOLDER); + AppendItem(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK); + AppendItem(IDS_BOOMARK_BAR_NEW_FOLDER); - if (configuration_ == BOOKMARK_BAR) { - AddSeparator(); - AddItem(IDS_BOOKMARK_MANAGER); - AddCheckboxItem(IDS_BOOMARK_BAR_ALWAYS_SHOW); + if (configuration == BOOKMARK_BAR) { + AppendSeparator(); + AppendItem(IDS_BOOKMARK_MANAGER); + AppendCheckboxItem(IDS_BOOMARK_BAR_ALWAYS_SHOW); } -} -void BookmarkContextMenuController::AddItem(int id) { - menu_model_->AddItem(id, l10n_util::GetStringUTF16(id)); + model_->AddObserver(this); } -void BookmarkContextMenuController::AddItem(int id, int localization_id) { - menu_model_->AddItemWithStringId(id, localization_id); +BookmarkContextMenuGtk::~BookmarkContextMenuGtk() { + if (model_) + model_->RemoveObserver(this); } -void BookmarkContextMenuController::AddSeparator() { - menu_model_->AddSeparator(); +void BookmarkContextMenuGtk::DelegateDestroyed() { + delegate_ = NULL; } -void BookmarkContextMenuController::AddCheckboxItem(int id) { - menu_model_->AddCheckItemWithStringId(id, id); -} +void BookmarkContextMenuGtk::ExecuteCommand(int id) { + if (model_changed_) + return; -void BookmarkContextMenuController::ExecuteCommand(int id) { - BookmarkModel* model = RemoveModelObserver(); if (delegate_) delegate_->WillExecuteCommand(); @@ -305,6 +297,8 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { case IDS_BOOMARK_BAR_OPEN_ALL: case IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO: case IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW: { + PageNavigator* navigator = browser_ ? + browser_->GetSelectedTabContents() : navigator_; WindowOpenDisposition initial_disposition; if (id == IDS_BOOMARK_BAR_OPEN_ALL) { initial_disposition = NEW_FOREGROUND_TAB; @@ -319,7 +313,8 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { UserMetrics::RecordAction("BookmarkBar_ContextMenu_OpenAllIncognito", profile_); } - bookmark_utils::OpenAll(parent_window_, profile_, navigator_, selection_, + + bookmark_utils::OpenAll(wnd_, profile_, navigator, selection_, initial_disposition); break; } @@ -339,24 +334,23 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { editor_config = BookmarkEditor::SHOW_TREE; else editor_config = BookmarkEditor::NO_TREE; - BookmarkEditor::Show(parent_window_, profile_, parent_, + BookmarkEditor::Show(wnd_, profile_, parent_, BookmarkEditor::EditDetails(selection_[0]), editor_config, NULL); } else { - EditFolderController::Show(profile_, parent_window_, selection_[0], - false, false); + EditFolderController::Show(profile_, wnd_, selection_[0], false, + false); } break; case IDS_BOOKMARK_BAR_REMOVE: { UserMetrics::RecordAction("BookmarkBar_ContextMenu_Remove", profile_); + BookmarkModel* model = RemoveModelObserver(); - delegate_->WillRemoveBookmarks(selection_); for (size_t i = 0; i < selection_.size(); ++i) { model->Remove(selection_[i]->GetParent(), selection_[i]->GetParent()->IndexOfChild(selection_[i])); } - delegate_->DidRemoveBookmarks(); selection_.clear(); break; } @@ -373,7 +367,7 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { // This is owned by the BookmarkEditorView. handler = new SelectOnCreationHandler(profile_); } - BookmarkEditor::Show(parent_window_, profile_, GetParentForNewNodes(), + BookmarkEditor::Show(wnd_, profile_, GetParentForNewNodes(), BookmarkEditor::EditDetails(), editor_config, handler); break; @@ -382,9 +376,8 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { case IDS_BOOMARK_BAR_NEW_FOLDER: { UserMetrics::RecordAction("BookmarkBar_ContextMenu_NewFolder", profile_); - EditFolderController::Show(profile_, parent_window_, - GetParentForNewNodes(), true, - configuration_ != BOOKMARK_BAR); + EditFolderController::Show(profile_, wnd_, GetParentForNewNodes(), + true, (configuration_ != BOOKMARK_BAR)); break; } @@ -411,29 +404,27 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { case IDS_BOOKMARK_MANAGER_SORT: UserMetrics::RecordAction("BookmarkManager_Sort", profile_); - model->SortChildren(parent_); - break; - - case IDS_CUT: - delegate_->WillRemoveBookmarks(selection_); - bookmark_utils::CopyToClipboard(model, selection_, false); - delegate_->DidRemoveBookmarks(); + model_->SortChildren(parent_); break; case IDS_COPY: - bookmark_utils::CopyToClipboard(model, selection_, false); + case IDS_CUT: + bookmark_utils::CopyToClipboard(profile_->GetBookmarkModel(), + selection_, id == IDS_CUT); break; case IDS_PASTE: { - // Always paste to parent. - if (!parent_) + const BookmarkNode* paste_target = GetParentForNewNodes(); + if (!paste_target) return; - int index = (selection_.size() == 1) ? - parent_->IndexOfChild(selection_[0]) : -1; - if (index != -1) - index++; - bookmark_utils::PasteFromClipboard(model, parent_, index); + int index = -1; + if (selection_.size() == 1 && selection_[0]->is_url()) { + index = paste_target->IndexOfChild(selection_[0]) + 1; + } + + bookmark_utils::PasteFromClipboard(profile_->GetBookmarkModel(), + paste_target, index); break; } @@ -442,16 +433,16 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { } } -bool BookmarkContextMenuController::IsCommandIdChecked(int command_id) const { - DCHECK(command_id == IDS_BOOMARK_BAR_ALWAYS_SHOW); +bool BookmarkContextMenuGtk::IsCommandIdChecked(int id) const { + DCHECK(id == IDS_BOOMARK_BAR_ALWAYS_SHOW); return profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); } -bool BookmarkContextMenuController::IsCommandIdEnabled(int command_id) const { +bool BookmarkContextMenuGtk::IsCommandIdEnabled(int id) const { bool is_root_node = (selection_.size() == 1 && selection_[0]->GetParent() == model_->root_node()); - switch (command_id) { + switch (id) { case IDS_BOOMARK_BAR_OPEN_INCOGNITO: return !profile_->IsOffTheRecord(); @@ -486,65 +477,84 @@ bool BookmarkContextMenuController::IsCommandIdEnabled(int command_id) const { return selection_.size() > 0 && !is_root_node; case IDS_PASTE: - // Always paste to parent. - return bookmark_utils::CanPasteFromClipboard(parent_); + // Paste to selection from the Bookmark Bar, to parent_ everywhere else + return (configuration_ == BOOKMARK_BAR && + !selection_.empty() && + bookmark_utils::CanPasteFromClipboard(selection_[0])) || + bookmark_utils::CanPasteFromClipboard(parent_); } return true; } -void BookmarkContextMenuController::BookmarkModelBeingDeleted( - BookmarkModel* model) { +bool BookmarkContextMenuGtk::GetAcceleratorForCommandId( + int command_id, + menus::Accelerator* accelerator) { + return false; +} + +void BookmarkContextMenuGtk::BookmarkModelBeingDeleted(BookmarkModel* model) { ModelChanged(); } -void BookmarkContextMenuController::BookmarkNodeMoved( - BookmarkModel* model, - const BookmarkNode* old_parent, - int old_index, - const BookmarkNode* new_parent, - int new_index) { +void BookmarkContextMenuGtk::BookmarkNodeMoved(BookmarkModel* model, + const BookmarkNode* old_parent, + int old_index, + const BookmarkNode* new_parent, + int new_index) { ModelChanged(); } -void BookmarkContextMenuController::BookmarkNodeAdded( - BookmarkModel* model, - const BookmarkNode* parent, - int index) { +void BookmarkContextMenuGtk::BookmarkNodeAdded(BookmarkModel* model, + const BookmarkNode* parent, + int index) { ModelChanged(); } -void BookmarkContextMenuController::BookmarkNodeRemoved( - BookmarkModel* model, - const BookmarkNode* parent, - int index, - const BookmarkNode* node) { +void BookmarkContextMenuGtk::BookmarkNodeRemoved(BookmarkModel* model, + const BookmarkNode* parent, + int index, + const BookmarkNode* node) { ModelChanged(); } -void BookmarkContextMenuController::BookmarkNodeChanged( - BookmarkModel* model, - const BookmarkNode* node) { +void BookmarkContextMenuGtk::BookmarkNodeChanged(BookmarkModel* model, + const BookmarkNode* node) { ModelChanged(); } -void BookmarkContextMenuController::BookmarkNodeChildrenReordered( - BookmarkModel* model, - const BookmarkNode* node) { +void BookmarkContextMenuGtk::BookmarkNodeChildrenReordered( + BookmarkModel* model, const BookmarkNode* node) { ModelChanged(); } -void BookmarkContextMenuController::ModelChanged() { - delegate_->CloseMenu(); +void BookmarkContextMenuGtk::ModelChanged() { + model_changed_ = true; +} + +void BookmarkContextMenuGtk::AppendItem(int id) { + menu_model_->AddItem(id, l10n_util::GetStringUTF16(id)); +} + +void BookmarkContextMenuGtk::AppendItem(int id, int localization_id) { + menu_model_->AddItemWithStringId(id, localization_id); +} + +void BookmarkContextMenuGtk::AppendSeparator() { + menu_model_->AddSeparator(); +} + +void BookmarkContextMenuGtk::AppendCheckboxItem(int id) { + menu_model_->AddCheckItemWithStringId(id, id); } -BookmarkModel* BookmarkContextMenuController::RemoveModelObserver() { +BookmarkModel* BookmarkContextMenuGtk::RemoveModelObserver() { BookmarkModel* model = model_; model_->RemoveObserver(this); model_ = NULL; return model; } -bool BookmarkContextMenuController::HasURLs() const { +bool BookmarkContextMenuGtk::HasURLs() const { for (size_t i = 0; i < selection_.size(); ++i) { if (NodeHasURLs(selection_[i])) return true; @@ -552,8 +562,7 @@ bool BookmarkContextMenuController::HasURLs() const { return false; } -const BookmarkNode* - BookmarkContextMenuController::GetParentForNewNodes() const { +const BookmarkNode* BookmarkContextMenuGtk::GetParentForNewNodes() const { return (selection_.size() == 1 && selection_[0]->is_folder()) ? selection_[0] : parent_; } diff --git a/chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h b/chrome/browser/gtk/bookmark_context_menu_gtk.h index 9b55996..a625611 100644 --- a/chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h +++ b/chrome/browser/gtk/bookmark_context_menu_gtk.h @@ -2,44 +2,26 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_GTK_H_ -#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_GTK_H_ +#ifndef CHROME_BROWSER_GTK_BOOKMARK_CONTEXT_MENU_GTK_H_ +#define CHROME_BROWSER_GTK_BOOKMARK_CONTEXT_MENU_GTK_H_ #include <vector> #include "app/gfx/native_widget_types.h" #include "app/menus/simple_menu_model.h" #include "base/basictypes.h" +#include "base/scoped_ptr.h" #include "chrome/browser/bookmarks/bookmark_model_observer.h" class Browser; class PageNavigator; class Profile; -// An interface implemented by an object that performs actions on the actual -// menu for the controller. -class BookmarkContextMenuControllerDelegate { - public: - virtual ~BookmarkContextMenuControllerDelegate() {} - - // Closes the bookmark context menu. - virtual void CloseMenu() = 0; - - // Sent before bookmarks are removed. - virtual void WillRemoveBookmarks( - const std::vector<const BookmarkNode*>& bookmarks) {} - - // Sent after bookmarks have been removed. - virtual void DidRemoveBookmarks() {} - - // Sent before any command from the menu is executed. - virtual void WillExecuteCommand() {} -}; - -// BookmarkContextMenuController creates and manages state for the context menu -// shown for any bookmark item. -class BookmarkContextMenuController : public BookmarkModelObserver, - public menus::SimpleMenuModel::Delegate { +// BookmarkContextMenu manages the context menu shown for the +// bookmark bar, items on the bookmark bar, submenus of the bookmark bar and +// the bookmark manager. +class BookmarkContextMenuGtk : public BookmarkModelObserver, + public menus::SimpleMenuModel::Delegate { public: // Used to configure what the context menu shows. enum ConfigurationType { @@ -55,6 +37,12 @@ class BookmarkContextMenuController : public BookmarkModelObserver, BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER }; + class Delegate { + public: + // Called when one of the menu items is selected and executed. + virtual void WillExecuteCommand() = 0; + }; + // Creates the bookmark context menu. // |profile| is used for opening urls as well as enabling 'open incognito'. // |browser| is used to determine the PageNavigator and may be null. @@ -62,47 +50,31 @@ class BookmarkContextMenuController : public BookmarkModelObserver, // |parent| is the parent for newly created nodes if |selection| is empty. // |selection| is the nodes the context menu operates on and may be empty. // |configuration| determines which items to show. - BookmarkContextMenuController( - gfx::NativeWindow parent_window, - BookmarkContextMenuControllerDelegate* delegate, - Profile* profile, - PageNavigator* navigator, - const BookmarkNode* parent, - const std::vector<const BookmarkNode*>& selection, - ConfigurationType configuration); - virtual ~BookmarkContextMenuController(); - - void BuildMenu(); - - menus::SimpleMenuModel* menu_model() { - return menu_model_.get(); - } - - // Adds a IDS_* style command to the menu. - void AddItem(int id); - // Adds a IDS_* style command to the menu with a different localized string. - void AddItem(int id, int localization_id); - // Adds a separator to the menu. - void AddSeparator(); - // Adds a checkable item to the menu. - void AddCheckboxItem(int id); - - // menus::SimpleMenuModel::Delegate implementation: + BookmarkContextMenuGtk(gfx::NativeWindow hwnd, + Profile* profile, + Browser* browser, + PageNavigator* navigator, + const BookmarkNode* parent, + const std::vector<const BookmarkNode*>& selection, + ConfigurationType configuration, + Delegate* delegate); + virtual ~BookmarkContextMenuGtk(); + + menus::MenuModel* menu_model() const { return menu_model_.get(); } + + // Should be called by the delegate when it is no longer valid. + void DelegateDestroyed(); + + // Menu::Delegate methods. virtual bool IsCommandIdChecked(int command_id) const; virtual bool IsCommandIdEnabled(int command_id) const; virtual bool GetAcceleratorForCommandId( int command_id, - menus::Accelerator* accelerator) { - return false; - } + menus::Accelerator* accelerator); virtual void ExecuteCommand(int command_id); - // Accessors: - Profile* profile() const { return profile_; } - PageNavigator* navigator() const { return navigator_; } - private: - // BookmarkModelObserver methods. Any change to the model results in closing + // BookmarkModelObserver method. Any change to the model results in closing // the menu. virtual void Loaded(BookmarkModel* model) {} virtual void BookmarkModelBeingDeleted(BookmarkModel* model); @@ -128,6 +100,15 @@ class BookmarkContextMenuController : public BookmarkModelObserver, // Invoked from the various bookmark model observer methods. Closes the menu. void ModelChanged(); + // Adds a IDS_* style command to the menu. + void AppendItem(int id); + // Adds a IDS_* style command to the menu with a different localized string. + void AppendItem(int id, int localization_id); + // Adds a separator to the menu. + void AppendSeparator(); + // Adds a checkable item to the menu. + void AppendCheckboxItem(int id); + // Removes the observer from the model and NULLs out model_. BookmarkModel* RemoveModelObserver(); @@ -139,17 +120,23 @@ class BookmarkContextMenuController : public BookmarkModelObserver, // parent_ is returned. const BookmarkNode* GetParentForNewNodes() const; - gfx::NativeWindow parent_window_; - BookmarkContextMenuControllerDelegate* delegate_; + gfx::NativeWindow wnd_; Profile* profile_; + Browser* browser_; PageNavigator* navigator_; const BookmarkNode* parent_; std::vector<const BookmarkNode*> selection_; - ConfigurationType configuration_; BookmarkModel* model_; + ConfigurationType configuration_; + Delegate* delegate_; scoped_ptr<menus::SimpleMenuModel> menu_model_; - DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuController); + // Tracks whether the model has changed. For the most part the model won't + // change while a context menu is showing, but if it does, we'd better not + // try to execute any commands. + bool model_changed_; + + DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuGtk); }; -#endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_GTK_H_ +#endif // CHROME_BROWSER_GTK_BOOKMARK_CONTEXT_MENU_GTK_H_ diff --git a/chrome/browser/gtk/bookmark_manager_gtk.cc b/chrome/browser/gtk/bookmark_manager_gtk.cc index 53de4d2..554a867 100644 --- a/chrome/browser/gtk/bookmark_manager_gtk.cc +++ b/chrome/browser/gtk/bookmark_manager_gtk.cc @@ -632,15 +632,10 @@ void BookmarkManagerGtk::ResetOrganizeMenu(bool left) { if (old_menu) MessageLoop::current()->DeleteSoon(FROM_HERE, old_menu); - BookmarkContextMenuController* old_controller = - organize_menu_controller_.release(); - if (old_controller) - MessageLoop::current()->DeleteSoon(FROM_HERE, old_controller); - organize_menu_controller_.reset( - new BookmarkContextMenuController(GTK_WINDOW(window_), this, profile_, - NULL, parent, nodes, - BookmarkContextMenuController::BOOKMARK_MANAGER_ORGANIZE_MENU)); + new BookmarkContextMenuGtk(GTK_WINDOW(window_), profile_, + NULL, NULL, parent, nodes, + BookmarkContextMenuGtk::BOOKMARK_MANAGER_ORGANIZE_MENU, NULL)); organize_menu_.reset( new MenuGtk(NULL, organize_menu_controller_->menu_model())); gtk_menu_item_set_submenu(GTK_MENU_ITEM(organize_), @@ -1488,10 +1483,6 @@ void BookmarkManagerGtk::OnStateChanged() { UpdateSyncStatus(); } -void BookmarkManagerGtk::CloseMenu() { - organize_menu_->Cancel(); -} - // static gboolean BookmarkManagerGtk::OnGtkAccelerator(GtkAccelGroup* accel_group, GObject* acceleratable, diff --git a/chrome/browser/gtk/bookmark_manager_gtk.h b/chrome/browser/gtk/bookmark_manager_gtk.h index 903a5ba..24db624 100644 --- a/chrome/browser/gtk/bookmark_manager_gtk.h +++ b/chrome/browser/gtk/bookmark_manager_gtk.h @@ -12,8 +12,8 @@ #include "base/gfx/rect.h" #include "base/ref_counted.h" #include "base/task.h" -#include "chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h" #include "chrome/browser/bookmarks/bookmark_model_observer.h" +#include "chrome/browser/gtk/bookmark_context_menu_gtk.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/shell_dialogs.h" #include "chrome/common/gtk_tree.h" @@ -27,8 +27,7 @@ class Profile; class BookmarkManagerGtk : public BookmarkModelObserver, public ProfileSyncServiceObserver, public gtk_tree::TableAdapter::Delegate, - public SelectFileDialog::Listener, - public BookmarkContextMenuControllerDelegate { + public SelectFileDialog::Listener { public: virtual ~BookmarkManagerGtk(); @@ -75,9 +74,6 @@ class BookmarkManagerGtk : public BookmarkModelObserver, // ProfileSyncServiceObserver implementation. virtual void OnStateChanged(); - // BookmarkContextMenuController::Delegate implementation. - virtual void CloseMenu(); - private: friend class BookmarkManagerTest; FRIEND_TEST(BookmarkManagerTest, Crash); @@ -344,7 +340,7 @@ class BookmarkManagerGtk : public BookmarkModelObserver, GtkWidget* organize_; // The submenu the item pops up. // The controller. - scoped_ptr<BookmarkContextMenuController> organize_menu_controller_; + scoped_ptr<BookmarkContextMenuGtk> organize_menu_controller_; // The view. scoped_ptr<MenuGtk> organize_menu_; // Whether the menu refers to the left selection. diff --git a/chrome/browser/gtk/bookmark_menu_controller_gtk.cc b/chrome/browser/gtk/bookmark_menu_controller_gtk.cc index a6232ac..e3bdcb3 100644 --- a/chrome/browser/gtk/bookmark_menu_controller_gtk.cc +++ b/chrome/browser/gtk/bookmark_menu_controller_gtk.cc @@ -12,6 +12,7 @@ #include "app/resource_bundle.h" #include "base/string_util.h" #include "chrome/browser/bookmarks/bookmark_model.h" +#include "chrome/browser/gtk/bookmark_context_menu_gtk.h" #include "chrome/browser/gtk/bookmark_utils_gtk.h" #include "chrome/browser/gtk/gtk_chrome_button.h" #include "chrome/browser/gtk/gtk_theme_provider.h" @@ -105,6 +106,8 @@ BookmarkMenuController::BookmarkMenuController(Browser* browser, } BookmarkMenuController::~BookmarkMenuController() { + if (context_menu_controller_.get()) + context_menu_controller_->DelegateDestroyed(); profile_->GetBookmarkModel()->RemoveObserver(this); gtk_menu_popdown(GTK_MENU(menu_)); } @@ -137,10 +140,6 @@ void BookmarkMenuController::WillExecuteCommand() { gtk_menu_popdown(GTK_MENU(menu_)); } -void BookmarkMenuController::CloseMenu() { - context_menu_->Cancel(); -} - void BookmarkMenuController::NavigateToMenuItem( GtkWidget* menu_item, WindowOpenDisposition disposition) { @@ -252,10 +251,10 @@ gboolean BookmarkMenuController::OnButtonPressed( if (node) nodes.push_back(node); controller->context_menu_controller_.reset( - new BookmarkContextMenuController( - controller->parent_window_, controller, controller->profile_, - controller->page_navigator_, parent, nodes, - BookmarkContextMenuController::BOOKMARK_BAR)); + new BookmarkContextMenuGtk( + controller->parent_window_, controller->profile_, + controller->browser_, controller->page_navigator_, parent, nodes, + BookmarkContextMenuGtk::BOOKMARK_BAR, controller)); controller->context_menu_.reset( new MenuGtk(NULL, controller->context_menu_controller_->menu_model())); diff --git a/chrome/browser/gtk/bookmark_menu_controller_gtk.h b/chrome/browser/gtk/bookmark_menu_controller_gtk.h index f8f6008..fdf098a 100644 --- a/chrome/browser/gtk/bookmark_menu_controller_gtk.h +++ b/chrome/browser/gtk/bookmark_menu_controller_gtk.h @@ -11,7 +11,7 @@ #include "base/scoped_ptr.h" #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" -#include "chrome/browser/bookmarks/bookmark_context_menu_controller_gtk.h" +#include "chrome/browser/gtk/bookmark_context_menu_gtk.h" #include "chrome/common/owned_widget_gtk.h" #include "webkit/glue/window_open_disposition.h" @@ -24,7 +24,7 @@ class BookmarkNode; class MenuGtk; class BookmarkMenuController : public BaseBookmarkModelObserver, - public BookmarkContextMenuControllerDelegate { + public BookmarkContextMenuGtk::Delegate { public: // Creates a BookmarkMenuController showing the children of |node| starting // at index |start_child_index|. @@ -47,9 +47,8 @@ class BookmarkMenuController : public BaseBookmarkModelObserver, virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model, const BookmarkNode* node); - // Overridden from BookmarkContextMenuController::Delegate: + // Overridden from BookmarkContextMenuGtk::Delegate: virtual void WillExecuteCommand(); - virtual void CloseMenu(); private: // Recursively change the bookmark hierarchy rooted in |parent| into a set of @@ -126,7 +125,7 @@ class BookmarkMenuController : public BaseBookmarkModelObserver, std::map<const BookmarkNode*, GtkWidget*> node_to_menu_widget_map_; // The controller and view for the right click context menu. - scoped_ptr<BookmarkContextMenuController> context_menu_controller_; + scoped_ptr<BookmarkContextMenuGtk> context_menu_controller_; scoped_ptr<MenuGtk> context_menu_; DISALLOW_COPY_AND_ASSIGN(BookmarkMenuController); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 748874d..8ced6dc 100755 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -167,8 +167,6 @@ 'browser/bookmarks/bookmark_codec.h', 'browser/bookmarks/bookmark_context_menu_controller.cc', 'browser/bookmarks/bookmark_context_menu_controller.h', - 'browser/bookmarks/bookmark_context_menu_controller_gtk.cc', - 'browser/bookmarks/bookmark_context_menu_controller_gtk.h', 'browser/bookmarks/bookmark_drag_data.cc', 'browser/bookmarks/bookmark_drag_data.h', 'browser/bookmarks/bookmark_drop_info.cc', @@ -910,6 +908,8 @@ 'browser/gtk/bookmark_bar_instructions_gtk.h', 'browser/gtk/bookmark_bubble_gtk.cc', 'browser/gtk/bookmark_bubble_gtk.h', + 'browser/gtk/bookmark_context_menu_gtk.cc', + 'browser/gtk/bookmark_context_menu_gtk.h', 'browser/gtk/bookmark_editor_gtk.cc', 'browser/gtk/bookmark_editor_gtk.h', 'browser/gtk/bookmark_manager_gtk.cc', @@ -2072,9 +2072,6 @@ # Windows-specific files. 'browser/password_manager/password_store_win.cc', 'browser/password_manager/password_store_win.h', - # Disabled while bookmark context menus are unforked. - 'browser/bookmarks/bookmark_context_menu_controller.cc', - 'browser/bookmarks/bookmark_context_menu_controller.h', ], 'sources': [ 'browser/crash_handler_host_linux.h', |