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 an alternative to TextButton, 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 virtual ~LabelButton();
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 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 // Get or set the text's multi-line property to break on '\n', etc.
47 bool GetTextMultiLine() const;
48 void SetTextMultiLine(bool text_multi_line);
50 // Get or set the font list used by this button.
51 const gfx::FontList& GetFontList() const;
52 void SetFontList(const gfx::FontList& font_list);
54 // Set the elide behavior of this button.
55 void SetElideBehavior(Label::ElideBehavior elide_behavior);
57 // Get or set the horizontal alignment used for the button; reversed in RTL.
58 // The optional image will lead the text, unless the button is right-aligned.
59 gfx::HorizontalAlignment GetHorizontalAlignment() const;
60 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
62 // Call set_min_size(gfx::Size()) to clear the monotonically increasing size.
63 void set_min_size(const gfx::Size& min_size) { min_size_ = min_size; }
64 void set_max_size(const gfx::Size& max_size) { max_size_ = max_size; }
66 // Get or set the option to handle the return key; false by default.
67 bool is_default() const { return is_default_; }
68 void SetIsDefault(bool is_default);
70 // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|.
71 ButtonStyle style() const { return style_; }
72 void SetStyle(ButtonStyle style);
74 void SetFocusPainter(scoped_ptr<Painter> focus_painter);
77 virtual gfx::Size GetPreferredSize() OVERRIDE;
78 virtual void Layout() OVERRIDE;
79 virtual const char* GetClassName() const OVERRIDE;
82 ImageView* image() const { return image_; }
83 Label* label() const { return label_; }
86 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
87 virtual void OnFocus() OVERRIDE;
88 virtual void OnBlur() OVERRIDE;
90 // Fill |params| with information about the button.
91 virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
93 // Resets colors from the NativeTheme, explicitly set colors are unchanged.
94 virtual void ResetColorsFromNativeTheme();
96 // Updates the image view to contain the appropriate button state image.
99 // Updates our border with a specific Border instance which has different
100 // insets, etc. This may wrap the border in an object which will draw a
101 // native style border.
102 void UpdateThemedBorder(scoped_ptr<Border> border);
104 // NativeThemeDelegate:
105 virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
108 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init);
109 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Label);
110 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Image);
111 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage);
112 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, FontList);
115 virtual void StateChanged() OVERRIDE;
118 virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
119 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
121 // NativeThemeDelegate:
122 virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
123 virtual ui::NativeTheme::State GetThemeState(
124 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
125 virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
126 virtual ui::NativeTheme::State GetBackgroundThemeState(
127 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
128 virtual ui::NativeTheme::State GetForegroundThemeState(
129 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
131 // The image and label shown in the button.
135 // The images and colors for each button state.
136 gfx::ImageSkia button_state_images_[STATE_COUNT];
137 SkColor button_state_colors_[STATE_COUNT];
139 // Used to track whether SetTextColor() has been invoked.
140 bool explicitly_set_colors_[STATE_COUNT];
142 // |min_size_| increases monotonically with the preferred size.
144 // |max_size_| may be set to clamp the preferred size.
147 // Flag indicating default handling of the return key via an accelerator.
148 // Whether or not the button appears or behaves as the default button in its
152 // The button's overall style.
155 scoped_ptr<Painter> focus_painter_;
157 DISALLOW_COPY_AND_ASSIGN(LabelButton);
162 #endif // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_