diff options
author | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-07 20:18:28 +0000 |
---|---|---|
committer | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-07 20:18:28 +0000 |
commit | 0bfa713f7ae0bd154e7a0246413561de64d8c84d (patch) | |
tree | 1eb0198e7a91815be2a8cc7007b53b11e241e6fe /chrome/browser/views | |
parent | 70771b35915b066928ac79c8a5ba4b950e4d74ec (diff) | |
download | chromium_src-0bfa713f7ae0bd154e7a0246413561de64d8c84d.zip chromium_src-0bfa713f7ae0bd154e7a0246413561de64d8c84d.tar.gz chromium_src-0bfa713f7ae0bd154e7a0246413561de64d8c84d.tar.bz2 |
Refactor AppModalDialogQueue and move JS Alert boxes into a MVC.
JavascriptMessageBoxHandler (handles alert, confirm, prompt, and onbeforeunload) was a views class. This change converts it into an MVC so we can port to linux/mac.
AppModalDialog is the model+controller, JavascriptMessageBoxDialog is the windows specific view.
The onbeforeunload dialog (JavascriptBeforeUnloadHandler) was a subclass of JavascriptMessageBoxHandler that had a different title and button text. I merged this class into JavascriptMessageBoxHandler by passing a bool to handle the custom button text.
Review URL: http://codereview.chromium.org/63033
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13276 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/views')
-rw-r--r-- | chrome/browser/views/browser_views.vcproj | 8 | ||||
-rw-r--r-- | chrome/browser/views/jsmessage_box_dialog.cc | 122 | ||||
-rw-r--r-- | chrome/browser/views/jsmessage_box_dialog.h | 64 |
3 files changed, 194 insertions, 0 deletions
diff --git a/chrome/browser/views/browser_views.vcproj b/chrome/browser/views/browser_views.vcproj index 9cde61c..0e433e9 100644 --- a/chrome/browser/views/browser_views.vcproj +++ b/chrome/browser/views/browser_views.vcproj @@ -634,6 +634,14 @@ > </File> <File + RelativePath=".\jsmessage_box_dialog.cc" + > + </File> + <File + RelativePath=".\jsmessage_box_dialog.h" + > + </File> + <File RelativePath=".\keyword_editor_view.cc" > </File> diff --git a/chrome/browser/views/jsmessage_box_dialog.cc b/chrome/browser/views/jsmessage_box_dialog.cc new file mode 100644 index 0000000..bd0c7f2 --- /dev/null +++ b/chrome/browser/views/jsmessage_box_dialog.cc @@ -0,0 +1,122 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/views/jsmessage_box_dialog.h" + +#include "chrome/browser/app_modal_dialog.h" +#include "chrome/browser/tab_contents/web_contents.h" +#include "chrome/common/l10n_util.h" +#include "chrome/common/message_box_flags.h" +#include "chrome/views/controls/message_box_view.h" +#include "chrome/views/window/window.h" +#include "grit/generated_resources.h" + +JavascriptMessageBoxDialog::JavascriptMessageBoxDialog( + AppModalDialog* parent, + const std::wstring& message_text, + const std::wstring& default_prompt_text, + bool display_suppress_checkbox) + : parent_(parent), + dialog_(NULL), + message_box_view_(new MessageBoxView( + parent->dialog_flags() | MessageBox::kAutoDetectAlignment, + message_text, default_prompt_text)) { + DCHECK(message_box_view_); + + if (display_suppress_checkbox) { + message_box_view_->SetCheckBoxLabel( + l10n_util::GetString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION)); + } +} + +JavascriptMessageBoxDialog::~JavascriptMessageBoxDialog() { +} + +void JavascriptMessageBoxDialog::ShowModalDialog() { + HWND root_hwnd = GetAncestor(web_contents()->GetNativeView(), + GA_ROOT); + dialog_ = views::Window::CreateChromeWindow(root_hwnd, gfx::Rect(), this); + dialog_->Show(); +} + +void JavascriptMessageBoxDialog::ActivateModalDialog() { + // Ensure that the dialog is visible and at the top of the z-order. These + // conditions may not be true if the dialog was opened on a different virtual + // desktop to the one the browser window is on. + dialog_->Show(); + dialog_->Activate(); +} + +void JavascriptMessageBoxDialog::CloseModalDialog() { + // If the dialog is visible close it. + if (dialog_) + dialog_->Close(); +} + +////////////////////////////////////////////////////////////////////////////// +// JavascriptMessageBoxDialog, views::DialogDelegate implementation: + +int JavascriptMessageBoxDialog::GetDialogButtons() const { + int dialog_buttons = 0; + if (parent_->dialog_flags() & MessageBox::kFlagHasOKButton) + dialog_buttons = DIALOGBUTTON_OK; + + if (parent_->dialog_flags() & MessageBox::kFlagHasCancelButton) + dialog_buttons |= DIALOGBUTTON_CANCEL; + + return dialog_buttons; +} + +std::wstring JavascriptMessageBoxDialog::GetWindowTitle() const { + return parent_->title();; +} + + +void JavascriptMessageBoxDialog::WindowClosing() { + dialog_ = NULL; + +} + +void JavascriptMessageBoxDialog::DeleteDelegate() { + delete parent_; + delete this; +} + +bool JavascriptMessageBoxDialog::Cancel() { + parent_->OnCancel(); + return true; +} + +bool JavascriptMessageBoxDialog::Accept() { + + parent_->OnAccept(message_box_view_->GetInputText(), + message_box_view_->IsCheckBoxSelected()); + return true; +} + +std::wstring JavascriptMessageBoxDialog::GetDialogButtonLabel( + DialogButton button) const { + if (parent_->is_before_unload_dialog()) { + if (button == DialogDelegate::DIALOGBUTTON_OK) { + return l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); + } else if (button == DialogDelegate::DIALOGBUTTON_CANCEL) { + return l10n_util::GetString( + IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); + } + } + return DialogDelegate::GetDialogButtonLabel(button); +} + +/////////////////////////////////////////////////////////////////////////////// +// JavascriptMessageBoxDialog, views::WindowDelegate implementation: + +views::View* JavascriptMessageBoxDialog::GetContentsView() { + return message_box_view_; +} + +views::View* JavascriptMessageBoxDialog::GetInitiallyFocusedView() { + if (message_box_view_->text_box()) + return message_box_view_->text_box(); + return views::DialogDelegate::GetInitiallyFocusedView(); +} diff --git a/chrome/browser/views/jsmessage_box_dialog.h b/chrome/browser/views/jsmessage_box_dialog.h new file mode 100644 index 0000000..7195904 --- /dev/null +++ b/chrome/browser/views/jsmessage_box_dialog.h @@ -0,0 +1,64 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_VIEWS_JSMESSAGE_BOX_DIALOG_H_ +#define CHROME_BROWSER_VIEWS_JSMESSAGE_BOX_DIALOG_H_ + +#include <string> + +#include "chrome/browser/app_modal_dialog.h" +#include "chrome/views/window/dialog_delegate.h" + +class MessageBoxView; +class WebContents; +namespace views { +class Window; +} + +class JavascriptMessageBoxDialog : public views::DialogDelegate { + public: + JavascriptMessageBoxDialog(AppModalDialog* parent, + const std::wstring& message_text, + const std::wstring& default_prompt_text, + bool display_suppress_checkbox); + + virtual ~JavascriptMessageBoxDialog(); + + // Methods called from AppModalDialog. + void ShowModalDialog(); + void ActivateModalDialog(); + void CloseModalDialog(); + + // views::DialogDelegate Methods: + virtual int GetDialogButtons() const; + virtual std::wstring GetWindowTitle() const; + virtual void WindowClosing(); + virtual void DeleteDelegate(); + virtual bool Cancel(); + virtual bool Accept(); + virtual std::wstring GetDialogButtonLabel(DialogButton button) const; + + // views::WindowDelegate Methods: + virtual bool IsModal() const { return true; } + virtual views::View* GetContentsView(); + virtual views::View* GetInitiallyFocusedView(); + + private: + WebContents* web_contents() { + return parent_->web_contents(); + } + + // A pointer to the AppModalDialog that owns us. + AppModalDialog* parent_; + + // The message box view whose commands we handle. + MessageBoxView* message_box_view_; + + // The dialog if it is currently visible. + views::Window* dialog_; + + DISALLOW_COPY_AND_ASSIGN(JavascriptMessageBoxDialog); +}; + +#endif // CHROME_BROWSER_VIEWS_JSMESSAGE_BOX_DIALOG_H_ |