MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / ui / views / controls / label.h
blob727b6db9a2f26a4524b16c20fa38e501cd1a6371
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 <string>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/strings/string16.h"
14 #include "third_party/skia/include/core/SkColor.h"
15 #include "ui/gfx/font_list.h"
16 #include "ui/gfx/shadow_value.h"
17 #include "ui/gfx/text_constants.h"
18 #include "ui/views/view.h"
20 namespace views {
22 // A view subclass that can display a string.
23 class VIEWS_EXPORT Label : public View {
24 public:
25 // Internal class name.
26 static const char kViewClassName[];
28 // The padding for the focus border when rendering focused text.
29 static const int kFocusBorderPadding;
31 Label();
32 explicit Label(const base::string16& text);
33 Label(const base::string16& text, const gfx::FontList& font_list);
34 virtual ~Label();
36 // Gets or sets the fonts used by this label.
37 const gfx::FontList& font_list() const { return font_list_; }
38 virtual void SetFontList(const gfx::FontList& font_list);
40 // Get or set the label text.
41 const base::string16& text() const { return text_; }
42 virtual void SetText(const base::string16& text);
44 // Enables or disables auto-color-readability (enabled by default). If this
45 // is enabled, then calls to set any foreground or background color will
46 // trigger an automatic mapper that uses color_utils::GetReadableColor() to
47 // ensure that the foreground colors are readable over the background color.
48 void SetAutoColorReadabilityEnabled(bool enabled);
50 // Sets the color. This will automatically force the color to be readable
51 // over the current background color, if auto color readability is enabled.
52 virtual void SetEnabledColor(SkColor color);
53 void SetDisabledColor(SkColor color);
55 SkColor enabled_color() const { return actual_enabled_color_; }
57 // Sets the background color. This won't be explicitly drawn, but the label
58 // will force the text color to be readable over it.
59 void SetBackgroundColor(SkColor color);
60 SkColor background_color() const { return background_color_; }
62 // Set drop shadows underneath the text.
63 void SetShadows(const gfx::ShadowValues& shadows);
64 const gfx::ShadowValues& shadows() const { return shadows_; }
66 // Sets whether subpixel rendering is used; the default is true, but this
67 // feature also requires an opaque background color.
68 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled);
70 // Sets the horizontal alignment; the argument value is mirrored in RTL UI.
71 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
72 gfx::HorizontalAlignment GetHorizontalAlignment() const;
74 // Get or set the distance in pixels between baselines of multi-line text.
75 // Default is 0, indicating the distance between lines should be the standard
76 // one for the label's text, font list, and platform.
77 int line_height() const { return line_height_; }
78 void SetLineHeight(int height);
80 // Get or set if the label text can wrap on multiple lines; default is false.
81 bool multi_line() const { return multi_line_; }
82 void SetMultiLine(bool multi_line);
84 // Get or set if the label text should be obscured before rendering (e.g.
85 // should "Password!" display as "*********"); default is false.
86 bool obscured() const { return obscured_; }
87 void SetObscured(bool obscured);
89 // Sets whether multi-line text can wrap mid-word; the default is false.
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);
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 const base::string16& GetLayoutTextForTesting() const;
122 // View:
123 virtual gfx::Insets GetInsets() const override;
124 virtual int GetBaseline() const override;
125 virtual gfx::Size GetPreferredSize() const override;
126 virtual gfx::Size GetMinimumSize() const override;
127 virtual int GetHeightForWidth(int w) const override;
128 virtual const char* GetClassName() const override;
129 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
130 virtual bool CanProcessEventsWithinSubtree() const override;
131 virtual void GetAccessibleState(ui::AXViewState* state) override;
132 virtual bool GetTooltipText(const gfx::Point& p,
133 base::string16* tooltip) const override;
135 protected:
136 // Called by Paint to paint the text.
137 void PaintText(gfx::Canvas* canvas,
138 const base::string16& text,
139 const gfx::Rect& text_bounds,
140 int flags);
142 virtual gfx::Size GetTextSize() const;
144 SkColor disabled_color() const { return actual_disabled_color_; }
146 // View:
147 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
148 virtual void OnPaint(gfx::Canvas* canvas) override;
149 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
151 private:
152 // These tests call CalculateDrawStringParams in order to verify the
153 // calculations done for drawing text.
154 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString);
155 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString);
156 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL);
157 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL);
158 FRIEND_TEST_ALL_PREFIXES(LabelTest, DirectionalityFromText);
159 FRIEND_TEST_ALL_PREFIXES(LabelTest, DisableSubpixelRendering);
161 // Sets both |text_| and |layout_text_| to appropriate values, taking
162 // the label's 'obscured' status into account.
163 void SetTextInternal(const base::string16& text);
165 void Init(const base::string16& text, const gfx::FontList& font_list);
167 void RecalculateColors();
169 // Returns where the text is drawn, in the receivers coordinate system.
170 gfx::Rect GetTextBounds() const;
172 int ComputeDrawStringFlags() const;
174 gfx::Rect GetAvailableRect() const;
176 // Returns parameters to be used for the DrawString call.
177 void CalculateDrawStringParams(base::string16* paint_text,
178 gfx::Rect* text_bounds,
179 int* flags) const;
181 // Updates any colors that have not been explicitly set from the theme.
182 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
184 // Resets |cached_heights_| and |cached_heights_cursor_| and mark
185 // |text_size_valid_| as false.
186 void ResetCachedSize();
188 bool ShouldShowDefaultTooltip() const;
190 base::string16 text_;
191 base::string16 layout_text_;
192 gfx::FontList font_list_;
193 SkColor requested_enabled_color_;
194 SkColor actual_enabled_color_;
195 SkColor requested_disabled_color_;
196 SkColor actual_disabled_color_;
197 SkColor background_color_;
199 // Set to true once the corresponding setter is invoked.
200 bool enabled_color_set_;
201 bool disabled_color_set_;
202 bool background_color_set_;
204 bool subpixel_rendering_enabled_;
205 bool auto_color_readability_;
206 mutable gfx::Size text_size_;
207 mutable bool text_size_valid_;
208 int line_height_;
209 bool multi_line_;
210 bool obscured_;
211 bool allow_character_break_;
212 gfx::ElideBehavior elide_behavior_;
213 gfx::HorizontalAlignment horizontal_alignment_;
214 base::string16 tooltip_text_;
215 bool handles_tooltips_;
216 // Whether to collapse the label when it's not visible.
217 bool collapse_when_hidden_;
218 gfx::ShadowValues shadows_;
220 // The cached heights to avoid recalculation in GetHeightForWidth().
221 mutable std::vector<gfx::Size> cached_heights_;
222 mutable int cached_heights_cursor_;
224 DISALLOW_COPY_AND_ASSIGN(Label);
227 } // namespace views
229 #endif // UI_VIEWS_CONTROLS_LABEL_H_