| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/views/confirm_message_box_dialog.h" | 5 #include "chrome/browser/views/confirm_message_box_dialog.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/message_box_flags.h" | 8 #include "app/message_box_flags.h" |
| 9 #include "base/message_loop.h" | |
| 10 #include "grit/generated_resources.h" | 9 #include "grit/generated_resources.h" |
| 11 #include "views/controls/message_box_view.h" | 10 #include "grit/locale_settings.h" |
| 11 #include "views/standard_layout.h" |
| 12 #include "views/widget/widget.h" | 12 #include "views/widget/widget.h" |
| 13 #include "views/window/window.h" | 13 #include "views/window/window.h" |
| 14 | 14 |
| 15 // static | 15 // static |
| 16 bool ConfirmMessageBoxDialog::Run(gfx::NativeWindow parent, | 16 void ConfirmMessageBoxDialog::Run(gfx::NativeWindow parent, |
| 17 ConfirmMessageBoxObserver* observer, |
| 17 const std::wstring& message_text, | 18 const std::wstring& message_text, |
| 18 const std::wstring& window_title) { | 19 const std::wstring& window_title) { |
| 19 ConfirmMessageBoxDialog* dialog = new ConfirmMessageBoxDialog(parent, | 20 DCHECK(observer); |
| 20 message_text, window_title); | 21 ConfirmMessageBoxDialog* dialog = new ConfirmMessageBoxDialog(observer, |
| 21 MessageLoopForUI::current()->Run(dialog); | 22 message_text, window_title); |
| 22 return dialog->accepted(); | 23 views::Window* window = views::Window::CreateChromeWindow( |
| 24 parent, gfx::Rect(), dialog); |
| 25 window->Show(); |
| 23 } | 26 } |
| 24 | 27 |
| 25 ConfirmMessageBoxDialog::ConfirmMessageBoxDialog(gfx::NativeWindow parent, | 28 ConfirmMessageBoxDialog::ConfirmMessageBoxDialog( |
| 26 const std::wstring& message_text, | 29 ConfirmMessageBoxObserver* observer, const std::wstring& message_text, |
| 27 const std::wstring& window_title) | 30 const std::wstring& window_title) |
| 28 : message_text_(message_text), | 31 : observer_(observer), |
| 29 window_title_(window_title), | 32 window_title_(window_title) { |
| 30 accepted_(false), | 33 message_label_ = new views::Label(message_text); |
| 31 is_blocking_(true) { | 34 message_label_->SetMultiLine(true); |
| 32 message_box_view_ = new MessageBoxView(MessageBoxFlags::kIsConfirmMessageBox, | 35 l10n_util::TextDirection direction = |
| 33 message_text_, window_title_); | 36 l10n_util::GetFirstStrongCharacterDirection(message_label_->GetText()); |
| 34 views::Window::CreateChromeWindow(parent, gfx::Rect(), this)->Show(); | 37 views::Label::Alignment alignment; |
| 35 } | 38 if (direction == l10n_util::RIGHT_TO_LEFT) |
| 36 | 39 alignment = views::Label::ALIGN_RIGHT; |
| 37 ConfirmMessageBoxDialog::~ConfirmMessageBoxDialog() { | 40 else |
| 38 } | 41 alignment = views::Label::ALIGN_LEFT; |
| 39 | 42 // In addition, we should set the RTL alignment mode as |
| 40 void ConfirmMessageBoxDialog::DeleteDelegate() { | 43 // AUTO_DETECT_ALIGNMENT so that the alignment will not be flipped around |
| 41 delete this; | 44 // in RTL locales. |
| 45 message_label_->SetRTLAlignmentMode(views::Label::AUTO_DETECT_ALIGNMENT); |
| 46 message_label_->SetHorizontalAlignment(alignment); |
| 47 AddChildView(message_label_); |
| 42 } | 48 } |
| 43 | 49 |
| 44 int ConfirmMessageBoxDialog::GetDialogButtons() const { | 50 int ConfirmMessageBoxDialog::GetDialogButtons() const { |
| 45 return MessageBoxFlags::DIALOGBUTTON_OK | | 51 return MessageBoxFlags::DIALOGBUTTON_OK | |
| 46 MessageBoxFlags::DIALOGBUTTON_CANCEL; | 52 MessageBoxFlags::DIALOGBUTTON_CANCEL; |
| 47 } | 53 } |
| 48 | 54 |
| 49 std::wstring ConfirmMessageBoxDialog::GetWindowTitle() const { | 55 std::wstring ConfirmMessageBoxDialog::GetWindowTitle() const { |
| 50 return window_title_; | 56 return window_title_; |
| 51 } | 57 } |
| 52 | 58 |
| 53 std::wstring ConfirmMessageBoxDialog::GetDialogButtonLabel( | 59 std::wstring ConfirmMessageBoxDialog::GetDialogButtonLabel( |
| 54 MessageBoxFlags::DialogButton button) const { | 60 MessageBoxFlags::DialogButton button) const { |
| 55 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { | 61 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { |
| 56 return l10n_util::GetString(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); | 62 return l10n_util::GetString(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); |
| 57 } | 63 } |
| 58 if (button == MessageBoxFlags::DIALOGBUTTON_CANCEL) | 64 if (button == MessageBoxFlags::DIALOGBUTTON_CANCEL) |
| 59 return l10n_util::GetString(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); | 65 return l10n_util::GetString(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); |
| 60 return DialogDelegate::GetDialogButtonLabel(button); | 66 return DialogDelegate::GetDialogButtonLabel(button); |
| 61 } | 67 } |
| 62 | 68 |
| 63 views::View* ConfirmMessageBoxDialog::GetContentsView() { | |
| 64 return message_box_view_; | |
| 65 } | |
| 66 | |
| 67 bool ConfirmMessageBoxDialog::Accept() { | 69 bool ConfirmMessageBoxDialog::Accept() { |
| 68 is_blocking_ = false; | 70 observer_->OnConfirmMessageAccept(); |
| 69 accepted_ = true; | 71 return true; // Dialog may now be closed. |
| 70 return true; | |
| 71 } | 72 } |
| 72 | 73 |
| 73 bool ConfirmMessageBoxDialog::Cancel() { | 74 bool ConfirmMessageBoxDialog::Cancel() { |
| 74 is_blocking_ = false; | 75 observer_->OnConfirmMessageCancel(); |
| 75 accepted_ = false; | 76 return true; // Dialog may now be closed. |
| 76 return true; | |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool ConfirmMessageBoxDialog::Dispatch(const MSG& msg) { | 79 void ConfirmMessageBoxDialog::Layout() { |
| 80 TranslateMessage(&msg); | 80 gfx::Size sz = message_label_->GetPreferredSize(); |
| 81 DispatchMessage(&msg); | 81 message_label_->SetBounds(kPanelHorizMargin, kPanelVertMargin, |
| 82 return is_blocking_; | 82 width() - 2 * kPanelHorizMargin, |
| 83 sz.height()); |
| 83 } | 84 } |
| 85 |
| 86 gfx::Size ConfirmMessageBoxDialog::GetPreferredSize() { |
| 87 return gfx::Size(views::Window::GetLocalizedContentsSize( |
| 88 IDS_CONFIRM_MESSAGE_BOX_DEFAULT_WIDTH_CHARS, |
| 89 IDS_CONFIRM_MESSAGE_BOX_DEFAULT_HEIGHT_LINES)); |
| 90 } |
| OLD | NEW |