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