1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h"
6
7#include "base/i18n/rtl.h"
8#include "base/utf_string_conversions.h"
9#include "chrome/app/chrome_command_ids.h"
10#include "chrome/browser/bookmarks/bookmark_model.h"
11#include "chrome/browser/profiles/profile.h"
12#include "content/common/notification_service.h"
13#include "grit/generated_resources.h"
14#include "ui/base/l10n/l10n_util.h"
15#include "views/controls/menu/menu_item_view.h"
16
17////////////////////////////////////////////////////////////////////////////////
18// BookmarkContextMenu, public:
19
20BookmarkContextMenu::BookmarkContextMenu(
21    gfx::NativeWindow parent_window,
22    Profile* profile,
23    PageNavigator* page_navigator,
24    const BookmarkNode* parent,
25    const std::vector<const BookmarkNode*>& selection)
26    : ALLOW_THIS_IN_INITIALIZER_LIST(
27          controller_(new BookmarkContextMenuControllerViews(parent_window,
28              this, profile, page_navigator, parent, selection))),
29      parent_window_(parent_window),
30      ALLOW_THIS_IN_INITIALIZER_LIST(menu_(new views::MenuItemView(this))),
31      parent_node_(parent),
32      observer_(NULL) {
33  controller_->BuildMenu();
34}
35
36BookmarkContextMenu::~BookmarkContextMenu() {
37}
38
39void BookmarkContextMenu::RunMenuAt(const gfx::Point& point) {
40  NotificationService::current()->Notify(
41      NotificationType::BOOKMARK_CONTEXT_MENU_SHOWN,
42      Source<BookmarkContextMenu>(this),
43      NotificationService::NoDetails());
44  // width/height don't matter here.
45  views::MenuItemView::AnchorPosition anchor = base::i18n::IsRTL() ?
46      views::MenuItemView::TOPRIGHT : views::MenuItemView::TOPLEFT;
47  menu_->RunMenuAt(parent_window_, NULL, gfx::Rect(point.x(), point.y(), 0, 0),
48                   anchor, true);
49}
50
51////////////////////////////////////////////////////////////////////////////////
52// BookmarkContextMenu, views::MenuDelegate implementation:
53
54void BookmarkContextMenu::ExecuteCommand(int command_id) {
55  controller_->ExecuteCommand(command_id);
56}
57
58bool BookmarkContextMenu::IsItemChecked(int command_id) const {
59  return controller_->IsItemChecked(command_id);
60}
61
62bool BookmarkContextMenu::IsCommandEnabled(int command_id) const {
63  return controller_->IsCommandEnabled(command_id);
64}
65
66bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id) {
67  return id != IDC_BOOKMARK_BAR_REMOVE ||
68      (parent_node_ ==
69       controller_->profile()->GetBookmarkModel()->other_node() &&
70       parent_node_->child_count() == 1);
71}
72
73////////////////////////////////////////////////////////////////////////////////
74// BookmarkContextMenu, BookmarkContextMenuControllerViewsDelegate
75// implementation:
76
77void BookmarkContextMenu::CloseMenu() {
78  menu_->Cancel();
79}
80
81void BookmarkContextMenu::AddItemWithStringId(int command_id, int string_id) {
82  menu_->AppendMenuItemWithLabel(
83      command_id, UTF16ToWide(l10n_util::GetStringUTF16(string_id)));
84}
85
86void BookmarkContextMenu::AddSeparator() {
87  menu_->AppendSeparator();
88}
89
90void BookmarkContextMenu::AddCheckboxItem(int command_id, int string_id) {
91  menu_->AppendMenuItem(command_id,
92                        UTF16ToWide(l10n_util::GetStringUTF16(string_id)),
93                        views::MenuItemView::CHECKBOX);
94}
95
96void BookmarkContextMenu::WillRemoveBookmarks(
97    const std::vector<const BookmarkNode*>& bookmarks) {
98  if (observer_)
99    observer_->WillRemoveBookmarks(bookmarks);
100}
101
102void BookmarkContextMenu::DidRemoveBookmarks() {
103  if (observer_)
104    observer_->DidRemoveBookmarks();
105}
106