OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "content/shell/shell_javascript_dialog.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/string_util.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "content/shell/resource.h" | |
13 #include "content/shell/shell.h" | |
14 #include "content/shell/shell_javascript_dialog_creator.h" | |
15 | |
16 namespace { | |
17 | |
18 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
| |
19 | |
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(); | |
28 } | |
29 | |
30 } // 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.
| |
31 | |
32 | |
33 namespace content { | |
34 | |
35 class ShellJavaScriptDialog; | |
jochen (gone - plz use gerrit)
2012/07/30 07:32:23
not needed
Shouqun Liu
2012/07/30 08:08:44
Done.
| |
36 | |
37 ShellJavaScriptDialog::ShellJavaScriptDialog( | |
38 ShellJavaScriptDialogCreator* creator, | |
39 gfx::NativeWindow parent_window, | |
40 JavaScriptMessageType message_type, | |
41 const string16& message_text, | |
42 const string16& default_prompt_text, | |
43 const JavaScriptDialogCreator::DialogClosedCallback& callback) | |
44 : creator_(creator), | |
45 callback_(callback), | |
46 parent_window_(parent_window) { | |
47 GtkButtonsType buttons; | |
48 GtkMessageType gtk_message_type = GTK_MESSAGE_OTHER; | |
49 | |
50 switch (message_type) { | |
51 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: | |
52 buttons = GTK_BUTTONS_NONE; | |
53 gtk_message_type = GTK_MESSAGE_WARNING; | |
54 break; | |
55 | |
56 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: | |
57 buttons = GTK_BUTTONS_CANCEL; | |
58 gtk_message_type = GTK_MESSAGE_QUESTION; | |
59 break; | |
60 | |
61 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: | |
62 buttons = GTK_BUTTONS_CANCEL; | |
63 gtk_message_type = GTK_MESSAGE_QUESTION; | |
64 break; | |
65 | |
66 default: | |
67 NOTREACHED(); | |
68 } | |
69 | |
70 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
| |
71 GTK_DIALOG_MODAL, gtk_message_type, buttons, "%s", | |
72 UTF16ToUTF8(message_text).c_str()); | |
73 g_signal_connect(gtk_dialog_, "delete-event", | |
74 G_CALLBACK(gtk_widget_hide_on_delete), NULL); | |
75 gtk_window_set_title(GTK_WINDOW(gtk_dialog_), "JavaScript"); | |
76 | |
77 GtkWidget* ok_button = gtk_dialog_add_button(GTK_DIALOG(gtk_dialog_), | |
78 GTK_STOCK_OK, GTK_RESPONSE_OK); | |
79 if (message_type != content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) | |
80 gtk_widget_grab_focus(ok_button); | |
81 | |
82 // 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.
| |
83 if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { | |
84 GtkWidget* content_area = | |
85 gtk_dialog_get_content_area(GTK_DIALOG(gtk_dialog_)); | |
86 GtkWidget* text_box = gtk_entry_new(); | |
87 gtk_entry_set_text(GTK_ENTRY(text_box), | |
88 UTF16ToUTF8(default_prompt_text).c_str()); | |
89 gtk_box_pack_start(GTK_BOX(content_area), text_box, TRUE, TRUE, 0); | |
90 g_object_set_data(G_OBJECT(gtk_dialog_), kPromptTextId, text_box); | |
91 gtk_entry_set_activates_default(GTK_ENTRY(text_box), TRUE); | |
92 } | |
93 | |
94 gtk_dialog_set_default_response(GTK_DIALOG(gtk_dialog_), GTK_RESPONSE_OK); | |
95 g_signal_connect(gtk_dialog_, "response", G_CALLBACK(OnResponseThunk), this); | |
96 gtk_widget_show_all(GTK_WIDGET(gtk_dialog_)); | |
97 } | |
98 | |
99 ShellJavaScriptDialog::~ShellJavaScriptDialog() { | |
100 } | |
101 | |
102 void ShellJavaScriptDialog::Cancel() { | |
103 } | |
104 | |
105 void ShellJavaScriptDialog::OnResponse(GtkWidget* dialog, int response_id) { | |
106 switch (response_id) { | |
107 case GTK_RESPONSE_OK: | |
108 callback_.Run(true, GetPromptText(GTK_DIALOG(dialog))); | |
109 break; | |
110 case GTK_RESPONSE_CANCEL: | |
111 case GTK_RESPONSE_DELETE_EVENT: | |
112 callback_.Run(false, string16()); | |
113 break; | |
114 default: | |
115 NOTREACHED(); | |
116 } | |
117 | |
118 gtk_widget_destroy(dialog); | |
119 | |
120 creator_->DialogClosed(this); | |
121 } | |
122 | |
123 } //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.
| |
OLD | NEW |