- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / window / dialog_delegate.cc
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/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"
17
18 #if defined(USE_AURA)
19 #include "ui/views/corewm/shadow_types.h"
20 #endif
21
22 namespace views {
23
24 ////////////////////////////////////////////////////////////////////////////////
25 // DialogDelegate:
26
27 DialogDelegate::~DialogDelegate() {
28 }
29
30 // static
31 bool DialogDelegate::UseNewStyle() {
32   // The new dialog style cannot host native Windows textfield controls.
33   return Textfield::IsViewsTextfieldEnabled();
34 }
35
36 // static
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();
45   if (use_new_style) {
46     // Note: Transparent widgets cannot host native Windows textfield controls.
47     params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
48     params.remove_standard_frame = true;
49   }
50   params.context = context;
51   params.parent = parent;
52   params.top_level = true;
53   widget->Init(params);
54   return widget;
55 }
56
57 View* DialogDelegate::CreateExtraView() {
58   return NULL;
59 }
60
61 View* DialogDelegate::CreateTitlebarExtraView() {
62   return NULL;
63 }
64
65 View* DialogDelegate::CreateFootnoteView() {
66   return NULL;
67 }
68
69 bool DialogDelegate::Cancel() {
70   return true;
71 }
72
73 bool DialogDelegate::Accept(bool window_closing) {
74   return Accept();
75 }
76
77 bool DialogDelegate::Accept() {
78   return true;
79 }
80
81 bool DialogDelegate::Close() {
82   int buttons = GetDialogButtons();
83   if ((buttons & ui::DIALOG_BUTTON_CANCEL) ||
84       (buttons == ui::DIALOG_BUTTON_NONE)) {
85     return Cancel();
86   }
87   return Accept(true);
88 }
89
90 base::string16 DialogDelegate::GetDialogLabel() const {
91   return base::string16();
92 }
93
94 base::string16 DialogDelegate::GetDialogTitle() const {
95   return GetWindowTitle();
96 }
97
98 int DialogDelegate::GetDialogButtons() const {
99   return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
100 }
101
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;
108 }
109
110 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
111   return false;
112 }
113
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);
122   }
123   NOTREACHED();
124   return base::string16();
125 }
126
127 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const {
128   return true;
129 }
130
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)
136     return NULL;
137
138   if ((default_button & GetDialogButtons()) == 0) {
139     // The default button is a button we don't have.
140     NOTREACHED();
141     return NULL;
142   }
143
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();
148   return NULL;
149 }
150
151 DialogDelegate* DialogDelegate::AsDialogDelegate() {
152   return this;
153 }
154
155 ClientView* DialogDelegate::CreateClientView(Widget* widget) {
156   return new DialogClientView(widget, GetContentsView());
157 }
158
159 NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) {
160   if (UseNewStyleForThisDialog())
161     return CreateNewStyleFrameView(widget);
162   return WidgetDelegate::CreateNonClientFrameView(widget);
163 }
164
165 // static
166 NonClientFrameView* DialogDelegate::CreateNewStyleFrameView(Widget* widget) {
167   return CreateNewStyleFrameView(widget, false);
168 }
169
170 // static
171 NonClientFrameView* DialogDelegate::CreateNewStyleFrameView(
172     Widget* widget,
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(
179         BubbleBorder::NONE,
180         BubbleBorder::NO_SHADOW_OPAQUE_BORDER,
181         color));
182   } else {
183     frame->SetBubbleBorder(new BubbleBorder(BubbleBorder::FLOAT,
184                                             BubbleBorder::SMALL_SHADOW,
185                                             color));
186   }
187   DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate();
188   if (delegate) {
189     View* titlebar_view = delegate->CreateTitlebarExtraView();
190     if (titlebar_view)
191       frame->SetTitlebarExtraView(titlebar_view);
192   }
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);
198 #endif
199   return frame;
200 }
201
202 bool DialogDelegate::UseNewStyleForThisDialog() const {
203   return UseNewStyle();
204 }
205
206 const DialogClientView* DialogDelegate::GetDialogClientView() const {
207   return GetWidget()->client_view()->AsDialogClientView();
208 }
209
210 DialogClientView* DialogDelegate::GetDialogClientView() {
211   return GetWidget()->client_view()->AsDialogClientView();
212 }
213
214 ui::AccessibilityTypes::Role DialogDelegate::GetAccessibleWindowRole() const {
215   return ui::AccessibilityTypes::ROLE_DIALOG;
216 }
217
218 ////////////////////////////////////////////////////////////////////////////////
219 // DialogDelegateView:
220
221 DialogDelegateView::DialogDelegateView() {
222   // A WidgetDelegate should be deleted on DeleteDelegate.
223   set_owned_by_client();
224 }
225
226 DialogDelegateView::~DialogDelegateView() {}
227
228 void DialogDelegateView::DeleteDelegate() {
229   delete this;
230 }
231
232 Widget* DialogDelegateView::GetWidget() {
233   return View::GetWidget();
234 }
235
236 const Widget* DialogDelegateView::GetWidget() const {
237   return View::GetWidget();
238 }
239
240 View* DialogDelegateView::GetContentsView() {
241   return this;
242 }
243
244 }  // namespace views