| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/input_window_dialog.h" | 5 #include "chrome/browser/gtk/input_window_dialog_gtk.h" |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 | 6 |
| 9 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 10 #include "base/scoped_ptr.h" | |
| 11 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 12 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/gtk/gtk_util.h" | 10 #include "chrome/browser/gtk/gtk_util.h" |
| 14 | 11 |
| 15 class GtkInputWindowDialog : public InputWindowDialog { | 12 InputWindowDialogGtk::InputWindowDialogGtk(GtkWindow* parent, |
| 16 public: | |
| 17 // Creates a dialog. Takes ownership of |delegate|. | |
| 18 GtkInputWindowDialog(GtkWindow* parent, | |
| 19 const std::string& window_title, | |
| 20 const std::string& label, | |
| 21 const std::string& contents, | |
| 22 Delegate* delegate); | |
| 23 virtual ~GtkInputWindowDialog(); | |
| 24 | |
| 25 virtual void Show(); | |
| 26 virtual void Close(); | |
| 27 | |
| 28 private: | |
| 29 static void OnEntryChanged(GtkEditable* entry, | |
| 30 GtkInputWindowDialog* window); | |
| 31 | |
| 32 static void OnResponse(GtkDialog* dialog, int response_id, | |
| 33 GtkInputWindowDialog* window); | |
| 34 | |
| 35 static gboolean OnWindowDeleteEvent(GtkWidget* widget, | |
| 36 GdkEvent* event, | |
| 37 GtkInputWindowDialog* dialog); | |
| 38 | |
| 39 static void OnWindowDestroy(GtkWidget* widget, GtkInputWindowDialog* dialog); | |
| 40 | |
| 41 // The underlying gtk dialog window. | |
| 42 GtkWidget* dialog_; | |
| 43 | |
| 44 // The GtkEntry in this form. | |
| 45 GtkWidget* input_; | |
| 46 | |
| 47 // Our delegate. Consumes the window's output. | |
| 48 scoped_ptr<InputWindowDialog::Delegate> delegate_; | |
| 49 }; | |
| 50 | |
| 51 | |
| 52 GtkInputWindowDialog::GtkInputWindowDialog(GtkWindow* parent, | |
| 53 const std::string& window_title, | 13 const std::string& window_title, |
| 54 const std::string& label, | 14 const std::string& label, |
| 55 const std::string& contents, | 15 const std::string& contents, |
| 56 Delegate* delegate) | 16 Delegate* delegate) |
| 57 : dialog_(gtk_dialog_new_with_buttons( | 17 : dialog_(gtk_dialog_new_with_buttons( |
| 58 window_title.c_str(), | 18 window_title.c_str(), |
| 59 parent, | 19 parent, |
| 60 GTK_DIALOG_MODAL, | 20 GTK_DIALOG_MODAL, |
| 61 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, | 21 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, |
| 62 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, | 22 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, |
| 63 NULL)), | 23 NULL)), |
| 64 delegate_(delegate) { | 24 delegate_(delegate) { |
| 65 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); | 25 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); |
| 66 gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE); | 26 gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE); |
| 67 | 27 |
| 68 GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox; | 28 GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox; |
| 69 gtk_box_set_spacing(GTK_BOX(content_area), 18); | 29 gtk_box_set_spacing(GTK_BOX(content_area), 18); |
| 70 | 30 |
| 71 GtkWidget* hbox = gtk_hbox_new(FALSE, 6); | 31 GtkWidget* hbox = gtk_hbox_new(FALSE, 6); |
| 72 GtkWidget* label_widget = gtk_label_new(label.c_str()); | 32 GtkWidget* label_widget = gtk_label_new(label.c_str()); |
| 73 gtk_box_pack_start(GTK_BOX(hbox), label_widget, FALSE, FALSE, 0); | 33 gtk_box_pack_start(GTK_BOX(hbox), label_widget, FALSE, FALSE, 0); |
| 74 | 34 |
| 75 input_ = gtk_entry_new(); | 35 input_ = gtk_entry_new(); |
| 76 gtk_entry_set_text(GTK_ENTRY(input_), contents.c_str()); | 36 gtk_entry_set_text(GTK_ENTRY(input_), contents.c_str()); |
| 77 g_signal_connect(input_, "changed", | 37 g_signal_connect(input_, "changed", |
| 78 G_CALLBACK(OnEntryChanged), this); | 38 G_CALLBACK(OnEntryChangedThunk), this); |
| 79 g_object_set(G_OBJECT(input_), "activates-default", TRUE, NULL); | 39 g_object_set(G_OBJECT(input_), "activates-default", TRUE, NULL); |
| 80 gtk_box_pack_start(GTK_BOX(hbox), input_, TRUE, TRUE, 0); | 40 gtk_box_pack_start(GTK_BOX(hbox), input_, TRUE, TRUE, 0); |
| 81 | 41 |
| 82 gtk_widget_show_all(hbox); | 42 gtk_widget_show_all(hbox); |
| 83 | 43 |
| 84 gtk_box_pack_start(GTK_BOX(content_area), hbox, FALSE, FALSE, 0); | 44 gtk_box_pack_start(GTK_BOX(content_area), hbox, FALSE, FALSE, 0); |
| 85 | 45 |
| 86 g_signal_connect(dialog_, "response", | 46 g_signal_connect(dialog_, "response", |
| 87 G_CALLBACK(OnResponse), this); | 47 G_CALLBACK(OnResponseThunk), this); |
| 88 g_signal_connect(dialog_, "delete-event", | 48 g_signal_connect(dialog_, "delete-event", |
| 89 G_CALLBACK(OnWindowDeleteEvent), this); | 49 G_CALLBACK(OnWindowDeleteEventThunk), this); |
| 90 g_signal_connect(dialog_, "destroy", | 50 g_signal_connect(dialog_, "destroy", |
| 91 G_CALLBACK(OnWindowDestroy), this); | 51 G_CALLBACK(OnWindowDestroyThunk), this); |
| 92 } | 52 } |
| 93 | 53 |
| 94 GtkInputWindowDialog::~GtkInputWindowDialog() { | 54 InputWindowDialogGtk::~InputWindowDialogGtk() { |
| 95 } | 55 } |
| 96 | 56 |
| 97 void GtkInputWindowDialog::Show() { | 57 void InputWindowDialogGtk::Show() { |
| 98 gtk_util::ShowDialog(dialog_); | 58 gtk_util::ShowDialog(dialog_); |
| 99 } | 59 } |
| 100 | 60 |
| 101 void GtkInputWindowDialog::Close() { | 61 void InputWindowDialogGtk::Close() { |
| 102 // Under the model that we've inherited from Windows, dialogs can receive | 62 // Under the model that we've inherited from Windows, dialogs can receive |
| 103 // more than one Close() call inside the current message loop event. | 63 // more than one Close() call inside the current message loop event. |
| 104 if (dialog_) { | 64 if (dialog_) { |
| 105 gtk_widget_destroy(GTK_WIDGET(dialog_)); | 65 gtk_widget_destroy(GTK_WIDGET(dialog_)); |
| 106 dialog_ = NULL; | 66 dialog_ = NULL; |
| 107 } | 67 } |
| 108 } | 68 } |
| 109 | 69 |
| 110 // static | 70 void InputWindowDialogGtk::OnEntryChanged(GtkEditable* entry) { |
| 111 void GtkInputWindowDialog::OnEntryChanged(GtkEditable* entry, | |
| 112 GtkInputWindowDialog* window) { | |
| 113 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(entry)))); | 71 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(entry)))); |
| 114 gtk_dialog_set_response_sensitive(GTK_DIALOG(window->dialog_), | 72 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), |
| 115 GTK_RESPONSE_ACCEPT, | 73 GTK_RESPONSE_ACCEPT, |
| 116 window->delegate_->IsValid(value)); | 74 delegate_->IsValid(value)); |
| 117 } | 75 } |
| 118 | 76 |
| 119 // static | 77 void InputWindowDialogGtk::OnResponse(GtkWidget* dialog, int response_id) { |
| 120 void GtkInputWindowDialog::OnResponse(GtkDialog* dialog, int response_id, | |
| 121 GtkInputWindowDialog* window) { | |
| 122 if (response_id == GTK_RESPONSE_ACCEPT) { | 78 if (response_id == GTK_RESPONSE_ACCEPT) { |
| 123 std::wstring value(UTF8ToWide(gtk_entry_get_text( | 79 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(input_)))); |
| 124 GTK_ENTRY(window->input_)))); | 80 delegate_->InputAccepted(value); |
| 125 window->delegate_->InputAccepted(value); | |
| 126 } else { | 81 } else { |
| 127 window->delegate_->InputCanceled(); | 82 delegate_->InputCanceled(); |
| 128 } | 83 } |
| 129 | 84 |
| 130 window->Close(); | 85 Close(); |
| 131 } | 86 } |
| 132 | 87 |
| 133 // static | 88 gboolean InputWindowDialogGtk::OnWindowDeleteEvent(GtkWidget* widget, |
| 134 gboolean GtkInputWindowDialog::OnWindowDeleteEvent( | 89 GdkEvent* event) { |
| 135 GtkWidget* widget, | 90 Close(); |
| 136 GdkEvent* event, | |
| 137 GtkInputWindowDialog* dialog) { | |
| 138 dialog->Close(); | |
| 139 | 91 |
| 140 // Return true to prevent the gtk dialog from being destroyed. Close will | 92 // Return true to prevent the gtk dialog from being destroyed. Close will |
| 141 // destroy it for us and the default gtk_dialog_delete_event_handler() will | 93 // destroy it for us and the default gtk_dialog_delete_event_handler() will |
| 142 // force the destruction without us being able to stop it. | 94 // force the destruction without us being able to stop it. |
| 143 return TRUE; | 95 return TRUE; |
| 144 } | 96 } |
| 145 | 97 |
| 146 // static | 98 void InputWindowDialogGtk::OnWindowDestroy(GtkWidget* widget) { |
| 147 void GtkInputWindowDialog::OnWindowDestroy(GtkWidget* widget, | 99 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 148 GtkInputWindowDialog* dialog) { | |
| 149 MessageLoop::current()->DeleteSoon(FROM_HERE, dialog); | |
| 150 } | 100 } |
| 151 | 101 |
| 152 // static | 102 // static |
| 153 InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent, | 103 InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent, |
| 154 const std::wstring& window_title, | 104 const std::wstring& window_title, |
| 155 const std::wstring& label, | 105 const std::wstring& label, |
| 156 const std::wstring& contents, | 106 const std::wstring& contents, |
| 157 Delegate* delegate) { | 107 Delegate* delegate) { |
| 158 return new GtkInputWindowDialog(parent, | 108 return new InputWindowDialogGtk(parent, |
| 159 WideToUTF8(window_title), | 109 WideToUTF8(window_title), |
| 160 WideToUTF8(label), | 110 WideToUTF8(label), |
| 161 WideToUTF8(contents), | 111 WideToUTF8(contents), |
| 162 delegate); | 112 delegate); |
| 163 } | 113 } |
| OLD | NEW |