1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
// Copyright (c) 2012 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/ui/simple_message_box.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/run_loop.h"
#include "chrome/browser/browser_process.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/controls/message_box_view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"
#if defined(USE_AURA)
#include "ui/aura/client/dispatcher_client.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#endif
namespace chrome {
namespace {
// Multiple SimpleMessageBoxViews can show up at the same time. Each of these
// start a nested message-loop. However, these SimpleMessageBoxViews can be
// deleted in any order. This creates problems if a box in an inner-loop gets
// destroyed before a box in an outer-loop. So to avoid this, ref-counting is
// used so that the SimpleMessageBoxViews gets deleted at the right time.
class SimpleMessageBoxViews : public views::DialogDelegate,
public MessageLoop::Dispatcher,
public base::RefCounted<SimpleMessageBoxViews> {
public:
SimpleMessageBoxViews(const string16& title,
const string16& message,
MessageBoxType type);
MessageBoxResult result() const { return result_; }
// Overridden from views::DialogDelegate:
virtual int GetDialogButtons() const OVERRIDE;
virtual string16 GetDialogButtonLabel(ui::DialogButton button) const OVERRIDE;
virtual bool Cancel() OVERRIDE;
virtual bool Accept() OVERRIDE;
// Overridden from views::WidgetDelegate:
virtual string16 GetWindowTitle() const OVERRIDE;
virtual void DeleteDelegate() OVERRIDE;
virtual ui::ModalType GetModalType() const OVERRIDE;
virtual views::View* GetContentsView() OVERRIDE;
virtual views::Widget* GetWidget() OVERRIDE;
virtual const views::Widget* GetWidget() const OVERRIDE;
// Overridden from MessageLoop::Dispatcher:
virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE;
private:
friend class base::RefCounted<SimpleMessageBoxViews>;
virtual ~SimpleMessageBoxViews();
const string16 window_title_;
const MessageBoxType type_;
MessageBoxResult result_;
views::MessageBoxView* message_box_view_;
// Set to false as soon as the user clicks a dialog button; this tells the
// dispatcher we're done.
bool should_show_dialog_;
DISALLOW_COPY_AND_ASSIGN(SimpleMessageBoxViews);
};
////////////////////////////////////////////////////////////////////////////////
// SimpleMessageBoxViews, public:
SimpleMessageBoxViews::SimpleMessageBoxViews(const string16& title,
const string16& message,
MessageBoxType type)
: window_title_(title),
type_(type),
result_(MESSAGE_BOX_RESULT_NO),
message_box_view_(new views::MessageBoxView(
views::MessageBoxView::InitParams(message))),
should_show_dialog_(true) {
AddRef();
}
int SimpleMessageBoxViews::GetDialogButtons() const {
if (type_ == MESSAGE_BOX_TYPE_QUESTION)
return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
return ui::DIALOG_BUTTON_OK;
}
string16 SimpleMessageBoxViews::GetDialogButtonLabel(
ui::DialogButton button) const {
if (type_ == MESSAGE_BOX_TYPE_QUESTION) {
return l10n_util::GetStringUTF16((button == ui::DIALOG_BUTTON_OK) ?
IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL :
IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL);
}
return l10n_util::GetStringUTF16(IDS_OK);
}
bool SimpleMessageBoxViews::Cancel() {
should_show_dialog_= false;
result_ = MESSAGE_BOX_RESULT_NO;
return true;
}
bool SimpleMessageBoxViews::Accept() {
should_show_dialog_ = false;
result_ = MESSAGE_BOX_RESULT_YES;
return true;
}
string16 SimpleMessageBoxViews::GetWindowTitle() const {
return window_title_;
}
void SimpleMessageBoxViews::DeleteDelegate() {
Release();
}
ui::ModalType SimpleMessageBoxViews::GetModalType() const {
return ui::MODAL_TYPE_WINDOW;
}
views::View* SimpleMessageBoxViews::GetContentsView() {
return message_box_view_;
}
views::Widget* SimpleMessageBoxViews::GetWidget() {
return message_box_view_->GetWidget();
}
const views::Widget* SimpleMessageBoxViews::GetWidget() const {
return message_box_view_->GetWidget();
}
bool SimpleMessageBoxViews::Dispatch(const base::NativeEvent& event) {
#if defined(OS_WIN)
TranslateMessage(&event);
DispatchMessage(&event);
#elif defined(USE_AURA)
aura::Env::GetInstance()->GetDispatcher()->Dispatch(event);
#endif
return should_show_dialog_;
}
////////////////////////////////////////////////////////////////////////////////
// SimpleMessageBoxViews, private:
SimpleMessageBoxViews::~SimpleMessageBoxViews() {
}
} // namespace
MessageBoxResult ShowMessageBox(gfx::NativeWindow parent,
const string16& title,
const string16& message,
MessageBoxType type) {
scoped_refptr<SimpleMessageBoxViews> dialog(
new SimpleMessageBoxViews(title, message, type));
views::Widget::CreateWindowWithParent(dialog, parent)->Show();
#if defined(USE_AURA)
// Use the widget's window itself so that the message loop
// exists when the dialog is closed by some other means than
// |Cancel| or |Accept|.
aura::Window* anchor = parent ?
parent : dialog->GetWidget()->GetNativeWindow();
aura::client::GetDispatcherClient(anchor->GetRootWindow())->
RunWithDispatcher(dialog, anchor, true);
#else
{
MessageLoop::ScopedNestableTaskAllower allow(MessageLoopForUI::current());
base::RunLoop run_loop(dialog);
run_loop.Run();
}
#endif
return dialog->result();
}
} // namespace chrome
|