Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: ui/views/controls/label.cc

Issue 9837059: Cleanup for views::Label: (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/label_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 47
48 void Label::SetFont(const gfx::Font& font) { 48 void Label::SetFont(const gfx::Font& font) {
49 font_ = font; 49 font_ = font;
50 text_size_valid_ = false; 50 text_size_valid_ = false;
51 PreferredSizeChanged(); 51 PreferredSizeChanged();
52 SchedulePaint(); 52 SchedulePaint();
53 } 53 }
54 54
55 void Label::SetText(const string16& text) { 55 void Label::SetText(const string16& text) {
56 text_ = text; 56 text_ = text;
57 url_set_ = false; 57 url_ = GURL();
58 text_size_valid_ = false; 58 text_size_valid_ = false;
59 PreferredSizeChanged(); 59 PreferredSizeChanged();
60 SchedulePaint(); 60 SchedulePaint();
61 } 61 }
62 62
63 const string16 Label::GetText() const {
64 return url_set_ ? UTF8ToUTF16(url_.spec()) : text_;
65 }
66
67 void Label::SetURL(const GURL& url) { 63 void Label::SetURL(const GURL& url) {
64 DCHECK(url.is_valid());
68 url_ = url; 65 url_ = url;
69 text_ = UTF8ToUTF16(url_.spec()); 66 text_ = UTF8ToUTF16(url_.spec());
70 url_set_ = true;
71 text_size_valid_ = false; 67 text_size_valid_ = false;
72 PreferredSizeChanged(); 68 PreferredSizeChanged();
73 SchedulePaint(); 69 SchedulePaint();
74 } 70 }
75 71
76 const GURL Label::GetURL() const {
77 return url_set_ ? url_ : GURL(UTF16ToUTF8(text_));
78 }
79
80 void Label::SetAutoColorReadabilityEnabled(bool enabled) { 72 void Label::SetAutoColorReadabilityEnabled(bool enabled) {
81 auto_color_readability_ = enabled; 73 auto_color_readability_ = enabled;
82 RecalculateColors(); 74 RecalculateColors();
83 } 75 }
84 76
85 void Label::SetEnabledColor(const SkColor& color) { 77 void Label::SetEnabledColor(const SkColor& color) {
86 requested_enabled_color_ = color; 78 requested_enabled_color_ = color;
87 RecalculateColors(); 79 RecalculateColors();
88 } 80 }
89 81
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 kDefaultDisabledColor = SK_ColorGRAY; 339 kDefaultDisabledColor = SK_ColorGRAY;
348 kDefaultBackgroundColor = SK_ColorWHITE; 340 kDefaultBackgroundColor = SK_ColorWHITE;
349 #endif 341 #endif
350 342
351 initialized = true; 343 initialized = true;
352 } 344 }
353 345
354 contains_mouse_ = false; 346 contains_mouse_ = false;
355 font_ = font; 347 font_ = font;
356 text_size_valid_ = false; 348 text_size_valid_ = false;
357 url_set_ = false;
358 requested_enabled_color_ = kDefaultEnabledColor; 349 requested_enabled_color_ = kDefaultEnabledColor;
359 requested_disabled_color_ = kDefaultDisabledColor; 350 requested_disabled_color_ = kDefaultDisabledColor;
360 background_color_ = kDefaultBackgroundColor; 351 background_color_ = kDefaultBackgroundColor;
361 auto_color_readability_ = true; 352 auto_color_readability_ = true;
362 RecalculateColors(); 353 RecalculateColors();
363 horiz_alignment_ = ALIGN_CENTER; 354 horiz_alignment_ = ALIGN_CENTER;
364 is_multi_line_ = false; 355 is_multi_line_ = false;
365 allow_character_break_ = false; 356 allow_character_break_ = false;
366 elide_in_middle_ = false; 357 elide_in_middle_ = false;
367 collapse_when_hidden_ = false; 358 collapse_when_hidden_ = false;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 418
428 int Label::ComputeDrawStringFlags() const { 419 int Label::ComputeDrawStringFlags() const {
429 int flags = 0; 420 int flags = 0;
430 421
431 // We can't use subpixel rendering if the background is non-opaque. 422 // We can't use subpixel rendering if the background is non-opaque.
432 if (SkColorGetA(background_color_) != 0xFF) 423 if (SkColorGetA(background_color_) != 0xFF)
433 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING; 424 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
434 425
435 if (directionality_mode_ == AUTO_DETECT_DIRECTIONALITY) { 426 if (directionality_mode_ == AUTO_DETECT_DIRECTIONALITY) {
436 base::i18n::TextDirection direction = 427 base::i18n::TextDirection direction =
437 base::i18n::GetFirstStrongCharacterDirection(GetText()); 428 base::i18n::GetFirstStrongCharacterDirection(text_);
438 if (direction == base::i18n::RIGHT_TO_LEFT) 429 if (direction == base::i18n::RIGHT_TO_LEFT)
439 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY; 430 flags |= gfx::Canvas::FORCE_RTL_DIRECTIONALITY;
440 else 431 else
441 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY; 432 flags |= gfx::Canvas::FORCE_LTR_DIRECTIONALITY;
442 } 433 }
443 434
444 if (!is_multi_line_) 435 if (!is_multi_line_)
445 return flags; 436 return flags;
446 437
447 flags |= gfx::Canvas::MULTI_LINE; 438 flags |= gfx::Canvas::MULTI_LINE;
(...skipping 27 matching lines...) Expand all
475 gfx::Insets insets(GetInsets()); 466 gfx::Insets insets(GetInsets());
476 bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); 467 bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
477 return bounds; 468 return bounds;
478 } 469 }
479 470
480 void Label::CalculateDrawStringParams(string16* paint_text, 471 void Label::CalculateDrawStringParams(string16* paint_text,
481 gfx::Rect* text_bounds, 472 gfx::Rect* text_bounds,
482 int* flags) const { 473 int* flags) const {
483 DCHECK(paint_text && text_bounds && flags); 474 DCHECK(paint_text && text_bounds && flags);
484 475
485 if (url_set_) { 476 if (!url_.is_empty()) {
486 // TODO(jungshik) : Figure out how to get 'intl.accept_languages' 477 // TODO(jungshik) : Figure out how to get 'intl.accept_languages'
487 // preference and use it when calling ElideUrl. 478 // preference and use it when calling ElideUrl.
488 *paint_text = 479 *paint_text =
489 ui::ElideUrl(url_, font_, GetAvailableRect().width(), std::string()); 480 ui::ElideUrl(url_, font_, GetAvailableRect().width(), std::string());
490 481
491 // An URLs is always treated as an LTR text and therefore we should 482 // An URLs is always treated as an LTR text and therefore we should
492 // explicitly mark it as such if the locale is RTL so that URLs containing 483 // explicitly mark it as such if the locale is RTL so that URLs containing
493 // Hebrew or Arabic characters are displayed correctly. 484 // Hebrew or Arabic characters are displayed correctly.
494 // 485 //
495 // Note that we don't check the View's UI layout setting in order to 486 // Note that we don't check the View's UI layout setting in order to
496 // determine whether or not to insert the special Unicode formatting 487 // determine whether or not to insert the special Unicode formatting
497 // characters. We use the locale settings because an URL is always treated 488 // characters. We use the locale settings because an URL is always treated
498 // as an LTR string, even if its containing view does not use an RTL UI 489 // as an LTR string, even if its containing view does not use an RTL UI
499 // layout. 490 // layout.
500 *paint_text = base::i18n::GetDisplayStringInLTRDirectionality( 491 *paint_text = base::i18n::GetDisplayStringInLTRDirectionality(
501 *paint_text); 492 *paint_text);
502 } else if (elide_in_middle_) { 493 } else if (elide_in_middle_) {
503 *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(), 494 *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(),
504 ui::ELIDE_IN_MIDDLE); 495 ui::ELIDE_IN_MIDDLE);
505 } else { 496 } else {
506 *paint_text = text_; 497 *paint_text = text_;
507 } 498 }
508 499
509 *text_bounds = GetTextBounds(); 500 *text_bounds = GetTextBounds();
510 *flags = ComputeDrawStringFlags(); 501 *flags = ComputeDrawStringFlags();
511 } 502 }
512 503
513 } // namespace views 504 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/label.h ('k') | ui/views/controls/label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698