| OLD | NEW |
| 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/button/text_button.h" | 5 #include "ui/views/controls/button/text_button.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "grit/ui_resources.h" | 10 #include "grit/ui_resources.h" |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 int h = font_list_.GetHeight(); | 351 int h = font_list_.GetHeight(); |
| 352 int w = multi_line_ ? max_width : 0; | 352 int w = multi_line_ ? max_width : 0; |
| 353 int flags = ComputeCanvasStringFlags(); | 353 int flags = ComputeCanvasStringFlags(); |
| 354 if (!multi_line_) | 354 if (!multi_line_) |
| 355 flags |= gfx::Canvas::NO_ELLIPSIS; | 355 flags |= gfx::Canvas::NO_ELLIPSIS; |
| 356 | 356 |
| 357 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, 0, flags); | 357 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, 0, flags); |
| 358 text_size->SetSize(w, h); | 358 text_size->SetSize(w, h); |
| 359 } | 359 } |
| 360 | 360 |
| 361 void TextButtonBase::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 362 gfx::Rect text_bounds(GetTextBounds()); |
| 363 if (text_bounds.width() > 0) { |
| 364 // Because the text button can (at times) draw multiple elements on the |
| 365 // canvas, we can not mirror the button by simply flipping the canvas as |
| 366 // doing this will mirror the text itself. Flipping the canvas will also |
| 367 // make the icons look wrong because icons are almost always represented as |
| 368 // direction-insensitive images and such images should never be flipped |
| 369 // horizontally. |
| 370 // |
| 371 // Due to the above, we must perform the flipping manually for RTL UIs. |
| 372 text_bounds.set_x(GetMirroredXForRect(text_bounds)); |
| 373 |
| 374 SkColor text_color = (show_multiple_icon_states_ && |
| 375 (state() == STATE_HOVERED || state() == STATE_PRESSED)) ? |
| 376 color_hover_ : color_; |
| 377 |
| 378 int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() | |
| 379 ComputeCanvasStringFlags(); |
| 380 |
| 381 if (mode == PB_FOR_DRAG) { |
| 382 // Disable sub-pixel rendering as background is transparent. |
| 383 draw_string_flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING; |
| 384 canvas->DrawStringRectWithHalo(text_, font_list_, |
| 385 SK_ColorBLACK, SK_ColorWHITE, |
| 386 text_bounds, draw_string_flags); |
| 387 } else { |
| 388 canvas->DrawStringRectWithFlags(text_, font_list_, text_color, |
| 389 text_bounds, draw_string_flags); |
| 390 } |
| 391 } |
| 392 } |
| 393 |
| 361 int TextButtonBase::ComputeCanvasStringFlags() const { | 394 int TextButtonBase::ComputeCanvasStringFlags() const { |
| 362 if (!multi_line_) | 395 if (!multi_line_) |
| 363 return 0; | 396 return 0; |
| 364 | 397 |
| 365 int flags = gfx::Canvas::MULTI_LINE; | 398 int flags = gfx::Canvas::MULTI_LINE; |
| 366 switch (alignment_) { | 399 switch (alignment_) { |
| 367 case ALIGN_LEFT: | 400 case ALIGN_LEFT: |
| 368 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; | 401 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| 369 break; | 402 break; |
| 370 case ALIGN_RIGHT: | 403 case ALIGN_RIGHT: |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 focus_painter_ = focus_painter.Pass(); | 471 focus_painter_ = focus_painter.Pass(); |
| 439 } | 472 } |
| 440 | 473 |
| 441 void TextButtonBase::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { | 474 void TextButtonBase::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 442 if (mode == PB_NORMAL) { | 475 if (mode == PB_NORMAL) { |
| 443 OnPaintBackground(canvas); | 476 OnPaintBackground(canvas); |
| 444 OnPaintBorder(canvas); | 477 OnPaintBorder(canvas); |
| 445 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 478 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
| 446 } | 479 } |
| 447 | 480 |
| 448 gfx::Rect text_bounds(GetTextBounds()); | 481 OnPaintText(canvas, mode); |
| 449 if (text_bounds.width() > 0) { | |
| 450 // Because the text button can (at times) draw multiple elements on the | |
| 451 // canvas, we can not mirror the button by simply flipping the canvas as | |
| 452 // doing this will mirror the text itself. Flipping the canvas will also | |
| 453 // make the icons look wrong because icons are almost always represented as | |
| 454 // direction-insensitive images and such images should never be flipped | |
| 455 // horizontally. | |
| 456 // | |
| 457 // Due to the above, we must perform the flipping manually for RTL UIs. | |
| 458 text_bounds.set_x(GetMirroredXForRect(text_bounds)); | |
| 459 | |
| 460 SkColor text_color = (show_multiple_icon_states_ && | |
| 461 (state() == STATE_HOVERED || state() == STATE_PRESSED)) ? | |
| 462 color_hover_ : color_; | |
| 463 | |
| 464 int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() | | |
| 465 ComputeCanvasStringFlags(); | |
| 466 | |
| 467 if (mode == PB_FOR_DRAG) { | |
| 468 // Disable sub-pixel rendering as background is transparent. | |
| 469 draw_string_flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING; | |
| 470 canvas->DrawStringRectWithHalo(text_, font_list_, | |
| 471 SK_ColorBLACK, SK_ColorWHITE, | |
| 472 text_bounds, draw_string_flags); | |
| 473 } else { | |
| 474 canvas->DrawStringRectWithFlags(text_, font_list_, text_color, | |
| 475 text_bounds, draw_string_flags); | |
| 476 } | |
| 477 } | |
| 478 } | 482 } |
| 479 | 483 |
| 480 gfx::Size TextButtonBase::GetMinimumSize() { | 484 gfx::Size TextButtonBase::GetMinimumSize() { |
| 481 return max_text_size_; | 485 return max_text_size_; |
| 482 } | 486 } |
| 483 | 487 |
| 484 void TextButtonBase::OnEnabledChanged() { | 488 void TextButtonBase::OnEnabledChanged() { |
| 485 // We should always call UpdateColor() since the state of the button might be | 489 // We should always call UpdateColor() since the state of the button might be |
| 486 // changed by other functions like CustomButton::SetState(). | 490 // changed by other functions like CustomButton::SetState(). |
| 487 UpdateColor(); | 491 UpdateColor(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 prefsize.set_height(std::max(prefsize.height(), min_height_)); | 627 prefsize.set_height(std::max(prefsize.height(), min_height_)); |
| 624 | 628 |
| 625 return prefsize; | 629 return prefsize; |
| 626 } | 630 } |
| 627 | 631 |
| 628 void TextButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { | 632 void TextButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 629 if (full_justification_ && icon_placement_ == ICON_ON_LEFT) | 633 if (full_justification_ && icon_placement_ == ICON_ON_LEFT) |
| 630 set_alignment(ALIGN_RIGHT); | 634 set_alignment(ALIGN_RIGHT); |
| 631 | 635 |
| 632 TextButtonBase::PaintButton(canvas, mode); | 636 TextButtonBase::PaintButton(canvas, mode); |
| 637 OnPaintIcon(canvas, mode); |
| 638 } |
| 633 | 639 |
| 640 void TextButton::OnPaintIcon(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 634 const gfx::ImageSkia& icon = GetImageToPaint(); | 641 const gfx::ImageSkia& icon = GetImageToPaint(); |
| 635 | 642 |
| 636 if (icon.width() > 0) { | 643 if (icon.width() > 0) { |
| 637 gfx::Rect text_bounds = GetTextBounds(); | 644 gfx::Rect text_bounds = GetTextBounds(); |
| 638 int icon_x = 0; | 645 int icon_x = 0; |
| 639 int spacing = text_.empty() ? 0 : icon_text_spacing_; | 646 int spacing = text_.empty() ? 0 : icon_text_spacing_; |
| 640 gfx::Insets insets = GetInsets(); | 647 gfx::Insets insets = GetInsets(); |
| 641 switch (icon_placement_) { | 648 switch (icon_placement_) { |
| 642 case ICON_ON_LEFT: | 649 case ICON_ON_LEFT: |
| 643 icon_x = full_justification_ ? insets.left() | 650 icon_x = full_justification_ ? insets.left() |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 if (show_multiple_icon_states_) { | 719 if (show_multiple_icon_states_) { |
| 713 if (has_hover_icon_ && (state() == STATE_HOVERED)) | 720 if (has_hover_icon_ && (state() == STATE_HOVERED)) |
| 714 return icon_hover_; | 721 return icon_hover_; |
| 715 if (has_pushed_icon_ && (state() == STATE_PRESSED)) | 722 if (has_pushed_icon_ && (state() == STATE_PRESSED)) |
| 716 return icon_pushed_; | 723 return icon_pushed_; |
| 717 } | 724 } |
| 718 return icon_; | 725 return icon_; |
| 719 } | 726 } |
| 720 | 727 |
| 721 } // namespace views | 728 } // namespace views |
| OLD | NEW |