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

Side by Side Diff: chrome/views/label.cc

Issue 7344: Convert GetPreferredSize from:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years, 2 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
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "chrome/views/label.h" 5 #include "chrome/views/label.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 SetText(text); 43 SetText(text);
44 url_set_ = false; 44 url_set_ = false;
45 color_ = kEnabledColor; 45 color_ = kEnabledColor;
46 horiz_alignment_ = ALIGN_CENTER; 46 horiz_alignment_ = ALIGN_CENTER;
47 is_multi_line_ = false; 47 is_multi_line_ = false;
48 } 48 }
49 49
50 Label::~Label() { 50 Label::~Label() {
51 } 51 }
52 52
53 void Label::GetPreferredSize(CSize* out) { 53 gfx::Size Label::GetPreferredSize() {
54 DCHECK(out); 54 gfx::Size prefsize;
55 if (is_multi_line_) { 55 if (is_multi_line_) {
56 ChromeCanvas cc(0, 0, true); 56 ChromeCanvas cc(0, 0, true);
57 int w = width(), h = 0; 57 int w = width(), h = 0;
58 cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); 58 cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags());
59 out->cx = w; 59 prefsize.SetSize(w, h);
60 out->cy = h;
61 } else { 60 } else {
62 GetTextSize(out); 61 prefsize = GetTextSize();
63 } 62 }
64 63
65 gfx::Insets insets = GetInsets(); 64 gfx::Insets insets = GetInsets();
66 out->cx += insets.left() + insets.right(); 65 prefsize.Enlarge(insets.left() + insets.right(),
67 out->cy += insets.top() + insets.bottom(); 66 insets.top() + insets.bottom());
67 return prefsize;
68 } 68 }
69 69
70 int Label::ComputeMultiLineFlags() { 70 int Label::ComputeMultiLineFlags() {
71 int flags = ChromeCanvas::MULTI_LINE; 71 int flags = ChromeCanvas::MULTI_LINE;
72 switch (horiz_alignment_) { 72 switch (horiz_alignment_) {
73 case ALIGN_LEFT: 73 case ALIGN_LEFT:
74 flags |= ChromeCanvas::TEXT_ALIGN_LEFT; 74 flags |= ChromeCanvas::TEXT_ALIGN_LEFT;
75 break; 75 break;
76 case ALIGN_CENTER: 76 case ALIGN_CENTER:
77 flags |= ChromeCanvas::TEXT_ALIGN_CENTER; 77 flags |= ChromeCanvas::TEXT_ALIGN_CENTER;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 return text_; 172 return text_;
173 } 173 }
174 174
175 const GURL Label::GetURL() const { 175 const GURL Label::GetURL() const {
176 if (url_set_) 176 if (url_set_)
177 return url_; 177 return url_;
178 else 178 else
179 return GURL(text_); 179 return GURL(text_);
180 } 180 }
181 181
182 void Label::GetTextSize(CSize* out) { 182 gfx::Size Label::GetTextSize() {
183 if (!text_size_valid_) { 183 if (!text_size_valid_) {
184 text_size_.cx = font_.GetStringWidth(text_); 184 text_size_.SetSize(font_.GetStringWidth(text_), font_.height());
185 text_size_.cy = font_.height();
186 text_size_valid_ = true; 185 text_size_valid_ = true;
187 } 186 }
188 187
189 if (text_size_valid_) { 188 if (text_size_valid_)
190 *out = text_size_; 189 return text_size_;
191 } else { 190 return gfx::Size();
192 out->cx = out->cy = 0;
193 }
194 } 191 }
195 192
196 int Label::GetHeightForWidth(int w) { 193 int Label::GetHeightForWidth(int w) {
197 if (is_multi_line_) { 194 if (is_multi_line_) {
198 int h = 0; 195 int h = 0;
199 ChromeCanvas cc(0, 0, true); 196 ChromeCanvas cc(0, 0, true);
200 cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); 197 cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags());
201 return h; 198 return h;
202 } 199 }
203 200
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 304
308 void Label::SetContainsMouse(bool contains_mouse) { 305 void Label::SetContainsMouse(bool contains_mouse) {
309 if (contains_mouse_ == contains_mouse) 306 if (contains_mouse_ == contains_mouse)
310 return; 307 return;
311 contains_mouse_ = contains_mouse; 308 contains_mouse_ = contains_mouse;
312 if (GetMouseOverBackground()) 309 if (GetMouseOverBackground())
313 SchedulePaint(); 310 SchedulePaint();
314 } 311 }
315 312
316 gfx::Rect Label::GetTextBounds() { 313 gfx::Rect Label::GetTextBounds() {
317 CSize text_size; 314 gfx::Size text_size = GetTextSize();
318 GetTextSize(&text_size);
319 gfx::Insets insets = GetInsets(); 315 gfx::Insets insets = GetInsets();
320 int avail_width = width() - insets.left() - insets.right(); 316 int avail_width = width() - insets.left() - insets.right();
321 // Respect the size set by the owner view 317 // Respect the size set by the owner view
322 text_size.cx = std::min(avail_width, static_cast<int>(text_size.cx)); 318 text_size.set_width(std::min(avail_width, text_size.width()));
323 319
324 int text_y = insets.top() + 320 int text_y = insets.top() +
325 (height() - text_size.cy - insets.top() - insets.bottom()) / 2; 321 (height() - text_size.height() - insets.top() - insets.bottom()) / 2;
326 int text_x; 322 int text_x;
327 switch (horiz_alignment_) { 323 switch (horiz_alignment_) {
328 case ALIGN_LEFT: 324 case ALIGN_LEFT:
329 text_x = insets.left(); 325 text_x = insets.left();
330 break; 326 break;
331 case ALIGN_CENTER: 327 case ALIGN_CENTER:
332 // We put any extra margin pixel on the left rather than the right, since 328 // We put any extra margin pixel on the left rather than the right, since
333 // GetTextExtentPoint32() can report a value one too large on the right. 329 // GetTextExtentPoint32() can report a value one too large on the right.
334 text_x = insets.left() + (avail_width + 1 - text_size.cx) / 2; 330 text_x = insets.left() + (avail_width + 1 - text_size.width()) / 2;
335 break; 331 break;
336 case ALIGN_RIGHT: 332 case ALIGN_RIGHT:
337 text_x = width() - insets.right() - text_size.cx; 333 text_x = width() - insets.right() - text_size.width();
338 break; 334 break;
339 } 335 }
340 return gfx::Rect(text_x, text_y, text_size.cx, text_size.cy); 336 return gfx::Rect(text_x, text_y, text_size.width(), text_size.height());
341 } 337 }
342 338
343 void Label::SizeToFit(int max_width) { 339 void Label::SizeToFit(int max_width) {
344 DCHECK(is_multi_line_); 340 DCHECK(is_multi_line_);
345 341
346 std::vector<std::wstring> lines; 342 std::vector<std::wstring> lines;
347 SplitString(text_, L'\n', &lines); 343 SplitString(text_, L'\n', &lines);
348 344
349 int label_width = 0; 345 int label_width = 0;
350 for (std::vector<std::wstring>::const_iterator iter = lines.begin(); 346 for (std::vector<std::wstring>::const_iterator iter = lines.begin();
(...skipping 23 matching lines...) Expand all
374 370
375 bool Label::GetAccessibleState(VARIANT* state) { 371 bool Label::GetAccessibleState(VARIANT* state) {
376 DCHECK(state); 372 DCHECK(state);
377 373
378 state->lVal |= STATE_SYSTEM_READONLY; 374 state->lVal |= STATE_SYSTEM_READONLY;
379 return true; 375 return true;
380 } 376 }
381 377
382 } 378 }
383 379
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698