GtkSharp/generator/Property.cs

190 lines
5.3 KiB
C#
Raw Normal View History

// GtkSharp.Generation.Property.cs - The Property Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// Copyright (c) 2001-2003 Mike Kestner
// Copyright (c) 2004 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the 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
// General Public License for more details.
//
// You should have received a copy of the GNU 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.
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Xml;
public class Property : PropertyBase {
public Property (XmlElement elem, ClassBase container_type) : base (elem, container_type) {}
public bool Validate ()
{
if (CSType == "" && !Hidden) {
Console.Write("Property has unknown Type {0} ", CType);
Statistics.ThrottledCount++;
return false;
}
return true;
}
bool Readable {
get {
return elem.GetAttribute ("readable") == "true";
}
}
bool Writable {
get {
return elem.GetAttribute ("writeable") == "true" &&
!elem.HasAttribute ("construct-only");
}
}
bool IsDeprecated {
get {
return !container_type.IsDeprecated &&
elem.GetAttribute ("deprecated") == "1";
}
}
protected virtual string PropertyAttribute (string qpname) {
return "[GLib.Property (" + qpname + ")]";
}
protected virtual string RawGetter (string qpname) {
return "GetProperty (" + qpname + ")";
}
protected virtual string RawSetter (string qpname) {
return "SetProperty(" + qpname + ", val)";
}
public void GenerateDecl (StreamWriter sw, string indent)
{
if (Hidden || (!Readable && !Writable))
return;
string name = Name;
if (name == container_type.Name)
name += "Prop";
sw.WriteLine (indent + CSType + " " + name + " {");
sw.Write (indent + "\t");
if (Readable || Getter != null)
sw.Write ("get; ");
if (Writable || Setter != null)
sw.Write ("set;");
sw.WriteLine ();
sw.WriteLine (indent + "}");
}
public void Generate (GenerationInfo gen_info, string indent, ClassBase implementor)
{
SymbolTable table = SymbolTable.Table;
2003-10-05 00:20:17 +00:00
StreamWriter sw = gen_info.Writer;
if (Hidden || (!Readable && !Writable))
return;
string modifiers = "";
if (IsNew || (container_type.Parent != null && container_type.Parent.GetPropertyRecursively (Name) != null))
modifiers = "new ";
else if (implementor != null && implementor.Parent.GetPropertyRecursively (Name) != null)
modifiers = "new ";
string name = Name;
if (name == container_type.Name) {
name += "Prop";
}
string qpname = "\"" + CName + "\"";
string v_type = "";
if (table.IsInterface (CType)) {
v_type = "(GLib.Object)";
} else if (table.IsOpaque (CType)) {
2002-07-25 Rachel Hestilow <hestilow@ximian.com> [about 60% of the marshalling patch that I lost. The rest to come tomorrow.] * generator/BoxedGen.cs, StructGen.cs: Move most of this to StructBase, delete large chunks duplicated from ClassBase. * generator/IGeneratable.cs: Add MarshalReturnType, FromNativeReturn. * generator/ClassBase.cs: Move ctor stuff here. Add a CallByName overload with no parameters for the "self" reference. * generator/EnumGen.cs, CallbackGen.cs: Implement new MarshalReturnType, FromNativeReturn. * generator/Method.cs: Use container_type.MarshalType, CallByName, and SymbolTable.FromNativeReturn when generating call and import sigs. * generator/OpaqueGen.cs: Added. * generator/Property.cs: Handle boxed and opaques differently. * generator/SymbolTable.cs: Update for the opaque stuff and the new Return methods. Also change GetClassGen to simply call the as operator. * glib/Boxed.cs: Update for struct usage -- this is now a wrapper for the purposes of using with Value. * glib/Opaque.cs: Added. New base class for opaque structs. * glue/textiter.c, gtk/TextIter.custom: Remove. * gnome/Program.cs: Update for new struct marshalling. * parser/Metadata.pm: Use our own getChildrenByTagName. * parser/README: Update for new requirements (was out of sync with build.pl) * parser/gapi2xml.pl: Hide struct like const in field elements. * parser/gapi_pp.pl: Handle embedded union fields (poorly). * sample/test/TestColorSelection.cs: Comment out null color tests for now. svn path=/trunk/gtk-sharp/; revision=6186
2002-07-26 06:08:52 +00:00
v_type = "(GLib.Opaque)";
} else if (table.IsEnum (CType)) {
v_type = "(Enum)";
}
GenerateImports (gen_info, indent);
if (IsDeprecated ||
(Getter != null && Getter.IsDeprecated) ||
(Setter != null && Setter.IsDeprecated))
sw.WriteLine (indent + "[Obsolete]");
sw.WriteLine (indent + PropertyAttribute (qpname));
sw.WriteLine (indent + "public " + modifiers + CSType + " " + name + " {");
indent += "\t";
if (Getter != null) {
sw.Write(indent + "get ");
Getter.GenerateBody(gen_info, "\t");
sw.WriteLine();
} else if (Readable) {
sw.WriteLine(indent + "get {");
sw.WriteLine(indent + "\tGLib.Value val = " + RawGetter (qpname) + ";");
if (table.IsOpaque (CType) || table.IsBoxed (CType)) {
sw.WriteLine(indent + "\t" + CSType + " ret = (" + CSType + ") val;");
2002-07-25 Rachel Hestilow <hestilow@ximian.com> [about 60% of the marshalling patch that I lost. The rest to come tomorrow.] * generator/BoxedGen.cs, StructGen.cs: Move most of this to StructBase, delete large chunks duplicated from ClassBase. * generator/IGeneratable.cs: Add MarshalReturnType, FromNativeReturn. * generator/ClassBase.cs: Move ctor stuff here. Add a CallByName overload with no parameters for the "self" reference. * generator/EnumGen.cs, CallbackGen.cs: Implement new MarshalReturnType, FromNativeReturn. * generator/Method.cs: Use container_type.MarshalType, CallByName, and SymbolTable.FromNativeReturn when generating call and import sigs. * generator/OpaqueGen.cs: Added. * generator/Property.cs: Handle boxed and opaques differently. * generator/SymbolTable.cs: Update for the opaque stuff and the new Return methods. Also change GetClassGen to simply call the as operator. * glib/Boxed.cs: Update for struct usage -- this is now a wrapper for the purposes of using with Value. * glib/Opaque.cs: Added. New base class for opaque structs. * glue/textiter.c, gtk/TextIter.custom: Remove. * gnome/Program.cs: Update for new struct marshalling. * parser/Metadata.pm: Use our own getChildrenByTagName. * parser/README: Update for new requirements (was out of sync with build.pl) * parser/gapi2xml.pl: Hide struct like const in field elements. * parser/gapi_pp.pl: Handle embedded union fields (poorly). * sample/test/TestColorSelection.cs: Comment out null color tests for now. svn path=/trunk/gtk-sharp/; revision=6186
2002-07-26 06:08:52 +00:00
} else {
sw.Write(indent + "\t" + CSType + " ret = ");
sw.Write ("(" + CSType + ") ");
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
if (v_type != "") {
sw.Write(v_type + " ");
}
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
sw.WriteLine("val;");
}
sw.WriteLine(indent + "\tval.Dispose ();");
sw.WriteLine(indent + "\treturn ret;");
sw.WriteLine(indent + "}");
}
if (Setter != null) {
sw.Write(indent + "set ");
Setter.GenerateBody(gen_info, "\t");
sw.WriteLine();
} else if (Writable) {
sw.WriteLine(indent + "set {");
sw.Write(indent + "\tGLib.Value val = ");
if (table.IsBoxed (CType)) {
sw.WriteLine("(GLib.Value) value;");
} else if (table.IsOpaque (CType)) {
sw.WriteLine("new GLib.Value(value, \"{0}\");", CType);
} else {
sw.Write("new GLib.Value(");
if (v_type != "" && !(table.IsObject (CType) || table.IsInterface (CType) || table.IsOpaque (CType))) {
sw.Write(v_type + " ");
}
sw.WriteLine("value);");
}
sw.WriteLine(indent + "\t" + RawSetter (qpname) + ";");
sw.WriteLine(indent + "\tval.Dispose ();");
sw.WriteLine(indent + "}");
}
sw.WriteLine(indent.Substring (1) + "}");
sw.WriteLine();
Statistics.PropCount++;
}
}
}