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. 4 5 #ifndef UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ 6 #define UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ 7 8 #include <string> 9 10 #include "base/strings/string16.h" 11 #include "base/time/time.h" 12 #include "ui/gfx/font.h" 13 #include "ui/views/background.h" 14 #include "ui/views/controls/button/text_button.h" 15 16 namespace views { 17 18 class MenuButtonListener; 19 20 //////////////////////////////////////////////////////////////////////////////// 21 // 22 // MenuButton 23 // 24 // A button that shows a menu when the left mouse button is pushed 25 // 26 //////////////////////////////////////////////////////////////////////////////// 27 class VIEWS_EXPORT MenuButton : public TextButton { 28 public: 29 static const char kViewClassName[]; 30 31 // The amount of time, in milliseconds, we wait before allowing another mouse 32 // pressed event to show the menu. 33 static const int64 kMinimumTimeBetweenButtonClicks; 34 35 // How much padding to put on the left and right of the menu marker. 36 static const int kMenuMarkerPaddingLeft; 37 static const int kMenuMarkerPaddingRight; 38 39 // Create a Button. 40 MenuButton(ButtonListener* listener, 41 const string16& text, 42 MenuButtonListener* menu_button_listener, 43 bool show_menu_marker); 44 virtual ~MenuButton(); 45 46 bool show_menu_marker() const { return show_menu_marker_; } 47 void set_menu_marker(const gfx::ImageSkia* menu_marker) { 48 menu_marker_ = menu_marker; 49 } 50 const gfx::ImageSkia* menu_marker() const { return menu_marker_; } 51 52 const gfx::Point& menu_offset() const { return menu_offset_; } 53 void set_menu_offset(int x, int y) { menu_offset_.SetPoint(x, y); } 54 55 // Activate the button (called when the button is pressed). 56 virtual bool Activate(); 57 58 // Overridden from TextButton for the potential use of a drop marker. 59 virtual void PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) OVERRIDE; 60 61 // Overridden from View: 62 virtual gfx::Size GetPreferredSize() OVERRIDE; 63 virtual const char* GetClassName() const OVERRIDE; 64 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 65 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 66 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; 67 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 68 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; 69 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; 70 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 71 72 protected: 73 // True if the menu is currently visible. 74 bool menu_visible_; 75 76 // Offset of the associated menu position. 77 gfx::Point menu_offset_; 78 79 private: 80 friend class TextButtonBackground; 81 82 // Compute the maximum X coordinate for the current screen. MenuButtons 83 // use this to make sure a menu is never shown off screen. 84 int GetMaximumScreenXCoordinate(); 85 86 // We use a time object in order to keep track of when the menu was closed. 87 // The time is used for simulating menu behavior for the menu button; that 88 // is, if the menu is shown and the button is pressed, we need to close the 89 // menu. There is no clean way to get the second click event because the 90 // menu is displayed using a modal loop and, unlike regular menus in Windows, 91 // the button is not part of the displayed menu. 92 base::Time menu_closed_time_; 93 94 // Our listener. Not owned. 95 MenuButtonListener* listener_; 96 97 // Whether or not we're showing a drop marker. 98 bool show_menu_marker_; 99 100 // The down arrow used to differentiate the menu button from normal 101 // text buttons. 102 const gfx::ImageSkia* menu_marker_; 103 104 // If non-null the destuctor sets this to true. This is set while the menu is 105 // showing and used to detect if the menu was deleted while running. 106 bool* destroyed_flag_; 107 108 DISALLOW_COPY_AND_ASSIGN(MenuButton); 109 }; 110 111 } // namespace views 112 113 #endif // UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ 114