summaryrefslogtreecommitdiffstats
path: root/chromium/ui/views/controls/button/text_button.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/ui/views/controls/button/text_button.cc')
-rw-r--r--chromium/ui/views/controls/button/text_button.cc185
1 files changed, 80 insertions, 105 deletions
diff --git a/chromium/ui/views/controls/button/text_button.cc b/chromium/ui/views/controls/button/text_button.cc
index e829405b9f4..bf12f8e086d 100644
--- a/chromium/ui/views/controls/button/text_button.cc
+++ b/chromium/ui/views/controls/button/text_button.cc
@@ -88,14 +88,6 @@ void TextButtonBorder::SetInsets(const gfx::Insets& insets) {
insets_ = insets;
}
-TextButtonBorder* TextButtonBorder::AsTextButtonBorder() {
- return this;
-}
-
-const TextButtonBorder* TextButtonBorder::AsTextButtonBorder() const {
- return this;
-}
-
// TextButtonDefaultBorder ----------------------------------------------------
@@ -199,15 +191,10 @@ void TextButtonNativeThemeBorder::Paint(const View& view, gfx::Canvas* canvas) {
// TextButtonBase -------------------------------------------------------------
-TextButtonBase::TextButtonBase(ButtonListener* listener, const string16& text)
+TextButtonBase::TextButtonBase(ButtonListener* listener,
+ const base::string16& text)
: CustomButton(listener),
alignment_(ALIGN_LEFT),
- font_(ResourceBundle::GetSharedInstance().GetFont(
- ResourceBundle::BaseFont)),
- has_text_shadow_(false),
- active_text_shadow_color_(0),
- inactive_text_shadow_color_(0),
- text_shadow_offset_(gfx::Point(1, 1)),
min_width_(0),
min_height_(0),
max_width_(0),
@@ -220,8 +207,6 @@ TextButtonBase::TextButtonBase(ButtonListener* listener, const string16& text)
use_hover_color_from_theme_(true),
focus_painter_(Painter::CreateDashedFocusPainter()) {
SetText(text);
- // OnNativeThemeChanged sets the color member variables.
- TextButtonBase::OnNativeThemeChanged(GetNativeTheme());
SetAnimationDuration(kHoverAnimationDurationMs);
}
@@ -239,7 +224,7 @@ void TextButtonBase::SetIsDefault(bool is_default) {
SchedulePaint();
}
-void TextButtonBase::SetText(const string16& text) {
+void TextButtonBase::SetText(const base::string16& text) {
if (text == text_)
return;
text_ = text;
@@ -247,8 +232,8 @@ void TextButtonBase::SetText(const string16& text) {
UpdateTextSize();
}
-void TextButtonBase::SetFont(const gfx::Font& font) {
- font_ = font;
+void TextButtonBase::SetFontList(const gfx::FontList& font_list) {
+ font_list_ = font_list;
UpdateTextSize();
}
@@ -274,21 +259,6 @@ void TextButtonBase::SetHoverColor(SkColor color) {
use_hover_color_from_theme_ = false;
}
-void TextButtonBase::SetTextShadowColors(SkColor active_color,
- SkColor inactive_color) {
- active_text_shadow_color_ = active_color;
- inactive_text_shadow_color_ = inactive_color;
- has_text_shadow_ = true;
-}
-
-void TextButtonBase::SetTextShadowOffset(int x, int y) {
- text_shadow_offset_.SetPoint(x, y);
-}
-
-void TextButtonBase::ClearEmbellishing() {
- has_text_shadow_ = false;
-}
-
void TextButtonBase::ClearMaxTextSize() {
max_text_size_ = text_size_;
}
@@ -306,7 +276,7 @@ void TextButtonBase::SetMultiLine(bool multi_line) {
}
}
-gfx::Size TextButtonBase::GetPreferredSize() {
+gfx::Size TextButtonBase::GetPreferredSize() const {
gfx::Insets insets = GetInsets();
// Use the max size to set the button boundaries.
@@ -326,7 +296,7 @@ gfx::Size TextButtonBase::GetPreferredSize() {
return prefsize;
}
-int TextButtonBase::GetHeightForWidth(int w) {
+int TextButtonBase::GetHeightForWidth(int w) const {
if (!multi_line_)
return View::GetHeightForWidth(w);
@@ -377,17 +347,51 @@ void TextButtonBase::UpdateTextSize() {
}
}
-void TextButtonBase::CalculateTextSize(gfx::Size* text_size, int max_width) {
- int h = font_.GetHeight();
+void TextButtonBase::CalculateTextSize(gfx::Size* text_size,
+ int max_width) const {
+ int h = font_list_.GetHeight();
int w = multi_line_ ? max_width : 0;
int flags = ComputeCanvasStringFlags();
if (!multi_line_)
flags |= gfx::Canvas::NO_ELLIPSIS;
- gfx::Canvas::SizeStringInt(text_, font_, &w, &h, 0, flags);
+ gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, 0, flags);
text_size->SetSize(w, h);
}
+void TextButtonBase::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) {
+ gfx::Rect text_bounds(GetTextBounds());
+ if (text_bounds.width() > 0) {
+ // Because the text button can (at times) draw multiple elements on the
+ // canvas, we can not mirror the button by simply flipping the canvas as
+ // doing this will mirror the text itself. Flipping the canvas will also
+ // make the icons look wrong because icons are almost always represented as
+ // direction-insensitive images and such images should never be flipped
+ // horizontally.
+ //
+ // Due to the above, we must perform the flipping manually for RTL UIs.
+ text_bounds.set_x(GetMirroredXForRect(text_bounds));
+
+ SkColor text_color = (show_multiple_icon_states_ &&
+ (state() == STATE_HOVERED || state() == STATE_PRESSED)) ?
+ color_hover_ : color_;
+
+ int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() |
+ ComputeCanvasStringFlags();
+
+ if (mode == PB_FOR_DRAG) {
+ // Disable sub-pixel rendering as background is transparent.
+ draw_string_flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
+ canvas->DrawStringRectWithHalo(text_, font_list_,
+ SK_ColorBLACK, SK_ColorWHITE,
+ text_bounds, draw_string_flags);
+ } else {
+ canvas->DrawStringRectWithFlags(text_, font_list_, text_color,
+ text_bounds, draw_string_flags);
+ }
+ }
+}
+
int TextButtonBase::ComputeCanvasStringFlags() const {
if (!multi_line_)
return 0;
@@ -475,59 +479,10 @@ void TextButtonBase::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
}
- gfx::Rect text_bounds(GetTextBounds());
- if (text_bounds.width() > 0) {
- // Because the text button can (at times) draw multiple elements on the
- // canvas, we can not mirror the button by simply flipping the canvas as
- // doing this will mirror the text itself. Flipping the canvas will also
- // make the icons look wrong because icons are almost always represented as
- // direction-insensitive images and such images should never be flipped
- // horizontally.
- //
- // Due to the above, we must perform the flipping manually for RTL UIs.
- text_bounds.set_x(GetMirroredXForRect(text_bounds));
-
- SkColor text_color = (show_multiple_icon_states_ &&
- (state() == STATE_HOVERED || state() == STATE_PRESSED)) ?
- color_hover_ : color_;
-
- int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() |
- ComputeCanvasStringFlags();
-
- if (mode == PB_FOR_DRAG) {
- // Disable sub-pixel rendering as background is transparent.
- draw_string_flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
-
-#if defined(OS_WIN)
- // TODO(erg): Either port DrawStringWithHalo to linux or find an
- // alternative here.
- canvas->DrawStringWithHalo(text_, font_, SK_ColorBLACK, SK_ColorWHITE,
- text_bounds.x(), text_bounds.y(), text_bounds.width(),
- text_bounds.height(), draw_string_flags);
-#else
- canvas->DrawStringInt(text_,
- font_,
- text_color,
- text_bounds.x(),
- text_bounds.y(),
- text_bounds.width(),
- text_bounds.height(),
- draw_string_flags);
-#endif
- } else {
- gfx::ShadowValues shadows;
- if (has_text_shadow_) {
- SkColor color = GetWidget()->IsActive() ? active_text_shadow_color_ :
- inactive_text_shadow_color_;
- shadows.push_back(gfx::ShadowValue(text_shadow_offset_, 0, color));
- }
- canvas->DrawStringWithShadows(text_, font_, text_color, text_bounds,
- 0, draw_string_flags, shadows);
- }
- }
+ OnPaintText(canvas, mode);
}
-gfx::Size TextButtonBase::GetMinimumSize() {
+gfx::Size TextButtonBase::GetMinimumSize() const {
return max_text_size_;
}
@@ -609,14 +564,15 @@ ui::NativeTheme::State TextButtonBase::GetForegroundThemeState(
// TextButton -----------------------------------------------------------------
-TextButton::TextButton(ButtonListener* listener, const string16& text)
+TextButton::TextButton(ButtonListener* listener, const base::string16& text)
: TextButtonBase(listener, text),
icon_placement_(ICON_ON_LEFT),
has_hover_icon_(false),
has_pushed_icon_(false),
icon_text_spacing_(kDefaultIconTextSpacing),
- ignore_minimum_size_(true) {
- set_border(new TextButtonDefaultBorder);
+ ignore_minimum_size_(true),
+ full_justification_(false) {
+ SetBorder(scoped_ptr<Border>(new TextButtonDefaultBorder));
SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(
gfx::Insets(kFocusRectInset, kFocusRectInset,
kFocusRectInset, kFocusRectInset)));
@@ -642,7 +598,7 @@ void TextButton::SetPushedIcon(const gfx::ImageSkia& icon) {
SchedulePaint();
}
-gfx::Size TextButton::GetPreferredSize() {
+gfx::Size TextButton::GetPreferredSize() const {
gfx::Size prefsize(TextButtonBase::GetPreferredSize());
prefsize.Enlarge(icon_.width(), 0);
prefsize.set_height(std::max(prefsize.height(), icon_.height()));
@@ -657,8 +613,8 @@ gfx::Size TextButton::GetPreferredSize() {
#if defined(OS_WIN)
// Clamp the size returned to at least the minimum size.
if (!ignore_minimum_size_) {
- gfx::PlatformFontWin* platform_font =
- static_cast<gfx::PlatformFontWin*>(font_.platform_font());
+ gfx::PlatformFontWin* platform_font = static_cast<gfx::PlatformFontWin*>(
+ font_list_.GetPrimaryFont().platform_font());
prefsize.set_width(std::max(
prefsize.width(),
platform_font->horizontal_dlus_to_pixels(kMinWidthDLUs)));
@@ -675,22 +631,37 @@ gfx::Size TextButton::GetPreferredSize() {
}
void TextButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
+ if (full_justification_ && icon_placement_ == ICON_ON_LEFT)
+ set_alignment(ALIGN_RIGHT);
+
TextButtonBase::PaintButton(canvas, mode);
+ OnPaintIcon(canvas, mode);
+}
+void TextButton::OnPaintIcon(gfx::Canvas* canvas, PaintButtonMode mode) {
const gfx::ImageSkia& icon = GetImageToPaint();
if (icon.width() > 0) {
gfx::Rect text_bounds = GetTextBounds();
- int icon_x;
+ int icon_x = 0;
int spacing = text_.empty() ? 0 : icon_text_spacing_;
gfx::Insets insets = GetInsets();
- if (icon_placement_ == ICON_ON_LEFT) {
- icon_x = text_bounds.x() - icon.width() - spacing;
- } else if (icon_placement_ == ICON_ON_RIGHT) {
- icon_x = text_bounds.right() + spacing;
- } else { // ICON_CENTERED
- DCHECK(text_.empty());
- icon_x = (width() - insets.width() - icon.width()) / 2 + insets.left();
+ switch (icon_placement_) {
+ case ICON_ON_LEFT:
+ icon_x = full_justification_ ? insets.left()
+ : text_bounds.x() - icon.width() - spacing;
+ break;
+ case ICON_ON_RIGHT:
+ icon_x = full_justification_ ? width() - insets.right() - icon.width()
+ : text_bounds.right() + spacing;
+ break;
+ case ICON_CENTERED:
+ DCHECK(text_.empty());
+ icon_x = (width() - insets.width() - icon.width()) / 2 + insets.left();
+ break;
+ default:
+ NOTREACHED();
+ break;
}
int available_height = height() - insets.height();
@@ -707,6 +678,10 @@ void TextButton::set_ignore_minimum_size(bool ignore_minimum_size) {
ignore_minimum_size_ = ignore_minimum_size;
}
+void TextButton::set_full_justification(bool full_justification) {
+ full_justification_ = full_justification;
+}
+
const char* TextButton::GetClassName() const {
return kViewClassName;
}