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/controls/textfield/textfield.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_observer.h"
16 #include "ui/views/window/dialog_client_view.h"
19 #include "ui/views/corewm/shadow_types.h"
24 ////////////////////////////////////////////////////////////////////////////////
27 DialogDelegate::~DialogDelegate() {
31 bool DialogDelegate::UseNewStyle() {
32 // The new dialog style cannot host native Windows textfield controls.
33 return Textfield::IsViewsTextfieldEnabled();
37 Widget* DialogDelegate::CreateDialogWidget(DialogDelegate* dialog,
38 gfx::NativeWindow context,
39 gfx::NativeWindow parent) {
40 views::Widget* widget = new views::Widget;
41 views::Widget::InitParams params;
42 params.delegate = dialog;
43 const bool use_new_style = dialog ?
44 dialog->UseNewStyleForThisDialog() : DialogDelegate::UseNewStyle();
46 // Note: Transparent widgets cannot host native Windows textfield controls.
47 params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
48 params.remove_standard_frame = true;
50 params.context = context;
51 params.parent = parent;
52 params.top_level = true;
57 View* DialogDelegate::CreateExtraView() {
61 View* DialogDelegate::CreateTitlebarExtraView() {
65 View* DialogDelegate::CreateFootnoteView() {
69 bool DialogDelegate::Cancel() {
73 bool DialogDelegate::Accept(bool window_closing) {
77 bool DialogDelegate::Accept() {
81 bool DialogDelegate::Close() {
82 int buttons = GetDialogButtons();
83 if ((buttons & ui::DIALOG_BUTTON_CANCEL) ||
84 (buttons == ui::DIALOG_BUTTON_NONE)) {
90 base::string16 DialogDelegate::GetDialogLabel() const {
91 return base::string16();
94 base::string16 DialogDelegate::GetDialogTitle() const {
95 return GetWindowTitle();
98 int DialogDelegate::GetDialogButtons() const {
99 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
102 int DialogDelegate::GetDefaultDialogButton() const {
103 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
104 return ui::DIALOG_BUTTON_OK;
105 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL)
106 return ui::DIALOG_BUTTON_CANCEL;
107 return ui::DIALOG_BUTTON_NONE;
110 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
114 base::string16 DialogDelegate::GetDialogButtonLabel(
115 ui::DialogButton button) const {
116 if (button == ui::DIALOG_BUTTON_OK)
117 return l10n_util::GetStringUTF16(IDS_APP_OK);
118 if (button == ui::DIALOG_BUTTON_CANCEL) {
119 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
120 return l10n_util::GetStringUTF16(IDS_APP_CANCEL);
121 return l10n_util::GetStringUTF16(IDS_APP_CLOSE);
124 return base::string16();
127 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const {
131 View* DialogDelegate::GetInitiallyFocusedView() {
132 // Focus the default button if any.
133 const DialogClientView* dcv = GetDialogClientView();
134 int default_button = GetDefaultDialogButton();
135 if (default_button == ui::DIALOG_BUTTON_NONE)
138 if ((default_button & GetDialogButtons()) == 0) {
139 // The default button is a button we don't have.
144 if (default_button & ui::DIALOG_BUTTON_OK)
145 return dcv->ok_button();
146 if (default_button & ui::DIALOG_BUTTON_CANCEL)
147 return dcv->cancel_button();
151 DialogDelegate* DialogDelegate::AsDialogDelegate() {
155 ClientView* DialogDelegate::CreateClientView(Widget* widget) {
156 return new DialogClientView(widget, GetContentsView());
159 NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) {
160 if (UseNewStyleForThisDialog())
161 return CreateNewStyleFrameView(widget);
162 return WidgetDelegate::CreateNonClientFrameView(widget);
166 NonClientFrameView* DialogDelegate::CreateNewStyleFrameView(Widget* widget) {
167 return CreateNewStyleFrameView(widget, false);
171 NonClientFrameView* DialogDelegate::CreateNewStyleFrameView(
173 bool force_opaque_border) {
174 BubbleFrameView* frame = new BubbleFrameView(gfx::Insets());
175 const SkColor color = widget->GetNativeTheme()->GetSystemColor(
176 ui::NativeTheme::kColorId_DialogBackground);
177 if (force_opaque_border) {
178 frame->SetBubbleBorder(new BubbleBorder(
180 BubbleBorder::NO_SHADOW_OPAQUE_BORDER,
183 frame->SetBubbleBorder(new BubbleBorder(BubbleBorder::FLOAT,
184 BubbleBorder::SMALL_SHADOW,
187 DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate();
189 View* titlebar_view = delegate->CreateTitlebarExtraView();
191 frame->SetTitlebarExtraView(titlebar_view);
193 if (force_opaque_border)
194 widget->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM);
195 #if defined(USE_AURA)
196 // TODO(msw): Add a matching shadow type and remove the bubble frame border?
197 corewm::SetShadowType(widget->GetNativeWindow(), corewm::SHADOW_TYPE_NONE);
202 bool DialogDelegate::UseNewStyleForThisDialog() const {
203 return UseNewStyle();
206 const DialogClientView* DialogDelegate::GetDialogClientView() const {
207 return GetWidget()->client_view()->AsDialogClientView();
210 DialogClientView* DialogDelegate::GetDialogClientView() {
211 return GetWidget()->client_view()->AsDialogClientView();
214 ui::AccessibilityTypes::Role DialogDelegate::GetAccessibleWindowRole() const {
215 return ui::AccessibilityTypes::ROLE_DIALOG;
218 ////////////////////////////////////////////////////////////////////////////////
219 // DialogDelegateView:
221 DialogDelegateView::DialogDelegateView() {
222 // A WidgetDelegate should be deleted on DeleteDelegate.
223 set_owned_by_client();
226 DialogDelegateView::~DialogDelegateView() {}
228 void DialogDelegateView::DeleteDelegate() {
232 Widget* DialogDelegateView::GetWidget() {
233 return View::GetWidget();
236 const Widget* DialogDelegateView::GetWidget() const {
237 return View::GetWidget();
240 View* DialogDelegateView::GetContentsView() {