From 1f4ff5bb864b9cb2ca1c4070b9ba9d0b36eab1cd Mon Sep 17 00:00:00 2001 From: Rachel Hestilow Date: Mon, 24 Jun 2002 22:04:10 +0000 Subject: [PATCH] 2002-06-24 Rachel Hestilow * glib/EnumWrapper.cs: New class which holds an enum int. * glib/Value.cs: Add support for glib enum types. We needed to use EnumWrapper for this because otherwise the int operator wouldn't know which glib function to use. * generator/BoxedGen.cs, ClassBase.cs, Ctor.cs, EnumGen.cs, InterfaceGen.cs, Method.cs, ObjectGen.cs, Signal.cs, StructGen.cs: Create more doc stubs. * generator/Property.cs: Generate enum values correctly. * generator/Ctor.cs: Refactor generation to honor metadata-specified collision preference. * parser/Gtk.metadata: Added constructor collision preferences to all known clashes. * parse/Gdk.metadata: Added (for Pixbuf clashes). svn path=/trunk/gtk-sharp/; revision=5437 --- ChangeLog | 22 ++++++++++++++++++++++ generator/BoxedGen.cs | 4 ++++ generator/ClassBase.cs | 8 ++++---- generator/Ctor.cs | 29 ++++++++++++++++++++++------- generator/EnumGen.cs | 15 +++++++++++++-- generator/InterfaceGen.cs | 4 ++++ generator/Method.cs | 30 ++++++++++++++++++++++++------ generator/ObjectGen.cs | 30 +++++++++++++++++++++++------- generator/Property.cs | 23 ++++++++++++++++------- generator/Signal.cs | 17 ++++++++++++----- generator/StructGen.cs | 4 ++++ generator/gtkapi.xml | 2 +- glib/EnumWrapper.cs | 26 ++++++++++++++++++++++++++ glib/Value.cs | 37 +++++++++++++++++++++++++++++++++++++ parser/Gdk.metadata | 17 +++++++++++++++++ parser/Gtk.metadata | 37 +++++++++++++++++++++++++++++++++++++ sources/Gdk.metadata | 17 +++++++++++++++++ sources/Gtk.metadata | 37 +++++++++++++++++++++++++++++++++++++ 18 files changed, 320 insertions(+), 39 deletions(-) create mode 100644 glib/EnumWrapper.cs create mode 100644 parser/Gdk.metadata create mode 100644 sources/Gdk.metadata diff --git a/ChangeLog b/ChangeLog index 2a2d96bab..78400d1b1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,25 @@ +2002-06-24 Rachel Hestilow + + * glib/EnumWrapper.cs: New class which holds an enum int. + + * glib/Value.cs: Add support for glib enum types. We needed + to use EnumWrapper for this because otherwise the int operator + wouldn't know which glib function to use. + + * generator/BoxedGen.cs, ClassBase.cs, Ctor.cs, EnumGen.cs, + InterfaceGen.cs, Method.cs, ObjectGen.cs, Signal.cs, StructGen.cs: + Create more doc stubs. + + * generator/Property.cs: Generate enum values correctly. + + * generator/Ctor.cs: Refactor generation to honor metadata-specified + collision preference. + + * parser/Gtk.metadata: Added constructor collision preferences to + all known clashes. + + * parse/Gdk.metadata: Added (for Pixbuf clashes). + 2002-06-24 Duncan Mak * glue/fileselection.c: New file, defines accessor functions to diff --git a/generator/BoxedGen.cs b/generator/BoxedGen.cs index f3d48f662..8df1d46a8 100644 --- a/generator/BoxedGen.cs +++ b/generator/BoxedGen.cs @@ -34,6 +34,10 @@ namespace GtkSharp.Generation { sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); + sw.WriteLine("\t\t/// " + Name + " Boxed Struct"); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]"); sw.WriteLine ("\tpublic class " + Name + " : GLib.Boxed {"); sw.WriteLine (); diff --git a/generator/ClassBase.cs b/generator/ClassBase.cs index 74a869be6..ab53dfdb8 100644 --- a/generator/ClassBase.cs +++ b/generator/ClassBase.cs @@ -101,14 +101,14 @@ namespace GtkSharp.Generation { } } - public void GenSignals (StreamWriter sw) + public void GenSignals (StreamWriter sw, bool gen_docs) { if (sigs == null) return; foreach (Signal sig in sigs.Values) { if (sig.Validate ()) - sig.Generate (sw); + sig.Generate (sw, gen_docs); else Console.WriteLine(" in Object " + Name); } @@ -135,7 +135,7 @@ namespace GtkSharp.Generation { (props != null) && props.ContainsKey(mname.Substring(3))); } - public void GenMethods (StreamWriter sw, Hashtable collisions) + public void GenMethods (StreamWriter sw, Hashtable collisions, bool gen_docs) { if (methods == null) return; @@ -154,7 +154,7 @@ namespace GtkSharp.Generation { method.Name = Name + "." + method.Name; method.Protection = ""; } - method.Generate (sw); + method.Generate (sw, gen_docs); if (oname != null) { method.Name = oname; diff --git a/generator/Ctor.cs b/generator/Ctor.cs index bbca1334f..89c210a0e 100644 --- a/generator/Ctor.cs +++ b/generator/Ctor.cs @@ -16,13 +16,21 @@ namespace GtkSharp.Generation { private string libname; private XmlElement elem; private Parameters parms; + private bool preferred; + public bool Preferred { + get { return preferred; } + set { preferred = value; } + } + public Ctor (string libname, XmlElement elem) { this.libname = libname; this.elem = elem; XmlElement parms_elem = elem ["parameters"]; if (parms_elem != null) parms = new Parameters (parms_elem); + if (elem.HasAttribute ("preferred")) + preferred = true; } public bool Validate () @@ -39,6 +47,18 @@ namespace GtkSharp.Generation { return true; } + public void InitClashMap (Hashtable clash_map) + { + string sigtypes = (parms != null) ? parms.SignatureTypes : ""; + if (clash_map.ContainsKey (sigtypes)) { + int num = (int) clash_map[sigtypes]; + clash_map[sigtypes] = ++num; + Console.WriteLine ("CLASH: {0} {1}", elem.GetAttribute ("cname"), num); + } + else + clash_map[sigtypes] = 0; + } + public void Generate (StreamWriter sw, Hashtable clash_map) { string sigtypes = ""; @@ -52,12 +72,7 @@ namespace GtkSharp.Generation { sigtypes = parms.SignatureTypes; } - bool clash = false; - if (clash_map.ContainsKey(sigtypes)) { - clash = true; - } else { - clash_map[sigtypes] = elem; - } + int clashes = (int) clash_map[sigtypes]; string cname = elem.GetAttribute("cname"); string name = ((XmlElement)elem.ParentNode).GetAttribute("name"); @@ -71,7 +86,7 @@ namespace GtkSharp.Generation { sw.WriteLine("\t\tstatic extern " + safety + "IntPtr " + cname + isig); sw.WriteLine(); - if (clash) { + if (clashes > 0 && !Preferred) { String mname = cname.Substring(cname.IndexOf("new")); mname = mname.Substring(0,1).ToUpper() + mname.Substring(1); int idx; diff --git a/generator/EnumGen.cs b/generator/EnumGen.cs index 18a167e49..d1fd0c0a1 100644 --- a/generator/EnumGen.cs +++ b/generator/EnumGen.cs @@ -38,9 +38,15 @@ namespace GtkSharp.Generation { if (Elem.GetAttribute("type") == "flags") { sw.WriteLine ("\tusing System;"); sw.WriteLine (); - sw.WriteLine ("\t[Flags]"); } - + + sw.WriteLine("\t\t/// " + Name + " enumeration "); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + + if (Elem.GetAttribute("type") == "flags") + sw.WriteLine ("\t[Flags]"); + sw.WriteLine ("\tpublic enum " + Name + " {"); sw.WriteLine (); @@ -50,6 +56,11 @@ namespace GtkSharp.Generation { } XmlElement member = (XmlElement) node; + + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + sw.Write ("\t\t" + member.GetAttribute("name")); if (member.HasAttribute("value")) { sw.WriteLine (" = " + member.GetAttribute("value") + ","); diff --git a/generator/InterfaceGen.cs b/generator/InterfaceGen.cs index a1cd7b0b8..8ca7501d3 100644 --- a/generator/InterfaceGen.cs +++ b/generator/InterfaceGen.cs @@ -21,6 +21,10 @@ namespace GtkSharp.Generation { sw.WriteLine ("\tusing System;"); sw.WriteLine (); + sw.WriteLine("\t\t/// " + Name + " Interface"); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + sw.WriteLine ("\tpublic interface " + Name + " : GLib.IWrapper {"); sw.WriteLine (); diff --git a/generator/Method.cs b/generator/Method.cs index 8c0580589..005ed2158 100644 --- a/generator/Method.cs +++ b/generator/Method.cs @@ -190,6 +190,8 @@ namespace GtkSharp.Generation { if (!Initialize ()) return; + GenerateComments (sw); + if (is_get || is_set) { Method comp = GetComplement (); @@ -219,6 +221,21 @@ namespace GtkSharp.Generation { Statistics.MethodCount++; } + void GenerateComments (StreamWriter sw) + { + string summary, sname; + sw.WriteLine(); + if (is_get || is_set) { + summary = "Property"; + sname = Name.Substring (3); + } else { + summary = "Method"; + sname = Name; + } + sw.WriteLine("\t\t/// " + sname + " " + summary + " "); + sw.WriteLine("\t\t/// To be completed "); + } + protected void GenerateImport (StreamWriter sw) { sw.WriteLine("\t\t[DllImport(\"" + libname + "\")]"); @@ -227,6 +244,11 @@ namespace GtkSharp.Generation { } public void Generate (StreamWriter sw) + { + Generate (sw, true); + } + + public void Generate (StreamWriter sw, bool gen_docs) { Method comp = null; @@ -255,12 +277,8 @@ namespace GtkSharp.Generation { if (comp != null && s_ret == comp.parms.AccessorReturnType) comp.GenerateImport (sw); - if (!(is_set || is_get)) - { - sw.WriteLine("\t\t/// " + Name + " Method "); - sw.WriteLine("\t\t/// To be completed "); - sw.WriteLine(); - } + if (gen_docs) + GenerateComments (sw); sw.Write("\t\t"); if (protection != "") diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index f72d839fa..a91690298 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -55,6 +55,9 @@ namespace GtkSharp.Generation { sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); + sw.WriteLine("\t\t/// " + Name + " Class"); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); sw.Write ("\tpublic class " + Name); string cs_parent = SymbolTable.GetCSType(Elem.GetAttribute("parent")); if (cs_parent != "") @@ -84,10 +87,10 @@ namespace GtkSharp.Generation { if (has_sigs) { sw.WriteLine("\t\tprivate Hashtable Signals = new Hashtable();"); - GenSignals (sw); + GenSignals (sw, true); } - GenMethods (sw, null); + GenMethods (sw, null, true); if (interfaces != null) { Hashtable all_methods = new Hashtable (); @@ -104,8 +107,8 @@ namespace GtkSharp.Generation { foreach (string iface in interfaces) { ClassBase igen = SymbolTable.GetClassGen (iface); - igen.GenMethods (sw, collisions); - igen.GenSignals (sw); + igen.GenMethods (sw, collisions, false); + igen.GenSignals (sw, false); } } @@ -159,13 +162,26 @@ namespace GtkSharp.Generation { Hashtable clash_map = new Hashtable(); - if (ctors != null) + if (ctors != null) { + bool has_preferred = false; foreach (Ctor ctor in ctors) { - if (ctor.Validate ()) - ctor.Generate (sw, clash_map); + if (ctor.Validate ()) { + ctor.InitClashMap (clash_map); + if (ctor.Preferred) + has_preferred = true; + } else Console.WriteLine(" in Object " + Name); } + + if (!has_preferred && ctors.Count > 0) + ((Ctor) ctors[0]).Preferred = true; + + foreach (Ctor ctor in ctors) { + if (ctor.Validate ()) + ctor.Generate (sw, clash_map); + } + } if (!clash_map.ContainsKey("")) { sw.WriteLine("\t\tprotected " + Name + "() : base(){}"); diff --git a/generator/Property.cs b/generator/Property.cs index b78dbacf3..604999881 100644 --- a/generator/Property.cs +++ b/generator/Property.cs @@ -55,22 +55,27 @@ namespace GtkSharp.Generation { string v_type = ""; if (SymbolTable.IsEnum(c_type)) { - v_type = "int"; + v_type = "(int) (GLib.EnumWrapper)"; } else if (SymbolTable.IsInterface(c_type)) { // FIXME: Handle interface props properly. Console.Write("Interface property detected "); Statistics.ThrottledCount++; return; } else if (SymbolTable.IsObject(c_type)) { - v_type = "GLib.Object"; + v_type = "(GLib.Object)"; } else if (SymbolTable.IsBoxed (c_type)) { - v_type = "GLib.Boxed"; + v_type = "(GLib.Boxed)"; } if (elem.HasAttribute("construct-only") && !elem.HasAttribute("readable")) { return; } + sw.WriteLine(); + sw.WriteLine("\t\t/// " + name + " Property "); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\tpublic " + cs_type + " " + name + " {"); if (elem.HasAttribute("readable")) { sw.WriteLine("\t\t\tget {"); @@ -78,7 +83,7 @@ namespace GtkSharp.Generation { sw.WriteLine("\t\t\t\tGetProperty(" + cname + ", val);"); sw.Write("\t\t\t\treturn (" + cs_type + ") "); if (v_type != "") { - sw.Write("(" + v_type + ") "); + sw.Write(v_type + " "); } sw.WriteLine("val;"); sw.WriteLine("\t\t\t}"); @@ -87,10 +92,14 @@ namespace GtkSharp.Generation { if (elem.HasAttribute("writeable") && !elem.HasAttribute("construct-only")) { sw.WriteLine("\t\t\tset {"); sw.Write("\t\t\t\tSetProperty(" + cname + ", new GLib.Value("); - if (v_type != "") { - sw.Write("(" + v_type + ") "); + if (SymbolTable.IsEnum(c_type)) { + sw.WriteLine("Handle, " + cname + ", new GLib.EnumWrapper ((int) value)));"); + } else { + if (v_type != "") { + sw.Write(v_type + " "); + } + sw.WriteLine("value));"); } - sw.WriteLine("value));"); sw.WriteLine("\t\t\t}"); } diff --git a/generator/Signal.cs b/generator/Signal.cs index 0dd6c753d..5476485a2 100644 --- a/generator/Signal.cs +++ b/generator/Signal.cs @@ -46,21 +46,28 @@ namespace GtkSharp.Generation { public void GenerateDecl (StreamWriter sw) { + GenComments (sw); if (elem.HasAttribute("new_flag")) sw.Write("new "); sw.WriteLine ("\t\tevent EventHandler " + Name + ";"); } - public void Generate (StreamWriter sw) + public void GenComments (StreamWriter sw) { - string cname = "\"" + elem.GetAttribute("cname") + "\""; - marsh = "GtkSharp." + marsh; - + sw.WriteLine(); sw.WriteLine("\t\t/// " + Name + " Event "); sw.WriteLine("\t\t/// "); // FIXME: Generate some signal docs sw.WriteLine("\t\t/// "); - sw.WriteLine(); + } + + public void Generate (StreamWriter sw, bool gen_docs) + { + string cname = "\"" + elem.GetAttribute("cname") + "\""; + marsh = "GtkSharp." + marsh; + + if (gen_docs) + GenComments (sw); sw.Write("\t\tpublic "); if (elem.HasAttribute("new_flag")) sw.Write("new "); diff --git a/generator/StructGen.cs b/generator/StructGen.cs index ffe8f0646..dcf7593a0 100644 --- a/generator/StructGen.cs +++ b/generator/StructGen.cs @@ -40,6 +40,10 @@ namespace GtkSharp.Generation { sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); + sw.WriteLine("\t\t/// " + Name + " Struct "); + sw.WriteLine("\t\t/// "); + sw.WriteLine("\t\t/// "); + sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]"); sw.WriteLine ("\tpublic class " + Name + " {"); sw.WriteLine (); diff --git a/generator/gtkapi.xml b/generator/gtkapi.xml index 33c0af17c..d375a048e 100644 --- a/generator/gtkapi.xml +++ b/generator/gtkapi.xml @@ -1,2 +1,2 @@ - + diff --git a/glib/EnumWrapper.cs b/glib/EnumWrapper.cs new file mode 100644 index 000000000..d67c0a1b5 --- /dev/null +++ b/glib/EnumWrapper.cs @@ -0,0 +1,26 @@ +// EnumWrapper.cs - Class to hold arbitrary glib enums +// +// Author: Rachel Hestilow +// +// (c) 2002 Rachel Hestilow + +namespace GLib { + + using System; + using System.Runtime.InteropServices; + + // Enum wrapping class + // + public class EnumWrapper { + int val; + + public EnumWrapper (int val) { + this.val = val; + } + + public static explicit operator int (EnumWrapper wrap) { + return wrap.val; + } + } +} + diff --git a/glib/Value.cs b/glib/Value.cs index 9fc4fd38c..a93e5ecda 100755 --- a/glib/Value.cs +++ b/glib/Value.cs @@ -219,6 +219,23 @@ namespace GLib { g_value_set_uint (_val, val); } + [DllImport("gobject-2.0")] + static extern void g_value_set_enum (IntPtr val, int data); + + /// + /// Value Constructor + /// + /// + /// + /// Constructs a Value from a specified enum wrapper. + /// + + public Value (IntPtr obj, string prop_name, EnumWrapper wrap) + { + _val = gtksharp_value_create_from_property (obj, prop_name); + g_value_set_enum (_val, (int) wrap); + } + [DllImport("gobject-2.0")] static extern bool g_value_get_boolean (IntPtr val); @@ -400,6 +417,26 @@ namespace GLib { return g_value_get_uint (val._val); } + [DllImport("gobject-2.0")] + static extern int g_value_get_enum (IntPtr val); + + /// + /// Value to Enum Conversion + /// + /// + /// + /// Extracts an enum from a Value. Note, this method + /// will produce an exception if the Value does not hold an + /// enum value. + /// + + public static explicit operator EnumWrapper (Value val) + { + // FIXME: Insert an appropriate exception here if + // _val.type indicates an error. + return new EnumWrapper (g_value_get_enum (val._val)); + } + /// /// Handle Property /// diff --git a/parser/Gdk.metadata b/parser/Gdk.metadata new file mode 100644 index 000000000..93a8f02ee --- /dev/null +++ b/parser/Gdk.metadata @@ -0,0 +1,17 @@ + + + + + + + gdk_pixbuf_new_from_file + + + + preferred + 1 + + + + + diff --git a/parser/Gtk.metadata b/parser/Gtk.metadata index 3f6e0ad0f..cf29c7014 100644 --- a/parser/Gtk.metadata +++ b/parser/Gtk.metadata @@ -56,6 +56,43 @@ + + + + gtk_button_new_with_mnemonic + + + gtk_check_button_new_with_mnemonic + + + gtk_radio_button_new_with_mnemonic + + + gtk_toggle_button_new_with_mnemonic + + + gtk_label_new_with_mnemonic + + + gtk_menu_item_new_with_mnemonic + + + gtk_check_menu_item_new_with_mnemonic + + + gtk_radio_menu_item_new_with_mnemonic + + + gtk_image_menu_item_new_with_mnemonic + + + + preferred + 1 + + + + diff --git a/sources/Gdk.metadata b/sources/Gdk.metadata new file mode 100644 index 000000000..93a8f02ee --- /dev/null +++ b/sources/Gdk.metadata @@ -0,0 +1,17 @@ + + + + + + + gdk_pixbuf_new_from_file + + + + preferred + 1 + + + + + diff --git a/sources/Gtk.metadata b/sources/Gtk.metadata index 3f6e0ad0f..cf29c7014 100644 --- a/sources/Gtk.metadata +++ b/sources/Gtk.metadata @@ -56,6 +56,43 @@ + + + + gtk_button_new_with_mnemonic + + + gtk_check_button_new_with_mnemonic + + + gtk_radio_button_new_with_mnemonic + + + gtk_toggle_button_new_with_mnemonic + + + gtk_label_new_with_mnemonic + + + gtk_menu_item_new_with_mnemonic + + + gtk_check_menu_item_new_with_mnemonic + + + gtk_radio_menu_item_new_with_mnemonic + + + gtk_image_menu_item_new_with_mnemonic + + + + preferred + 1 + + + +