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 #include "ui/views/controls/label.h"
12 #include "base/i18n/rtl.h"
13 #include "base/logging.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "ui/accessibility/ax_view_state.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/color_utils.h"
20 #include "ui/gfx/insets.h"
21 #include "ui/gfx/text_elider.h"
22 #include "ui/gfx/text_utils.h"
23 #include "ui/gfx/utf16_indexing.h"
24 #include "ui/native_theme/native_theme.h"
25 #include "ui/views/background.h"
29 const int kCachedSizeLimit = 10;
30 const base::char16 kPasswordReplacementChar = '*';
37 const char Label::kViewClassName[] = "Label";
38 const int Label::kFocusBorderPadding = 1;
41 Init(base::string16(), gfx::FontList());
44 Label::Label(const base::string16& text) {
45 Init(text, gfx::FontList());
48 Label::Label(const base::string16& text, const gfx::FontList& font_list) {
49 Init(text, font_list);
55 void Label::SetFontList(const gfx::FontList& font_list) {
56 font_list_ = font_list;
58 PreferredSizeChanged();
62 void Label::SetText(const base::string16& text) {
64 SetTextInternal(text);
67 void Label::SetTextInternal(const base::string16& text) {
71 size_t obscured_text_length =
72 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, text_.length()));
73 layout_text_.assign(obscured_text_length, kPasswordReplacementChar);
79 PreferredSizeChanged();
83 void Label::SetAutoColorReadabilityEnabled(bool enabled) {
84 auto_color_readability_ = enabled;
88 void Label::SetEnabledColor(SkColor color) {
89 requested_enabled_color_ = color;
90 enabled_color_set_ = true;
94 void Label::SetDisabledColor(SkColor color) {
95 requested_disabled_color_ = color;
96 disabled_color_set_ = true;
100 void Label::SetBackgroundColor(SkColor color) {
101 background_color_ = color;
102 background_color_set_ = true;
106 void Label::SetShadows(const gfx::ShadowValues& shadows) {
108 text_size_valid_ = false;
111 void Label::SetSubpixelRenderingEnabled(bool subpixel_rendering_enabled) {
112 subpixel_rendering_enabled_ = subpixel_rendering_enabled;
115 void Label::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
116 // If the UI layout is right-to-left, flip the alignment direction.
117 if (base::i18n::IsRTL() &&
118 (alignment == gfx::ALIGN_LEFT || alignment == gfx::ALIGN_RIGHT)) {
119 alignment = (alignment == gfx::ALIGN_LEFT) ?
120 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
122 if (horizontal_alignment_ != alignment) {
123 horizontal_alignment_ = alignment;
128 gfx::HorizontalAlignment Label::GetHorizontalAlignment() const {
129 if (horizontal_alignment_ != gfx::ALIGN_TO_HEAD)
130 return horizontal_alignment_;
132 const base::i18n::TextDirection dir =
133 base::i18n::GetFirstStrongCharacterDirection(layout_text_);
134 return dir == base::i18n::RIGHT_TO_LEFT ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
137 void Label::SetLineHeight(int height) {
138 if (height != line_height_) {
139 line_height_ = height;
141 PreferredSizeChanged();
146 void Label::SetMultiLine(bool multi_line) {
147 DCHECK(!multi_line || (elide_behavior_ == gfx::ELIDE_TAIL ||
148 elide_behavior_ == gfx::NO_ELIDE));
149 if (multi_line != multi_line_) {
150 multi_line_ = multi_line;
152 PreferredSizeChanged();
157 void Label::SetObscured(bool obscured) {
158 if (obscured != obscured_) {
159 obscured_ = obscured;
160 SetTextInternal(text_);
164 void Label::SetAllowCharacterBreak(bool allow_character_break) {
165 if (allow_character_break != allow_character_break_) {
166 allow_character_break_ = allow_character_break;
168 PreferredSizeChanged();
173 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
174 DCHECK(!multi_line_ || (elide_behavior_ == gfx::ELIDE_TAIL ||
175 elide_behavior_ == gfx::NO_ELIDE));
176 if (elide_behavior != elide_behavior_) {
177 elide_behavior_ = elide_behavior;
179 PreferredSizeChanged();
184 void Label::SetTooltipText(const base::string16& tooltip_text) {
185 tooltip_text_ = tooltip_text;
188 void Label::SizeToFit(int max_width) {
191 std::vector<base::string16> lines;
192 base::SplitString(layout_text_, '\n', &lines);
195 for (std::vector<base::string16>::const_iterator iter = lines.begin();
196 iter != lines.end(); ++iter) {
197 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_));
200 label_width += GetInsets().width();
203 label_width = std::min(label_width, max_width);
205 SetBounds(x(), y(), label_width, 0);
206 SizeToPreferredSize();
209 const base::string16& Label::GetLayoutTextForTesting() const {
213 gfx::Insets Label::GetInsets() const {
214 gfx::Insets insets = View::GetInsets();
216 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding,
217 kFocusBorderPadding, kFocusBorderPadding);
222 int Label::GetBaseline() const {
223 return GetInsets().top() + font_list_.GetBaseline();
226 gfx::Size Label::GetPreferredSize() const {
227 // Return a size of (0, 0) if the label is not visible and if the
228 // collapse_when_hidden_ flag is set.
229 // TODO(munjal): This logic probably belongs to the View class. But for now,
230 // put it here since putting it in View class means all inheriting classes
231 // need ot respect the collapse_when_hidden_ flag.
232 if (!visible() && collapse_when_hidden_)
235 gfx::Size size(GetTextSize());
236 gfx::Insets insets = GetInsets();
237 size.Enlarge(insets.width(), insets.height());
241 gfx::Size Label::GetMinimumSize() const {
242 gfx::Size text_size(GetTextSize());
243 if ((!visible() && collapse_when_hidden_) || text_size.IsEmpty())
246 gfx::Size size(gfx::GetStringWidth(base::string16(gfx::kEllipsisUTF16),
248 font_list_.GetHeight());
249 size.SetToMin(text_size); // The actual text may be shorter than an ellipsis.
250 gfx::Insets insets = GetInsets();
251 size.Enlarge(insets.width(), insets.height());
255 int Label::GetHeightForWidth(int w) const {
257 return View::GetHeightForWidth(w);
259 w = std::max(0, w - GetInsets().width());
261 for (size_t i = 0; i < cached_heights_.size(); ++i) {
262 const gfx::Size& s = cached_heights_[i];
264 return s.height() + GetInsets().height();
269 int h = font_list_.GetHeight();
270 const int flags = ComputeDrawStringFlags();
271 gfx::Canvas::SizeStringInt(
272 layout_text_, font_list_, &w, &h, line_height_, flags);
273 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h);
274 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit;
275 return h + GetInsets().height();
278 const char* Label::GetClassName() const {
279 return kViewClassName;
282 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) {
283 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip())
286 return HitTestPoint(point) ? this : NULL;
289 bool Label::CanProcessEventsWithinSubtree() const {
290 // Send events to the parent view for handling.
294 void Label::GetAccessibleState(ui::AXViewState* state) {
295 state->role = ui::AX_ROLE_STATIC_TEXT;
296 state->AddStateFlag(ui::AX_STATE_READ_ONLY);
297 state->name = layout_text_;
300 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const {
301 if (!tooltip_text_.empty()) {
302 tooltip->assign(tooltip_text_);
306 if (ShouldShowDefaultTooltip()) {
307 *tooltip = layout_text_;
314 void Label::PaintText(gfx::Canvas* canvas,
315 const base::string16& text,
316 const gfx::Rect& text_bounds,
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);
322 canvas->DrawStringRectWithShadows(text, font_list_, color, text_bounds,
323 line_height_, flags, shadows_);
327 gfx::Rect focus_bounds = text_bounds;
328 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
329 canvas->DrawFocusRect(focus_bounds);
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
339 int w = 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();
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;
358 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) {
359 text_size_valid_ &= !multi_line_;
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);
369 base::string16 paint_text;
370 gfx::Rect text_bounds;
372 CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
373 PaintText(canvas, paint_text, text_bounds, flags);
376 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) {
377 UpdateColorsFromTheme(theme);
380 void Label::Init(const base::string16& text, const gfx::FontList& font_list) {
381 font_list_ = font_list;
382 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false;
383 subpixel_rendering_enabled_ = true;
384 auto_color_readability_ = true;
385 UpdateColorsFromTheme(ui::NativeTheme::instance());
386 horizontal_alignment_ = gfx::ALIGN_CENTER;
390 allow_character_break_ = false;
391 elide_behavior_ = gfx::ELIDE_TAIL;
392 collapse_when_hidden_ = false;
393 cached_heights_.resize(kCachedSizeLimit);
399 void Label::RecalculateColors() {
400 actual_enabled_color_ = auto_color_readability_ ?
401 color_utils::GetReadableColor(requested_enabled_color_,
403 requested_enabled_color_;
404 actual_disabled_color_ = auto_color_readability_ ?
405 color_utils::GetReadableColor(requested_disabled_color_,
407 requested_disabled_color_;
410 gfx::Rect Label::GetTextBounds() const {
411 gfx::Rect available(GetAvailableRect());
412 gfx::Size text_size(GetTextSize());
413 text_size.set_width(std::min(available.width(), text_size.width()));
414 gfx::Point origin(GetInsets().left(), GetInsets().top());
415 switch (GetHorizontalAlignment()) {
416 case gfx::ALIGN_LEFT:
418 case gfx::ALIGN_CENTER:
419 // Put any extra margin pixel on the left to match the legacy behavior
420 // from the use of GetTextExtentPoint32() on Windows.
421 origin.Offset((available.width() + 1 - text_size.width()) / 2, 0);
423 case gfx::ALIGN_RIGHT:
424 origin.set_x(available.right() - text_size.width());
430 origin.Offset(0, std::max(0, (available.height() - text_size.height())) / 2);
431 return gfx::Rect(origin, text_size);
434 int Label::ComputeDrawStringFlags() const {
437 // We can't use subpixel rendering if the background is non-opaque.
438 if (SkColorGetA(background_color_) != 0xFF || !subpixel_rendering_enabled_)
439 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
441 base::i18n::TextDirection direction =
442 base::i18n::GetFirstStrongCharacterDirection(layout_text_);
443 if (direction == base::i18n::RIGHT_TO_LEFT)
444 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
446 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
448 switch (GetHorizontalAlignment()) {
449 case gfx::ALIGN_LEFT:
450 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
452 case gfx::ALIGN_CENTER:
453 flags |= gfx::Canvas::TEXT_ALIGN_CENTER;
455 case gfx::ALIGN_RIGHT:
456 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
466 flags |= gfx::Canvas::MULTI_LINE;
468 // Don't elide multiline labels on Linux.
469 // Todo(davemoore): Do we depend on eliding multiline text?
470 // Pango insists on limiting the number of lines to one if text is
471 // elided. You can get around this if you can pass a maximum height
472 // but we don't currently have that data when we call the pango code.
473 flags |= gfx::Canvas::NO_ELLIPSIS;
475 if (allow_character_break_)
476 flags |= gfx::Canvas::CHARACTER_BREAK;
481 gfx::Rect Label::GetAvailableRect() const {
482 gfx::Rect bounds(size());
483 bounds.Inset(GetInsets());
487 void Label::CalculateDrawStringParams(base::string16* paint_text,
488 gfx::Rect* text_bounds,
490 DCHECK(paint_text && text_bounds && flags);
492 const bool forbid_ellipsis = elide_behavior_ == gfx::NO_ELIDE ||
493 elide_behavior_ == gfx::FADE_TAIL;
494 if (multi_line_ || forbid_ellipsis) {
495 *paint_text = layout_text_;
497 *paint_text = gfx::ElideText(layout_text_, font_list_,
498 GetAvailableRect().width(), elide_behavior_);
501 *text_bounds = GetTextBounds();
502 *flags = ComputeDrawStringFlags();
503 // TODO(msw): Elide multi-line text with ElideRectangleText instead.
504 if (!multi_line_ || forbid_ellipsis)
505 *flags |= gfx::Canvas::NO_ELLIPSIS;
508 void Label::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
509 if (!enabled_color_set_) {
510 requested_enabled_color_ = theme->GetSystemColor(
511 ui::NativeTheme::kColorId_LabelEnabledColor);
513 if (!disabled_color_set_) {
514 requested_disabled_color_ = theme->GetSystemColor(
515 ui::NativeTheme::kColorId_LabelDisabledColor);
517 if (!background_color_set_) {
518 background_color_ = theme->GetSystemColor(
519 ui::NativeTheme::kColorId_LabelBackgroundColor);
524 void Label::ResetCachedSize() {
525 text_size_valid_ = false;
526 cached_heights_cursor_ = 0;
527 for (int i = 0; i < kCachedSizeLimit; ++i)
528 cached_heights_[i] = gfx::Size();
531 bool Label::ShouldShowDefaultTooltip() const {
532 const gfx::Size text_size = GetTextSize();
533 const gfx::Size size = GetContentsBounds().size();
534 return !obscured() && (text_size.width() > size.width() ||
535 (multi_line_ && text_size.height() > size.height()));