2002-05-23 23:43:25 +00:00
|
|
|
// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
|
|
|
|
//
|
2005-01-24 18:25:02 +00:00
|
|
|
// Author: Mike Kestner <mkestner@novell.com>
|
2002-05-23 23:43:25 +00:00
|
|
|
//
|
2004-06-25 16:35:15 +00:00
|
|
|
// Copyright (c) 2001-2003 Mike Kestner
|
2005-01-24 18:25:02 +00:00
|
|
|
// Copyright (c) 2004-2005 Novell, Inc.
|
2004-06-25 16:35:15 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2002-05-23 23:43:25 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
public class Ctor : MethodBase {
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2002-06-24 22:04:10 +00:00
|
|
|
private bool preferred;
|
2005-01-24 18:25:02 +00:00
|
|
|
private string name;
|
2004-05-07 13:42:59 +00:00
|
|
|
private bool needs_chaining = false;
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
public Ctor (XmlElement elem, ClassBase implementor) : base (elem, implementor)
|
|
|
|
{
|
|
|
|
if (elem.HasAttribute ("preferred"))
|
|
|
|
preferred = true;
|
|
|
|
if (implementor is ObjectGen)
|
|
|
|
needs_chaining = true;
|
|
|
|
name = implementor.Name;
|
|
|
|
}
|
|
|
|
|
2002-06-24 22:04:10 +00:00
|
|
|
public bool Preferred {
|
|
|
|
get { return preferred; }
|
|
|
|
set { preferred = value; }
|
|
|
|
}
|
2002-07-30 23:02:12 +00:00
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
public string StaticName {
|
|
|
|
get {
|
|
|
|
if (!IsStatic)
|
|
|
|
return String.Empty;
|
2003-04-06 09:21:15 +00:00
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
string[] toks = CName.Substring(CName.IndexOf("new")).Split ('_');
|
|
|
|
string result = String.Empty;
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
foreach (string tok in toks)
|
|
|
|
result += tok.Substring(0,1).ToUpper() + tok.Substring(1);
|
|
|
|
return result;
|
2002-05-23 23:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
public void GenerateImport (StreamWriter sw)
|
2002-06-24 22:04:10 +00:00
|
|
|
{
|
2005-01-24 18:25:02 +00:00
|
|
|
sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\")]");
|
|
|
|
sw.WriteLine("\t\tstatic extern " + Safety + "IntPtr " + CName + "(" + ImportSignature + ");");
|
|
|
|
sw.WriteLine();
|
2002-06-24 22:04:10 +00:00
|
|
|
}
|
2002-07-13 Rachel Hestilow <hestilow@ximian.com>
* parser/Gnome.metadata, Gtk.metadata: More conflict
fixes.
* parser/build.pl: Fully qualify all lib names. (Gtk+ packages
are now LFS-compliant in Debian...)
* parser/gapi2xml.pl: Fix for whitespace in fields, defines,
and docs.
* generator/BoxedGen.cs: Remove extraneous CallByName definition,
add "override" keyword to FromNative.
(Generate): Generate methods after fields.
* generator/ClassBase.cs: Change CallByName, FromNative to virtual.
(.ctor): Ignore "hidden" nodes. Set container on signal.
(GenSignals, GenMethods): Add "implementor" argument for interface
use.
(Get(Method|Signal|Property)Recursively): Rework to correctly
recurse interfaces.
(Implements): Added.
* generator/Ctor.cs (Initialize): Move clash initialization completely
out of Generate, so we can check for collisions.
* generator/Method.cs (GenerateDeclCommon): Check for duplicates,
for "new" keyword.
(Generate): Add "implementor" argument.
* generator/ObjectGen.cs (Generate): Initialize ctor clashes on
this and all parents, before generating.
(Ctors, InitializeCtors): Added.
* generator/Signal.cs: Store the container_type, check for
collisions.
* generator/StructGen.cs: Add "override" keyword to overriden methods.
* gtk/FileSelection.custom (ActionArea): Add "new" keyword.
svn path=/trunk/gtk-sharp/; revision=5782
2002-07-13 20:31:23 +00:00
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
public void GenerateStatic (GenerationInfo gen_info)
|
2002-07-13 Rachel Hestilow <hestilow@ximian.com>
* parser/Gnome.metadata, Gtk.metadata: More conflict
fixes.
* parser/build.pl: Fully qualify all lib names. (Gtk+ packages
are now LFS-compliant in Debian...)
* parser/gapi2xml.pl: Fix for whitespace in fields, defines,
and docs.
* generator/BoxedGen.cs: Remove extraneous CallByName definition,
add "override" keyword to FromNative.
(Generate): Generate methods after fields.
* generator/ClassBase.cs: Change CallByName, FromNative to virtual.
(.ctor): Ignore "hidden" nodes. Set container on signal.
(GenSignals, GenMethods): Add "implementor" argument for interface
use.
(Get(Method|Signal|Property)Recursively): Rework to correctly
recurse interfaces.
(Implements): Added.
* generator/Ctor.cs (Initialize): Move clash initialization completely
out of Generate, so we can check for collisions.
* generator/Method.cs (GenerateDeclCommon): Check for duplicates,
for "new" keyword.
(Generate): Add "implementor" argument.
* generator/ObjectGen.cs (Generate): Initialize ctor clashes on
this and all parents, before generating.
(Ctors, InitializeCtors): Added.
* generator/Signal.cs: Store the container_type, check for
collisions.
* generator/StructGen.cs: Add "override" keyword to overriden methods.
* gtk/FileSelection.custom (ActionArea): Add "new" keyword.
svn path=/trunk/gtk-sharp/; revision=5782
2002-07-13 20:31:23 +00:00
|
|
|
{
|
2005-01-24 18:25:02 +00:00
|
|
|
StreamWriter sw = gen_info.Writer;
|
|
|
|
sw.WriteLine("\t\tpublic static " + Safety + Modifiers + name + " " + StaticName + "(" + Signature + ")");
|
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
|
|
|
|
Body.Initialize(gen_info, false, false, "");
|
|
|
|
|
|
|
|
sw.Write("\t\t\treturn ");
|
|
|
|
if (container_type is StructBase)
|
|
|
|
sw.Write ("{0}.New (", name);
|
|
|
|
else
|
|
|
|
sw.Write ("new {0} (", name);
|
|
|
|
sw.WriteLine (CName + "(" + Body.GetCallString (false) + "));");
|
2002-07-13 Rachel Hestilow <hestilow@ximian.com>
* parser/Gnome.metadata, Gtk.metadata: More conflict
fixes.
* parser/build.pl: Fully qualify all lib names. (Gtk+ packages
are now LFS-compliant in Debian...)
* parser/gapi2xml.pl: Fix for whitespace in fields, defines,
and docs.
* generator/BoxedGen.cs: Remove extraneous CallByName definition,
add "override" keyword to FromNative.
(Generate): Generate methods after fields.
* generator/ClassBase.cs: Change CallByName, FromNative to virtual.
(.ctor): Ignore "hidden" nodes. Set container on signal.
(GenSignals, GenMethods): Add "implementor" argument for interface
use.
(Get(Method|Signal|Property)Recursively): Rework to correctly
recurse interfaces.
(Implements): Added.
* generator/Ctor.cs (Initialize): Move clash initialization completely
out of Generate, so we can check for collisions.
* generator/Method.cs (GenerateDeclCommon): Check for duplicates,
for "new" keyword.
(Generate): Add "implementor" argument.
* generator/ObjectGen.cs (Generate): Initialize ctor clashes on
this and all parents, before generating.
(Ctors, InitializeCtors): Added.
* generator/Signal.cs: Store the container_type, check for
collisions.
* generator/StructGen.cs: Add "override" keyword to overriden methods.
* gtk/FileSelection.custom (ActionArea): Add "new" keyword.
svn path=/trunk/gtk-sharp/; revision=5782
2002-07-13 20:31:23 +00:00
|
|
|
}
|
2005-01-24 18:25:02 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
public void Generate (GenerationInfo gen_info)
|
2002-05-23 23:43:25 +00:00
|
|
|
{
|
2003-10-05 00:20:17 +00:00
|
|
|
StreamWriter sw = gen_info.Writer;
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2004-05-07 13:42:59 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
GenerateImport (sw);
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
if (IsStatic)
|
|
|
|
GenerateStatic (gen_info);
|
|
|
|
else {
|
|
|
|
sw.WriteLine("\t\tpublic {0}{1} ({2}) {3}", Safety, name, Signature.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : "");
|
2002-05-23 23:43:25 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
2002-07-30 23:02:12 +00:00
|
|
|
|
2004-05-07 13:42:59 +00:00
|
|
|
if (needs_chaining) {
|
|
|
|
sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {");
|
|
|
|
|
2005-01-26 19:17:07 +00:00
|
|
|
if (Parameters.Count == 0) {
|
2004-05-07 13:42:59 +00:00
|
|
|
sw.WriteLine ("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn;");
|
|
|
|
} else {
|
|
|
|
ArrayList names = new ArrayList ();
|
|
|
|
ArrayList values = new ArrayList ();
|
2005-01-24 18:25:02 +00:00
|
|
|
for (int i = 0; i < Parameters.Count; i++) {
|
|
|
|
Parameter p = Parameters[i];
|
2004-05-07 13:42:59 +00:00
|
|
|
if (container_type.GetPropertyRecursively (p.StudlyName) != null) {
|
|
|
|
names.Add (p.Name);
|
|
|
|
values.Add (p.Name);
|
|
|
|
} else if (p.PropertyName != String.Empty) {
|
|
|
|
names.Add (p.PropertyName);
|
|
|
|
values.Add (p.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
if (names.Count == Parameters.Count) {
|
2004-05-07 13:42:59 +00:00
|
|
|
sw.WriteLine ("\t\t\t\tArrayList vals = new ArrayList();");
|
|
|
|
sw.WriteLine ("\t\t\t\tArrayList names = new ArrayList();");
|
|
|
|
for (int i = 0; i < names.Count; i++) {
|
2005-01-24 18:25:02 +00:00
|
|
|
Parameter p = Parameters [i];
|
2004-05-07 13:42:59 +00:00
|
|
|
string indent = "\t\t\t\t";
|
2005-03-03 22:40:32 +00:00
|
|
|
if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) {
|
2004-05-07 13:42:59 +00:00
|
|
|
sw.WriteLine (indent + "if (" + p.Name + " != null) {");
|
|
|
|
indent += "\t";
|
|
|
|
}
|
|
|
|
sw.WriteLine (indent + "names.Add (\"" + names [i] + "\");");
|
|
|
|
sw.Write (indent + "vals.Add (");
|
|
|
|
|
|
|
|
if (table.IsEnum (p.CType))
|
|
|
|
sw.WriteLine ("new GLib.Value (this, \"" + names[i] + "\", new GLib.EnumWrapper ((int)" + values[i] + ", " + (table.IsEnumFlags (p.CType) ? "true" : "false") + ")));");
|
|
|
|
else
|
|
|
|
sw.WriteLine ("new GLib.Value (" + values[i] + "));");
|
|
|
|
|
2005-03-03 22:40:32 +00:00
|
|
|
if (p.Generatable is ClassBase && !(p.Generatable is StructBase))
|
2004-05-07 13:42:59 +00:00
|
|
|
sw.WriteLine ("\t\t\t\t}");
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\t\tCreateNativeObject ((string[])names.ToArray (typeof (string)), (GLib.Value[])vals.ToArray (typeof (GLib.Value)));");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn;");
|
|
|
|
} else
|
|
|
|
sw.WriteLine ("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");");
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
}
|
|
|
|
|
2005-01-24 18:25:02 +00:00
|
|
|
Body.Initialize(gen_info, false, false, "");
|
|
|
|
sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, CName, Body.GetCallString (false));
|
2005-03-08 21:28:08 +00:00
|
|
|
Body.Finish (sw, "");
|
2005-01-24 18:25:02 +00:00
|
|
|
Body.HandleException (sw, "");
|
2002-05-23 23:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
|
|
|
|
Statistics.CtorCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|