|
|
|
|
@ -9,9 +9,11 @@ using System;
@@ -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
@@ -38,6 +40,7 @@ namespace ICSharpCode.FormDesigner
|
|
|
|
|
CodeDomProvider codeProvider; |
|
|
|
|
|
|
|
|
|
CodeDOMGeneratorUtility codeDOMGeneratorUtility = new CodeDOMGeneratorUtility(); |
|
|
|
|
List<string> addedVariables = new List<string>(); |
|
|
|
|
|
|
|
|
|
public CodeDOMGenerator(IDesignerHost host, CodeDomProvider codeProvider) |
|
|
|
|
{ |
|
|
|
|
@ -53,29 +56,51 @@ namespace ICSharpCode.FormDesigner
@@ -53,29 +56,51 @@ namespace ICSharpCode.FormDesigner
|
|
|
|
|
DesignerResourceService designerResourceService = (DesignerResourceService)host.GetService(typeof(System.ComponentModel.Design.IResourceService)); |
|
|
|
|
designerResourceService.SerializationStarted(true); |
|
|
|
|
|
|
|
|
|
Type componentType = host.RootComponent.GetType(); |
|
|
|
|
ExpressionContext exprContext = new ExpressionContext(new CodeThisReferenceExpression(), componentType, host.RootComponent, host.RootComponent); |
|
|
|
|
addedVariables.Clear(); |
|
|
|
|
|
|
|
|
|
foreach (IComponent component in host.Container.Components) { |
|
|
|
|
if (!IsComponentAdded(component)) { |
|
|
|
|
GenerateComponentCode(component, writer, serializationManager); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
// 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
@@ -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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|