| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ui/simple_message_box.h" | 5 #include "chrome/browser/ui/simple_message_box.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 namespace chrome { | 30 namespace chrome { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // Multiple SimpleMessageBoxViews can show up at the same time. Each of these | 34 // Multiple SimpleMessageBoxViews can show up at the same time. Each of these |
| 35 // start a nested message-loop. However, these SimpleMessageBoxViews can be | 35 // start a nested message-loop. However, these SimpleMessageBoxViews can be |
| 36 // deleted in any order. This creates problems if a box in an inner-loop gets | 36 // deleted in any order. This creates problems if a box in an inner-loop gets |
| 37 // destroyed before a box in an outer-loop. So to avoid this, ref-counting is | 37 // destroyed before a box in an outer-loop. So to avoid this, ref-counting is |
| 38 // used so that the SimpleMessageBoxViews gets deleted at the right time. | 38 // used so that the SimpleMessageBoxViews gets deleted at the right time. |
| 39 class SimpleMessageBoxViews : public views::DialogDelegate, | 39 class SimpleMessageBoxViews : public views::DialogDelegate, |
| 40 public base::MessagePumpDispatcher, |
| 40 public base::RefCounted<SimpleMessageBoxViews> { | 41 public base::RefCounted<SimpleMessageBoxViews> { |
| 41 public: | 42 public: |
| 42 SimpleMessageBoxViews(const base::string16& title, | 43 SimpleMessageBoxViews(const base::string16& title, |
| 43 const base::string16& message, | 44 const base::string16& message, |
| 44 MessageBoxType type, | 45 MessageBoxType type, |
| 45 const base::string16& yes_text, | 46 const base::string16& yes_text, |
| 46 const base::string16& no_text); | 47 const base::string16& no_text); |
| 47 | 48 |
| 48 MessageBoxResult result() const { return result_; } | 49 MessageBoxResult result() const { return result_; } |
| 49 | 50 |
| 50 // Overridden from views::DialogDelegate: | 51 // Overridden from views::DialogDelegate: |
| 51 virtual int GetDialogButtons() const OVERRIDE; | 52 virtual int GetDialogButtons() const OVERRIDE; |
| 52 virtual base::string16 GetDialogButtonLabel( | 53 virtual base::string16 GetDialogButtonLabel( |
| 53 ui::DialogButton button) const OVERRIDE; | 54 ui::DialogButton button) const OVERRIDE; |
| 54 virtual bool Cancel() OVERRIDE; | 55 virtual bool Cancel() OVERRIDE; |
| 55 virtual bool Accept() OVERRIDE; | 56 virtual bool Accept() OVERRIDE; |
| 56 | 57 |
| 57 // Overridden from views::WidgetDelegate: | 58 // Overridden from views::WidgetDelegate: |
| 58 virtual base::string16 GetWindowTitle() const OVERRIDE; | 59 virtual base::string16 GetWindowTitle() const OVERRIDE; |
| 59 virtual void DeleteDelegate() OVERRIDE; | 60 virtual void DeleteDelegate() OVERRIDE; |
| 60 virtual ui::ModalType GetModalType() const OVERRIDE; | 61 virtual ui::ModalType GetModalType() const OVERRIDE; |
| 61 virtual views::View* GetContentsView() OVERRIDE; | 62 virtual views::View* GetContentsView() OVERRIDE; |
| 62 virtual views::Widget* GetWidget() OVERRIDE; | 63 virtual views::Widget* GetWidget() OVERRIDE; |
| 63 virtual const views::Widget* GetWidget() const OVERRIDE; | 64 virtual const views::Widget* GetWidget() const OVERRIDE; |
| 64 | 65 |
| 66 // Overridden from MessagePumpDispatcher: |
| 67 virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE; |
| 68 |
| 65 private: | 69 private: |
| 66 friend class base::RefCounted<SimpleMessageBoxViews>; | 70 friend class base::RefCounted<SimpleMessageBoxViews>; |
| 67 virtual ~SimpleMessageBoxViews(); | 71 virtual ~SimpleMessageBoxViews(); |
| 68 | 72 |
| 69 // This terminates the nested message-loop. | |
| 70 void Done(); | |
| 71 | |
| 72 const base::string16 window_title_; | 73 const base::string16 window_title_; |
| 73 const MessageBoxType type_; | 74 const MessageBoxType type_; |
| 74 base::string16 yes_text_; | 75 base::string16 yes_text_; |
| 75 base::string16 no_text_; | 76 base::string16 no_text_; |
| 76 MessageBoxResult result_; | 77 MessageBoxResult result_; |
| 77 views::MessageBoxView* message_box_view_; | 78 views::MessageBoxView* message_box_view_; |
| 78 | 79 |
| 80 // Set to false as soon as the user clicks a dialog button; this tells the |
| 81 // dispatcher we're done. |
| 82 bool should_show_dialog_; |
| 83 |
| 79 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); | 84 DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews); |
| 80 }; | 85 }; |
| 81 | 86 |
| 82 //////////////////////////////////////////////////////////////////////////////// | 87 //////////////////////////////////////////////////////////////////////////////// |
| 83 // SimpleMessageBoxViews, public: | 88 // SimpleMessageBoxViews, public: |
| 84 | 89 |
| 85 SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title, | 90 SimpleMessageBoxViews::SimpleMessageBoxViews(const base::string16& title, |
| 86 const base::string16& message, | 91 const base::string16& message, |
| 87 MessageBoxType type, | 92 MessageBoxType type, |
| 88 const base::string16& yes_text, | 93 const base::string16& yes_text, |
| 89 const base::string16& no_text) | 94 const base::string16& no_text) |
| 90 : window_title_(title), | 95 : window_title_(title), |
| 91 type_(type), | 96 type_(type), |
| 92 yes_text_(yes_text), | 97 yes_text_(yes_text), |
| 93 no_text_(no_text), | 98 no_text_(no_text), |
| 94 result_(MESSAGE_BOX_RESULT_NO), | 99 result_(MESSAGE_BOX_RESULT_NO), |
| 95 message_box_view_(new views::MessageBoxView( | 100 message_box_view_(new views::MessageBoxView( |
| 96 views::MessageBoxView::InitParams(message))) { | 101 views::MessageBoxView::InitParams(message))), |
| 102 should_show_dialog_(true) { |
| 97 AddRef(); | 103 AddRef(); |
| 98 | 104 |
| 99 if (yes_text_.empty()) { | 105 if (yes_text_.empty()) { |
| 100 if (type_ == MESSAGE_BOX_TYPE_QUESTION) | 106 if (type_ == MESSAGE_BOX_TYPE_QUESTION) |
| 101 yes_text_ = | 107 yes_text_ = |
| 102 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); | 108 l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); |
| 103 else if (type_ == MESSAGE_BOX_TYPE_OK_CANCEL) | 109 else if (type_ == MESSAGE_BOX_TYPE_OK_CANCEL) |
| 104 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); | 110 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); |
| 105 else | 111 else |
| 106 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); | 112 yes_text_ = l10n_util::GetStringUTF16(IDS_OK); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 125 } | 131 } |
| 126 | 132 |
| 127 base::string16 SimpleMessageBoxViews::GetDialogButtonLabel( | 133 base::string16 SimpleMessageBoxViews::GetDialogButtonLabel( |
| 128 ui::DialogButton button) const { | 134 ui::DialogButton button) const { |
| 129 if (button == ui::DIALOG_BUTTON_CANCEL) | 135 if (button == ui::DIALOG_BUTTON_CANCEL) |
| 130 return no_text_; | 136 return no_text_; |
| 131 return yes_text_; | 137 return yes_text_; |
| 132 } | 138 } |
| 133 | 139 |
| 134 bool SimpleMessageBoxViews::Cancel() { | 140 bool SimpleMessageBoxViews::Cancel() { |
| 141 should_show_dialog_= false; |
| 135 result_ = MESSAGE_BOX_RESULT_NO; | 142 result_ = MESSAGE_BOX_RESULT_NO; |
| 136 Done(); | |
| 137 return true; | 143 return true; |
| 138 } | 144 } |
| 139 | 145 |
| 140 bool SimpleMessageBoxViews::Accept() { | 146 bool SimpleMessageBoxViews::Accept() { |
| 147 should_show_dialog_ = false; |
| 141 result_ = MESSAGE_BOX_RESULT_YES; | 148 result_ = MESSAGE_BOX_RESULT_YES; |
| 142 Done(); | |
| 143 return true; | 149 return true; |
| 144 } | 150 } |
| 145 | 151 |
| 146 base::string16 SimpleMessageBoxViews::GetWindowTitle() const { | 152 base::string16 SimpleMessageBoxViews::GetWindowTitle() const { |
| 147 return window_title_; | 153 return window_title_; |
| 148 } | 154 } |
| 149 | 155 |
| 150 void SimpleMessageBoxViews::DeleteDelegate() { | 156 void SimpleMessageBoxViews::DeleteDelegate() { |
| 151 Release(); | 157 Release(); |
| 152 } | 158 } |
| 153 | 159 |
| 154 ui::ModalType SimpleMessageBoxViews::GetModalType() const { | 160 ui::ModalType SimpleMessageBoxViews::GetModalType() const { |
| 155 return ui::MODAL_TYPE_WINDOW; | 161 return ui::MODAL_TYPE_WINDOW; |
| 156 } | 162 } |
| 157 | 163 |
| 158 views::View* SimpleMessageBoxViews::GetContentsView() { | 164 views::View* SimpleMessageBoxViews::GetContentsView() { |
| 159 return message_box_view_; | 165 return message_box_view_; |
| 160 } | 166 } |
| 161 | 167 |
| 162 views::Widget* SimpleMessageBoxViews::GetWidget() { | 168 views::Widget* SimpleMessageBoxViews::GetWidget() { |
| 163 return message_box_view_->GetWidget(); | 169 return message_box_view_->GetWidget(); |
| 164 } | 170 } |
| 165 | 171 |
| 166 const views::Widget* SimpleMessageBoxViews::GetWidget() const { | 172 const views::Widget* SimpleMessageBoxViews::GetWidget() const { |
| 167 return message_box_view_->GetWidget(); | 173 return message_box_view_->GetWidget(); |
| 168 } | 174 } |
| 169 | 175 |
| 176 uint32_t SimpleMessageBoxViews::Dispatch(const base::NativeEvent& event) { |
| 177 uint32_t action = POST_DISPATCH_PERFORM_DEFAULT; |
| 178 if (!should_show_dialog_) |
| 179 action |= POST_DISPATCH_QUIT_LOOP; |
| 180 return action; |
| 181 } |
| 182 |
| 170 //////////////////////////////////////////////////////////////////////////////// | 183 //////////////////////////////////////////////////////////////////////////////// |
| 171 // SimpleMessageBoxViews, private: | 184 // SimpleMessageBoxViews, private: |
| 172 | 185 |
| 173 SimpleMessageBoxViews::~SimpleMessageBoxViews() { | 186 SimpleMessageBoxViews::~SimpleMessageBoxViews() { |
| 174 } | 187 } |
| 175 | 188 |
| 176 #if defined(OS_WIN) | 189 #if defined(OS_WIN) |
| 177 UINT GetMessageBoxFlagsFromType(MessageBoxType type) { | 190 UINT GetMessageBoxFlagsFromType(MessageBoxType type) { |
| 178 UINT flags = MB_SETFOREGROUND; | 191 UINT flags = MB_SETFOREGROUND; |
| 179 switch (type) { | 192 switch (type) { |
| 180 case MESSAGE_BOX_TYPE_INFORMATION: | 193 case MESSAGE_BOX_TYPE_INFORMATION: |
| 181 return flags | MB_OK | MB_ICONINFORMATION; | 194 return flags | MB_OK | MB_ICONINFORMATION; |
| 182 case MESSAGE_BOX_TYPE_WARNING: | 195 case MESSAGE_BOX_TYPE_WARNING: |
| 183 return flags | MB_OK | MB_ICONWARNING; | 196 return flags | MB_OK | MB_ICONWARNING; |
| 184 case MESSAGE_BOX_TYPE_QUESTION: | 197 case MESSAGE_BOX_TYPE_QUESTION: |
| 185 return flags | MB_YESNO | MB_ICONQUESTION; | 198 return flags | MB_YESNO | MB_ICONQUESTION; |
| 186 case MESSAGE_BOX_TYPE_OK_CANCEL: | 199 case MESSAGE_BOX_TYPE_OK_CANCEL: |
| 187 return flags | MB_OKCANCEL | MB_ICONWARNING; | 200 return flags | MB_OKCANCEL | MB_ICONWARNING; |
| 188 } | 201 } |
| 189 NOTREACHED(); | 202 NOTREACHED(); |
| 190 return flags | MB_OK | MB_ICONWARNING; | 203 return flags | MB_OK | MB_ICONWARNING; |
| 191 } | 204 } |
| 192 #endif | 205 #endif |
| 193 | 206 |
| 194 void SimpleMessageBoxViews::Done() { | |
| 195 aura::Window* window = GetWidget()->GetNativeView(); | |
| 196 aura::client::DispatcherClient* client = | |
| 197 aura::client::GetDispatcherClient(window->GetRootWindow()); | |
| 198 client->QuitNestedMessageLoop(); | |
| 199 } | |
| 200 | |
| 201 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent, | 207 MessageBoxResult ShowMessageBoxImpl(gfx::NativeWindow parent, |
| 202 const base::string16& title, | 208 const base::string16& title, |
| 203 const base::string16& message, | 209 const base::string16& message, |
| 204 MessageBoxType type, | 210 MessageBoxType type, |
| 205 const base::string16& yes_text, | 211 const base::string16& yes_text, |
| 206 const base::string16& no_text) { | 212 const base::string16& no_text) { |
| 207 #if defined(OS_WIN) | 213 #if defined(OS_WIN) |
| 208 // GPU-based dialogs can't be used early on; fallback to a Windows MessageBox. | 214 // GPU-based dialogs can't be used early on; fallback to a Windows MessageBox. |
| 209 if (!ui::ContextFactory::GetInstance()) { | 215 if (!ui::ContextFactory::GetInstance()) { |
| 210 int result = ui::MessageBox(views::HWNDForNativeWindow(parent), message, | 216 int result = ui::MessageBox(views::HWNDForNativeWindow(parent), message, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 221 aura::Window* anchor = parent; | 227 aura::Window* anchor = parent; |
| 222 aura::client::DispatcherClient* client = anchor ? | 228 aura::client::DispatcherClient* client = anchor ? |
| 223 aura::client::GetDispatcherClient(anchor->GetRootWindow()) : NULL; | 229 aura::client::GetDispatcherClient(anchor->GetRootWindow()) : NULL; |
| 224 if (!client) { | 230 if (!client) { |
| 225 // Use the widget's window itself so that the message loop | 231 // Use the widget's window itself so that the message loop |
| 226 // exists when the dialog is closed by some other means than | 232 // exists when the dialog is closed by some other means than |
| 227 // |Cancel| or |Accept|. | 233 // |Cancel| or |Accept|. |
| 228 anchor = dialog->GetWidget()->GetNativeWindow(); | 234 anchor = dialog->GetWidget()->GetNativeWindow(); |
| 229 client = aura::client::GetDispatcherClient(anchor->GetRootWindow()); | 235 client = aura::client::GetDispatcherClient(anchor->GetRootWindow()); |
| 230 } | 236 } |
| 231 client->RunWithDispatcher(NULL, anchor); | 237 client->RunWithDispatcher(dialog.get(), anchor); |
| 232 return dialog->result(); | 238 return dialog->result(); |
| 233 } | 239 } |
| 234 | 240 |
| 235 } // namespace | 241 } // namespace |
| 236 | 242 |
| 237 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, | 243 MessageBoxResult ShowMessageBox(gfx::NativeWindow parent, |
| 238 const base::string16& title, | 244 const base::string16& title, |
| 239 const base::string16& message, | 245 const base::string16& message, |
| 240 MessageBoxType type) { | 246 MessageBoxType type) { |
| 241 return ShowMessageBoxImpl( | 247 return ShowMessageBoxImpl( |
| 242 parent, title, message, type, base::string16(), base::string16()); | 248 parent, title, message, type, base::string16(), base::string16()); |
| 243 } | 249 } |
| 244 | 250 |
| 245 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, | 251 MessageBoxResult ShowMessageBoxWithButtonText(gfx::NativeWindow parent, |
| 246 const base::string16& title, | 252 const base::string16& title, |
| 247 const base::string16& message, | 253 const base::string16& message, |
| 248 const base::string16& yes_text, | 254 const base::string16& yes_text, |
| 249 const base::string16& no_text) { | 255 const base::string16& no_text) { |
| 250 return ShowMessageBoxImpl( | 256 return ShowMessageBoxImpl( |
| 251 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); | 257 parent, title, message, MESSAGE_BOX_TYPE_QUESTION, yes_text, no_text); |
| 252 } | 258 } |
| 253 | 259 |
| 254 } // namespace chrome | 260 } // namespace chrome |
| OLD | NEW |