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.
5 #include "chrome/browser/ui/views/javascript_app_modal_dialog_views.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
9 #include "chrome/browser/ui/views/constrained_window_views.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/events/keycodes/keyboard_codes.h"
13 #include "ui/views/controls/message_box_view.h"
14 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/widget/widget.h"
16 #include "ui/views/window/dialog_client_view.h"
18 ////////////////////////////////////////////////////////////////////////////////
19 // JavaScriptAppModalDialogViews, public:
21 JavaScriptAppModalDialogViews::JavaScriptAppModalDialogViews(
22 JavaScriptAppModalDialog
* parent
)
24 int options
= views::MessageBoxView::DETECT_DIRECTIONALITY
;
25 if (parent
->javascript_message_type() ==
26 content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
)
27 options
|= views::MessageBoxView::HAS_PROMPT_FIELD
;
29 views::MessageBoxView::InitParams
params(parent
->message_text());
30 params
.options
= options
;
31 params
.default_prompt
= parent
->default_prompt_text();
32 message_box_view_
= new views::MessageBoxView(params
);
33 DCHECK(message_box_view_
);
35 message_box_view_
->AddAccelerator(
36 ui::Accelerator(ui::VKEY_C
, ui::EF_CONTROL_DOWN
));
37 if (parent
->display_suppress_checkbox()) {
38 message_box_view_
->SetCheckBoxLabel(
39 l10n_util::GetStringUTF16(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION
));
43 JavaScriptAppModalDialogViews::~JavaScriptAppModalDialogViews() {
46 ////////////////////////////////////////////////////////////////////////////////
47 // JavaScriptAppModalDialogViews, NativeAppModalDialog implementation:
49 int JavaScriptAppModalDialogViews::GetAppModalDialogButtons() const {
50 return GetDialogButtons();
53 void JavaScriptAppModalDialogViews::ShowAppModalDialog() {
57 void JavaScriptAppModalDialogViews::ActivateAppModalDialog() {
59 GetWidget()->Activate();
62 void JavaScriptAppModalDialogViews::CloseAppModalDialog() {
66 void JavaScriptAppModalDialogViews::AcceptAppModalDialog() {
67 GetDialogClientView()->AcceptWindow();
70 void JavaScriptAppModalDialogViews::CancelAppModalDialog() {
71 GetDialogClientView()->CancelWindow();
74 //////////////////////////////////////////////////////////////////////////////
75 // JavaScriptAppModalDialogViews, views::DialogDelegate implementation:
77 int JavaScriptAppModalDialogViews::GetDefaultDialogButton() const {
78 return ui::DIALOG_BUTTON_OK
;
81 int JavaScriptAppModalDialogViews::GetDialogButtons() const {
82 if (parent_
->javascript_message_type() ==
83 content::JAVASCRIPT_MESSAGE_TYPE_ALERT
)
84 return ui::DIALOG_BUTTON_OK
;
86 return ui::DIALOG_BUTTON_OK
| ui::DIALOG_BUTTON_CANCEL
;
89 base::string16
JavaScriptAppModalDialogViews::GetWindowTitle() const {
90 return parent_
->title();
93 void JavaScriptAppModalDialogViews::WindowClosing() {
96 void JavaScriptAppModalDialogViews::DeleteDelegate() {
100 bool JavaScriptAppModalDialogViews::Cancel() {
101 parent_
->OnCancel(message_box_view_
->IsCheckBoxSelected());
105 bool JavaScriptAppModalDialogViews::Accept() {
106 parent_
->OnAccept(message_box_view_
->GetInputText(),
107 message_box_view_
->IsCheckBoxSelected());
111 void JavaScriptAppModalDialogViews::OnClosed() {
115 views::Widget
* JavaScriptAppModalDialogViews::GetWidget() {
116 return message_box_view_
->GetWidget();
119 const views::Widget
* JavaScriptAppModalDialogViews::GetWidget() const {
120 return message_box_view_
->GetWidget();
123 base::string16
JavaScriptAppModalDialogViews::GetDialogButtonLabel(
124 ui::DialogButton button
) const {
125 if (parent_
->is_before_unload_dialog()) {
126 if (button
== ui::DIALOG_BUTTON_OK
) {
127 return l10n_util::GetStringUTF16(
128 parent_
->is_reload() ?
129 IDS_BEFORERELOAD_MESSAGEBOX_OK_BUTTON_LABEL
:
130 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL
);
131 } else if (button
== ui::DIALOG_BUTTON_CANCEL
) {
132 return l10n_util::GetStringUTF16(
133 parent_
->is_reload() ?
134 IDS_BEFORERELOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL
:
135 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL
);
138 return DialogDelegate::GetDialogButtonLabel(button
);
141 ///////////////////////////////////////////////////////////////////////////////
142 // JavaScriptAppModalDialogViews, views::WidgetDelegate implementation:
144 ui::ModalType
JavaScriptAppModalDialogViews::GetModalType() const {
145 return ui::MODAL_TYPE_SYSTEM
;
148 views::View
* JavaScriptAppModalDialogViews::GetContentsView() {
149 return message_box_view_
;
152 views::View
* JavaScriptAppModalDialogViews::GetInitiallyFocusedView() {
153 if (message_box_view_
->text_box())
154 return message_box_view_
->text_box();
155 return views::DialogDelegate::GetInitiallyFocusedView();
158 ////////////////////////////////////////////////////////////////////////////////
159 // NativeAppModalDialog, public:
162 NativeAppModalDialog
* NativeAppModalDialog::CreateNativeJavaScriptPrompt(
163 JavaScriptAppModalDialog
* dialog
,
164 gfx::NativeWindow parent_window
) {
165 JavaScriptAppModalDialogViews
* d
= new JavaScriptAppModalDialogViews(dialog
);
166 CreateBrowserModalDialogViews(d
, parent_window
);