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/tab_modal_confirm_dialog_views.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser_dialogs.h"
9 #include "chrome/browser/ui/browser_list.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
16 #include "content/public/browser/web_contents.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/window_open_disposition.h"
20 #include "ui/views/controls/message_box_view.h"
21 #include "ui/views/layout/layout_constants.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/window/dialog_client_view.h"
25 using web_modal::WebContentsModalDialogManager
;
26 using web_modal::WebContentsModalDialogManagerDelegate
;
29 TabModalConfirmDialog
* TabModalConfirmDialog::Create(
30 TabModalConfirmDialogDelegate
* delegate
,
31 content::WebContents
* web_contents
) {
32 return new TabModalConfirmDialogViews(
33 delegate
, web_contents
);
36 //////////////////////////////////////////////////////////////////////////////
37 // TabModalConfirmDialogViews, constructor & destructor:
39 TabModalConfirmDialogViews::TabModalConfirmDialogViews(
40 TabModalConfirmDialogDelegate
* delegate
,
41 content::WebContents
* web_contents
)
42 : delegate_(delegate
),
44 views::MessageBoxView::InitParams
init_params(delegate
->GetDialogMessage());
45 init_params
.inter_row_vertical_spacing
=
46 views::kUnrelatedControlVerticalSpacing
;
47 message_box_view_
= new views::MessageBoxView(init_params
);
49 base::string16
link_text(delegate
->GetLinkText());
50 if (!link_text
.empty())
51 message_box_view_
->SetLink(link_text
, this);
53 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
54 WebContentsModalDialogManager::FromWebContents(web_contents
);
55 WebContentsModalDialogManagerDelegate
* modal_delegate
=
56 web_contents_modal_dialog_manager
->delegate();
57 DCHECK(modal_delegate
);
58 dialog_
= views::Widget::CreateWindowAsFramelessChild(
59 this, modal_delegate
->GetWebContentsModalDialogHost()->GetHostView());
60 web_contents_modal_dialog_manager
->ShowDialog(dialog_
->GetNativeView());
61 delegate_
->set_close_delegate(this);
64 TabModalConfirmDialogViews::~TabModalConfirmDialogViews() {
67 void TabModalConfirmDialogViews::AcceptTabModalDialog() {
68 GetDialogClientView()->AcceptWindow();
71 void TabModalConfirmDialogViews::CancelTabModalDialog() {
72 GetDialogClientView()->CancelWindow();
75 void TabModalConfirmDialogViews::CloseDialog() {
79 //////////////////////////////////////////////////////////////////////////////
80 // TabModalConfirmDialogViews, views::LinkListener implementation:
82 void TabModalConfirmDialogViews::LinkClicked(views::Link
* source
,
84 delegate_
->LinkClicked(ui::DispositionFromEventFlags(event_flags
));
87 //////////////////////////////////////////////////////////////////////////////
88 // TabModalConfirmDialogViews, views::DialogDelegate implementation:
90 base::string16
TabModalConfirmDialogViews::GetWindowTitle() const {
91 return delegate_
->GetTitle();
94 base::string16
TabModalConfirmDialogViews::GetDialogButtonLabel(
95 ui::DialogButton button
) const {
96 if (button
== ui::DIALOG_BUTTON_OK
)
97 return delegate_
->GetAcceptButtonTitle();
98 if (button
== ui::DIALOG_BUTTON_CANCEL
)
99 return delegate_
->GetCancelButtonTitle();
100 return base::string16();
103 bool TabModalConfirmDialogViews::Cancel() {
108 bool TabModalConfirmDialogViews::Accept() {
113 bool TabModalConfirmDialogViews::Close() {
118 ///////////////////////////////////////////////////////////////////////////////
119 // TabModalConfirmDialogViews, views::WidgetDelegate implementation:
121 views::View
* TabModalConfirmDialogViews::GetContentsView() {
122 return message_box_view_
;
125 views::Widget
* TabModalConfirmDialogViews::GetWidget() {
126 return message_box_view_
->GetWidget();
129 const views::Widget
* TabModalConfirmDialogViews::GetWidget() const {
130 return message_box_view_
->GetWidget();
133 void TabModalConfirmDialogViews::DeleteDelegate() {
137 ui::ModalType
TabModalConfirmDialogViews::GetModalType() const {
139 return ui::MODAL_TYPE_CHILD
;
141 return views::WidgetDelegate::GetModalType();