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.
4
5 #include "ui/views/window/dialog_delegate.h"
6
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
17 #if defined(USE_AURA)
18 #include "ui/views/corewm/shadow_types.h"
19 #endif
20
21 namespace views {
22
23 ////////////////////////////////////////////////////////////////////////////////
24 // DialogDelegate:
25
~DialogDelegate()26 DialogDelegate::~DialogDelegate() {
27 }
28
29 // static
CreateDialogWidget(DialogDelegate * dialog,gfx::NativeWindow context,gfx::NativeWindow parent)30 Widget* DialogDelegate::CreateDialogWidget(DialogDelegate* dialog,
31 gfx::NativeWindow context,
32 gfx::NativeWindow parent) {
33 views::Widget* widget = new views::Widget;
34 views::Widget::InitParams params;
35 params.delegate = dialog;
36 if (!dialog || dialog->UseNewStyleForThisDialog()) {
37 params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
38 params.remove_standard_frame = true;
39 }
40 params.context = context;
41 params.parent = parent;
42 params.top_level = true;
43 widget->Init(params);
44 return widget;
45 }
46
CreateExtraView()47 View* DialogDelegate::CreateExtraView() {
48 return NULL;
49 }
50
CreateTitlebarExtraView()51 View* DialogDelegate::CreateTitlebarExtraView() {
52 return NULL;
53 }
54
CreateFootnoteView()55 View* DialogDelegate::CreateFootnoteView() {
56 return NULL;
57 }
58
Cancel()59 bool DialogDelegate::Cancel() {
60 return true;
61 }
62
Accept(bool window_closing)63 bool DialogDelegate::Accept(bool window_closing) {
64 return Accept();
65 }
66
Accept()67 bool DialogDelegate::Accept() {
68 return true;
69 }
70
Close()71 bool DialogDelegate::Close() {
72 int buttons = GetDialogButtons();
73 if ((buttons & ui::DIALOG_BUTTON_CANCEL) ||
74 (buttons == ui::DIALOG_BUTTON_NONE)) {
75 return Cancel();
76 }
77 return Accept(true);
78 }
79
GetDialogLabel() const80 base::string16 DialogDelegate::GetDialogLabel() const {
81 return base::string16();
82 }
83
GetDialogTitle() const84 base::string16 DialogDelegate::GetDialogTitle() const {
85 return GetWindowTitle();
86 }
87
GetDialogButtons() const88 int DialogDelegate::GetDialogButtons() const {
89 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
90 }
91
GetDefaultDialogButton() const92 int DialogDelegate::GetDefaultDialogButton() const {
93 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
94 return ui::DIALOG_BUTTON_OK;
95 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL)
96 return ui::DIALOG_BUTTON_CANCEL;
97 return ui::DIALOG_BUTTON_NONE;
98 }
99
ShouldDefaultButtonBeBlue() const100 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
101 return false;
102 }
103
GetDialogButtonLabel(ui::DialogButton button) const104 base::string16 DialogDelegate::GetDialogButtonLabel(
105 ui::DialogButton button) const {
106 if (button == ui::DIALOG_BUTTON_OK)
107 return l10n_util::GetStringUTF16(IDS_APP_OK);
108 if (button == ui::DIALOG_BUTTON_CANCEL) {
109 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
110 return l10n_util::GetStringUTF16(IDS_APP_CANCEL);
111 return l10n_util::GetStringUTF16(IDS_APP_CLOSE);
112 }
113 NOTREACHED();
114 return base::string16();
115 }
116
IsDialogButtonEnabled(ui::DialogButton button) const117 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const {
118 return true;
119 }
120
GetInitiallyFocusedView()121 View* DialogDelegate::GetInitiallyFocusedView() {
122 // Focus the default button if any.
123 const DialogClientView* dcv = GetDialogClientView();
124 int default_button = GetDefaultDialogButton();
125 if (default_button == ui::DIALOG_BUTTON_NONE)
126 return NULL;
127
128 if ((default_button & GetDialogButtons()) == 0) {
129 // The default button is a button we don't have.
130 NOTREACHED();
131 return NULL;
132 }
133
134 if (default_button & ui::DIALOG_BUTTON_OK)
135 return dcv->ok_button();
136 if (default_button & ui::DIALOG_BUTTON_CANCEL)
137 return dcv->cancel_button();
138 return NULL;
139 }
140
AsDialogDelegate()141 DialogDelegate* DialogDelegate::AsDialogDelegate() {
142 return this;
143 }
144
CreateClientView(Widget * widget)145 ClientView* DialogDelegate::CreateClientView(Widget* widget) {
146 return new DialogClientView(widget, GetContentsView());
147 }
148
CreateNonClientFrameView(Widget * widget)149 NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) {
150 if (UseNewStyleForThisDialog())
151 return CreateDialogFrameView(widget);
152 return WidgetDelegate::CreateNonClientFrameView(widget);
153 }
154
155 // static
CreateDialogFrameView(Widget * widget)156 NonClientFrameView* DialogDelegate::CreateDialogFrameView(Widget* widget) {
157 return CreateDialogFrameView(widget, false);
158 }
159
160 // static
CreateDialogFrameView(Widget * widget,bool force_opaque_border)161 NonClientFrameView* DialogDelegate::CreateDialogFrameView(
162 Widget* widget,
163 bool force_opaque_border) {
164 BubbleFrameView* frame = new BubbleFrameView(gfx::Insets());
165 const SkColor color = widget->GetNativeTheme()->GetSystemColor(
166 ui::NativeTheme::kColorId_DialogBackground);
167 if (force_opaque_border) {
168 frame->SetBubbleBorder(new BubbleBorder(
169 BubbleBorder::NONE,
170 BubbleBorder::NO_SHADOW_OPAQUE_BORDER,
171 color));
172 } else {
173 frame->SetBubbleBorder(new BubbleBorder(BubbleBorder::FLOAT,
174 BubbleBorder::SMALL_SHADOW,
175 color));
176 }
177 DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate();
178 if (delegate) {
179 View* titlebar_view = delegate->CreateTitlebarExtraView();
180 if (titlebar_view)
181 frame->SetTitlebarExtraView(titlebar_view);
182 }
183 if (force_opaque_border)
184 widget->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM);
185 #if defined(USE_AURA)
186 // TODO(msw): Add a matching shadow type and remove the bubble frame border?
187 corewm::SetShadowType(widget->GetNativeWindow(), corewm::SHADOW_TYPE_NONE);
188 #endif
189 return frame;
190 }
191
UseNewStyleForThisDialog() const192 bool DialogDelegate::UseNewStyleForThisDialog() const {
193 return true;
194 }
195
GetDialogClientView() const196 const DialogClientView* DialogDelegate::GetDialogClientView() const {
197 return GetWidget()->client_view()->AsDialogClientView();
198 }
199
GetDialogClientView()200 DialogClientView* DialogDelegate::GetDialogClientView() {
201 return GetWidget()->client_view()->AsDialogClientView();
202 }
203
GetAccessibleWindowRole() const204 ui::AccessibilityTypes::Role DialogDelegate::GetAccessibleWindowRole() const {
205 return ui::AccessibilityTypes::ROLE_DIALOG;
206 }
207
208 ////////////////////////////////////////////////////////////////////////////////
209 // DialogDelegateView:
210
DialogDelegateView()211 DialogDelegateView::DialogDelegateView() {
212 // A WidgetDelegate should be deleted on DeleteDelegate.
213 set_owned_by_client();
214 }
215
~DialogDelegateView()216 DialogDelegateView::~DialogDelegateView() {}
217
DeleteDelegate()218 void DialogDelegateView::DeleteDelegate() {
219 delete this;
220 }
221
GetWidget()222 Widget* DialogDelegateView::GetWidget() {
223 return View::GetWidget();
224 }
225
GetWidget() const226 const Widget* DialogDelegateView::GetWidget() const {
227 return View::GetWidget();
228 }
229
GetContentsView()230 View* DialogDelegateView::GetContentsView() {
231 return this;
232 }
233
234 } // namespace views
235