dialog_delegate.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 17namespace views { 18 19//////////////////////////////////////////////////////////////////////////////// 20// DialogDelegate: 21 22DialogDelegate::~DialogDelegate() { 23} 24 25// static 26Widget* DialogDelegate::CreateDialogWidget(DialogDelegate* dialog, 27 gfx::NativeView context, 28 gfx::NativeView parent) { 29 views::Widget* widget = new views::Widget; 30 views::Widget::InitParams params; 31 params.delegate = dialog; 32 if (!dialog || dialog->UseNewStyleForThisDialog()) { 33 params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW; 34 params.remove_standard_frame = true; 35 } 36 params.context = context; 37 params.parent = parent; 38 // TODO(msw): Add a matching shadow type and remove the bubble frame border? 39 params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE; 40 widget->Init(params); 41 return widget; 42} 43 44View* DialogDelegate::CreateExtraView() { 45 return NULL; 46} 47 48View* DialogDelegate::CreateTitlebarExtraView() { 49 return NULL; 50} 51 52View* DialogDelegate::CreateFootnoteView() { 53 return NULL; 54} 55 56bool DialogDelegate::Cancel() { 57 return true; 58} 59 60bool DialogDelegate::Accept(bool window_closing) { 61 return Accept(); 62} 63 64bool DialogDelegate::Accept() { 65 return true; 66} 67 68bool DialogDelegate::Close() { 69 int buttons = GetDialogButtons(); 70 if ((buttons & ui::DIALOG_BUTTON_CANCEL) || 71 (buttons == ui::DIALOG_BUTTON_NONE)) { 72 return Cancel(); 73 } 74 return Accept(true); 75} 76 77base::string16 DialogDelegate::GetDialogLabel() const { 78 return base::string16(); 79} 80 81base::string16 DialogDelegate::GetDialogTitle() const { 82 return GetWindowTitle(); 83} 84 85int DialogDelegate::GetDialogButtons() const { 86 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; 87} 88 89int DialogDelegate::GetDefaultDialogButton() const { 90 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK) 91 return ui::DIALOG_BUTTON_OK; 92 if (GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL) 93 return ui::DIALOG_BUTTON_CANCEL; 94 return ui::DIALOG_BUTTON_NONE; 95} 96 97bool DialogDelegate::ShouldDefaultButtonBeBlue() const { 98 return false; 99} 100 101base::string16 DialogDelegate::GetDialogButtonLabel( 102 ui::DialogButton button) const { 103 if (button == ui::DIALOG_BUTTON_OK) 104 return l10n_util::GetStringUTF16(IDS_APP_OK); 105 if (button == ui::DIALOG_BUTTON_CANCEL) { 106 if (GetDialogButtons() & ui::DIALOG_BUTTON_OK) 107 return l10n_util::GetStringUTF16(IDS_APP_CANCEL); 108 return l10n_util::GetStringUTF16(IDS_APP_CLOSE); 109 } 110 NOTREACHED(); 111 return base::string16(); 112} 113 114bool DialogDelegate::IsDialogButtonEnabled(ui::DialogButton button) const { 115 return true; 116} 117 118View* DialogDelegate::GetInitiallyFocusedView() { 119 // Focus the default button if any. 120 const DialogClientView* dcv = GetDialogClientView(); 121 int default_button = GetDefaultDialogButton(); 122 if (default_button == ui::DIALOG_BUTTON_NONE) 123 return NULL; 124 125 if ((default_button & GetDialogButtons()) == 0) { 126 // The default button is a button we don't have. 127 NOTREACHED(); 128 return NULL; 129 } 130 131 if (default_button & ui::DIALOG_BUTTON_OK) 132 return dcv->ok_button(); 133 if (default_button & ui::DIALOG_BUTTON_CANCEL) 134 return dcv->cancel_button(); 135 return NULL; 136} 137 138DialogDelegate* DialogDelegate::AsDialogDelegate() { 139 return this; 140} 141 142ClientView* DialogDelegate::CreateClientView(Widget* widget) { 143 return new DialogClientView(widget, GetContentsView()); 144} 145 146NonClientFrameView* DialogDelegate::CreateNonClientFrameView(Widget* widget) { 147 if (UseNewStyleForThisDialog()) 148 return CreateDialogFrameView(widget); 149 return WidgetDelegate::CreateNonClientFrameView(widget); 150} 151 152// static 153NonClientFrameView* DialogDelegate::CreateDialogFrameView(Widget* widget) { 154 BubbleFrameView* frame = new BubbleFrameView(gfx::Insets()); 155 scoped_ptr<BubbleBorder> border(new BubbleBorder( 156 BubbleBorder::FLOAT, BubbleBorder::SMALL_SHADOW, SK_ColorRED)); 157 border->set_use_theme_background_color(true); 158 frame->SetBubbleBorder(border.Pass()); 159 DialogDelegate* delegate = widget->widget_delegate()->AsDialogDelegate(); 160 if (delegate) { 161 View* titlebar_view = delegate->CreateTitlebarExtraView(); 162 if (titlebar_view) 163 frame->SetTitlebarExtraView(titlebar_view); 164 } 165 return frame; 166} 167 168bool DialogDelegate::UseNewStyleForThisDialog() const { 169 return true; 170} 171 172const DialogClientView* DialogDelegate::GetDialogClientView() const { 173 return GetWidget()->client_view()->AsDialogClientView(); 174} 175 176DialogClientView* DialogDelegate::GetDialogClientView() { 177 return GetWidget()->client_view()->AsDialogClientView(); 178} 179 180ui::AXRole DialogDelegate::GetAccessibleWindowRole() const { 181 return ui::AX_ROLE_DIALOG; 182} 183 184//////////////////////////////////////////////////////////////////////////////// 185// DialogDelegateView: 186 187DialogDelegateView::DialogDelegateView() { 188 // A WidgetDelegate should be deleted on DeleteDelegate. 189 set_owned_by_client(); 190} 191 192DialogDelegateView::~DialogDelegateView() {} 193 194void DialogDelegateView::DeleteDelegate() { 195 delete this; 196} 197 198Widget* DialogDelegateView::GetWidget() { 199 return View::GetWidget(); 200} 201 202const Widget* DialogDelegateView::GetWidget() const { 203 return View::GetWidget(); 204} 205 206View* DialogDelegateView::GetContentsView() { 207 return this; 208} 209 210} // namespace views 211