gtk: Be aware of "Cancel" button in message box.
This commit is contained in:
parent
4d175b8f81
commit
5f215cfa6b
1 changed files with 30 additions and 3 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include "browser/ui/message_box.h"
|
#include "browser/ui/message_box.h"
|
||||||
|
|
||||||
#include "base/callback.h"
|
#include "base/callback.h"
|
||||||
|
#include "base/strings/string_util.h"
|
||||||
#include "browser/native_window.h"
|
#include "browser/native_window.h"
|
||||||
#include "browser/ui/gtk/gtk_util.h"
|
#include "browser/ui/gtk/gtk_util.h"
|
||||||
#include "ui/base/gtk/gtk_signal.h"
|
#include "ui/base/gtk/gtk_signal.h"
|
||||||
|
@ -20,7 +21,8 @@ class MessageBox {
|
||||||
const std::vector<std::string>& buttons,
|
const std::vector<std::string>& buttons,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
const std::string& message,
|
const std::string& message,
|
||||||
const std::string& detail) {
|
const std::string& detail)
|
||||||
|
: cancel_id_(0) {
|
||||||
GtkWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
|
GtkWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
|
||||||
dialog_ = gtk_dialog_new_with_buttons(
|
dialog_ = gtk_dialog_new_with_buttons(
|
||||||
title.c_str(),
|
title.c_str(),
|
||||||
|
@ -29,7 +31,9 @@ class MessageBox {
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
for (size_t i = 0; i < buttons.size(); ++i)
|
for (size_t i = 0; i < buttons.size(); ++i)
|
||||||
gtk_dialog_add_button(GTK_DIALOG(dialog_), buttons[i].c_str(), i);
|
gtk_dialog_add_button(GTK_DIALOG(dialog_),
|
||||||
|
TranslateToStock(i, buttons[i]),
|
||||||
|
i);
|
||||||
|
|
||||||
GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_));
|
GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_));
|
||||||
GtkWidget* message_label = gtk_util::CreateBoldLabel(message);
|
GtkWidget* message_label = gtk_util::CreateBoldLabel(message);
|
||||||
|
@ -46,6 +50,22 @@ class MessageBox {
|
||||||
gtk_widget_destroy(dialog_);
|
gtk_widget_destroy(dialog_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RunAsynchronous(const MessageBoxCallback& callback) {
|
void RunAsynchronous(const MessageBoxCallback& callback) {
|
||||||
callback_ = callback;
|
callback_ = callback;
|
||||||
g_signal_connect(dialog_, "delete-event",
|
g_signal_connect(dialog_, "delete-event",
|
||||||
|
@ -63,12 +83,19 @@ class MessageBox {
|
||||||
GtkWidget* dialog_;
|
GtkWidget* dialog_;
|
||||||
MessageBoxCallback callback_;
|
MessageBoxCallback callback_;
|
||||||
|
|
||||||
|
// The id to return when the dialog is closed without pressing buttons.
|
||||||
|
int cancel_id_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(MessageBox);
|
DISALLOW_COPY_AND_ASSIGN(MessageBox);
|
||||||
};
|
};
|
||||||
|
|
||||||
void MessageBox::OnResponseDialog(GtkWidget* widget, int response) {
|
void MessageBox::OnResponseDialog(GtkWidget* widget, int response) {
|
||||||
gtk_widget_hide_all(dialog_);
|
gtk_widget_hide_all(dialog_);
|
||||||
callback_.Run(response);
|
|
||||||
|
if (response < 0)
|
||||||
|
callback_.Run(cancel_id_);
|
||||||
|
else
|
||||||
|
callback_.Run(response);
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue