Chromium Code Reviews| Index: content/shell/shell_javascript_dialog_gtk.cc |
| diff --git a/content/shell/shell_javascript_dialog_gtk.cc b/content/shell/shell_javascript_dialog_gtk.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..de203f4eb1aa7651b63531e86f7950d6a50e99ff |
| --- /dev/null |
| +++ b/content/shell/shell_javascript_dialog_gtk.cc |
| @@ -0,0 +1,123 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/shell/shell_javascript_dialog.h" |
| + |
| +#include <gtk/gtk.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/string_util.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "content/shell/resource.h" |
| +#include "content/shell/shell.h" |
| +#include "content/shell/shell_javascript_dialog_creator.h" |
| + |
| +namespace { |
| + |
| +const char kPromptTextId[] = "content_shell_prompt_text"; |
|
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
not needed?
Shouqun Liu
2012/07/30 08:08:44
the kPromptTextId is used to get text value from g
jochen (gone - plz use gerrit)
2012/07/30 08:15:49
ah, sorry, missed that
|
| + |
| +// If there's a text entry in the dialog, get the text from the first one and |
| +// return it. |
| +string16 GetPromptText(GtkDialog* dialog) { |
| + GtkWidget* widget = static_cast<GtkWidget*>( |
| + g_object_get_data(G_OBJECT(dialog), kPromptTextId)); |
| + if (widget) |
| + return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(widget))); |
| + return string16(); |
| +} |
| + |
| +} // namespace |
|
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
nit. two spaces before //
Shouqun Liu
2012/07/30 08:08:44
Done.
|
| + |
| + |
| +namespace content { |
| + |
| +class ShellJavaScriptDialog; |
|
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
not needed
Shouqun Liu
2012/07/30 08:08:44
Done.
|
| + |
| +ShellJavaScriptDialog::ShellJavaScriptDialog( |
| + ShellJavaScriptDialogCreator* creator, |
| + gfx::NativeWindow parent_window, |
| + JavaScriptMessageType message_type, |
| + const string16& message_text, |
| + const string16& default_prompt_text, |
| + const JavaScriptDialogCreator::DialogClosedCallback& callback) |
| + : creator_(creator), |
| + callback_(callback), |
| + parent_window_(parent_window) { |
| + GtkButtonsType buttons; |
| + GtkMessageType gtk_message_type = GTK_MESSAGE_OTHER; |
| + |
| + switch (message_type) { |
| + case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: |
| + buttons = GTK_BUTTONS_NONE; |
| + gtk_message_type = GTK_MESSAGE_WARNING; |
| + break; |
| + |
| + case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: |
| + buttons = GTK_BUTTONS_CANCEL; |
| + gtk_message_type = GTK_MESSAGE_QUESTION; |
| + break; |
| + |
| + case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: |
| + buttons = GTK_BUTTONS_CANCEL; |
| + gtk_message_type = GTK_MESSAGE_QUESTION; |
| + break; |
| + |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + gtk_dialog_ = gtk_message_dialog_new(parent_window_, |
|
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
if you need to wrap, either all parameters go on t
Shouqun Liu
2012/07/30 08:08:44
Fixed the wrap style
On 2012/07/30 07:32:23, joche
|
| + GTK_DIALOG_MODAL, gtk_message_type, buttons, "%s", |
| + UTF16ToUTF8(message_text).c_str()); |
| + g_signal_connect(gtk_dialog_, "delete-event", |
| + G_CALLBACK(gtk_widget_hide_on_delete), NULL); |
| + gtk_window_set_title(GTK_WINDOW(gtk_dialog_), "JavaScript"); |
| + |
| + GtkWidget* ok_button = gtk_dialog_add_button(GTK_DIALOG(gtk_dialog_), |
| + GTK_STOCK_OK, GTK_RESPONSE_OK); |
| + if (message_type != content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) |
| + gtk_widget_grab_focus(ok_button); |
| + |
| + // Adjust content area as needed. Set up the prompt text entry. |
|
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
nit. comments that just repeat what the code does
Shouqun Liu
2012/07/30 08:08:44
Done.
|
| + if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { |
| + GtkWidget* content_area = |
| + gtk_dialog_get_content_area(GTK_DIALOG(gtk_dialog_)); |
| + GtkWidget* text_box = gtk_entry_new(); |
| + gtk_entry_set_text(GTK_ENTRY(text_box), |
| + UTF16ToUTF8(default_prompt_text).c_str()); |
| + gtk_box_pack_start(GTK_BOX(content_area), text_box, TRUE, TRUE, 0); |
| + g_object_set_data(G_OBJECT(gtk_dialog_), kPromptTextId, text_box); |
| + gtk_entry_set_activates_default(GTK_ENTRY(text_box), TRUE); |
| + } |
| + |
| + gtk_dialog_set_default_response(GTK_DIALOG(gtk_dialog_), GTK_RESPONSE_OK); |
| + g_signal_connect(gtk_dialog_, "response", G_CALLBACK(OnResponseThunk), this); |
| + gtk_widget_show_all(GTK_WIDGET(gtk_dialog_)); |
| +} |
| + |
| +ShellJavaScriptDialog::~ShellJavaScriptDialog() { |
| +} |
| + |
| +void ShellJavaScriptDialog::Cancel() { |
| +} |
| + |
| +void ShellJavaScriptDialog::OnResponse(GtkWidget* dialog, int response_id) { |
| + switch (response_id) { |
| + case GTK_RESPONSE_OK: |
| + callback_.Run(true, GetPromptText(GTK_DIALOG(dialog))); |
| + break; |
| + case GTK_RESPONSE_CANCEL: |
| + case GTK_RESPONSE_DELETE_EVENT: |
| + callback_.Run(false, string16()); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + gtk_widget_destroy(dialog); |
| + |
| + creator_->DialogClosed(this); |
| +} |
| + |
| +} //namespace content |
|
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
nit. two spaces before // and one after
Shouqun Liu
2012/07/30 08:08:44
Done.
|