dialog_delegate.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/command_line.h" 8#include "base/logging.h" 9#include "grit/ui_strings.h" 10#include "ui/base/l10n/l10n_util.h" 11#include "ui/base/ui_base_switches.h" 12#include "ui/views/bubble/bubble_border.h" 13#include "ui/views/bubble/bubble_frame_view.h" 14#include "ui/views/controls/button/label_button.h" 15#include "ui/views/widget/widget.h" 16#include "ui/views/widget/widget_observer.h" 17#include "ui/views/window/dialog_client_view.h" 18 19#if defined(USE_AURA) 20#include "ui/views/corewm/shadow_types.h" 21#endif 22 23namespace views { 24 25namespace { 26 27// Create a widget to host the dialog. 28Widget* CreateDialogWidgetImpl(DialogDelegateView* dialog_delegate_view, 29 gfx::NativeWindow context, 30 gfx::NativeWindow parent) { 31 views::Widget* widget = new views::Widget; 32 views::Widget::InitParams params; 33 params.delegate = dialog_delegate_view; 34 if (DialogDelegate::UseNewStyle()) { 35 // TODO(msw): Avoid Windows native controls or support dialog transparency 36 // with a separate border Widget, like BubbleDelegateView. 37 params.transparent = views::View::get_use_acceleration_when_possible(); 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 if (DialogDelegate::UseNewStyle()) { 45#if defined(USE_AURA) 46 // TODO(msw): Add a matching shadow type and remove the bubble frame border? 47 corewm::SetShadowType(widget->GetNativeWindow(), corewm::SHADOW_TYPE_NONE); 48#endif 49 } 50 return widget; 51} 52 53} // namespace 54 55 56//////////////////////////////////////////////////////////////////////////////// 57// DialogDelegate: 58 59DialogDelegate::~DialogDelegate() { 60} 61 62// static 63bool DialogDelegate::UseNewStyle() { 64 static const bool use_new_style = CommandLine::ForCurrentProcess()->HasSwitch( 65 switches::kEnableNewDialogStyle); 66 return use_new_style; 67} 68 69int DialogDelegate::GetDialogButtons() const { 70 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; 71} 72 73int DialogDelegate::GetDefaultDialogButton() const { 74 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK) 75 return ui::DIALOG_BUTTON_OK; 76 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL) 77 return ui::DIALOG_BUTTON_CANCEL; 78 return ui::DIALOG_BUTTON_NONE; 79} 80 81string16 DialogDelegate::GetDialogButtonLabel(ui::DialogButton button) const { 82 if (button == ui::DIALOG_BUTTON_OK) 83 return l10n_util::GetStringUTF16(IDS_APP_OK); 84 if (button == ui::DIALOG_BUTTON_CANCEL) { 85 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK) 86 return l10n_util::GetStringUTF16(IDS_APP_CANCEL); 87 return l10n_util::GetStringUTF16(IDS_APP_CLOSE); 88 } 89 NOTREACHED(); 90 return string16(); 91} 92 93bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const { 94 return true; 95} 96 97View* DialogDelegate::CreateExtraView() { 98 return NULL; 99} 100 101View* DialogDelegate::CreateTitlebarExtraView() { 102 return NULL; 103} 104 105View* DialogDelegate::CreateFootnoteView() { 106 return NULL; 107} 108 109bool DialogDelegate::Cancel() { 110 return true; 111} 112 113bool DialogDelegate::Accept(bool window_closing) { 114 return Accept(); 115} 116 117bool DialogDelegate::Accept() { 118 return true; 119} 120 121View* 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 141DialogDelegate* DialogDelegate::AsDialogDelegate() { 142 return this; 143} 144 145ClientView* DialogDelegate::CreateClientView(Widget* widget) { 146 return new DialogClientView(widget, GetContentsView()); 147} 148 149NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) { 150 return UseNewStyle() ? CreateNewStyleFrameView(widget) : 151 WidgetDelegate::CreateNonClientFrameView(widget); 152} 153 154// static 155NonClientFrameView* DialogDelegate::CreateNewStyleFrameView(Widget* widget) { 156 BubbleFrameView* frame = new BubbleFrameView(gfx::Insets(20, 20, 20, 20)); 157 const SkColor color = widget->GetNativeTheme()->GetSystemColor( 158 ui::NativeTheme::kColorId_DialogBackground); 159 frame->SetBubbleBorder( 160 new BubbleBorder(BubbleBorder::FLOAT, BubbleBorder::SMALL_SHADOW, color)); 161 frame->SetTitle(widget->widget_delegate()->GetWindowTitle()); 162 DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate(); 163 if (delegate) { 164 View* titlebar_view = delegate->CreateTitlebarExtraView(); 165 if (titlebar_view) 166 frame->SetTitlebarExtraView(titlebar_view); 167 } 168 frame->SetShowCloseButton(true); 169 frame->set_can_drag(true); 170 return frame; 171} 172 173const DialogClientView* DialogDelegate::GetDialogClientView() const { 174 return GetWidget()->client_view()->AsDialogClientView(); 175} 176 177DialogClientView* DialogDelegate::GetDialogClientView() { 178 return GetWidget()->client_view()->AsDialogClientView(); 179} 180 181ui::AccessibilityTypes::Role DialogDelegate::GetAccessibleWindowRole() const { 182 return ui::AccessibilityTypes::ROLE_DIALOG; 183} 184 185//////////////////////////////////////////////////////////////////////////////// 186// DialogDelegateView: 187 188DialogDelegateView::DialogDelegateView() { 189 // A WidgetDelegate should be deleted on DeleteDelegate. 190 set_owned_by_client(); 191} 192 193DialogDelegateView::~DialogDelegateView() {} 194 195// static 196Widget* DialogDelegateView::CreateDialogWidget(DialogDelegateView* dialog, 197 gfx::NativeWindow context, 198 gfx::NativeWindow parent) { 199 return CreateDialogWidgetImpl(dialog, context, parent); 200} 201 202void DialogDelegateView::DeleteDelegate() { 203 delete this; 204} 205 206Widget* DialogDelegateView::GetWidget() { 207 return View::GetWidget(); 208} 209 210const Widget* DialogDelegateView::GetWidget() const { 211 return View::GetWidget(); 212} 213 214View* DialogDelegateView::GetContentsView() { 215 return this; 216} 217 218} // namespace views 219