| 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 <cmath> | 8 #include <cmath> |
| 8 #include <limits> | 9 #include <limits> |
| 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 13 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 14 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 15 #include "ui/base/accessibility/accessible_view_state.h" | 17 #include "ui/base/accessibility/accessible_view_state.h" |
| 16 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/base/text/text_elider.h" | 19 #include "ui/base/text/text_elider.h" |
| 18 #include "ui/gfx/canvas_skia.h" | 20 #include "ui/gfx/canvas_skia.h" |
| 19 #include "ui/gfx/color_utils.h" | 21 #include "ui/gfx/color_utils.h" |
| 20 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
| 21 #include "ui/gfx/insets.h" | 23 #include "ui/gfx/insets.h" |
| 22 #include "views/background.h" | 24 #include "views/background.h" |
| 23 | 25 |
| 24 namespace views { | 26 namespace views { |
| 25 | 27 |
| 26 // The colors to use for enabled and disabled labels. | 28 // The colors to use for enabled and disabled labels. |
| 27 static SkColor kEnabledColor; | 29 static SkColor kEnabledColor; |
| 28 static SkColor kDisabledColor; | 30 static SkColor kDisabledColor; |
| 29 | 31 |
| 30 // static | 32 // static |
| 31 const char Label::kViewClassName[] = "views/Label"; | 33 const char Label::kViewClassName[] = "views/Label"; |
| 32 | 34 |
| 33 const int Label::kFocusBorderPadding = 1; | 35 const int Label::kFocusBorderPadding = 1; |
| 34 | 36 |
| 35 Label::Label() { | 37 Label::Label() { |
| 36 Init(std::wstring(), GetDefaultFont()); | 38 Init(string16(), GetDefaultFont()); |
| 37 } | 39 } |
| 38 | 40 |
| 39 Label::Label(const std::wstring& text) { | 41 Label::Label(const string16& text) { |
| 40 Init(text, GetDefaultFont()); | 42 Init(text, GetDefaultFont()); |
| 41 } | 43 } |
| 42 | 44 |
| 43 Label::Label(const std::wstring& text, const gfx::Font& font) { | 45 Label::Label(const string16& text, const gfx::Font& font) { |
| 44 Init(text, font); | 46 Init(text, font); |
| 45 } | 47 } |
| 46 | 48 |
| 47 Label::~Label() { | 49 Label::~Label() { |
| 48 } | 50 } |
| 49 | 51 |
| 50 void Label::SetFont(const gfx::Font& font) { | 52 void Label::SetFont(const gfx::Font& font) { |
| 51 font_ = font; | 53 font_ = font; |
| 52 text_size_valid_ = false; | 54 text_size_valid_ = false; |
| 53 PreferredSizeChanged(); | 55 PreferredSizeChanged(); |
| 54 SchedulePaint(); | 56 SchedulePaint(); |
| 55 } | 57 } |
| 56 | 58 |
| 57 void Label::SetText(const std::wstring& text) { | 59 void Label::SetText(const string16& text) { |
| 58 text_ = WideToUTF16Hack(text); | 60 text_ = text; |
| 59 url_set_ = false; | 61 url_set_ = false; |
| 60 text_size_valid_ = false; | 62 text_size_valid_ = false; |
| 61 PreferredSizeChanged(); | 63 PreferredSizeChanged(); |
| 62 SchedulePaint(); | 64 SchedulePaint(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 const std::wstring Label::GetText() const { | 67 const string16 Label::GetText() const { |
| 66 return url_set_ ? UTF8ToWide(url_.spec()) : UTF16ToWideHack(text_); | 68 return url_set_ ? UTF8ToUTF16(url_.spec()) : text_; |
| 67 } | 69 } |
| 68 | 70 |
| 69 void Label::SetURL(const GURL& url) { | 71 void Label::SetURL(const GURL& url) { |
| 70 url_ = url; | 72 url_ = url; |
| 71 text_ = UTF8ToUTF16(url_.spec()); | 73 text_ = UTF8ToUTF16(url_.spec()); |
| 72 url_set_ = true; | 74 url_set_ = true; |
| 73 text_size_valid_ = false; | 75 text_size_valid_ = false; |
| 74 PreferredSizeChanged(); | 76 PreferredSizeChanged(); |
| 75 SchedulePaint(); | 77 SchedulePaint(); |
| 76 } | 78 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 return false; | 254 return false; |
| 253 } | 255 } |
| 254 | 256 |
| 255 void Label::GetAccessibleState(ui::AccessibleViewState* state) { | 257 void Label::GetAccessibleState(ui::AccessibleViewState* state) { |
| 256 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; | 258 state->role = ui::AccessibilityTypes::ROLE_STATICTEXT; |
| 257 state->state = ui::AccessibilityTypes::STATE_READONLY; | 259 state->state = ui::AccessibilityTypes::STATE_READONLY; |
| 258 state->name = text_; | 260 state->name = text_; |
| 259 } | 261 } |
| 260 | 262 |
| 261 void Label::PaintText(gfx::Canvas* canvas, | 263 void Label::PaintText(gfx::Canvas* canvas, |
| 262 const std::wstring& text, | 264 const string16& text, |
| 263 const gfx::Rect& text_bounds, | 265 const gfx::Rect& text_bounds, |
| 264 int flags) { | 266 int flags) { |
| 265 canvas->DrawStringInt(WideToUTF16Hack(text), font_, color_, | 267 canvas->DrawStringInt(text, font_, color_, |
| 266 text_bounds.x(), text_bounds.y(), | 268 text_bounds.x(), text_bounds.y(), |
| 267 text_bounds.width(), text_bounds.height(), flags); | 269 text_bounds.width(), text_bounds.height(), flags); |
| 268 | 270 |
| 269 if (HasFocus() || paint_as_focused_) { | 271 if (HasFocus() || paint_as_focused_) { |
| 270 gfx::Rect focus_bounds = text_bounds; | 272 gfx::Rect focus_bounds = text_bounds; |
| 271 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | 273 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 272 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), | 274 canvas->DrawFocusRect(focus_bounds.x(), focus_bounds.y(), |
| 273 focus_bounds.width(), focus_bounds.height()); | 275 focus_bounds.width(), focus_bounds.height()); |
| 274 } | 276 } |
| 275 } | 277 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 296 return text_size_; | 298 return text_size_; |
| 297 } | 299 } |
| 298 | 300 |
| 299 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 301 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 300 text_size_valid_ &= !is_multi_line_; | 302 text_size_valid_ &= !is_multi_line_; |
| 301 } | 303 } |
| 302 | 304 |
| 303 void Label::OnPaint(gfx::Canvas* canvas) { | 305 void Label::OnPaint(gfx::Canvas* canvas) { |
| 304 OnPaintBackground(canvas); | 306 OnPaintBackground(canvas); |
| 305 | 307 |
| 306 std::wstring paint_text; | 308 string16 paint_text; |
| 307 gfx::Rect text_bounds; | 309 gfx::Rect text_bounds; |
| 308 int flags = 0; | 310 int flags = 0; |
| 309 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 311 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 310 PaintText(canvas, paint_text, text_bounds, flags); | 312 PaintText(canvas, paint_text, text_bounds, flags); |
| 311 } | 313 } |
| 312 | 314 |
| 313 void Label::OnPaintBackground(gfx::Canvas* canvas) { | 315 void Label::OnPaintBackground(gfx::Canvas* canvas) { |
| 314 const Background* bg = contains_mouse_ ? GetMouseOverBackground() : NULL; | 316 const Background* bg = contains_mouse_ ? GetMouseOverBackground() : NULL; |
| 315 if (!bg) | 317 if (!bg) |
| 316 bg = background(); | 318 bg = background(); |
| 317 if (bg) | 319 if (bg) |
| 318 bg->Paint(canvas, this); | 320 bg->Paint(canvas, this); |
| 319 } | 321 } |
| 320 | 322 |
| 321 // static | 323 // static |
| 322 gfx::Font Label::GetDefaultFont() { | 324 gfx::Font Label::GetDefaultFont() { |
| 323 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); | 325 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void Label::Init(const std::wstring& text, const gfx::Font& font) { | 328 void Label::Init(const string16& text, const gfx::Font& font) { |
| 327 static bool initialized = false; | 329 static bool initialized = false; |
| 328 if (!initialized) { | 330 if (!initialized) { |
| 329 #if defined(OS_WIN) | 331 #if defined(OS_WIN) |
| 330 kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); | 332 kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); |
| 331 kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); | 333 kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); |
| 332 #else | 334 #else |
| 333 // TODO(beng): source from theme provider. | 335 // TODO(beng): source from theme provider. |
| 334 kEnabledColor = SK_ColorBLACK; | 336 kEnabledColor = SK_ColorBLACK; |
| 335 kDisabledColor = SK_ColorGRAY; | 337 kDisabledColor = SK_ColorGRAY; |
| 336 #endif | 338 #endif |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 return flags; | 427 return flags; |
| 426 } | 428 } |
| 427 | 429 |
| 428 gfx::Rect Label::GetAvailableRect() const { | 430 gfx::Rect Label::GetAvailableRect() const { |
| 429 gfx::Rect bounds(gfx::Point(), size()); | 431 gfx::Rect bounds(gfx::Point(), size()); |
| 430 gfx::Insets insets(GetInsets()); | 432 gfx::Insets insets(GetInsets()); |
| 431 bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); | 433 bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); |
| 432 return bounds; | 434 return bounds; |
| 433 } | 435 } |
| 434 | 436 |
| 435 void Label::CalculateDrawStringParams(std::wstring* paint_text, | 437 void Label::CalculateDrawStringParams(string16* paint_text, |
| 436 gfx::Rect* text_bounds, | 438 gfx::Rect* text_bounds, |
| 437 int* flags) const { | 439 int* flags) const { |
| 438 DCHECK(paint_text && text_bounds && flags); | 440 DCHECK(paint_text && text_bounds && flags); |
| 439 | 441 |
| 440 if (url_set_) { | 442 if (url_set_) { |
| 441 // TODO(jungshik) : Figure out how to get 'intl.accept_languages' | 443 // TODO(jungshik) : Figure out how to get 'intl.accept_languages' |
| 442 // preference and use it when calling ElideUrl. | 444 // preference and use it when calling ElideUrl. |
| 443 *paint_text = UTF16ToWideHack( | 445 *paint_text = ui::ElideUrl(url_, font_, GetAvailableRect().width(), |
| 444 ui::ElideUrl(url_, font_, GetAvailableRect().width(), std::string())); | 446 std::string()); |
| 445 | 447 |
| 446 // An URLs is always treated as an LTR text and therefore we should | 448 // An URLs is always treated as an LTR text and therefore we should |
| 447 // explicitly mark it as such if the locale is RTL so that URLs containing | 449 // explicitly mark it as such if the locale is RTL so that URLs containing |
| 448 // Hebrew or Arabic characters are displayed correctly. | 450 // Hebrew or Arabic characters are displayed correctly. |
| 449 // | 451 // |
| 450 // Note that we don't check the View's UI layout setting in order to | 452 // Note that we don't check the View's UI layout setting in order to |
| 451 // determine whether or not to insert the special Unicode formatting | 453 // determine whether or not to insert the special Unicode formatting |
| 452 // characters. We use the locale settings because an URL is always treated | 454 // characters. We use the locale settings because an URL is always treated |
| 453 // as an LTR string, even if its containing view does not use an RTL UI | 455 // as an LTR string, even if its containing view does not use an RTL UI |
| 454 // layout. | 456 // layout. |
| 455 *paint_text = UTF16ToWide(base::i18n::GetDisplayStringInLTRDirectionality( | 457 *paint_text = base::i18n::GetDisplayStringInLTRDirectionality( |
| 456 WideToUTF16(*paint_text))); | 458 *paint_text); |
| 457 } else if (elide_in_middle_) { | 459 } else if (elide_in_middle_) { |
| 458 *paint_text = UTF16ToWideHack(ui::ElideText(text_, | 460 *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(), |
| 459 font_, GetAvailableRect().width(), true)); | 461 true); |
| 460 } else { | 462 } else { |
| 461 *paint_text = UTF16ToWideHack(text_); | 463 *paint_text = text_; |
| 462 } | 464 } |
| 463 | 465 |
| 464 *text_bounds = GetTextBounds(); | 466 *text_bounds = GetTextBounds(); |
| 465 *flags = ComputeMultiLineFlags(); | 467 *flags = ComputeMultiLineFlags(); |
| 466 // If rtl_alignment_mode_ is AUTO_DETECT_ALIGNMENT (such as for text from | 468 // If rtl_alignment_mode_ is AUTO_DETECT_ALIGNMENT (such as for text from |
| 467 // webpage, not from chrome's UI), its directionality is forced to be RTL if | 469 // webpage, not from chrome's UI), its directionality is forced to be RTL if |
| 468 // it is right aligned. Otherwise, its directionality is forced to be LTR. | 470 // it is right aligned. Otherwise, its directionality is forced to be LTR. |
| 469 if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) { | 471 if (rtl_alignment_mode_ == AUTO_DETECT_ALIGNMENT) { |
| 470 if (horiz_alignment_ == ALIGN_RIGHT) | 472 if (horiz_alignment_ == ALIGN_RIGHT) |
| 471 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; | 473 *flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; |
| 472 else | 474 else |
| 473 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | 475 *flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; |
| 474 } | 476 } |
| 475 } | 477 } |
| 476 | 478 |
| 477 } // namespace views | 479 } // namespace views |
| OLD | NEW |