// 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 TextBufferDeserializeFuncNative(IntPtr register_buffer, IntPtr content_buffer, IntPtr iter, byte[] data, UIntPtr length, bool create_tags, IntPtr user_data, out IntPtr error);

	internal class TextBufferDeserializeFuncInvoker {

		TextBufferDeserializeFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal TextBufferDeserializeFuncInvoker (TextBufferDeserializeFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal TextBufferDeserializeFuncInvoker (TextBufferDeserializeFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		bool InvokeNative (Gtk.TextBuffer register_buffer, Gtk.TextBuffer content_buffer, Gtk.TextIter iter, byte[] data, ulong length, bool create_tags)
		{
			IntPtr native_iter = GLib.Marshaller.StructureToPtrAlloc (iter);
			IntPtr error = IntPtr.Zero;
			bool __result = native_cb (register_buffer == null ? IntPtr.Zero : register_buffer.Handle, content_buffer == null ? IntPtr.Zero : content_buffer.Handle, native_iter, data, new UIntPtr (length), create_tags, __data, out error);
			Marshal.FreeHGlobal (native_iter);
			return __result;
		}
	}

	internal class TextBufferDeserializeFuncWrapper {

		public bool NativeCallback (IntPtr register_buffer, IntPtr content_buffer, IntPtr iter, byte[] data, UIntPtr length, bool create_tags, IntPtr user_data, out IntPtr error)
		{
			error = IntPtr.Zero;

			try {
				bool __ret = managed (GLib.Object.GetObject(register_buffer) as Gtk.TextBuffer, GLib.Object.GetObject(content_buffer) as Gtk.TextBuffer, Gtk.TextIter.New (iter), data, (ulong) length, create_tags);
				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 TextBufferDeserializeFuncNative NativeDelegate;
		Gtk.TextBufferDeserializeFunc managed;

		public TextBufferDeserializeFuncWrapper (Gtk.TextBufferDeserializeFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new TextBufferDeserializeFuncNative (NativeCallback);
		}

		public static Gtk.TextBufferDeserializeFunc GetManagedDelegate (TextBufferDeserializeFuncNative native)
		{
			if (native == null)
				return null;
			TextBufferDeserializeFuncWrapper wrapper = (TextBufferDeserializeFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
