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_LABEL_H_
6 #define UI_VIEWS_CONTROLS_LABEL_H_
8 #include "base/compiler_specific.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/scoped_vector.h"
11 #include "ui/gfx/render_text.h"
12 #include "ui/views/view.h"
16 // A view subclass that can display a string.
17 class VIEWS_EXPORT Label
: public View
{
19 // Internal class name.
20 static const char kViewClassName
[];
22 // The padding for the focus border when rendering focused text.
23 static const int kFocusBorderPadding
;
26 explicit Label(const base::string16
& text
);
27 Label(const base::string16
& text
, const gfx::FontList
& font_list
);
30 // Gets or sets the fonts used by this label.
31 const gfx::FontList
& font_list() const { return render_text_
->font_list(); }
33 virtual void SetFontList(const gfx::FontList
& font_list
);
35 // Get or set the label text.
36 const base::string16
& text() const { return render_text_
->text(); }
37 virtual void SetText(const base::string16
& text
);
39 // Enables or disables auto-color-readability (enabled by default). If this
40 // is enabled, then calls to set any foreground or background color will
41 // trigger an automatic mapper that uses color_utils::GetReadableColor() to
42 // ensure that the foreground colors are readable over the background color.
43 void SetAutoColorReadabilityEnabled(bool enabled
);
45 // Sets the color. This will automatically force the color to be readable
46 // over the current background color, if auto color readability is enabled.
47 virtual void SetEnabledColor(SkColor color
);
48 void SetDisabledColor(SkColor color
);
50 SkColor
enabled_color() const { return actual_enabled_color_
; }
52 // Sets the background color. This won't be explicitly drawn, but the label
53 // will force the text color to be readable over it.
54 void SetBackgroundColor(SkColor color
);
55 SkColor
background_color() const { return background_color_
; }
57 // Set drop shadows underneath the text.
58 void SetShadows(const gfx::ShadowValues
& shadows
);
59 const gfx::ShadowValues
& shadows() const { return render_text_
->shadows(); }
61 // Sets whether subpixel rendering is used; the default is true, but this
62 // feature also requires an opaque background color.
63 // TODO(mukai): rename this as SetSubpixelRenderingSuppressed() to keep the
64 // consistency with RenderText field name.
65 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled
);
67 // Sets the horizontal alignment; the argument value is mirrored in RTL UI.
68 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment
);
69 gfx::HorizontalAlignment
horizontal_alignment() const {
70 return render_text_
->horizontal_alignment();
73 // Get or set the distance in pixels between baselines of multi-line text.
74 // Default is 0, indicating the distance between lines should be the standard
75 // one for the label's text, font list, and platform.
76 int line_height() const { return render_text_
->min_line_height(); }
77 void SetLineHeight(int height
);
79 // Get or set if the label text can wrap on multiple lines; default is false.
80 bool multi_line() const { return multi_line_
; }
81 void SetMultiLine(bool multi_line
);
83 // Get or set if the label text should be obscured before rendering (e.g.
84 // should "Password!" display as "*********"); default is false.
85 bool obscured() const { return render_text_
->obscured(); }
86 void SetObscured(bool obscured
);
88 // Sets whether multi-line text can wrap mid-word; the default is false.
89 // TODO(mukai): allow specifying WordWrapBehavior.
90 void SetAllowCharacterBreak(bool allow_character_break
);
92 // Sets the eliding or fading behavior, applied as necessary. The default is
93 // to elide at the end. Eliding is not well-supported for multi-line labels.
94 void SetElideBehavior(gfx::ElideBehavior elide_behavior
);
95 gfx::ElideBehavior
elide_behavior() const { return elide_behavior_
; }
97 // Sets the tooltip text. Default behavior for a label (single-line) is to
98 // show the full text if it is wider than its bounds. Calling this overrides
99 // the default behavior and lets you set a custom tooltip. To revert to
100 // default behavior, call this with an empty string.
101 void SetTooltipText(const base::string16
& tooltip_text
);
103 // Get or set whether this label can act as a tooltip handler; the default is
104 // true. Set to false whenever an ancestor view should handle tooltips
106 bool handles_tooltips() const { return handles_tooltips_
; }
107 void SetHandlesTooltips(bool enabled
);
109 // Resizes the label so its width is set to the width of the longest line and
110 // its height deduced accordingly.
111 // This is only intended for multi-line labels and is useful when the label's
112 // text contains several lines separated with \n.
113 // |max_width| is the maximum width that will be used (longer lines will be
114 // wrapped). If 0, no maximum width is enforced.
115 void SizeToFit(int max_width
);
117 // Sets whether the preferred size is empty when the label is not visible.
118 void set_collapse_when_hidden(bool value
) { collapse_when_hidden_
= value
; }
120 // Get the text as displayed to the user, respecting the obscured flag.
121 base::string16
GetDisplayTextForTesting();
124 gfx::Insets
GetInsets() const override
;
125 int GetBaseline() const override
;
126 gfx::Size
GetPreferredSize() const override
;
127 gfx::Size
GetMinimumSize() const override
;
128 int GetHeightForWidth(int w
) const override
;
129 void Layout() override
;
130 const char* GetClassName() const override
;
131 View
* GetTooltipHandlerForPoint(const gfx::Point
& point
) override
;
132 bool CanProcessEventsWithinSubtree() const override
;
133 void GetAccessibleState(ui::AXViewState
* state
) override
;
134 bool GetTooltipText(const gfx::Point
& p
,
135 base::string16
* tooltip
) const override
;
136 void OnEnabledChanged() override
;
139 // Create a single RenderText instance to actually be painted.
140 virtual scoped_ptr
<gfx::RenderText
> CreateRenderText(
141 const base::string16
& text
,
142 gfx::HorizontalAlignment alignment
,
143 gfx::DirectionalityMode directionality
,
144 gfx::ElideBehavior elide_behavior
);
146 void PaintText(gfx::Canvas
* canvas
);
148 SkColor
disabled_color() const { return actual_disabled_color_
; }
151 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
152 void VisibilityChanged(View
* starting_from
, bool is_visible
) override
;
153 void OnPaint(gfx::Canvas
* canvas
) override
;
154 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
155 void OnNativeThemeChanged(const ui::NativeTheme
* theme
) override
;
158 FRIEND_TEST_ALL_PREFIXES(LabelTest
, ResetRenderTextData
);
159 FRIEND_TEST_ALL_PREFIXES(LabelTest
, MultilineSupportedRenderText
);
160 FRIEND_TEST_ALL_PREFIXES(LabelTest
, TextChangeWithoutLayout
);
161 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest
, FocusBounds
);
162 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest
, EmptyLabel
);
164 void Init(const base::string16
& text
, const gfx::FontList
& font_list
);
168 // Set up |lines_| to actually be painted.
169 void MaybeBuildRenderTextLines();
171 gfx::Rect
GetFocusBounds();
173 // Get the text broken into lines as needed to fit the given |width|.
174 std::vector
<base::string16
> GetLinesForWidth(int width
) const;
176 // Get the text size for the current layout.
177 gfx::Size
GetTextSize() const;
179 void RecalculateColors();
181 // Updates any colors that have not been explicitly set from the theme.
182 void UpdateColorsFromTheme(const ui::NativeTheme
* theme
);
184 bool ShouldShowDefaultTooltip() const;
186 // An un-elided and single-line RenderText object used for preferred sizing.
187 scoped_ptr
<gfx::RenderText
> render_text_
;
189 // The RenderText instances used to display elided and multi-line text.
190 ScopedVector
<gfx::RenderText
> lines_
;
192 SkColor requested_enabled_color_
;
193 SkColor actual_enabled_color_
;
194 SkColor requested_disabled_color_
;
195 SkColor actual_disabled_color_
;
196 SkColor background_color_
;
198 // Set to true once the corresponding setter is invoked.
199 bool enabled_color_set_
;
200 bool disabled_color_set_
;
201 bool background_color_set_
;
203 gfx::ElideBehavior elide_behavior_
;
205 bool subpixel_rendering_enabled_
;
206 bool auto_color_readability_
;
207 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline.
209 base::string16 tooltip_text_
;
210 bool handles_tooltips_
;
211 // Whether to collapse the label when it's not visible.
212 bool collapse_when_hidden_
;
215 // TODO(vadimt): Remove is_first_paint_text_ before crbug.com/431326 is
217 bool is_first_paint_text_
;
219 DISALLOW_COPY_AND_ASSIGN(Label
);
224 #endif // UI_VIEWS_CONTROLS_LABEL_H_