OLD | NEW |
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 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "third_party/skia/include/core/SkColor.h" | 14 #include "third_party/skia/include/core/SkColor.h" |
15 #include "ui/gfx/font_list.h" | 15 #include "ui/gfx/font_list.h" |
16 #include "ui/gfx/text_constants.h" | 16 #include "ui/gfx/text_constants.h" |
17 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
18 | 18 |
19 namespace views { | 19 namespace views { |
20 | 20 |
21 // A view subclass that can display a string. | 21 ///////////////////////////////////////////////////////////////////////////// |
| 22 // |
| 23 // Label class |
| 24 // |
| 25 // A label is a view subclass that can display a string. |
| 26 // |
| 27 ///////////////////////////////////////////////////////////////////////////// |
22 class VIEWS_EXPORT Label : public View { | 28 class VIEWS_EXPORT Label : public View { |
23 public: | 29 public: |
| 30 // The following enum is used to indicate whether using the Chrome UI's |
| 31 // directionality as the label's directionality, or auto-detecting the label's |
| 32 // directionality. |
| 33 // |
| 34 // If the label text originates from the Chrome UI, we should use the Chrome |
| 35 // UI's directionality as the label's directionality. |
| 36 // |
| 37 // If the text originates from a web page, its directionality is determined |
| 38 // based on its first character with strong directionality, disregarding what |
| 39 // directionality the Chrome UI is. |
| 40 enum DirectionalityMode { |
| 41 USE_UI_DIRECTIONALITY = 0, |
| 42 AUTO_DETECT_DIRECTIONALITY |
| 43 }; |
| 44 |
24 // Internal class name. | 45 // Internal class name. |
25 static const char kViewClassName[]; | 46 static const char kViewClassName[]; |
26 | 47 |
27 // The padding for the focus border when rendering focused text. | 48 // The padding for the focus border when rendering focused text. |
28 static const int kFocusBorderPadding; | 49 static const int kFocusBorderPadding; |
29 | 50 |
30 Label(); | 51 Label(); |
31 explicit Label(const base::string16& text); | 52 explicit Label(const base::string16& text); |
32 Label(const base::string16& text, const gfx::FontList& font_list); | 53 Label(const base::string16& text, const gfx::FontList& font_list); |
33 virtual ~Label(); | 54 virtual ~Label(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 87 |
67 // Sets the shadow blur. Default is zero. | 88 // Sets the shadow blur. Default is zero. |
68 void set_shadow_blur(double shadow_blur) { shadow_blur_ = shadow_blur; } | 89 void set_shadow_blur(double shadow_blur) { shadow_blur_ = shadow_blur; } |
69 | 90 |
70 // Disables shadows. | 91 // Disables shadows. |
71 void ClearEmbellishing(); | 92 void ClearEmbellishing(); |
72 | 93 |
73 // Set the color of a halo on the painted text (use transparent for none). | 94 // Set the color of a halo on the painted text (use transparent for none). |
74 void set_halo_color(SkColor halo_color) { halo_color_ = halo_color; } | 95 void set_halo_color(SkColor halo_color) { halo_color_ = halo_color; } |
75 | 96 |
76 // Sets the horizontal alignment; the argument value is mirrored in RTL UI. | 97 // Sets horizontal alignment. If the locale is RTL, and the directionality |
| 98 // mode is USE_UI_DIRECTIONALITY, the alignment is flipped around. |
| 99 // |
| 100 // Caveat: for labels originating from a web page, the directionality mode |
| 101 // should be reset to AUTO_DETECT_DIRECTIONALITY before the horizontal |
| 102 // alignment is set. Otherwise, the label's alignment specified as a parameter |
| 103 // will be flipped in RTL locales. |
77 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment); | 104 void SetHorizontalAlignment(gfx::HorizontalAlignment alignment); |
78 gfx::HorizontalAlignment GetHorizontalAlignment() const; | |
79 | 105 |
80 // Sets the directionality mode. The default value is DIRECTIONALITY_FROM_UI, | 106 gfx::HorizontalAlignment horizontal_alignment() const { |
81 // which should be suitable for most text originating from UI string assets. | 107 return horizontal_alignment_; |
82 // Most text originating from web content should use DIRECTIONALITY_FROM_TEXT. | 108 } |
83 void set_directionality_mode(gfx::DirectionalityMode mode) { | 109 |
| 110 // Sets the directionality mode. The directionality mode is initialized to |
| 111 // USE_UI_DIRECTIONALITY when the label is constructed. USE_UI_DIRECTIONALITY |
| 112 // applies to every label that originates from the Chrome UI. However, if the |
| 113 // label originates from a web page, its directionality is auto-detected. |
| 114 void set_directionality_mode(DirectionalityMode mode) { |
84 directionality_mode_ = mode; | 115 directionality_mode_ = mode; |
85 } | 116 } |
86 gfx::DirectionalityMode directionality_mode() const { | 117 |
| 118 DirectionalityMode directionality_mode() const { |
87 return directionality_mode_; | 119 return directionality_mode_; |
88 } | 120 } |
89 | 121 |
90 // Get or set the distance in pixels between baselines of multi-line text. | 122 // Get or set the distance in pixels between baselines of multi-line text. |
91 // Default is 0, indicating the distance between lines should be the standard | 123 // Default is 0, indicating the distance between lines should be the standard |
92 // one for the label's text, font list, and platform. | 124 // one for the label's text, font list, and platform. |
93 int line_height() const { return line_height_; } | 125 int line_height() const { return line_height_; } |
94 void SetLineHeight(int height); | 126 void SetLineHeight(int height); |
95 | 127 |
96 // Get or set if the label text can wrap on multiple lines; default is false. | 128 // Get or set if the label text can wrap on multiple lines; default is false. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 205 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
174 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; | 206 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; |
175 | 207 |
176 private: | 208 private: |
177 // These tests call CalculateDrawStringParams in order to verify the | 209 // These tests call CalculateDrawStringParams in order to verify the |
178 // calculations done for drawing text. | 210 // calculations done for drawing text. |
179 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString); | 211 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString); |
180 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString); | 212 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString); |
181 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL); | 213 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL); |
182 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL); | 214 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL); |
183 FRIEND_TEST_ALL_PREFIXES(LabelTest, DirectionalityFromText); | 215 FRIEND_TEST_ALL_PREFIXES(LabelTest, AutoDetectDirectionality); |
| 216 |
| 217 // Calls ComputeDrawStringFlags(). |
184 FRIEND_TEST_ALL_PREFIXES(LabelTest, DisableSubpixelRendering); | 218 FRIEND_TEST_ALL_PREFIXES(LabelTest, DisableSubpixelRendering); |
185 | 219 |
186 // Sets both |text_| and |layout_text_| to appropriate values, taking | 220 // Sets both |text_| and |layout_text_| to appropriate values, taking |
187 // the label's 'obscured' status into account. | 221 // the label's 'obscured' status into account. |
188 void SetTextInternal(const base::string16& text); | 222 void SetTextInternal(const base::string16& text); |
189 | 223 |
190 void Init(const base::string16& text, const gfx::FontList& font_list); | 224 void Init(const base::string16& text, const gfx::FontList& font_list); |
191 | 225 |
192 void RecalculateColors(); | 226 void RecalculateColors(); |
193 | 227 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 mutable bool text_size_valid_; | 265 mutable bool text_size_valid_; |
232 int line_height_; | 266 int line_height_; |
233 bool is_multi_line_; | 267 bool is_multi_line_; |
234 bool is_obscured_; | 268 bool is_obscured_; |
235 bool allow_character_break_; | 269 bool allow_character_break_; |
236 gfx::ElideBehavior elide_behavior_; | 270 gfx::ElideBehavior elide_behavior_; |
237 gfx::HorizontalAlignment horizontal_alignment_; | 271 gfx::HorizontalAlignment horizontal_alignment_; |
238 base::string16 tooltip_text_; | 272 base::string16 tooltip_text_; |
239 // Whether to collapse the label when it's not visible. | 273 // Whether to collapse the label when it's not visible. |
240 bool collapse_when_hidden_; | 274 bool collapse_when_hidden_; |
241 // Controls whether the directionality is auto-detected based on first strong | 275 // The following member variable is used to control whether the |
242 // directionality character or is determined by the application UI's locale. | 276 // directionality is auto-detected based on first strong directionality |
243 gfx::DirectionalityMode directionality_mode_; | 277 // character or is determined by chrome UI's locale. |
| 278 DirectionalityMode directionality_mode_; |
244 | 279 |
245 // Colors for shadow. | 280 // Colors for shadow. |
246 SkColor enabled_shadow_color_; | 281 SkColor enabled_shadow_color_; |
247 SkColor disabled_shadow_color_; | 282 SkColor disabled_shadow_color_; |
248 | 283 |
249 // Space between text and shadow. | 284 // Space between text and shadow. |
250 gfx::Point shadow_offset_; | 285 gfx::Point shadow_offset_; |
251 | 286 |
252 // Should a shadow be drawn behind the text? | 287 // Should a shadow be drawn behind the text? |
253 bool has_shadow_; | 288 bool has_shadow_; |
254 | 289 |
255 // Indicates the level of shadow blurring. Default is zero. | 290 // Indicates the level of shadow blurring. Default is zero. |
256 double shadow_blur_; | 291 double shadow_blur_; |
257 | 292 |
258 // The halo color drawn around the text if it is not transparent. | 293 // The halo color drawn around the text if it is not transparent. |
259 SkColor halo_color_; | 294 SkColor halo_color_; |
260 | 295 |
261 // The cached heights to avoid recalculation in GetHeightForWidth(). | 296 // The cached heights to avoid recalculation in GetHeightForWidth(). |
262 mutable std::vector<gfx::Size> cached_heights_; | 297 mutable std::vector<gfx::Size> cached_heights_; |
263 mutable int cached_heights_cursor_; | 298 mutable int cached_heights_cursor_; |
264 | 299 |
265 DISALLOW_COPY_AND_ASSIGN(Label); | 300 DISALLOW_COPY_AND_ASSIGN(Label); |
266 }; | 301 }; |
267 | 302 |
268 } // namespace views | 303 } // namespace views |
269 | 304 |
270 #endif // UI_VIEWS_CONTROLS_LABEL_H_ | 305 #endif // UI_VIEWS_CONTROLS_LABEL_H_ |
OLD | NEW |