diff --git a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CSharpDesignerGenerator.cs b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CSharpDesignerGenerator.cs index 38b720d7b6..cf3f3d573f 100644 --- a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CSharpDesignerGenerator.cs +++ b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CSharpDesignerGenerator.cs @@ -53,6 +53,8 @@ namespace ICSharpCode.FormDesigner FormDesignerViewContent viewContent; bool failedDesignerInitialize = false; + string NonVisualComponentContainerName = "components"; + public void Attach(FormDesignerViewContent viewContent) { this.viewContent = viewContent; @@ -93,6 +95,11 @@ namespace ICSharpCode.FormDesigner Reparse(viewContent.Document.TextContent); int endOffset = viewContent.Document.PositionToOffset(new Point(0, initializeComponents.BodyRegion.EndLine)); viewContent.Document.Insert(endOffset, "\t\tprivate " + e.Component.GetType() + " " + e.Component.Site.Name + ";" + Environment.NewLine); + if (CodeDOMGenerator.IsNonVisualComponent(viewContent.Host, e.Component)) { + if (!IsNonVisualComponentContainerDefined) { + viewContent.Document.Insert(endOffset, "\t\tprivate " + typeof(Container) + " " + NonVisualComponentContainerName + ";" + Environment.NewLine); + } + } } catch (Exception ex) { MessageService.ShowError(ex); } @@ -270,5 +277,22 @@ namespace ICSharpCode.FormDesigner } return compatibleMethods; } + + bool IsNonVisualComponentContainerDefined + { + get { + return GetField(c, NonVisualComponentContainerName) != null; + } + } + + IField GetField(IClass c, string name) + { + foreach (IField field in c.Fields) { + if (field.Name == name) { + return field; + } + } + return null; + } } } diff --git a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CodeDOMGenerator.cs b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CodeDOMGenerator.cs index 889eee2b72..226825ba20 100644 --- a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CodeDOMGenerator.cs +++ b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CodeDOMGenerator.cs @@ -9,9 +9,11 @@ using System; using System.IO; using System.Xml; using System.Collections; +using System.Collections.Generic; using System.Drawing; using System.Reflection; using System.Windows.Forms; +using System.Windows.Forms.Design; using System.ComponentModel; using System.ComponentModel.Design; using System.ComponentModel.Design.Serialization; @@ -38,6 +40,7 @@ namespace ICSharpCode.FormDesigner CodeDomProvider codeProvider; CodeDOMGeneratorUtility codeDOMGeneratorUtility = new CodeDOMGeneratorUtility(); + List addedVariables = new List(); public CodeDOMGenerator(IDesignerHost host, CodeDomProvider codeProvider) { @@ -52,30 +55,52 @@ namespace ICSharpCode.FormDesigner IDisposable session = serializationManager.CreateSession(); DesignerResourceService designerResourceService = (DesignerResourceService)host.GetService(typeof(System.ComponentModel.Design.IResourceService)); designerResourceService.SerializationStarted(true); + + addedVariables.Clear(); + + foreach (IComponent component in host.Container.Components) { + if (!IsComponentAdded(component)) { + GenerateComponentCode(component, writer, serializationManager); + } + } - Type componentType = host.RootComponent.GetType(); - ExpressionContext exprContext = new ExpressionContext(new CodeThisReferenceExpression(), componentType, host.RootComponent, host.RootComponent); + designerResourceService.SerializationEnded(true); + session.Dispose(); + LoggingService.Info("End CodeCOMGenerator.ConvertContentDefinition"); + } + + public static bool IsNonVisualComponent(IDesignerHost host, IComponent component) + { + IDesigner designer = host.GetDesigner(component); + return !(designer is ControlDesigner); + } + + void GenerateComponentCode(IComponent component, TextWriter writer, DesignerSerializationManager serializationManager) + { + Type componentType = component.GetType(); + ExpressionContext exprContext = new ExpressionContext(new CodeThisReferenceExpression(), componentType, component, component); ((IDesignerSerializationManager)serializationManager).Context.Append(exprContext); - CodeDomSerializer rootSerializer = (CodeDomSerializer)serializationManager.GetSerializer(componentType, typeof(CodeDomSerializer)); + CodeDomSerializer serializer = (CodeDomSerializer)serializationManager.GetSerializer(componentType, typeof(CodeDomSerializer)); - if (rootSerializer == null) { - throw new Exception("No root serializer found"); + if (serializer == null) { + throw new Exception("No serializer found for component type=" + componentType.ToString()); } - ICollection statements = rootSerializer.Serialize(serializationManager, host.RootComponent) as ICollection; + ICollection statements = serializer.Serialize(serializationManager, component) as ICollection; CodeGeneratorOptions options = codeDOMGeneratorUtility.CreateCodeGeneratorOptions; options.IndentString = "\t\t\t"; // ICSharpCode.NRefactory.Parser.CodeDOMVerboseOutputGenerator outputGenerator = new ICSharpCode.NRefactory.Parser.CodeDOMVerboseOutputGenerator(); - + foreach (CodeStatement statement in statements) { - if (!(statement is CodeVariableDeclarationStatement)) { + if ((statement is CodeVariableDeclarationStatement)) { + addedVariables.Add(((CodeVariableDeclarationStatement)statement).Name); + } else { // indentation isn't generated when calling GenerateCodeFromStatement writer.Write(options.IndentString); - try { + try { // outputGenerator.PublicGenerateCodeFromStatement(statement, Console.Out, options); - codeProvider.GenerateCodeFromStatement(statement, writer, options); } catch (Exception e) { codeProvider.GenerateCodeFromStatement(new CodeCommentStatement("TODO: Error while generating statement : " + e.Message), @@ -84,9 +109,14 @@ namespace ICSharpCode.FormDesigner } } } - designerResourceService.SerializationEnded(true); - session.Dispose(); - LoggingService.Info("End CodeCOMGenerator.ConvertContentDefinition"); + } + + bool IsComponentAdded(IComponent component) + { + if (component.Site != null) { + return addedVariables.Contains(component.Site.Name); + } + return false; } } } diff --git a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/VBNetDesignerGenerator.cs b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/VBNetDesignerGenerator.cs index 2c49f528e8..1a3bd40705 100644 --- a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/VBNetDesignerGenerator.cs +++ b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/VBNetDesignerGenerator.cs @@ -53,6 +53,8 @@ namespace ICSharpCode.FormDesigner FormDesignerViewContent viewContent; bool failedDesignerInitialize = false; + string NonVisualComponentContainerName = "components"; + public void Attach(FormDesignerViewContent viewContent) { this.viewContent = viewContent; @@ -93,6 +95,11 @@ namespace ICSharpCode.FormDesigner Reparse(viewContent.Document.TextContent); int endOffset = viewContent.Document.PositionToOffset(new Point(0, initializeComponents.BodyRegion.EndLine)); viewContent.Document.Insert(endOffset, "\tPrivate " + e.Component.Site.Name + " As " + e.Component.GetType() + Environment.NewLine); + if (CodeDOMGenerator.IsNonVisualComponent(viewContent.Host, e.Component)) { + if (!IsNonVisualComponentContainerDefined) { + viewContent.Document.Insert(endOffset, "\tPrivate " + NonVisualComponentContainerName + " As " + typeof(Container) + Environment.NewLine); + } + } } catch (Exception ex) { MessageService.ShowError(ex); } @@ -269,5 +276,22 @@ namespace ICSharpCode.FormDesigner } return compatibleMethods; } + + bool IsNonVisualComponentContainerDefined + { + get { + return GetField(c, NonVisualComponentContainerName) != null; + } + } + + IField GetField(IClass c, string name) + { + foreach (IField field in c.Fields) { + if (field.Name == name) { + return field; + } + } + return null; + } } }