Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: ui/views/controls/label.h

Issue 23228004: Prepare to use gfx::RenderText in views::Label. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Revert Label implementation changes. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/button/label_button.cc ('k') | ui/views/controls/label.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef UI_VIEWS_CONTROLS_LABEL_H_ 5 #ifndef UI_VIEWS_CONTROLS_LABEL_H_
6 #define UI_VIEWS_CONTROLS_LABEL_H_ 6 #define UI_VIEWS_CONTROLS_LABEL_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 30 matching lines...) Expand all
41 const base::string16& text() const { return text_; } 41 const base::string16& text() const { return text_; }
42 virtual void SetText(const base::string16& text); 42 virtual void SetText(const base::string16& text);
43 43
44 // Enables or disables auto-color-readability (enabled by default). If this 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 45 // is enabled, then calls to set any foreground or background color will
46 // trigger an automatic mapper that uses color_utils::GetReadableColor() to 46 // trigger an automatic mapper that uses color_utils::GetReadableColor() to
47 // ensure that the foreground colors are readable over the background color. 47 // ensure that the foreground colors are readable over the background color.
48 void SetAutoColorReadabilityEnabled(bool enabled); 48 void SetAutoColorReadabilityEnabled(bool enabled);
49 49
50 // Sets the color. This will automatically force the color to be readable 50 // Sets the color. This will automatically force the color to be readable
51 // over the current background color. 51 // over the current background color, if auto color readability is enabled.
52 virtual void SetEnabledColor(SkColor color); 52 virtual void SetEnabledColor(SkColor color);
53 void SetDisabledColor(SkColor color); 53 void SetDisabledColor(SkColor color);
54 54
55 SkColor enabled_color() const { return actual_enabled_color_; } 55 SkColor enabled_color() const { return actual_enabled_color_; }
56 56
57 // Sets the background color. This won't be explicitly drawn, but the label 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. 58 // will force the text color to be readable over it.
59 void SetBackgroundColor(SkColor color); 59 void SetBackgroundColor(SkColor color);
60 SkColor background_color() const { return background_color_; } 60 SkColor background_color() const { return background_color_; }
61 61
62 // Set drop shadows underneath the text. 62 // Set drop shadows underneath the text.
63 void set_shadows(const gfx::ShadowValues& shadows) { 63 void SetShadows(const gfx::ShadowValues& shadows);
64 shadows_ = shadows;
65 text_size_valid_ = false;
66 }
67 const gfx::ShadowValues& shadows() const { return shadows_; } 64 const gfx::ShadowValues& shadows() const { return shadows_; }
68 65
69 // Sets whether subpixel rendering is used; the default is true, but this 66 // Sets whether subpixel rendering is used; the default is true, but this
70 // feature also requires an opaque background color. 67 // feature also requires an opaque background color.
71 void set_subpixel_rendering_enabled(bool subpixel_rendering_enabled) { 68 void SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled);
72 subpixel_rendering_enabled_ = subpixel_rendering_enabled;
73 }
74 69
75 // Sets the horizontal alignment; the argument value is mirrored in RTL UI. 70 // Sets the horizontal alignment; the argument value is mirrored in RTL UI.
76 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment); 71 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
77 gfx::HorizontalAlignment GetHorizontalAlignment() const; 72 gfx::HorizontalAlignment GetHorizontalAlignment() const;
78 73
79 // Sets the directionality mode. The default value is DIRECTIONALITY_FROM_UI,
80 // which should be suitable for most text originating from UI string assets.
81 // Most text originating from web content should use DIRECTIONALITY_FROM_TEXT.
82 void set_directionality_mode(gfx::DirectionalityMode mode) {
83 directionality_mode_ = mode;
84 }
85 gfx::DirectionalityMode directionality_mode() const {
86 return directionality_mode_;
87 }
88
89 // Get or set the distance in pixels between baselines of multi-line text. 74 // Get or set the distance in pixels between baselines of multi-line text.
90 // Default is 0, indicating the distance between lines should be the standard 75 // Default is 0, indicating the distance between lines should be the standard
91 // one for the label's text, font list, and platform. 76 // one for the label's text, font list, and platform.
92 int line_height() const { return line_height_; } 77 int line_height() const { return line_height_; }
93 void SetLineHeight(int height); 78 void SetLineHeight(int height);
94 79
95 // Get or set if the label text can wrap on multiple lines; default is false. 80 // Get or set if the label text can wrap on multiple lines; default is false.
96 bool is_multi_line() const { return is_multi_line_; } 81 bool multi_line() const { return multi_line_; }
97 void SetMultiLine(bool multi_line); 82 void SetMultiLine(bool multi_line);
98 83
99 // Get or set if the label text should be obscured before rendering (e.g. 84 // Get or set if the label text should be obscured before rendering (e.g.
100 // should "Password!" display as "*********"); default is false. 85 // should "Password!" display as "*********"); default is false.
101 bool is_obscured() const { return is_obscured_; } 86 bool obscured() const { return obscured_; }
102 void SetObscured(bool obscured); 87 void SetObscured(bool obscured);
103 88
104 // Get the text as displayed to the user, respecting the 'obscured' flag. 89 // Sets whether multi-line text can wrap mid-word; the default is false.
105 const base::string16& layout_text() const { return layout_text_; }
106
107 // Sets whether the label text can be split on words.
108 // Default is false. This only works when is_multi_line is true.
109 void SetAllowCharacterBreak(bool allow_character_break); 90 void SetAllowCharacterBreak(bool allow_character_break);
110 91
111 // Sets the eliding or fading behavior, applied as necessary. The default is 92 // Sets the eliding or fading behavior, applied as necessary. The default is
112 // to elide at the end. Eliding is not well supported for multi-line labels. 93 // to elide at the end. Eliding is not well supported for multi-line labels.
113 void SetElideBehavior(gfx::ElideBehavior elide_behavior); 94 void SetElideBehavior(gfx::ElideBehavior elide_behavior);
114 95
115 // Sets the tooltip text. Default behavior for a label (single-line) is to 96 // Sets the tooltip text. Default behavior for a label (single-line) is to
116 // show the full text if it is wider than its bounds. Calling this overrides 97 // show the full text if it is wider than its bounds. Calling this overrides
117 // the default behavior and lets you set a custom tooltip. To revert to 98 // the default behavior and lets you set a custom tooltip. To revert to
118 // default behavior, call this with an empty string. 99 // default behavior, call this with an empty string.
119 void SetTooltipText(const base::string16& tooltip_text); 100 void SetTooltipText(const base::string16& tooltip_text);
120 101
121 // Resizes the label so its width is set to the width of the longest line and 102 // Resizes the label so its width is set to the width of the longest line and
122 // its height deduced accordingly. 103 // its height deduced accordingly.
123 // This is only intended for multi-line labels and is useful when the label's 104 // This is only intended for multi-line labels and is useful when the label's
124 // text contains several lines separated with \n. 105 // text contains several lines separated with \n.
125 // |max_width| is the maximum width that will be used (longer lines will be 106 // |max_width| is the maximum width that will be used (longer lines will be
126 // wrapped). If 0, no maximum width is enforced. 107 // wrapped). If 0, no maximum width is enforced.
127 void SizeToFit(int max_width); 108 void SizeToFit(int max_width);
128 109
129 // Gets/sets the flag to determine whether the label should be collapsed when 110 // Sets whether the preferred size is empty when the label is not visible.
130 // it's hidden (not visible). If this flag is true, the label will return a
131 // preferred size of (0, 0) when it's not visible.
132 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; } 111 void set_collapse_when_hidden(bool value) { collapse_when_hidden_ = value; }
133 bool collapse_when_hidden() const { return collapse_when_hidden_; } 112
113 // Get the text as displayed to the user, respecting the obscured flag.
114 const base::string16& GetLayoutTextForTesting() const;
134 115
135 // View: 116 // View:
136 virtual gfx::Insets GetInsets() const OVERRIDE; 117 virtual gfx::Insets GetInsets() const OVERRIDE;
137 virtual int GetBaseline() const OVERRIDE; 118 virtual int GetBaseline() const OVERRIDE;
138 // Overridden to compute the size required to display this label.
139 virtual gfx::Size GetPreferredSize() const OVERRIDE; 119 virtual gfx::Size GetPreferredSize() const OVERRIDE;
140 // Returns the width of an ellipsis if the label is non-empty, or 0 otherwise.
141 virtual gfx::Size GetMinimumSize() const OVERRIDE; 120 virtual gfx::Size GetMinimumSize() const OVERRIDE;
142 // Returns the height necessary to display this label with the provided width.
143 // This method is used to layout multi-line labels. It is equivalent to
144 // GetPreferredSize().height() if the receiver is not multi-line.
145 virtual int GetHeightForWidth(int w) const OVERRIDE; 121 virtual int GetHeightForWidth(int w) const OVERRIDE;
146 virtual const char* GetClassName() const OVERRIDE; 122 virtual const char* GetClassName() const OVERRIDE;
147 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) OVERRIDE; 123 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) OVERRIDE;
148 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE; 124 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE;
149 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 125 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
150 // Gets the tooltip text for labels that are wider than their bounds, except
151 // when the label is multiline, in which case it just returns false (no
152 // tooltip). If a custom tooltip has been specified with SetTooltipText()
153 // it is returned instead.
154 virtual bool GetTooltipText(const gfx::Point& p, 126 virtual bool GetTooltipText(const gfx::Point& p,
155 base::string16* tooltip) const OVERRIDE; 127 base::string16* tooltip) const OVERRIDE;
156 128
157 protected: 129 protected:
158 // Called by Paint to paint the text. Override this to change how 130 // Called by Paint to paint the text.
159 // text is painted. 131 void PaintText(gfx::Canvas* canvas,
160 virtual void PaintText(gfx::Canvas* canvas, 132 const base::string16& text,
161 const base::string16& text, 133 const gfx::Rect& text_bounds,
162 const gfx::Rect& text_bounds, 134 int flags);
163 int flags);
164 135
165 virtual gfx::Size GetTextSize() const; 136 virtual gfx::Size GetTextSize() const;
166 137
167 SkColor disabled_color() const { return actual_disabled_color_; } 138 SkColor disabled_color() const { return actual_disabled_color_; }
168 139
169 // Overridden from View: 140 // View:
170 // Overridden to dirty our text bounds if we're multi-line.
171 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 141 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
172 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 142 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
173 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; 143 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
174 144
175 private: 145 private:
176 // These tests call CalculateDrawStringParams in order to verify the 146 // These tests call CalculateDrawStringParams in order to verify the
177 // calculations done for drawing text. 147 // calculations done for drawing text.
178 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString); 148 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString);
179 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString); 149 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString);
180 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL); 150 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 // Set to true once the corresponding setter is invoked. 193 // Set to true once the corresponding setter is invoked.
224 bool enabled_color_set_; 194 bool enabled_color_set_;
225 bool disabled_color_set_; 195 bool disabled_color_set_;
226 bool background_color_set_; 196 bool background_color_set_;
227 197
228 bool subpixel_rendering_enabled_; 198 bool subpixel_rendering_enabled_;
229 bool auto_color_readability_; 199 bool auto_color_readability_;
230 mutable gfx::Size text_size_; 200 mutable gfx::Size text_size_;
231 mutable bool text_size_valid_; 201 mutable bool text_size_valid_;
232 int line_height_; 202 int line_height_;
233 bool is_multi_line_; 203 bool multi_line_;
234 bool is_obscured_; 204 bool obscured_;
235 bool allow_character_break_; 205 bool allow_character_break_;
236 gfx::ElideBehavior elide_behavior_; 206 gfx::ElideBehavior elide_behavior_;
237 gfx::HorizontalAlignment horizontal_alignment_; 207 gfx::HorizontalAlignment horizontal_alignment_;
238 base::string16 tooltip_text_; 208 base::string16 tooltip_text_;
239 // Whether to collapse the label when it's not visible. 209 // Whether to collapse the label when it's not visible.
240 bool collapse_when_hidden_; 210 bool collapse_when_hidden_;
241 // Controls whether the directionality is auto-detected based on first strong
242 // directionality character or is determined by the application UI's locale.
243 gfx::DirectionalityMode directionality_mode_;
244 gfx::ShadowValues shadows_; 211 gfx::ShadowValues shadows_;
245 212
246 // The cached heights to avoid recalculation in GetHeightForWidth(). 213 // The cached heights to avoid recalculation in GetHeightForWidth().
247 mutable std::vector<gfx::Size> cached_heights_; 214 mutable std::vector<gfx::Size> cached_heights_;
248 mutable int cached_heights_cursor_; 215 mutable int cached_heights_cursor_;
249 216
250 DISALLOW_COPY_AND_ASSIGN(Label); 217 DISALLOW_COPY_AND_ASSIGN(Label);
251 }; 218 };
252 219
253 } // namespace views 220 } // namespace views
254 221
255 #endif // UI_VIEWS_CONTROLS_LABEL_H_ 222 #endif // UI_VIEWS_CONTROLS_LABEL_H_
OLDNEW
« no previous file with comments | « ui/views/controls/button/label_button.cc ('k') | ui/views/controls/label.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698