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
17namespace views {
18
19class LabelButtonBorder;
20class Painter;
21
22// LabelButton is an alternative to TextButton, it's not focusable by default.
23class 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  // Set the directionality mode used for the button text.
69  void SetDirectionalityMode(gfx::DirectionalityMode mode);
70
71  // Call set_min_size(gfx::Size()) to clear the monotonically increasing size.
72  void set_min_size(const gfx::Size& min_size) { min_size_ = min_size; }
73  void set_max_size(const gfx::Size& max_size) { max_size_ = max_size; }
74
75  // Get or set the option to handle the return key; false by default.
76  bool is_default() const { return is_default_; }
77  void SetIsDefault(bool is_default);
78
79  // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|.
80  ButtonStyle style() const { return style_; }
81  void SetStyle(ButtonStyle style);
82
83  void SetFocusPainter(scoped_ptr<Painter> focus_painter);
84  Painter* focus_painter() { return focus_painter_.get(); }
85
86  // View:
87  virtual void SetBorder(scoped_ptr<Border> border) OVERRIDE;
88  virtual gfx::Size GetPreferredSize() const OVERRIDE;
89  virtual void Layout() OVERRIDE;
90  virtual const char* GetClassName() const OVERRIDE;
91
92 protected:
93  ImageView* image() const { return image_; }
94  Label* label() const { return label_; }
95
96  // Returns the available area for the label and image. Subclasses can change
97  // these bounds if they need room to do manual painting.
98  virtual gfx::Rect GetChildAreaBounds();
99
100  // View:
101  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
102  virtual void OnFocus() OVERRIDE;
103  virtual void OnBlur() OVERRIDE;
104  virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
105
106  // Fill |params| with information about the button.
107  virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
108
109  // Resets colors from the NativeTheme, explicitly set colors are unchanged.
110  virtual void ResetColorsFromNativeTheme();
111
112  // Creates the default border for this button. This can be overridden by
113  // subclasses or by LinuxUI.
114  virtual scoped_ptr<LabelButtonBorder> CreateDefaultBorder() const;
115
116  // Updates the image view to contain the appropriate button state image.
117  void UpdateImage();
118
119  // Updates the border as per the NativeTheme, unless a different border was
120  // set with SetBorder.
121  void UpdateThemedBorder();
122
123  // NativeThemeDelegate:
124  virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
125
126 private:
127  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init);
128  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Label);
129  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Image);
130  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage);
131  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, FontList);
132
133  // CustomButton:
134  virtual void StateChanged() OVERRIDE;
135
136  // View:
137  virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
138
139  // NativeThemeDelegate:
140  virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
141  virtual ui::NativeTheme::State GetThemeState(
142      ui::NativeTheme::ExtraParams* params) const OVERRIDE;
143  virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
144  virtual ui::NativeTheme::State GetBackgroundThemeState(
145      ui::NativeTheme::ExtraParams* params) const OVERRIDE;
146  virtual ui::NativeTheme::State GetForegroundThemeState(
147      ui::NativeTheme::ExtraParams* params) const OVERRIDE;
148
149  // The image and label shown in the button.
150  ImageView* image_;
151  Label* label_;
152
153  // The cached font lists in the normal and bold style.
154  gfx::FontList cached_normal_font_list_;
155  gfx::FontList cached_bold_font_list_;
156
157  // The images and colors for each button state.
158  gfx::ImageSkia button_state_images_[STATE_COUNT];
159  SkColor button_state_colors_[STATE_COUNT];
160
161  // Used to track whether SetTextColor() has been invoked.
162  bool explicitly_set_colors_[STATE_COUNT];
163
164  // |min_size_| increases monotonically with the preferred size.
165  mutable gfx::Size min_size_;
166  // |max_size_| may be set to clamp the preferred size.
167  gfx::Size max_size_;
168
169  // Flag indicating default handling of the return key via an accelerator.
170  // Whether or not the button appears or behaves as the default button in its
171  // current context;
172  bool is_default_;
173
174  // The button's overall style.
175  ButtonStyle style_;
176
177  // True if current border was set by UpdateThemedBorder. Defaults to true.
178  bool border_is_themed_border_;
179
180  scoped_ptr<Painter> focus_painter_;
181
182  DISALLOW_COPY_AND_ASSIGN(LabelButton);
183};
184
185}  // namespace views
186
187#endif  // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
188