2014-02-14 14:07:23 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/ui/message_box.h"
|
2014-02-14 14:07:23 +00:00
|
|
|
|
|
|
|
#include "base/callback.h"
|
2014-03-12 13:10:00 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
2014-03-16 00:39:43 +00:00
|
|
|
#include "chrome/browser/ui/gtk/gtk_util.h"
|
2014-03-12 13:01:04 +00:00
|
|
|
#include "ui/base/gtk/gtk_signal.h"
|
2014-02-14 14:07:23 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2014-03-12 13:01:04 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MessageBox {
|
|
|
|
public:
|
|
|
|
MessageBox(NativeWindow* parent_window,
|
|
|
|
MessageBoxType type,
|
|
|
|
const std::vector<std::string>& buttons,
|
|
|
|
const std::string& title,
|
|
|
|
const std::string& message,
|
2014-03-12 13:10:00 +00:00
|
|
|
const std::string& detail)
|
|
|
|
: cancel_id_(0) {
|
2014-03-12 13:01:04 +00:00
|
|
|
GtkWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
|
|
|
|
dialog_ = gtk_dialog_new_with_buttons(
|
|
|
|
title.c_str(),
|
|
|
|
window,
|
|
|
|
static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < buttons.size(); ++i)
|
2014-03-12 13:10:00 +00:00
|
|
|
gtk_dialog_add_button(GTK_DIALOG(dialog_),
|
|
|
|
TranslateToStock(i, buttons[i]),
|
|
|
|
i);
|
2014-03-12 13:01:04 +00:00
|
|
|
|
|
|
|
GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_));
|
|
|
|
GtkWidget* message_label = gtk_util::CreateBoldLabel(message);
|
|
|
|
gtk_util::LeftAlignMisc(message_label);
|
|
|
|
gtk_box_pack_start(GTK_BOX(content_area), message_label, FALSE, FALSE, 0);
|
|
|
|
GtkWidget* detail_label = gtk_label_new(detail.c_str());
|
|
|
|
gtk_util::LeftAlignMisc(detail_label);
|
|
|
|
gtk_box_pack_start(GTK_BOX(content_area), detail_label, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
gtk_window_set_resizable(GTK_WINDOW(dialog_), FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
~MessageBox() {
|
|
|
|
gtk_widget_destroy(dialog_);
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:10:00 +00:00
|
|
|
const char* TranslateToStock(int id, const std::string& text) {
|
|
|
|
if (LowerCaseEqualsASCII(text, "cancel")) {
|
|
|
|
cancel_id_ = id;
|
|
|
|
return GTK_STOCK_CANCEL;
|
|
|
|
} else if (LowerCaseEqualsASCII(text, "no")) {
|
|
|
|
cancel_id_ = id;
|
|
|
|
return GTK_STOCK_NO;
|
|
|
|
} else if (LowerCaseEqualsASCII(text, "ok")) {
|
|
|
|
return GTK_STOCK_OK;
|
|
|
|
} else if (LowerCaseEqualsASCII(text, "yes")) {
|
|
|
|
return GTK_STOCK_YES;
|
|
|
|
} else {
|
|
|
|
return text.c_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:01:04 +00:00
|
|
|
void RunAsynchronous(const MessageBoxCallback& callback) {
|
|
|
|
callback_ = callback;
|
|
|
|
g_signal_connect(dialog_, "delete-event",
|
|
|
|
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
|
|
|
|
g_signal_connect(dialog_, "response",
|
|
|
|
G_CALLBACK(OnResponseDialogThunk), this);
|
|
|
|
gtk_widget_show_all(dialog_);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHROMEGTK_CALLBACK_1(MessageBox, void, OnResponseDialog, int);
|
|
|
|
|
|
|
|
GtkWidget* dialog() const { return dialog_; }
|
2014-03-12 13:17:13 +00:00
|
|
|
int cancel_id() const { return cancel_id_; }
|
2014-03-12 13:01:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
GtkWidget* dialog_;
|
|
|
|
MessageBoxCallback callback_;
|
|
|
|
|
2014-03-12 13:10:00 +00:00
|
|
|
// The id to return when the dialog is closed without pressing buttons.
|
|
|
|
int cancel_id_;
|
|
|
|
|
2014-03-12 13:01:04 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(MessageBox);
|
|
|
|
};
|
|
|
|
|
|
|
|
void MessageBox::OnResponseDialog(GtkWidget* widget, int response) {
|
|
|
|
gtk_widget_hide_all(dialog_);
|
2014-03-12 13:10:00 +00:00
|
|
|
|
|
|
|
if (response < 0)
|
|
|
|
callback_.Run(cancel_id_);
|
|
|
|
else
|
|
|
|
callback_.Run(response);
|
2014-03-12 13:01:04 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-02-14 14:07:23 +00:00
|
|
|
int ShowMessageBox(NativeWindow* parent_window,
|
|
|
|
MessageBoxType type,
|
|
|
|
const std::vector<std::string>& buttons,
|
|
|
|
const std::string& title,
|
|
|
|
const std::string& message,
|
|
|
|
const std::string& detail) {
|
2014-03-12 13:17:13 +00:00
|
|
|
MessageBox message_box(parent_window, type, buttons, title, message, detail);
|
|
|
|
gtk_widget_show_all(message_box.dialog());
|
|
|
|
int response = gtk_dialog_run(GTK_DIALOG(message_box.dialog()));
|
|
|
|
if (response < 0)
|
|
|
|
return message_box.cancel_id();
|
|
|
|
else
|
|
|
|
return response;
|
2014-02-14 14:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShowMessageBox(NativeWindow* parent_window,
|
|
|
|
MessageBoxType type,
|
|
|
|
const std::vector<std::string>& buttons,
|
|
|
|
const std::string& title,
|
|
|
|
const std::string& message,
|
|
|
|
const std::string& detail,
|
|
|
|
const MessageBoxCallback& callback) {
|
2014-03-12 13:01:04 +00:00
|
|
|
MessageBox* message_box = new MessageBox(
|
|
|
|
parent_window, type, buttons, title, message, detail);
|
|
|
|
message_box->RunAsynchronous(callback);
|
2014-02-14 14:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|