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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/infobars/confirm_infobar.h"
7 #include "base/logging.h"
8 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
9 #include "ui/base/window_open_disposition.h"
10 #include "ui/views/controls/button/label_button.h"
11 #include "ui/views/controls/label.h"
12 #include "ui/views/controls/link.h"
15 // ConfirmInfoBarDelegate -----------------------------------------------------
18 scoped_ptr
<InfoBar
> ConfirmInfoBarDelegate::CreateInfoBar(
19 scoped_ptr
<ConfirmInfoBarDelegate
> delegate
) {
20 return scoped_ptr
<InfoBar
>(new ConfirmInfoBar(delegate
.Pass()));
24 // ConfirmInfoBar -------------------------------------------------------------
26 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
> delegate
)
27 : InfoBarView(delegate
.PassAs
<InfoBarDelegate
>()),
34 ConfirmInfoBar::~ConfirmInfoBar() {
37 void ConfirmInfoBar::Layout() {
38 InfoBarView::Layout();
42 labels
.push_back(label_
);
43 labels
.push_back(link_
);
44 AssignWidths(&labels
, std::max(0, EndX() - x
- NonLabelWidth()));
46 label_
->SetPosition(gfx::Point(x
, OffsetY(label_
)));
47 if (!label_
->text().empty())
48 x
= label_
->bounds().right() + kEndOfLabelSpacing
;
51 ok_button_
->SetPosition(gfx::Point(x
, OffsetY(ok_button_
)));
52 x
= ok_button_
->bounds().right() + kButtonButtonSpacing
;
56 cancel_button_
->SetPosition(gfx::Point(x
, OffsetY(cancel_button_
)));
58 link_
->SetPosition(gfx::Point(EndX() - link_
->width(), OffsetY(link_
)));
61 void ConfirmInfoBar::ViewHierarchyChanged(
62 const ViewHierarchyChangedDetails
& details
) {
63 if (details
.is_add
&& details
.child
== this && (label_
== NULL
)) {
64 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
65 label_
= CreateLabel(delegate
->GetMessageText());
68 if (delegate
->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK
) {
69 ok_button_
= CreateLabelButton(this,
70 delegate
->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK
),
71 delegate
->NeedElevation(ConfirmInfoBarDelegate::BUTTON_OK
));
72 AddChildView(ok_button_
);
75 if (delegate
->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL
) {
76 cancel_button_
= CreateLabelButton(this,
77 delegate
->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL
),
78 delegate
->NeedElevation(ConfirmInfoBarDelegate::BUTTON_CANCEL
));
79 AddChildView(cancel_button_
);
82 base::string16
link_text(delegate
->GetLinkText());
83 link_
= CreateLink(link_text
, this);
87 // This must happen after adding all other children so InfoBarView can ensure
88 // the close button is the last child.
89 InfoBarView::ViewHierarchyChanged(details
);
92 void ConfirmInfoBar::ButtonPressed(views::Button
* sender
,
93 const ui::Event
& event
) {
95 return; // We're closing; don't call anything, it might access the owner.
96 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
97 if ((ok_button_
!= NULL
) && sender
== ok_button_
) {
98 if (delegate
->Accept())
100 } else if ((cancel_button_
!= NULL
) && (sender
== cancel_button_
)) {
101 if (delegate
->Cancel())
104 InfoBarView::ButtonPressed(sender
, event
);
108 int ConfirmInfoBar::ContentMinimumWidth() {
109 return label_
->GetMinimumSize().width() + link_
->GetMinimumSize().width() +
113 void ConfirmInfoBar::LinkClicked(views::Link
* source
, int event_flags
) {
115 return; // We're closing; don't call anything, it might access the owner.
116 DCHECK_EQ(link_
, source
);
117 if (GetDelegate()->LinkClicked(ui::DispositionFromEventFlags(event_flags
)))
121 ConfirmInfoBarDelegate
* ConfirmInfoBar::GetDelegate() {
122 return delegate()->AsConfirmInfoBarDelegate();
125 int ConfirmInfoBar::NonLabelWidth() const {
126 int width
= (label_
->text().empty() || (!ok_button_
&& !cancel_button_
)) ?
127 0 : kEndOfLabelSpacing
;
129 width
+= ok_button_
->width() + (cancel_button_
? kButtonButtonSpacing
: 0);
130 width
+= (cancel_button_
? cancel_button_
->width() : 0);
131 return width
+ ((link_
->text().empty() || !width
) ? 0 : kEndOfLabelSpacing
);