| 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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 269 |
| 270 void TextButtonBase::SetMultiLine(bool multi_line) { | 270 void TextButtonBase::SetMultiLine(bool multi_line) { |
| 271 if (multi_line != multi_line_) { | 271 if (multi_line != multi_line_) { |
| 272 multi_line_ = multi_line; | 272 multi_line_ = multi_line; |
| 273 max_text_size_.SetSize(0, 0); | 273 max_text_size_.SetSize(0, 0); |
| 274 UpdateTextSize(); | 274 UpdateTextSize(); |
| 275 SchedulePaint(); | 275 SchedulePaint(); |
| 276 } | 276 } |
| 277 } | 277 } |
| 278 | 278 |
| 279 gfx::Size TextButtonBase::GetPreferredSize() { | 279 gfx::Size TextButtonBase::GetPreferredSize() const { |
| 280 gfx::Insets insets = GetInsets(); | 280 gfx::Insets insets = GetInsets(); |
| 281 | 281 |
| 282 // Use the max size to set the button boundaries. | 282 // Use the max size to set the button boundaries. |
| 283 // In multiline mode max size can be undefined while | 283 // In multiline mode max size can be undefined while |
| 284 // width() is 0, so max it out with current text size. | 284 // width() is 0, so max it out with current text size. |
| 285 gfx::Size prefsize(std::max(max_text_size_.width(), | 285 gfx::Size prefsize(std::max(max_text_size_.width(), |
| 286 text_size_.width()) + insets.width(), | 286 text_size_.width()) + insets.width(), |
| 287 std::max(max_text_size_.height(), | 287 std::max(max_text_size_.height(), |
| 288 text_size_.height()) + insets.height()); | 288 text_size_.height()) + insets.height()); |
| 289 | 289 |
| 290 if (max_width_ > 0) | 290 if (max_width_ > 0) |
| 291 prefsize.set_width(std::min(max_width_, prefsize.width())); | 291 prefsize.set_width(std::min(max_width_, prefsize.width())); |
| 292 | 292 |
| 293 prefsize.set_width(std::max(prefsize.width(), min_width_)); | 293 prefsize.set_width(std::max(prefsize.width(), min_width_)); |
| 294 prefsize.set_height(std::max(prefsize.height(), min_height_)); | 294 prefsize.set_height(std::max(prefsize.height(), min_height_)); |
| 295 | 295 |
| 296 return prefsize; | 296 return prefsize; |
| 297 } | 297 } |
| 298 | 298 |
| 299 int TextButtonBase::GetHeightForWidth(int w) { | 299 int TextButtonBase::GetHeightForWidth(int w) const { |
| 300 if (!multi_line_) | 300 if (!multi_line_) |
| 301 return View::GetHeightForWidth(w); | 301 return View::GetHeightForWidth(w); |
| 302 | 302 |
| 303 if (max_width_ > 0) | 303 if (max_width_ > 0) |
| 304 w = std::min(max_width_, w); | 304 w = std::min(max_width_, w); |
| 305 | 305 |
| 306 gfx::Size text_size; | 306 gfx::Size text_size; |
| 307 CalculateTextSize(&text_size, w); | 307 CalculateTextSize(&text_size, w); |
| 308 int height = text_size.height() + GetInsets().height(); | 308 int height = text_size.height() + GetInsets().height(); |
| 309 | 309 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 340 // Do not store max_text_size in this case. UpdateTextSize will be called | 340 // Do not store max_text_size in this case. UpdateTextSize will be called |
| 341 // again once width() changes. | 341 // again once width() changes. |
| 342 if (!multi_line_ || text_width != 0) { | 342 if (!multi_line_ || text_width != 0) { |
| 343 max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()), | 343 max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()), |
| 344 std::max(max_text_size_.height(), | 344 std::max(max_text_size_.height(), |
| 345 text_size_.height())); | 345 text_size_.height())); |
| 346 PreferredSizeChanged(); | 346 PreferredSizeChanged(); |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 | 349 |
| 350 void TextButtonBase::CalculateTextSize(gfx::Size* text_size, int max_width) { | 350 void TextButtonBase::CalculateTextSize(gfx::Size* text_size, |
| 351 int max_width) const { |
| 351 int h = font_list_.GetHeight(); | 352 int h = font_list_.GetHeight(); |
| 352 int w = multi_line_ ? max_width : 0; | 353 int w = multi_line_ ? max_width : 0; |
| 353 int flags = ComputeCanvasStringFlags(); | 354 int flags = ComputeCanvasStringFlags(); |
| 354 if (!multi_line_) | 355 if (!multi_line_) |
| 355 flags |= gfx::Canvas::NO_ELLIPSIS; | 356 flags |= gfx::Canvas::NO_ELLIPSIS; |
| 356 | 357 |
| 357 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, 0, flags); | 358 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, 0, flags); |
| 358 text_size->SetSize(w, h); | 359 text_size->SetSize(w, h); |
| 359 } | 360 } |
| 360 | 361 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 canvas->DrawStringRectWithHalo(text_, font_list_, | 471 canvas->DrawStringRectWithHalo(text_, font_list_, |
| 471 SK_ColorBLACK, SK_ColorWHITE, | 472 SK_ColorBLACK, SK_ColorWHITE, |
| 472 text_bounds, draw_string_flags); | 473 text_bounds, draw_string_flags); |
| 473 } else { | 474 } else { |
| 474 canvas->DrawStringRectWithFlags(text_, font_list_, text_color, | 475 canvas->DrawStringRectWithFlags(text_, font_list_, text_color, |
| 475 text_bounds, draw_string_flags); | 476 text_bounds, draw_string_flags); |
| 476 } | 477 } |
| 477 } | 478 } |
| 478 } | 479 } |
| 479 | 480 |
| 480 gfx::Size TextButtonBase::GetMinimumSize() { | 481 gfx::Size TextButtonBase::GetMinimumSize() const { |
| 481 return max_text_size_; | 482 return max_text_size_; |
| 482 } | 483 } |
| 483 | 484 |
| 484 void TextButtonBase::OnEnabledChanged() { | 485 void TextButtonBase::OnEnabledChanged() { |
| 485 // We should always call UpdateColor() since the state of the button might be | 486 // We should always call UpdateColor() since the state of the button might be |
| 486 // changed by other functions like CustomButton::SetState(). | 487 // changed by other functions like CustomButton::SetState(). |
| 487 UpdateColor(); | 488 UpdateColor(); |
| 488 CustomButton::OnEnabledChanged(); | 489 CustomButton::OnEnabledChanged(); |
| 489 } | 490 } |
| 490 | 491 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 has_hover_icon_ = true; | 587 has_hover_icon_ = true; |
| 587 SchedulePaint(); | 588 SchedulePaint(); |
| 588 } | 589 } |
| 589 | 590 |
| 590 void TextButton::SetPushedIcon(const gfx::ImageSkia& icon) { | 591 void TextButton::SetPushedIcon(const gfx::ImageSkia& icon) { |
| 591 icon_pushed_ = icon; | 592 icon_pushed_ = icon; |
| 592 has_pushed_icon_ = true; | 593 has_pushed_icon_ = true; |
| 593 SchedulePaint(); | 594 SchedulePaint(); |
| 594 } | 595 } |
| 595 | 596 |
| 596 gfx::Size TextButton::GetPreferredSize() { | 597 gfx::Size TextButton::GetPreferredSize() const { |
| 597 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); | 598 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); |
| 598 prefsize.Enlarge(icon_.width(), 0); | 599 prefsize.Enlarge(icon_.width(), 0); |
| 599 prefsize.set_height(std::max(prefsize.height(), icon_.height())); | 600 prefsize.set_height(std::max(prefsize.height(), icon_.height())); |
| 600 | 601 |
| 601 // Use the max size to set the button boundaries. | 602 // Use the max size to set the button boundaries. |
| 602 if (icon_.width() > 0 && !text_.empty()) | 603 if (icon_.width() > 0 && !text_.empty()) |
| 603 prefsize.Enlarge(icon_text_spacing_, 0); | 604 prefsize.Enlarge(icon_text_spacing_, 0); |
| 604 | 605 |
| 605 if (max_width_ > 0) | 606 if (max_width_ > 0) |
| 606 prefsize.set_width(std::min(max_width_, prefsize.width())); | 607 prefsize.set_width(std::min(max_width_, prefsize.width())); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 if (show_multiple_icon_states_) { | 713 if (show_multiple_icon_states_) { |
| 713 if (has_hover_icon_ && (state() == STATE_HOVERED)) | 714 if (has_hover_icon_ && (state() == STATE_HOVERED)) |
| 714 return icon_hover_; | 715 return icon_hover_; |
| 715 if (has_pushed_icon_ && (state() == STATE_PRESSED)) | 716 if (has_pushed_icon_ && (state() == STATE_PRESSED)) |
| 716 return icon_pushed_; | 717 return icon_pushed_; |
| 717 } | 718 } |
| 718 return icon_; | 719 return icon_; |
| 719 } | 720 } |
| 720 | 721 |
| 721 } // namespace views | 722 } // namespace views |
| OLD | NEW |