OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/platform_util.h" | 5 #include "chrome/browser/platform_util.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/file_util.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/process_util.h" | |
12 #include "base/string_util.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "chrome/browser/ui/gtk/gtk_util.h" | |
15 #include "content/common/process_watcher.h" | |
16 #include "googleurl/src/gurl.h" | |
17 #include "ui/gfx/native_widget_types.h" | |
18 | |
19 namespace { | |
20 | |
21 void SetDialogTitle(GtkWidget* dialog, const string16& title) { | |
22 gtk_window_set_title(GTK_WINDOW(dialog), UTF16ToUTF8(title).c_str()); | |
23 | |
24 #if !defined(OS_CHROMEOS) | |
25 // The following code requires the dialog to be realized. However, we host | |
26 // dialog's content in a Chrome window without really realize the dialog | |
27 // on ChromeOS. Thus, skip the following code for ChromeOS. | |
28 gtk_widget_realize(dialog); | |
29 | |
30 // Make sure it's big enough to show the title. | |
31 GtkRequisition req; | |
32 gtk_widget_size_request(dialog, &req); | |
33 int width; | |
34 gtk_util::GetWidgetSizeFromCharacters(dialog, title.length(), 0, | |
35 &width, NULL); | |
36 // The fudge factor accounts for extra space needed by the frame | |
37 // decorations as well as width differences between average text and the | |
38 // actual title text. | |
39 width = width * 1.2 + 50; | |
40 | |
41 if (width > req.width) | |
42 gtk_widget_set_size_request(dialog, width, -1); | |
43 #endif // !defined(OS_CHROMEOS) | |
44 } | |
45 | |
46 int g_dialog_response; | |
47 | |
48 void HandleOnResponseDialog(GtkWidget* widget, | |
49 int response, | |
50 void* user_data) { | |
51 g_dialog_response = response; | |
52 gtk_widget_destroy(widget); | |
53 MessageLoop::current()->QuitNow(); | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 namespace platform_util { | 9 namespace platform_util { |
59 | 10 |
60 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { | 11 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { |
61 // A detached widget won't have a toplevel window as an ancestor, so we can't | 12 // A detached widget won't have a toplevel window as an ancestor, so we can't |
62 // assume that the query for toplevel will return a window. | 13 // assume that the query for toplevel will return a window. |
63 GtkWidget* toplevel = gtk_widget_get_ancestor(view, GTK_TYPE_WINDOW); | 14 GtkWidget* toplevel = gtk_widget_get_ancestor(view, GTK_TYPE_WINDOW); |
64 return GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; | 15 return GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; |
65 } | 16 } |
66 | 17 |
67 gfx::NativeView GetParent(gfx::NativeView view) { | 18 gfx::NativeView GetParent(gfx::NativeView view) { |
68 return gtk_widget_get_parent(view); | 19 return gtk_widget_get_parent(view); |
69 } | 20 } |
70 | 21 |
71 bool IsWindowActive(gfx::NativeWindow window) { | 22 bool IsWindowActive(gfx::NativeWindow window) { |
72 return gtk_window_is_active(window); | 23 return gtk_window_is_active(window); |
73 } | 24 } |
74 | 25 |
75 void ActivateWindow(gfx::NativeWindow window) { | 26 void ActivateWindow(gfx::NativeWindow window) { |
76 gtk_window_present(window); | 27 gtk_window_present(window); |
77 } | 28 } |
78 | 29 |
79 bool IsVisible(gfx::NativeView view) { | 30 bool IsVisible(gfx::NativeView view) { |
80 return gtk_widget_get_visible(view); | 31 return gtk_widget_get_visible(view); |
81 } | 32 } |
82 | 33 |
83 void SimpleErrorBox(gfx::NativeWindow parent, | |
84 const string16& title, | |
85 const string16& message) { | |
86 GtkWidget* dialog = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL, | |
87 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", UTF16ToUTF8(message).c_str()); | |
88 gtk_util::ApplyMessageDialogQuirks(dialog); | |
89 SetDialogTitle(dialog, title); | |
90 | |
91 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK); | |
92 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL); | |
93 gtk_util::ShowDialog(dialog); | |
94 } | |
95 | |
96 bool SimpleYesNoBox(gfx::NativeWindow parent, | |
97 const string16& title, | |
98 const string16& message) { | |
99 GtkWidget* dialog = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL, | |
100 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "%s", | |
101 UTF16ToUTF8(message).c_str()); | |
102 gtk_util::ApplyMessageDialogQuirks(dialog); | |
103 SetDialogTitle(dialog, title); | |
104 | |
105 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES); | |
106 g_signal_connect(dialog, | |
107 "response", | |
108 G_CALLBACK(HandleOnResponseDialog), | |
109 NULL); | |
110 gtk_util::ShowDialog(dialog); | |
111 // Not gtk_dialog_run as it prevents timers from running in the unit tests. | |
112 MessageLoop::current()->Run(); | |
113 return g_dialog_response == GTK_RESPONSE_YES; | |
114 } | |
115 | |
116 } // namespace platform_util | 34 } // namespace platform_util |
OLD | NEW |