js_modal_dialog_views.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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/views/js_modal_dialog_views.h" 6 7#include "app/keyboard_codes.h" 8#include "app/l10n_util.h" 9#include "app/message_box_flags.h" 10#include "base/utf_string_conversions.h" 11#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" 12#include "chrome/browser/views/window.h" 13#include "grit/generated_resources.h" 14#include "views/controls/message_box_view.h" 15#include "views/window/window.h" 16 17//////////////////////////////////////////////////////////////////////////////// 18// JSModalDialogViews, public: 19 20JSModalDialogViews::JSModalDialogViews( 21 JavaScriptAppModalDialog* parent) 22 : parent_(parent), 23 message_box_view_(new MessageBoxView( 24 parent->dialog_flags() | MessageBoxFlags::kAutoDetectAlignment, 25 parent->message_text(), parent->default_prompt_text())) { 26 DCHECK(message_box_view_); 27 28 message_box_view_->AddAccelerator( 29 views::Accelerator(app::VKEY_C, false, true, false)); 30 if (parent->display_suppress_checkbox()) { 31 message_box_view_->SetCheckBoxLabel(UTF16ToWide( 32 l10n_util::GetStringUTF16(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION))); 33 } 34} 35 36JSModalDialogViews::~JSModalDialogViews() { 37} 38 39//////////////////////////////////////////////////////////////////////////////// 40// JSModalDialogViews, NativeAppModalDialog implementation: 41 42int JSModalDialogViews::GetAppModalDialogButtons() const { 43 return GetDialogButtons(); 44} 45 46void JSModalDialogViews::ShowAppModalDialog() { 47 window()->Show(); 48} 49 50void JSModalDialogViews::ActivateAppModalDialog() { 51 window()->Show(); 52 window()->Activate(); 53} 54 55void JSModalDialogViews::CloseAppModalDialog() { 56 window()->Close(); 57} 58 59void JSModalDialogViews::AcceptAppModalDialog() { 60 GetDialogClientView()->AcceptWindow(); 61} 62 63void JSModalDialogViews::CancelAppModalDialog() { 64 GetDialogClientView()->CancelWindow(); 65} 66 67////////////////////////////////////////////////////////////////////////////// 68// JSModalDialogViews, views::DialogDelegate implementation: 69 70int JSModalDialogViews::GetDefaultDialogButton() const { 71 if (parent_->dialog_flags() & MessageBoxFlags::kFlagHasOKButton) 72 return MessageBoxFlags::DIALOGBUTTON_OK; 73 74 if (parent_->dialog_flags() & MessageBoxFlags::kFlagHasCancelButton) 75 return MessageBoxFlags::DIALOGBUTTON_CANCEL; 76 77 return MessageBoxFlags::DIALOGBUTTON_NONE; 78} 79 80int JSModalDialogViews::GetDialogButtons() const { 81 int dialog_buttons = 0; 82 if (parent_->dialog_flags() & MessageBoxFlags::kFlagHasOKButton) 83 dialog_buttons = MessageBoxFlags::DIALOGBUTTON_OK; 84 85 if (parent_->dialog_flags() & MessageBoxFlags::kFlagHasCancelButton) 86 dialog_buttons |= MessageBoxFlags::DIALOGBUTTON_CANCEL; 87 88 return dialog_buttons; 89} 90 91std::wstring JSModalDialogViews::GetWindowTitle() const { 92 return parent_->title(); 93} 94 95 96void JSModalDialogViews::WindowClosing() { 97} 98 99void JSModalDialogViews::DeleteDelegate() { 100 delete parent_; 101 delete this; 102} 103 104bool JSModalDialogViews::Cancel() { 105 parent_->OnCancel(message_box_view_->IsCheckBoxSelected()); 106 return true; 107} 108 109bool JSModalDialogViews::Accept() { 110 parent_->OnAccept(message_box_view_->GetInputText(), 111 message_box_view_->IsCheckBoxSelected()); 112 return true; 113} 114 115void JSModalDialogViews::OnClose() { 116 parent_->OnClose(); 117} 118 119std::wstring JSModalDialogViews::GetDialogButtonLabel( 120 MessageBoxFlags::DialogButton button) const { 121 if (parent_->is_before_unload_dialog()) { 122 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { 123 return UTF16ToWide(l10n_util::GetStringUTF16( 124 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL)); 125 } else if (button == MessageBoxFlags::DIALOGBUTTON_CANCEL) { 126 return UTF16ToWide(l10n_util::GetStringUTF16( 127 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL)); 128 } 129 } 130 return DialogDelegate::GetDialogButtonLabel(button); 131} 132 133/////////////////////////////////////////////////////////////////////////////// 134// JSModalDialogViews, views::WindowDelegate implementation: 135 136views::View* JSModalDialogViews::GetContentsView() { 137 return message_box_view_; 138} 139 140views::View* JSModalDialogViews::GetInitiallyFocusedView() { 141 if (message_box_view_->text_box()) 142 return message_box_view_->text_box(); 143 return views::DialogDelegate::GetInitiallyFocusedView(); 144} 145 146//////////////////////////////////////////////////////////////////////////////// 147// NativeAppModalDialog, public: 148 149// static 150NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( 151 JavaScriptAppModalDialog* dialog, 152 gfx::NativeWindow parent_window) { 153 JSModalDialogViews* d = new JSModalDialogViews(dialog); 154 155 browser::CreateViewsWindow(parent_window, gfx::Rect(), d); 156 return d; 157} 158