1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
// Copyright (c) 2010 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 "chrome/browser/views/bookmark_menu_button.h"
#include "build/build_config.h"
#if defined(OS_GTK)
#include <gtk/gtk.h>
#endif
#include "app/os_exchange_data.h"
#include "app/resource_bundle.h"
#include "app/theme_provider.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_theme_provider.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/view_ids.h"
#include "grit/theme_resources.h"
#include "views/window/window.h"
BookmarkMenuButton::BookmarkMenuButton(Browser* browser)
: views::MenuButton(NULL, std::wstring(), NULL, false),
browser_(browser),
bookmark_drop_menu_(NULL),
drop_operation_(0) {
// TODO(sky): if we keep this code, we need real icons, a11y support, and a
// tooltip.
set_menu_delegate(this);
SetID(VIEW_ID_BOOKMARK_MENU);
ThemeProvider* tp = browser_->profile()->GetThemeProvider();
SetIcon(*tp->GetBitmapNamed(IDR_MENU_BOOKMARK));
}
BookmarkMenuButton::~BookmarkMenuButton() {
if (bookmark_drop_menu_)
bookmark_drop_menu_->set_observer(NULL);
}
bool BookmarkMenuButton::GetDropFormats(
int* formats,
std::set<OSExchangeData::CustomFormat>* custom_formats) {
BookmarkModel* bookmark_model = GetBookmarkModel();
if (!bookmark_model || !bookmark_model->IsLoaded())
return false;
*formats = OSExchangeData::URL;
custom_formats->insert(BookmarkDragData::GetBookmarkCustomFormat());
return true;
}
bool BookmarkMenuButton::AreDropTypesRequired() {
return true;
}
bool BookmarkMenuButton::CanDrop(const OSExchangeData& data) {
BookmarkModel* bookmark_model = GetBookmarkModel();
if (!bookmark_model || !bookmark_model->IsLoaded())
return false;
drag_data_ = BookmarkDragData();
// Only accept drops of 1 node, which is the case for all data dragged from
// bookmark bar and menus.
return drag_data_.Read(data) && drag_data_.has_single_url();
}
int BookmarkMenuButton::OnDragUpdated(const views::DropTargetEvent& event) {
if (!drag_data_.is_valid())
return 0;
BookmarkModel* bookmark_model = GetBookmarkModel();
drop_operation_ = bookmark_utils::BookmarkDropOperation(
browser_->profile(), event, drag_data_,
bookmark_model->GetBookmarkBarNode(),
bookmark_model->GetBookmarkBarNode()->GetChildCount());
if (drop_operation_ != 0)
StartShowFolderDropMenuTimer();
else
StopShowFolderDropMenuTimer();
return drop_operation_;
}
void BookmarkMenuButton::OnDragExited() {
StopShowFolderDropMenuTimer();
drag_data_ = BookmarkDragData();
}
int BookmarkMenuButton::OnPerformDrop(const views::DropTargetEvent& event) {
StopShowFolderDropMenuTimer();
if (bookmark_drop_menu_)
bookmark_drop_menu_->Cancel();
// Reset the drag data to take as little memory as needed.
BookmarkDragData data = drag_data_;
drag_data_ = BookmarkDragData();
if (!drop_operation_)
return DragDropTypes::DRAG_NONE;
BookmarkModel* model = GetBookmarkModel();
if (!model)
return DragDropTypes::DRAG_NONE;
const BookmarkNode* parent = model->GetBookmarkBarNode();
return bookmark_utils::PerformBookmarkDrop(
browser_->profile(), data, parent, parent->GetChildCount());
}
void BookmarkMenuButton::BookmarkMenuDeleted(
BookmarkMenuController* controller) {
bookmark_drop_menu_ = NULL;
}
void BookmarkMenuButton::RunMenu(views::View* source,
const gfx::Point& pt) {
RunMenu(source, pt, GetWindow()->GetNativeWindow(), false);
}
void BookmarkMenuButton::RunMenu(views::View* source,
const gfx::Point& pt,
gfx::NativeWindow hwnd,
bool for_drop) {
Profile* profile = browser_->profile();
UserMetrics::RecordAction(UserMetricsAction("BookmarkMenu_clicked"),
profile);
BookmarkMenuController* menu = new BookmarkMenuController(
browser_, profile, browser_->GetSelectedTabContents(), hwnd,
GetBookmarkModel()->GetBookmarkBarNode(), 0, true);
views::MenuItemView::AnchorPosition anchor = views::MenuItemView::TOPRIGHT;
if (base::i18n::IsRTL())
anchor = views::MenuItemView::TOPLEFT;
if (for_drop) {
bookmark_drop_menu_ = menu;
bookmark_drop_menu_->set_observer(this);
}
menu->RunMenuAt(this, views::MenuItemView::TOPRIGHT, for_drop);
}
BookmarkModel* BookmarkMenuButton::GetBookmarkModel() {
return browser_->profile()->GetBookmarkModel();
}
void BookmarkMenuButton::StartShowFolderDropMenuTimer() {
if (show_drop_menu_timer_.IsRunning())
return;
#if defined(OS_WIN)
static DWORD delay = 0;
if (!delay && !SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &delay, 0))
delay = 400;
#else
static int delay = 400;
#endif
show_drop_menu_timer_.Start(base::TimeDelta::FromMilliseconds(delay),
this, &BookmarkMenuButton::ShowDropMenu);
}
void BookmarkMenuButton::StopShowFolderDropMenuTimer() {
show_drop_menu_timer_.Stop();
}
void BookmarkMenuButton::ShowDropMenu() {
RunMenu(NULL, gfx::Point(), GetWindow()->GetNativeWindow(), true);
}
|