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