| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/gtk/input_window_dialog_gtk.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/string_piece.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/gtk/gtk_util.h" | |
| 11 | |
| 12 InputWindowDialogGtk::InputWindowDialogGtk(GtkWindow* parent, | |
| 13 const std::string& window_title, | |
| 14 const std::string& label, | |
| 15 const std::string& contents, | |
| 16 Delegate* delegate) | |
| 17 : dialog_(gtk_dialog_new_with_buttons( | |
| 18 window_title.c_str(), | |
| 19 parent, | |
| 20 GTK_DIALOG_MODAL, | |
| 21 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, | |
| 22 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, | |
| 23 NULL)), | |
| 24 delegate_(delegate) { | |
| 25 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); | |
| 26 gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE); | |
| 27 | |
| 28 GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox; | |
| 29 gtk_box_set_spacing(GTK_BOX(content_area), 18); | |
| 30 | |
| 31 GtkWidget* hbox = gtk_hbox_new(FALSE, 6); | |
| 32 GtkWidget* label_widget = gtk_label_new(label.c_str()); | |
| 33 gtk_box_pack_start(GTK_BOX(hbox), label_widget, FALSE, FALSE, 0); | |
| 34 | |
| 35 input_ = gtk_entry_new(); | |
| 36 gtk_entry_set_text(GTK_ENTRY(input_), contents.c_str()); | |
| 37 g_signal_connect(input_, "changed", | |
| 38 G_CALLBACK(OnEntryChangedThunk), this); | |
| 39 g_object_set(G_OBJECT(input_), "activates-default", TRUE, NULL); | |
| 40 gtk_box_pack_start(GTK_BOX(hbox), input_, TRUE, TRUE, 0); | |
| 41 | |
| 42 gtk_widget_show_all(hbox); | |
| 43 | |
| 44 gtk_box_pack_start(GTK_BOX(content_area), hbox, FALSE, FALSE, 0); | |
| 45 | |
| 46 g_signal_connect(dialog_, "response", | |
| 47 G_CALLBACK(OnResponseThunk), this); | |
| 48 g_signal_connect(dialog_, "delete-event", | |
| 49 G_CALLBACK(OnWindowDeleteEventThunk), this); | |
| 50 g_signal_connect(dialog_, "destroy", | |
| 51 G_CALLBACK(OnWindowDestroyThunk), this); | |
| 52 } | |
| 53 | |
| 54 InputWindowDialogGtk::~InputWindowDialogGtk() { | |
| 55 } | |
| 56 | |
| 57 void InputWindowDialogGtk::Show() { | |
| 58 gtk_util::ShowDialog(dialog_); | |
| 59 } | |
| 60 | |
| 61 void InputWindowDialogGtk::Close() { | |
| 62 // Under the model that we've inherited from Windows, dialogs can receive | |
| 63 // more than one Close() call inside the current message loop event. | |
| 64 if (dialog_) { | |
| 65 gtk_widget_destroy(GTK_WIDGET(dialog_)); | |
| 66 dialog_ = NULL; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void InputWindowDialogGtk::OnEntryChanged(GtkEditable* entry) { | |
| 71 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(entry)))); | |
| 72 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), | |
| 73 GTK_RESPONSE_ACCEPT, | |
| 74 delegate_->IsValid(value)); | |
| 75 } | |
| 76 | |
| 77 void InputWindowDialogGtk::OnResponse(GtkWidget* dialog, int response_id) { | |
| 78 if (response_id == GTK_RESPONSE_ACCEPT) { | |
| 79 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(input_)))); | |
| 80 delegate_->InputAccepted(value); | |
| 81 } else { | |
| 82 delegate_->InputCanceled(); | |
| 83 } | |
| 84 | |
| 85 Close(); | |
| 86 } | |
| 87 | |
| 88 gboolean InputWindowDialogGtk::OnWindowDeleteEvent(GtkWidget* widget, | |
| 89 GdkEvent* event) { | |
| 90 Close(); | |
| 91 | |
| 92 // Return true to prevent the gtk dialog from being destroyed. Close will | |
| 93 // destroy it for us and the default gtk_dialog_delete_event_handler() will | |
| 94 // force the destruction without us being able to stop it. | |
| 95 return TRUE; | |
| 96 } | |
| 97 | |
| 98 void InputWindowDialogGtk::OnWindowDestroy(GtkWidget* widget) { | |
| 99 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 100 } | |
| 101 | |
| 102 // static | |
| 103 InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent, | |
| 104 const std::wstring& window_title, | |
| 105 const std::wstring& label, | |
| 106 const std::wstring& contents, | |
| 107 Delegate* delegate) { | |
| 108 return new InputWindowDialogGtk(parent, | |
| 109 WideToUTF8(window_title), | |
| 110 WideToUTF8(label), | |
| 111 WideToUTF8(contents), | |
| 112 delegate); | |
| 113 } | |
| OLD | NEW |