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