| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/infobars/infobars.h" | 5 #include "chrome/browser/views/infobars/infobars.h" |
| 6 | 6 |
| 7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
| 8 #include "app/l10n_util.h" | 8 #include "app/l10n_util.h" |
| 9 #include "app/resource_bundle.h" | 9 #include "app/resource_bundle.h" |
| 10 #include "app/slide_animation.h" | 10 #include "app/slide_animation.h" |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 void AlertInfoBar::Layout() { | 303 void AlertInfoBar::Layout() { |
| 304 // Layout the close button. | 304 // Layout the close button. |
| 305 InfoBar::Layout(); | 305 InfoBar::Layout(); |
| 306 | 306 |
| 307 // Layout the icon and text. | 307 // Layout the icon and text. |
| 308 gfx::Size icon_ps = icon_->GetPreferredSize(); | 308 gfx::Size icon_ps = icon_->GetPreferredSize(); |
| 309 icon_->SetBounds(kHorizontalPadding, OffsetY(this, icon_ps), icon_ps.width(), | 309 icon_->SetBounds(kHorizontalPadding, OffsetY(this, icon_ps), icon_ps.width(), |
| 310 icon_ps.height()); | 310 icon_ps.height()); |
| 311 | 311 |
| 312 gfx::Size text_ps = label_->GetPreferredSize(); | 312 gfx::Size text_ps = label_->GetPreferredSize(); |
| 313 int text_width = | 313 int text_width = std::min( |
| 314 GetAvailableWidth() - icon_->bounds().right() - kIconLabelSpacing; | 314 text_ps.width(), |
| 315 GetAvailableWidth() - icon_->bounds().right() - kIconLabelSpacing); |
| 315 label_->SetBounds(icon_->bounds().right() + kIconLabelSpacing, | 316 label_->SetBounds(icon_->bounds().right() + kIconLabelSpacing, |
| 316 OffsetY(this, text_ps), text_width, text_ps.height()); | 317 OffsetY(this, text_ps), text_width, text_ps.height()); |
| 317 } | 318 } |
| 318 | 319 |
| 319 // AlertInfoBar, private: ------------------------------------------------------ | 320 // AlertInfoBar, private: ------------------------------------------------------ |
| 320 | 321 |
| 321 AlertInfoBarDelegate* AlertInfoBar::GetDelegate() { | 322 AlertInfoBarDelegate* AlertInfoBar::GetDelegate() { |
| 322 return delegate()->AsAlertInfoBarDelegate(); | 323 return delegate()->AsAlertInfoBarDelegate(); |
| 323 } | 324 } |
| 324 | 325 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 LinkInfoBarDelegate* LinkInfoBar::GetDelegate() { | 425 LinkInfoBarDelegate* LinkInfoBar::GetDelegate() { |
| 425 return delegate()->AsLinkInfoBarDelegate(); | 426 return delegate()->AsLinkInfoBarDelegate(); |
| 426 } | 427 } |
| 427 | 428 |
| 428 // ConfirmInfoBar, public: ----------------------------------------------------- | 429 // ConfirmInfoBar, public: ----------------------------------------------------- |
| 429 | 430 |
| 430 ConfirmInfoBar::ConfirmInfoBar(ConfirmInfoBarDelegate* delegate) | 431 ConfirmInfoBar::ConfirmInfoBar(ConfirmInfoBarDelegate* delegate) |
| 431 : AlertInfoBar(delegate), | 432 : AlertInfoBar(delegate), |
| 432 ok_button_(NULL), | 433 ok_button_(NULL), |
| 433 cancel_button_(NULL), | 434 cancel_button_(NULL), |
| 435 link_(NULL), |
| 434 initialized_(false) { | 436 initialized_(false) { |
| 435 ok_button_ = new views::NativeButton( | 437 ok_button_ = new views::NativeButton( |
| 436 this, delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK)); | 438 this, delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK)); |
| 437 if (delegate->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK_DEFAULT) | 439 if (delegate->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK_DEFAULT) |
| 438 ok_button_->SetAppearsAsDefault(true); | 440 ok_button_->SetAppearsAsDefault(true); |
| 439 cancel_button_ = new views::NativeButton( | 441 cancel_button_ = new views::NativeButton( |
| 440 this, delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL)); | 442 this, delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
| 443 |
| 444 // Set up the link. |
| 445 link_ = new views::Link; |
| 446 const std::wstring link_text(delegate->GetLinkText()); |
| 447 if (!link_text.empty()) { |
| 448 link_->SetText(link_text); |
| 449 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 450 link_->SetFont(rb.GetFont(ResourceBundle::MediumFont)); |
| 451 link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 452 link_->SetController(this); |
| 453 link_->MakeReadableOverBackgroundColor(background()->get_color()); |
| 454 } |
| 441 } | 455 } |
| 442 | 456 |
| 443 ConfirmInfoBar::~ConfirmInfoBar() { | 457 ConfirmInfoBar::~ConfirmInfoBar() { |
| 458 if (!initialized_) { |
| 459 delete ok_button_; |
| 460 delete cancel_button_; |
| 461 delete link_; |
| 462 } |
| 463 } |
| 464 |
| 465 // ConfirmInfoBar, views::LinkController implementation: ----------------------- |
| 466 |
| 467 void ConfirmInfoBar::LinkActivated(views::Link* source, int event_flags) { |
| 468 DCHECK(source == link_); |
| 469 DCHECK(link_->IsVisible()); |
| 470 DCHECK(!link_->GetText().empty()); |
| 471 if (GetDelegate()->LinkClicked( |
| 472 event_utils::DispositionFromEventFlags(event_flags))) { |
| 473 RemoveInfoBar(); |
| 474 } |
| 444 } | 475 } |
| 445 | 476 |
| 446 // ConfirmInfoBar, views::View overrides: -------------------------------------- | 477 // ConfirmInfoBar, views::View overrides: -------------------------------------- |
| 447 | 478 |
| 448 void ConfirmInfoBar::Layout() { | 479 void ConfirmInfoBar::Layout() { |
| 480 // First layout right aligned items (from right to left) in order to determine |
| 481 // the space avalable, then layout the left aligned items. |
| 482 |
| 483 // Layout the close button. |
| 449 InfoBar::Layout(); | 484 InfoBar::Layout(); |
| 450 int available_width = InfoBar::GetAvailableWidth(); | 485 |
| 486 // Layout the cancel and OK buttons. |
| 487 int available_width = AlertInfoBar::GetAvailableWidth(); |
| 451 int ok_button_width = 0; | 488 int ok_button_width = 0; |
| 452 int cancel_button_width = 0; | 489 int cancel_button_width = 0; |
| 453 gfx::Size ok_ps = ok_button_->GetPreferredSize(); | 490 gfx::Size ok_ps = ok_button_->GetPreferredSize(); |
| 454 gfx::Size cancel_ps = cancel_button_->GetPreferredSize(); | 491 gfx::Size cancel_ps = cancel_button_->GetPreferredSize(); |
| 455 | 492 |
| 456 if (GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK) { | 493 if (GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK) { |
| 457 ok_button_width = ok_ps.width(); | 494 ok_button_width = ok_ps.width(); |
| 458 } else { | 495 } else { |
| 459 ok_button_->SetVisible(false); | 496 ok_button_->SetVisible(false); |
| 460 } | 497 } |
| 461 if (GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL) { | 498 if (GetDelegate()->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL) { |
| 462 cancel_button_width = cancel_ps.width(); | 499 cancel_button_width = cancel_ps.width(); |
| 463 } else { | 500 } else { |
| 464 cancel_button_->SetVisible(false); | 501 cancel_button_->SetVisible(false); |
| 465 } | 502 } |
| 466 | 503 |
| 467 cancel_button_->SetBounds(available_width - cancel_button_width, | 504 cancel_button_->SetBounds(available_width - cancel_button_width, |
| 468 OffsetY(this, cancel_ps), cancel_ps.width(), | 505 OffsetY(this, cancel_ps), cancel_ps.width(), |
| 469 cancel_ps.height()); | 506 cancel_ps.height()); |
| 470 int spacing = cancel_button_width > 0 ? kButtonButtonSpacing : 0; | 507 int spacing = cancel_button_width > 0 ? kButtonButtonSpacing : 0; |
| 471 ok_button_->SetBounds(cancel_button_->x() - spacing - ok_button_width, | 508 ok_button_->SetBounds(cancel_button_->x() - spacing - ok_button_width, |
| 472 OffsetY(this, ok_ps), ok_ps.width(), ok_ps.height()); | 509 OffsetY(this, ok_ps), ok_ps.width(), ok_ps.height()); |
| 510 |
| 511 // Layout the icon and label. |
| 473 AlertInfoBar::Layout(); | 512 AlertInfoBar::Layout(); |
| 513 |
| 514 // Now append the link to the label's right edge. |
| 515 link_->SetVisible(!link_->GetText().empty()); |
| 516 gfx::Size link_ps = link_->GetPreferredSize(); |
| 517 int link_x = label()->bounds().right() + kEndOfLabelSpacing; |
| 518 int link_w = std::min(GetAvailableWidth() - link_x, link_ps.width()); |
| 519 link_->SetBounds(link_x, OffsetY(this, link_ps), link_w, link_ps.height()); |
| 474 } | 520 } |
| 475 | 521 |
| 476 void ConfirmInfoBar::ViewHierarchyChanged(bool is_add, | 522 void ConfirmInfoBar::ViewHierarchyChanged(bool is_add, |
| 477 views::View* parent, | 523 views::View* parent, |
| 478 views::View* child) { | 524 views::View* child) { |
| 479 InfoBar::ViewHierarchyChanged(is_add, parent, child); | 525 InfoBar::ViewHierarchyChanged(is_add, parent, child); |
| 480 if (is_add && child == this && !initialized_) { | 526 if (is_add && child == this && !initialized_) { |
| 481 Init(); | 527 Init(); |
| 482 initialized_ = true; | 528 initialized_ = true; |
| 483 } | 529 } |
| 484 } | 530 } |
| 485 | 531 |
| 486 // ConfirmInfoBar, views::ButtonListener implementation: --------------- | 532 // ConfirmInfoBar, views::ButtonListener implementation: --------------- |
| 487 | 533 |
| 488 void ConfirmInfoBar::ButtonPressed( | 534 void ConfirmInfoBar::ButtonPressed( |
| 489 views::Button* sender, const views::Event& event) { | 535 views::Button* sender, const views::Event& event) { |
| 490 InfoBar::ButtonPressed(sender, event); | 536 InfoBar::ButtonPressed(sender, event); |
| 491 if (sender == ok_button_) { | 537 if (sender == ok_button_) { |
| 492 if (GetDelegate()->Accept()) | 538 if (GetDelegate()->Accept()) |
| 493 RemoveInfoBar(); | 539 RemoveInfoBar(); |
| 494 } else if (sender == cancel_button_) { | 540 } else if (sender == cancel_button_) { |
| 495 if (GetDelegate()->Cancel()) | 541 if (GetDelegate()->Cancel()) |
| 496 RemoveInfoBar(); | 542 RemoveInfoBar(); |
| 497 } | 543 } |
| 498 } | 544 } |
| 499 | 545 |
| 500 // ConfirmInfoBar, InfoBar overrides: ------------------------------------------ | 546 // ConfirmInfoBar, InfoBar overrides: ------------------------------------------ |
| 501 | 547 |
| 502 int ConfirmInfoBar::GetAvailableWidth() const { | 548 int ConfirmInfoBar::GetAvailableWidth() const { |
| 503 if (ok_button_) | 549 return ok_button_->x() - kEndOfLabelSpacing; |
| 504 return ok_button_->x() - kEndOfLabelSpacing; | |
| 505 if (cancel_button_) | |
| 506 return cancel_button_->x() - kEndOfLabelSpacing; | |
| 507 return InfoBar::GetAvailableWidth(); | |
| 508 } | 550 } |
| 509 | 551 |
| 510 // ConfirmInfoBar, private: ---------------------------------------------------- | 552 // ConfirmInfoBar, private: ---------------------------------------------------- |
| 511 | 553 |
| 512 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { | 554 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { |
| 513 return delegate()->AsConfirmInfoBarDelegate(); | 555 return delegate()->AsConfirmInfoBarDelegate(); |
| 514 } | 556 } |
| 515 | 557 |
| 516 void ConfirmInfoBar::Init() { | 558 void ConfirmInfoBar::Init() { |
| 517 AddChildView(ok_button_); | 559 AddChildView(ok_button_); |
| 518 AddChildView(cancel_button_); | 560 AddChildView(cancel_button_); |
| 561 AddChildView(link_); |
| 519 } | 562 } |
| 520 | 563 |
| 521 // AlertInfoBarDelegate, InfoBarDelegate overrides: ---------------------------- | 564 // AlertInfoBarDelegate, InfoBarDelegate overrides: ---------------------------- |
| 522 | 565 |
| 523 InfoBar* AlertInfoBarDelegate::CreateInfoBar() { | 566 InfoBar* AlertInfoBarDelegate::CreateInfoBar() { |
| 524 return new AlertInfoBar(this); | 567 return new AlertInfoBar(this); |
| 525 } | 568 } |
| 526 | 569 |
| 527 // LinkInfoBarDelegate, InfoBarDelegate overrides: ----------------------------- | 570 // LinkInfoBarDelegate, InfoBarDelegate overrides: ----------------------------- |
| 528 | 571 |
| 529 InfoBar* LinkInfoBarDelegate::CreateInfoBar() { | 572 InfoBar* LinkInfoBarDelegate::CreateInfoBar() { |
| 530 return new LinkInfoBar(this); | 573 return new LinkInfoBar(this); |
| 531 } | 574 } |
| 532 | 575 |
| 533 // ConfirmInfoBarDelegate, InfoBarDelegate overrides: -------------------------- | 576 // ConfirmInfoBarDelegate, InfoBarDelegate overrides: -------------------------- |
| 534 | 577 |
| 535 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { | 578 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { |
| 536 return new ConfirmInfoBar(this); | 579 return new ConfirmInfoBar(this); |
| 537 } | 580 } |
| OLD | NEW |