Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / shell / browser / shell_javascript_dialog_gtk.cc
blob87bd6f3124b556096f022142c4902c7ff4d4ff10
1 // Copyright 2013 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.
5 #include "content/shell/browser/shell_javascript_dialog.h"
7 #include <gtk/gtk.h>
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/shell/app/resource.h"
13 #include "content/shell/browser/shell.h"
14 #include "content/shell/browser/shell_javascript_dialog_manager.h"
16 namespace {
18 const char kPromptTextId[] = "content_shell_prompt_text";
20 // If there's a text entry in the dialog, get the text from the first one and
21 // return it.
22 string16 GetPromptText(GtkDialog* dialog) {
23 GtkWidget* widget = static_cast<GtkWidget*>(
24 g_object_get_data(G_OBJECT(dialog), kPromptTextId));
25 if (widget)
26 return UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(widget)));
27 return string16();
30 } // namespace
33 namespace content {
35 ShellJavaScriptDialog::ShellJavaScriptDialog(
36 ShellJavaScriptDialogManager* manager,
37 gfx::NativeWindow parent_window,
38 JavaScriptMessageType message_type,
39 const string16& message_text,
40 const string16& default_prompt_text,
41 const JavaScriptDialogManager::DialogClosedCallback& callback)
42 : manager_(manager),
43 callback_(callback),
44 parent_window_(parent_window) {
45 GtkButtonsType buttons = GTK_BUTTONS_NONE;
46 GtkMessageType gtk_message_type = GTK_MESSAGE_OTHER;
48 switch (message_type) {
49 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT:
50 buttons = GTK_BUTTONS_NONE;
51 gtk_message_type = GTK_MESSAGE_WARNING;
52 break;
54 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM:
55 buttons = GTK_BUTTONS_CANCEL;
56 gtk_message_type = GTK_MESSAGE_QUESTION;
57 break;
59 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT:
60 buttons = GTK_BUTTONS_CANCEL;
61 gtk_message_type = GTK_MESSAGE_QUESTION;
62 break;
64 default:
65 NOTREACHED();
68 gtk_dialog_ = gtk_message_dialog_new(parent_window_,
69 GTK_DIALOG_MODAL,
70 gtk_message_type,
71 buttons,
72 "%s",
73 UTF16ToUTF8(message_text).c_str());
74 g_signal_connect(gtk_dialog_,
75 "delete-event",
76 G_CALLBACK(gtk_widget_hide_on_delete),
77 NULL);
78 gtk_window_set_title(GTK_WINDOW(gtk_dialog_), "JavaScript");
80 GtkWidget* ok_button = gtk_dialog_add_button(GTK_DIALOG(gtk_dialog_),
81 GTK_STOCK_OK,
82 GTK_RESPONSE_OK);
84 if (message_type != content::JAVASCRIPT_MESSAGE_TYPE_PROMPT)
85 gtk_widget_grab_focus(ok_button);
87 if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) {
88 GtkWidget* content_area =
89 gtk_dialog_get_content_area(GTK_DIALOG(gtk_dialog_));
90 GtkWidget* text_box = gtk_entry_new();
91 gtk_entry_set_text(GTK_ENTRY(text_box),
92 UTF16ToUTF8(default_prompt_text).c_str());
93 gtk_box_pack_start(GTK_BOX(content_area), text_box, TRUE, TRUE, 0);
94 g_object_set_data(G_OBJECT(gtk_dialog_), kPromptTextId, text_box);
95 gtk_entry_set_activates_default(GTK_ENTRY(text_box), TRUE);
98 gtk_dialog_set_default_response(GTK_DIALOG(gtk_dialog_), GTK_RESPONSE_OK);
99 g_signal_connect(gtk_dialog_, "response", G_CALLBACK(OnResponseThunk), this);
100 gtk_widget_show_all(GTK_WIDGET(gtk_dialog_));
103 ShellJavaScriptDialog::~ShellJavaScriptDialog() {
106 void ShellJavaScriptDialog::Cancel() {
109 void ShellJavaScriptDialog::OnResponse(GtkWidget* dialog, int response_id) {
110 switch (response_id) {
111 case GTK_RESPONSE_OK:
112 callback_.Run(true, GetPromptText(GTK_DIALOG(dialog)));
113 break;
114 case GTK_RESPONSE_CANCEL:
115 case GTK_RESPONSE_DELETE_EVENT:
116 callback_.Run(false, string16());
117 break;
118 default:
119 NOTREACHED();
122 gtk_widget_destroy(dialog);
124 manager_->DialogClosed(this);
127 } // namespace content