| 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 #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> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 namespace views { | 41 namespace views { |
| 42 | 42 |
| 43 // static | 43 // static |
| 44 const char Label::kViewClassName[] = "Label"; | 44 const char Label::kViewClassName[] = "Label"; |
| 45 | 45 |
| 46 Label::Label() { | 46 Label::Label() { |
| 47 Init(string16(), GetDefaultFontList()); | 47 Init(base::string16(), GetDefaultFontList()); |
| 48 } | 48 } |
| 49 | 49 |
| 50 Label::Label(const string16& text) { | 50 Label::Label(const base::string16& text) { |
| 51 Init(text, GetDefaultFontList()); | 51 Init(text, GetDefaultFontList()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 Label::Label(const string16& text, const gfx::FontList& font_list) { | 54 Label::Label(const base::string16& text, const gfx::FontList& font_list) { |
| 55 Init(text, font_list); | 55 Init(text, font_list); |
| 56 } | 56 } |
| 57 | 57 |
| 58 Label::Label(const string16& text, const gfx::Font& font) { | 58 Label::Label(const base::string16& text, const gfx::Font& font) { |
| 59 Init(text, gfx::FontList(font)); | 59 Init(text, gfx::FontList(font)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 Label::~Label() { | 62 Label::~Label() { |
| 63 } | 63 } |
| 64 | 64 |
| 65 void Label::SetFontList(const gfx::FontList& font_list) { | 65 void Label::SetFontList(const gfx::FontList& font_list) { |
| 66 font_list_ = font_list; | 66 font_list_ = font_list; |
| 67 ResetCachedSize(); | 67 ResetCachedSize(); |
| 68 PreferredSizeChanged(); | 68 PreferredSizeChanged(); |
| 69 SchedulePaint(); | 69 SchedulePaint(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 const gfx::Font& Label::font() const { | 72 const gfx::Font& Label::font() const { |
| 73 return font_list_.GetPrimaryFont(); | 73 return font_list_.GetPrimaryFont(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void Label::SetFont(const gfx::Font& font) { | 76 void Label::SetFont(const gfx::Font& font) { |
| 77 SetFontList(gfx::FontList(font)); | 77 SetFontList(gfx::FontList(font)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void Label::SetText(const string16& text) { | 80 void Label::SetText(const base::string16& text) { |
| 81 if (text == text_) | 81 if (text == text_) |
| 82 return; | 82 return; |
| 83 text_ = text; | 83 text_ = text; |
| 84 ResetCachedSize(); | 84 ResetCachedSize(); |
| 85 PreferredSizeChanged(); | 85 PreferredSizeChanged(); |
| 86 SchedulePaint(); | 86 SchedulePaint(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void Label::SetAutoColorReadabilityEnabled(bool enabled) { | 89 void Label::SetAutoColorReadabilityEnabled(bool enabled) { |
| 90 auto_color_readability_ = enabled; | 90 auto_color_readability_ = enabled; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 void Label::SetElideBehavior(ElideBehavior elide_behavior) { | 169 void Label::SetElideBehavior(ElideBehavior elide_behavior) { |
| 170 DCHECK(elide_behavior != ELIDE_IN_MIDDLE || !is_multi_line_); | 170 DCHECK(elide_behavior != ELIDE_IN_MIDDLE || !is_multi_line_); |
| 171 if (elide_behavior != elide_behavior_) { | 171 if (elide_behavior != elide_behavior_) { |
| 172 elide_behavior_ = elide_behavior; | 172 elide_behavior_ = elide_behavior; |
| 173 ResetCachedSize(); | 173 ResetCachedSize(); |
| 174 PreferredSizeChanged(); | 174 PreferredSizeChanged(); |
| 175 SchedulePaint(); | 175 SchedulePaint(); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 void Label::SetTooltipText(const string16& tooltip_text) { | 179 void Label::SetTooltipText(const base::string16& tooltip_text) { |
| 180 tooltip_text_ = tooltip_text; | 180 tooltip_text_ = tooltip_text; |
| 181 } | 181 } |
| 182 | 182 |
| 183 void Label::SizeToFit(int max_width) { | 183 void Label::SizeToFit(int max_width) { |
| 184 DCHECK(is_multi_line_); | 184 DCHECK(is_multi_line_); |
| 185 | 185 |
| 186 std::vector<string16> lines; | 186 std::vector<base::string16> lines; |
| 187 base::SplitString(text_, '\n', &lines); | 187 base::SplitString(text_, '\n', &lines); |
| 188 | 188 |
| 189 int label_width = 0; | 189 int label_width = 0; |
| 190 for (std::vector<string16>::const_iterator iter = lines.begin(); | 190 for (std::vector<base::string16>::const_iterator iter = lines.begin(); |
| 191 iter != lines.end(); ++iter) { | 191 iter != lines.end(); ++iter) { |
| 192 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); | 192 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 label_width += GetInsets().width(); | 195 label_width += GetInsets().width(); |
| 196 | 196 |
| 197 if (max_width > 0) | 197 if (max_width > 0) |
| 198 label_width = std::min(label_width, max_width); | 198 label_width = std::min(label_width, max_width); |
| 199 | 199 |
| 200 SetBounds(x(), y(), label_width, 0); | 200 SetBounds(x(), y(), label_width, 0); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip()) | 275 if (tooltip_text_.empty() && !ShouldShowDefaultTooltip()) |
| 276 return NULL; | 276 return NULL; |
| 277 | 277 |
| 278 return this; | 278 return this; |
| 279 } | 279 } |
| 280 | 280 |
| 281 bool Label::HitTestRect(const gfx::Rect& rect) const { | 281 bool Label::HitTestRect(const gfx::Rect& rect) const { |
| 282 return false; | 282 return false; |
| 283 } | 283 } |
| 284 | 284 |
| 285 bool Label::GetTooltipText(const gfx::Point& p, string16* tooltip) const { | 285 bool Label::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { |
| 286 DCHECK(tooltip); | 286 DCHECK(tooltip); |
| 287 | 287 |
| 288 // If a tooltip has been explicitly set, use it. | 288 // If a tooltip has been explicitly set, use it. |
| 289 if (!tooltip_text_.empty()) { | 289 if (!tooltip_text_.empty()) { |
| 290 tooltip->assign(tooltip_text_); | 290 tooltip->assign(tooltip_text_); |
| 291 return true; | 291 return true; |
| 292 } | 292 } |
| 293 | 293 |
| 294 // Show the full text if the text does not fit. | 294 // Show the full text if the text does not fit. |
| 295 if (ShouldShowDefaultTooltip()) { | 295 if (ShouldShowDefaultTooltip()) { |
| 296 *tooltip = text_; | 296 *tooltip = text_; |
| 297 return true; | 297 return true; |
| 298 } | 298 } |
| 299 | 299 |
| 300 return false; | 300 return false; |
| 301 } | 301 } |
| 302 | 302 |
| 303 void Label::GetAccessibleState(ui::AccessibleViewState* state) { | 303 void Label::GetAccessibleState(ui::AccessibleViewState* state) { |
| 304 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; | 304 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; |
| 305 state->state = ui::AccessibilityTypes::STATE_READONLY; | 305 state->state = ui::AccessibilityTypes::STATE_READONLY; |
| 306 state->name = text_; | 306 state->name = text_; |
| 307 } | 307 } |
| 308 | 308 |
| 309 void Label::PaintText(gfx::Canvas* canvas, | 309 void Label::PaintText(gfx::Canvas* canvas, |
| 310 const string16& text, | 310 const base::string16& text, |
| 311 const gfx::Rect& text_bounds, | 311 const gfx::Rect& text_bounds, |
| 312 int flags) { | 312 int flags) { |
| 313 gfx::ShadowValues shadows; | 313 gfx::ShadowValues shadows; |
| 314 if (has_shadow_) | 314 if (has_shadow_) |
| 315 shadows.push_back(gfx::ShadowValue(shadow_offset_, 0, | 315 shadows.push_back(gfx::ShadowValue(shadow_offset_, 0, |
| 316 enabled() ? enabled_shadow_color_ : disabled_shadow_color_)); | 316 enabled() ? enabled_shadow_color_ : disabled_shadow_color_)); |
| 317 canvas->DrawStringRectWithShadows(text, font_list_, | 317 canvas->DrawStringRectWithShadows(text, font_list_, |
| 318 enabled() ? actual_enabled_color_ : actual_disabled_color_, | 318 enabled() ? actual_enabled_color_ : actual_disabled_color_, |
| 319 text_bounds, line_height_, flags, shadows); | 319 text_bounds, line_height_, flags, shadows); |
| 320 | 320 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 351 text_size_valid_ &= !is_multi_line_; | 351 text_size_valid_ &= !is_multi_line_; |
| 352 } | 352 } |
| 353 | 353 |
| 354 void Label::OnPaint(gfx::Canvas* canvas) { | 354 void Label::OnPaint(gfx::Canvas* canvas) { |
| 355 OnPaintBackground(canvas); | 355 OnPaintBackground(canvas); |
| 356 // We skip painting the focus border because it is being handled seperately by | 356 // We skip painting the focus border because it is being handled seperately by |
| 357 // some subclasses of Label. We do not want View's focus border painting to | 357 // some subclasses of Label. We do not want View's focus border painting to |
| 358 // interfere with that. | 358 // interfere with that. |
| 359 OnPaintBorder(canvas); | 359 OnPaintBorder(canvas); |
| 360 | 360 |
| 361 string16 paint_text; | 361 base::string16 paint_text; |
| 362 gfx::Rect text_bounds; | 362 gfx::Rect text_bounds; |
| 363 int flags = 0; | 363 int flags = 0; |
| 364 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 364 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 365 PaintText(canvas, paint_text, text_bounds, flags); | 365 PaintText(canvas, paint_text, text_bounds, flags); |
| 366 } | 366 } |
| 367 | 367 |
| 368 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 368 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 369 UpdateColorsFromTheme(theme); | 369 UpdateColorsFromTheme(theme); |
| 370 } | 370 } |
| 371 | 371 |
| 372 void Label::Init(const string16& text, const gfx::FontList& font_list) { | 372 void Label::Init(const base::string16& text, const gfx::FontList& font_list) { |
| 373 font_list_ = font_list; | 373 font_list_ = font_list; |
| 374 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; | 374 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; |
| 375 auto_color_readability_ = true; | 375 auto_color_readability_ = true; |
| 376 UpdateColorsFromTheme(ui::NativeTheme::instance()); | 376 UpdateColorsFromTheme(ui::NativeTheme::instance()); |
| 377 horizontal_alignment_ = gfx::ALIGN_CENTER; | 377 horizontal_alignment_ = gfx::ALIGN_CENTER; |
| 378 line_height_ = 0; | 378 line_height_ = 0; |
| 379 is_multi_line_ = false; | 379 is_multi_line_ = false; |
| 380 allow_character_break_ = false; | 380 allow_character_break_ = false; |
| 381 elide_behavior_ = ELIDE_AT_END; | 381 elide_behavior_ = ELIDE_AT_END; |
| 382 collapse_when_hidden_ = false; | 382 collapse_when_hidden_ = false; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 478 |
| 479 return flags; | 479 return flags; |
| 480 } | 480 } |
| 481 | 481 |
| 482 gfx::Rect Label::GetAvailableRect() const { | 482 gfx::Rect Label::GetAvailableRect() const { |
| 483 gfx::Rect bounds(size()); | 483 gfx::Rect bounds(size()); |
| 484 bounds.Inset(GetInsets()); | 484 bounds.Inset(GetInsets()); |
| 485 return bounds; | 485 return bounds; |
| 486 } | 486 } |
| 487 | 487 |
| 488 void Label::CalculateDrawStringParams(string16* paint_text, | 488 void Label::CalculateDrawStringParams(base::string16* paint_text, |
| 489 gfx::Rect* text_bounds, | 489 gfx::Rect* text_bounds, |
| 490 int* flags) const { | 490 int* flags) const { |
| 491 DCHECK(paint_text && text_bounds && flags); | 491 DCHECK(paint_text && text_bounds && flags); |
| 492 | 492 |
| 493 // TODO(msw): Use ElideRectangleText to support eliding multi-line text. Once | 493 // TODO(msw): Use ElideRectangleText to support eliding multi-line text. Once |
| 494 // this is done, we can set NO_ELLIPSIS unconditionally at the bottom. | 494 // this is done, we can set NO_ELLIPSIS unconditionally at the bottom. |
| 495 if (is_multi_line_ || (elide_behavior_ == NO_ELIDE)) { | 495 if (is_multi_line_ || (elide_behavior_ == NO_ELIDE)) { |
| 496 *paint_text = text_; | 496 *paint_text = text_; |
| 497 } else if (elide_behavior_ == ELIDE_IN_MIDDLE) { | 497 } else if (elide_behavior_ == ELIDE_IN_MIDDLE) { |
| 498 *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), | 498 *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 for (int i = 0; i < kCachedSizeLimit; ++i) | 534 for (int i = 0; i < kCachedSizeLimit; ++i) |
| 535 cached_heights_[i] = gfx::Size(); | 535 cached_heights_[i] = gfx::Size(); |
| 536 } | 536 } |
| 537 | 537 |
| 538 bool Label::ShouldShowDefaultTooltip() const { | 538 bool Label::ShouldShowDefaultTooltip() const { |
| 539 return !is_multi_line_ && | 539 return !is_multi_line_ && |
| 540 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); | 540 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); |
| 541 } | 541 } |
| 542 | 542 |
| 543 } // namespace views | 543 } // namespace views |
| OLD | NEW |