OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/app_modal_dialog.h" | 5 #include "chrome/browser/app_modal_dialog.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "app/message_box_flags.h" | 10 #include "app/message_box_flags.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "chrome/browser/browser_list.h" | 13 #include "chrome/browser/browser_list.h" |
14 #include "chrome/browser/browser_window.h" | 14 #include "chrome/browser/browser_window.h" |
15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
16 #include "chrome/browser/tab_contents/tab_contents_view.h" | 16 #include "chrome/browser/tab_contents/tab_contents_view.h" |
17 #include "chrome/common/gtk_util.h" | 17 #include "chrome/common/gtk_util.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 #include "grit/locale_settings.h" | 19 #include "grit/locale_settings.h" |
20 | 20 |
21 namespace { | |
22 | |
23 // We stash pointers to widgets on the gtk_dialog so we can refer to them | |
24 // after dialog creation. | |
25 const char kPromptTextId[] = "chrome_prompt_text"; | |
26 const char kSuppressCheckboxId[] = "chrome_suppress_checkbox"; | |
27 | |
28 // If there's a text entry in the dialog, get the text from the first one and | |
29 // return it. | |
30 std::wstring GetPromptText(GtkDialog* dialog) { | |
31 GtkWidget* widget = static_cast<GtkWidget*>( | |
32 g_object_get_data(G_OBJECT(dialog), kPromptTextId)); | |
33 if (widget) | |
34 return UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(widget))); | |
35 return std::wstring(); | |
36 } | |
37 | |
38 // If there's a toggle button in the dialog, return the toggled state. | |
39 // Otherwise, return false. | |
40 bool ShouldSuppressJSDialogs(GtkDialog* dialog) { | |
41 GtkWidget* widget = static_cast<GtkWidget*>( | |
42 g_object_get_data(G_OBJECT(dialog), kSuppressCheckboxId)); | |
43 if (widget) | |
44 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); | |
45 return false; | |
46 } | |
47 | |
48 void OnDialogResponse(GtkDialog* dialog, gint response_id, | |
49 AppModalDialog* app_modal_dialog) { | |
50 switch (response_id) { | |
51 case GTK_RESPONSE_OK: | |
52 // The first arg is the prompt text and the second is true if we want to | |
53 // suppress additional popups from the page. | |
54 app_modal_dialog->OnAccept(GetPromptText(dialog), | |
55 ShouldSuppressJSDialogs(dialog)); | |
56 break; | |
57 | |
58 case GTK_RESPONSE_CANCEL: | |
59 case GTK_RESPONSE_DELETE_EVENT: // User hit the X on the dialog. | |
60 app_modal_dialog->OnCancel(); | |
61 break; | |
62 | |
63 default: | |
64 NOTREACHED(); | |
65 } | |
66 gtk_widget_destroy(GTK_WIDGET(dialog)); | |
67 | |
68 // Now that the dialog is gone, we can put all the windows into separate | |
69 // window groups so other dialogs are no longer app modal. | |
70 for (BrowserList::const_iterator it = BrowserList::begin(); | |
71 it != BrowserList::end(); ++it) { | |
72 GtkWindowGroup* window_group = gtk_window_group_new(); | |
73 gtk_window_group_add_window(window_group, | |
74 (*it)->window()->GetNativeHandle()); | |
75 g_object_unref(window_group); | |
76 } | |
77 | |
78 delete app_modal_dialog; | |
79 } | |
80 | |
81 } // namespace | |
82 | |
83 AppModalDialog::~AppModalDialog() { | 21 AppModalDialog::~AppModalDialog() { |
84 } | 22 } |
85 | 23 |
86 void AppModalDialog::CreateAndShowDialog() { | 24 void AppModalDialog::CreateAndShowDialog() { |
87 GtkButtonsType buttons = GTK_BUTTONS_NONE; | 25 } |
88 GtkMessageType message_type = GTK_MESSAGE_OTHER; | |
89 // We add in the OK button manually later because we want to focus it | |
90 // explicitly. | |
91 switch (dialog_flags_) { | |
92 case MessageBoxFlags::kIsJavascriptAlert: | |
93 buttons = GTK_BUTTONS_NONE; | |
94 message_type = GTK_MESSAGE_WARNING; | |
95 break; | |
96 | 26 |
97 case MessageBoxFlags::kIsJavascriptConfirm: | 27 // static |
98 if (is_before_unload_dialog_) { | 28 void AppModalDialog::OnDialogResponse(GtkDialog* dialog, gint response_id, |
99 // onbeforeunload also uses a confirm prompt, it just has custom | 29 AppModalDialog* app_modal_dialog) { |
100 // buttons. We add the buttons using gtk_dialog_add_button below. | 30 app_modal_dialog->HandleDialogResponse(dialog, response_id); |
101 buttons = GTK_BUTTONS_NONE; | |
102 } else { | |
103 buttons = GTK_BUTTONS_CANCEL; | |
104 } | |
105 message_type = GTK_MESSAGE_QUESTION; | |
106 break; | |
107 | |
108 case MessageBoxFlags::kIsJavascriptPrompt: | |
109 buttons = GTK_BUTTONS_CANCEL; | |
110 message_type = GTK_MESSAGE_QUESTION; | |
111 break; | |
112 | |
113 default: | |
114 NOTREACHED(); | |
115 } | |
116 | |
117 // We want the alert to be app modal so put all the browser windows into the | |
118 // same window group. | |
119 GtkWindowGroup* window_group = gtk_window_group_new(); | |
120 for (BrowserList::const_iterator it = BrowserList::begin(); | |
121 it != BrowserList::end(); ++it) { | |
122 gtk_window_group_add_window(window_group, | |
123 (*it)->window()->GetNativeHandle()); | |
124 } | |
125 g_object_unref(window_group); | |
126 | |
127 gfx::NativeWindow window = client_->GetMessageBoxRootWindow(); | |
128 dialog_ = gtk_message_dialog_new(window, GTK_DIALOG_MODAL, | |
129 message_type, buttons, "%s", WideToUTF8(message_text_).c_str()); | |
130 gtk_util::ApplyMessageDialogQuirks(dialog_); | |
131 gtk_window_set_title(GTK_WINDOW(dialog_), WideToUTF8(title_).c_str()); | |
132 | |
133 // Adjust content area as needed. Set up the prompt text entry or | |
134 // suppression check box. | |
135 if (MessageBoxFlags::kIsJavascriptPrompt == dialog_flags_) { | |
136 // TODO(tc): Replace with gtk_dialog_get_content_area() when using GTK 2.14+ | |
137 GtkWidget* contents_vbox = GTK_DIALOG(dialog_)->vbox; | |
138 GtkWidget* text_box = gtk_entry_new(); | |
139 gtk_entry_set_text(GTK_ENTRY(text_box), | |
140 WideToUTF8(default_prompt_text_).c_str()); | |
141 gtk_box_pack_start(GTK_BOX(contents_vbox), text_box, TRUE, TRUE, 0); | |
142 g_object_set_data(G_OBJECT(dialog_), kPromptTextId, text_box); | |
143 gtk_entry_set_activates_default(GTK_ENTRY(text_box), TRUE); | |
144 } | |
145 | |
146 if (display_suppress_checkbox_) { | |
147 GtkWidget* contents_vbox = GTK_DIALOG(dialog_)->vbox; | |
148 GtkWidget* check_box = gtk_check_button_new_with_label( | |
149 l10n_util::GetStringUTF8( | |
150 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION).c_str()); | |
151 gtk_box_pack_start(GTK_BOX(contents_vbox), check_box, TRUE, TRUE, 0); | |
152 g_object_set_data(G_OBJECT(dialog_), kSuppressCheckboxId, check_box); | |
153 } | |
154 | |
155 // Adjust buttons/action area as needed. | |
156 if (is_before_unload_dialog_) { | |
157 std::string button_text = l10n_util::GetStringUTF8( | |
158 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); | |
159 gtk_dialog_add_button(GTK_DIALOG(dialog_), button_text.c_str(), | |
160 GTK_RESPONSE_OK); | |
161 | |
162 button_text = l10n_util::GetStringUTF8( | |
163 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); | |
164 gtk_dialog_add_button(GTK_DIALOG(dialog_), button_text.c_str(), | |
165 GTK_RESPONSE_CANCEL); | |
166 } else { | |
167 // Add the OK button and focus it. | |
168 GtkWidget* ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog_), | |
169 GTK_STOCK_OK, GTK_RESPONSE_OK); | |
170 if (MessageBoxFlags::kIsJavascriptPrompt != dialog_flags_) | |
171 gtk_widget_grab_focus(ok_button); | |
172 } | |
173 | |
174 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_OK); | |
175 g_signal_connect(dialog_, "response", G_CALLBACK(OnDialogResponse), this); | |
176 | |
177 gtk_widget_show_all(GTK_WIDGET(GTK_DIALOG(dialog_))); | |
178 | |
179 // Suggest a minimum size. | |
180 gint width; | |
181 GtkRequisition req; | |
182 gtk_widget_size_request(dialog_, &req); | |
183 gtk_util::GetWidgetSizeFromResources(dialog_, IDS_ALERT_DIALOG_WIDTH_CHARS, 0, | |
184 &width, NULL); | |
185 if (width > req.width) | |
186 gtk_widget_set_size_request(dialog_, width, -1); | |
187 } | 31 } |
188 | 32 |
189 void AppModalDialog::ActivateModalDialog() { | 33 void AppModalDialog::ActivateModalDialog() { |
190 gtk_window_present(GTK_WINDOW(dialog_)); | 34 gtk_window_present(GTK_WINDOW(dialog_)); |
191 } | 35 } |
192 | 36 |
193 void AppModalDialog::CloseModalDialog() { | 37 void AppModalDialog::CloseModalDialog() { |
194 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_DELETE_EVENT, this); | 38 HandleDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_DELETE_EVENT); |
195 } | |
196 | |
197 int AppModalDialog::GetDialogButtons() { | |
198 switch (dialog_flags_) { | |
199 case MessageBoxFlags::kIsJavascriptAlert: | |
200 return MessageBoxFlags::DIALOGBUTTON_OK; | |
201 | |
202 case MessageBoxFlags::kIsJavascriptConfirm: | |
203 return MessageBoxFlags::DIALOGBUTTON_OK | | |
204 MessageBoxFlags::DIALOGBUTTON_CANCEL; | |
205 | |
206 case MessageBoxFlags::kIsJavascriptPrompt: | |
207 return MessageBoxFlags::DIALOGBUTTON_OK; | |
208 | |
209 default: | |
210 NOTREACHED(); | |
211 return 0; | |
212 } | |
213 } | 39 } |
214 | 40 |
215 void AppModalDialog::AcceptWindow() { | 41 void AppModalDialog::AcceptWindow() { |
216 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_OK, this); | 42 HandleDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_OK); |
217 } | 43 } |
218 | 44 |
219 void AppModalDialog::CancelWindow() { | 45 void AppModalDialog::CancelWindow() { |
220 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_CANCEL, this); | 46 HandleDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_CANCEL); |
221 } | 47 } |
OLD | NEW |