// 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 ClipboardTextReceivedFuncNative(IntPtr clipboard, IntPtr text, IntPtr data);

	internal class ClipboardTextReceivedFuncInvoker {

		ClipboardTextReceivedFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal ClipboardTextReceivedFuncInvoker (ClipboardTextReceivedFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal ClipboardTextReceivedFuncInvoker (ClipboardTextReceivedFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		void InvokeNative (Gtk.Clipboard clipboard, string text)
		{
			IntPtr native_text = GLib.Marshaller.StringToPtrGStrdup (text);
			native_cb (clipboard == null ? IntPtr.Zero : clipboard.Handle, native_text, __data);
			GLib.Marshaller.Free (native_text);
		}
	}

	internal class ClipboardTextReceivedFuncWrapper {

		public void NativeCallback (IntPtr clipboard, IntPtr text, IntPtr data)
		{
			try {
				managed (GLib.Object.GetObject(clipboard) as Gtk.Clipboard, GLib.Marshaller.Utf8PtrToString (text));
				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 ClipboardTextReceivedFuncNative NativeDelegate;
		Gtk.ClipboardTextReceivedFunc managed;

		public ClipboardTextReceivedFuncWrapper (Gtk.ClipboardTextReceivedFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new ClipboardTextReceivedFuncNative (NativeCallback);
		}

		public static Gtk.ClipboardTextReceivedFunc GetManagedDelegate (ClipboardTextReceivedFuncNative native)
		{
			if (native == null)
				return null;
			ClipboardTextReceivedFuncWrapper wrapper = (ClipboardTextReceivedFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
