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_min_width(const int min_width) { min_size_.set_width(min_width); }
65 void set_max_size(const gfx::Size& max_size) { max_size_ = max_size; }
67 // Get or set the option to handle the return key; false by default.
68 bool is_default() const { return is_default_; }
69 void SetIsDefault(bool is_default);
71 // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|.
72 ButtonStyle style() const { return style_; }
73 void SetStyle(ButtonStyle style);
75 void SetFocusPainter(scoped_ptr<Painter> focus_painter);
78 virtual gfx::Size GetPreferredSize() OVERRIDE;
79 virtual void Layout() OVERRIDE;
80 virtual const char* GetClassName() const OVERRIDE;
83 ImageView* image() const { return image_; }
84 Label* label() const { return label_; }
87 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
88 virtual void OnFocus() OVERRIDE;
89 virtual void OnBlur() OVERRIDE;
91 // Fill |params| with information about the button.
92 virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
94 // Resets colors from the NativeTheme, explicitly set colors are unchanged.
95 virtual void ResetColorsFromNativeTheme();
97 // Updates the image view to contain the appropriate button state image.
100 // Updates our border with a specific Border instance which has different
101 // insets, etc. This may wrap the border in an object which will draw a
102 // native style border.
103 void UpdateThemedBorder(scoped_ptr<Border> border);
105 // NativeThemeDelegate:
106 virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
109 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init);
110 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Label);
111 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Image);
112 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage);
113 FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, FontList);
116 virtual void StateChanged() OVERRIDE;
119 virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
120 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
122 // NativeThemeDelegate:
123 virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
124 virtual ui::NativeTheme::State GetThemeState(
125 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
126 virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
127 virtual ui::NativeTheme::State GetBackgroundThemeState(
128 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
129 virtual ui::NativeTheme::State GetForegroundThemeState(
130 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
132 // The image and label shown in the button.
136 // The cached font lists in the normal and bold style.
137 gfx::FontList cached_normal_font_list_;
138 gfx::FontList cached_bold_font_list_;
140 // The images and colors for each button state.
141 gfx::ImageSkia button_state_images_[STATE_COUNT];
142 SkColor button_state_colors_[STATE_COUNT];
144 // Used to track whether SetTextColor() has been invoked.
145 bool explicitly_set_colors_[STATE_COUNT];
147 // |min_size_| increases monotonically with the preferred size.
149 // |max_size_| may be set to clamp the preferred size.
152 // Flag indicating default handling of the return key via an accelerator.
153 // Whether or not the button appears or behaves as the default button in its
157 // The button's overall style.
160 scoped_ptr<Painter> focus_painter_;
162 DISALLOW_COPY_AND_ASSIGN(LabelButton);
167 #endif // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_