javascript_app_modal_dialog_views.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "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"
17
18////////////////////////////////////////////////////////////////////////////////
19// JavaScriptAppModalDialogViews, public:
20
21JavaScriptAppModalDialogViews::JavaScriptAppModalDialogViews(
22    JavaScriptAppModalDialog* parent)
23    : parent_(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;
28
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_);
34
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));
40  }
41}
42
43JavaScriptAppModalDialogViews::~JavaScriptAppModalDialogViews() {
44}
45
46////////////////////////////////////////////////////////////////////////////////
47// JavaScriptAppModalDialogViews, NativeAppModalDialog implementation:
48
49int JavaScriptAppModalDialogViews::GetAppModalDialogButtons() const {
50  return GetDialogButtons();
51}
52
53void JavaScriptAppModalDialogViews::ShowAppModalDialog() {
54  GetWidget()->Show();
55}
56
57void JavaScriptAppModalDialogViews::ActivateAppModalDialog() {
58  GetWidget()->Show();
59  GetWidget()->Activate();
60}
61
62void JavaScriptAppModalDialogViews::CloseAppModalDialog() {
63  GetWidget()->Close();
64}
65
66void JavaScriptAppModalDialogViews::AcceptAppModalDialog() {
67  GetDialogClientView()->AcceptWindow();
68}
69
70void JavaScriptAppModalDialogViews::CancelAppModalDialog() {
71  GetDialogClientView()->CancelWindow();
72}
73
74//////////////////////////////////////////////////////////////////////////////
75// JavaScriptAppModalDialogViews, views::DialogDelegate implementation:
76
77int JavaScriptAppModalDialogViews::GetDefaultDialogButton() const {
78  return ui::DIALOG_BUTTON_OK;
79}
80
81int JavaScriptAppModalDialogViews::GetDialogButtons() const {
82  if (parent_->javascript_message_type() ==
83          content::JAVASCRIPT_MESSAGE_TYPE_ALERT)
84    return ui::DIALOG_BUTTON_OK;
85
86  return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
87}
88
89base::string16 JavaScriptAppModalDialogViews::GetWindowTitle() const {
90  return parent_->title();
91}
92
93void JavaScriptAppModalDialogViews::WindowClosing() {
94}
95
96void JavaScriptAppModalDialogViews::DeleteDelegate() {
97  delete this;
98}
99
100bool JavaScriptAppModalDialogViews::Cancel() {
101  parent_->OnCancel(message_box_view_->IsCheckBoxSelected());
102  return true;
103}
104
105bool JavaScriptAppModalDialogViews::Accept() {
106  parent_->OnAccept(message_box_view_->GetInputText(),
107                    message_box_view_->IsCheckBoxSelected());
108  return true;
109}
110
111void JavaScriptAppModalDialogViews::OnClosed() {
112  parent_->OnClose();
113}
114
115views::Widget* JavaScriptAppModalDialogViews::GetWidget() {
116  return message_box_view_->GetWidget();
117}
118
119const views::Widget* JavaScriptAppModalDialogViews::GetWidget() const {
120  return message_box_view_->GetWidget();
121}
122
123base::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);
136    }
137  }
138  return DialogDelegate::GetDialogButtonLabel(button);
139}
140
141///////////////////////////////////////////////////////////////////////////////
142// JavaScriptAppModalDialogViews, views::WidgetDelegate implementation:
143
144ui::ModalType JavaScriptAppModalDialogViews::GetModalType() const {
145  return ui::MODAL_TYPE_SYSTEM;
146}
147
148views::View* JavaScriptAppModalDialogViews::GetContentsView() {
149  return message_box_view_;
150}
151
152views::View* JavaScriptAppModalDialogViews::GetInitiallyFocusedView() {
153  if (message_box_view_->text_box())
154    return message_box_view_->text_box();
155  return views::DialogDelegate::GetInitiallyFocusedView();
156}
157
158////////////////////////////////////////////////////////////////////////////////
159// NativeAppModalDialog, public:
160
161// static
162NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt(
163    JavaScriptAppModalDialog* dialog,
164    gfx::NativeWindow parent_window) {
165  JavaScriptAppModalDialogViews* d = new JavaScriptAppModalDialogViews(dialog);
166  CreateBrowserModalDialogViews(d, parent_window);
167  return d;
168}
169