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

namespace GtkSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	internal delegate bool CellCallbackNative(IntPtr renderer, IntPtr data);

	internal class CellCallbackInvoker {

		CellCallbackNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal CellCallbackInvoker (CellCallbackNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal CellCallbackInvoker (CellCallbackNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

		internal Gtk.CellCallback Handler {
			get {
				return new Gtk.CellCallback(InvokeNative);
			}
		}

		bool InvokeNative (Gtk.CellRenderer renderer)
		{
			bool __result = native_cb (renderer == null ? IntPtr.Zero : renderer.Handle, __data);
			return __result;
		}
	}

	internal class CellCallbackWrapper {

		public bool NativeCallback (IntPtr renderer, IntPtr data)
		{
			try {
				bool __ret = managed (GLib.Object.GetObject(renderer) as Gtk.CellRenderer);
				if (release_on_call)
					gch.Free ();
				return __ret;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
				return false;
			}
		}

		bool release_on_call = false;
		GCHandle gch;

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

		internal CellCallbackNative NativeDelegate;
		Gtk.CellCallback managed;

		public CellCallbackWrapper (Gtk.CellCallback managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new CellCallbackNative (NativeCallback);
		}

		public static Gtk.CellCallback GetManagedDelegate (CellCallbackNative native)
		{
			if (native == null)
				return null;
			CellCallbackWrapper wrapper = (CellCallbackWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
