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_TEXT_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_TEXT_BUTTON_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/views/border.h"
17 #include "ui/views/controls/button/custom_button.h"
18 #include "ui/views/native_theme_delegate.h"
19 #include "ui/views/painter.h"
23 // A Border subclass for TextButtons that allows configurable insets for the
25 class VIEWS_EXPORT TextButtonBorder : public Border {
28 virtual ~TextButtonBorder();
30 void SetInsets(const gfx::Insets& insets);
33 virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
34 virtual gfx::Insets GetInsets() const OVERRIDE;
35 virtual gfx::Size GetMinimumSize() const OVERRIDE;
40 DISALLOW_COPY_AND_ASSIGN(TextButtonBorder);
44 // A Border subclass that paints a TextButton's background layer -- basically
45 // the button frame in the hot/pushed states.
47 // Note that this type of button is not focusable by default and will not be
48 // part of the focus chain. Call SetFocusable(true) to make it part of the
50 class VIEWS_EXPORT TextButtonDefaultBorder : public TextButtonBorder {
52 TextButtonDefaultBorder();
53 virtual ~TextButtonDefaultBorder();
55 // TextButtonDefaultBorder takes and retains ownership of these |painter|s.
56 void set_normal_painter(Painter* painter) { normal_painter_.reset(painter); }
57 void set_hot_painter(Painter* painter) { hot_painter_.reset(painter); }
58 void set_pushed_painter(Painter* painter) { pushed_painter_.reset(painter); }
62 virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
63 virtual gfx::Size GetMinimumSize() const OVERRIDE;
65 scoped_ptr<Painter> normal_painter_;
66 scoped_ptr<Painter> hot_painter_;
67 scoped_ptr<Painter> pushed_painter_;
69 int vertical_padding_;
71 DISALLOW_COPY_AND_ASSIGN(TextButtonDefaultBorder);
75 // A Border subclass that paints a TextButton's background layer using the
76 // platform's native theme look. This handles normal/disabled/hot/pressed
77 // states, with possible animation between states.
78 class VIEWS_EXPORT TextButtonNativeThemeBorder : public TextButtonBorder {
80 explicit TextButtonNativeThemeBorder(NativeThemeDelegate* delegate);
81 virtual ~TextButtonNativeThemeBorder();
84 virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
85 // We don't override GetMinimumSize(), since there's no easy way to calculate
86 // the minimum size required by the various theme components.
89 // The delegate the controls the appearance of this border.
90 NativeThemeDelegate* delegate_;
92 DISALLOW_COPY_AND_ASSIGN(TextButtonNativeThemeBorder);
96 // A base class for different types of buttons, like push buttons, radio
97 // buttons, and checkboxes, that do not depend on native components for look and
98 // feel. TextButton reserves space for the largest string passed to SetText. To
99 // reset the cached max size invoke ClearMaxTextSize.
100 class VIEWS_EXPORT TextButtonBase : public CustomButton,
101 public NativeThemeDelegate {
103 // The menu button's class name.
104 static const char kViewClassName[];
106 virtual ~TextButtonBase();
108 // Call SetText once per string in your set of possible values at button
109 // creation time, so that it can contain the largest of them and avoid
110 // resizing the button when the text changes.
111 virtual void SetText(const base::string16& text);
112 const base::string16& text() const { return text_; }
120 void set_alignment(TextAlignment alignment) { alignment_ = alignment; }
122 const gfx::Animation* GetAnimation() const;
124 void SetIsDefault(bool is_default);
125 bool is_default() const { return is_default_; }
127 // Set whether the button text can wrap on multiple lines.
129 void SetMultiLine(bool multi_line);
131 // Return whether the button text can wrap on multiple lines.
132 bool multi_line() const { return multi_line_; }
134 // TextButton remembers the maximum display size of the text passed to
135 // SetText. This method resets the cached maximum display size to the
137 void ClearMaxTextSize();
139 void set_min_width(int min_width) { min_width_ = min_width; }
140 void set_min_height(int min_height) { min_height_ = min_height; }
141 void set_max_width(int max_width) { max_width_ = max_width; }
142 const gfx::FontList& font_list() const { return font_list_; }
143 void SetFontList(const gfx::FontList& font_list);
145 void SetEnabledColor(SkColor color);
146 void SetDisabledColor(SkColor color);
147 void SetHighlightColor(SkColor color);
148 void SetHoverColor(SkColor color);
150 // Enables a drop shadow underneath the text.
151 void SetTextShadowColors(SkColor active_color, SkColor inactive_color);
153 // Sets the drop shadow's offset from the text.
154 void SetTextShadowOffset(int x, int y);
157 void ClearEmbellishing();
159 // Sets whether or not to show the hot and pushed states for the button icon
160 // (if present) in addition to the normal state. Defaults to true.
161 bool show_multiple_icon_states() const { return show_multiple_icon_states_; }
162 void SetShowMultipleIconStates(bool show_multiple_icon_states);
164 void SetFocusPainter(scoped_ptr<Painter> focus_painter);
165 Painter* focus_painter() { return focus_painter_.get(); }
167 // Paint the button into the specified canvas. If |mode| is |PB_FOR_DRAG|, the
168 // function paints a drag image representation into the canvas.
169 enum PaintButtonMode { PB_NORMAL, PB_FOR_DRAG };
170 virtual void PaintButton(gfx::Canvas* canvas, PaintButtonMode mode);
172 // Overridden from View:
173 virtual gfx::Size GetPreferredSize() OVERRIDE;
174 virtual gfx::Size GetMinimumSize() OVERRIDE;
175 virtual int GetHeightForWidth(int w) OVERRIDE;
176 virtual void OnEnabledChanged() OVERRIDE;
177 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
178 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
179 virtual const char* GetClassName() const OVERRIDE;
180 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
183 TextButtonBase(ButtonListener* listener, const base::string16& text);
185 // Called when enabled or disabled state changes, or the colors for those
187 virtual void UpdateColor();
189 // Updates text_size_ and max_text_size_ from the current text/font. This is
190 // invoked when the font list or text changes.
191 void UpdateTextSize();
193 // Calculate the size of the text size without setting any of the members.
194 void CalculateTextSize(gfx::Size* text_size, int max_width);
196 void set_color_enabled(SkColor color) { color_enabled_ = color; }
197 void set_color_disabled(SkColor color) { color_disabled_ = color; }
198 void set_color_hover(SkColor color) { color_hover_ = color; }
200 bool use_enabled_color_from_theme() const {
201 return use_enabled_color_from_theme_;
204 bool use_disabled_color_from_theme() const {
205 return use_disabled_color_from_theme_;
208 bool use_hover_color_from_theme() const {
209 return use_hover_color_from_theme_;
212 // Overridden from NativeThemeDelegate:
213 virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
214 virtual ui::NativeTheme::State GetThemeState(
215 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
216 virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
217 virtual ui::NativeTheme::State GetBackgroundThemeState(
218 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
219 virtual ui::NativeTheme::State GetForegroundThemeState(
220 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
222 // Overridden from View:
223 virtual void OnFocus() OVERRIDE;
224 virtual void OnBlur() OVERRIDE;
226 virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
228 virtual gfx::Rect GetTextBounds() const;
230 int ComputeCanvasStringFlags() const;
232 // Calculate the bounds of the content of this button, including any extra
233 // width needed on top of the text width.
234 gfx::Rect GetContentBounds(int extra_width) const;
236 // The text string that is displayed in the button.
237 base::string16 text_;
239 // The size of the text string.
240 gfx::Size text_size_;
242 // Track the size of the largest text string seen so far, so that
243 // changing text_ will not resize the button boundary.
244 gfx::Size max_text_size_;
246 // The alignment of the text string within the button.
247 TextAlignment alignment_;
249 // The font list used to paint the text.
250 gfx::FontList font_list_;
252 // Flag indicating if a shadow should be drawn behind the text.
253 bool has_text_shadow_;
254 // Optional shadow text colors for active and inactive widget states.
255 SkColor active_text_shadow_color_;
256 SkColor inactive_text_shadow_color_;
257 // Space between the text and its shadow. Defaults to (1,1).
258 gfx::Point text_shadow_offset_;
260 // The dimensions of the button will be at least these values.
264 // The width of the button will never be larger than this value. A value <= 0
265 // indicates the width is not constrained.
268 // Whether or not to show the hot and pushed icon states.
269 bool show_multiple_icon_states_;
271 // Whether or not the button appears and behaves as the default button in its
275 // Whether the text button should handle its text string as multi-line.
283 SkColor color_enabled_;
284 SkColor color_disabled_;
285 SkColor color_highlight_;
286 SkColor color_hover_;
288 // True if the specified color should be used from the theme.
289 bool use_enabled_color_from_theme_;
290 bool use_disabled_color_from_theme_;
291 bool use_highlight_color_from_theme_;
292 bool use_hover_color_from_theme_;
294 scoped_ptr<Painter> focus_painter_;
296 DISALLOW_COPY_AND_ASSIGN(TextButtonBase);
300 // A button which displays text and/or and icon that can be changed in response
301 // to actions. TextButton reserves space for the largest string passed to
302 // SetText. To reset the cached max size invoke ClearMaxTextSize.
303 class VIEWS_EXPORT TextButton : public TextButtonBase {
305 // The button's class name.
306 static const char kViewClassName[];
308 TextButton(ButtonListener* listener, const base::string16& text);
309 virtual ~TextButton();
311 void set_icon_text_spacing(int icon_text_spacing) {
312 icon_text_spacing_ = icon_text_spacing;
316 virtual void SetIcon(const gfx::ImageSkia& icon);
317 virtual void SetHoverIcon(const gfx::ImageSkia& icon);
318 virtual void SetPushedIcon(const gfx::ImageSkia& icon);
320 bool HasIcon() const { return !icon_.isNull(); }
322 // Meanings are reversed for right-to-left layouts.
326 ICON_CENTERED // Centered is valid only when text is empty.
329 IconPlacement icon_placement() { return icon_placement_; }
330 void set_icon_placement(IconPlacement icon_placement) {
331 // ICON_CENTERED works only when |text_| is empty.
332 DCHECK((icon_placement != ICON_CENTERED) || text_.empty());
333 icon_placement_ = icon_placement;
336 void set_ignore_minimum_size(bool ignore_minimum_size);
338 void set_full_justification(bool full_justification);
340 // Overridden from View:
341 virtual gfx::Size GetPreferredSize() OVERRIDE;
342 virtual const char* GetClassName() const OVERRIDE;
344 // Overridden from TextButtonBase:
345 virtual void PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) OVERRIDE;
348 gfx::ImageSkia icon() const { return icon_; }
350 virtual const gfx::ImageSkia& GetImageToPaint() const;
352 // Overridden from NativeThemeDelegate:
353 virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
355 // Overridden from TextButtonBase:
356 virtual void GetExtraParams(
357 ui::NativeTheme::ExtraParams* params) const OVERRIDE;
358 virtual gfx::Rect GetTextBounds() const OVERRIDE;
361 // The position of the icon.
362 IconPlacement icon_placement_;
364 // An icon displayed with the text.
365 gfx::ImageSkia icon_;
367 // An optional different version of the icon for hover state.
368 gfx::ImageSkia icon_hover_;
369 bool has_hover_icon_;
371 // An optional different version of the icon for pushed state.
372 gfx::ImageSkia icon_pushed_;
373 bool has_pushed_icon_;
375 // Space between icon and text.
376 int icon_text_spacing_;
378 // True if the button should ignore the minimum size for the platform. Default
379 // is true. Set to false to prevent narrower buttons.
380 bool ignore_minimum_size_;
382 // True if the icon and the text are aligned along both the left and right
383 // margins of the button.
384 bool full_justification_;
386 DISALLOW_COPY_AND_ASSIGN(TextButton);
391 #endif // UI_VIEWS_CONTROLS_BUTTON_TEXT_BUTTON_H_