From f750d78d6a256f749ad506be9fef7a6f36e54c41 Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Wed, 25 Dec 2002 00:36:00 +0000 Subject: [PATCH] 2002-12-24 Mike Kestner * generator/ObjectGen.cs : generate protected GType ctors * generator/SymbolTable.cs : map GType to uint * glib/Object.cs : add GType ctor. add RegisterGType. * glue/type.c (gtksharp_register_type): new GType registrar * */*.custom : make GType params uints * sample/Subclass.cs : a simple type registration example svn path=/trunk/gtk-sharp/; revision=9870 --- ChangeLog | 9 +++++++ generator/ObjectGen.cs | 1 + generator/SymbolTable.cs | 2 +- glib/Object.cs | 34 ++++++++++++++++++++++- glue/type.c | 15 +++++++++++ gnome/CanvasGroup.custom | 2 +- gnome/CanvasItem.custom | 4 +-- gnome/CanvasRE.custom | 2 +- gnome/CanvasShape.custom | 2 +- gtk/ListStore.custom | 2 +- gtk/TreeStore.custom | 2 +- sample/Makefile.in | 5 +++- sample/Subclass.cs | 58 ++++++++++++++++++++++++++++++++++++++++ 13 files changed, 128 insertions(+), 10 deletions(-) create mode 100755 sample/Subclass.cs diff --git a/ChangeLog b/ChangeLog index f09350dc0..0d6d30838 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2002-12-24 Mike Kestner + + * generator/ObjectGen.cs : generate protected GType ctors + * generator/SymbolTable.cs : map GType to uint + * glib/Object.cs : add GType ctor. add RegisterGType. + * glue/type.c (gtksharp_register_type): new GType registrar + * */*.custom : make GType params uints + * sample/Subclass.cs : a simple type registration example + 2002-12-24 Alejandro Sánchez Acosta * samples/tutorial/notebook: Added notebook sample. diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index 95633b87f..199132d80 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -190,6 +190,7 @@ namespace GtkSharp.Generation { sw.WriteLine("\t\t\tDispose();"); sw.WriteLine("\t\t}"); sw.WriteLine(); + sw.WriteLine("\t\tprotected " + Name + "(uint gtype) : base(gtype) {}"); sw.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}"); sw.WriteLine(); diff --git a/generator/SymbolTable.cs b/generator/SymbolTable.cs index 8d26f33d3..5983f307f 100644 --- a/generator/SymbolTable.cs +++ b/generator/SymbolTable.cs @@ -53,7 +53,7 @@ namespace GtkSharp.Generation { simple_types.Add ("gunichar", "string"); simple_types.Add ("uint1", "bool"); simple_types.Add ("GPtrArray", "System.IntPtr[]"); - simple_types.Add ("GType", "int"); + simple_types.Add ("GType", "uint"); simple_types.Add ("GError", "IntPtr"); // gsize is a system-specific typedef in glibconfig.h, // but this should work for now diff --git a/glib/Object.cs b/glib/Object.cs index c49d4e8cb..f3bb92783 100644 --- a/glib/Object.cs +++ b/glib/Object.cs @@ -10,8 +10,15 @@ namespace GLib { using System; using System.Collections; using System.ComponentModel; + using System.Reflection; using System.Runtime.InteropServices; + [AttributeUsage(AttributeTargets.All)] + public class WrapperClassAttribute : Attribute { + + public WrapperClassAttribute () : base () {} + } + /// /// Object Class /// @@ -131,6 +138,23 @@ namespace GLib { return GtkSharp.ObjectManager.CreateObject(o); } + [DllImport("gtksharpglue")] + static extern uint gtksharp_register_type (string name, uint parent_type); + + public static uint RegisterGType (Type t) + { + Type parent = t.BaseType; + PropertyInfo pi = parent.GetProperty ("GType", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public); + if (pi == null) { + Console.WriteLine ("null PropertyInfo"); + return 0; + } + uint parent_gtype = (uint) pi.GetValue (null, null); + string name = t.Namespace + t.Name; + GtkSharp.ObjectManager.RegisterType (name, t.Namespace + t.Name, t.Assembly.GetName().Name); + return gtksharp_register_type (name, parent_gtype); + } + /// /// Object Constructor /// @@ -156,6 +180,14 @@ namespace GLib { Raw = raw; } + [DllImport("gobject-2.0")] + static extern IntPtr g_object_new (uint gtype, IntPtr dummy); + + public Object (uint gtype) + { + Raw = g_object_new (gtype, IntPtr.Zero); + } + /// /// Raw Property /// @@ -190,7 +222,7 @@ namespace GLib { /// The type associated with this object class. /// - [DllImport("libgtksharpglue.so")] + [DllImport("gtksharpglue")] private static extern uint gtksharp_get_type_id (IntPtr obj); public static uint GType { diff --git a/glue/type.c b/glue/type.c index 18744f9f2..53f373058 100644 --- a/glue/type.c +++ b/glue/type.c @@ -6,6 +6,7 @@ */ #include +#include gchar * gtksharp_get_type_name (GObject *obj) @@ -36,3 +37,17 @@ gtksharp_get_type_name_for_id (GType typ) { return g_type_name (typ); } + +GType +gtksharp_register_type (gchar *name, GType parent) +{ + GTypeQuery query; + GTypeInfo info = {0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL }; + + g_type_query (parent, &query); + + info.class_size = query.class_size; + info.instance_size = query.instance_size; + + return g_type_register_static (parent, name, &info, 0); +} diff --git a/gnome/CanvasGroup.custom b/gnome/CanvasGroup.custom index da75cefb5..b6a8586f9 100644 --- a/gnome/CanvasGroup.custom +++ b/gnome/CanvasGroup.custom @@ -8,7 +8,7 @@ // This code is inserted after the automatically generated code. // -protected CanvasGroup (Gnome.CanvasGroup group, int type) : base (group, type) +protected CanvasGroup (Gnome.CanvasGroup group, uint type) : base (group, type) { } diff --git a/gnome/CanvasItem.custom b/gnome/CanvasItem.custom index 5897b81f0..e60c02552 100644 --- a/gnome/CanvasItem.custom +++ b/gnome/CanvasItem.custom @@ -10,9 +10,9 @@ [DllImport("gnomecanvas-2")] -static extern System.IntPtr gnome_canvas_item_new (IntPtr group, int type, IntPtr null_terminator); +static extern System.IntPtr gnome_canvas_item_new (IntPtr group, uint type, IntPtr null_terminator); -public CanvasItem (Gnome.CanvasGroup group, int type) +public CanvasItem (Gnome.CanvasGroup group, uint type) : base (gnome_canvas_item_new (group.Handle, type, IntPtr.Zero)) { } diff --git a/gnome/CanvasRE.custom b/gnome/CanvasRE.custom index 8f1e41d06..5f679ac6e 100644 --- a/gnome/CanvasRE.custom +++ b/gnome/CanvasRE.custom @@ -8,7 +8,7 @@ // This code is inserted after the automatically generated code. // -protected CanvasRE (Gnome.CanvasGroup group, int type) : base (group, type) +protected CanvasRE (Gnome.CanvasGroup group, uint type) : base (group, type) { } diff --git a/gnome/CanvasShape.custom b/gnome/CanvasShape.custom index f824f8af5..ac0f1f2d2 100644 --- a/gnome/CanvasShape.custom +++ b/gnome/CanvasShape.custom @@ -8,7 +8,7 @@ // This code is inserted after the automatically generated code. // -protected CanvasShape (Gnome.CanvasGroup group, int type) : base (group, type) +protected CanvasShape (Gnome.CanvasGroup group, uint type) : base (group, type) { } diff --git a/gtk/ListStore.custom b/gtk/ListStore.custom index 537f1d373..1c6adc25d 100644 --- a/gtk/ListStore.custom +++ b/gtk/ListStore.custom @@ -30,7 +30,7 @@ return ret; } - public void SetColumnTypes (params int[] types) + public void SetColumnTypes (params uint[] types) { SetColumnTypes (types.Length, types); } diff --git a/gtk/TreeStore.custom b/gtk/TreeStore.custom index 88b8b7213..8b1672157 100644 --- a/gtk/TreeStore.custom +++ b/gtk/TreeStore.custom @@ -110,7 +110,7 @@ return ret; } - public void SetColumnTypes (params int[] types) + public void SetColumnTypes (params uint[] types) { SetColumnTypes (types.Length, types); } diff --git a/sample/Makefile.in b/sample/Makefile.in index d0afd04e2..747e4de38 100755 --- a/sample/Makefile.in +++ b/sample/Makefile.in @@ -18,7 +18,7 @@ windows: $(CSC) /unsafe /out:gtk-hello-world.exe /r:../glib/glib-sharp.dll /r:../gtk/gtk-sharp.dll /r:../gdk/gdk-sharp.dll HelloWorld.cs $(CSC) /unsafe /out:button.exe /r:../glib/glib-sharp.dll /r:../gtk/gtk-sharp.dll ButtonApp.cs -linux: gtk-hello-world.exe button.exe menu.exe size.exe scribble.exe treeviewdemo.exe $(GNOME_TARGETS) $(GLADE_TARGETS) +linux: gtk-hello-world.exe button.exe subclass.exe menu.exe size.exe scribble.exe treeviewdemo.exe $(GNOME_TARGETS) $(GLADE_TARGETS) @ENABLE_GNOME_TRUE@ $(MAKE) -C gconf gtk-hello-world.exe: HelloWorld.cs @@ -36,6 +36,9 @@ fifteen.exe: Fifteen.cs button.exe: ButtonApp.cs $(MCS) --unsafe -o button.exe $(local_paths) $(all_assemblies) ButtonApp.cs +subclass.exe: Subclass.cs + $(MCS) --unsafe -o subclass.exe $(local_paths) $(all_assemblies) Subclass.cs + menu.exe: Menu.cs $(MCS) --unsafe -o menu.exe $(local_paths) $(all_assemblies) Menu.cs diff --git a/sample/Subclass.cs b/sample/Subclass.cs new file mode 100755 index 000000000..51865e3c7 --- /dev/null +++ b/sample/Subclass.cs @@ -0,0 +1,58 @@ +// Subclass.cs - Widget subclass Test implementation +// +// Author: Mike Kestner +// +// (c) 2001-2002 Mike Kestner + +namespace GtkSamples { + + using Gtk; + using GtkSharp; + using System; + using System.Drawing; + + public class ButtonApp { + + public static int Main (string[] args) + { + Application.Init (); + Window win = new Window ("Button Tester"); + win.DefaultSize = new Size (200, 150); + win.DeleteEvent += new DeleteEventHandler (Window_Delete); + Button btn = new MyButton (); + btn.Label = "I'm a subclassed button"; + btn.Clicked += new EventHandler (btn_click); + win.Add (btn); + win.ShowAll (); + Application.Run (); + return 0; + } + + static void btn_click (object obj, EventArgs args) + { + Console.WriteLine ("Button Clicked"); + } + + static void Window_Delete (object obj, DeleteEventArgs args) + { + Application.Quit (); + args.RetVal = true; + } + + } + + public class MyButton : Gtk.Button { + + static uint gtype = 0; + + public MyButton () : base (GType) {} + + public static new uint GType { + get { + if (gtype == 0) + gtype = RegisterGType (typeof (MyButton)); + return gtype; + } + } + } +}