GtkSharp/gtk/Object.custom
Mike Kestner 2469ba16ef 2007-02-16 Mike Kestner <mkestner@novell.com>
* glib/Object.cs : switch to ToggleRefs for all items created with
	CreateNativeObject.  This gets all managed subclasses, with a little
	overhang into simple wrappers.
	* glib/ToggleRef.cs : new class to manage the weak to strong ref 
	transitions as a native object flips between shared and unshared
	ownership.
	* gtk/Object.custom : revamp of the Destroyed signal handling. 
	* gtk/Gtk.metadata : hide destroy signal so we can deal with it 
	manually. [Fixes the reopen note of #72018.]

svn path=/trunk/gtk-sharp/; revision=73023
2007-02-16 16:18:59 +00:00

144 lines
3.8 KiB
Plaintext
Executable file

// Gtk.Object.custom - Gtk Object class customizations
//
// Author: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2002-2003 Mike Kestner
// Copyright (c) 2007 Novell, Inc.
//
// This code is inserted after the automatically generated code.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
static Hashtable destroy_handlers;
static Hashtable DestroyHandlers {
get {
if (destroy_handlers == null)
destroy_handlers = new Hashtable ();
return destroy_handlers;
}
}
private static void OverrideDestroyed (GLib.GType gtype)
{
// Do Nothing. We don't want to hook into the native vtable.
// We will manually invoke the VM on signal invocation. The signal
// always raises before the default handler because this signal
// is RUN_CLEANUP.
}
[GLib.DefaultSignalHandler(Type=typeof(Gtk.Object), ConnectionMethod="OverrideDestroyed")]
protected virtual void OnDestroyed ()
{
if (DestroyHandlers [Handle] == null)
return;
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
handler (this, EventArgs.Empty);
DestroyHandlers [Handle] = null;
Dispose ();
}
[GLib.Signal("destroy")]
public event EventHandler Destroyed {
add {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
DestroyHandlers [Handle] = Delegate.Combine (handler, value);
}
remove {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
DestroyHandlers [Handle] = Delegate.Remove (handler, value);
}
}
event EventHandler InternalDestroyed {
add {
GLib.Signal sig = GLib.Signal.Lookup (this, "destroy");
sig.AddDelegate (value);
}
remove {
GLib.Signal sig = GLib.Signal.Lookup (this, "destroy");
sig.RemoveDelegate (value);
}
}
static void NativeDestroy (object o, EventArgs args)
{
Gtk.Object obj = o as Gtk.Object;
if (obj == null)
return;
obj.OnDestroyed ();
}
static EventHandler native_destroy_handler;
static EventHandler NativeDestroyHandler {
get {
if (native_destroy_handler == null)
native_destroy_handler = new EventHandler (NativeDestroy);
return native_destroy_handler;
}
}
protected override void CreateNativeObject (string[] names, GLib.Value[] vals)
{
base.CreateNativeObject (names, vals);
}
public override void Dispose ()
{
InternalDestroyed -= NativeDestroyHandler;
base.Dispose ();
}
[DllImport("libgobject-2.0-0.dll")]
private static extern void g_object_ref_sink (IntPtr raw);
protected override IntPtr Raw {
get {
return base.Raw;
}
set {
base.Raw = value;
if (value == IntPtr.Zero)
return;
g_object_ref_sink (value);
InternalDestroyed += NativeDestroyHandler;
}
}
[DllImport("libgtk-win32-2.0-0.dll")]
private static extern void gtk_object_destroy (IntPtr raw);
public virtual void Destroy ()
{
gtk_object_destroy (Handle);
}
[DllImport("gtksharpglue-2")]
private static extern bool gtksharp_object_is_floating (IntPtr raw);
[DllImport("gtksharpglue-2")]
private static extern bool gtksharp_object_set_floating (IntPtr raw, bool val);
public bool IsFloating {
get {
return gtksharp_object_is_floating (Handle);
}
set {
gtksharp_object_set_floating (Handle, value);
}
}