diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-16 19:17:55 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-16 19:17:55 +0000 |
commit | 75b841290867be611cb33719fbe267b0b2f9b362 (patch) | |
tree | 054ba514f71c6b7a0c44c8d8573bb8cf4c22bb6e /chrome/browser/gtk | |
parent | 20c116aafb9841c035115685d2fe7c876b2521ae (diff) | |
download | chromium_src-75b841290867be611cb33719fbe267b0b2f9b362.zip chromium_src-75b841290867be611cb33719fbe267b0b2f9b362.tar.gz chromium_src-75b841290867be611cb33719fbe267b0b2f9b362.tar.bz2 |
Refactor the implementation of InputWindowDialog.
- Move the implementation of each platform under its respective source directory.
- Rename the classes to match with pattern used in the chrome source.
- In the gtk side, use the gtk signal macros for the callback events.
BUG=None
TEST=open the bookmark editor dialog, everything should works as before on windows and linux.
Review URL: http://codereview.chromium.org/2832028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52721 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gtk')
-rw-r--r-- | chrome/browser/gtk/input_window_dialog_gtk.cc | 113 | ||||
-rw-r--r-- | chrome/browser/gtk/input_window_dialog_gtk.h | 49 |
2 files changed, 162 insertions, 0 deletions
diff --git a/chrome/browser/gtk/input_window_dialog_gtk.cc b/chrome/browser/gtk/input_window_dialog_gtk.cc new file mode 100644 index 0000000..2a7e2d0 --- /dev/null +++ b/chrome/browser/gtk/input_window_dialog_gtk.cc @@ -0,0 +1,113 @@ +// Copyright (c) 2010 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 "chrome/browser/gtk/input_window_dialog_gtk.h" + +#include "base/message_loop.h" +#include "base/string_piece.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/gtk/gtk_util.h" + +InputWindowDialogGtk::InputWindowDialogGtk(GtkWindow* parent, + const std::string& window_title, + const std::string& label, + const std::string& contents, + Delegate* delegate) + : dialog_(gtk_dialog_new_with_buttons( + window_title.c_str(), + parent, + GTK_DIALOG_MODAL, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + NULL)), + delegate_(delegate) { + gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); + gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE); + + GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox; + gtk_box_set_spacing(GTK_BOX(content_area), 18); + + GtkWidget* hbox = gtk_hbox_new(FALSE, 6); + GtkWidget* label_widget = gtk_label_new(label.c_str()); + gtk_box_pack_start(GTK_BOX(hbox), label_widget, FALSE, FALSE, 0); + + input_ = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(input_), contents.c_str()); + g_signal_connect(input_, "changed", + G_CALLBACK(OnEntryChangedThunk), this); + g_object_set(G_OBJECT(input_), "activates-default", TRUE, NULL); + gtk_box_pack_start(GTK_BOX(hbox), input_, TRUE, TRUE, 0); + + gtk_widget_show_all(hbox); + + gtk_box_pack_start(GTK_BOX(content_area), hbox, FALSE, FALSE, 0); + + g_signal_connect(dialog_, "response", + G_CALLBACK(OnResponseThunk), this); + g_signal_connect(dialog_, "delete-event", + G_CALLBACK(OnWindowDeleteEventThunk), this); + g_signal_connect(dialog_, "destroy", + G_CALLBACK(OnWindowDestroyThunk), this); +} + +InputWindowDialogGtk::~InputWindowDialogGtk() { +} + +void InputWindowDialogGtk::Show() { + gtk_util::ShowDialog(dialog_); +} + +void InputWindowDialogGtk::Close() { + // Under the model that we've inherited from Windows, dialogs can receive + // more than one Close() call inside the current message loop event. + if (dialog_) { + gtk_widget_destroy(GTK_WIDGET(dialog_)); + dialog_ = NULL; + } +} + +void InputWindowDialogGtk::OnEntryChanged(GtkEditable* entry) { + std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(entry)))); + gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), + GTK_RESPONSE_ACCEPT, + delegate_->IsValid(value)); +} + +void InputWindowDialogGtk::OnResponse(GtkWidget* dialog, int response_id) { + if (response_id == GTK_RESPONSE_ACCEPT) { + std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(input_)))); + delegate_->InputAccepted(value); + } else { + delegate_->InputCanceled(); + } + + Close(); +} + +gboolean InputWindowDialogGtk::OnWindowDeleteEvent(GtkWidget* widget, + GdkEvent* event) { + Close(); + + // Return true to prevent the gtk dialog from being destroyed. Close will + // destroy it for us and the default gtk_dialog_delete_event_handler() will + // force the destruction without us being able to stop it. + return TRUE; +} + +void InputWindowDialogGtk::OnWindowDestroy(GtkWidget* widget) { + MessageLoop::current()->DeleteSoon(FROM_HERE, this); +} + +// static +InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent, + const std::wstring& window_title, + const std::wstring& label, + const std::wstring& contents, + Delegate* delegate) { + return new InputWindowDialogGtk(parent, + WideToUTF8(window_title), + WideToUTF8(label), + WideToUTF8(contents), + delegate); +} diff --git a/chrome/browser/gtk/input_window_dialog_gtk.h b/chrome/browser/gtk/input_window_dialog_gtk.h new file mode 100644 index 0000000..7f7b736c --- /dev/null +++ b/chrome/browser/gtk/input_window_dialog_gtk.h @@ -0,0 +1,49 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_GTK_INPUT_WINDOW_DIALOG_GTK_H_ +#define CHROME_BROWSER_GTK_INPUT_WINDOW_DIALOG_GTK_H_ + +#include <gtk/gtk.h> + +#include "app/gtk_signal.h" +#include "base/basictypes.h" +#include "base/scoped_ptr.h" +#include "chrome/browser/input_window_dialog.h" + +class InputWindowDialogGtk : public InputWindowDialog { + public: + // Creates a dialog. Takes ownership of |delegate|. + InputWindowDialogGtk(GtkWindow* parent, + const std::string& window_title, + const std::string& label, + const std::string& contents, + Delegate* delegate); + virtual ~InputWindowDialogGtk(); + + virtual void Show(); + virtual void Close(); + + private: + CHROMEG_CALLBACK_0(InputWindowDialogGtk, void, OnEntryChanged, GtkEditable*); + + CHROMEGTK_CALLBACK_1(InputWindowDialogGtk, void, OnResponse, int); + + CHROMEGTK_CALLBACK_1(InputWindowDialogGtk, gboolean, + OnWindowDeleteEvent, GdkEvent*); + + CHROMEGTK_CALLBACK_0(InputWindowDialogGtk, void, OnWindowDestroy); + + // The underlying gtk dialog window. + GtkWidget* dialog_; + + // The GtkEntry in this form. + GtkWidget* input_; + + // Our delegate. Consumes the window's output. + scoped_ptr<InputWindowDialog::Delegate> delegate_; + + DISALLOW_COPY_AND_ASSIGN(InputWindowDialogGtk); +}; +#endif // CHROME_BROWSER_GTK_INPUT_WINDOW_DIALOG_GTK_H_ |