diff options
author | erg@chromium.org <erg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-08 21:28:04 +0000 |
---|---|---|
committer | erg@chromium.org <erg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-08 21:28:04 +0000 |
commit | 284f766950396abce8f0672c2ccde6b45de25578 (patch) | |
tree | a711fe12c1c3ac2aa83e1392f667cdff8b07221f /app/gtk_signal_registrar.h | |
parent | b3f2f32e4cdf73df0073930cef7b4d3531df8e5d (diff) | |
download | chromium_src-284f766950396abce8f0672c2ccde6b45de25578.zip chromium_src-284f766950396abce8f0672c2ccde6b45de25578.tar.gz chromium_src-284f766950396abce8f0672c2ccde6b45de25578.tar.bz2 |
GTK: Minimize usage of gtk headers.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/2891006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51898 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/gtk_signal_registrar.h')
-rw-r--r-- | app/gtk_signal_registrar.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/app/gtk_signal_registrar.h b/app/gtk_signal_registrar.h new file mode 100644 index 0000000..3f14dbe --- /dev/null +++ b/app/gtk_signal_registrar.h @@ -0,0 +1,66 @@ +// 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 APP_GTK_SIGNAL_REGISTRAR_H_ +#define APP_GTK_SIGNAL_REGISTRAR_H_ + +#include <glib.h> +#include <map> +#include <vector> + +#include "base/basictypes.h" + +typedef void (*GCallback) (void); +typedef struct _GObject GObject; +typedef struct _GtkWidget GtkWidget; + +// A class that ensures that callbacks don't run on stale owner objects. Similar +// in spirit to NotificationRegistrar. Use as follows: +// +// class ChromeObject { +// public: +// ChromeObject() { +// ... +// +// signals_.Connect(widget, "event", CallbackThunk, this); +// } +// +// ... +// +// private: +// GtkSignalRegistrar signals_; +// }; +// +// When |signals_| goes down, it will disconnect the handlers connected via +// Connect. +class GtkSignalRegistrar { + public: + GtkSignalRegistrar(); + ~GtkSignalRegistrar(); + + // Connect before the default handler. Returns the handler id. + glong Connect(gpointer instance, const gchar* detailed_signal, + GCallback signal_handler, gpointer data); + // Connect after the default handler. Returns the handler id. + glong ConnectAfter(gpointer instance, const gchar* detailed_signal, + GCallback signal_handler, gpointer data); + + private: + static void WeakNotifyThunk(gpointer data, GObject* where_the_object_was) { + reinterpret_cast<GtkSignalRegistrar*>(data)->WeakNotify( + where_the_object_was); + } + void WeakNotify(GObject* where_the_object_was); + + glong ConnectInternal(gpointer instance, const gchar* detailed_signal, + GCallback signal_handler, gpointer data, bool after); + + typedef std::vector<glong> HandlerList; + typedef std::map<GObject*, HandlerList> HandlerMap; + HandlerMap handler_lists_; + + DISALLOW_COPY_AND_ASSIGN(GtkSignalRegistrar); +}; + +#endif // APP_GTK_SIGNAL_REGISTRAR_H_ |