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

Side by Side Diff: chrome/browser/views/about_chrome_view.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/browser/views/about_chrome_view.h" 5 #include "chrome/browser/views/about_chrome_view.h"
6 6
7 #include <commdlg.h> 7 #include <commdlg.h>
8 8
9 #include "base/file_version_info.h" 9 #include "base/file_version_info.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 terms_of_service_url_ = 182 terms_of_service_url_ =
183 new ChromeViews::Link(l10n_util::GetString(IDS_TERMS_OF_SERVICE)); 183 new ChromeViews::Link(l10n_util::GetString(IDS_TERMS_OF_SERVICE));
184 AddChildView(terms_of_service_url_); 184 AddChildView(terms_of_service_url_);
185 terms_of_service_url_->SetController(this); 185 terms_of_service_url_->SetController(this);
186 #endif 186 #endif
187 } 187 }
188 188
189 //////////////////////////////////////////////////////////////////////////////// 189 ////////////////////////////////////////////////////////////////////////////////
190 // AboutChromeView, ChromeViews::View implementation: 190 // AboutChromeView, ChromeViews::View implementation:
191 191
192 void AboutChromeView::GetPreferredSize(CSize *out) { 192 gfx::Size AboutChromeView::GetPreferredSize() {
193 DCHECK(out); 193 gfx::Size prefsize(ChromeViews::Window::GetLocalizedContentsSize(
194 *out = ChromeViews::Window::GetLocalizedContentsSize(
195 IDS_ABOUT_DIALOG_WIDTH_CHARS, 194 IDS_ABOUT_DIALOG_WIDTH_CHARS,
196 IDS_ABOUT_DIALOG_HEIGHT_LINES).ToSIZE(); 195 IDS_ABOUT_DIALOG_HEIGHT_LINES));
197 // We compute the height of the dialog based on the size of the image (it 196 // We compute the height of the dialog based on the size of the image (it
198 // would be nice to not hard code this), the text in the about dialog and the 197 // would be nice to not hard code this), the text in the about dialog and the
199 // margins around the text. 198 // margins around the text.
200 out->cy += 145 + (kPanelVertMargin * 2); 199 prefsize.Enlarge(0, 145 + (kPanelVertMargin * 2));
201 // TODO(beng): Eventually the image should be positioned such that hard- 200 // TODO(beng): Eventually the image should be positioned such that hard-
202 // coding the width isn't necessary. This breaks with fonts 201 // coding the width isn't necessary. This breaks with fonts
203 // that are large and cause wrapping. 202 // that are large and cause wrapping.
204 out->cx = 422; 203 prefsize.set_width(422);
204 return prefsize;
205 } 205 }
206 206
207 void AboutChromeView::Layout() { 207 void AboutChromeView::Layout() {
208 CSize panel_size; 208 gfx::Size panel_size = GetPreferredSize();
209 GetPreferredSize(&panel_size);
210
211 CSize sz;
212 209
213 // Background image for the dialog. 210 // Background image for the dialog.
214 about_dlg_background_->GetPreferredSize(&sz); 211 gfx::Size sz = about_dlg_background_->GetPreferredSize();
215 int background_image_height = sz.cy; // used to position main text below. 212 // used to position main text below.
216 about_dlg_background_->SetBounds(0, 0, sz.cx, sz.cy); 213 int background_image_height = sz.height();
214 about_dlg_background_->SetBounds(0, 0, sz.width(), sz.height());
217 215
218 // First label goes to the top left corner. 216 // First label goes to the top left corner.
219 about_title_label_->GetPreferredSize(&sz); 217 sz = about_title_label_->GetPreferredSize();
220 about_title_label_->SetBounds(kPanelHorizMargin, kPanelVertMargin, 218 about_title_label_->SetBounds(kPanelHorizMargin, kPanelVertMargin,
221 sz.cx, sz.cy); 219 sz.width(), sz.height());
222 220
223 // Then we have the version number right below it. 221 // Then we have the version number right below it.
224 version_label_->GetPreferredSize(&sz); 222 sz = version_label_->GetPreferredSize();
225 version_label_->SetBounds(kPanelHorizMargin, 223 version_label_->SetBounds(kPanelHorizMargin,
226 about_title_label_->y() + 224 about_title_label_->y() +
227 about_title_label_->height() + 225 about_title_label_->height() +
228 kRelatedControlVerticalSpacing, 226 kRelatedControlVerticalSpacing,
229 kVersionFieldWidth, 227 kVersionFieldWidth,
230 sz.cy); 228 sz.height());
231 229
232 // For the width of the main text label we want to use up the whole panel 230 // For the width of the main text label we want to use up the whole panel
233 // width and remaining height, minus a little margin on each side. 231 // width and remaining height, minus a little margin on each side.
234 int y_pos = background_image_height + kRelatedControlVerticalSpacing; 232 int y_pos = background_image_height + kRelatedControlVerticalSpacing;
235 sz.cx = panel_size.cx - 2 * kPanelHorizMargin; 233 sz.set_width(panel_size.width() - 2 * kPanelHorizMargin);
236 234
237 // Draw the text right below the background image. 235 // Draw the text right below the background image.
238 copyright_label_->SetBounds(kPanelHorizMargin, 236 copyright_label_->SetBounds(kPanelHorizMargin,
239 y_pos, 237 y_pos,
240 sz.cx, 238 sz.width(),
241 sz.cy); 239 sz.height());
242 240
243 // Then the main_text_label. 241 // Then the main_text_label.
244 main_text_label_->SetBounds(kPanelHorizMargin, 242 main_text_label_->SetBounds(kPanelHorizMargin,
245 copyright_label_->y() + 243 copyright_label_->y() +
246 copyright_label_->height(), 244 copyright_label_->height(),
247 sz.cx, 245 sz.width(),
248 main_text_label_height_); 246 main_text_label_height_);
249 247
250 // Position the URLs within the main label. The rects here are calculated when 248 // Position the URLs within the main label. The rects here are calculated when
251 // we draw the strings. First position the Chromium URL within the main label. 249 // we draw the strings. First position the Chromium URL within the main label.
252 chromium_url_->SetBounds(chromium_url_rect_.x(), 250 chromium_url_->SetBounds(chromium_url_rect_.x(),
253 chromium_url_rect_.y(), 251 chromium_url_rect_.y(),
254 chromium_url_rect_.width(), 252 chromium_url_rect_.width(),
255 chromium_url_rect_.height()); 253 chromium_url_rect_.height());
256 // Then position the Open Source URL within the main label. 254 // Then position the Open Source URL within the main label.
257 open_source_url_->SetBounds(open_source_url_rect_.x(), 255 open_source_url_->SetBounds(open_source_url_rect_.x(),
258 open_source_url_rect_.y(), 256 open_source_url_rect_.y(),
259 open_source_url_rect_.width(), 257 open_source_url_rect_.width(),
260 open_source_url_rect_.height()); 258 open_source_url_rect_.height());
261 #if defined(GOOGLE_CHROME_BUILD) 259 #if defined(GOOGLE_CHROME_BUILD)
262 // Then position the TOS URL within the main label. 260 // Then position the TOS URL within the main label.
263 terms_of_service_url_->SetBounds(terms_of_service_url_rect_.x(), 261 terms_of_service_url_->SetBounds(terms_of_service_url_rect_.x(),
264 terms_of_service_url_rect_.y(), 262 terms_of_service_url_rect_.y(),
265 terms_of_service_url_rect_.width(), 263 terms_of_service_url_rect_.width(),
266 terms_of_service_url_rect_.height()); 264 terms_of_service_url_rect_.height());
267 #endif 265 #endif
268 266
269 // Get the y-coordinate of our parent so we can position the text left of the 267 // Get the y-coordinate of our parent so we can position the text left of the
270 // buttons at the bottom. 268 // buttons at the bottom.
271 CRect parent_bounds; 269 CRect parent_bounds;
272 GetParent()->GetLocalBounds(&parent_bounds, false); 270 GetParent()->GetLocalBounds(&parent_bounds, false);
273 271
274 throbber_->GetPreferredSize(&sz); 272 sz = throbber_->GetPreferredSize();
275 int throbber_topleft_x = kPanelHorizMargin; 273 int throbber_topleft_x = kPanelHorizMargin;
276 int throbber_topleft_y = parent_bounds.bottom - sz.cy - 274 int throbber_topleft_y = parent_bounds.bottom - sz.height() -
277 kButtonVEdgeMargin - 3; 275 kButtonVEdgeMargin - 3;
278 throbber_->SetBounds(throbber_topleft_x, throbber_topleft_y, sz.cx, sz.cy); 276 throbber_->SetBounds(throbber_topleft_x, throbber_topleft_y,
277 sz.width(), sz.height());
279 278
280 // This image is hidden (see ViewHierarchyChanged) and displayed on demand. 279 // This image is hidden (see ViewHierarchyChanged) and displayed on demand.
281 success_indicator_.GetPreferredSize(&sz); 280 sz = success_indicator_.GetPreferredSize();
282 success_indicator_.SetBounds(throbber_topleft_x, throbber_topleft_y, 281 success_indicator_.SetBounds(throbber_topleft_x, throbber_topleft_y,
283 sz.cx, sz.cy); 282 sz.width(), sz.height());
284 283
285 // This image is hidden (see ViewHierarchyChanged) and displayed on demand. 284 // This image is hidden (see ViewHierarchyChanged) and displayed on demand.
286 update_available_indicator_.GetPreferredSize(&sz); 285 sz = update_available_indicator_.GetPreferredSize();
287 update_available_indicator_.SetBounds(throbber_topleft_x, throbber_topleft_y, 286 update_available_indicator_.SetBounds(throbber_topleft_x, throbber_topleft_y,
288 sz.cx, sz.cy); 287 sz.width(), sz.height());
289 288
290 // This image is hidden (see ViewHierarchyChanged) and displayed on demand. 289 // This image is hidden (see ViewHierarchyChanged) and displayed on demand.
291 timeout_indicator_.GetPreferredSize(&sz); 290 sz = timeout_indicator_.GetPreferredSize();
292 timeout_indicator_.SetBounds(throbber_topleft_x, throbber_topleft_y, 291 timeout_indicator_.SetBounds(throbber_topleft_x, throbber_topleft_y,
293 sz.cx, sz.cy); 292 sz.width(), sz.height());
294 293
295 // The update label should be at the bottom of the screen, to the right of 294 // The update label should be at the bottom of the screen, to the right of
296 // the throbber. We specify width to the end of the dialog because it contains 295 // the throbber. We specify width to the end of the dialog because it contains
297 // variable length messages. 296 // variable length messages.
298 update_label_.GetPreferredSize(&sz); 297 sz = update_label_.GetPreferredSize();
299 int update_label_x = throbber_->x() + throbber_->width() + 298 int update_label_x = throbber_->x() + throbber_->width() +
300 kRelatedControlHorizontalSpacing; 299 kRelatedControlHorizontalSpacing;
301 update_label_.SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT); 300 update_label_.SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
302 update_label_.SetBounds(update_label_x, 301 update_label_.SetBounds(update_label_x,
303 throbber_topleft_y + 1, 302 throbber_topleft_y + 1,
304 parent_bounds.Width() - update_label_x, 303 parent_bounds.Width() - update_label_x,
305 sz.cy); 304 sz.height());
306 } 305 }
307 306
308 307
309 void AboutChromeView::Paint(ChromeCanvas* canvas) { 308 void AboutChromeView::Paint(ChromeCanvas* canvas) {
310 ChromeViews::View::Paint(canvas); 309 ChromeViews::View::Paint(canvas);
311 310
312 ChromeFont font = 311 ChromeFont font =
313 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); 312 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
314 313
315 const gfx::Rect label_bounds = main_text_label_->bounds(); 314 const gfx::Rect label_bounds = main_text_label_->bounds();
316 315
317 ChromeViews::Link* link1 = 316 ChromeViews::Link* link1 =
318 chromium_url_appears_first_ ? chromium_url_ : open_source_url_; 317 chromium_url_appears_first_ ? chromium_url_ : open_source_url_;
319 ChromeViews::Link* link2 = 318 ChromeViews::Link* link2 =
320 chromium_url_appears_first_ ? open_source_url_ : chromium_url_; 319 chromium_url_appears_first_ ? open_source_url_ : chromium_url_;
321 gfx::Rect* rect1 = chromium_url_appears_first_ ? 320 gfx::Rect* rect1 = chromium_url_appears_first_ ?
322 &chromium_url_rect_ : &open_source_url_rect_; 321 &chromium_url_rect_ : &open_source_url_rect_;
323 gfx::Rect* rect2 = chromium_url_appears_first_ ? 322 gfx::Rect* rect2 = chromium_url_appears_first_ ?
324 &open_source_url_rect_ : &chromium_url_rect_; 323 &open_source_url_rect_ : &chromium_url_rect_;
325 324
326 // This struct keeps track of where to write the next word (which x,y 325 // This struct keeps track of where to write the next word (which x,y
327 // pixel coordinate). This struct is updated after drawing text and checking 326 // pixel coordinate). This struct is updated after drawing text and checking
328 // if we need to wrap. 327 // if we need to wrap.
329 CSize position; 328 gfx::Size position;
330 // Draw the first text chunk and position the Chromium url. 329 // Draw the first text chunk and position the Chromium url.
331 DrawTextAndPositionUrl(canvas, main_label_chunk1_, link1, 330 DrawTextAndPositionUrl(canvas, main_label_chunk1_, link1,
332 rect1, &position, label_bounds, font); 331 rect1, &position, label_bounds, font);
333 // Draw the second text chunk and position the Open Source url. 332 // Draw the second text chunk and position the Open Source url.
334 DrawTextAndPositionUrl(canvas, main_label_chunk2_, link2, 333 DrawTextAndPositionUrl(canvas, main_label_chunk2_, link2,
335 rect2, &position, label_bounds, font); 334 rect2, &position, label_bounds, font);
336 // Draw the third text chunk. 335 // Draw the third text chunk.
337 DrawTextStartingFrom(canvas, main_label_chunk3_, &position, label_bounds, 336 DrawTextStartingFrom(canvas, main_label_chunk3_, &position, label_bounds,
338 font); 337 font);
339 338
340 #if defined(GOOGLE_CHROME_BUILD) 339 #if defined(GOOGLE_CHROME_BUILD)
341 // Insert a line break and some whitespace. 340 // Insert a line break and some whitespace.
342 position.cx = 0; 341 position.set_width(0);
343 position.cy += font.height() + kRelatedControlVerticalSpacing; 342 position.Enlarge(0, font.height() + kRelatedControlVerticalSpacing);
344 343
345 // And now the Terms of Service and position the TOS url. 344 // And now the Terms of Service and position the TOS url.
346 DrawTextAndPositionUrl(canvas, main_label_chunk4_, terms_of_service_url_, 345 DrawTextAndPositionUrl(canvas, main_label_chunk4_, terms_of_service_url_,
347 &terms_of_service_url_rect_, &position, label_bounds, 346 &terms_of_service_url_rect_, &position, label_bounds,
348 font); 347 font);
349 // The last text chunk doesn't have a URL associated with it. 348 // The last text chunk doesn't have a URL associated with it.
350 DrawTextStartingFrom(canvas, main_label_chunk5_, &position, label_bounds, 349 DrawTextStartingFrom(canvas, main_label_chunk5_, &position, label_bounds,
351 font); 350 font);
352 #endif 351 #endif
353 352
354 // Save the height so we can set the bounds correctly. 353 // Save the height so we can set the bounds correctly.
355 main_text_label_height_ = position.cy + font.height(); 354 main_text_label_height_ = position.height() + font.height();
356 } 355 }
357 356
358 void AboutChromeView::DrawTextAndPositionUrl(ChromeCanvas* canvas, 357 void AboutChromeView::DrawTextAndPositionUrl(ChromeCanvas* canvas,
359 const std::wstring& text, 358 const std::wstring& text,
360 ChromeViews::Link* link, 359 ChromeViews::Link* link,
361 gfx::Rect* rect, 360 gfx::Rect* rect,
362 CSize* position, 361 gfx::Size* position,
363 const gfx::Rect& bounds, 362 const gfx::Rect& bounds,
364 const ChromeFont& font) { 363 const ChromeFont& font) {
365 DCHECK(canvas && link && rect && position); 364 DCHECK(canvas && link && rect && position);
366 // Draw the text chunk. 365 // Draw the text chunk.
367 DrawTextStartingFrom(canvas, text, position, bounds, font); 366 DrawTextStartingFrom(canvas, text, position, bounds, font);
368 367
369 // And then position the link after it. 368 // And then position the link after it.
370 CSize sz; 369 gfx::Size sz = link->GetPreferredSize();
371 link->GetPreferredSize(&sz); 370 WrapIfWordDoesntFit(sz.width(), font.height(), position, bounds);
372 WrapIfWordDoesntFit(sz.cx, font.height(), position, bounds); 371 *rect = gfx::Rect(position->width(), position->height(), sz.width(),
373 *rect = gfx::Rect(position->cx, position->cy, sz.cx, sz.cy); 372 sz.height());
374 373
375 // Going from relative to absolute pixel coordinates again. 374 // Going from relative to absolute pixel coordinates again.
376 rect->Offset(bounds.x(), bounds.y()); 375 rect->Offset(bounds.x(), bounds.y());
377 // And leave some space to draw the link in. 376 // And leave some space to draw the link in.
378 position->cx += sz.cx; 377 position->Enlarge(sz.width(), 0);
379 } 378 }
380 379
381 void AboutChromeView::DrawTextStartingFrom(ChromeCanvas* canvas, 380 void AboutChromeView::DrawTextStartingFrom(ChromeCanvas* canvas,
382 const std::wstring& text, 381 const std::wstring& text,
383 CSize* position, 382 gfx::Size* position,
384 const gfx::Rect& bounds, 383 const gfx::Rect& bounds,
385 const ChromeFont& font) { 384 const ChromeFont& font) {
386 // Iterate through line breaking opportunities (which in English would be 385 // Iterate through line breaking opportunities (which in English would be
387 // spaces and such. This tells us where to wrap. 386 // spaces and such. This tells us where to wrap.
388 WordIterator iter(text, WordIterator::BREAK_LINE); 387 WordIterator iter(text, WordIterator::BREAK_LINE);
389 if (!iter.Init()) 388 if (!iter.Init())
390 return; 389 return;
391 390
392 int flags = (UILayoutIsRightToLeft() ? 391 int flags = (UILayoutIsRightToLeft() ?
393 ChromeCanvas::TEXT_ALIGN_RIGHT : 392 ChromeCanvas::TEXT_ALIGN_RIGHT :
394 ChromeCanvas::TEXT_ALIGN_LEFT) | 393 ChromeCanvas::TEXT_ALIGN_LEFT) |
395 ChromeCanvas::MULTI_LINE | 394 ChromeCanvas::MULTI_LINE |
396 ChromeCanvas::HIDE_PREFIX; 395 ChromeCanvas::HIDE_PREFIX;
397 396
398 // Iterate over each word in the text, or put in a more locale-neutral way: 397 // Iterate over each word in the text, or put in a more locale-neutral way:
399 // iterate to the next line breaking opportunity. 398 // iterate to the next line breaking opportunity.
400 while (iter.Advance()) { 399 while (iter.Advance()) {
401 // Get the word and figure out the dimensions. 400 // Get the word and figure out the dimensions.
402 std::wstring word = iter.GetWord(); 401 std::wstring word = iter.GetWord();
403 int w = font.GetStringWidth(word), h = font.height(); 402 int w = font.GetStringWidth(word), h = font.height();
404 canvas->SizeStringInt(word, font, &w, &h, flags); 403 canvas->SizeStringInt(word, font, &w, &h, flags);
405 404
406 // If we exceed the boundaries, we need to wrap. 405 // If we exceed the boundaries, we need to wrap.
407 WrapIfWordDoesntFit(w, font.height(), position, bounds); 406 WrapIfWordDoesntFit(w, font.height(), position, bounds);
408 407
409 // Draw the word on the screen (mirrored if RTL locale). 408 // Draw the word on the screen (mirrored if RTL locale).
410 canvas->DrawStringInt(word, font, SK_ColorBLACK, 409 canvas->DrawStringInt(word, font, SK_ColorBLACK,
411 main_text_label_->MirroredXCoordinateInsideView( 410 main_text_label_->MirroredXCoordinateInsideView(
412 position->cx + bounds.x()), 411 position->width() + bounds.x()),
413 position->cy + bounds.y(), 412 position->height() + bounds.y(),
414 w, h, flags); 413 w, h, flags);
415 414
416 if (word.size() > 0 && word[word.size() - 1] == L'\x0a') { 415 if (word.size() > 0 && word[word.size() - 1] == L'\x0a') {
417 // When we come across '\n', we move to the beginning of the next line. 416 // When we come across '\n', we move to the beginning of the next line.
418 position->cx = 0; 417 position->set_width(0);
419 position->cy += font.height(); 418 position->Enlarge(0, font.height());
420 } else { 419 } else {
421 // Otherwise, we advance position to the next word. 420 // Otherwise, we advance position to the next word.
422 position->cx += w; 421 position->Enlarge(w, 0);
423 } 422 }
424 } 423 }
425 } 424 }
426 425
427 void AboutChromeView::WrapIfWordDoesntFit(int word_width, 426 void AboutChromeView::WrapIfWordDoesntFit(int word_width,
428 int font_height, 427 int font_height,
429 CSize* position, 428 gfx::Size* position,
430 const gfx::Rect& bounds) { 429 const gfx::Rect& bounds) {
431 if (position->cx + word_width > bounds.right()) { 430 if (position->width() + word_width > bounds.right()) {
432 position->cx = 0; 431 position->set_width(0);
433 position->cy += font_height; 432 position->Enlarge(0, font_height);
434 } 433 }
435 } 434 }
436 435
437 void AboutChromeView::ViewHierarchyChanged(bool is_add, 436 void AboutChromeView::ViewHierarchyChanged(bool is_add,
438 ChromeViews::View* parent, 437 ChromeViews::View* parent,
439 ChromeViews::View* child) { 438 ChromeViews::View* child) {
440 // Since we want the some of the controls to show up in the same visual row 439 // Since we want the some of the controls to show up in the same visual row
441 // as the buttons, which are provided by the framework, we must add the 440 // as the buttons, which are provided by the framework, we must add the
442 // buttons to the non-client view, which is the parent of this view. 441 // buttons to the non-client view, which is the parent of this view.
443 // Similarly, when we're removed from the view hierarchy, we must take care 442 // Similarly, when we're removed from the view hierarchy, we must take care
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 681
683 // We have updated controls on the parent, so we need to update its layout. 682 // We have updated controls on the parent, so we need to update its layout.
684 View* parent = GetParent(); 683 View* parent = GetParent();
685 parent->Layout(); 684 parent->Layout();
686 685
687 // Check button may have appeared/disappeared. We cannot call this during 686 // Check button may have appeared/disappeared. We cannot call this during
688 // ViewHierarchyChanged because the |window()| pointer hasn't been set yet. 687 // ViewHierarchyChanged because the |window()| pointer hasn't been set yet.
689 if (window()) 688 if (window())
690 GetDialogClientView()->UpdateDialogButtons(); 689 GetDialogClientView()->UpdateDialogButtons();
691 } 690 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698