blob: 6896e5ff1be674b5d3297a53dd2dda42dc4a6d2a [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <set>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "components/bookmarks/browser/base_bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_node_data.h"
#include "ui/views/controls/menu/menu_delegate.h"
class BookmarkBarView;
class BookmarkMenuControllerObserver;
class BookmarkMenuDelegate;
class Browser;
namespace bookmarks {
class BookmarkNode;
namespace content {
class PageNavigator;
namespace ui {
class OSExchangeData;
namespace views {
class MenuButton;
class MenuItemView;
class MenuRunner;
class Widget;
// BookmarkMenuController is responsible for showing a menu of bookmarks,
// each item in the menu represents a bookmark.
// BookmarkMenuController deletes itself as necessary, although the menu can
// be explicitly hidden by way of the Cancel method.
class BookmarkMenuController : public bookmarks::BaseBookmarkModelObserver,
public views::MenuDelegate {
// Creates a BookmarkMenuController showing the children of |node| starting
// at |start_child_index|.
BookmarkMenuController(Browser* browser,
content::PageNavigator* page_navigator,
views::Widget* parent,
const bookmarks::BookmarkNode* node,
int start_child_index,
bool for_drop);
void RunMenuAt(BookmarkBarView* bookmark_bar);
void clear_bookmark_bar() {
bookmark_bar_ = NULL;
// Hides the menu.
void Cancel();
// Returns the node the menu is showing for.
const bookmarks::BookmarkNode* node() const { return node_; }
// Returns the menu.
views::MenuItemView* menu() const;
// Returns the context menu, or NULL if the context menu isn't showing.
views::MenuItemView* context_menu() const;
// Sets the page navigator.
void SetPageNavigator(content::PageNavigator* navigator);
void set_observer(BookmarkMenuControllerObserver* observer) {
observer_ = observer;
// views::MenuDelegate:
base::string16 GetTooltipText(int id, const gfx::Point& p) const override;
bool IsTriggerableEvent(views::MenuItemView* view,
const ui::Event& e) override;
void ExecuteCommand(int id, int mouse_event_flags) override;
bool ShouldExecuteCommandWithoutClosingMenu(int id,
const ui::Event& e) override;
bool GetDropFormats(
views::MenuItemView* menu,
int* formats,
std::set<ui::Clipboard::FormatType>* format_types) override;
bool AreDropTypesRequired(views::MenuItemView* menu) override;
bool CanDrop(views::MenuItemView* menu,
const ui::OSExchangeData& data) override;
int GetDropOperation(views::MenuItemView* item,
const ui::DropTargetEvent& event,
DropPosition* position) override;
int OnPerformDrop(views::MenuItemView* menu,
DropPosition position,
const ui::DropTargetEvent& event) override;
bool ShowContextMenu(views::MenuItemView* source,
int id,
const gfx::Point& p,
ui::MenuSourceType source_type) override;
bool CanDrag(views::MenuItemView* menu) override;
void WriteDragData(views::MenuItemView* sender,
ui::OSExchangeData* data) override;
int GetDragOperations(views::MenuItemView* sender) override;
void OnMenuClosed(views::MenuItemView* menu,
views::MenuRunner::RunResult result) override;
views::MenuItemView* GetSiblingMenu(views::MenuItemView* menu,
const gfx::Point& screen_point,
views::MenuAnchorPosition* anchor,
bool* has_mnemonics,
views::MenuButton** button) override;
int GetMaxWidthForMenu(views::MenuItemView* view) override;
void WillShowMenu(views::MenuItemView* menu) override;
// bookmarks::BaseBookmarkModelObserver:
void BookmarkModelChanged() override;
// BookmarkMenuController deletes itself as necessary.
~BookmarkMenuController() override;
std::unique_ptr<views::MenuRunner> menu_runner_;
std::unique_ptr<BookmarkMenuDelegate> menu_delegate_;
// The node we're showing the contents of.
const bookmarks::BookmarkNode* node_;
// Data for the drop.
bookmarks::BookmarkNodeData drop_data_;
// The observer, may be null.
BookmarkMenuControllerObserver* observer_;
// Is the menu being shown for a drop?
bool for_drop_;
// The bookmark bar. This is only non-null if we're showing a menu item for a
// folder on the bookmark bar and not for drop, or if the BookmarkBarView has
// been destroyed before the menu.
BookmarkBarView* bookmark_bar_;