Adding Test Fixture for initial test cases for the App Remoting Test Driver. Also...
[chromium-blink-merge.git] / ui / views / controls / label.h
blob03eade981af0e8d38aeb93c7ee45c1b0ca15bb76
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"
14 namespace views {
16 // A view subclass that can display a string.
17 class VIEWS_EXPORT Label : public View {
18 public:
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;
25 Label();
26 explicit Label(const base::string16& text);
27 Label(const base::string16& text, const gfx::FontList& font_list);
28 ~Label() override;
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 void SetAllowCharacterBreak(bool allow_character_break);
91 // Sets the eliding or fading behavior, applied as necessary. The default is
92 // to elide at the end. Eliding is not well-supported for multi-line labels.
93 void SetElideBehavior(gfx::ElideBehavior elide_behavior);
94 gfx::ElideBehavior elide_behavior() const { return elide_behavior_; }
96 // Sets the tooltip text. Default behavior for a label (single-line) is to
97 // show the full text if it is wider than its bounds. Calling this overrides
98 // the default behavior and lets you set a custom tooltip. To revert to
99 // default behavior, call this with an empty string.
100 void SetTooltipText(const base::string16& tooltip_text);
102 // Get or set whether this label can act as a tooltip handler; the default is
103 // true. Set to false whenever an ancestor view should handle tooltips
104 // instead.
105 bool handles_tooltips() const { return handles_tooltips_; }
106 void SetHandlesTooltips(bool enabled);
108 // Resizes the label so its width is set to the width of the longest line and
109 // its height deduced accordingly.
110 // This is only intended for multi-line labels and is useful when the label's
111 // text contains several lines separated with \n.
112 // |max_width| is the maximum width that will be used (longer lines will be
113 // wrapped). If 0, no maximum width is enforced.
114 void SizeToFit(int max_width);
116 // Sets whether the preferred size is empty when the label is not visible.
117 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; }
119 // Get the text as displayed to the user, respecting the obscured flag.
120 base::string16 GetDisplayTextForTesting();
122 // View:
123 gfx::Insets GetInsets() const override;
124 int GetBaseline() const override;
125 gfx::Size GetPreferredSize() const override;
126 gfx::Size GetMinimumSize() const override;
127 int GetHeightForWidth(int w) const override;
128 void Layout() override;
129 const char* GetClassName() const override;
130 View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
131 bool CanProcessEventsWithinSubtree() const override;
132 void GetAccessibleState(ui::AXViewState* state) override;
133 bool GetTooltipText(const gfx::Point& p,
134 base::string16* tooltip) const override;
135 void OnEnabledChanged() override;
137 protected:
138 void PaintText(gfx::Canvas* canvas);
140 SkColor disabled_color() const { return actual_disabled_color_; }
142 // View:
143 void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
144 void VisibilityChanged(View* starting_from, bool is_visible) override;
145 void OnPaint(gfx::Canvas* canvas) override;
146 void OnDeviceScaleFactorChanged(float device_scale_factor) override;
147 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
149 private:
150 FRIEND_TEST_ALL_PREFIXES(LabelTest, ResetRenderTextData);
151 FRIEND_TEST_ALL_PREFIXES(LabelTest, MultilineSupportedRenderText);
152 FRIEND_TEST_ALL_PREFIXES(LabelTest, TextChangeWithoutLayout);
153 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, FocusBounds);
154 FRIEND_TEST_ALL_PREFIXES(LabelFocusTest, EmptyLabel);
156 void Init(const base::string16& text, const gfx::FontList& font_list);
158 void ResetLayout();
160 // Create a single RenderText instance to actually be painted.
161 scoped_ptr<gfx::RenderText> CreateRenderText(
162 const base::string16& text,
163 gfx::HorizontalAlignment alignment,
164 gfx::DirectionalityMode directionality,
165 gfx::ElideBehavior elide_behavior);
167 // Set up |lines_| to actually be painted.
168 void MaybeBuildRenderTextLines();
170 gfx::Rect GetFocusBounds();
172 // Get the text broken into lines as needed to fit the given |width|.
173 std::vector<base::string16> GetLinesForWidth(int width) const;
175 // Get the text size for the current layout.
176 gfx::Size GetTextSize() const;
178 void RecalculateColors();
180 // Updates any colors that have not been explicitly set from the theme.
181 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
183 bool ShouldShowDefaultTooltip() const;
185 // An un-elided and single-line RenderText object used for preferred sizing.
186 scoped_ptr<gfx::RenderText> render_text_;
188 // The RenderText instances used to display elided and multi-line text.
189 ScopedVector<gfx::RenderText> lines_;
191 SkColor requested_enabled_color_;
192 SkColor actual_enabled_color_;
193 SkColor requested_disabled_color_;
194 SkColor actual_disabled_color_;
195 SkColor background_color_;
197 // Set to true once the corresponding setter is invoked.
198 bool enabled_color_set_;
199 bool disabled_color_set_;
200 bool background_color_set_;
202 gfx::ElideBehavior elide_behavior_;
204 bool subpixel_rendering_enabled_;
205 bool auto_color_readability_;
206 // TODO(mukai): remove |multi_line_| when all RenderText can render multiline.
207 bool multi_line_;
208 base::string16 tooltip_text_;
209 bool handles_tooltips_;
210 // Whether to collapse the label when it's not visible.
211 bool collapse_when_hidden_;
212 bool allow_character_break_;
213 int max_width_;
215 // TODO(vadimt): Remove is_first_paint_text_ before crbug.com/431326 is
216 // closed.
217 bool is_first_paint_text_;
219 DISALLOW_COPY_AND_ASSIGN(Label);
222 } // namespace views
224 #endif // UI_VIEWS_CONTROLS_LABEL_H_