| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/controls/label.h" | 5 #include "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/string_split.h" | 14 #include "base/string_split.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "ui/base/accessibility/accessible_view_state.h" | 17 #include "ui/base/accessibility/accessible_view_state.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/base/text/text_elider.h" | 19 #include "ui/base/text/text_elider.h" |
| 20 #include "ui/gfx/canvas_skia.h" | 20 #include "ui/gfx/canvas_skia.h" |
| 21 #include "ui/gfx/color_utils.h" | 21 #include "ui/gfx/color_utils.h" |
| 22 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
| 23 #include "ui/gfx/insets.h" | 23 #include "ui/gfx/insets.h" |
| 24 #include "views/background.h" | 24 #include "views/background.h" |
| 25 | 25 |
| 26 namespace views { | 26 namespace views { |
| 27 | 27 |
| 28 // The colors to use for enabled and disabled labels. | |
| 29 static SkColor kEnabledColor; | |
| 30 static SkColor kDisabledColor; | |
| 31 | |
| 32 // static | 28 // static |
| 33 const char Label::kViewClassName[] = "views/Label"; | 29 const char Label::kViewClassName[] = "views/Label"; |
| 34 | 30 |
| 35 const int Label::kFocusBorderPadding = 1; | 31 const int Label::kFocusBorderPadding = 1; |
| 36 | 32 |
| 37 Label::Label() { | 33 Label::Label() { |
| 38 Init(string16(), GetDefaultFont()); | 34 Init(string16(), GetDefaultFont()); |
| 39 } | 35 } |
| 40 | 36 |
| 41 Label::Label(const string16& text) { | 37 Label::Label(const string16& text) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 url_set_ = true; | 70 url_set_ = true; |
| 75 text_size_valid_ = false; | 71 text_size_valid_ = false; |
| 76 PreferredSizeChanged(); | 72 PreferredSizeChanged(); |
| 77 SchedulePaint(); | 73 SchedulePaint(); |
| 78 } | 74 } |
| 79 | 75 |
| 80 const GURL Label::GetURL() const { | 76 const GURL Label::GetURL() const { |
| 81 return url_set_ ? url_ : GURL(UTF16ToUTF8(text_)); | 77 return url_set_ ? url_ : GURL(UTF16ToUTF8(text_)); |
| 82 } | 78 } |
| 83 | 79 |
| 84 void Label::SetColor(const SkColor& color) { | 80 void Label::SetAutoColorReadabilityEnabled(bool enabled) { |
| 85 color_ = color; | 81 auto_color_readability_ = enabled; |
| 82 RecalculateColors(); |
| 86 } | 83 } |
| 87 | 84 |
| 88 SkColor Label::GetColor() const { | 85 void Label::SetEnabledColor(const SkColor& color) { |
| 89 return color_; | 86 requested_enabled_color_ = color; |
| 87 RecalculateColors(); |
| 90 } | 88 } |
| 91 | 89 |
| 92 void Label::MakeReadableOverBackgroundColor(const SkColor& background_color) { | 90 void Label::SetDisabledColor(const SkColor& color) { |
| 93 SetColor(color_utils::GetReadableColor( | 91 requested_disabled_color_ = color; |
| 94 IsEnabled() ? kEnabledColor : kDisabledColor, background_color)); | 92 RecalculateColors(); |
| 93 } |
| 94 |
| 95 void Label::SetBackgroundColor(const SkColor& color) { |
| 96 background_color_ = color; |
| 97 RecalculateColors(); |
| 95 } | 98 } |
| 96 | 99 |
| 97 void Label::SetHorizontalAlignment(Alignment alignment) { | 100 void Label::SetHorizontalAlignment(Alignment alignment) { |
| 98 // If the View's UI layout is right-to-left and rtl_alignment_mode_ is | 101 // If the View's UI layout is right-to-left and rtl_alignment_mode_ is |
| 99 // USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment | 102 // USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment |
| 100 // settings take into account the text directionality. | 103 // settings take into account the text directionality. |
| 101 if (base::i18n::IsRTL() && (rtl_alignment_mode_ == USE_UI_ALIGNMENT) && | 104 if (base::i18n::IsRTL() && (rtl_alignment_mode_ == USE_UI_ALIGNMENT) && |
| 102 (alignment != ALIGN_CENTER)) | 105 (alignment != ALIGN_CENTER)) |
| 103 alignment = (alignment == ALIGN_LEFT) ? ALIGN_RIGHT : ALIGN_LEFT; | 106 alignment = (alignment == ALIGN_LEFT) ? ALIGN_RIGHT : ALIGN_LEFT; |
| 104 if (horiz_alignment_ != alignment) { | 107 if (horiz_alignment_ != alignment) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 if (!is_multi_line_) | 212 if (!is_multi_line_) |
| 210 return View::GetHeightForWidth(w); | 213 return View::GetHeightForWidth(w); |
| 211 | 214 |
| 212 w = std::max(0, w - GetInsets().width()); | 215 w = std::max(0, w - GetInsets().width()); |
| 213 int h = font_.GetHeight(); | 216 int h = font_.GetHeight(); |
| 214 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, | 217 gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, |
| 215 ComputeMultiLineFlags()); | 218 ComputeMultiLineFlags()); |
| 216 return h + GetInsets().height(); | 219 return h + GetInsets().height(); |
| 217 } | 220 } |
| 218 | 221 |
| 219 void Label::OnEnabledChanged() { | |
| 220 View::OnEnabledChanged(); | |
| 221 SetColor(IsEnabled() ? kEnabledColor : kDisabledColor); | |
| 222 } | |
| 223 | |
| 224 std::string Label::GetClassName() const { | 222 std::string Label::GetClassName() const { |
| 225 return kViewClassName; | 223 return kViewClassName; |
| 226 } | 224 } |
| 227 | 225 |
| 228 bool Label::HitTest(const gfx::Point& l) const { | 226 bool Label::HitTest(const gfx::Point& l) const { |
| 229 return false; | 227 return false; |
| 230 } | 228 } |
| 231 | 229 |
| 232 void Label::OnMouseMoved(const MouseEvent& event) { | 230 void Label::OnMouseMoved(const MouseEvent& event) { |
| 233 UpdateContainsMouse(event); | 231 UpdateContainsMouse(event); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 262 void Label::GetAccessibleState(ui::AccessibleViewState* state) { | 260 void Label::GetAccessibleState(ui::AccessibleViewState* state) { |
| 263 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; | 261 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; |
| 264 state->state = ui::AccessibilityTypes::STATE_READONLY; | 262 state->state = ui::AccessibilityTypes::STATE_READONLY; |
| 265 state->name = text_; | 263 state->name = text_; |
| 266 } | 264 } |
| 267 | 265 |
| 268 void Label::PaintText(gfx::Canvas* canvas, | 266 void Label::PaintText(gfx::Canvas* canvas, |
| 269 const string16& text, | 267 const string16& text, |
| 270 const gfx::Rect& text_bounds, | 268 const gfx::Rect& text_bounds, |
| 271 int flags) { | 269 int flags) { |
| 272 canvas->DrawStringInt(text, font_, color_, | 270 canvas->DrawStringInt(text, font_, |
| 273 text_bounds.x(), text_bounds.y(), | 271 IsEnabled() ? actual_enabled_color_ : actual_disabled_color_, |
| 274 text_bounds.width(), text_bounds.height(), flags); | 272 text_bounds.x(), text_bounds.y(), text_bounds.width(), |
| 273 text_bounds.height(), flags); |
| 275 | 274 |
| 276 if (HasFocus() || paint_as_focused_) { | 275 if (HasFocus() || paint_as_focused_) { |
| 277 gfx::Rect focus_bounds = text_bounds; | 276 gfx::Rect focus_bounds = text_bounds; |
| 278 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | 277 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 279 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), | 278 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), |
| 280 focus_bounds.width(), focus_bounds.height()); | 279 focus_bounds.width(), focus_bounds.height()); |
| 281 } | 280 } |
| 282 } | 281 } |
| 283 | 282 |
| 284 gfx::Size Label::GetTextSize() const { | 283 gfx::Size Label::GetTextSize() const { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 bg->Paint(canvas, this); | 324 bg->Paint(canvas, this); |
| 326 } | 325 } |
| 327 | 326 |
| 328 // static | 327 // static |
| 329 gfx::Font Label::GetDefaultFont() { | 328 gfx::Font Label::GetDefaultFont() { |
| 330 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); | 329 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); |
| 331 } | 330 } |
| 332 | 331 |
| 333 void Label::Init(const string16& text, const gfx::Font& font) { | 332 void Label::Init(const string16& text, const gfx::Font& font) { |
| 334 static bool initialized = false; | 333 static bool initialized = false; |
| 334 static SkColor kDefaultEnabledColor; |
| 335 static SkColor kDefaultDisabledColor; |
| 336 static SkColor kDefaultBackgroundColor; |
| 335 if (!initialized) { | 337 if (!initialized) { |
| 336 #if defined(OS_WIN) | 338 #if defined(OS_WIN) |
| 337 kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); | 339 kDefaultEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); |
| 338 kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); | 340 kDefaultDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); |
| 341 kDefaultBackgroundColor = color_utils::GetSysSkColor(COLOR_WINDOW); |
| 339 #else | 342 #else |
| 340 // TODO(beng): source from theme provider. | 343 // TODO(beng): source from theme provider. |
| 341 kEnabledColor = SK_ColorBLACK; | 344 kDefaultEnabledColor = SK_ColorBLACK; |
| 342 kDisabledColor = SK_ColorGRAY; | 345 kDefaultDisabledColor = SK_ColorGRAY; |
| 346 kDefaultBackgroundColor = SK_ColorWHITE; |
| 343 #endif | 347 #endif |
| 344 | 348 |
| 345 initialized = true; | 349 initialized = true; |
| 346 } | 350 } |
| 347 | 351 |
| 348 contains_mouse_ = false; | 352 contains_mouse_ = false; |
| 349 font_ = font; | 353 font_ = font; |
| 350 text_size_valid_ = false; | 354 text_size_valid_ = false; |
| 351 SetText(text); | |
| 352 url_set_ = false; | 355 url_set_ = false; |
| 353 color_ = kEnabledColor; | 356 requested_enabled_color_ = kDefaultEnabledColor; |
| 357 requested_disabled_color_ = kDefaultDisabledColor; |
| 358 background_color_ = kDefaultBackgroundColor; |
| 359 auto_color_readability_ = true; |
| 360 RecalculateColors(); |
| 354 horiz_alignment_ = ALIGN_CENTER; | 361 horiz_alignment_ = ALIGN_CENTER; |
| 355 is_multi_line_ = false; | 362 is_multi_line_ = false; |
| 356 allow_character_break_ = false; | 363 allow_character_break_ = false; |
| 357 elide_in_middle_ = false; | 364 elide_in_middle_ = false; |
| 358 collapse_when_hidden_ = false; | 365 collapse_when_hidden_ = false; |
| 359 rtl_alignment_mode_ = USE_UI_ALIGNMENT; | 366 rtl_alignment_mode_ = USE_UI_ALIGNMENT; |
| 360 paint_as_focused_ = false; | 367 paint_as_focused_ = false; |
| 361 has_focus_border_ = false; | 368 has_focus_border_ = false; |
| 369 |
| 370 SetText(text); |
| 371 } |
| 372 |
| 373 void Label::RecalculateColors() { |
| 374 actual_enabled_color_ = auto_color_readability_ ? |
| 375 color_utils::GetReadableColor(requested_enabled_color_, |
| 376 background_color_) : |
| 377 requested_enabled_color_; |
| 378 actual_disabled_color_ = auto_color_readability_ ? |
| 379 color_utils::GetReadableColor(requested_disabled_color_, |
| 380 background_color_) : |
| 381 requested_disabled_color_; |
| 362 } | 382 } |
| 363 | 383 |
| 364 void Label::UpdateContainsMouse(const MouseEvent& event) { | 384 void Label::UpdateContainsMouse(const MouseEvent& event) { |
| 365 SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); | 385 SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); |
| 366 } | 386 } |
| 367 | 387 |
| 368 void Label::SetContainsMouse(bool contains_mouse) { | 388 void Label::SetContainsMouse(bool contains_mouse) { |
| 369 if (contains_mouse_ == contains_mouse) | 389 if (contains_mouse_ == contains_mouse) |
| 370 return; | 390 return; |
| 371 contains_mouse_ = contains_mouse; | 391 contains_mouse_ = contains_mouse; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 // it is right aligned. Otherwise, its directionality is forced to be LTR. | 495 // it is right aligned. Otherwise, its directionality is forced to be LTR. |
| 476 if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) { | 496 if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) { |
| 477 if (horiz_alignment_ == ALIGN_RIGHT) | 497 if (horiz_alignment_ == ALIGN_RIGHT) |
| 478 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; | 498 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; |
| 479 else | 499 else |
| 480 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | 500 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; |
| 481 } | 501 } |
| 482 } | 502 } |
| 483 | 503 |
| 484 } // namespace views | 504 } // namespace views |
| OLD | NEW |