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 "ui/base/l10n/l10n_util.h"
9 #include "ui/strings/grit/ui_strings.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"
18 #include "ui/base/win/shell.h"
23 ////////////////////////////////////////////////////////////////////////////////
26 DialogDelegate::DialogDelegate() : supports_new_style_(true) {
29 DialogDelegate::~DialogDelegate() {
33 Widget
* DialogDelegate::CreateDialogWidget(WidgetDelegate
* delegate
,
34 gfx::NativeWindow context
,
35 gfx::NativeView parent
) {
36 return CreateDialogWidgetWithBounds(delegate
, context
, parent
, gfx::Rect());
40 Widget
* DialogDelegate::CreateDialogWidgetWithBounds(WidgetDelegate
* delegate
,
41 gfx::NativeWindow context
,
42 gfx::NativeView parent
,
43 const gfx::Rect
& bounds
) {
44 views::Widget
* widget
= new views::Widget
;
45 views::Widget::InitParams params
;
46 params
.delegate
= delegate
;
47 params
.bounds
= bounds
;
48 DialogDelegate
* dialog
= delegate
->AsDialogDelegate();
50 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
51 // The new style doesn't support unparented dialogs on Linux desktop.
53 dialog
->supports_new_style_
&= parent
!= NULL
;
55 // The new style doesn't support unparented dialogs on Windows Classic themes.
56 if (dialog
&& !ui::win::IsAeroGlassEnabled())
57 dialog
->supports_new_style_
&= parent
!= NULL
;
60 if (!dialog
|| dialog
->UseNewStyleForThisDialog()) {
61 params
.opacity
= Widget::InitParams::TRANSLUCENT_WINDOW
;
62 params
.remove_standard_frame
= true;
63 // The bubble frame includes its own shadow; remove any native shadowing.
64 params
.shadow_type
= views::Widget::InitParams::SHADOW_TYPE_NONE
;
66 params
.context
= context
;
67 params
.parent
= parent
;
68 // Web-modal (ui::MODAL_TYPE_CHILD) dialogs with parents are marked as child
69 // widgets to prevent top-level window behavior (independent movement, etc).
70 params
.child
= parent
&& (delegate
->GetModalType() == ui::MODAL_TYPE_CHILD
);
75 View
* DialogDelegate::CreateExtraView() {
79 View
* DialogDelegate::CreateTitlebarExtraView() {
83 View
* DialogDelegate::CreateFootnoteView() {
87 bool DialogDelegate::Cancel() {
91 bool DialogDelegate::Accept(bool window_closing
) {
95 bool DialogDelegate::Accept() {
99 bool DialogDelegate::Close() {
100 int buttons
= GetDialogButtons();
101 if ((buttons
& ui::DIALOG_BUTTON_CANCEL
) ||
102 (buttons
== ui::DIALOG_BUTTON_NONE
)) {
108 base::string16
DialogDelegate::GetDialogLabel() const {
109 return base::string16();
112 base::string16
DialogDelegate::GetDialogTitle() const {
113 return GetWindowTitle();
116 int DialogDelegate::GetDialogButtons() const {
117 return ui::DIALOG_BUTTON_OK
| ui::DIALOG_BUTTON_CANCEL
;
120 int DialogDelegate::GetDefaultDialogButton() const {
121 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK
)
122 return ui::DIALOG_BUTTON_OK
;
123 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL
)
124 return ui::DIALOG_BUTTON_CANCEL
;
125 return ui::DIALOG_BUTTON_NONE
;
128 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
132 base::string16
DialogDelegate::GetDialogButtonLabel(
133 ui::DialogButton button
) const {
134 if (button
== ui::DIALOG_BUTTON_OK
)
135 return l10n_util::GetStringUTF16(IDS_APP_OK
);
136 if (button
== ui::DIALOG_BUTTON_CANCEL
) {
137 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK
)
138 return l10n_util::GetStringUTF16(IDS_APP_CANCEL
);
139 return l10n_util::GetStringUTF16(IDS_APP_CLOSE
);
142 return base::string16();
145 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button
) const {
149 View
* DialogDelegate::GetInitiallyFocusedView() {
150 // Focus the default button if any.
151 const DialogClientView
* dcv
= GetDialogClientView();
152 int default_button
= GetDefaultDialogButton();
153 if (default_button
== ui::DIALOG_BUTTON_NONE
)
156 if ((default_button
& GetDialogButtons()) == 0) {
157 // The default button is a button we don't have.
162 if (default_button
& ui::DIALOG_BUTTON_OK
)
163 return dcv
->ok_button();
164 if (default_button
& ui::DIALOG_BUTTON_CANCEL
)
165 return dcv
->cancel_button();
169 DialogDelegate
* DialogDelegate::AsDialogDelegate() {
173 ClientView
* DialogDelegate::CreateClientView(Widget
* widget
) {
174 return new DialogClientView(widget
, GetContentsView());
177 NonClientFrameView
* DialogDelegate::CreateNonClientFrameView(Widget
* widget
) {
178 if (UseNewStyleForThisDialog())
179 return CreateDialogFrameView(widget
);
180 return WidgetDelegate::CreateNonClientFrameView(widget
);
184 NonClientFrameView
* DialogDelegate::CreateDialogFrameView(Widget
* widget
) {
185 BubbleFrameView
* frame
= new BubbleFrameView(gfx::Insets());
186 scoped_ptr
<BubbleBorder
> border(new BubbleBorder(
187 BubbleBorder::FLOAT
, BubbleBorder::SMALL_SHADOW
, SK_ColorRED
));
188 border
->set_use_theme_background_color(true);
189 frame
->SetBubbleBorder(border
.Pass());
190 DialogDelegate
* delegate
= widget
->widget_delegate()->AsDialogDelegate();
192 View
* titlebar_view
= delegate
->CreateTitlebarExtraView();
194 frame
->SetTitlebarExtraView(titlebar_view
);
199 bool DialogDelegate::UseNewStyleForThisDialog() const {
200 return supports_new_style_
;
203 const DialogClientView
* DialogDelegate::GetDialogClientView() const {
204 return GetWidget()->client_view()->AsDialogClientView();
207 DialogClientView
* DialogDelegate::GetDialogClientView() {
208 return GetWidget()->client_view()->AsDialogClientView();
211 ui::AXRole
DialogDelegate::GetAccessibleWindowRole() const {
212 return ui::AX_ROLE_DIALOG
;
215 ////////////////////////////////////////////////////////////////////////////////
216 // DialogDelegateView:
218 DialogDelegateView::DialogDelegateView() {
219 // A WidgetDelegate should be deleted on DeleteDelegate.
220 set_owned_by_client();
223 DialogDelegateView::~DialogDelegateView() {}
225 void DialogDelegateView::DeleteDelegate() {
229 Widget
* DialogDelegateView::GetWidget() {
230 return View::GetWidget();
233 const Widget
* DialogDelegateView::GetWidget() const {
234 return View::GetWidget();
237 View
* DialogDelegateView::GetContentsView() {