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/tab_contents/tab_contents.h" | 13 #include "chrome/browser/tab_contents/tab_contents.h" |
14 #include "chrome/browser/tab_contents/tab_contents_view.h" | 14 #include "chrome/browser/tab_contents/tab_contents_view.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
| 19 // We stash pointers to widgets on the gtk_dialog so we can refer to them |
| 20 // after dialog creation. |
| 21 const char kPromptTextId[] = "chrome_prompt_text"; |
| 22 const char kSuppressCheckboxId[] = "chrome_suppress_checkbox"; |
| 23 |
19 // If there's a text entry in the dialog, get the text from the first one and | 24 // If there's a text entry in the dialog, get the text from the first one and |
20 // return it. | 25 // return it. |
21 std::wstring GetPromptText(GtkDialog* dialog) { | 26 std::wstring GetPromptText(GtkDialog* dialog) { |
22 std::wstring text; | 27 GtkWidget* widget = static_cast<GtkWidget*>( |
23 // TODO(tc): Replace with gtk_dialog_get_content_area() when using GTK 2.14+ | 28 g_object_get_data(G_OBJECT(dialog), kPromptTextId)); |
24 GtkWidget* contents_vbox = dialog->vbox; | 29 if (widget) |
25 GList* first_child = gtk_container_get_children(GTK_CONTAINER(contents_vbox)); | 30 return UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(widget))); |
26 for (GList* item = first_child; item; item = g_list_next(item)) { | 31 return std::wstring(); |
27 if (GTK_IS_ENTRY(item->data)) { | 32 } |
28 text = UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(item->data))); | 33 |
29 } | 34 // If there's a toggle button in the dialog, return the toggled state. |
30 } | 35 // Otherwise, return false. |
31 g_list_free(first_child); | 36 bool ShouldSuppressJSDialogs(GtkDialog* dialog) { |
32 return text; | 37 GtkWidget* widget = static_cast<GtkWidget*>( |
| 38 g_object_get_data(G_OBJECT(dialog), kSuppressCheckboxId)); |
| 39 if (widget) |
| 40 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)); |
| 41 return false; |
33 } | 42 } |
34 | 43 |
35 void OnDialogResponse(GtkDialog* dialog, gint response_id, | 44 void OnDialogResponse(GtkDialog* dialog, gint response_id, |
36 AppModalDialog* app_modal_dialog) { | 45 AppModalDialog* app_modal_dialog) { |
37 switch (response_id) { | 46 switch (response_id) { |
38 case GTK_RESPONSE_OK: | 47 case GTK_RESPONSE_OK: |
39 // The first arg is the prompt text and the second is true if we want to | 48 // The first arg is the prompt text and the second is true if we want to |
40 // suppress additional popups from the page. | 49 // suppress additional popups from the page. |
41 app_modal_dialog->OnAccept(GetPromptText(dialog), false); | 50 app_modal_dialog->OnAccept(GetPromptText(dialog), |
| 51 ShouldSuppressJSDialogs(dialog)); |
42 break; | 52 break; |
43 | 53 |
44 case GTK_RESPONSE_CANCEL: | 54 case GTK_RESPONSE_CANCEL: |
45 case GTK_RESPONSE_DELETE_EVENT: // User hit the X on the dialog. | 55 case GTK_RESPONSE_DELETE_EVENT: // User hit the X on the dialog. |
46 app_modal_dialog->OnCancel(); | 56 app_modal_dialog->OnCancel(); |
47 break; | 57 break; |
48 | 58 |
49 default: | 59 default: |
50 NOTREACHED(); | 60 NOTREACHED(); |
51 } | 61 } |
52 gtk_widget_destroy(GTK_WIDGET(dialog)); | 62 gtk_widget_destroy(GTK_WIDGET(dialog)); |
53 delete app_modal_dialog; | 63 delete app_modal_dialog; |
54 } | 64 } |
55 | 65 |
56 } // namespace | 66 } // namespace |
57 | 67 |
58 AppModalDialog::~AppModalDialog() { | 68 AppModalDialog::~AppModalDialog() { |
59 } | 69 } |
60 | 70 |
61 void AppModalDialog::CreateAndShowDialog() { | 71 void AppModalDialog::CreateAndShowDialog() { |
62 GtkButtonsType buttons = GTK_BUTTONS_NONE; | 72 GtkButtonsType buttons = GTK_BUTTONS_NONE; |
63 GtkMessageType message_type = GTK_MESSAGE_OTHER; | 73 GtkMessageType message_type = GTK_MESSAGE_OTHER; |
| 74 // We add in the OK button manually later because we want to focus it |
| 75 // explicitly. |
64 switch (dialog_flags_) { | 76 switch (dialog_flags_) { |
65 case MessageBoxFlags::kIsJavascriptAlert: | 77 case MessageBoxFlags::kIsJavascriptAlert: |
66 buttons = GTK_BUTTONS_OK; | 78 buttons = GTK_BUTTONS_NONE; |
67 message_type = GTK_MESSAGE_WARNING; | 79 message_type = GTK_MESSAGE_WARNING; |
68 break; | 80 break; |
69 | 81 |
70 case MessageBoxFlags::kIsJavascriptConfirm: | 82 case MessageBoxFlags::kIsJavascriptConfirm: |
71 if (is_before_unload_dialog_) { | 83 if (is_before_unload_dialog_) { |
72 // onbeforeunload also uses a confirm prompt, it just has custom | 84 // onbeforeunload also uses a confirm prompt, it just has custom |
73 // buttons. We add the buttons using gtk_dialog_add_button below. | 85 // buttons. We add the buttons using gtk_dialog_add_button below. |
74 buttons = GTK_BUTTONS_NONE; | 86 buttons = GTK_BUTTONS_NONE; |
75 } else { | 87 } else { |
76 buttons = GTK_BUTTONS_OK_CANCEL; | 88 buttons = GTK_BUTTONS_CANCEL; |
77 } | 89 } |
78 message_type = GTK_MESSAGE_QUESTION; | 90 message_type = GTK_MESSAGE_QUESTION; |
79 break; | 91 break; |
80 | 92 |
81 case MessageBoxFlags::kIsJavascriptPrompt: | 93 case MessageBoxFlags::kIsJavascriptPrompt: |
82 buttons = GTK_BUTTONS_OK_CANCEL; | 94 buttons = GTK_BUTTONS_CANCEL; |
83 message_type = GTK_MESSAGE_QUESTION; | 95 message_type = GTK_MESSAGE_QUESTION; |
84 break; | 96 break; |
85 | 97 |
86 default: | 98 default: |
87 NOTREACHED(); | 99 NOTREACHED(); |
88 } | 100 } |
89 | 101 |
90 GtkWindow* window = tab_contents_->view()->GetTopLevelNativeWindow(); | 102 GtkWindow* window = tab_contents_->view()->GetTopLevelNativeWindow(); |
91 dialog_ = gtk_message_dialog_new(window, GTK_DIALOG_MODAL, | 103 dialog_ = gtk_message_dialog_new(window, GTK_DIALOG_MODAL, |
92 message_type, buttons, "%s", WideToUTF8(message_text_).c_str()); | 104 message_type, buttons, "%s", WideToUTF8(message_text_).c_str()); |
93 gtk_window_set_title(GTK_WINDOW(dialog_), WideToUTF8(title_).c_str()); | 105 gtk_window_set_title(GTK_WINDOW(dialog_), WideToUTF8(title_).c_str()); |
94 | 106 |
| 107 // Adjust content area as needed. |
| 108 if (MessageBoxFlags::kIsJavascriptPrompt == dialog_flags_) { |
| 109 // TODO(tc): Replace with gtk_dialog_get_content_area() when using GTK 2.14+ |
| 110 GtkWidget* contents_vbox = GTK_DIALOG(dialog_)->vbox; |
| 111 GtkWidget* text_box = gtk_entry_new(); |
| 112 gtk_entry_set_text(GTK_ENTRY(text_box), |
| 113 WideToUTF8(default_prompt_text_).c_str()); |
| 114 gtk_box_pack_start(GTK_BOX(contents_vbox), text_box, TRUE, TRUE, 0); |
| 115 g_object_set_data(G_OBJECT(dialog_), kPromptTextId, text_box); |
| 116 } |
| 117 |
| 118 if (display_suppress_checkbox_) { |
| 119 GtkWidget* contents_vbox = GTK_DIALOG(dialog_)->vbox; |
| 120 GtkWidget* check_box = gtk_check_button_new_with_label( |
| 121 l10n_util::GetStringUTF8( |
| 122 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION).c_str()); |
| 123 gtk_box_pack_start(GTK_BOX(contents_vbox), check_box, TRUE, TRUE, 0); |
| 124 g_object_set_data(G_OBJECT(dialog_), kSuppressCheckboxId, check_box); |
| 125 } |
| 126 |
| 127 // Adjust buttons/action area as needed. |
95 if (is_before_unload_dialog_) { | 128 if (is_before_unload_dialog_) { |
96 std::string button_text = l10n_util::GetStringUTF8( | 129 std::string button_text = l10n_util::GetStringUTF8( |
97 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); | 130 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); |
98 gtk_dialog_add_button(GTK_DIALOG(dialog_), button_text.c_str(), | 131 gtk_dialog_add_button(GTK_DIALOG(dialog_), button_text.c_str(), |
99 GTK_RESPONSE_OK); | 132 GTK_RESPONSE_OK); |
100 | 133 |
101 button_text = l10n_util::GetStringUTF8( | 134 button_text = l10n_util::GetStringUTF8( |
102 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); | 135 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); |
103 gtk_dialog_add_button(GTK_DIALOG(dialog_), button_text.c_str(), | 136 gtk_dialog_add_button(GTK_DIALOG(dialog_), button_text.c_str(), |
104 GTK_RESPONSE_CANCEL); | 137 GTK_RESPONSE_CANCEL); |
105 } else if (MessageBoxFlags::kIsJavascriptPrompt == dialog_flags_) { | 138 } else { |
106 // TODO(tc): Replace with gtk_dialog_get_content_area() when using GTK 2.14+ | 139 // Add the OK button and focus it. |
107 GtkWidget* contents_vbox = GTK_DIALOG(dialog_)->vbox; | 140 GtkWidget* ok_button = gtk_dialog_add_button(GTK_DIALOG(dialog_), |
108 GtkWidget* text_box = gtk_entry_new(); | 141 GTK_STOCK_OK, GTK_RESPONSE_OK); |
109 gtk_entry_set_text(GTK_ENTRY(text_box), | 142 if (MessageBoxFlags::kIsJavascriptPrompt != dialog_flags_) |
110 WideToUTF8(default_prompt_text_).c_str()); | 143 gtk_widget_grab_focus(ok_button); |
111 gtk_widget_show(text_box); | |
112 gtk_box_pack_start(GTK_BOX(contents_vbox), text_box, TRUE, TRUE, 0); | |
113 } | 144 } |
114 | 145 |
| 146 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_OK); |
115 g_signal_connect(dialog_, "response", G_CALLBACK(OnDialogResponse), this); | 147 g_signal_connect(dialog_, "response", G_CALLBACK(OnDialogResponse), this); |
116 gtk_widget_show(GTK_WIDGET(GTK_DIALOG(dialog_))); | 148 |
| 149 gtk_widget_show_all(GTK_WIDGET(GTK_DIALOG(dialog_))); |
117 } | 150 } |
118 | 151 |
119 void AppModalDialog::ActivateModalDialog() { | 152 void AppModalDialog::ActivateModalDialog() { |
120 gtk_window_present(GTK_WINDOW(dialog_)); | 153 gtk_window_present(GTK_WINDOW(dialog_)); |
121 } | 154 } |
122 | 155 |
123 void AppModalDialog::CloseModalDialog() { | 156 void AppModalDialog::CloseModalDialog() { |
124 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_DELETE_EVENT, this); | 157 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_DELETE_EVENT, this); |
125 } | 158 } |
126 | 159 |
(...skipping 15 matching lines...) Expand all Loading... |
142 } | 175 } |
143 } | 176 } |
144 | 177 |
145 void AppModalDialog::AcceptWindow() { | 178 void AppModalDialog::AcceptWindow() { |
146 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_OK, this); | 179 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_OK, this); |
147 } | 180 } |
148 | 181 |
149 void AppModalDialog::CancelWindow() { | 182 void AppModalDialog::CancelWindow() { |
150 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_CANCEL, this); | 183 OnDialogResponse(GTK_DIALOG(dialog_), GTK_RESPONSE_CANCEL, this); |
151 } | 184 } |
OLD | NEW |