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 "components/app_modal/views/javascript_app_modal_dialog_views.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/app_modal/javascript_app_modal_dialog.h"
9 #include "components/constrained_window/constrained_window_views.h"
10 #include "grit/components_strings.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"
20 ////////////////////////////////////////////////////////////////////////////////
21 // JavaScriptAppModalDialogViews, public:
23 JavaScriptAppModalDialogViews::JavaScriptAppModalDialogViews(
24 JavaScriptAppModalDialog
* parent
)
26 int options
= views::MessageBoxView::DETECT_DIRECTIONALITY
;
27 if (parent
->javascript_message_type() ==
28 content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
)
29 options
|= views::MessageBoxView::HAS_PROMPT_FIELD
;
31 views::MessageBoxView::InitParams
params(parent
->message_text());
32 params
.options
= options
;
33 params
.default_prompt
= parent
->default_prompt_text();
34 message_box_view_
= new views::MessageBoxView(params
);
35 DCHECK(message_box_view_
);
37 message_box_view_
->AddAccelerator(
38 ui::Accelerator(ui::VKEY_C
, ui::EF_CONTROL_DOWN
));
39 if (parent
->display_suppress_checkbox()) {
40 message_box_view_
->SetCheckBoxLabel(
41 l10n_util::GetStringUTF16(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION
));
45 JavaScriptAppModalDialogViews::~JavaScriptAppModalDialogViews() {
48 ////////////////////////////////////////////////////////////////////////////////
49 // JavaScriptAppModalDialogViews, NativeAppModalDialog implementation:
51 int JavaScriptAppModalDialogViews::GetAppModalDialogButtons() const {
52 return GetDialogButtons();
55 void JavaScriptAppModalDialogViews::ShowAppModalDialog() {
59 void JavaScriptAppModalDialogViews::ActivateAppModalDialog() {
61 GetWidget()->Activate();
64 void JavaScriptAppModalDialogViews::CloseAppModalDialog() {
68 void JavaScriptAppModalDialogViews::AcceptAppModalDialog() {
69 GetDialogClientView()->AcceptWindow();
72 void JavaScriptAppModalDialogViews::CancelAppModalDialog() {
73 GetDialogClientView()->CancelWindow();
76 bool JavaScriptAppModalDialogViews::IsShowing() const {
77 return GetWidget()->IsVisible();
80 //////////////////////////////////////////////////////////////////////////////
81 // JavaScriptAppModalDialogViews, views::DialogDelegate implementation:
83 int JavaScriptAppModalDialogViews::GetDefaultDialogButton() const {
84 return ui::DIALOG_BUTTON_OK
;
87 int JavaScriptAppModalDialogViews::GetDialogButtons() const {
88 if (parent_
->javascript_message_type() ==
89 content::JAVASCRIPT_MESSAGE_TYPE_ALERT
)
90 return ui::DIALOG_BUTTON_OK
;
92 return ui::DIALOG_BUTTON_OK
| ui::DIALOG_BUTTON_CANCEL
;
95 base::string16
JavaScriptAppModalDialogViews::GetWindowTitle() const {
96 return parent_
->title();
99 void JavaScriptAppModalDialogViews::DeleteDelegate() {
103 bool JavaScriptAppModalDialogViews::Cancel() {
104 parent_
->OnCancel(message_box_view_
->IsCheckBoxSelected());
108 bool JavaScriptAppModalDialogViews::Accept() {
109 parent_
->OnAccept(message_box_view_
->GetInputText(),
110 message_box_view_
->IsCheckBoxSelected());
114 void JavaScriptAppModalDialogViews::OnClosed() {
118 views::Widget
* JavaScriptAppModalDialogViews::GetWidget() {
119 return message_box_view_
->GetWidget();
122 const views::Widget
* JavaScriptAppModalDialogViews::GetWidget() const {
123 return message_box_view_
->GetWidget();
126 base::string16
JavaScriptAppModalDialogViews::GetDialogButtonLabel(
127 ui::DialogButton button
) const {
128 if (parent_
->is_before_unload_dialog()) {
129 if (button
== ui::DIALOG_BUTTON_OK
) {
130 return l10n_util::GetStringUTF16(
131 parent_
->is_reload() ?
132 IDS_BEFORERELOAD_MESSAGEBOX_OK_BUTTON_LABEL
:
133 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL
);
134 } else if (button
== ui::DIALOG_BUTTON_CANCEL
) {
135 return l10n_util::GetStringUTF16(
136 parent_
->is_reload() ?
137 IDS_BEFORERELOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL
:
138 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL
);
141 return DialogDelegate::GetDialogButtonLabel(button
);
144 ///////////////////////////////////////////////////////////////////////////////
145 // JavaScriptAppModalDialogViews, views::WidgetDelegate implementation:
147 ui::ModalType
JavaScriptAppModalDialogViews::GetModalType() const {
148 return ui::MODAL_TYPE_SYSTEM
;
151 views::View
* JavaScriptAppModalDialogViews::GetContentsView() {
152 return message_box_view_
;
155 views::View
* JavaScriptAppModalDialogViews::GetInitiallyFocusedView() {
156 if (message_box_view_
->text_box())
157 return message_box_view_
->text_box();
158 return views::DialogDelegate::GetInitiallyFocusedView();
161 } // namespace app_modal