Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / button / menu_button.cc
blob614fd5e9b0eef5ee7df3a91ffa91a177ad08cc2d
1 // Copyright (c) 2012 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.
5 #include "ui/views/controls/button/menu_button.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/accessibility/ax_view_state.h"
9 #include "ui/base/dragdrop/drag_drop_types.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/base/ui_base_switches_util.h"
13 #include "ui/events/event.h"
14 #include "ui/events/event_constants.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/gfx/text_constants.h"
19 #include "ui/resources/grit/ui_resources.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/views/controls/button/button.h"
22 #include "ui/views/controls/button/menu_button_listener.h"
23 #include "ui/views/mouse_constants.h"
24 #include "ui/views/resources/grit/views_resources.h"
25 #include "ui/views/widget/root_view.h"
26 #include "ui/views/widget/widget.h"
28 using base::TimeTicks;
29 using base::TimeDelta;
31 namespace views {
33 // Default menu offset.
34 static const int kDefaultMenuOffsetX = -2;
35 static const int kDefaultMenuOffsetY = -4;
37 // static
38 const char MenuButton::kViewClassName[] = "MenuButton";
39 const int MenuButton::kMenuMarkerPaddingLeft = 3;
40 const int MenuButton::kMenuMarkerPaddingRight = -1;
42 ////////////////////////////////////////////////////////////////////////////////
44 // MenuButton::PressedLock
46 ////////////////////////////////////////////////////////////////////////////////
48 MenuButton::PressedLock::PressedLock(MenuButton* menu_button)
49 : menu_button_(menu_button->weak_factory_.GetWeakPtr()) {
50 menu_button_->IncrementPressedLocked();
53 MenuButton::PressedLock::~PressedLock() {
54 if (menu_button_.get())
55 menu_button_->DecrementPressedLocked();
58 ////////////////////////////////////////////////////////////////////////////////
60 // MenuButton - constructors, destructors, initialization
62 ////////////////////////////////////////////////////////////////////////////////
64 MenuButton::MenuButton(ButtonListener* listener,
65 const base::string16& text,
66 MenuButtonListener* menu_button_listener,
67 bool show_menu_marker)
68 : LabelButton(listener, text),
69 menu_offset_(kDefaultMenuOffsetX, kDefaultMenuOffsetY),
70 listener_(menu_button_listener),
71 show_menu_marker_(show_menu_marker),
72 menu_marker_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
73 IDR_MENU_DROPARROW).ToImageSkia()),
74 destroyed_flag_(NULL),
75 pressed_lock_count_(0),
76 should_disable_after_press_(false),
77 weak_factory_(this) {
78 SetHorizontalAlignment(gfx::ALIGN_LEFT);
81 MenuButton::~MenuButton() {
82 if (destroyed_flag_)
83 *destroyed_flag_ = true;
86 ////////////////////////////////////////////////////////////////////////////////
88 // MenuButton - Public APIs
90 ////////////////////////////////////////////////////////////////////////////////
92 bool MenuButton::Activate() {
93 SetState(STATE_PRESSED);
94 if (listener_) {
95 gfx::Rect lb = GetLocalBounds();
97 // The position of the menu depends on whether or not the locale is
98 // right-to-left.
99 gfx::Point menu_position(lb.right(), lb.bottom());
100 if (base::i18n::IsRTL())
101 menu_position.set_x(lb.x());
103 View::ConvertPointToScreen(this, &menu_position);
104 if (base::i18n::IsRTL())
105 menu_position.Offset(-menu_offset_.x(), menu_offset_.y());
106 else
107 menu_position.Offset(menu_offset_.x(), menu_offset_.y());
109 int max_x_coordinate = GetMaximumScreenXCoordinate();
110 if (max_x_coordinate && max_x_coordinate <= menu_position.x())
111 menu_position.set_x(max_x_coordinate - 1);
113 // We're about to show the menu from a mouse press. By showing from the
114 // mouse press event we block RootView in mouse dispatching. This also
115 // appears to cause RootView to get a mouse pressed BEFORE the mouse
116 // release is seen, which means RootView sends us another mouse press no
117 // matter where the user pressed. To force RootView to recalculate the
118 // mouse target during the mouse press we explicitly set the mouse handler
119 // to NULL.
120 static_cast<internal::RootView*>(GetWidget()->GetRootView())->
121 SetMouseHandler(NULL);
123 bool destroyed = false;
124 destroyed_flag_ = &destroyed;
126 // We don't set our state here. It's handled in the MenuController code or
127 // by our click listener.
129 listener_->OnMenuButtonClicked(this, menu_position);
131 if (destroyed) {
132 // The menu was deleted while showing. Don't attempt any processing.
133 return false;
136 destroyed_flag_ = NULL;
138 menu_closed_time_ = TimeTicks::Now();
140 // We must return false here so that the RootView does not get stuck
141 // sending all mouse pressed events to us instead of the appropriate
142 // target.
143 return false;
145 return true;
148 void MenuButton::OnPaint(gfx::Canvas* canvas) {
149 LabelButton::OnPaint(canvas);
151 if (show_menu_marker_)
152 PaintMenuMarker(canvas);
155 ////////////////////////////////////////////////////////////////////////////////
157 // MenuButton - Events
159 ////////////////////////////////////////////////////////////////////////////////
161 gfx::Size MenuButton::GetPreferredSize() const {
162 gfx::Size prefsize = LabelButton::GetPreferredSize();
163 if (show_menu_marker_) {
164 prefsize.Enlarge(menu_marker_->width() + kMenuMarkerPaddingLeft +
165 kMenuMarkerPaddingRight,
168 return prefsize;
171 const char* MenuButton::GetClassName() const {
172 return kViewClassName;
175 bool MenuButton::OnMousePressed(const ui::MouseEvent& event) {
176 RequestFocus();
177 if (state() != STATE_DISABLED && ShouldEnterPushedState(event) &&
178 HitTestPoint(event.location())) {
179 TimeDelta delta = TimeTicks::Now() - menu_closed_time_;
180 if (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)
181 return Activate();
183 return true;
186 void MenuButton::OnMouseReleased(const ui::MouseEvent& event) {
187 if (state() != STATE_DISABLED && ShouldEnterPushedState(event) &&
188 HitTestPoint(event.location()) && !InDrag()) {
189 Activate();
190 } else {
191 LabelButton::OnMouseReleased(event);
195 void MenuButton::OnMouseEntered(const ui::MouseEvent& event) {
196 if (pressed_lock_count_ == 0) // Ignore mouse movement if state is locked.
197 LabelButton::OnMouseEntered(event);
200 void MenuButton::OnMouseExited(const ui::MouseEvent& event) {
201 if (pressed_lock_count_ == 0) // Ignore mouse movement if state is locked.
202 LabelButton::OnMouseExited(event);
205 void MenuButton::OnMouseMoved(const ui::MouseEvent& event) {
206 if (pressed_lock_count_ == 0) // Ignore mouse movement if state is locked.
207 LabelButton::OnMouseMoved(event);
210 void MenuButton::OnGestureEvent(ui::GestureEvent* event) {
211 if (state() != STATE_DISABLED) {
212 if (ShouldEnterPushedState(*event) && !Activate()) {
213 // When |Activate()| returns |false|, it means that a menu is shown and
214 // has handled the gesture event. So, there is no need to further process
215 // the gesture event here.
216 return;
218 if (switches::IsTouchFeedbackEnabled()) {
219 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
220 event->SetHandled();
221 SetState(Button::STATE_HOVERED);
222 } else if (state() == Button::STATE_HOVERED &&
223 (event->type() == ui::ET_GESTURE_TAP_CANCEL ||
224 event->type() == ui::ET_GESTURE_END)) {
225 SetState(Button::STATE_NORMAL);
229 LabelButton::OnGestureEvent(event);
232 bool MenuButton::OnKeyPressed(const ui::KeyEvent& event) {
233 switch (event.key_code()) {
234 case ui::VKEY_SPACE:
235 // Alt-space on windows should show the window menu.
236 if (event.IsAltDown())
237 break;
238 case ui::VKEY_RETURN:
239 case ui::VKEY_UP:
240 case ui::VKEY_DOWN: {
241 // WARNING: we may have been deleted by the time Activate returns.
242 Activate();
243 // This is to prevent the keyboard event from being dispatched twice. If
244 // the keyboard event is not handled, we pass it to the default handler
245 // which dispatches the event back to us causing the menu to get displayed
246 // again. Return true to prevent this.
247 return true;
249 default:
250 break;
252 return false;
255 bool MenuButton::OnKeyReleased(const ui::KeyEvent& event) {
256 // Override CustomButton's implementation, which presses the button when
257 // you press space and clicks it when you release space. For a MenuButton
258 // we always activate the menu on key press.
259 return false;
262 void MenuButton::GetAccessibleState(ui::AXViewState* state) {
263 CustomButton::GetAccessibleState(state);
264 state->role = ui::AX_ROLE_POP_UP_BUTTON;
265 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
266 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
269 void MenuButton::PaintMenuMarker(gfx::Canvas* canvas) {
270 gfx::Insets insets = GetInsets();
272 // Using the Views mirroring infrastructure incorrectly flips icon content.
273 // Instead, manually mirror the position of the down arrow.
274 gfx::Rect arrow_bounds(width() - insets.right() -
275 menu_marker_->width() - kMenuMarkerPaddingRight,
276 height() / 2 - menu_marker_->height() / 2,
277 menu_marker_->width(),
278 menu_marker_->height());
279 arrow_bounds.set_x(GetMirroredXForRect(arrow_bounds));
280 canvas->DrawImageInt(*menu_marker_, arrow_bounds.x(), arrow_bounds.y());
283 gfx::Rect MenuButton::GetChildAreaBounds() {
284 gfx::Size s = size();
286 if (show_menu_marker_) {
287 s.set_width(s.width() - menu_marker_->width() - kMenuMarkerPaddingLeft -
288 kMenuMarkerPaddingRight);
291 return gfx::Rect(s);
294 bool MenuButton::ShouldEnterPushedState(const ui::Event& event) {
295 if (event.IsMouseEvent()) {
296 const ui::MouseEvent& mouseev = static_cast<const ui::MouseEvent&>(event);
297 // Active on left mouse button only, to prevent a menu from being activated
298 // when a right-click would also activate a context menu.
299 if (!mouseev.IsOnlyLeftMouseButton())
300 return false;
301 // If dragging is supported activate on release, otherwise activate on
302 // pressed.
303 ui::EventType active_on =
304 GetDragOperations(mouseev.location()) == ui::DragDropTypes::DRAG_NONE
305 ? ui::ET_MOUSE_PRESSED
306 : ui::ET_MOUSE_RELEASED;
307 return event.type() == active_on;
310 return event.type() == ui::ET_GESTURE_TAP;
313 void MenuButton::StateChanged() {
314 if (pressed_lock_count_ != 0) {
315 // The button's state was changed while it was supposed to be locked in a
316 // pressed state. This shouldn't happen, but conceivably could if a caller
317 // tries to switch from enabled to disabled or vice versa while the button
318 // is pressed.
319 if (state() == STATE_NORMAL)
320 should_disable_after_press_ = false;
321 else if (state() == STATE_DISABLED)
322 should_disable_after_press_ = true;
326 void MenuButton::IncrementPressedLocked() {
327 ++pressed_lock_count_;
328 should_disable_after_press_ = state() == STATE_DISABLED;
329 SetState(STATE_PRESSED);
332 void MenuButton::DecrementPressedLocked() {
333 --pressed_lock_count_;
334 DCHECK_GE(pressed_lock_count_, 0);
336 // If this was the last lock, manually reset state to the desired state.
337 if (pressed_lock_count_ == 0) {
338 ButtonState desired_state = STATE_NORMAL;
339 if (should_disable_after_press_) {
340 desired_state = STATE_DISABLED;
341 should_disable_after_press_ = false;
342 } else if (IsMouseHovered()) {
343 desired_state = STATE_HOVERED;
345 SetState(desired_state);
349 int MenuButton::GetMaximumScreenXCoordinate() {
350 if (!GetWidget()) {
351 NOTREACHED();
352 return 0;
355 gfx::Rect monitor_bounds = GetWidget()->GetWorkAreaBoundsInScreen();
356 return monitor_bounds.right() - 1;
359 } // namespace views