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/base/ui_base_switches_util.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/controls/textfield/textfield.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 23 namespace views { 24 25 //////////////////////////////////////////////////////////////////////////////// 26 // DialogDelegate: 27 28 DialogDelegate::~DialogDelegate() { 29 } 30 31 // static 32 bool DialogDelegate::UseNewStyle() { 33 return switches::IsNewDialogStyleEnabled() && 34 Textfield::IsViewsTextfieldEnabled(); 35 } 36 37 // static 38 Widget* DialogDelegate::CreateDialogWidget(DialogDelegate* dialog, 39 gfx::NativeWindow context, 40 gfx::NativeWindow parent) { 41 views::Widget* widget = new views::Widget; 42 views::Widget::InitParams params; 43 params.delegate = dialog; 44 const bool use_new_style = dialog ? 45 dialog->UseNewStyleForThisDialog() : DialogDelegate::UseNewStyle(); 46 if (use_new_style) { 47 // Note: Transparent widgets cannot host native Windows textfield controls. 48 params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW; 49 params.remove_standard_frame = true; 50 } 51 params.context = context; 52 params.parent = parent; 53 params.top_level = true; 54 widget->Init(params); 55 if (use_new_style) { 56 #if defined(USE_AURA) 57 // TODO(msw): Add a matching shadow type and remove the bubble frame border? 58 corewm::SetShadowType(widget->GetNativeWindow(), corewm::SHADOW_TYPE_NONE); 59 #endif 60 } 61 return widget; 62 } 63 64 View* DialogDelegate::CreateExtraView() { 65 return NULL; 66 } 67 68 View* DialogDelegate::CreateTitlebarExtraView() { 69 return NULL; 70 } 71 72 View* DialogDelegate::CreateFootnoteView() { 73 return NULL; 74 } 75 76 bool DialogDelegate::Cancel() { 77 return true; 78 } 79 80 bool DialogDelegate::Accept(bool window_closing) { 81 return Accept(); 82 } 83 84 bool DialogDelegate::Accept() { 85 return true; 86 } 87 88 bool DialogDelegate::Close() { 89 int buttons = GetDialogButtons(); 90 if ((buttons & ui::DIALOG_BUTTON_CANCEL) || 91 (buttons == ui::DIALOG_BUTTON_NONE)) { 92 return Cancel(); 93 } 94 return Accept(true); 95 } 96 97 base::string16 DialogDelegate::GetDialogLabel() const { 98 return base::string16(); 99 } 100 101 base::string16 DialogDelegate::GetDialogTitle() const { 102 return GetWindowTitle(); 103 } 104 105 int DialogDelegate::GetDialogButtons() const { 106 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; 107 } 108 109 int DialogDelegate::GetDefaultDialogButton() const { 110 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK) 111 return ui::DIALOG_BUTTON_OK; 112 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL) 113 return ui::DIALOG_BUTTON_CANCEL; 114 return ui::DIALOG_BUTTON_NONE; 115 } 116 117 bool DialogDelegate::ShouldDefaultButtonBeBlue() const { 118 return false; 119 } 120 121 base::string16 DialogDelegate::GetDialogButtonLabel( 122 ui::DialogButton button) const { 123 if (button == ui::DIALOG_BUTTON_OK) 124 return l10n_util::GetStringUTF16(IDS_APP_OK); 125 if (button == ui::DIALOG_BUTTON_CANCEL) { 126 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK) 127 return l10n_util::GetStringUTF16(IDS_APP_CANCEL); 128 return l10n_util::GetStringUTF16(IDS_APP_CLOSE); 129 } 130 NOTREACHED(); 131 return base::string16(); 132 } 133 134 bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const { 135 return true; 136 } 137 138 View* DialogDelegate::GetInitiallyFocusedView() { 139 // Focus the default button if any. 140 const DialogClientView* dcv = GetDialogClientView(); 141 int default_button = GetDefaultDialogButton(); 142 if (default_button == ui::DIALOG_BUTTON_NONE) 143 return NULL; 144 145 if ((default_button & GetDialogButtons()) == 0) { 146 // The default button is a button we don't have. 147 NOTREACHED(); 148 return NULL; 149 } 150 151 if (default_button & ui::DIALOG_BUTTON_OK) 152 return dcv->ok_button(); 153 if (default_button & ui::DIALOG_BUTTON_CANCEL) 154 return dcv->cancel_button(); 155 return NULL; 156 } 157 158 DialogDelegate* DialogDelegate::AsDialogDelegate() { 159 return this; 160 } 161 162 ClientView* DialogDelegate::CreateClientView(Widget* widget) { 163 return new DialogClientView(widget, GetContentsView()); 164 } 165 166 NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) { 167 if (UseNewStyleForThisDialog()) 168 return CreateNewStyleFrameView(widget); 169 return WidgetDelegate::CreateNonClientFrameView(widget); 170 } 171 172 // static 173 NonClientFrameView* DialogDelegate::CreateNewStyleFrameView(Widget* widget) { 174 return CreateNewStyleFrameView(widget, false); 175 } 176 177 // static 178 NonClientFrameView* DialogDelegate::CreateNewStyleFrameView( 179 Widget* widget, 180 bool force_opaque_border) { 181 BubbleFrameView* frame = new BubbleFrameView(gfx::Insets()); 182 const SkColor color = widget->GetNativeTheme()->GetSystemColor( 183 ui::NativeTheme::kColorId_DialogBackground); 184 if (force_opaque_border) { 185 frame->SetBubbleBorder(new BubbleBorder( 186 BubbleBorder::NONE, 187 BubbleBorder::NO_SHADOW_OPAQUE_BORDER, 188 color)); 189 } else { 190 frame->SetBubbleBorder(new BubbleBorder(BubbleBorder::FLOAT, 191 BubbleBorder::SMALL_SHADOW, 192 color)); 193 } 194 DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate(); 195 if (delegate) { 196 View* titlebar_view = delegate->CreateTitlebarExtraView(); 197 if (titlebar_view) 198 frame->SetTitlebarExtraView(titlebar_view); 199 } 200 if (force_opaque_border) 201 widget->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); 202 return frame; 203 } 204 205 bool DialogDelegate::UseNewStyleForThisDialog() const { 206 return UseNewStyle(); 207 } 208 209 const DialogClientView* DialogDelegate::GetDialogClientView() const { 210 return GetWidget()->client_view()->AsDialogClientView(); 211 } 212 213 DialogClientView* DialogDelegate::GetDialogClientView() { 214 return GetWidget()->client_view()->AsDialogClientView(); 215 } 216 217 ui::AccessibilityTypes::Role DialogDelegate::GetAccessibleWindowRole() const { 218 return ui::AccessibilityTypes::ROLE_DIALOG; 219 } 220 221 //////////////////////////////////////////////////////////////////////////////// 222 // DialogDelegateView: 223 224 DialogDelegateView::DialogDelegateView() { 225 // A WidgetDelegate should be deleted on DeleteDelegate. 226 set_owned_by_client(); 227 } 228 229 DialogDelegateView::~DialogDelegateView() {} 230 231 void DialogDelegateView::DeleteDelegate() { 232 delete this; 233 } 234 235 Widget* DialogDelegateView::GetWidget() { 236 return View::GetWidget(); 237 } 238 239 const Widget* DialogDelegateView::GetWidget() const { 240 return View::GetWidget(); 241 } 242 243 View* DialogDelegateView::GetContentsView() { 244 return this; 245 } 246 247 } // namespace views 248