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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// 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 "views/controls/button/menu_button.h"
#include "app/drag_drop_types.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "gfx/canvas.h"
#include "grit/app_strings.h"
#include "grit/app_resources.h"
#include "views/controls/button/button.h"
#include "views/controls/menu/view_menu_delegate.h"
#include "views/event.h"
#include "views/screen.h"
#include "views/widget/root_view.h"
#include "views/widget/widget.h"
#include "views/window/window.h"
using base::Time;
using base::TimeDelta;
namespace views {
// The amount of time, in milliseconds, we wait before allowing another mouse
// pressed event to show the menu.
static const int64 kMinimumTimeBetweenButtonClicks = 100;
// How much padding to put on the left and right of the menu marker.
static const int kMenuMarkerPaddingLeft = 3;
static const int kMenuMarkerPaddingRight = -1;
// Default menu offset.
static const int kDefaultMenuOffsetX = -2;
static const int kDefaultMenuOffsetY = -4;
// static
const char MenuButton::kViewClassName[] = "views/MenuButton";
////////////////////////////////////////////////////////////////////////////////
//
// MenuButton - constructors, destructors, initialization
//
////////////////////////////////////////////////////////////////////////////////
MenuButton::MenuButton(ButtonListener* listener,
const std::wstring& text,
ViewMenuDelegate* menu_delegate,
bool show_menu_marker)
: TextButton(listener, text),
menu_visible_(false),
menu_offset_(kDefaultMenuOffsetX, kDefaultMenuOffsetY),
menu_delegate_(menu_delegate),
show_menu_marker_(show_menu_marker),
menu_marker_(ResourceBundle::GetSharedInstance().GetBitmapNamed(
IDR_MENU_DROPARROW)),
destroyed_flag_(NULL) {
set_alignment(TextButton::ALIGN_LEFT);
}
MenuButton::~MenuButton() {
if (destroyed_flag_)
*destroyed_flag_ = true;
}
////////////////////////////////////////////////////////////////////////////////
//
// MenuButton - Public APIs
//
////////////////////////////////////////////////////////////////////////////////
gfx::Size MenuButton::GetPreferredSize() {
gfx::Size prefsize = TextButton::GetPreferredSize();
if (show_menu_marker_) {
prefsize.Enlarge(menu_marker_->width() + kMenuMarkerPaddingLeft +
kMenuMarkerPaddingRight,
0);
}
return prefsize;
}
void MenuButton::Paint(gfx::Canvas* canvas, bool for_drag) {
TextButton::Paint(canvas, for_drag);
if (show_menu_marker_) {
gfx::Insets insets = GetInsets();
// We can not use the views' mirroring infrastructure for mirroring a
// MenuButton control (see TextButton::Paint() for a detailed explanation
// regarding why we can not flip the canvas). Therefore, we need to
// manually mirror the position of the down arrow.
gfx::Rect arrow_bounds(width() - insets.right() -
menu_marker_->width() - kMenuMarkerPaddingRight,
height() / 2 - menu_marker_->height() / 2,
menu_marker_->width(),
menu_marker_->height());
arrow_bounds.set_x(MirroredLeftPointForRect(arrow_bounds));
canvas->DrawBitmapInt(*menu_marker_, arrow_bounds.x(), arrow_bounds.y());
}
}
////////////////////////////////////////////////////////////////////////////////
//
// MenuButton - Events
//
////////////////////////////////////////////////////////////////////////////////
int MenuButton::GetMaximumScreenXCoordinate() {
if (!GetWidget()) {
NOTREACHED();
return 0;
}
gfx::Rect monitor_bounds =
Screen::GetMonitorWorkAreaNearestWindow(GetWidget()->GetNativeView());
return monitor_bounds.right() - 1;
}
bool MenuButton::Activate() {
SetState(BS_PUSHED);
// We need to synchronously paint here because subsequently we enter a
// menu modal loop which will stop this window from updating and
// receiving the paint message that should be spawned by SetState until
// after the menu closes.
PaintNow();
if (menu_delegate_) {
gfx::Rect lb = GetLocalBounds(true);
// The position of the menu depends on whether or not the locale is
// right-to-left.
gfx::Point menu_position(lb.right(), lb.bottom());
if (base::i18n::IsRTL())
menu_position.set_x(lb.x());
View::ConvertPointToScreen(this, &menu_position);
if (base::i18n::IsRTL())
menu_position.Offset(-menu_offset_.x(), menu_offset_.y());
else
menu_position.Offset(menu_offset_.x(), menu_offset_.y());
int max_x_coordinate = GetMaximumScreenXCoordinate();
if (max_x_coordinate && max_x_coordinate <= menu_position.x())
menu_position.set_x(max_x_coordinate - 1);
// We're about to show the menu from a mouse press. By showing from the
// mouse press event we block RootView in mouse dispatching. This also
// appears to cause RootView to get a mouse pressed BEFORE the mouse
// release is seen, which means RootView sends us another mouse press no
// matter where the user pressed. To force RootView to recalculate the
// mouse target during the mouse press we explicitly set the mouse handler
// to NULL.
GetRootView()->SetMouseHandler(NULL);
menu_visible_ = true;
bool destroyed = false;
destroyed_flag_ = &destroyed;
menu_delegate_->RunMenu(this, menu_position);
if (destroyed) {
// The menu was deleted while showing. Don't attempt any processing.
return false;
}
destroyed_flag_ = NULL;
menu_visible_ = false;
menu_closed_time_ = Time::Now();
// Now that the menu has closed, we need to manually reset state to
// "normal" since the menu modal loop will have prevented normal
// mouse move messages from getting to this View. We set "normal"
// and not "hot" because the likelihood is that the mouse is now
// somewhere else (user clicked elsewhere on screen to close the menu
// or selected an item) and we will inevitably refresh the hot state
// in the event the mouse _is_ over the view.
SetState(BS_NORMAL);
// We must return false here so that the RootView does not get stuck
// sending all mouse pressed events to us instead of the appropriate
// target.
return false;
}
return true;
}
bool MenuButton::OnMousePressed(const MouseEvent& e) {
RequestFocus();
if (state() != BS_DISABLED) {
// If we're draggable (GetDragOperations returns a non-zero value), then
// don't pop on press, instead wait for release.
if (e.IsOnlyLeftMouseButton() && HitTest(e.location()) &&
GetDragOperations(e.location()) == DragDropTypes::DRAG_NONE) {
TimeDelta delta = Time::Now() - menu_closed_time_;
int64 delta_in_milliseconds = delta.InMilliseconds();
if (delta_in_milliseconds > kMinimumTimeBetweenButtonClicks) {
return Activate();
}
}
}
return true;
}
void MenuButton::OnMouseReleased(const MouseEvent& e,
bool canceled) {
// Explicitly test for left mouse button to show the menu. If we tested for
// !IsTriggerableEvent it could lead to a situation where we end up showing
// the menu and context menu (this would happen if the right button is not
// triggerable and there's a context menu).
if (GetDragOperations(e.location()) != DragDropTypes::DRAG_NONE &&
state() != BS_DISABLED && !canceled && !InDrag() &&
e.IsOnlyLeftMouseButton() && HitTest(e.location())) {
Activate();
} else {
TextButton::OnMouseReleased(e, canceled);
}
}
bool MenuButton::OnKeyPressed(const KeyEvent& e) {
if (e.GetKeyCode() == app::VKEY_SPACE ||
e.GetKeyCode() == app::VKEY_RETURN ||
e.GetKeyCode() == app::VKEY_UP ||
e.GetKeyCode() == app::VKEY_DOWN) {
bool result = Activate();
if (GetFocusManager()->GetFocusedView() == NULL)
RequestFocus();
return result;
}
return false;
}
bool MenuButton::OnKeyReleased(const KeyEvent& e) {
// Override CustomButton's implementation, which presses the button when
// you press space and clicks it when you release space. For a MenuButton
// we always activate the menu on key press.
return false;
}
// The reason we override View::OnMouseExited is because we get this event when
// we display the menu. If we don't override this method then
// BaseButton::OnMouseExited will get the event and will set the button's state
// to BS_NORMAL instead of keeping the state BM_PUSHED. This, in turn, will
// cause the button to appear depressed while the menu is displayed.
void MenuButton::OnMouseExited(const MouseEvent& event) {
if ((state_ != BS_DISABLED) && (!menu_visible_) && (!InDrag())) {
SetState(BS_NORMAL);
}
}
////////////////////////////////////////////////////////////////////////////////
//
// MenuButton - accessibility
//
////////////////////////////////////////////////////////////////////////////////
bool MenuButton::GetAccessibleDefaultAction(std::wstring* action) {
DCHECK(action);
action->assign(l10n_util::GetString(IDS_APP_ACCACTION_PRESS));
return true;
}
bool MenuButton::GetAccessibleRole(AccessibilityTypes::Role* role) {
DCHECK(role);
*role = AccessibilityTypes::ROLE_BUTTONMENU;
return true;
}
bool MenuButton::GetAccessibleState(AccessibilityTypes::State* state) {
DCHECK(state);
*state = AccessibilityTypes::STATE_HASPOPUP;
return true;
}
std::string MenuButton::GetClassName() const {
return kViewClassName;
}
} // namespace views
|