// 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 void MenuTrackerInsertFuncNative(IntPtr item, int position, IntPtr user_data);

	internal class MenuTrackerInsertFuncInvoker {

		MenuTrackerInsertFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal MenuTrackerInsertFuncInvoker (MenuTrackerInsertFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal MenuTrackerInsertFuncInvoker (MenuTrackerInsertFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		void InvokeNative (Gtk.MenuTrackerItem item, int position)
		{
			native_cb (item == null ? IntPtr.Zero : item.Handle, position, __data);
		}
	}

	internal class MenuTrackerInsertFuncWrapper {

		public void NativeCallback (IntPtr item, int position, IntPtr user_data)
		{
			try {
				managed (GLib.Object.GetObject(item) as Gtk.MenuTrackerItem, position);
				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 MenuTrackerInsertFuncNative NativeDelegate;
		Gtk.MenuTrackerInsertFunc managed;

		public MenuTrackerInsertFuncWrapper (Gtk.MenuTrackerInsertFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new MenuTrackerInsertFuncNative (NativeCallback);
		}

		public static Gtk.MenuTrackerInsertFunc GetManagedDelegate (MenuTrackerInsertFuncNative native)
		{
			if (native == null)
				return null;
			MenuTrackerInsertFuncWrapper wrapper = (MenuTrackerInsertFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
