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