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/strings/string16.h"
11 #include "base/time/time.h"
12 #include "ui/views/background.h"
13 #include "ui/views/controls/button/label_button.h"
17 class MenuButtonListener
;
19 ////////////////////////////////////////////////////////////////////////////////
23 // A button that shows a menu when the left mouse button is pushed
25 ////////////////////////////////////////////////////////////////////////////////
26 class VIEWS_EXPORT MenuButton
: public LabelButton
{
28 static const char kViewClassName
[];
30 // How much padding to put on the left and right of the menu marker.
31 static const int kMenuMarkerPaddingLeft
;
32 static const int kMenuMarkerPaddingRight
;
35 MenuButton(ButtonListener
* listener
,
36 const base::string16
& text
,
37 MenuButtonListener
* menu_button_listener
,
38 bool show_menu_marker
);
39 virtual ~MenuButton();
41 bool show_menu_marker() const { return show_menu_marker_
; }
42 void set_menu_marker(const gfx::ImageSkia
* menu_marker
) {
43 menu_marker_
= menu_marker
;
45 const gfx::ImageSkia
* menu_marker() const { return menu_marker_
; }
47 const gfx::Point
& menu_offset() const { return menu_offset_
; }
48 void set_menu_offset(int x
, int y
) { menu_offset_
.SetPoint(x
, y
); }
50 // Activate the button (called when the button is pressed).
51 virtual bool Activate();
53 // Overridden from View:
54 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
55 virtual const char* GetClassName() const OVERRIDE
;
56 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
57 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
58 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
59 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
60 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
61 virtual bool OnKeyPressed(const ui::KeyEvent
& event
) OVERRIDE
;
62 virtual bool OnKeyReleased(const ui::KeyEvent
& event
) OVERRIDE
;
63 virtual void GetAccessibleState(ui::AXViewState
* state
) OVERRIDE
;
66 // Paint the menu marker image.
67 void PaintMenuMarker(gfx::Canvas
* canvas
);
69 // Overridden from LabelButton:
70 virtual gfx::Rect
GetChildAreaBounds() OVERRIDE
;
72 // True if the menu is currently visible.
75 // Offset of the associated menu position.
76 gfx::Point menu_offset_
;
79 // Compute the maximum X coordinate for the current screen. MenuButtons
80 // use this to make sure a menu is never shown off screen.
81 int GetMaximumScreenXCoordinate();
83 // We use a time object in order to keep track of when the menu was closed.
84 // The time is used for simulating menu behavior for the menu button; that
85 // is, if the menu is shown and the button is pressed, we need to close the
86 // menu. There is no clean way to get the second click event because the
87 // menu is displayed using a modal loop and, unlike regular menus in Windows,
88 // the button is not part of the displayed menu.
89 base::TimeTicks menu_closed_time_
;
91 // Our listener. Not owned.
92 MenuButtonListener
* listener_
;
94 // Whether or not we're showing a drop marker.
95 bool show_menu_marker_
;
97 // The down arrow used to differentiate the menu button from normal buttons.
98 const gfx::ImageSkia
* menu_marker_
;
100 // If non-null the destuctor sets this to true. This is set while the menu is
101 // showing and used to detect if the menu was deleted while running.
102 bool* destroyed_flag_
;
104 DISALLOW_COPY_AND_ASSIGN(MenuButton
);
109 #endif // UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_