static void GenerateWidgetCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, GenerationOptions options, List<SteticCompilationUnit> units, Gtk.Widget w, ArrayList warnings) { // Generate the build method CodeTypeDeclaration type = CreatePartialClass (globalUnit, units, options, w.Name); CodeMemberMethod met = new CodeMemberMethod (); met.Name = "Build"; type.Members.Add (met); met.ReturnType = new CodeTypeReference (typeof(void)); met.Attributes = MemberAttributes.Family; if (options.GenerateEmptyBuildMethod) { GenerateWrapperFields (type, Wrapper.Widget.Lookup (w)); return; } met.Statements.Add ( new CodeMethodInvokeExpression ( new CodeTypeReferenceExpression (globalNs.Name + ".Gui"), "Initialize", new CodeThisReferenceExpression () ) ); Stetic.Wrapper.Widget wwidget = Stetic.Wrapper.Widget.Lookup (w); if (wwidget.GeneratePublic) type.TypeAttributes = TypeAttributes.Public; else type.TypeAttributes = TypeAttributes.NotPublic; Stetic.WidgetMap map = Stetic.CodeGenerator.GenerateCreationCode (globalNs, type, w, new CodeThisReferenceExpression (), met.Statements, options, warnings); CodeGenerator.BindSignalHandlers (new CodeThisReferenceExpression (), wwidget, map, met.Statements, options); }
static CodeTypeDeclaration CreatePartialClass(SteticCompilationUnit globalUnit, List <SteticCompilationUnit> units, GenerationOptions options, string name) { SteticCompilationUnit unit; if (options.GenerateSingleFile) { unit = globalUnit; } else { unit = new SteticCompilationUnit(name); units.Add(unit); } string ns = ""; int i = name.LastIndexOf('.'); if (i != -1) { ns = name.Substring(0, i); name = name.Substring(i + 1); } CodeTypeDeclaration type = new CodeTypeDeclaration(name); type.IsPartial = true; type.Attributes = MemberAttributes.Public; type.TypeAttributes = TypeAttributes.Public; CodeNamespace cns = new CodeNamespace(ns); cns.Types.Add(type); unit.Namespaces.Add(cns); return(type); }
static CodeTypeDeclaration CreatePartialClass (SteticCompilationUnit globalUnit, List<SteticCompilationUnit> units, GenerationOptions options, string name) { SteticCompilationUnit unit; if (options.GenerateSingleFile) unit = globalUnit; else { unit = new SteticCompilationUnit (name); units.Add (unit); } string ns = ""; int i = name.LastIndexOf ('.'); if (i != -1) { ns = name.Substring (0, i); name = name.Substring (i+1); } CodeTypeDeclaration type = new CodeTypeDeclaration (name); type.IsPartial = true; type.Attributes = MemberAttributes.Public; type.TypeAttributes = TypeAttributes.Public; CodeNamespace cns = new CodeNamespace (ns); cns.Types.Add (type); unit.Namespaces.Add (cns); return type; }
static void GenerateComponentCode(object component, SteticCompilationUnit globalUnit, CodeNamespace globalNs, CodeExpression cobj, CodeStatementCollection statements, CodeTypeDeclaration globalType, GenerationOptions options, List <SteticCompilationUnit> units, CodeIdentifiers ids, ArrayList warnings) { Gtk.Widget widget = component as Gtk.Widget; Wrapper.Widget wwidget = Stetic.Wrapper.Widget.Lookup(widget); Wrapper.ActionGroup agroup = component as Wrapper.ActionGroup; string name = widget != null ? widget.Name : agroup.Name; string internalClassName = ids.MakeUnique(CodeIdentifier.MakeValid(name)); string typeName = widget != null ? wwidget.WrappedTypeName : "Gtk.ActionGroup"; // Create the build method for the top level CodeMemberMethod met; met = GetBuildMethod(name, internalClassName, typeName, globalUnit, options, units); // Generate the build code CodeVariableDeclarationStatement varDecHash = new CodeVariableDeclarationStatement(typeof(System.Collections.Hashtable), "bindings"); met.Statements.Add(varDecHash); varDecHash.InitExpression = new CodeObjectCreateExpression( typeof(System.Collections.Hashtable), new CodeExpression [0] ); CodeVariableReferenceExpression targetObjectVar = new CodeVariableReferenceExpression("cobj"); Stetic.WidgetMap map; if (widget != null) { map = Stetic.CodeGenerator.GenerateCreationCode(globalNs, globalType, widget, targetObjectVar, met.Statements, options, warnings); CodeGenerator.BindSignalHandlers(targetObjectVar, wwidget, map, met.Statements, options); } else { map = Stetic.CodeGenerator.GenerateCreationCode(globalNs, globalType, agroup, targetObjectVar, met.Statements, options, warnings); foreach (Wrapper.Action ac in agroup.Actions) { CodeGenerator.BindSignalHandlers(targetObjectVar, ac, map, met.Statements, options); } } GenerateBindFieldCode(met.Statements, cobj); // Add a method call to the build method statements.Add( new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(options.GlobalNamespace + ".SteticGenerated." + internalClassName), "Build", new CodeCastExpression(typeName, cobj) ) ); }
static void GenerateWidgetCode (SteticCompilationUnit globalUnit, CodeNamespace globalNs, GenerationOptions options, List<SteticCompilationUnit> units, Gtk.Widget w, ArrayList warnings, bool regenerateWidgetClass) { if (options.GenerateSingleFile) regenerateWidgetClass = true; // Don't register this unit if the class doesn't need to be regenerated if (!regenerateWidgetClass) units = null; CodeTypeDeclaration type = CreatePartialClass (globalUnit, units, options, w.Name); // Generate the build method CodeMemberMethod met = new CodeMemberMethod (); met.Name = "Build"; type.Members.Add (met); met.ReturnType = new CodeTypeReference (typeof(void)); met.Attributes = MemberAttributes.Family; Stetic.Wrapper.Widget wwidget = Stetic.Wrapper.Widget.Lookup (w); if (regenerateWidgetClass) { if (options.GenerateEmptyBuildMethod) { GenerateWrapperFields (type, wwidget); return; } met.Statements.Add ( new CodeMethodInvokeExpression ( new CodeTypeReferenceExpression (new CodeTypeReference (globalNs.Name + ".Gui", CodeTypeReferenceOptions.GlobalReference)), "Initialize", new CodeThisReferenceExpression () ) ); if (wwidget.GeneratePublic) type.TypeAttributes = TypeAttributes.Public; else type.TypeAttributes = TypeAttributes.NotPublic; if (!String.IsNullOrEmpty (wwidget.UIManagerName)) type.Members.Add (new CodeMemberField (new CodeTypeReference ("Gtk.UIManager", CodeTypeReferenceOptions.GlobalReference), wwidget.UIManagerName)); } // We need to generate the creation code even if regenerateWidgetClass is false because GenerateCreationCode // may inject support classes or methods into the global namespace, which is always generated Stetic.WidgetMap map = Stetic.CodeGenerator.GenerateCreationCode (globalNs, type, w, new CodeThisReferenceExpression (), met.Statements, options, warnings); if (regenerateWidgetClass) CodeGenerator.BindSignalHandlers (new CodeThisReferenceExpression (), wwidget, map, met.Statements, options); }
public static void GenerateProjectGuiCode (SteticCompilationUnit globalUnit, CodeNamespace globalNs, CodeTypeDeclaration globalType, GenerationOptions options, List<SteticCompilationUnit> units, ProjectBackend[] projects, ArrayList warnings) { // Generate code for each project foreach (ProjectBackend gp in projects) { // Generate top levels foreach (Gtk.Widget w in gp.Toplevels) GenerateWidgetCode (globalUnit, globalNs, options, units, w, warnings); // Generate global action groups foreach (Wrapper.ActionGroup agroup in gp.ActionGroups) GenerateGlobalActionGroupCode (globalUnit, globalNs, options, units, agroup, warnings); } }
static void GenerateGlobalActionGroupCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, GenerationOptions options, List<SteticCompilationUnit> units, Wrapper.ActionGroup agroup, ArrayList warnings) { CodeTypeDeclaration type = CreatePartialClass (globalUnit, units, options, agroup.Name); // Generate the build method CodeMemberMethod met = new CodeMemberMethod (); met.Name = "Build"; type.Members.Add (met); met.ReturnType = new CodeTypeReference (typeof(void)); met.Attributes = MemberAttributes.Public; Stetic.WidgetMap map = Stetic.CodeGenerator.GenerateCreationCode (globalNs, type, agroup, new CodeThisReferenceExpression (), met.Statements, options, warnings); foreach (Wrapper.Action ac in agroup.Actions) CodeGenerator.BindSignalHandlers (new CodeThisReferenceExpression (), ac, map, met.Statements, options); }
static void GenerateWidgetCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, GenerationOptions options, List <SteticCompilationUnit> units, Gtk.Widget w, ArrayList warnings) { // Generate the build method CodeTypeDeclaration type = CreatePartialClass(globalUnit, units, options, w.Name); CodeMemberMethod met = new CodeMemberMethod(); met.Name = "Build"; type.Members.Add(met); met.ReturnType = new CodeTypeReference(typeof(void)); met.Attributes = MemberAttributes.Family; Stetic.Wrapper.Widget wwidget = Stetic.Wrapper.Widget.Lookup(w); if (options.GenerateEmptyBuildMethod) { GenerateWrapperFields(type, wwidget); return; } met.Statements.Add( new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(new CodeTypeReference(globalNs.Name + ".Gui", CodeTypeReferenceOptions.GlobalReference)), "Initialize", new CodeThisReferenceExpression() ) ); if (wwidget.GeneratePublic) { type.TypeAttributes = TypeAttributes.Public; } else { type.TypeAttributes = TypeAttributes.NotPublic; } if (!String.IsNullOrEmpty(wwidget.UIManagerName)) { type.Members.Add(new CodeMemberField(new CodeTypeReference("Gtk.UIManager", CodeTypeReferenceOptions.GlobalReference), wwidget.UIManagerName)); } Stetic.WidgetMap map = Stetic.CodeGenerator.GenerateCreationCode(globalNs, type, w, new CodeThisReferenceExpression(), met.Statements, options, warnings); CodeGenerator.BindSignalHandlers(new CodeThisReferenceExpression(), wwidget, map, met.Statements, options); }
public static void GenerateProjectGuiCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, CodeTypeDeclaration globalType, GenerationOptions options, List <SteticCompilationUnit> units, ProjectBackend[] projects, ArrayList warnings) { // Generate code for each project foreach (ProjectBackend gp in projects) { // Generate top levels foreach (Gtk.Widget w in gp.Toplevels) { GenerateWidgetCode(globalUnit, globalNs, options, units, w, warnings); } // Generate global action groups foreach (Wrapper.ActionGroup agroup in gp.ActionGroups) { GenerateGlobalActionGroupCode(globalUnit, globalNs, options, units, agroup, warnings); } } }
static void GenerateGlobalActionGroupCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, GenerationOptions options, List <SteticCompilationUnit> units, Wrapper.ActionGroup agroup, ArrayList warnings) { CodeTypeDeclaration type = CreatePartialClass(globalUnit, units, options, agroup.Name); // Generate the build method CodeMemberMethod met = new CodeMemberMethod(); met.Name = "Build"; type.Members.Add(met); met.ReturnType = new CodeTypeReference(typeof(void)); met.Attributes = MemberAttributes.Public; Stetic.WidgetMap map = Stetic.CodeGenerator.GenerateCreationCode(globalNs, type, agroup, new CodeThisReferenceExpression(), met.Statements, options, warnings); foreach (Wrapper.Action ac in agroup.Actions) { CodeGenerator.BindSignalHandlers(new CodeThisReferenceExpression(), ac, map, met.Statements, options); } }
static CodeMemberMethod GetBuildMethod(string name, string internalClassName, string typeName, SteticCompilationUnit globalUnit, GenerationOptions options, List <SteticCompilationUnit> units) { SteticCompilationUnit unit; if (options.GenerateSingleFile) { unit = globalUnit; } else { unit = new SteticCompilationUnit(name); units.Add(unit); } CodeTypeDeclaration type = new CodeTypeDeclaration(internalClassName); type.Attributes = MemberAttributes.Private; type.TypeAttributes = TypeAttributes.NestedAssembly; CodeNamespace cns = new CodeNamespace(options.GlobalNamespace + ".SteticGenerated"); cns.Types.Add(type); unit.Namespaces.Add(cns); // Create the build method for the component CodeMemberMethod met = new CodeMemberMethod(); met.Name = "Build"; type.Members.Add(met); met.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeName), "cobj")); met.ReturnType = new CodeTypeReference(typeof(void)); met.Attributes = MemberAttributes.Public | MemberAttributes.Static; return(met); }
static void GenerateComponentCode(object component, SteticCompilationUnit globalUnit, CodeNamespace globalNs, CodeExpression cobj, CodeStatementCollection statements, CodeTypeDeclaration globalType, GenerationOptions options, List<SteticCompilationUnit> units, CodeIdentifiers ids, ArrayList warnings) { Gtk.Widget widget = component as Gtk.Widget; Wrapper.Widget wwidget = Stetic.Wrapper.Widget.Lookup (widget); Wrapper.ActionGroup agroup = component as Wrapper.ActionGroup; string name = widget != null ? widget.Name : agroup.Name; string internalClassName = ids.MakeUnique (CodeIdentifier.MakeValid (name)); string typeName = widget != null ? wwidget.WrappedTypeName : "Gtk.ActionGroup"; // Create the build method for the top level CodeMemberMethod met; met = GetBuildMethod (name, internalClassName, typeName, globalUnit, options, units); // Generate the build code CodeVariableDeclarationStatement varDecHash = new CodeVariableDeclarationStatement (typeof(System.Collections.Hashtable), "bindings"); met.Statements.Add (varDecHash); varDecHash.InitExpression = new CodeObjectCreateExpression ( typeof(System.Collections.Hashtable), new CodeExpression [0] ); CodeVariableReferenceExpression targetObjectVar = new CodeVariableReferenceExpression ("cobj"); Stetic.WidgetMap map; if (widget != null) { map = Stetic.CodeGenerator.GenerateCreationCode (globalNs, globalType, widget, targetObjectVar, met.Statements, options, warnings); CodeGenerator.BindSignalHandlers (targetObjectVar, wwidget, map, met.Statements, options); } else { map = Stetic.CodeGenerator.GenerateCreationCode (globalNs, globalType, agroup, targetObjectVar, met.Statements, options, warnings); foreach (Wrapper.Action ac in agroup.Actions) CodeGenerator.BindSignalHandlers (targetObjectVar, ac, map, met.Statements, options); } GenerateBindFieldCode (met.Statements, cobj); // Add a method call to the build method statements.Add ( new CodeMethodInvokeExpression ( new CodeTypeReferenceExpression (options.GlobalNamespace + ".SteticGenerated." + internalClassName), "Build", new CodeCastExpression (typeName, cobj) ) ); }
public static void GenerateProjectGuiCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, CodeTypeDeclaration globalType, GenerationOptions options, List <SteticCompilationUnit> units, ProjectBackend[] projects, ArrayList warnings) { bool multiProject = projects.Length > 1; // Build method overload that takes a type as parameter. CodeMemberMethod met = new CodeMemberMethod(); met.Name = "Build"; globalType.Members.Add(met); met.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "cobj")); met.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Type), "type")); if (multiProject) { met.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "file")); } met.ReturnType = new CodeTypeReference(typeof(void)); met.Attributes = MemberAttributes.Public | MemberAttributes.Static; CodeMethodInvokeExpression call = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeTypeReferenceExpression(globalNs.Name + ".Gui"), "Build" ), new CodeArgumentReferenceExpression("cobj"), new CodePropertyReferenceExpression( new CodeArgumentReferenceExpression("type"), "FullName" ) ); if (multiProject) { call.Parameters.Add(new CodeArgumentReferenceExpression("file")); } met.Statements.Add(call); // Generate the build method met = new CodeMemberMethod(); met.Name = "Build"; globalType.Members.Add(met); met.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "cobj")); met.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "id")); if (multiProject) { met.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "file")); } met.ReturnType = new CodeTypeReference(typeof(void)); met.Attributes = MemberAttributes.Public | MemberAttributes.Static; if (options.GenerateEmptyBuildMethod) { return; } CodeArgumentReferenceExpression cobj = new CodeArgumentReferenceExpression("cobj"); CodeArgumentReferenceExpression cfile = new CodeArgumentReferenceExpression("file"); CodeArgumentReferenceExpression cid = new CodeArgumentReferenceExpression("id"); CodeStatementCollection projectCol = met.Statements; CodeConditionStatement tcond = new CodeConditionStatement(); tcond.Condition = new CodeMethodInvokeExpression(new CodeTypeOfExpression(typeof(Gtk.Widget)), "IsAssignableFrom", cobj); tcond.TrueStatements.Add( new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(globalNs.Name + ".Gui"), "Initialize", cobj ) ); // Generate code for each project foreach (ProjectBackend gp in projects) { CodeStatementCollection widgetCol; if (multiProject) { CodeConditionStatement pcond = new CodeConditionStatement(); pcond.Condition = new CodeBinaryOperatorExpression( cfile, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(gp.Id) ); projectCol.Add(pcond); widgetCol = pcond.TrueStatements; projectCol = pcond.FalseStatements; } else { widgetCol = projectCol; } // Generate top levels CodeIdentifiers ids = new CodeIdentifiers(); foreach (Gtk.Widget w in gp.Toplevels) { CodeConditionStatement cond = new CodeConditionStatement(); cond.Condition = new CodeBinaryOperatorExpression( cid, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(w.Name) ); widgetCol.Add(cond); GenerateComponentCode(w, globalUnit, globalNs, cobj, cond.TrueStatements, globalType, options, units, ids, warnings); widgetCol = cond.FalseStatements; } // Generate action groups foreach (Wrapper.ActionGroup agroup in gp.ActionGroups) { CodeConditionStatement cond = new CodeConditionStatement(); cond.Condition = new CodeBinaryOperatorExpression( cid, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(agroup.Name) ); widgetCol.Add(cond); GenerateComponentCode(agroup, globalUnit, globalNs, cobj, cond.TrueStatements, globalType, options, units, ids, warnings); widgetCol = cond.FalseStatements; } } }
static CodeMemberMethod GetBuildMethod(string name, string internalClassName, string typeName, SteticCompilationUnit globalUnit, GenerationOptions options, List<SteticCompilationUnit> units) { SteticCompilationUnit unit; if (options.GenerateSingleFile) unit = globalUnit; else { unit = new SteticCompilationUnit (name); units.Add (unit); } CodeTypeDeclaration type = new CodeTypeDeclaration (internalClassName); type.Attributes = MemberAttributes.Private; type.TypeAttributes = TypeAttributes.NestedAssembly; CodeNamespace cns = new CodeNamespace (options.GlobalNamespace + ".SteticGenerated"); cns.Types.Add (type); unit.Namespaces.Add (cns); // Create the build method for the component CodeMemberMethod met = new CodeMemberMethod (); met.Name = "Build"; type.Members.Add (met); met.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference (typeName), "cobj")); met.ReturnType = new CodeTypeReference (typeof(void)); met.Attributes = MemberAttributes.Public | MemberAttributes.Static; return met; }
internal CodeGenerationResult (SteticCompilationUnit[] units, string[] warnings) { this.units = units; this.warnings = warnings; }
public static CodeGenerationResult GenerateProjectCode (GenerationOptions options, ProjectBackend[] projects) { ArrayList warningList = new ArrayList (); List<SteticCompilationUnit> units = new List<SteticCompilationUnit> (); SteticCompilationUnit globalUnit = new SteticCompilationUnit (""); units.Add (globalUnit); if (options == null) options = new GenerationOptions (); CodeNamespace globalNs = new CodeNamespace (options.GlobalNamespace); globalUnit.Namespaces.Add (globalNs); // Global class CodeTypeDeclaration globalType = new CodeTypeDeclaration ("Gui"); globalType.Attributes = MemberAttributes.Private; globalType.TypeAttributes = TypeAttributes.NestedAssembly; globalNs.Types.Add (globalType); // Create the project initialization method // This method will only be added at the end if there // is actually something to initialize CodeMemberMethod initMethod = new CodeMemberMethod (); initMethod.Name = "Initialize"; initMethod.ReturnType = new CodeTypeReference (typeof(void)); initMethod.Attributes = MemberAttributes.Assembly | MemberAttributes.Static; initMethod.Parameters.Add (new CodeParameterDeclarationExpression (typeof(Gtk.Widget), "iconRenderer")); GeneratorContext initContext = new ProjectGeneratorContext (globalNs, globalType, initMethod.Statements, options); initContext.RootObject = new CodeArgumentReferenceExpression ("iconRenderer"); // Generate icon factory creation foreach (ProjectBackend gp in projects) { if (gp.IconFactory.Icons.Count > 0) gp.IconFactory.GenerateBuildCode (initContext); } warningList.AddRange (initContext.Warnings); // Generate the code if (options.UsePartialClasses) CodeGeneratorPartialClass.GenerateProjectGuiCode (globalUnit, globalNs, globalType, options, units, projects, warningList); else CodeGeneratorInternalClass.GenerateProjectGuiCode (globalUnit, globalNs, globalType, options, units, projects, warningList); GenerateProjectActionsCode (globalNs, options, projects); // Final step. If there is some initialization code, add all needed infrastructure globalType.Members.Add (initMethod); CodeMemberField initField = new CodeMemberField (typeof(bool), "initialized"); initField.Attributes = MemberAttributes.Private | MemberAttributes.Static; globalType.Members.Add (initField); CodeFieldReferenceExpression initVar = new CodeFieldReferenceExpression ( new CodeTypeReferenceExpression (globalNs.Name + ".Gui"), "initialized" ); CodeConditionStatement initCondition = new CodeConditionStatement (); initCondition.Condition = new CodeBinaryOperatorExpression ( initVar, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression (false) ); initCondition.TrueStatements.Add (new CodeAssignStatement ( initVar, new CodePrimitiveExpression (true) )); initCondition.TrueStatements.AddRange (initMethod.Statements); initMethod.Statements.Clear (); initMethod.Statements.Add (initCondition); return new CodeGenerationResult (units.ToArray (), (string[]) warningList.ToArray (typeof(string))); }
// public static void GenerateProjectCode (string file, CodeDomProvider provider, GenerationOptions options, ProjectBackend[] projects) // { // CodeGenerationResult res = GenerateProjectCode (options, projects); // // string basePath = Path.GetDirectoryName (file); // // foreach (SteticCompilationUnit unit in res.Units) { // string fname; // if (unit.Name.Length == 0) // fname = file; // else // fname = Path.Combine (basePath, unit.Name); // StreamWriter fileStream = new StreamWriter (fname); // try { // provider.GenerateCodeFromCompileUnit (unit, fileStream, new CodeGeneratorOptions ()); // } finally { // fileStream.Close (); // } // } // } public static CodeGenerationResult GenerateProjectCode(GenerationOptions options, ProjectBackend[] projects) { ArrayList warningList = new ArrayList(); List <SteticCompilationUnit> units = new List <SteticCompilationUnit> (); // SteticCompilationUnit globalUnit = new SteticCompilationUnit (""); // units.Add (globalUnit); if (options == null) { options = new GenerationOptions(); } CodeNamespace globalNs = new CodeNamespace(options.GlobalNamespace); // globalUnit.Namespaces.Add (globalNs); // Global class CodeTypeDeclaration globalType = new CodeTypeDeclaration("Gui"); globalType.Attributes = MemberAttributes.Private; globalType.TypeAttributes = TypeAttributes.NestedAssembly; globalNs.Types.Add(globalType); // Create the project initialization method // This method will only be added at the end if there // is actually something to initialize CodeMemberMethod initMethod = new CodeMemberMethod(); initMethod.Name = "Initialize"; initMethod.ReturnType = new CodeTypeReference(typeof(void)); initMethod.Attributes = MemberAttributes.Assembly | MemberAttributes.Static; initMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Gtk.Widget), "iconRenderer")); GeneratorContext initContext = new ProjectGeneratorContext(globalNs, globalType, initMethod.Statements, options); initContext.RootObject = new CodeArgumentReferenceExpression("iconRenderer"); // Generate icon factory creation foreach (ProjectBackend gp in projects) { if (gp.IconFactory.Icons.Count > 0) { gp.IconFactory.GenerateBuildCode(initContext); } } warningList.AddRange(initContext.Warnings); // Generate the code CodeGeneratorPartialClass.GenerateProjectGuiCode(globalNs, globalType, options, units, projects, warningList); GenerateProjectActionsCode(globalNs, options, projects); // Final step. If there is some initialization code, add all needed infrastructure globalType.Members.Add(initMethod); CodeMemberField initField = new CodeMemberField(typeof(bool), "initialized"); initField.Attributes = MemberAttributes.Private | MemberAttributes.Static; globalType.Members.Add(initField); CodeFieldReferenceExpression initVar = new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(globalNs.Name + ".Gui"), "initialized" ); CodeConditionStatement initCondition = new CodeConditionStatement(); initCondition.Condition = new CodeBinaryOperatorExpression( initVar, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(false) ); initCondition.TrueStatements.Add(new CodeAssignStatement( initVar, new CodePrimitiveExpression(true) )); initCondition.TrueStatements.AddRange(initMethod.Statements); initMethod.Statements.Clear(); initMethod.Statements.Add(initCondition); //create separate compilation unit for each type in the global namespace //and insert them at the begining of the units list. int index = 0; foreach (CodeTypeDeclaration type in globalNs.Types) { SteticCompilationUnit unit = new SteticCompilationUnit(type.Name); CodeNamespace ns = new CodeNamespace(globalNs.Name); ns.Types.Add(type); unit.Namespaces.Add(ns); units.Insert(index++, unit); } return(new CodeGenerationResult(units.ToArray(), (string[])warningList.ToArray(typeof(string)))); }
public static void GenerateProjectGuiCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, CodeTypeDeclaration globalType, GenerationOptions options, List<SteticCompilationUnit> units, ProjectBackend[] projects, ArrayList warnings) { bool multiProject = projects.Length > 1; // Build method overload that takes a type as parameter. CodeMemberMethod met = new CodeMemberMethod (); met.Name = "Build"; globalType.Members.Add (met); met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(object), "cobj")); met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(Type), "type")); if (multiProject) met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(string), "file")); met.ReturnType = new CodeTypeReference (typeof(void)); met.Attributes = MemberAttributes.Public | MemberAttributes.Static; CodeMethodInvokeExpression call = new CodeMethodInvokeExpression ( new CodeMethodReferenceExpression ( new CodeTypeReferenceExpression (globalNs.Name + ".Gui"), "Build" ), new CodeArgumentReferenceExpression ("cobj"), new CodePropertyReferenceExpression ( new CodeArgumentReferenceExpression ("type"), "FullName" ) ); if (multiProject) call.Parameters.Add (new CodeArgumentReferenceExpression ("file")); met.Statements.Add (call); // Generate the build method met = new CodeMemberMethod (); met.Name = "Build"; globalType.Members.Add (met); met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(object), "cobj")); met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(string), "id")); if (multiProject) met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(string), "file")); met.ReturnType = new CodeTypeReference (typeof(void)); met.Attributes = MemberAttributes.Public | MemberAttributes.Static; if (options.GenerateEmptyBuildMethod) return; CodeArgumentReferenceExpression cobj = new CodeArgumentReferenceExpression ("cobj"); CodeArgumentReferenceExpression cfile = new CodeArgumentReferenceExpression ("file"); CodeArgumentReferenceExpression cid = new CodeArgumentReferenceExpression ("id"); CodeStatementCollection projectCol = met.Statements; CodeConditionStatement tcond = new CodeConditionStatement (); tcond.Condition = new CodeMethodInvokeExpression (new CodeTypeOfExpression (typeof(Gtk.Widget)), "IsAssignableFrom", cobj); tcond.TrueStatements.Add ( new CodeMethodInvokeExpression ( new CodeTypeReferenceExpression (globalNs.Name + ".Gui"), "Initialize", cobj ) ); // Generate code for each project foreach (ProjectBackend gp in projects) { CodeStatementCollection widgetCol; if (multiProject) { CodeConditionStatement pcond = new CodeConditionStatement (); pcond.Condition = new CodeBinaryOperatorExpression ( cfile, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression (gp.Id) ); projectCol.Add (pcond); widgetCol = pcond.TrueStatements; projectCol = pcond.FalseStatements; } else { widgetCol = projectCol; } // Generate top levels CodeIdentifiers ids = new CodeIdentifiers (); foreach (Gtk.Widget w in gp.Toplevels) { CodeConditionStatement cond = new CodeConditionStatement (); cond.Condition = new CodeBinaryOperatorExpression ( cid, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression (w.Name) ); widgetCol.Add (cond); GenerateComponentCode (w, globalUnit, globalNs, cobj, cond.TrueStatements, globalType, options, units, ids, warnings); widgetCol = cond.FalseStatements; } // Generate action groups foreach (Wrapper.ActionGroup agroup in gp.ActionGroups) { CodeConditionStatement cond = new CodeConditionStatement (); cond.Condition = new CodeBinaryOperatorExpression ( cid, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression (agroup.Name) ); widgetCol.Add (cond); GenerateComponentCode (agroup, globalUnit, globalNs, cobj, cond.TrueStatements, globalType, options, units, ids, warnings); widgetCol = cond.FalseStatements; } } }
static void GenerateWidgetCode(SteticCompilationUnit globalUnit, CodeNamespace globalNs, GenerationOptions options, List <SteticCompilationUnit> units, Gtk.Widget w, ArrayList warnings, bool regenerateWidgetClass) { if (options.GenerateSingleFile) { regenerateWidgetClass = true; } // Don't register this unit if the class doesn't need to be regenerated if (!regenerateWidgetClass) { units = null; } CodeTypeDeclaration type = CreatePartialClass(globalUnit, units, options, w.Name); // Generate the build method CodeMemberMethod met = new CodeMemberMethod(); met.Name = "Build"; type.Members.Add(met); met.ReturnType = new CodeTypeReference(typeof(void)); met.Attributes = MemberAttributes.Family; Stetic.Wrapper.Widget wwidget = Stetic.Wrapper.Widget.Lookup(w); if (regenerateWidgetClass) { if (options.GenerateEmptyBuildMethod) { GenerateWrapperFields(type, wwidget); return; } met.Statements.Add( new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(new CodeTypeReference(globalNs.Name + ".Gui", CodeTypeReferenceOptions.GlobalReference)), "Initialize", new CodeThisReferenceExpression() ) ); if (wwidget.GeneratePublic) { type.TypeAttributes = TypeAttributes.Public; } else { type.TypeAttributes = TypeAttributes.NotPublic; } if (!String.IsNullOrEmpty(wwidget.UIManagerName)) { type.Members.Add(new CodeMemberField(new CodeTypeReference("Gtk.UIManager", CodeTypeReferenceOptions.GlobalReference), wwidget.UIManagerName)); } } // We need to generate the creation code even if regenerateWidgetClass is false because GenerateCreationCode // may inject support classes or methods into the global namespace, which is always generated Stetic.WidgetMap map = Stetic.CodeGenerator.GenerateCreationCode(globalNs, type, w, new CodeThisReferenceExpression(), met.Statements, options, warnings); if (regenerateWidgetClass) { CodeGenerator.BindSignalHandlers(new CodeThisReferenceExpression(), wwidget, map, met.Statements, options); } }