Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / simple_message_box_gtk.cc
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.
4
5 #include "chrome/browser/ui/simple_message_box.h"
6
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"
10
11 namespace {
12
13 void SetDialogTitle(GtkWidget* dialog, const base::string16& title) {
14   gtk_window_set_title(GTK_WINDOW(dialog), base::UTF16ToUTF8(title).c_str());
15
16   // The following code requires the dialog to be realized.
17   gtk_widget_realize(dialog);
18
19   // Make sure it's big enough to show the title.
20   GtkRequisition req;
21   gtk_widget_size_request(dialog, &req);
22   int width;
23   gtk_util::GetWidgetSizeFromCharacters(dialog, title.length(), 0,
24                                         &width, NULL);
25   // The fudge factor accounts for extra space needed by the frame
26   // decorations as well as width differences between average text and the
27   // actual title text.
28   width = width * 1.2 + 50;
29
30   if (width > req.width)
31     gtk_widget_set_size_request(dialog, width, -1);
32 }
33
34 int g_dialog_response;
35
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();
40 }
41
42 }  // namespace
43
44 namespace chrome {
45
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)
51     NOTIMPLEMENTED();
52
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;
58   } else {
59     gtk_message_type = (type == MESSAGE_BOX_TYPE_INFORMATION) ?
60         GTK_MESSAGE_INFO : GTK_MESSAGE_WARNING;
61   }
62
63   GtkWidget* dialog = gtk_message_dialog_new(
64       parent,
65       GTK_DIALOG_MODAL,
66       gtk_message_type,
67       gtk_buttons_type,
68       "%s",
69       base::UTF16ToUTF8(message).c_str());
70   gtk_util::ApplyMessageDialogQuirks(dialog);
71   SetDialogTitle(dialog, title);
72
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;
81   }
82
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;
87 }
88
89 }  // namespace chrome