electron/atom/browser/ui/message_box_gtk.cc

206 lines
6.4 KiB
C++
Raw Normal View History

// 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 "atom/browser/ui/message_box.h"
#include "atom/browser/browser.h"
2015-07-07 07:45:13 +00:00
#include "atom/browser/native_window.h"
#include "base/callback.h"
2015-07-07 07:45:13 +00:00
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
2015-07-07 07:45:13 +00:00
#include "chrome/browser/ui/libgtk2ui/gtk2_signal.h"
#include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
#include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
2015-07-07 08:42:03 +00:00
#include "ui/views/widget/desktop_aura/x11_desktop_handler.h"
#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 atom {
2015-07-07 07:45:13 +00:00
namespace {
class GtkMessageBox {
public:
2015-07-07 09:03:47 +00:00
GtkMessageBox(NativeWindow* parent_window,
2015-07-07 07:45:13 +00:00
MessageBoxType type,
const std::vector<std::string>& buttons,
2015-07-07 10:26:50 +00:00
int cancel_id,
2015-07-07 07:45:13 +00:00
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon)
: dialog_scope_(parent_window),
2015-07-07 10:26:50 +00:00
cancel_id_(cancel_id) {
2015-07-07 07:45:13 +00:00
// Create dialog.
dialog_ = gtk_message_dialog_new(
nullptr, // parent
2015-07-07 09:03:47 +00:00
static_cast<GtkDialogFlags>(0), // no flags
GetMessageType(type), // type
2015-07-07 07:45:13 +00:00
GTK_BUTTONS_NONE, // no buttons
"%s", message.c_str());
2015-07-07 09:08:30 +00:00
if (!detail.empty())
gtk_message_dialog_format_secondary_text(
GTK_MESSAGE_DIALOG(dialog_), "%s", detail.c_str());
2015-07-07 09:09:58 +00:00
if (!title.empty())
gtk_window_set_title(GTK_WINDOW(dialog_), title.c_str());
2015-07-07 07:45:13 +00:00
// Set dialog's icon.
if (!icon.isNull()) {
GdkPixbuf* pixbuf = libgtk2ui::GdkPixbufFromSkBitmap(*icon.bitmap());
GtkWidget* image = gtk_image_new_from_pixbuf(pixbuf);
gtk_message_dialog_set_image(GTK_MESSAGE_DIALOG(dialog_), image);
gtk_widget_show(image);
g_object_unref(pixbuf);
}
// Add buttons.
for (size_t i = 0; i < buttons.size(); ++i) {
gtk_dialog_add_button(GTK_DIALOG(dialog_),
TranslateToStock(i, buttons[i]),
i);
}
// Parent window.
if (parent_window) {
gfx::NativeWindow window = parent_window->GetNativeWindow();
libgtk2ui::SetGtkTransientForAura(dialog_, window);
}
}
~GtkMessageBox() {
gtk_widget_destroy(dialog_);
}
GtkMessageType GetMessageType(MessageBoxType type) {
switch (type) {
case MESSAGE_BOX_TYPE_INFORMATION:
return GTK_MESSAGE_INFO;
case MESSAGE_BOX_TYPE_WARNING:
return GTK_MESSAGE_WARNING;
case MESSAGE_BOX_TYPE_QUESTION:
return GTK_MESSAGE_QUESTION;
case MESSAGE_BOX_TYPE_ERROR:
return GTK_MESSAGE_ERROR;
default:
return GTK_MESSAGE_OTHER;
}
}
2015-07-07 07:45:13 +00:00
const char* TranslateToStock(int id, const std::string& text) {
2015-12-07 11:56:23 +00:00
std::string lower = base::ToLowerASCII(text);
2015-07-07 10:33:11 +00:00
if (lower == "cancel")
2015-07-07 07:45:13 +00:00
return GTK_STOCK_CANCEL;
2015-07-07 10:33:11 +00:00
else if (lower == "no")
2015-07-07 07:45:13 +00:00
return GTK_STOCK_NO;
2015-07-07 10:33:11 +00:00
else if (lower == "ok")
2015-07-07 07:45:13 +00:00
return GTK_STOCK_OK;
2015-07-07 10:33:11 +00:00
else if (lower == "yes")
2015-07-07 07:45:13 +00:00
return GTK_STOCK_YES;
2015-07-07 10:33:11 +00:00
else
2015-07-07 07:45:13 +00:00
return text.c_str();
}
2015-07-07 08:42:03 +00:00
void Show() {
gtk_widget_show_all(dialog_);
// We need to call gtk_window_present after making the widgets visible to
// make sure window gets correctly raised and gets focus.
int time = views::X11DesktopHandler::get()->wm_user_time_ms();
gtk_window_present_with_time(GTK_WINDOW(dialog_), time);
}
2015-07-07 09:03:47 +00:00
int RunSynchronous() {
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
Show();
int response = gtk_dialog_run(GTK_DIALOG(dialog_));
if (response < 0)
return cancel_id_;
else
return response;
}
2015-07-07 07:45:13 +00:00
void RunAsynchronous(const MessageBoxCallback& callback) {
callback_ = callback;
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), nullptr);
g_signal_connect(dialog_, "response",
G_CALLBACK(OnResponseDialogThunk), this);
2015-07-07 08:42:03 +00:00
Show();
2015-07-07 07:45:13 +00:00
}
CHROMEGTK_CALLBACK_1(GtkMessageBox, void, OnResponseDialog, int);
private:
atom::NativeWindow::DialogScope dialog_scope_;
// The id to return when the dialog is closed without pressing buttons.
int cancel_id_;
GtkWidget* dialog_;
MessageBoxCallback callback_;
DISALLOW_COPY_AND_ASSIGN(GtkMessageBox);
};
void GtkMessageBox::OnResponseDialog(GtkWidget* widget, int response) {
gtk_widget_hide_all(dialog_);
if (response < 0)
callback_.Run(cancel_id_);
else
callback_.Run(response);
delete this;
}
} // namespace
int ShowMessageBox(NativeWindow* parent,
MessageBoxType type,
const std::vector<std::string>& buttons,
2015-07-07 10:26:50 +00:00
int cancel_id,
2015-07-23 06:16:43 +00:00
int options,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon) {
2015-07-07 10:26:50 +00:00
return GtkMessageBox(parent, type, buttons, cancel_id, title, message, detail,
2015-07-07 09:03:47 +00:00
icon).RunSynchronous();
}
2015-07-07 07:45:13 +00:00
void ShowMessageBox(NativeWindow* parent,
MessageBoxType type,
const std::vector<std::string>& buttons,
2015-07-07 10:26:50 +00:00
int cancel_id,
2015-07-23 06:16:43 +00:00
int options,
const std::string& title,
const std::string& message,
const std::string& detail,
const gfx::ImageSkia& icon,
const MessageBoxCallback& callback) {
2015-07-07 10:26:50 +00:00
(new GtkMessageBox(parent, type, buttons, cancel_id, title, message, detail,
2015-07-07 09:03:47 +00:00
icon))->RunAsynchronous(callback);
}
void ShowErrorBox(const base::string16& title, const base::string16& content) {
if (Browser::Get()->is_ready()) {
2015-07-07 10:26:50 +00:00
GtkMessageBox(nullptr, MESSAGE_BOX_TYPE_ERROR, { "OK" }, 0, "Error",
2015-07-07 09:03:47 +00:00
base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(content).c_str(),
gfx::ImageSkia()).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 atom