Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: ui/views/controls/button/text_button.cc

Issue 273223002: views: Make view::Views::GetPreferredSize() const. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More compile fix for ToT Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/button/text_button.h ('k') | ui/views/controls/combobox/combobox.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 void TextButtonBase::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { 475 void TextButtonBase::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
475 if (mode == PB_NORMAL) { 476 if (mode == PB_NORMAL) {
476 OnPaintBackground(canvas); 477 OnPaintBackground(canvas);
477 OnPaintBorder(canvas); 478 OnPaintBorder(canvas);
478 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); 479 Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
479 } 480 }
480 481
481 OnPaintText(canvas, mode); 482 OnPaintText(canvas, mode);
482 } 483 }
483 484
484 gfx::Size TextButtonBase::GetMinimumSize() { 485 gfx::Size TextButtonBase::GetMinimumSize() const {
485 return max_text_size_; 486 return max_text_size_;
486 } 487 }
487 488
488 void TextButtonBase::OnEnabledChanged() { 489 void TextButtonBase::OnEnabledChanged() {
489 // We should always call UpdateColor() since the state of the button might be 490 // We should always call UpdateColor() since the state of the button might be
490 // changed by other functions like CustomButton::SetState(). 491 // changed by other functions like CustomButton::SetState().
491 UpdateColor(); 492 UpdateColor();
492 CustomButton::OnEnabledChanged(); 493 CustomButton::OnEnabledChanged();
493 } 494 }
494 495
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 has_hover_icon_ = true; 591 has_hover_icon_ = true;
591 SchedulePaint(); 592 SchedulePaint();
592 } 593 }
593 594
594 void TextButton::SetPushedIcon(const gfx::ImageSkia& icon) { 595 void TextButton::SetPushedIcon(const gfx::ImageSkia& icon) {
595 icon_pushed_ = icon; 596 icon_pushed_ = icon;
596 has_pushed_icon_ = true; 597 has_pushed_icon_ = true;
597 SchedulePaint(); 598 SchedulePaint();
598 } 599 }
599 600
600 gfx::Size TextButton::GetPreferredSize() { 601 gfx::Size TextButton::GetPreferredSize() const {
601 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); 602 gfx::Size prefsize(TextButtonBase::GetPreferredSize());
602 prefsize.Enlarge(icon_.width(), 0); 603 prefsize.Enlarge(icon_.width(), 0);
603 prefsize.set_height(std::max(prefsize.height(), icon_.height())); 604 prefsize.set_height(std::max(prefsize.height(), icon_.height()));
604 605
605 // Use the max size to set the button boundaries. 606 // Use the max size to set the button boundaries.
606 if (icon_.width() > 0 && !text_.empty()) 607 if (icon_.width() > 0 && !text_.empty())
607 prefsize.Enlarge(icon_text_spacing_, 0); 608 prefsize.Enlarge(icon_text_spacing_, 0);
608 609
609 if (max_width_ > 0) 610 if (max_width_ > 0)
610 prefsize.set_width(std::min(max_width_, prefsize.width())); 611 prefsize.set_width(std::min(max_width_, prefsize.width()));
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 if (show_multiple_icon_states_) { 720 if (show_multiple_icon_states_) {
720 if (has_hover_icon_ && (state() == STATE_HOVERED)) 721 if (has_hover_icon_ && (state() == STATE_HOVERED))
721 return icon_hover_; 722 return icon_hover_;
722 if (has_pushed_icon_ && (state() == STATE_PRESSED)) 723 if (has_pushed_icon_ && (state() == STATE_PRESSED))
723 return icon_pushed_; 724 return icon_pushed_;
724 } 725 }
725 return icon_; 726 return icon_;
726 } 727 }
727 728
728 } // namespace views 729 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/text_button.h ('k') | ui/views/controls/combobox/combobox.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698