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;
91 // Offset of the associated menu position.
92 gfx::Point menu_offset_;
95 friend class PressedLock;
97 // Increment/decrement the number of "pressed" locks this button has, and
98 // set the state accordingly.
99 void IncrementPressedLocked();
100 void DecrementPressedLocked();
102 // Compute the maximum X coordinate for the current screen. MenuButtons
103 // use this to make sure a menu is never shown off screen.
104 int GetMaximumScreenXCoordinate();
106 // We use a time object in order to keep track of when the menu was closed.
107 // The time is used for simulating menu behavior for the menu button; that
108 // is, if the menu is shown and the button is pressed, we need to close the
109 // menu. There is no clean way to get the second click event because the
110 // menu is displayed using a modal loop and, unlike regular menus in Windows,
111 // the button is not part of the displayed menu.
112 base::TimeTicks menu_closed_time_;
114 // Our listener. Not owned.
115 MenuButtonListener* listener_;
117 // Whether or not we're showing a drop marker.
118 bool show_menu_marker_;
120 // The down arrow used to differentiate the menu button from normal buttons.
121 const gfx::ImageSkia* menu_marker_;
123 // If non-null the destuctor sets this to true. This is set while the menu is
124 // showing and used to detect if the menu was deleted while running.
125 bool* destroyed_flag_;
127 // The current number of "pressed" locks this button has.
128 int pressed_lock_count_;
130 base::WeakPtrFactory<MenuButton> weak_factory_;
132 DISALLOW_COPY_AND_ASSIGN(MenuButton);
137 #endif // UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_