1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/simple_message_box.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/gtk/gtk_util.h"
13 void SetDialogTitle(GtkWidget
* dialog
, const base::string16
& title
) {
14 gtk_window_set_title(GTK_WINDOW(dialog
), base::UTF16ToUTF8(title
).c_str());
16 // The following code requires the dialog to be realized.
17 gtk_widget_realize(dialog
);
19 // Make sure it's big enough to show the title.
21 gtk_widget_size_request(dialog
, &req
);
23 gtk_util::GetWidgetSizeFromCharacters(dialog
, title
.length(), 0,
25 // The fudge factor accounts for extra space needed by the frame
26 // decorations as well as width differences between average text and the
28 width
= width
* 1.2 + 50;
30 if (width
> req
.width
)
31 gtk_widget_set_size_request(dialog
, width
, -1);
34 int g_dialog_response
;
36 void OnDialogResponse(GtkWidget
* widget
, int response
, void* user_data
) {
37 g_dialog_response
= response
;
38 gtk_widget_destroy(widget
);
39 base::MessageLoop::current()->QuitNow();
46 MessageBoxResult
ShowMessageBox(gfx::NativeWindow parent
,
47 const base::string16
& title
,
48 const base::string16
& message
,
49 MessageBoxType type
) {
50 if (type
== MESSAGE_BOX_TYPE_OK_CANCEL
)
53 GtkMessageType gtk_message_type
= GTK_MESSAGE_OTHER
;
54 GtkButtonsType gtk_buttons_type
= GTK_BUTTONS_OK
;
55 if (type
== MESSAGE_BOX_TYPE_QUESTION
) {
56 gtk_message_type
= GTK_MESSAGE_QUESTION
;
57 gtk_buttons_type
= GTK_BUTTONS_YES_NO
;
59 gtk_message_type
= (type
== MESSAGE_BOX_TYPE_INFORMATION
) ?
60 GTK_MESSAGE_INFO
: GTK_MESSAGE_WARNING
;
63 GtkWidget
* dialog
= gtk_message_dialog_new(
69 base::UTF16ToUTF8(message
).c_str());
70 gtk_util::ApplyMessageDialogQuirks(dialog
);
71 SetDialogTitle(dialog
, title
);
73 if (type
== MESSAGE_BOX_TYPE_QUESTION
) {
74 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_YES
);
75 g_signal_connect(dialog
, "response", G_CALLBACK(OnDialogResponse
), NULL
);
76 gtk_util::ShowDialog(dialog
);
77 // Not gtk_dialog_run as it prevents timers from running in the unit tests.
78 base::MessageLoop::current()->Run();
79 return g_dialog_response
== GTK_RESPONSE_YES
? MESSAGE_BOX_RESULT_YES
80 : MESSAGE_BOX_RESULT_NO
;
83 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_OK
);
84 g_signal_connect(dialog
, "response", G_CALLBACK(gtk_widget_destroy
), NULL
);
85 gtk_util::ShowDialog(dialog
);
86 return MESSAGE_BOX_RESULT_YES
;