diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-22 21:15:28 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-22 21:15:28 +0000 |
commit | 5c8899c6e31f922eae11e20367d71acde966d2ce (patch) | |
tree | 6fe3d13c20a6b65c1ec59adaa629855bd01aa75e | |
parent | f0e1d9fa17f7f2fd64d29b7c797baab0000b9125 (diff) | |
download | chromium_src-5c8899c6e31f922eae11e20367d71acde966d2ce.zip chromium_src-5c8899c6e31f922eae11e20367d71acde966d2ce.tar.gz chromium_src-5c8899c6e31f922eae11e20367d71acde966d2ce.tar.bz2 |
Rewrite/clean up a bunch of label.cc to fix the following problems:
* Focus rects on single-line labels could be the wrong height. Turns out SizeStringInt() doesn't adjust the height of a single-line string because it assumes you know your font's height. Passing in a default height of the font height fixed this.
* Remove a bunch of confusing code/functions that did totally different things in single- and multi-line mode, and make things handle both modes and do what you'd expect.
* Make functions be in the same order in the header and .cc files.
* Make several things const.
* Simplify.
BUG=38679
TEST=Radio buttons in content blocked bubbles do not produce weird-looking focus rects when clicked. The third page of the Options dialog lays out correctly, and checkboxes don't produce too-wide focus rects when clicked. In RTL languages, the Options dialog still looks right, with no string clipping.
Review URL: http://codereview.chromium.org/1072007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42256 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | views/controls/button/checkbox.cc | 2 | ||||
-rw-r--r-- | views/controls/label.cc | 457 | ||||
-rw-r--r-- | views/controls/label.h | 76 | ||||
-rw-r--r-- | views/controls/label_unittest.cc | 10 |
4 files changed, 220 insertions, 325 deletions
diff --git a/views/controls/button/checkbox.cc b/views/controls/button/checkbox.cc index 1dcb80a..ac756cd 100644 --- a/views/controls/button/checkbox.cc +++ b/views/controls/button/checkbox.cc @@ -207,7 +207,7 @@ void Checkbox::Init(const std::wstring& label_text) { // Checkboxs don't need to enforce a minimum size. set_ignore_minimum_size(true); label_ = new Label(label_text); - label_->set_has_focus_border(true); + label_->SetHasFocusBorder(true); label_->SetHorizontalAlignment(Label::ALIGN_LEFT); AddChildView(label_); } diff --git a/views/controls/label.cc b/views/controls/label.cc index 0116b5a..e4f61fa 100644 --- a/views/controls/label.cc +++ b/views/controls/label.cc @@ -28,7 +28,7 @@ SkColor Label::kEnabledColor, Label::kDisabledColor; static const int kFocusBorderPadding = 1; Label::Label() { - Init(L"", GetDefaultFont()); + Init(std::wstring(), GetDefaultFont()); } Label::Label(const std::wstring& text) { @@ -39,62 +39,19 @@ Label::Label(const std::wstring& text, const gfx::Font& font) { Init(text, font); } -void Label::Init(const std::wstring& text, const gfx::Font& font) { - static bool initialized = false; - if (!initialized) { -#if defined(OS_WIN) - kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); - kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); -#else - // TODO(beng): source from theme provider. - kEnabledColor = SK_ColorBLACK; - kDisabledColor = SK_ColorGRAY; -#endif - - initialized = true; - } - - contains_mouse_ = false; - font_ = font; - text_size_valid_ = false; - SetText(text); - url_set_ = false; - color_ = kEnabledColor; - highlight_color_ = kEnabledColor; - horiz_alignment_ = ALIGN_CENTER; - is_multi_line_ = false; - allow_character_break_ = false; - collapse_when_hidden_ = false; - rtl_alignment_mode_ = USE_UI_ALIGNMENT; - paint_as_focused_ = false; - has_focus_border_ = false; - highlighted_ = false; -} - Label::~Label() { } gfx::Size Label::GetPreferredSize() { - gfx::Size prefsize; - // Return a size of (0, 0) if the label is not visible and if the // collapse_when_hidden_ flag is set. // TODO(munjal): This logic probably belongs to the View class. But for now, // put it here since putting it in View class means all inheriting classes // need ot respect the collapse_when_hidden_ flag. if (!IsVisible() && collapse_when_hidden_) - return prefsize; - - if (is_multi_line_) { - int w = width(), h = 0; - gfx::Canvas::SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); - // TODO(erikkay) With highlighted_ enabled, should we adjust the size - // in the multi-line case? - prefsize.SetSize(w, h); - } else { - prefsize = GetTextSize(); - } + return gfx::Size(); + gfx::Size prefsize(GetTextSize()); gfx::Insets insets = GetInsets(); prefsize.Enlarge(insets.width(), insets.height()); return prefsize; @@ -104,68 +61,15 @@ int Label::GetBaseline() { return GetInsets().top() + font_.baseline(); } -int Label::ComputeMultiLineFlags() { - int flags = gfx::Canvas::MULTI_LINE; -#if !defined(OS_WIN) - // Don't ellide multiline labels on Linux. - // Todo(davemoore): Do we depend on elliding multiline text? - // Pango insists on limiting the number of lines to one if text is - // ellided. You can get around this if you can pass a maximum height - // but we don't currently have that data when we call the pango code. - flags |= gfx::Canvas::NO_ELLIPSIS; -#endif - if (allow_character_break_) - flags |= gfx::Canvas::CHARACTER_BREAK; - switch (horiz_alignment_) { - case ALIGN_LEFT: - flags |= gfx::Canvas::TEXT_ALIGN_LEFT; - break; - case ALIGN_CENTER: - flags |= gfx::Canvas::TEXT_ALIGN_CENTER; - break; - case ALIGN_RIGHT: - flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; - break; - } - return flags; +int Label::GetHeightForWidth(int w) { + return is_multi_line_ ? + (GetTextSize().height() + GetInsets().height()) : + View::GetHeightForWidth(w); } -void Label::CalculateDrawStringParams(std::wstring* paint_text, - gfx::Rect* text_bounds, - int* flags) { - DCHECK(paint_text && text_bounds && flags); - - if (url_set_) { - // TODO(jungshik) : Figure out how to get 'intl.accept_languages' - // preference and use it when calling ElideUrl. - *paint_text = gfx::ElideUrl(url_, font_, width(), std::wstring()); - - // An URLs is always treated as an LTR text and therefore we should - // explicitly mark it as such if the locale is RTL so that URLs containing - // Hebrew or Arabic characters are displayed correctly. - // - // Note that we don't check the View's UI layout setting in order to - // determine whether or not to insert the special Unicode formatting - // characters. We use the locale settings because an URL is always treated - // as an LTR string, even if its containing view does not use an RTL UI - // layout. - if (base::i18n::IsRTL()) - base::i18n::WrapStringWithLTRFormatting(paint_text); - } else { - *paint_text = text_; - } - - if (is_multi_line_) { - gfx::Insets insets = GetInsets(); - text_bounds->SetRect(insets.left(), - insets.top(), - width() - insets.width(), - height() - insets.height()); - *flags = ComputeMultiLineFlags(); - } else { - *text_bounds = GetTextBounds(); - *flags = 0; - } +void Label::DidChangeBounds(const gfx::Rect& previous, + const gfx::Rect& current) { + text_size_valid_ &= !is_multi_line_; } void Label::Paint(gfx::Canvas* canvas) { @@ -174,46 +78,22 @@ void Label::Paint(gfx::Canvas* canvas) { gfx::Rect text_bounds; int flags = 0; CalculateDrawStringParams(&paint_text, &text_bounds, &flags); - if (highlighted_) { - // Draw a second version of the string underneath the main one, but down - // and to the right by a pixel to create a highlight. - canvas->DrawStringInt(paint_text, - font_, - highlight_color_, - text_bounds.x() + 1, - text_bounds.y() + 1, - text_bounds.width(), - text_bounds.height()); - } - canvas->DrawStringInt(paint_text, - font_, - color_, - text_bounds.x(), - text_bounds.y(), - text_bounds.width(), - text_bounds.height(), - flags); - - // The focus border always hugs the text, regardless of the label's bounds. + canvas->DrawStringInt(paint_text, font_, color_, + text_bounds.x(), text_bounds.y(), + text_bounds.width(), text_bounds.height(), flags); + if (HasFocus() || paint_as_focused_) { - int w = text_bounds.width(); - int h = 0; - gfx::Canvas::SizeStringInt(paint_text, font_, &w, &h, flags); - gfx::Rect focus_rect = text_bounds; - focus_rect.set_width(w); - focus_rect.set_height(h); - focus_rect.Inset(-kFocusBorderPadding, -kFocusBorderPadding); + text_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); // If the label is a single line of text, then the computed text bound // corresponds directly to the text being drawn and no mirroring is needed // for the RTL case. For multiline text, the text bound is an estimation // and is recomputed in gfx::Canvas::SizeStringInt(). For multiline text // in RTL, we need to take mirroring into account when computing the focus // rectangle. - int x = focus_rect.x(); if (flags & gfx::Canvas::MULTI_LINE) - x = MirroredLeftPointForRect(focus_rect); - canvas->DrawFocusRect(x, focus_rect.y(), focus_rect.width(), - focus_rect.height()); + text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); + canvas->DrawFocusRect(text_bounds.x(), text_bounds.y(), + text_bounds.width(), text_bounds.height()); } } @@ -231,10 +111,6 @@ void Label::SetFont(const gfx::Font& font) { SchedulePaint(); } -gfx::Font Label::GetFont() const { - return font_; -} - void Label::SetText(const std::wstring& text) { text_ = text; url_set_ = false; @@ -242,6 +118,10 @@ void Label::SetText(const std::wstring& text) { SchedulePaint(); } +const std::wstring Label::GetText() const { + return url_set_ ? UTF8ToWide(url_.spec()) : text_; +} + void Label::SetURL(const GURL& url) { url_ = url; text_ = UTF8ToWide(url_.spec()); @@ -250,97 +130,27 @@ void Label::SetURL(const GURL& url) { SchedulePaint(); } -const std::wstring Label::GetText() const { - if (url_set_) - return UTF8ToWide(url_.spec()); - else - return text_; -} - const GURL Label::GetURL() const { - if (url_set_) - return url_; - else - return GURL(WideToUTF8(text_)); -} - -gfx::Size Label::GetTextSize() { - if (!text_size_valid_) { - // Multi-line labels need a boundary width (see GetHeightForWidth). - DCHECK(!is_multi_line_); - int h = 0, w = std::numeric_limits<int>::max(); - gfx::Canvas cc(0, 0, true); - cc.SizeStringInt(text_, font_, &w, &h, 0); - text_size_.SetSize(w, font_.height()); - if (highlighted_) - text_size_.Enlarge(1, 1); - text_size_valid_ = true; - } - - return text_size_; -} - -int Label::GetHeightForWidth(int w) { - if (is_multi_line_) { - gfx::Insets insets = GetInsets(); - w = std::max<int>(0, w - insets.width()); - int h = 0; - gfx::Canvas cc(0, 0, true); - cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); - return h + insets.height(); - } - - return View::GetHeightForWidth(w); -} - -std::string Label::GetClassName() const { - return kViewClassName; -} - -void Label::SetColor(const SkColor& color) { - color_ = color; -} - -SkColor Label::GetColor() const { - return color_; -} - -void Label::SetDrawHighlighted(bool h) { - highlighted_ = h; - text_size_valid_ = false; + return url_set_ ? url_ : GURL(WideToUTF8(text_)); } void Label::SetHorizontalAlignment(Alignment a) { // If the View's UI layout is right-to-left and rtl_alignment_mode_ is // USE_UI_ALIGNMENT, we need to flip the alignment so that the alignment // settings take into account the text directionality. - if (UILayoutIsRightToLeft() && rtl_alignment_mode_ == USE_UI_ALIGNMENT) { - if (a == ALIGN_LEFT) - a = ALIGN_RIGHT; - else if (a == ALIGN_RIGHT) - a = ALIGN_LEFT; - } + if (UILayoutIsRightToLeft() && (rtl_alignment_mode_ == USE_UI_ALIGNMENT) && + (a != ALIGN_CENTER)) + a = (a == ALIGN_LEFT) ? ALIGN_RIGHT : ALIGN_LEFT; if (horiz_alignment_ != a) { horiz_alignment_ = a; SchedulePaint(); } } -Label::Alignment Label::GetHorizontalAlignment() const { - return horiz_alignment_; -} - -void Label::SetRTLAlignmentMode(RTLAlignmentMode mode) { - rtl_alignment_mode_ = mode; -} - -Label::RTLAlignmentMode Label::GetRTLAlignmentMode() const { - return rtl_alignment_mode_; -} - void Label::SetMultiLine(bool f) { if (f != is_multi_line_) { is_multi_line_ = f; + text_size_valid_ = false; SchedulePaint(); } } @@ -348,14 +158,11 @@ void Label::SetMultiLine(bool f) { void Label::SetAllowCharacterBreak(bool f) { if (f != allow_character_break_) { allow_character_break_ = f; + text_size_valid_ = false; SchedulePaint(); } } -bool Label::IsMultiLine() { - return is_multi_line_; -} - void Label::SetTooltipText(const std::wstring& tooltip_text) { tooltip_text_ = tooltip_text; } @@ -413,18 +220,117 @@ gfx::Insets Label::GetInsets() const { return insets; } +void Label::SizeToFit(int max_width) { + DCHECK(is_multi_line_); + + std::vector<std::wstring> lines; + SplitString(text_, L'\n', &lines); + + int label_width = 0; + for (std::vector<std::wstring>::const_iterator iter = lines.begin(); + iter != lines.end(); ++iter) + label_width = std::max(label_width, font_.GetStringWidth(*iter)); + + label_width += GetInsets().width(); + + if (max_width > 0) + label_width = std::min(label_width, max_width); + + SetBounds(x(), y(), label_width, 0); + SizeToPreferredSize(); +} + +bool Label::GetAccessibleRole(AccessibilityTypes::Role* role) { + DCHECK(role); + + *role = AccessibilityTypes::ROLE_TEXT; + return true; +} + +bool Label::GetAccessibleName(std::wstring* name) { + DCHECK(name); + *name = GetText(); + return !name->empty(); +} + +bool Label::GetAccessibleState(AccessibilityTypes::State* state) { + DCHECK(state); + + *state = AccessibilityTypes::STATE_READONLY; + return true; +} + +void Label::SetHasFocusBorder(bool has_focus_border) { + has_focus_border_ = has_focus_border; + text_size_valid_ &= !is_multi_line_; +} + // static gfx::Font Label::GetDefaultFont() { return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); } -void Label::UpdateContainsMouse(const MouseEvent& event) { - if (is_multi_line_) { - gfx::Rect rect(width(), GetHeightForWidth(width())); - SetContainsMouse(rect.Contains(event.x(), event.y())); +void Label::Init(const std::wstring& text, const gfx::Font& font) { + static bool initialized = false; + if (!initialized) { +#if defined(OS_WIN) + kEnabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); + kDisabledColor = color_utils::GetSysSkColor(COLOR_GRAYTEXT); +#else + // TODO(beng): source from theme provider. + kEnabledColor = SK_ColorBLACK; + kDisabledColor = SK_ColorGRAY; +#endif + + initialized = true; + } + + contains_mouse_ = false; + font_ = font; + text_size_valid_ = false; + SetText(text); + url_set_ = false; + color_ = kEnabledColor; + horiz_alignment_ = ALIGN_CENTER; + is_multi_line_ = false; + allow_character_break_ = false; + collapse_when_hidden_ = false; + rtl_alignment_mode_ = USE_UI_ALIGNMENT; + paint_as_focused_ = false; + has_focus_border_ = false; +} + +void Label::CalculateDrawStringParams(std::wstring* paint_text, + gfx::Rect* text_bounds, + int* flags) const { + DCHECK(paint_text && text_bounds && flags); + + if (url_set_) { + // TODO(jungshik) : Figure out how to get 'intl.accept_languages' + // preference and use it when calling ElideUrl. + *paint_text = gfx::ElideUrl(url_, font_, width(), std::wstring()); + + // An URLs is always treated as an LTR text and therefore we should + // explicitly mark it as such if the locale is RTL so that URLs containing + // Hebrew or Arabic characters are displayed correctly. + // + // Note that we don't check the View's UI layout setting in order to + // determine whether or not to insert the special Unicode formatting + // characters. We use the locale settings because an URL is always treated + // as an LTR string, even if its containing view does not use an RTL UI + // layout. + if (base::i18n::IsRTL()) + base::i18n::WrapStringWithLTRFormatting(paint_text); } else { - SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); + *paint_text = text_; } + + *text_bounds = GetTextBounds(); + *flags = ComputeMultiLineFlags(); +} + +void Label::UpdateContainsMouse(const MouseEvent& event) { + SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); } void Label::SetContainsMouse(bool contains_mouse) { @@ -435,76 +341,81 @@ void Label::SetContainsMouse(bool contains_mouse) { SchedulePaint(); } -gfx::Rect Label::GetTextBounds() { - gfx::Size text_size = GetTextSize(); - gfx::Insets insets = GetInsets(); - int avail_width = width() - insets.width(); - // Respect the size set by the owner view - text_size.set_width(std::max(0, std::min(avail_width, text_size.width()))); +gfx::Rect Label::GetTextBounds() const { + gfx::Rect available_rect(GetAvailableRect()); + gfx::Size text_size(GetTextSize()); + text_size.set_width(std::min(available_rect.width(), text_size.width())); - int text_y = insets.top() + - (height() - text_size.height() - insets.height()) / 2; - int text_x; + gfx::Insets insets = GetInsets(); + gfx::Point text_origin(insets.left(), insets.top()); switch (horiz_alignment_) { case ALIGN_LEFT: - text_x = insets.left(); break; case ALIGN_CENTER: // We put any extra margin pixel on the left rather than the right, since // GetTextExtentPoint32() can report a value one too large on the right. - text_x = insets.left() + (avail_width + 1 - text_size.width()) / 2; + text_origin.Offset((available_rect.width() + 1 - text_size.width()) / 2, + 0); break; case ALIGN_RIGHT: - text_x = width() - insets.right() - text_size.width(); + text_origin.set_x(available_rect.right() - text_size.width()); break; default: NOTREACHED(); - text_x = 0; break; } - return gfx::Rect(text_x, text_y, text_size.width(), text_size.height()); + text_origin.Offset(0, + std::max(0, (available_rect.height() - text_size.height())) / 2); + return gfx::Rect(text_origin, text_size); } -void Label::SizeToFit(int max_width) { - DCHECK(is_multi_line_); - - std::vector<std::wstring> lines; - SplitString(text_, L'\n', &lines); - - int label_width = 0; - for (std::vector<std::wstring>::const_iterator iter = lines.begin(); - iter != lines.end(); ++iter) { - label_width = std::max(label_width, font_.GetStringWidth(*iter)); +gfx::Size Label::GetTextSize() const { + if (!text_size_valid_) { + int w = is_multi_line_ ? + GetAvailableRect().width() : std::numeric_limits<int>::max(); + int h = font_.height(); + gfx::Canvas::SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); + text_size_.SetSize(w, h); + text_size_valid_ = true; } - gfx::Insets insets = GetInsets(); - label_width += insets.width(); - - if (max_width > 0) - label_width = std::min(label_width, max_width); - - SetBounds(x(), y(), label_width, 0); - SizeToPreferredSize(); + return text_size_; } -bool Label::GetAccessibleRole(AccessibilityTypes::Role* role) { - DCHECK(role); - - *role = AccessibilityTypes::ROLE_TEXT; - return true; -} +int Label::ComputeMultiLineFlags() const { + if (!is_multi_line_) + return 0; -bool Label::GetAccessibleName(std::wstring* name) { - DCHECK(name); - *name = GetText(); - return !name->empty(); + int flags = gfx::Canvas::MULTI_LINE; +#if !defined(OS_WIN) + // Don't ellide multiline labels on Linux. + // Todo(davemoore): Do we depend on elliding multiline text? + // Pango insists on limiting the number of lines to one if text is + // ellided. You can get around this if you can pass a maximum height + // but we don't currently have that data when we call the pango code. + flags |= gfx::Canvas::NO_ELLIPSIS; +#endif + if (allow_character_break_) + flags |= gfx::Canvas::CHARACTER_BREAK; + switch (horiz_alignment_) { + case ALIGN_LEFT: + flags |= gfx::Canvas::TEXT_ALIGN_LEFT; + break; + case ALIGN_CENTER: + flags |= gfx::Canvas::TEXT_ALIGN_CENTER; + break; + case ALIGN_RIGHT: + flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; + break; + } + return flags; } -bool Label::GetAccessibleState(AccessibilityTypes::State* state) { - DCHECK(state); - - *state = AccessibilityTypes::STATE_READONLY; - return true; +gfx::Rect Label::GetAvailableRect() const { + gfx::Rect bounds(gfx::Point(), size()); + gfx::Insets insets(GetInsets()); + bounds.Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); + return bounds; } } // namespace views diff --git a/views/controls/label.h b/views/controls/label.h index 9aeb951..2249a82 100644 --- a/views/controls/label.h +++ b/views/controls/label.h @@ -45,14 +45,9 @@ class Label : public View { // The view class name. static const char kViewClassName[]; - // Create a new label with a default font and empty value Label(); - - // Create a new label with a default font explicit Label(const std::wstring& text); - Label(const std::wstring& text, const gfx::Font& font); - virtual ~Label(); // Overridden to compute the size required to display this label @@ -66,8 +61,12 @@ class Label : public View { // GetPreferredSize().height() if the receiver is not multi-line virtual int GetHeightForWidth(int w); + // Overriden to dirty our text bounds if we're multi-line. + virtual void DidChangeBounds(const gfx::Rect& previous, + const gfx::Rect& current); + // Returns views/Label. - virtual std::string GetClassName() const; + virtual std::string GetClassName() const { return kViewClassName; } // Overridden to paint virtual void Paint(gfx::Canvas* canvas); @@ -79,12 +78,13 @@ class Label : public View { // Set the font. void SetFont(const gfx::Font& font); - // Return the font used by this label - gfx::Font GetFont() const; - // Set the label text. void SetText(const std::wstring& text); + // Return the font used by this label. + // TODO(pkasting): Convert to unix_hacker() style. + gfx::Font GetFont() const { return font_; } + // Return the label text. const std::wstring GetText() const; @@ -95,20 +95,10 @@ class Label : public View { const GURL GetURL() const; // Set the color - virtual void SetColor(const SkColor& color); + virtual void SetColor(const SkColor& color) { color_ = color; } // Return a reference to the currently used color - virtual SkColor GetColor() const; - - // Set and Get the highlight color - virtual void SetHighlightColor(const SkColor& color) { - highlight_color_ = color; - } - virtual SkColor GetHighlightColor() const { return highlight_color_; } - - // Whether to draw highlighted text. - virtual bool DrawHighlighted() const { return highlighted_; } - virtual void SetDrawHighlighted(bool h); + virtual SkColor GetColor() const { return color_; } // Set horizontal alignment. If the locale is RTL, and the RTL alignment // setting is set as USE_UI_ALIGNMENT, the alignment is flipped around. @@ -120,7 +110,8 @@ class Label : public View { // more information. void SetHorizontalAlignment(Alignment a); - Alignment GetHorizontalAlignment() const; + // TODO(pkasting): Convert to unix_hacker() style. + Alignment GetHorizontalAlignment() const { return horiz_alignment_; } // Set the RTL alignment mode. The RTL alignment mode is initialized to // USE_UI_ALIGNMENT when the label is constructed. USE_UI_ALIGNMENT applies @@ -129,21 +120,24 @@ class Label : public View { // RTL locales. For such labels, we need to set the RTL alignment mode to // AUTO_DETECT_ALIGNMENT so that subsequent SetHorizontalAlignment() calls // will not flip the label's alignment around. - void SetRTLAlignmentMode(RTLAlignmentMode mode); - - RTLAlignmentMode GetRTLAlignmentMode() const; + // TODO(pkasting): Convert both of these to unix_hacker() style. + void SetRTLAlignmentMode(RTLAlignmentMode mode) { + rtl_alignment_mode_ = mode; + } + RTLAlignmentMode GetRTLAlignmentMode() const { return rtl_alignment_mode_; } // Set whether the label text can wrap on multiple lines. // Default is false. void SetMultiLine(bool f); + // Return whether the label text can wrap on multiple lines. + // TODO(pkasting): Convert to unix_hacker() style. + bool IsMultiLine() const { return is_multi_line_; } + // Set whether the label text can be split on words. // Default is false. This only works when is_multi_line is true. void SetAllowCharacterBreak(bool f); - // Return whether the label text can wrap on multiple lines - bool IsMultiLine(); - // Sets the tooltip text. Default behavior for a label (single-line) is to // show the full text if it is wider than its bounds. Calling this overrides // the default behavior and lets you set a custom tooltip. To revert to @@ -195,9 +189,8 @@ class Label : public View { void set_paint_as_focused(bool paint_as_focused) { paint_as_focused_ = paint_as_focused; } - void set_has_focus_border(bool has_focus_border) { - has_focus_border_ = has_focus_border; - } + + void SetHasFocusBorder(bool has_focus_border); private: // These tests call CalculateDrawStringParams in order to verify the @@ -207,10 +200,12 @@ class Label : public View { static gfx::Font GetDefaultFont(); + void Init(const std::wstring& text, const gfx::Font& font); + // Returns parameters to be used for the DrawString call. void CalculateDrawStringParams(std::wstring* paint_text, gfx::Rect* text_bounds, - int* flags); + int* flags) const; // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise // SetContainsMouse(false) is invoked. @@ -222,11 +217,13 @@ class Label : public View { void SetContainsMouse(bool contains_mouse); // Returns where the text is drawn, in the receivers coordinate system. - gfx::Rect GetTextBounds(); + gfx::Rect GetTextBounds() const; - int ComputeMultiLineFlags(); - gfx::Size GetTextSize(); - void Init(const std::wstring& text, const gfx::Font& font); + gfx::Size GetTextSize() const; + + int ComputeMultiLineFlags() const; + + gfx::Rect GetAvailableRect() const; // The colors to use for enabled and disabled labels. static SkColor kEnabledColor, kDisabledColor; @@ -235,9 +232,8 @@ class Label : public View { GURL url_; gfx::Font font_; SkColor color_; - SkColor highlight_color_; - gfx::Size text_size_; - bool text_size_valid_; + mutable gfx::Size text_size_; + mutable bool text_size_valid_; bool is_multi_line_; bool allow_character_break_; bool url_set_; @@ -259,8 +255,6 @@ class Label : public View { // allows this view to reserve space for a focus border that it otherwise // might not have because it is not itself focusable. bool has_focus_border_; - // Whether the text is drawn with an inset highlight. - bool highlighted_; DISALLOW_COPY_AND_ASSIGN(Label); }; diff --git a/views/controls/label_unittest.cc b/views/controls/label_unittest.cc index 8caabb0..05c204c 100644 --- a/views/controls/label_unittest.cc +++ b/views/controls/label_unittest.cc @@ -59,9 +59,6 @@ TEST(LabelTest, ColorProperty) { SkColor color = SkColorSetARGB(20, 40, 10, 5); label.SetColor(color); EXPECT_EQ(color, label.GetColor()); - SkColor h_color = SkColorSetARGB(40, 80, 20, 10); - label.SetHighlightColor(h_color); - EXPECT_EQ(h_color, label.GetHighlightColor()); } TEST(LabelTest, AlignmentProperty) { @@ -185,13 +182,6 @@ TEST(LabelTest, SingleLineSizing) { EXPECT_GT(required_size.height(), kMinTextDimension); EXPECT_GT(required_size.width(), kMinTextDimension); - // Test with highlights. - label.SetDrawHighlighted(true); - gfx::Size highlighted_size = label.GetPreferredSize(); - EXPECT_GT(highlighted_size.height(), required_size.height()); - EXPECT_GT(highlighted_size.width(), required_size.width()); - label.SetDrawHighlighted(false); - // Test everything with borders. gfx::Insets border(10, 20, 30, 40); label.set_border(Border::CreateEmptyBorder(border.top(), |