Upstream version 7.36.149.0
[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/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"
17
18 namespace views {
19
20 ////////////////////////////////////////////////////////////////////////////////
21 // DialogDelegate:
22
23 DialogDelegate::~DialogDelegate() {
24 }
25
26 // static
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;
36   }
37 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
38   // Dialogs on Linux always have custom frames.
39   params.remove_standard_frame = true;
40 #endif
41   params.context = context;
42   params.parent = parent;
43   params.top_level = true;
44   widget->Init(params);
45   return widget;
46 }
47
48 View* DialogDelegate::CreateExtraView() {
49   return NULL;
50 }
51
52 View* DialogDelegate::CreateTitlebarExtraView() {
53   return NULL;
54 }
55
56 View* DialogDelegate::CreateFootnoteView() {
57   return NULL;
58 }
59
60 bool DialogDelegate::Cancel() {
61   return true;
62 }
63
64 bool DialogDelegate::Accept(bool window_closing) {
65   return Accept();
66 }
67
68 bool DialogDelegate::Accept() {
69   return true;
70 }
71
72 bool DialogDelegate::Close() {
73   int buttons = GetDialogButtons();
74   if ((buttons & ui::DIALOG_BUTTON_CANCEL) ||
75       (buttons == ui::DIALOG_BUTTON_NONE)) {
76     return Cancel();
77   }
78   return Accept(true);
79 }
80
81 base::string16 DialogDelegate::GetDialogLabel() const {
82   return base::string16();
83 }
84
85 base::string16 DialogDelegate::GetDialogTitle() const {
86   return GetWindowTitle();
87 }
88
89 int DialogDelegate::GetDialogButtons() const {
90   return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
91 }
92
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;
99 }
100
101 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
102   return false;
103 }
104
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);
113   }
114   NOTREACHED();
115   return base::string16();
116 }
117
118 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const {
119   return true;
120 }
121
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)
127     return NULL;
128
129   if ((default_button & GetDialogButtons()) == 0) {
130     // The default button is a button we don't have.
131     NOTREACHED();
132     return NULL;
133   }
134
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();
139   return NULL;
140 }
141
142 DialogDelegate* DialogDelegate::AsDialogDelegate() {
143   return this;
144 }
145
146 ClientView* DialogDelegate::CreateClientView(Widget* widget) {
147   return new DialogClientView(widget, GetContentsView());
148 }
149
150 NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) {
151   if (UseNewStyleForThisDialog())
152     return CreateDialogFrameView(widget);
153   return WidgetDelegate::CreateNonClientFrameView(widget);
154 }
155
156 // static
157 NonClientFrameView* DialogDelegate::CreateDialogFrameView(Widget* widget) {
158   BubbleFrameView* frame = new BubbleFrameView(gfx::Insets());
159   scoped_ptr<BubbleBorder> border(new BubbleBorder(
160       BubbleBorder::FLOAT, BubbleBorder::SMALL_SHADOW, SK_ColorRED));
161   border->set_use_theme_background_color(true);
162   frame->SetBubbleBorder(border.Pass());
163   DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate();
164   if (delegate) {
165     View* titlebar_view = delegate->CreateTitlebarExtraView();
166     if (titlebar_view)
167       frame->SetTitlebarExtraView(titlebar_view);
168   }
169   // TODO(msw): Add a matching shadow type and remove the bubble frame border?
170   wm::SetShadowType(widget->GetNativeWindow(), wm::SHADOW_TYPE_NONE);
171   return frame;
172 }
173
174 bool DialogDelegate::UseNewStyleForThisDialog() const {
175   return true;
176 }
177
178 const DialogClientView* DialogDelegate::GetDialogClientView() const {
179   return GetWidget()->client_view()->AsDialogClientView();
180 }
181
182 DialogClientView* DialogDelegate::GetDialogClientView() {
183   return GetWidget()->client_view()->AsDialogClientView();
184 }
185
186 ui::AXRole DialogDelegate::GetAccessibleWindowRole() const {
187   return ui::AX_ROLE_DIALOG;
188 }
189
190 ////////////////////////////////////////////////////////////////////////////////
191 // DialogDelegateView:
192
193 DialogDelegateView::DialogDelegateView() {
194   // A WidgetDelegate should be deleted on DeleteDelegate.
195   set_owned_by_client();
196 }
197
198 DialogDelegateView::~DialogDelegateView() {}
199
200 void DialogDelegateView::DeleteDelegate() {
201   delete this;
202 }
203
204 Widget* DialogDelegateView::GetWidget() {
205   return View::GetWidget();
206 }
207
208 const Widget* DialogDelegateView::GetWidget() const {
209   return View::GetWidget();
210 }
211
212 View* DialogDelegateView::GetContentsView() {
213   return this;
214 }
215
216 }  // namespace views