// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace AtkSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	internal delegate void PropertyChangeHandlerNative(IntPtr obj, IntPtr vals);

	internal class PropertyChangeHandlerInvoker {

		PropertyChangeHandlerNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

		~PropertyChangeHandlerInvoker ()
		{
			if (__notify == null)
				return;
			__notify (__data);
		}

		internal PropertyChangeHandlerInvoker (PropertyChangeHandlerNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal PropertyChangeHandlerInvoker (PropertyChangeHandlerNative native_cb, IntPtr data) : this (native_cb, data, null) {}

		internal PropertyChangeHandlerInvoker (PropertyChangeHandlerNative native_cb, IntPtr data, GLib.DestroyNotify notify)
		{
			this.native_cb = native_cb;
			__data = data;
			__notify = notify;
		}

		internal Atk.PropertyChangeHandler Handler {
			get {
				return new Atk.PropertyChangeHandler(InvokeNative);
			}
		}

		void InvokeNative (Atk.Object obj, Atk.PropertyValues vals)
		{
			IntPtr native_vals = GLib.Marshaller.StructureToPtrAlloc (vals);
			native_cb (obj == null ? IntPtr.Zero : obj.Handle, native_vals);
			Marshal.FreeHGlobal (native_vals);
		}
	}

	internal class PropertyChangeHandlerWrapper {

		public void NativeCallback (IntPtr obj, IntPtr vals)
		{
			try {
				managed (GLib.Object.GetObject(obj) as Atk.Object, Atk.PropertyValues.New (vals));
				if (release_on_call)
					gch.Free ();
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		bool release_on_call = false;
		GCHandle gch;

		public void PersistUntilCalled ()
		{
			release_on_call = true;
			gch = GCHandle.Alloc (this);
		}

		internal PropertyChangeHandlerNative NativeDelegate;
		Atk.PropertyChangeHandler managed;

		public PropertyChangeHandlerWrapper (Atk.PropertyChangeHandler managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new PropertyChangeHandlerNative (NativeCallback);
		}

		public static Atk.PropertyChangeHandler GetManagedDelegate (PropertyChangeHandlerNative native)
		{
			if (native == null)
				return null;
			PropertyChangeHandlerWrapper wrapper = (PropertyChangeHandlerWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
