/
message_box_gtk.cc
242 lines (205 loc) · 7.98 KB
/
message_box_gtk.cc
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/gtk_util.h"
#include "shell/browser/ui/message_box.h"
#include "base/callback.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_window_observer.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/unresponsive_suppressor.h"
#include "ui/base/glib/glib_signal.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gtk/gtk_util.h"
#if defined(USE_X11)
#include "ui/events/platform/x11/x11_event_source.h"
#endif
#if defined(USE_OZONE) || defined(USE_X11)
#include "ui/base/ui_base_features.h"
#endif
#define ANSI_FOREGROUND_RED "\x1b[31m"
#define ANSI_FOREGROUND_BLACK "\x1b[30m"
#define ANSI_TEXT_BOLD "\x1b[1m"
#define ANSI_BACKGROUND_GRAY "\x1b[47m"
#define ANSI_RESET "\x1b[0m"
namespace electron {
MessageBoxSettings::MessageBoxSettings() = default;
MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
MessageBoxSettings::~MessageBoxSettings() = default;
namespace {
class GtkMessageBox : public NativeWindowObserver {
public:
explicit GtkMessageBox(const MessageBoxSettings& settings)
: cancel_id_(settings.cancel_id),
parent_(static_cast<NativeWindow*>(settings.parent_window)) {
// Create dialog.
dialog_ =
gtk_message_dialog_new(nullptr, // parent
static_cast<GtkDialogFlags>(0), // no flags
GetMessageType(settings.type), // type
GTK_BUTTONS_NONE, // no buttons
"%s", settings.message.c_str());
if (!settings.detail.empty())
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_),
"%s", settings.detail.c_str());
if (!settings.title.empty())
gtk_window_set_title(GTK_WINDOW(dialog_), settings.title.c_str());
if (!settings.icon.isNull()) {
// No easy way to obtain this programmatically, but GTK+'s docs
// define GTK_ICON_SIZE_DIALOG to be 48 pixels
static constexpr int pixel_width = 48;
static constexpr int pixel_height = 48;
GdkPixbuf* pixbuf =
gtk_util::GdkPixbufFromSkBitmap(*settings.icon.bitmap());
GdkPixbuf* scaled_pixbuf = gdk_pixbuf_scale_simple(
pixbuf, pixel_width, pixel_height, GDK_INTERP_BILINEAR);
GtkWidget* w = gtk_image_new_from_pixbuf(scaled_pixbuf);
gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog_), w);
gtk_widget_show(w);
g_clear_pointer(&scaled_pixbuf, g_object_unref);
g_clear_pointer(&pixbuf, g_object_unref);
}
if (!settings.checkbox_label.empty()) {
GtkWidget* message_area =
gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog_));
GtkWidget* check_button =
gtk_check_button_new_with_label(settings.checkbox_label.c_str());
g_signal_connect(check_button, "toggled",
G_CALLBACK(OnCheckboxToggledThunk), this);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button),
settings.checkbox_checked);
gtk_container_add(GTK_CONTAINER(message_area), check_button);
gtk_widget_show(check_button);
}
// Add buttons.
GtkDialog* dialog = GTK_DIALOG(dialog_);
if (settings.buttons.size() == 0) {
gtk_dialog_add_button(dialog, TranslateToStock(0, "OK"), 0);
} else {
for (size_t i = 0; i < settings.buttons.size(); ++i) {
gtk_dialog_add_button(dialog, TranslateToStock(i, settings.buttons[i]),
i);
}
}
gtk_dialog_set_default_response(dialog, settings.default_id);
// Parent window.
if (parent_) {
parent_->AddObserver(this);
static_cast<NativeWindowViews*>(parent_)->SetEnabled(false);
gtk::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
}
}
~GtkMessageBox() override {
gtk_widget_destroy(dialog_);
if (parent_) {
parent_->RemoveObserver(this);
static_cast<NativeWindowViews*>(parent_)->SetEnabled(true);
}
}
GtkMessageType GetMessageType(MessageBoxType type) {
switch (type) {
case MessageBoxType::kInformation:
return GTK_MESSAGE_INFO;
case MessageBoxType::kWarning:
return GTK_MESSAGE_WARNING;
case MessageBoxType::kQuestion:
return GTK_MESSAGE_QUESTION;
case MessageBoxType::kError:
return GTK_MESSAGE_ERROR;
default:
return GTK_MESSAGE_OTHER;
}
}
const char* TranslateToStock(int id, const std::string& text) {
const std::string lower = base::ToLowerASCII(text);
if (lower == "cancel")
return gtk_util::GetCancelLabel();
if (lower == "no")
return gtk_util::GetNoLabel();
if (lower == "ok")
return gtk_util::GetOkLabel();
if (lower == "yes")
return gtk_util::GetYesLabel();
return text.c_str();
}
void Show() {
gtk_widget_show(dialog_);
#if defined(USE_X11)
if (!features::IsUsingOzonePlatform()) {
// We need to call gtk_window_present after making the widgets visible to
// make sure window gets correctly raised and gets focus.
x11::Time time = ui::X11EventSource::GetInstance()->GetTimestamp();
gtk_window_present_with_time(GTK_WINDOW(dialog_),
static_cast<uint32_t>(time));
}
#endif
}
int RunSynchronous() {
Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
return (response < 0) ? cancel_id_ : response;
}
void RunAsynchronous(MessageBoxCallback callback) {
callback_ = std::move(callback);
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), nullptr);
g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseDialogThunk),
this);
Show();
}
void OnWindowClosed() override {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
CHROMEG_CALLBACK_1(GtkMessageBox, void, OnResponseDialog, GtkWidget*, int);
CHROMEG_CALLBACK_0(GtkMessageBox, void, OnCheckboxToggled, GtkWidget*);
private:
electron::UnresponsiveSuppressor unresponsive_suppressor_;
// The id to return when the dialog is closed without pressing buttons.
int cancel_id_ = 0;
bool checkbox_checked_ = false;
NativeWindow* parent_;
GtkWidget* dialog_;
MessageBoxCallback callback_;
DISALLOW_COPY_AND_ASSIGN(GtkMessageBox);
};
void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
gtk_widget_hide(dialog_);
if (response < 0)
std::move(callback_).Run(cancel_id_, checkbox_checked_);
else
std::move(callback_).Run(response, checkbox_checked_);
delete this;
}
void GtkMessageBox::OnCheckboxToggled(GtkWidget* widget) {
checkbox_checked_ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
}
} // namespace
int ShowMessageBoxSync(const MessageBoxSettings& settings) {
return GtkMessageBox(settings).RunSynchronous();
}
void ShowMessageBox(const MessageBoxSettings& settings,
MessageBoxCallback callback) {
(new GtkMessageBox(settings))->RunAsynchronous(std::move(callback));
}
void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
if (Browser::Get()->is_ready()) {
electron::MessageBoxSettings settings;
settings.type = electron::MessageBoxType::kError;
settings.buttons = {};
settings.title = "Error";
settings.message = base::UTF16ToUTF8(title);
settings.detail = base::UTF16ToUTF8(content);
GtkMessageBox(settings).RunSynchronous();
} else {
fprintf(stderr,
ANSI_TEXT_BOLD ANSI_BACKGROUND_GRAY ANSI_FOREGROUND_RED
"%s\n" ANSI_FOREGROUND_BLACK "%s" ANSI_RESET "\n",
base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(content).c_str());
}
}
} // namespace electron