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_LABEL_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
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"
19 class LabelButtonBorder
;
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
{
26 // The length of the hover fade animation.
27 static const int kHoverAnimationDurationMs
;
29 static const char kViewClassName
[];
31 LabelButton(ButtonListener
* listener
, const base::string16
& text
);
32 ~LabelButton() override
;
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
);
39 // Get or set the text shown on the button.
40 const base::string16
& GetText() const;
41 virtual void SetText(const base::string16
& text
);
43 // Set the text color shown for the specified button state.
44 void SetTextColor(ButtonState for_state
, SkColor color
);
46 // Set drop shadows underneath the text.
47 void SetTextShadows(const gfx::ShadowValues
& shadows
);
49 // Sets whether subpixel rendering is used on the label.
50 void SetTextSubpixelRenderingEnabled(bool enabled
);
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
);
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
);
60 // Set the elide behavior of this button.
61 void SetElideBehavior(gfx::ElideBehavior elide_behavior
);
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
);
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
);
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
);
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
);
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
);
85 void SetFocusPainter(scoped_ptr
<Painter
> focus_painter
);
86 Painter
* focus_painter() { return focus_painter_
.get(); }
89 void SetBorder(scoped_ptr
<Border
> border
) override
;
90 gfx::Size
GetPreferredSize() const override
;
91 int GetHeightForWidth(int w
) const override
;
92 void Layout() override
;
93 const char* GetClassName() const override
;
96 ImageView
* image() const { return image_
; }
97 Label
* label() const { return label_
; }
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();
104 void OnPaint(gfx::Canvas
* canvas
) override
;
105 void OnFocus() override
;
106 void OnBlur() override
;
107 void OnNativeThemeChanged(const ui::NativeTheme
* theme
) override
;
109 // Fill |params| with information about the button.
110 virtual void GetExtraParams(ui::NativeTheme::ExtraParams
* params
) const;
112 // Resets colors from the NativeTheme, explicitly set colors are unchanged.
113 virtual void ResetColorsFromNativeTheme();
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;
119 // Updates the image view to contain the appropriate button state image.
122 // Updates the border as per the NativeTheme, unless a different border was
123 // set with SetBorder.
124 void UpdateThemedBorder();
126 // NativeThemeDelegate:
127 gfx::Rect
GetThemePaintRect() const override
;
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
);
137 void StateChanged() override
;
140 void ChildPreferredSizeChanged(View
* child
) override
;
142 // NativeThemeDelegate:
143 ui::NativeTheme::Part
GetThemePart() const override
;
144 ui::NativeTheme::State
GetThemeState(
145 ui::NativeTheme::ExtraParams
* params
) const override
;
146 const gfx::Animation
* GetThemeAnimation() const override
;
147 ui::NativeTheme::State
GetBackgroundThemeState(
148 ui::NativeTheme::ExtraParams
* params
) const override
;
149 ui::NativeTheme::State
GetForegroundThemeState(
150 ui::NativeTheme::ExtraParams
* params
) const override
;
152 // Resets |cached_preferred_size_| and marks |cached_preferred_size_valid_|
154 void ResetCachedPreferredSize();
156 // The image and label shown in the button.
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_
;
164 // The images and colors for each button state.
165 gfx::ImageSkia button_state_images_
[STATE_COUNT
];
166 SkColor button_state_colors_
[STATE_COUNT
];
168 // Used to track whether SetTextColor() has been invoked.
169 bool explicitly_set_colors_
[STATE_COUNT
];
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.
176 // Cache the last computed preferred size.
177 mutable gfx::Size cached_preferred_size_
;
178 mutable bool cached_preferred_size_valid_
;
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
185 // The button's overall style.
188 // True if current border was set by UpdateThemedBorder. Defaults to true.
189 bool border_is_themed_border_
;
191 // Spacing between the image and the text.
192 int image_label_spacing_
;
194 scoped_ptr
<Painter
> focus_painter_
;
196 DISALLOW_COPY_AND_ASSIGN(LabelButton
);
201 #endif // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_