Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / ui / views / window / dialog_delegate.cc
blob2ce3d34a4fe95d148cb8da60014c935b44a7033d
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/accessibility/ax_view_state.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/strings/grit/ui_strings.h"
11 #include "ui/views/bubble/bubble_border.h"
12 #include "ui/views/bubble/bubble_frame_view.h"
13 #include "ui/views/controls/button/label_button.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"
18 #if defined(OS_WIN)
19 #include "ui/base/win/shell.h"
20 #endif
22 namespace views {
24 ////////////////////////////////////////////////////////////////////////////////
25 // DialogDelegate:
27 DialogDelegate::DialogDelegate() : supports_new_style_(true) {
30 DialogDelegate::~DialogDelegate() {
33 // static
34 Widget* DialogDelegate::CreateDialogWidget(WidgetDelegate* delegate,
35 gfx::NativeWindow context,
36 gfx::NativeView parent) {
37 return CreateDialogWidgetWithBounds(delegate, context, parent, gfx::Rect());
40 // static
41 Widget* DialogDelegate::CreateDialogWidgetWithBounds(WidgetDelegate* delegate,
42 gfx::NativeWindow context,
43 gfx::NativeView parent,
44 const gfx::Rect& bounds) {
45 views::Widget* widget = new views::Widget;
46 views::Widget::InitParams params;
47 params.delegate = delegate;
48 params.bounds = bounds;
49 DialogDelegate* dialog = delegate->AsDialogDelegate();
51 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
52 // The new style doesn't support unparented dialogs on Linux desktop.
53 if (dialog)
54 dialog->supports_new_style_ &= parent != NULL;
55 #elif defined(OS_WIN)
56 // The new style doesn't support unparented dialogs on Windows Classic themes.
57 if (dialog && !ui::win::IsAeroGlassEnabled())
58 dialog->supports_new_style_ &= parent != NULL;
59 #endif
61 if (!dialog || dialog->UseNewStyleForThisDialog()) {
62 params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
63 params.remove_standard_frame = true;
64 // The bubble frame includes its own shadow; remove any native shadowing.
65 params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE;
67 params.context = context;
68 params.parent = parent;
69 // Web-modal (ui::MODAL_TYPE_CHILD) dialogs with parents are marked as child
70 // widgets to prevent top-level window behavior (independent movement, etc).
71 params.child = parent && (delegate->GetModalType() == ui::MODAL_TYPE_CHILD);
72 widget->Init(params);
73 return widget;
76 View* DialogDelegate::CreateExtraView() {
77 return NULL;
80 bool DialogDelegate::GetExtraViewPadding(int* padding) {
81 return false;
84 View* DialogDelegate::CreateTitlebarExtraView() {
85 return NULL;
88 View* DialogDelegate::CreateFootnoteView() {
89 return NULL;
92 bool DialogDelegate::Cancel() {
93 return true;
96 bool DialogDelegate::Accept(bool window_closing) {
97 return Accept();
100 bool DialogDelegate::Accept() {
101 return true;
104 bool DialogDelegate::Close() {
105 int buttons = GetDialogButtons();
106 if ((buttons & ui::DIALOG_BUTTON_CANCEL) ||
107 (buttons == ui::DIALOG_BUTTON_NONE)) {
108 return Cancel();
110 return Accept(true);
113 base::string16 DialogDelegate::GetDialogTitle() const {
114 return GetWindowTitle();
117 int DialogDelegate::GetDialogButtons() const {
118 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
121 int DialogDelegate::GetDefaultDialogButton() const {
122 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
123 return ui::DIALOG_BUTTON_OK;
124 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL)
125 return ui::DIALOG_BUTTON_CANCEL;
126 return ui::DIALOG_BUTTON_NONE;
129 bool DialogDelegate::ShouldDefaultButtonBeBlue() const {
130 return false;
133 base::string16 DialogDelegate::GetDialogButtonLabel(
134 ui::DialogButton button) const {
135 if (button == ui::DIALOG_BUTTON_OK)
136 return l10n_util::GetStringUTF16(IDS_APP_OK);
137 if (button == ui::DIALOG_BUTTON_CANCEL) {
138 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK)
139 return l10n_util::GetStringUTF16(IDS_APP_CANCEL);
140 return l10n_util::GetStringUTF16(IDS_APP_CLOSE);
142 NOTREACHED();
143 return base::string16();
146 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const {
147 return true;
150 View* DialogDelegate::GetInitiallyFocusedView() {
151 // Focus the default button if any.
152 const DialogClientView* dcv = GetDialogClientView();
153 int default_button = GetDefaultDialogButton();
154 if (default_button == ui::DIALOG_BUTTON_NONE)
155 return NULL;
157 if ((default_button & GetDialogButtons()) == 0) {
158 // The default button is a button we don't have.
159 NOTREACHED();
160 return NULL;
163 if (default_button & ui::DIALOG_BUTTON_OK)
164 return dcv->ok_button();
165 if (default_button & ui::DIALOG_BUTTON_CANCEL)
166 return dcv->cancel_button();
167 return NULL;
170 DialogDelegate* DialogDelegate::AsDialogDelegate() {
171 return this;
174 ClientView* DialogDelegate::CreateClientView(Widget* widget) {
175 return new DialogClientView(widget, GetContentsView());
178 NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) {
179 if (UseNewStyleForThisDialog())
180 return CreateDialogFrameView(widget);
181 return WidgetDelegate::CreateNonClientFrameView(widget);
184 // static
185 NonClientFrameView* DialogDelegate::CreateDialogFrameView(Widget* widget) {
186 BubbleFrameView* frame = new BubbleFrameView(gfx::Insets());
187 scoped_ptr<BubbleBorder> border(new BubbleBorder(
188 BubbleBorder::FLOAT, BubbleBorder::SMALL_SHADOW, SK_ColorRED));
189 border->set_use_theme_background_color(true);
190 frame->SetBubbleBorder(border.Pass());
191 DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate();
192 if (delegate) {
193 View* titlebar_view = delegate->CreateTitlebarExtraView();
194 if (titlebar_view)
195 frame->SetTitlebarExtraView(titlebar_view);
197 return frame;
200 bool DialogDelegate::UseNewStyleForThisDialog() const {
201 return supports_new_style_;
204 const DialogClientView* DialogDelegate::GetDialogClientView() const {
205 return GetWidget()->client_view()->AsDialogClientView();
208 DialogClientView* DialogDelegate::GetDialogClientView() {
209 return GetWidget()->client_view()->AsDialogClientView();
212 ui::AXRole DialogDelegate::GetAccessibleWindowRole() const {
213 return ui::AX_ROLE_DIALOG;
216 ////////////////////////////////////////////////////////////////////////////////
217 // DialogDelegateView:
219 DialogDelegateView::DialogDelegateView() {
220 // A WidgetDelegate should be deleted on DeleteDelegate.
221 set_owned_by_client();
224 DialogDelegateView::~DialogDelegateView() {}
226 void DialogDelegateView::DeleteDelegate() {
227 delete this;
230 Widget* DialogDelegateView::GetWidget() {
231 return View::GetWidget();
234 const Widget* DialogDelegateView::GetWidget() const {
235 return View::GetWidget();
238 View* DialogDelegateView::GetContentsView() {
239 return this;
242 void DialogDelegateView::GetAccessibleState(ui::AXViewState* state) {
243 state->name = GetDialogTitle();
244 state->role = ui::AX_ROLE_DIALOG;
247 void DialogDelegateView::ViewHierarchyChanged(
248 const ViewHierarchyChangedDetails& details) {
249 if (details.is_add && details.child == this && GetWidget())
250 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
253 } // namespace views