GtkSharp/generator/ClassBase.cs
Rachel Hestilow 967e3e9c5a 2002-07-30 Rachel Hestilow <hestilow@ximian.com>
* generator/ClassBase.cs: Change hasDefaultConstructor to protected,
	adjust now that it is an attr and not a subnode. Also add virtual
	property AssignToName (for ctors).

	* generator/Ctor.cs: Add property ForceStatic.
	(Generate): Optimize return code a bit for the static case.

	* generator/Method.cs: Assign to a "raw_ret" pointer before calling
	FromNativeReturn.

	* generator/Parameters.cs: Change "out ref" to "out", not "ref".

	* generator/Property.cs: Fix to work correctly with all object and
	struct types (mostly just some if-cases added).

	* generator/SignalHandler.cs: Remove args_type and argfields (unused).
	(Generate): Initialize struct if necessary.

	* generator/StructBase.cs: Massive reworking to support methods, ctors,
	etc.

	* generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types.

	* glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both.

	* glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event
	fields. Fix docs.

	* glib/Value.cs: Work correctly with boxed properties.

	* gnome/Modules.cs: Use new struct ctors.

	* gnome/Program.custom: Remove Get, this is being generated now.

	* parser/Gdk.metadata: Fix the drawable classes to inherit correctly.

	* parser/Metadata.pm: Change per-class attributes to actually be
	attributes.

	* parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor.

	* parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap
	typedefs.

	* sample/test/TestColorSelection.cs: Display color string in hex format,
	update to use IsNull instead of == null, and size dialog to look pretty.

	* sample/Size.cs: Added.

svn path=/trunk/gtk-sharp/; revision=6264
2002-07-30 23:02:12 +00:00

357 lines
7.6 KiB
C#

// GtkSharp.Generation.ClassBase.cs - Common code between object
// and interface wrappers
//
// Authors: Rachel Hestilow <hestilow@ximian.com>
// Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2002 Rachel Hestilow, 2001-2002 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Xml;
public class ClassBase : GenBase {
protected Hashtable props = new Hashtable();
protected Hashtable sigs = new Hashtable();
protected Hashtable methods = new Hashtable();
protected ArrayList interfaces = null;
protected ArrayList ctors = new ArrayList();
protected bool hasDefaultConstructor = true;
private bool ctors_initted = false;
private Hashtable clash_map;
public Hashtable Methods {
get {
return methods;
}
}
public Hashtable Signals {
get {
return sigs;
}
}
public ClassBase Parent {
get {
string parent = Elem.GetAttribute("parent");
return SymbolTable.GetClassGen(parent);
}
}
protected ClassBase (XmlElement ns, XmlElement elem) : base (ns, elem) {
hasDefaultConstructor = !elem.HasAttribute ("disabledefaultconstructor");
foreach (XmlNode node in elem.ChildNodes) {
if (!(node is XmlElement)) continue;
XmlElement member = (XmlElement) node;
if (member.HasAttribute ("hidden"))
continue;
switch (node.Name) {
case "method":
string name = member.GetAttribute("name");
while (methods.ContainsKey(name))
name += "mangled";
methods.Add (name, new Method (LibraryName, member, this));
break;
case "property":
props.Add (member.GetAttribute ("name"), new Property (member, this));
break;
case "signal":
sigs.Add (member.GetAttribute ("name"), new Signal (member, this));
break;
case "implements":
interfaces = ParseImplements (member);
break;
case "constructor":
ctors.Add (new Ctor (LibraryName, member, this));
break;
default:
break;
}
}
}
protected bool IsNodeNameHandled (string name)
{
switch (name) {
case "method":
case "property":
case "signal":
case "implements":
case "constructor":
case "disabledefaultconstructor":
return true;
default:
return false;
}
}
public virtual String MarshalType {
get
{
return "IntPtr";
}
}
public virtual String MarshalReturnType {
get
{
return "IntPtr";
}
}
public virtual String CallByName (String var_name)
{
return var_name + ".Handle";
}
public virtual String CallByName ()
{
return "Handle";
}
public virtual String AssignToName {
get { return "Raw"; }
}
public virtual String FromNative(String var)
{
return "(" + QualifiedName + ") GLib.Object.GetObject(" + var + ")";
}
public virtual String FromNativeReturn(String var)
{
return FromNative (var);
}
protected void GenProperties (StreamWriter sw)
{
if (props == null)
return;
foreach (Property prop in props.Values) {
if (prop.Validate ())
prop.Generate (sw);
else
Console.WriteLine(" in Object " + Name);
}
}
public void GenSignals (StreamWriter sw, ClassBase implementor, bool gen_docs)
{
if (sigs == null)
return;
foreach (Signal sig in sigs.Values) {
if (sig.Validate ())
sig.Generate (sw, implementor, gen_docs);
else
Console.WriteLine(" in Object " + Name);
}
}
private ArrayList ParseImplements (XmlElement member)
{
ArrayList ifaces = new ArrayList ();
foreach (XmlNode node in member.ChildNodes) {
if (node.Name != "interface")
continue;
XmlElement element = (XmlElement) node;
ifaces.Add (element.GetAttribute ("cname"));
}
return ifaces;
}
protected bool IgnoreMethod (Method method)
{
string mname = method.Name;
return ((mname.StartsWith("Set") || mname.StartsWith("Get")) &&
(props != null) && props.ContainsKey(mname.Substring(3)));
}
public void GenMethods (StreamWriter sw, Hashtable collisions, ClassBase implementor, bool gen_docs)
{
if (methods == null)
return;
foreach (Method method in methods.Values) {
if (IgnoreMethod (method))
continue;
if (method.Validate ())
{
String oname = null, oprotection = null;
if (collisions != null && collisions.Contains (method.Name))
{
oname = method.Name;
oprotection = method.Protection;
method.Name = Name + "." + method.Name;
method.Protection = "";
}
method.Generate (sw, implementor, gen_docs);
if (oname != null)
{
method.Name = oname;
method.Protection = oprotection;
}
}
else
Console.WriteLine(" in Object " + Name);
}
}
public Method GetMethod (string name)
{
return (Method) methods[name];
}
public Property GetProperty (string name)
{
return (Property) props[name];
}
public Signal GetSignal (string name)
{
return (Signal) sigs[name];
}
public Method GetMethodRecursively (string name)
{
return GetMethodRecursively (name, false);
}
public virtual Method GetMethodRecursively (string name, bool check_self)
{
Method p = null;
if (check_self)
p = GetMethod (name);
if (p == null && Parent != null)
p = Parent.GetMethodRecursively (name, true);
if (check_self && p == null && interfaces != null) {
foreach (string iface in interfaces) {
ClassBase igen = SymbolTable.GetClassGen (iface);
p = igen.GetMethodRecursively (name, true);
if (p != null)
break;
}
}
return p;
}
public virtual Property GetPropertyRecursively (string name)
{
ClassBase klass = this;
Property p = null;
while (klass != null && p == null) {
p = (Property) klass.GetProperty (name);
klass = klass.Parent;
}
return p;
}
public Signal GetSignalRecursively (string name)
{
return GetSignalRecursively (name, false);
}
public virtual Signal GetSignalRecursively (string name, bool check_self)
{
Signal p = null;
if (check_self)
p = GetSignal (name);
if (p == null && Parent != null)
p = Parent.GetSignalRecursively (name, true);
if (check_self && p == null && interfaces != null) {
foreach (string iface in interfaces) {
ClassBase igen = SymbolTable.GetClassGen (iface);
p = igen.GetSignalRecursively (name, true);
if (p != null)
break;
}
}
return p;
}
public bool Implements (string iface)
{
if (interfaces != null)
return interfaces.Contains (iface);
else
return false;
}
public ArrayList Ctors { get { return ctors; } }
private void InitializeCtors ()
{
if (ctors_initted)
return;
clash_map = new Hashtable();
if (ctors != null) {
bool has_preferred = false;
foreach (Ctor ctor in ctors) {
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.Initialize (clash_map);
}
}
ctors_initted = true;
}
protected virtual void GenCtors (StreamWriter sw)
{
ClassBase klass = this;
while (klass != null) {
klass.InitializeCtors ();
klass = klass.Parent;
}
if (ctors != null) {
foreach (Ctor ctor in ctors) {
if (ctor.Validate ())
ctor.Generate (sw);
}
}
if (!clash_map.ContainsKey("") && hasDefaultConstructor) {
sw.WriteLine("\t\tprotected " + Name + "() : base(){}");
sw.WriteLine();
}
}
}
}