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 "ui/views/window/dialog_delegate.h"
7 #include "base/logging.h"
8 #include "grit/ui_strings.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/views/bubble/bubble_border.h"
11 #include "ui/views/bubble/bubble_frame_view.h"
12 #include "ui/views/controls/button/label_button.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_observer.h"
15 #include "ui/views/window/dialog_client_view.h"
16 #include "ui/wm/core/shadow_types.h"
20 ////////////////////////////////////////////////////////////////////////////////
23 DialogDelegate::~DialogDelegate() {
27 Widget
* DialogDelegate::CreateDialogWidget(DialogDelegate
* dialog
,
28 gfx::NativeView context
,
29 gfx::NativeView parent
) {
30 views::Widget
* widget
= new views::Widget
;
31 views::Widget::InitParams params
;
32 params
.delegate
= dialog
;
33 if (!dialog
|| dialog
->UseNewStyleForThisDialog()) {
34 params
.opacity
= Widget::InitParams::TRANSLUCENT_WINDOW
;
35 params
.remove_standard_frame
= true;
37 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
38 // Dialogs on Linux always have custom frames.
39 params
.remove_standard_frame
= true;
41 params
.context
= context
;
42 params
.parent
= parent
;
43 params
.top_level
= true;
48 View
* DialogDelegate::CreateExtraView() {
52 View
* DialogDelegate::CreateTitlebarExtraView() {
56 View
* DialogDelegate::CreateFootnoteView() {
60 bool DialogDelegate::Cancel() {
64 bool DialogDelegate::Accept(bool window_closing
) {
68 bool DialogDelegate::Accept() {
72 bool DialogDelegate::Close() {
73 int buttons
= GetDialogButtons();
74 if ((buttons
& ui::DIALOG_BUTTON_CANCEL
) ||
75 (buttons
== ui::DIALOG_BUTTON_NONE
)) {
81 base::string16
DialogDelegate::GetDialogLabel() const {
82 return base::string16();
85 base::string16
DialogDelegate::GetDialogTitle() const {
86 return GetWindowTitle();
89 int DialogDelegate::GetDialogButtons() const {
90 return ui::DIALOG_BUTTON_OK
| ui::DIALOG_BUTTON_CANCEL
;
93 int DialogDelegate::GetDefaultDialogButton() const {
94 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK
)
95 return ui::DIALOG_BUTTON_OK
;
96 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL
)
97 return ui::DIALOG_BUTTON_CANCEL
;
98 return ui::DIALOG_BUTTON_NONE
;
101 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
105 base::string16
DialogDelegate::GetDialogButtonLabel(
106 ui::DialogButton button
) const {
107 if (button
== ui::DIALOG_BUTTON_OK
)
108 return l10n_util::GetStringUTF16(IDS_APP_OK
);
109 if (button
== ui::DIALOG_BUTTON_CANCEL
) {
110 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK
)
111 return l10n_util::GetStringUTF16(IDS_APP_CANCEL
);
112 return l10n_util::GetStringUTF16(IDS_APP_CLOSE
);
115 return base::string16();
118 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button
) const {
122 View
* DialogDelegate::GetInitiallyFocusedView() {
123 // Focus the default button if any.
124 const DialogClientView
* dcv
= GetDialogClientView();
125 int default_button
= GetDefaultDialogButton();
126 if (default_button
== ui::DIALOG_BUTTON_NONE
)
129 if ((default_button
& GetDialogButtons()) == 0) {
130 // The default button is a button we don't have.
135 if (default_button
& ui::DIALOG_BUTTON_OK
)
136 return dcv
->ok_button();
137 if (default_button
& ui::DIALOG_BUTTON_CANCEL
)
138 return dcv
->cancel_button();
142 DialogDelegate
* DialogDelegate::AsDialogDelegate() {
146 ClientView
* DialogDelegate::CreateClientView(Widget
* widget
) {
147 return new DialogClientView(widget
, GetContentsView());
150 NonClientFrameView
* DialogDelegate::CreateNonClientFrameView(Widget
* widget
) {
151 if (UseNewStyleForThisDialog())
152 return CreateDialogFrameView(widget
);
153 return WidgetDelegate::CreateNonClientFrameView(widget
);
157 NonClientFrameView
* DialogDelegate::CreateDialogFrameView(Widget
* widget
) {
158 BubbleFrameView
* frame
= new BubbleFrameView(gfx::Insets());
159 const SkColor color
= widget
->GetNativeTheme()->GetSystemColor(
160 ui::NativeTheme::kColorId_DialogBackground
);
161 frame
->SetBubbleBorder(scoped_ptr
<BubbleBorder
>(new BubbleBorder(
162 BubbleBorder::FLOAT
, BubbleBorder::SMALL_SHADOW
, color
)));
163 DialogDelegate
* delegate
= widget
->widget_delegate()->AsDialogDelegate();
165 View
* titlebar_view
= delegate
->CreateTitlebarExtraView();
167 frame
->SetTitlebarExtraView(titlebar_view
);
169 // TODO(msw): Add a matching shadow type and remove the bubble frame border?
170 wm::SetShadowType(widget
->GetNativeWindow(), wm::SHADOW_TYPE_NONE
);
174 bool DialogDelegate::UseNewStyleForThisDialog() const {
178 const DialogClientView
* DialogDelegate::GetDialogClientView() const {
179 return GetWidget()->client_view()->AsDialogClientView();
182 DialogClientView
* DialogDelegate::GetDialogClientView() {
183 return GetWidget()->client_view()->AsDialogClientView();
186 ui::AXRole
DialogDelegate::GetAccessibleWindowRole() const {
187 return ui::AX_ROLE_DIALOG
;
190 ////////////////////////////////////////////////////////////////////////////////
191 // DialogDelegateView:
193 DialogDelegateView::DialogDelegateView() {
194 // A WidgetDelegate should be deleted on DeleteDelegate.
195 set_owned_by_client();
198 DialogDelegateView::~DialogDelegateView() {}
200 void DialogDelegateView::DeleteDelegate() {
204 Widget
* DialogDelegateView::GetWidget() {
205 return View::GetWidget();
208 const Widget
* DialogDelegateView::GetWidget() const {
209 return View::GetWidget();
212 View
* DialogDelegateView::GetContentsView() {