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

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

Issue 23228004: Prepare to use gfx::RenderText in views::Label. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix multiline LabelButton layout and sizing. 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/label.h ('k') | ui/views/controls/label_unittest.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 #include "ui/views/controls/label.h" 5 #include "ui/views/controls/label.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/i18n/rtl.h" 12 #include "base/i18n/rtl.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
17 #include "ui/accessibility/ax_view_state.h" 16 #include "ui/accessibility/ax_view_state.h"
18 #include "ui/base/resource/resource_bundle.h" 17 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/color_utils.h" 19 #include "ui/gfx/color_utils.h"
21 #include "ui/gfx/insets.h"
22 #include "ui/gfx/text_elider.h" 20 #include "ui/gfx/text_elider.h"
23 #include "ui/gfx/text_utils.h"
24 #include "ui/gfx/utf16_indexing.h"
25 #include "ui/native_theme/native_theme.h" 21 #include "ui/native_theme/native_theme.h"
26 #include "ui/views/background.h"
27
28 namespace {
29
30 const int kCachedSizeLimit = 10;
31 const base::char16 kPasswordReplacementChar = '*';
32
33 } // namespace
34 22
35 namespace views { 23 namespace views {
36 24
37 // static 25 // static
38 const char Label::kViewClassName[] = "Label"; 26 const char Label::kViewClassName[] = "Label";
39 const int Label::kFocusBorderPadding = 1; 27 const int Label::kFocusBorderPadding = 1;
40 28
41 Label::Label() { 29 Label::Label() {
42 Init(base::string16(), gfx::FontList()); 30 Init(base::string16(), gfx::FontList());
43 } 31 }
44 32
45 Label::Label(const base::string16& text) { 33 Label::Label(const base::string16& text) {
46 Init(text, gfx::FontList()); 34 Init(text, gfx::FontList());
47 } 35 }
48 36
49 Label::Label(const base::string16& text, const gfx::FontList& font_list) { 37 Label::Label(const base::string16& text, const gfx::FontList& font_list) {
50 Init(text, font_list); 38 Init(text, font_list);
51 } 39 }
52 40
53 Label::~Label() { 41 Label::~Label() {
54 } 42 }
55 43
56 void Label::SetFontList(const gfx::FontList& font_list) { 44 void Label::SetFontList(const gfx::FontList& font_list) {
57 font_list_ = font_list; 45 render_text_->SetFontList(font_list);
58 ResetCachedSize(); 46 ResetLayout();
59 PreferredSizeChanged();
60 SchedulePaint();
61 } 47 }
62 48
63 void Label::SetText(const base::string16& text) { 49 void Label::SetText(const base::string16& text) {
64 if (text != text_) 50 render_text_->SetText(text);
65 SetTextInternal(text); 51 ResetLayout();
66 }
67
68 void Label::SetTextInternal(const base::string16& text) {
69 text_ = text;
70
71 if (is_obscured_) {
72 size_t obscured_text_length =
73 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length()));
74 layout_text_.assign(obscured_text_length, kPasswordReplacementChar);
75 } else {
76 layout_text_ = text_;
77 }
78
79 ResetCachedSize();
80 PreferredSizeChanged();
81 SchedulePaint();
82 } 52 }
83 53
84 void Label::SetAutoColorReadabilityEnabled(bool enabled) { 54 void Label::SetAutoColorReadabilityEnabled(bool enabled) {
85 auto_color_readability_ = enabled; 55 auto_color_readability_ = enabled;
86 RecalculateColors(); 56 RecalculateColors();
87 } 57 }
88 58
89 void Label::SetEnabledColor(SkColor color) { 59 void Label::SetEnabledColor(SkColor color) {
90 requested_enabled_color_ = color; 60 requested_enabled_color_ = color;
91 enabled_color_set_ = true; 61 enabled_color_set_ = true;
92 RecalculateColors(); 62 RecalculateColors();
93 } 63 }
94 64
95 void Label::SetDisabledColor(SkColor color) { 65 void Label::SetDisabledColor(SkColor color) {
96 requested_disabled_color_ = color; 66 requested_disabled_color_ = color;
97 disabled_color_set_ = true; 67 disabled_color_set_ = true;
98 RecalculateColors(); 68 RecalculateColors();
99 } 69 }
100 70
101 void Label::SetBackgroundColor(SkColor color) { 71 void Label::SetBackgroundColor(SkColor color) {
102 background_color_ = color; 72 background_color_ = color;
103 background_color_set_ = true; 73 background_color_set_ = true;
104 RecalculateColors(); 74 RecalculateColors();
105 } 75 }
106 76
77 void Label::SetShadows(const gfx::ShadowValues& shadows) {
78 render_text_->set_shadows(shadows);
79 ResetLayout();
80 }
81
82 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) {
83 subpixel_rendering_enabled_ = subpixel_rendering_enabled;
84 RecalculateColors();
85 }
86
107 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { 87 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
108 // If the UI layout is right-to-left, flip the alignment direction. 88 // If the UI layout is right-to-left, flip the alignment direction.
109 if (base::i18n::IsRTL() && 89 if (base::i18n::IsRTL() &&
110 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) { 90 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) {
111 alignment = (alignment == gfx::ALIGN_LEFT) ? 91 alignment = (alignment == gfx::ALIGN_LEFT) ?
112 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; 92 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
113 } 93 }
114 if (horizontal_alignment_ != alignment) { 94 render_text_->SetHorizontalAlignment(alignment);
115 horizontal_alignment_ = alignment; 95 ResetLayout();
116 SchedulePaint();
117 }
118 }
119
120 gfx::HorizontalAlignment Label::GetHorizontalAlignment() const {
121 if (horizontal_alignment_ != gfx::ALIGN_TO_HEAD)
122 return horizontal_alignment_;
123
124 const base::i18n::TextDirection dir =
125 base::i18n::GetFirstStrongCharacterDirection(layout_text());
126 return dir == base::i18n::RIGHT_TO_LEFT ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
127 } 96 }
128 97
129 void Label::SetLineHeight(int height) { 98 void Label::SetLineHeight(int height) {
130 if (height != line_height_) { 99 if (line_height_ == height)
131 line_height_ = height; 100 return;
132 ResetCachedSize(); 101
133 PreferredSizeChanged(); 102 line_height_ = height;
134 SchedulePaint(); 103 ResetLayout();
135 }
136 } 104 }
137 105
138 void Label::SetMultiLine(bool multi_line) { 106 void Label::SetMultiLine(bool multi_line) {
139 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL || 107 if (multi_line_ == multi_line)
140 elide_behavior_ == gfx::NO_ELIDE)); 108 return;
141 if (multi_line != is_multi_line_) { 109
142 is_multi_line_ = multi_line; 110 multi_line_ = multi_line;
143 ResetCachedSize(); 111 render_text_->SetReplaceNewlineCharsWithSymbols(!multi_line);
144 PreferredSizeChanged(); 112 ResetLayout();
145 SchedulePaint();
146 }
147 } 113 }
148 114
149 void Label::SetObscured(bool obscured) { 115 void Label::SetObscured(bool obscured) {
150 if (obscured != is_obscured_) { 116 render_text_->SetObscured(obscured);
151 is_obscured_ = obscured; 117 ResetLayout();
152 SetTextInternal(text_);
153 }
154 } 118 }
155 119
156 void Label::SetAllowCharacterBreak(bool allow_character_break) { 120 void Label::SetAllowCharacterBreak(bool allow_character_break) {
157 if (allow_character_break != allow_character_break_) { 121 if (allow_character_break_ == allow_character_break)
158 allow_character_break_ = allow_character_break; 122 return;
159 ResetCachedSize(); 123
160 PreferredSizeChanged(); 124 allow_character_break_ = allow_character_break;
161 SchedulePaint(); 125 ResetLayout();
162 }
163 } 126 }
164 127
165 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { 128 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
166 DCHECK(!is_multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL || 129 if (elide_behavior_ == elide_behavior)
167 elide_behavior_ == gfx::NO_ELIDE)); 130 return;
168 if (elide_behavior != elide_behavior_) { 131
169 elide_behavior_ = elide_behavior; 132 elide_behavior_ = elide_behavior;
170 ResetCachedSize(); 133 ResetLayout();
171 PreferredSizeChanged();
172 SchedulePaint();
173 }
174 } 134 }
175 135
176 void Label::SetTooltipText(const base::string16& tooltip_text) { 136 void Label::SetTooltipText(const base::string16& tooltip_text) {
177 tooltip_text_ = tooltip_text; 137 tooltip_text_ = tooltip_text;
178 } 138 }
179 139
180 void Label::SizeToFit(int max_width) { 140 void Label::SizeToFit(int max_width) {
181 DCHECK(is_multi_line_); 141 DCHECK(multi_line_);
142 max_width_ = max_width;
143 SizeToPreferredSize();
144 }
182 145
183 std::vector<base::string16> lines; 146 const base::string16& Label::GetLayoutTextForTesting() const {
184 base::SplitString(layout_text(), '\n', &lines); 147 return render_text_->layout_text();
185
186 int label_width = 0;
187 for (std::vector<base::string16>::const_iterator iter = lines.begin();
188 iter != lines.end(); ++iter) {
189 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_));
190 }
191
192 label_width += GetInsets().width();
193
194 if (max_width > 0)
195 label_width = std::min(label_width, max_width);
196
197 SetBounds(x(), y(), label_width, 0);
198 SizeToPreferredSize();
199 } 148 }
200 149
201 gfx::Insets Label::GetInsets() const { 150 gfx::Insets Label::GetInsets() const {
202 gfx::Insets insets = View::GetInsets(); 151 gfx::Insets insets = View::GetInsets();
203 if (focusable()) { 152 if (focusable()) {
204 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, 153 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding,
205 kFocusBorderPadding, kFocusBorderPadding); 154 kFocusBorderPadding, kFocusBorderPadding);
206 } 155 }
207 return insets; 156 return insets;
208 } 157 }
209 158
210 int Label::GetBaseline() const { 159 int Label::GetBaseline() const {
211 return GetInsets().top() + font_list_.GetBaseline(); 160 return GetInsets().top() + font_list().GetBaseline();
212 } 161 }
213 162
214 gfx::Size Label::GetPreferredSize() const { 163 gfx::Size Label::GetPreferredSize() const {
215 // Return a size of (0, 0) if the label is not visible and if the
216 // collapse_when_hidden_ flag is set.
217 // TODO(munjal): This logic probably belongs to the View class. But for now,
218 // put it here since putting it in View class means all inheriting classes
219 // need ot respect the collapse_when_hidden_ flag.
220 if (!visible() && collapse_when_hidden_) 164 if (!visible() && collapse_when_hidden_)
221 return gfx::Size(); 165 return gfx::Size();
222 166
167 if (multi_line_ && max_width_ != 0)
168 return gfx::Size(max_width_, GetHeightForWidth(max_width_));
169
223 gfx::Size size(GetTextSize()); 170 gfx::Size size(GetTextSize());
224 gfx::Insets insets = GetInsets(); 171 size.Enlarge(GetInsets().width(), GetInsets().height());
225 size.Enlarge(insets.width(), insets.height());
226 return size; 172 return size;
227 } 173 }
228 174
229 gfx::Size Label::GetMinimumSize() const { 175 gfx::Size Label::GetMinimumSize() const {
230 gfx::Size text_size(GetTextSize()); 176 if (!visible() && collapse_when_hidden_)
231 if ((!visible() && collapse_when_hidden_) || text_size.IsEmpty())
sky 2014/07/14 21:49:56 Does empty text still get a pref of 0x0?
msw 2014/07/14 23:41:36 Label used to return (0 x <font height>) for empty
232 return gfx::Size(); 177 return gfx::Size();
233 178
234 gfx::Size size(gfx::GetStringWidth(base::string16(gfx::kEllipsisUTF16), 179 gfx::Size size(0, font_list().GetHeight());
235 font_list_), 180 if (elide_behavior_ == gfx::ELIDE_HEAD ||
236 font_list_.GetHeight()); 181 elide_behavior_ == gfx::ELIDE_MIDDLE ||
237 size.SetToMin(text_size); // The actual text may be shorter than an ellipsis. 182 elide_behavior_ == gfx::ELIDE_TAIL ||
238 gfx::Insets insets = GetInsets(); 183 elide_behavior_ == gfx::ELIDE_EMAIL) {
239 size.Enlarge(insets.width(), insets.height()); 184 size.set_width(gfx::Canvas::GetStringWidth(
185 base::string16(gfx::kEllipsisUTF16), font_list()));
186 }
187 if (!multi_line_)
188 size.SetToMin(GetTextSize());
189 size.Enlarge(GetInsets().width(), GetInsets().height());
240 return size; 190 return size;
241 } 191 }
242 192
243 int Label::GetHeightForWidth(int w) const { 193 int Label::GetHeightForWidth(int w) const {
244 if (!is_multi_line_) 194 w -= GetInsets().width();
245 return View::GetHeightForWidth(w); 195 if (!multi_line_ || text().empty() || w <= 0)
196 return std::max(line_height_, font_list().GetHeight());
246 197
247 w = std::max(0, w - GetInsets().width()); 198 std::vector<base::string16> lines = GetLinesForWidth(w);
199 int height = lines.size() * std::max(line_height_, font_list().GetHeight());
200 height -= gfx::ShadowValue::GetMargin(render_text_->shadows()).height();
201 return height + GetInsets().height();
202 }
248 203
249 for (size_t i = 0; i < cached_heights_.size(); ++i) { 204 void Label::Layout() {
250 const gfx::Size& s = cached_heights_[i]; 205 lines_.clear();
251 if (s.width() == w) 206 gfx::Rect rect = GetContentsBounds();
252 return s.height() + GetInsets().height(); 207 if (rect.width() == 0 || rect.height() == 0)
208 return;
209
210 std::vector<base::string16> lines;
211 gfx::HorizontalAlignment alignment = horizontal_alignment();
212 gfx::DirectionalityMode directionality = render_text_->directionality_mode();
213 if (multi_line_) {
214 // Force the directionality and alignment of the first line on other lines.
215 bool rtl = render_text_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
216 if (alignment == gfx::ALIGN_TO_HEAD)
217 alignment = rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
218 directionality =
219 rtl ? gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR;
220 lines = GetLinesForWidth(rect.width());
221 if (lines.size() > 1)
222 rect.set_height(std::max(line_height_, font_list().GetHeight()));
223 } else {
224 lines.push_back(render_text_->layout_text());
253 } 225 }
254 226
255 int cache_width = w; 227 const int bottom = GetContentsBounds().bottom();
256 228 for (size_t i = 0; i < lines.size() && rect.y() <= bottom; ++i) {
257 int h = font_list_.GetHeight(); 229 scoped_ptr<gfx::RenderText> line(gfx::RenderText::CreateInstance());
258 const int flags = ComputeDrawStringFlags(); 230 line->SetHorizontalAlignment(alignment);
259 gfx::Canvas::SizeStringInt( 231 line->SetDirectionalityMode(directionality);
260 layout_text(), font_list_, &w, &h, line_height_, flags); 232 line->SetElideBehavior(elide_behavior_);
261 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); 233 line->SetFontList(font_list());
262 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; 234 line->set_shadows(shadows());
263 return h + GetInsets().height(); 235 line->SetCursorEnabled(false);
236 line->SetText(lines[i]);
237 line->SetDisplayRect(rect);
238 lines_.push_back(line.release());
239 rect.set_y(rect.y() + rect.height());
240 }
241 for (size_t i = lines_.size(); i < lines.size(); ++i)
242 lines_.back()->SetText(lines_.back()->text() + lines[i]);
243 RecalculateColors();
264 } 244 }
265 245
266 const char* Label::GetClassName() const { 246 const char* Label::GetClassName() const {
267 return kViewClassName; 247 return kViewClassName;
268 } 248 }
269 249
270 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { 250 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) {
271 // Bail out if the label does not contain the point.
272 // Note that HitTestPoint() cannot be used here as it uses
273 // Label::HitTestRect() to determine if the point hits the label; and
274 // Label::HitTestRect() always fails. Instead, default HitTestRect()
275 // implementation should be used.
276 if (!View::HitTestRect(gfx::Rect(point, gfx::Size(1, 1))))
277 return NULL;
278
279 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip()) 251 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip())
280 return NULL; 252 return NULL;
281 253
282 return this; 254 return HitTestPoint(point) ? this : NULL;
283 } 255 }
284 256
285 bool Label::CanProcessEventsWithinSubtree() const { 257 bool Label::CanProcessEventsWithinSubtree() const {
286 // Send events to the parent view for handling. 258 // Send events to the parent view for handling.
287 return false; 259 return false;
288 } 260 }
289 261
262 void Label::GetAccessibleState(ui::AXViewState* state) {
263 state->role = ui::AX_ROLE_STATIC_TEXT;
264 state->AddStateFlag(ui::AX_STATE_READ_ONLY);
265 state->name = render_text_->layout_text();
266 }
267
290 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { 268 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const {
291 DCHECK(tooltip);
292
293 // If a tooltip has been explicitly set, use it.
294 if (!tooltip_text_.empty()) { 269 if (!tooltip_text_.empty()) {
295 tooltip->assign(tooltip_text_); 270 tooltip->assign(tooltip_text_);
296 return true; 271 return true;
297 } 272 }
298 273
299 // Show the full text if the text does not fit.
300 if (ShouldShowDefaultTooltip()) { 274 if (ShouldShowDefaultTooltip()) {
301 *tooltip = layout_text(); 275 tooltip->assign(render_text_->layout_text());
302 return true; 276 return true;
303 } 277 }
304 278
305 return false; 279 return false;
306 } 280 }
307 281
308 void Label::GetAccessibleState(ui::AXViewState* state) { 282 void Label::OnEnabledChanged() {
309 state->role = ui::AX_ROLE_STATIC_TEXT; 283 RecalculateColors();
310 state->AddStateFlag(ui::AX_STATE_READ_ONLY);
311 state->name = layout_text();
312 } 284 }
313 285
314 void Label::PaintText(gfx::Canvas* canvas, 286 void Label::PaintText(gfx::Canvas* canvas) {
315 const base::string16& text, 287 for (size_t i = 0; i < lines_.size(); ++i)
316 const gfx::Rect& text_bounds, 288 lines_[i]->Draw(canvas);
317 int flags) { 289 }
318 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_;
319 if (elide_behavior_ == gfx::FADE_TAIL) {
320 canvas->DrawFadedString(text, font_list_, color, text_bounds, flags);
321 } else {
322 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds,
323 line_height_, flags, shadows_);
324 }
325 290
291 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) {
292 if (previous_bounds.size() != size())
293 Layout();
sky 2014/07/14 21:49:56 Seems better to me to delay layout. Is there a rea
msw 2014/07/14 23:41:35 I changed this to call ResetLayout(), which no lon
msw 2014/07/15 02:11:17 Actually, I changed this to InvalidateLayout as yo
294 }
295
296 void Label::OnPaint(gfx::Canvas* canvas) {
297 View::OnPaint(canvas);
326 if (HasFocus()) { 298 if (HasFocus()) {
327 gfx::Rect focus_bounds = text_bounds; 299 gfx::Rect focus_bounds = GetLocalBounds();
328 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); 300 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
329 canvas->DrawFocusRect(focus_bounds); 301 canvas->DrawFocusRect(focus_bounds);
330 } 302 }
331 } 303 PaintText(canvas);
sky 2014/07/14 21:49:56 Do any subclasses completely overriding PaintText
msw 2014/07/14 23:41:36 Nope, there are no Label::PaintText overrides. Thi
332
333 gfx::Size Label::GetTextSize() const {
334 if (!text_size_valid_) {
335 // For single-line strings, we supply the largest possible width, because
336 // while adding NO_ELLIPSIS to the flags works on Windows for forcing
337 // SizeStringInt() to calculate the desired width, it doesn't seem to work
338 // on Linux.
339 int w = is_multi_line_ ?
340 GetAvailableRect().width() : std::numeric_limits<int>::max();
341 int h = font_list_.GetHeight();
342 // For single-line strings, ignore the available width and calculate how
343 // wide the text wants to be.
344 int flags = ComputeDrawStringFlags();
345 if (!is_multi_line_)
346 flags |= gfx::Canvas::NO_ELLIPSIS;
347 gfx::Canvas::SizeStringInt(
348 layout_text(), font_list_, &w, &h, line_height_, flags);
349 text_size_.SetSize(w, h);
350 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows_);
351 text_size_.Enlarge(shadow_margin.width(), shadow_margin.height());
352 text_size_valid_ = true;
353 }
354
355 return text_size_;
356 }
357
358 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) {
359 text_size_valid_ &= !is_multi_line_;
360 }
361
362 void Label::OnPaint(gfx::Canvas* canvas) {
363 OnPaintBackground(canvas);
364 // We skip painting the focus border because it is being handled seperately by
365 // some subclasses of Label. We do not want View's focus border painting to
366 // interfere with that.
367 OnPaintBorder(canvas);
368
369 base::string16 paint_text;
370 gfx::Rect text_bounds;
371 int flags = 0;
372 CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
373 PaintText(canvas, paint_text, text_bounds, flags);
374 } 304 }
375 305
376 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { 306 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) {
377 UpdateColorsFromTheme(theme); 307 UpdateColorsFromTheme(theme);
378 } 308 }
379 309
380 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { 310 void Label::Init(const base::string16& text, const gfx::FontList& font_list) {
381 font_list_ = font_list; 311 render_text_.reset(gfx::RenderText::CreateInstance());
312 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
313 render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_TEXT);
314 render_text_->SetElideBehavior(gfx::NO_ELIDE);
315 render_text_->SetFontList(font_list);
316 render_text_->SetCursorEnabled(false);
317
318 elide_behavior_ = gfx::ELIDE_TAIL;
382 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; 319 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
383 subpixel_rendering_enabled_ = true; 320 subpixel_rendering_enabled_ = true;
384 auto_color_readability_ = true; 321 auto_color_readability_ = true;
385 UpdateColorsFromTheme(ui::NativeTheme::instance()); 322 UpdateColorsFromTheme(ui::NativeTheme::instance());
386 horizontal_alignment_ = gfx::ALIGN_CENTER; 323 collapse_when_hidden_ = false;
324 allow_character_break_ = false;
325 multi_line_ = false;
387 line_height_ = 0; 326 line_height_ = 0;
388 is_multi_line_ = false; 327 max_width_ = 0;
389 is_obscured_ = false; 328 SetText(text);
390 allow_character_break_ = false; 329 }
391 elide_behavior_ = gfx::ELIDE_TAIL;
392 collapse_when_hidden_ = false;
393 directionality_mode_ = gfx::DIRECTIONALITY_FROM_UI;
394 cached_heights_.resize(kCachedSizeLimit);
395 ResetCachedSize();
396 330
397 SetText(text); 331 void Label::ResetLayout() {
332 PreferredSizeChanged();
333 InvalidateLayout();
sky 2014/07/14 21:49:56 I think you should InvalidateLayout() first. That
msw 2014/07/14 23:41:35 Done. I reordered InvalidateLayout() and removed t
334 Layout();
335 SchedulePaint();
336 }
337
338 std::vector<base::string16> Label::GetLinesForWidth(int width) const {
339 std::vector<base::string16> lines;
340 const gfx::WordWrapBehavior wrap =
341 allow_character_break_ ? gfx::WRAP_LONG_WORDS : gfx::TRUNCATE_LONG_WORDS;
342 gfx::ElideRectangleText(render_text_->layout_text(),
343 font_list(),
344 width,
345 std::numeric_limits<int>::max(),
346 wrap,
347 &lines);
348 return lines;
349 }
350
351 gfx::Size Label::GetTextSize() const {
352 gfx::Size size;
353 if (!multi_line_) {
354 size = render_text_->GetStringSize();
355 } else {
356 // Get the natural text size, unelided and only wrapped on newlines.
357 std::vector<base::string16> lines;
358 base::SplitString(render_text_->layout_text(), '\n', &lines);
359 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance());
360 render_text->SetFontList(font_list());
361 for (size_t i = 0; i < lines.size(); ++i) {
362 render_text->SetText(lines[i]);
363 const gfx::Size line = render_text->GetStringSize();
364 size.set_width(std::max(size.width(), line.width()));
365 size.set_height(size.height() + std::max(line_height_, line.height()));
366 }
367 }
368 const gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows());
369 size.Enlarge(shadow_margin.width(), shadow_margin.height());
370 return size;
398 } 371 }
399 372
400 void Label::RecalculateColors() { 373 void Label::RecalculateColors() {
401 actual_enabled_color_ = auto_color_readability_ ? 374 actual_enabled_color_ = auto_color_readability_ ?
402 color_utils::GetReadableColor(requested_enabled_color_, 375 color_utils::GetReadableColor(requested_enabled_color_,
403 background_color_) : 376 background_color_) :
404 requested_enabled_color_; 377 requested_enabled_color_;
405 actual_disabled_color_ = auto_color_readability_ ? 378 actual_disabled_color_ = auto_color_readability_ ?
406 color_utils::GetReadableColor(requested_disabled_color_, 379 color_utils::GetReadableColor(requested_disabled_color_,
407 background_color_) : 380 background_color_) :
408 requested_disabled_color_; 381 requested_disabled_color_;
409 }
410 382
411 gfx::Rect Label::GetTextBounds() const { 383 SkColor color = enabled() ? actual_enabled_color_ : actual_disabled_color_;
412 gfx::Rect available(GetAvailableRect()); 384 bool background_is_transparent =
413 gfx::Size text_size(GetTextSize()); 385 SkColorGetA(background_color_) != 0xFF && !subpixel_rendering_enabled_;
414 text_size.set_width(std::min(available.width(), text_size.width())); 386 for (size_t i = 0; i < lines_.size(); ++i) {
415 gfx::Point origin(GetInsets().left(), GetInsets().top()); 387 lines_[i]->SetColor(color);
416 switch (GetHorizontalAlignment()) { 388 lines_[i]->set_background_is_transparent(background_is_transparent);
417 case gfx::ALIGN_LEFT:
418 break;
419 case gfx::ALIGN_CENTER:
420 // Put any extra margin pixel on the left to match the legacy behavior
421 // from the use of GetTextExtentPoint32() on Windows.
422 origin.Offset((available.width() + 1 - text_size.width()) / 2, 0);
423 break;
424 case gfx::ALIGN_RIGHT:
425 origin.set_x(available.right() - text_size.width());
426 break;
427 default:
428 NOTREACHED();
429 break;
430 } 389 }
431 origin.Offset(0, std::max(0, (available.height() - text_size.height())) / 2); 390 SchedulePaint();
432 return gfx::Rect(origin, text_size);
433 }
434
435 int Label::ComputeDrawStringFlags() const {
436 int flags = 0;
437
438 // We can't use subpixel rendering if the background is non-opaque.
439 if (SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_)
440 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
441
442 if (directionality_mode_ == gfx::DIRECTIONALITY_FORCE_LTR) {
443 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
444 } else if (directionality_mode_ == gfx::DIRECTIONALITY_FORCE_RTL) {
445 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
446 } else if (directionality_mode_ == gfx::DIRECTIONALITY_FROM_TEXT) {
447 base::i18n::TextDirection direction =
448 base::i18n::GetFirstStrongCharacterDirection(layout_text());
449 if (direction == base::i18n::RIGHT_TO_LEFT)
450 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
451 else
452 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
453 }
454
455 switch (GetHorizontalAlignment()) {
456 case gfx::ALIGN_LEFT:
457 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
458 break;
459 case gfx::ALIGN_CENTER:
460 flags |= gfx::Canvas::TEXT_ALIGN_CENTER;
461 break;
462 case gfx::ALIGN_RIGHT:
463 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
464 break;
465 default:
466 NOTREACHED();
467 break;
468 }
469
470 if (!is_multi_line_)
471 return flags;
472
473 flags |= gfx::Canvas::MULTI_LINE;
474 #if !defined(OS_WIN)
475 // Don't elide multiline labels on Linux.
476 // Todo(davemoore): Do we depend on eliding multiline text?
477 // Pango insists on limiting the number of lines to one if text is
478 // elided. You can get around this if you can pass a maximum height
479 // but we don't currently have that data when we call the pango code.
480 flags |= gfx::Canvas::NO_ELLIPSIS;
481 #endif
482 if (allow_character_break_)
483 flags |= gfx::Canvas::CHARACTER_BREAK;
484
485 return flags;
486 }
487
488 gfx::Rect Label::GetAvailableRect() const {
489 gfx::Rect bounds(size());
490 bounds.Inset(GetInsets());
491 return bounds;
492 }
493
494 void Label::CalculateDrawStringParams(base::string16* paint_text,
495 gfx::Rect* text_bounds,
496 int* flags) const {
497 DCHECK(paint_text && text_bounds && flags);
498
499 const bool forbid_ellipsis = elide_behavior_ == gfx::NO_ELIDE ||
500 elide_behavior_ == gfx::FADE_TAIL;
501 if (is_multi_line_ || forbid_ellipsis) {
502 *paint_text = layout_text();
503 } else {
504 *paint_text = gfx::ElideText(layout_text(), font_list_,
505 GetAvailableRect().width(), elide_behavior_);
506 }
507
508 *text_bounds = GetTextBounds();
509 *flags = ComputeDrawStringFlags();
510 // TODO(msw): Elide multi-line text with ElideRectangleText instead.
511 if (!is_multi_line_ || forbid_ellipsis)
512 *flags |= gfx::Canvas::NO_ELLIPSIS;
513 } 391 }
514 392
515 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) { 393 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
516 if (!enabled_color_set_) { 394 if (!enabled_color_set_) {
517 requested_enabled_color_ = theme->GetSystemColor( 395 requested_enabled_color_ = theme->GetSystemColor(
518 ui::NativeTheme::kColorId_LabelEnabledColor); 396 ui::NativeTheme::kColorId_LabelEnabledColor);
519 } 397 }
520 if (!disabled_color_set_) { 398 if (!disabled_color_set_) {
521 requested_disabled_color_ = theme->GetSystemColor( 399 requested_disabled_color_ = theme->GetSystemColor(
522 ui::NativeTheme::kColorId_LabelDisabledColor); 400 ui::NativeTheme::kColorId_LabelDisabledColor);
523 } 401 }
524 if (!background_color_set_) { 402 if (!background_color_set_) {
525 background_color_ = theme->GetSystemColor( 403 background_color_ = theme->GetSystemColor(
526 ui::NativeTheme::kColorId_LabelBackgroundColor); 404 ui::NativeTheme::kColorId_LabelBackgroundColor);
527 } 405 }
528 RecalculateColors(); 406 RecalculateColors();
529 } 407 }
530 408
531 void Label::ResetCachedSize() {
532 text_size_valid_ = false;
533 cached_heights_cursor_ = 0;
534 for (int i = 0; i < kCachedSizeLimit; ++i)
535 cached_heights_[i] = gfx::Size();
536 }
537
538 bool Label::ShouldShowDefaultTooltip() const { 409 bool Label::ShouldShowDefaultTooltip() const {
539 return !is_multi_line_ && !is_obscured_ && 410 const gfx::Size text_size = GetTextSize();
540 gfx::GetStringWidth(layout_text(), font_list_) > 411 const gfx::Size size = GetContentsBounds().size();
541 GetAvailableRect().width(); 412 return !obscured() && (text_size.width() > size.width() ||
413 text_size.height() > size.height());
542 } 414 }
543 415
544 } // namespace views 416 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698