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 #ifndef UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "ui/views/background.h"
14 #include "ui/views/controls/button/label_button.h"
18 class MenuButtonListener
;
20 ////////////////////////////////////////////////////////////////////////////////
24 // A button that shows a menu when the left mouse button is pushed
26 ////////////////////////////////////////////////////////////////////////////////
27 class VIEWS_EXPORT MenuButton
: public LabelButton
{
29 // A scoped lock for keeping the MenuButton in STATE_PRESSED e.g., while a
30 // menu is running. These are cumulative.
31 class VIEWS_EXPORT PressedLock
{
33 explicit PressedLock(MenuButton
* menu_button
);
37 base::WeakPtr
<MenuButton
> menu_button_
;
39 DISALLOW_COPY_AND_ASSIGN(PressedLock
);
42 static const char kViewClassName
[];
44 // How much padding to put on the left and right of the menu marker.
45 static const int kMenuMarkerPaddingLeft
;
46 static const int kMenuMarkerPaddingRight
;
49 MenuButton(ButtonListener
* listener
,
50 const base::string16
& text
,
51 MenuButtonListener
* menu_button_listener
,
52 bool show_menu_marker
);
53 ~MenuButton() override
;
55 bool show_menu_marker() const { return show_menu_marker_
; }
56 void set_menu_marker(const gfx::ImageSkia
* menu_marker
) {
57 menu_marker_
= menu_marker
;
59 const gfx::ImageSkia
* menu_marker() const { return menu_marker_
; }
61 const gfx::Point
& menu_offset() const { return menu_offset_
; }
62 void set_menu_offset(int x
, int y
) { menu_offset_
.SetPoint(x
, y
); }
64 // Activate the button (called when the button is pressed).
65 virtual bool Activate();
67 // Overridden from View:
68 gfx::Size
GetPreferredSize() const override
;
69 const char* GetClassName() const override
;
70 void OnPaint(gfx::Canvas
* canvas
) override
;
71 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
72 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
73 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
74 void OnMouseExited(const ui::MouseEvent
& event
) override
;
75 void OnMouseMoved(const ui::MouseEvent
& event
) override
;
76 void OnGestureEvent(ui::GestureEvent
* event
) override
;
77 bool OnKeyPressed(const ui::KeyEvent
& event
) override
;
78 bool OnKeyReleased(const ui::KeyEvent
& event
) override
;
79 void GetAccessibleState(ui::AXViewState
* state
) override
;
82 // Paint the menu marker image.
83 void PaintMenuMarker(gfx::Canvas
* canvas
);
85 // Overridden from LabelButton:
86 gfx::Rect
GetChildAreaBounds() override
;
88 // Overridden from CustomButton:
89 bool ShouldEnterPushedState(const ui::Event
& event
) override
;
90 void StateChanged() override
;
92 // Offset of the associated menu position.
93 gfx::Point menu_offset_
;
96 friend class PressedLock
;
98 // Increment/decrement the number of "pressed" locks this button has, and
99 // set the state accordingly.
100 void IncrementPressedLocked();
101 void DecrementPressedLocked();
103 // Compute the maximum X coordinate for the current screen. MenuButtons
104 // use this to make sure a menu is never shown off screen.
105 int GetMaximumScreenXCoordinate();
107 // We use a time object in order to keep track of when the menu was closed.
108 // The time is used for simulating menu behavior for the menu button; that
109 // is, if the menu is shown and the button is pressed, we need to close the
110 // menu. There is no clean way to get the second click event because the
111 // menu is displayed using a modal loop and, unlike regular menus in Windows,
112 // the button is not part of the displayed menu.
113 base::TimeTicks menu_closed_time_
;
115 // Our listener. Not owned.
116 MenuButtonListener
* listener_
;
118 // Whether or not we're showing a drop marker.
119 bool show_menu_marker_
;
121 // The down arrow used to differentiate the menu button from normal buttons.
122 const gfx::ImageSkia
* menu_marker_
;
124 // If non-null the destuctor sets this to true. This is set while the menu is
125 // showing and used to detect if the menu was deleted while running.
126 bool* destroyed_flag_
;
128 // The current number of "pressed" locks this button has.
129 int pressed_lock_count_
;
131 // True if the button was in a disabled state when a menu was run, and should
132 // return to it once the press is complete. This can happen if, e.g., we
133 // programmatically show a menu on a disabled button.
134 bool should_disable_after_press_
;
136 base::WeakPtrFactory
<MenuButton
> weak_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(MenuButton
);
143 #endif // UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_