// 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 TreeSelectionFuncNative(IntPtr selection, IntPtr model, IntPtr path, bool path_currently_selected, IntPtr data);

	internal class TreeSelectionFuncInvoker {

		TreeSelectionFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal TreeSelectionFuncInvoker (TreeSelectionFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal TreeSelectionFuncInvoker (TreeSelectionFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		bool InvokeNative (Gtk.TreeSelection selection, Gtk.ITreeModel model, Gtk.TreePath path, bool path_currently_selected)
		{
			bool __result = native_cb (selection == null ? IntPtr.Zero : selection.Handle, model == null ? IntPtr.Zero : ((model is GLib.Object) ? (model as GLib.Object).Handle : (model as Gtk.TreeModelAdapter).Handle), path == null ? IntPtr.Zero : path.Handle, path_currently_selected, __data);
			return __result;
		}
	}

	internal class TreeSelectionFuncWrapper {

		public bool NativeCallback (IntPtr selection, IntPtr model, IntPtr path, bool path_currently_selected, IntPtr data)
		{
			try {
				bool __ret = managed (GLib.Object.GetObject(selection) as Gtk.TreeSelection, Gtk.TreeModelAdapter.GetObject (model, false), path == IntPtr.Zero ? null : (Gtk.TreePath) GLib.Opaque.GetOpaque (path, typeof (Gtk.TreePath), false), path_currently_selected);
				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 TreeSelectionFuncNative NativeDelegate;
		Gtk.TreeSelectionFunc managed;

		public TreeSelectionFuncWrapper (Gtk.TreeSelectionFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new TreeSelectionFuncNative (NativeCallback);
		}

		public static Gtk.TreeSelectionFunc GetManagedDelegate (TreeSelectionFuncNative native)
		{
			if (native == null)
				return null;
			TreeSelectionFuncWrapper wrapper = (TreeSelectionFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
