diff options
author | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-06 18:30:53 +0000 |
---|---|---|
committer | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-06 18:30:53 +0000 |
commit | a3aa55c8af222b7108f7ebd50f6f093fa7fc0996 (patch) | |
tree | bd355ecf6cae8bf19ecec20524d7ed2b286e08ff /views/controls | |
parent | 89df6eb95d00bd0b09e05a8914af930c0fe87f62 (diff) | |
download | chromium_src-a3aa55c8af222b7108f7ebd50f6f093fa7fc0996.zip chromium_src-a3aa55c8af222b7108f7ebd50f6f093fa7fc0996.tar.gz chromium_src-a3aa55c8af222b7108f7ebd50f6f093fa7fc0996.tar.bz2 |
Add multi-line support to TextButtonBase since this is needed by the derived
checkbox and radio button classes.
BUG=None
TEST=There should be no visible changes to existing text buttons, since this
option is off by default. It will be turned on for some radio buttons and
checkboxes in a future CL.
Review URL: http://codereview.chromium.org/6927023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84471 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/controls')
-rw-r--r-- | views/controls/button/text_button.cc | 51 | ||||
-rw-r--r-- | views/controls/button/text_button.h | 12 |
2 files changed, 55 insertions, 8 deletions
diff --git a/views/controls/button/text_button.cc b/views/controls/button/text_button.cc index 42059d3..e72deb3 100644 --- a/views/controls/button/text_button.cc +++ b/views/controls/button/text_button.cc @@ -270,6 +270,7 @@ TextButtonBase::TextButtonBase(ButtonListener* listener, normal_has_border_(false), show_multiple_icon_states_(true), is_default_(false), + multi_line_(false), prefix_type_(PREFIX_NONE) { SetText(text); SetAnimationDuration(kHoverAnimationDurationMs); @@ -351,6 +352,15 @@ void TextButtonBase::ClearEmbellishing() { has_text_halo_ = false; } +void TextButtonBase::SetMultiLine(bool multi_line) { + if (multi_line != multi_line_) { + multi_line_ = multi_line; + UpdateTextSize(); + PreferredSizeChanged(); + SchedulePaint(); + } +} + gfx::Size TextButtonBase::GetPreferredSize() { gfx::Insets insets = GetInsets(); @@ -377,24 +387,49 @@ void TextButtonBase::UpdateColor() { } void TextButtonBase::UpdateTextSize() { - int width = 0, height = 0; - gfx::CanvasSkia::SizeStringInt( - text_, font_, &width, &height, - gfx::Canvas::NO_ELLIPSIS | PrefixTypeToCanvasType(prefix_type_)); + int h = font_.GetHeight(); + int w = multi_line_ ? width() : 0; + int flags = ComputeCanvasStringFlags(); + if (!multi_line_) + flags |= gfx::Canvas::NO_ELLIPSIS; + + gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, flags); // Add 2 extra pixels to width and height when text halo is used. if (has_text_halo_) { - width += 2; - height += 2; + w += 2; + h += 2; } - text_size_.SetSize(width, font_.GetHeight()); + text_size_.SetSize(w, h); max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()), std::max(max_text_size_.height(), text_size_.height())); PreferredSizeChanged(); } +int TextButtonBase::ComputeCanvasStringFlags() const { + int flags = PrefixTypeToCanvasType(prefix_type_); + + if (multi_line_) { + flags |= gfx::Canvas::MULTI_LINE; + + switch (alignment_) { + case ALIGN_LEFT: + flags |= gfx::Canvas::TEXT_ALIGN_LEFT; + break; + case ALIGN_RIGHT: + flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; + break; + case ALIGN_CENTER: + flags |= gfx::Canvas::TEXT_ALIGN_CENTER; + break; + } + } + + return flags; +} + void TextButtonBase::GetExtraParams( gfx::NativeTheme::ExtraParams* params) const { params->button.checked = false; @@ -460,7 +495,7 @@ void TextButtonBase::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { (state() == BS_HOT || state() == BS_PUSHED)) ? color_hover_ : color_; int draw_string_flags = gfx::CanvasSkia::DefaultCanvasTextAlignment() | - PrefixTypeToCanvasType(prefix_type_); + ComputeCanvasStringFlags(); if (mode == PB_FOR_DRAG) { #if defined(OS_WIN) diff --git a/views/controls/button/text_button.h b/views/controls/button/text_button.h index d9a1fed..c878eb1 100644 --- a/views/controls/button/text_button.h +++ b/views/controls/button/text_button.h @@ -141,6 +141,13 @@ class TextButtonBase : public CustomButton, public NativeThemeDelegate { void SetIsDefault(bool is_default); bool is_default() const { return is_default_; } + // Set whether the button text can wrap on multiple lines. + // Default is false. + void SetMultiLine(bool multi_line); + + // Return whether the button text can wrap on multiple lines. + bool multi_line() const { return multi_line_; } + // TextButton remembers the maximum display size of the text passed to // SetText. This method resets the cached maximum display size to the // current size. @@ -218,6 +225,8 @@ class TextButtonBase : public CustomButton, public NativeThemeDelegate { virtual gfx::Rect GetTextBounds() const; + int ComputeCanvasStringFlags() const; + // Calculate the bounds of the content of this button, including any extra // width needed on top of the text width. gfx::Rect GetContentBounds(int extra_width) const; @@ -272,6 +281,9 @@ class TextButtonBase : public CustomButton, public NativeThemeDelegate { // current context. bool is_default_; + // Whether the text button should handle its text string as multi-line. + bool multi_line_; + PrefixType prefix_type_; DISALLOW_COPY_AND_ASSIGN(TextButtonBase); |