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