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

namespace PangoSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	internal delegate void CairoShapeRendererFuncNative(IntPtr cr, IntPtr attr, bool do_path, IntPtr data);

	internal class CairoShapeRendererFuncInvoker {

		CairoShapeRendererFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal CairoShapeRendererFuncInvoker (CairoShapeRendererFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal CairoShapeRendererFuncInvoker (CairoShapeRendererFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

		internal Pango.CairoShapeRendererFunc Handler {
			get {
				return new Pango.CairoShapeRendererFunc(InvokeNative);
			}
		}

		void InvokeNative (Cairo.Context cr, Pango.Attribute attr, bool do_path)
		{
			native_cb (cr == null ? IntPtr.Zero : cr.Handle, attr.Handle, do_path, __data);
		}
	}

	internal class CairoShapeRendererFuncWrapper {

		public void NativeCallback (IntPtr cr, IntPtr attr, bool do_path, IntPtr data)
		{
			Cairo.Context mycr = null;

			try {
				mycr = new Cairo.Context (cr, false);

				managed (mycr, Pango.Attribute.GetAttribute (attr), do_path);
				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 CairoShapeRendererFuncNative NativeDelegate;
		Pango.CairoShapeRendererFunc managed;

		public CairoShapeRendererFuncWrapper (Pango.CairoShapeRendererFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new CairoShapeRendererFuncNative (NativeCallback);
		}

		public static Pango.CairoShapeRendererFunc GetManagedDelegate (CairoShapeRendererFuncNative native)
		{
			if (native == null)
				return null;
			CairoShapeRendererFuncWrapper wrapper = (CairoShapeRendererFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
