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_LABEL_BUTTON_H_
      6 #define UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "third_party/skia/include/core/SkColor.h"
     11 #include "ui/gfx/image/image_skia.h"
     12 #include "ui/views/controls/button/custom_button.h"
     13 #include "ui/views/controls/image_view.h"
     14 #include "ui/views/controls/label.h"
     15 #include "ui/views/native_theme_delegate.h"
     16 
     17 namespace views {
     18 
     19 class LabelButtonBorder;
     20 class Painter;
     21 
     22 // LabelButton is a button with text and an icon, it's not focusable by default.
     23 class VIEWS_EXPORT LabelButton : public CustomButton,
     24                                  public NativeThemeDelegate {
     25  public:
     26   // The length of the hover fade animation.
     27   static const int kHoverAnimationDurationMs;
     28 
     29   static const char kViewClassName[];
     30 
     31   LabelButton(ButtonListener* listener, const base::string16& text);
     32   virtual ~LabelButton();
     33 
     34   // Get or set the image shown for the specified button state.
     35   // GetImage returns the image for STATE_NORMAL if the state's image is empty.
     36   virtual const gfx::ImageSkia& GetImage(ButtonState for_state);
     37   void SetImage(ButtonState for_state, const gfx::ImageSkia& image);
     38 
     39   // Get or set the text shown on the button.
     40   const base::string16& GetText() const;
     41   virtual void SetText(const base::string16& text);
     42 
     43   // Set the text color shown for the specified button state.
     44   void SetTextColor(ButtonState for_state, SkColor color);
     45 
     46   // Set drop shadows underneath the text.
     47   void SetTextShadows(const gfx::ShadowValues& shadows);
     48 
     49   // Sets whether subpixel rendering is used on the label.
     50   void SetTextSubpixelRenderingEnabled(bool enabled);
     51 
     52   // Get or set the text's multi-line property to break on '\n', etc.
     53   bool GetTextMultiLine() const;
     54   void SetTextMultiLine(bool text_multi_line);
     55 
     56   // Get or set the font list used by this button.
     57   const gfx::FontList& GetFontList() const;
     58   void SetFontList(const gfx::FontList& font_list);
     59 
     60   // Set the elide behavior of this button.
     61   void SetElideBehavior(gfx::ElideBehavior elide_behavior);
     62 
     63   // Get or set the horizontal alignment used for the button; reversed in RTL.
     64   // The optional image will lead the text, unless the button is right-aligned.
     65   gfx::HorizontalAlignment GetHorizontalAlignment() const;
     66   void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
     67 
     68   // Call SetMinSize(gfx::Size()) to clear the monotonically increasing size.
     69   void SetMinSize(const gfx::Size& min_size);
     70   void SetMaxSize(const gfx::Size& max_size);
     71 
     72   // Get or set the option to handle the return key; false by default.
     73   bool is_default() const { return is_default_; }
     74   void SetIsDefault(bool is_default);
     75 
     76   // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|.
     77   ButtonStyle style() const { return style_; }
     78   void SetStyle(ButtonStyle style);
     79 
     80   // Set the spacing between the image and the text. Shrinking the spacing
     81   // will not shrink the overall button size, as it is monotonically increasing.
     82   // Call SetMinSize(gfx::Size()) to clear the size if needed.
     83   void SetImageLabelSpacing(int spacing);
     84 
     85   void SetFocusPainter(scoped_ptr<Painter> focus_painter);
     86   Painter* focus_painter() { return focus_painter_.get(); }
     87 
     88   // View:
     89   virtual void SetBorder(scoped_ptr<Border> border) OVERRIDE;
     90   virtual gfx::Size GetPreferredSize() const OVERRIDE;
     91   virtual int GetHeightForWidth(int w) const OVERRIDE;
     92   virtual void Layout() OVERRIDE;
     93   virtual const char* GetClassName() const OVERRIDE;
     94 
     95  protected:
     96   ImageView* image() const { return image_; }
     97   Label* label() const { return label_; }
     98 
     99   // Returns the available area for the label and image. Subclasses can change
    100   // these bounds if they need room to do manual painting.
    101   virtual gfx::Rect GetChildAreaBounds();
    102 
    103   // View:
    104   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
    105   virtual void OnFocus() OVERRIDE;
    106   virtual void OnBlur() OVERRIDE;
    107   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
    108 
    109   // Fill |params| with information about the button.
    110   virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
    111 
    112   // Resets colors from the NativeTheme, explicitly set colors are unchanged.
    113   virtual void ResetColorsFromNativeTheme();
    114 
    115   // Creates the default border for this button. This can be overridden by
    116   // subclasses or by LinuxUI.
    117   virtual scoped_ptr<LabelButtonBorder> CreateDefaultBorder() const;
    118 
    119   // Updates the image view to contain the appropriate button state image.
    120   void UpdateImage();
    121 
    122   // Updates the border as per the NativeTheme, unless a different border was
    123   // set with SetBorder.
    124   void UpdateThemedBorder();
    125 
    126   // NativeThemeDelegate:
    127   virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
    128 
    129  private:
    130   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init);
    131   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Label);
    132   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Image);
    133   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage);
    134   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, FontList);
    135 
    136   // CustomButton:
    137   virtual void StateChanged() OVERRIDE;
    138 
    139   // View:
    140   virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
    141 
    142   // NativeThemeDelegate:
    143   virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
    144   virtual ui::NativeTheme::State GetThemeState(
    145       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
    146   virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
    147   virtual ui::NativeTheme::State GetBackgroundThemeState(
    148       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
    149   virtual ui::NativeTheme::State GetForegroundThemeState(
    150       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
    151 
    152   // Resets |cached_preferred_size_| and marks |cached_preferred_size_valid_|
    153   // as false.
    154   void ResetCachedPreferredSize();
    155 
    156   // The image and label shown in the button.
    157   ImageView* image_;
    158   Label* label_;
    159 
    160   // The cached font lists in the normal and bold style.
    161   gfx::FontList cached_normal_font_list_;
    162   gfx::FontList cached_bold_font_list_;
    163 
    164   // The images and colors for each button state.
    165   gfx::ImageSkia button_state_images_[STATE_COUNT];
    166   SkColor button_state_colors_[STATE_COUNT];
    167 
    168   // Used to track whether SetTextColor() has been invoked.
    169   bool explicitly_set_colors_[STATE_COUNT];
    170 
    171   // |min_size_| increases monotonically with the preferred size.
    172   mutable gfx::Size min_size_;
    173   // |max_size_| may be set to clamp the preferred size.
    174   gfx::Size max_size_;
    175 
    176   // Cache the last computed preferred size.
    177   mutable gfx::Size cached_preferred_size_;
    178   mutable bool cached_preferred_size_valid_;
    179 
    180   // Flag indicating default handling of the return key via an accelerator.
    181   // Whether or not the button appears or behaves as the default button in its
    182   // current context;
    183   bool is_default_;
    184 
    185   // The button's overall style.
    186   ButtonStyle style_;
    187 
    188   // True if current border was set by UpdateThemedBorder. Defaults to true.
    189   bool border_is_themed_border_;
    190 
    191   // Spacing between the image and the text.
    192   int image_label_spacing_;
    193 
    194   scoped_ptr<Painter> focus_painter_;
    195 
    196   DISALLOW_COPY_AND_ASSIGN(LabelButton);
    197 };
    198 
    199 }  // namespace views
    200 
    201 #endif  // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
    202