GtkSharp/gapi/GTypeExtensions.cs
Mike Kestner 7db633218f 2009-08-07 Mike Kestner <mkestner@novell.com>
* configure.in.in: add new dir and autofu for it.
	* Makefile.am: add new dir
	* gapi/*: a small extension method library to add generation-related
	api to GType in 2.0. Access it with -pkg:gapi-2.0-compat to pick up
	the needed refs.
	* generator/GObjectVM.cs: generate new GType getter methods.
	* generator/ObjectGen.cs: generate new GType getter methods.
	* glib/GType.cs: change a few props to methods to make them extension
	method friendly for 2.0 compat.
	* glib/Object.cs: use new GType getter methods.
	* glib/Value.cs: use new GType getter methods.
	* gtk/Widget.custom: use new GetClassPtr method.

svn path=/trunk/gtk-sharp/; revision=139609
2009-08-08 23:42:15 +00:00

95 lines
2.6 KiB
C#

// Author: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2009 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
using System;
using System.Runtime.InteropServices;
namespace Gapi {
public static class GTypeExtensions {
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_type_class_peek (IntPtr gtype);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_type_class_ref (IntPtr gtype);
public static IntPtr GetClassPtr (this GLib.GType gtype)
{
IntPtr klass = g_type_class_peek (gtype.Val);
if (klass == IntPtr.Zero)
klass = g_type_class_ref (gtype.Val);
return klass;
}
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_type_parent (IntPtr type);
public static GLib.GType GetBaseType (this GLib.GType gtype)
{
IntPtr parent = g_type_parent (gtype.Val);
return parent == IntPtr.Zero ? GLib.GType.None : new GLib.GType (parent);
}
public static GLib.GType GetThresholdType (this GLib.GType gtype)
{
GLib.GType curr = gtype;
while (curr.ToString ().StartsWith ("__gtksharp_"))
curr = GetBaseType (curr);
return curr;
}
struct GTypeQuery {
public IntPtr type;
public IntPtr type_name;
public uint class_size;
public uint instance_size;
}
[DllImport("libgobject-2.0-0.dll")]
static extern void g_type_query (IntPtr type, out GTypeQuery query);
public static uint GetClassSize (this GLib.GType gtype)
{
GTypeQuery query;
g_type_query (gtype.Val, out query);
return query.class_size;
}
static IntPtr ValFromInstancePtr (IntPtr handle)
{
if (handle == IntPtr.Zero)
return IntPtr.Zero;
// First field of instance is a GTypeClass*.
IntPtr klass = Marshal.ReadIntPtr (handle);
// First field of GTypeClass is a GType.
return Marshal.ReadIntPtr (klass);
}
[DllImport("libgobject-2.0-0.dll")]
static extern bool g_type_is_a (IntPtr type, IntPtr is_a_type);
public static bool IsInstance (this GLib.GType gtype, IntPtr raw)
{
return g_type_is_a (ValFromInstancePtr (raw), gtype.Val);
}
}
}