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/infobar_service.h"
9 #include "chrome/browser/ui/views/elevation_icon_setter.h"
10 #include "components/infobars/core/confirm_infobar_delegate.h"
11 #include "ui/base/window_open_disposition.h"
12 #include "ui/views/controls/button/label_button.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/controls/link.h"
16 // InfoBarService -------------------------------------------------------------
18 scoped_ptr
<infobars::InfoBar
> InfoBarService::CreateConfirmInfoBar(
19 scoped_ptr
<ConfirmInfoBarDelegate
> delegate
) {
20 return make_scoped_ptr(new ConfirmInfoBar(delegate
.Pass()));
24 // ConfirmInfoBar -------------------------------------------------------------
26 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
> delegate
)
27 : InfoBarView(delegate
.Pass()),
34 ConfirmInfoBar::~ConfirmInfoBar() {
35 // Ensure |elevation_icon_setter_| is destroyed before |ok_button_|.
36 elevation_icon_setter_
.reset();
39 void ConfirmInfoBar::Layout() {
40 InfoBarView::Layout();
44 labels
.push_back(label_
);
45 labels
.push_back(link_
);
46 AssignWidths(&labels
, std::max(0, EndX() - x
- NonLabelWidth()));
48 label_
->SetPosition(gfx::Point(x
, OffsetY(label_
)));
49 if (!label_
->text().empty())
50 x
= label_
->bounds().right() + kEndOfLabelSpacing
;
53 ok_button_
->SetPosition(gfx::Point(x
, OffsetY(ok_button_
)));
54 x
= ok_button_
->bounds().right() + kButtonButtonSpacing
;
58 cancel_button_
->SetPosition(gfx::Point(x
, OffsetY(cancel_button_
)));
60 link_
->SetPosition(gfx::Point(EndX() - link_
->width(), OffsetY(link_
)));
63 void ConfirmInfoBar::ViewHierarchyChanged(
64 const ViewHierarchyChangedDetails
& details
) {
65 if (details
.is_add
&& details
.child
== this && (label_
== NULL
)) {
66 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
67 label_
= CreateLabel(delegate
->GetMessageText());
70 if (delegate
->GetButtons() & ConfirmInfoBarDelegate::BUTTON_OK
) {
71 ok_button_
= CreateLabelButton(
72 this, delegate
->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK
));
73 if (delegate
->OKButtonTriggersUACPrompt())
74 elevation_icon_setter_
.reset(new ElevationIconSetter(
76 base::Bind(&ConfirmInfoBar::Layout
, base::Unretained(this))));
77 AddChildView(ok_button_
);
80 if (delegate
->GetButtons() & ConfirmInfoBarDelegate::BUTTON_CANCEL
) {
81 cancel_button_
= CreateLabelButton(
83 delegate
->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL
));
84 AddChildView(cancel_button_
);
87 base::string16
link_text(delegate
->GetLinkText());
88 link_
= CreateLink(link_text
, this);
92 // This must happen after adding all other children so InfoBarView can ensure
93 // the close button is the last child.
94 InfoBarView::ViewHierarchyChanged(details
);
97 void ConfirmInfoBar::ButtonPressed(views::Button
* sender
,
98 const ui::Event
& event
) {
100 return; // We're closing; don't call anything, it might access the owner.
101 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
102 if ((ok_button_
!= NULL
) && sender
== ok_button_
) {
103 if (delegate
->Accept())
105 } else if ((cancel_button_
!= NULL
) && (sender
== cancel_button_
)) {
106 if (delegate
->Cancel())
109 InfoBarView::ButtonPressed(sender
, event
);
113 int ConfirmInfoBar::ContentMinimumWidth() const {
114 return label_
->GetMinimumSize().width() + link_
->GetMinimumSize().width() +
118 void ConfirmInfoBar::LinkClicked(views::Link
* source
, int event_flags
) {
120 return; // We're closing; don't call anything, it might access the owner.
121 DCHECK_EQ(link_
, source
);
122 if (GetDelegate()->LinkClicked(ui::DispositionFromEventFlags(event_flags
)))
126 ConfirmInfoBarDelegate
* ConfirmInfoBar::GetDelegate() {
127 return delegate()->AsConfirmInfoBarDelegate();
130 int ConfirmInfoBar::NonLabelWidth() const {
131 int width
= (label_
->text().empty() || (!ok_button_
&& !cancel_button_
)) ?
132 0 : kEndOfLabelSpacing
;
134 width
+= ok_button_
->width() + (cancel_button_
? kButtonButtonSpacing
: 0);
135 width
+= (cancel_button_
? cancel_button_
->width() : 0);
136 return width
+ ((link_
->text().empty() || !width
) ? 0 : kEndOfLabelSpacing
);