javascript_app_modal_dialog_views.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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 "chrome/browser/ui/views/javascript_app_modal_dialog_views.h" 6 7#include "base/strings/utf_string_conversions.h" 8#include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" 9#include "grit/generated_resources.h" 10#include "ui/base/keycodes/keyboard_codes.h" 11#include "ui/base/l10n/l10n_util.h" 12#include "ui/views/controls/message_box_view.h" 13#include "ui/views/controls/textfield/textfield.h" 14#include "ui/views/widget/widget.h" 15#include "ui/views/window/dialog_client_view.h" 16 17//////////////////////////////////////////////////////////////////////////////// 18// JavaScriptAppModalDialogViews, public: 19 20JavaScriptAppModalDialogViews::JavaScriptAppModalDialogViews( 21 JavaScriptAppModalDialog* parent) 22 : parent_(parent) { 23 int options = views::MessageBoxView::DETECT_DIRECTIONALITY; 24 if (parent->javascript_message_type() == 25 content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) 26 options |= views::MessageBoxView::HAS_PROMPT_FIELD; 27 28 views::MessageBoxView::InitParams params(parent->message_text()); 29 params.options = options; 30 params.default_prompt = parent->default_prompt_text(); 31 message_box_view_ = new views::MessageBoxView(params); 32 DCHECK(message_box_view_); 33 34 message_box_view_->AddAccelerator( 35 ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); 36 if (parent->display_suppress_checkbox()) { 37 message_box_view_->SetCheckBoxLabel( 38 l10n_util::GetStringUTF16(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION)); 39 } 40} 41 42JavaScriptAppModalDialogViews::~JavaScriptAppModalDialogViews() { 43} 44 45//////////////////////////////////////////////////////////////////////////////// 46// JavaScriptAppModalDialogViews, NativeAppModalDialog implementation: 47 48int JavaScriptAppModalDialogViews::GetAppModalDialogButtons() const { 49 return GetDialogButtons(); 50} 51 52void JavaScriptAppModalDialogViews::ShowAppModalDialog() { 53 GetWidget()->Show(); 54} 55 56void JavaScriptAppModalDialogViews::ActivateAppModalDialog() { 57 GetWidget()->Show(); 58 GetWidget()->Activate(); 59} 60 61void JavaScriptAppModalDialogViews::CloseAppModalDialog() { 62 GetWidget()->Close(); 63} 64 65void JavaScriptAppModalDialogViews::AcceptAppModalDialog() { 66 GetDialogClientView()->AcceptWindow(); 67} 68 69void JavaScriptAppModalDialogViews::CancelAppModalDialog() { 70 GetDialogClientView()->CancelWindow(); 71} 72 73////////////////////////////////////////////////////////////////////////////// 74// JavaScriptAppModalDialogViews, views::DialogDelegate implementation: 75 76int JavaScriptAppModalDialogViews::GetDefaultDialogButton() const { 77 return ui::DIALOG_BUTTON_OK; 78} 79 80int JavaScriptAppModalDialogViews::GetDialogButtons() const { 81 if (parent_->javascript_message_type() == 82 content::JAVASCRIPT_MESSAGE_TYPE_ALERT) 83 return ui::DIALOG_BUTTON_OK; 84 85 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; 86} 87 88string16 JavaScriptAppModalDialogViews::GetWindowTitle() const { 89 return parent_->title(); 90} 91 92void JavaScriptAppModalDialogViews::WindowClosing() { 93} 94 95void JavaScriptAppModalDialogViews::DeleteDelegate() { 96 delete this; 97} 98 99bool JavaScriptAppModalDialogViews::Cancel() { 100 parent_->OnCancel(message_box_view_->IsCheckBoxSelected()); 101 return true; 102} 103 104bool JavaScriptAppModalDialogViews::Accept() { 105 parent_->OnAccept(message_box_view_->GetInputText(), 106 message_box_view_->IsCheckBoxSelected()); 107 return true; 108} 109 110void JavaScriptAppModalDialogViews::OnClose() { 111 parent_->OnClose(); 112} 113 114views::Widget* JavaScriptAppModalDialogViews::GetWidget() { 115 return message_box_view_->GetWidget(); 116} 117 118const views::Widget* JavaScriptAppModalDialogViews::GetWidget() const { 119 return message_box_view_->GetWidget(); 120} 121 122string16 JavaScriptAppModalDialogViews::GetDialogButtonLabel( 123 ui::DialogButton button) const { 124 if (parent_->is_before_unload_dialog()) { 125 if (button == ui::DIALOG_BUTTON_OK) { 126 return l10n_util::GetStringUTF16( 127 parent_->is_reload() ? 128 IDS_BEFORERELOAD_MESSAGEBOX_OK_BUTTON_LABEL : 129 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); 130 } else if (button == ui::DIALOG_BUTTON_CANCEL) { 131 return l10n_util::GetStringUTF16( 132 parent_->is_reload() ? 133 IDS_BEFORERELOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL : 134 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); 135 } 136 } 137 return DialogDelegate::GetDialogButtonLabel(button); 138} 139 140/////////////////////////////////////////////////////////////////////////////// 141// JavaScriptAppModalDialogViews, views::WidgetDelegate implementation: 142 143ui::ModalType JavaScriptAppModalDialogViews::GetModalType() const { 144 return ui::MODAL_TYPE_SYSTEM; 145} 146 147views::View* JavaScriptAppModalDialogViews::GetContentsView() { 148 return message_box_view_; 149} 150 151views::View* JavaScriptAppModalDialogViews::GetInitiallyFocusedView() { 152 if (message_box_view_->text_box()) 153 return message_box_view_->text_box(); 154 return views::DialogDelegate::GetInitiallyFocusedView(); 155} 156 157//////////////////////////////////////////////////////////////////////////////// 158// NativeAppModalDialog, public: 159 160// static 161NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( 162 JavaScriptAppModalDialog* dialog, 163 gfx::NativeWindow parent_window) { 164 JavaScriptAppModalDialogViews* d = new JavaScriptAppModalDialogViews(dialog); 165 views::Widget* widget = 166 views::DialogDelegate::CreateDialogWidget(d, NULL, parent_window); 167 views::Widget* parent_widget = parent_window ? 168 views::Widget::GetWidgetForNativeWindow(parent_window) : NULL; 169 // Horizontally center the dialog window within the parent window's bounds. 170 if (widget && parent_widget) { 171 gfx::Rect bounds(widget->GetWindowBoundsInScreen()); 172 gfx::Rect parent_bounds(parent_widget->GetWindowBoundsInScreen()); 173 bounds.set_x(parent_bounds.CenterPoint().x() - bounds.width() / 2); 174 widget->SetBounds(bounds); 175 } 176 return d; 177} 178