Browse Source

Python forms designer now supports local bitmap resources.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4358 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
5fff800683
  1. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Project/PythonBinding.csproj
  2. 5
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs
  3. 29
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs
  4. 45
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControl.cs
  5. 79
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs
  6. 7
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponentFactory.cs
  7. 19
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerGenerator.cs
  8. 45
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs
  9. 31
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonLanguageProperties.cs
  10. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonProject.cs
  11. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/CreateNewPythonProjectTestFixture.cs
  12. 41
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateLocalImageResourceTestFixture.cs
  13. 69
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadLocalImageResourceTestFixture.cs
  14. 121
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/ProjectRootNamespacePassedToMergeTestFixture.cs
  15. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj
  16. 24
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonLanguagePropertiesTests.cs
  17. 13
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs
  18. 6
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProject.cs
  19. 6
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProjectContent.cs
  20. 12
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockResourceReader.cs
  21. 16
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockResourceWriter.cs

1
src/AddIns/BackendBindings/Python/PythonBinding/Project/PythonBinding.csproj

@ -100,6 +100,7 @@ @@ -100,6 +100,7 @@
<Compile Include="Src\PythonComponentWalker.cs" />
<Compile Include="Src\PythonComponentWalkerException.cs" />
<Compile Include="Src\PythonLanguageBinding.cs" />
<Compile Include="Src\PythonLanguageProperties.cs" />
<Compile Include="Src\PythonListViewComponent.cs" />
<Compile Include="Src\PythonOptionsPanel.cs" />
<Compile Include="Src\PythonOutputStream.cs" />

5
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs

@ -145,7 +145,8 @@ namespace ICSharpCode.PythonBinding @@ -145,7 +145,8 @@ namespace ICSharpCode.PythonBinding
Type GetType(PythonControlFieldExpression field)
{
return componentCreator.GetType(PythonControlFieldExpression.GetPrefix(field.FullMemberName));
string typeName = PythonControlFieldExpression.GetPrefix(field.FullMemberName);
return componentCreator.GetType(typeName);
}
/// <summary>
@ -185,7 +186,7 @@ namespace ICSharpCode.PythonBinding @@ -185,7 +186,7 @@ namespace ICSharpCode.PythonBinding
{
PythonControlFieldExpression field = PythonControlFieldExpression.Create(memberExpression);
Type type = GetType(field);
if (type != null) {
if (type != null) {
foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) {
if (method.Name == field.MemberName) {
if (method.GetParameters().Length == callExpression.Args.Length) {

29
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs

@ -332,6 +332,8 @@ namespace ICSharpCode.PythonBinding @@ -332,6 +332,8 @@ namespace ICSharpCode.PythonBinding
object obj = deserializer.Deserialize(node);
if (obj != null) {
SetPropertyValue(component, fieldExpression.MemberName, obj);
} else if (IsResource(memberExpression)) {
SetPropertyValue(fieldExpression.MemberName, GetResource(node));
} else {
throw new PythonComponentWalkerException(String.Format("Could not find type '{0}'.", PythonControlFieldExpression.GetMemberName(memberExpression)));
}
@ -390,6 +392,9 @@ namespace ICSharpCode.PythonBinding @@ -390,6 +392,9 @@ namespace ICSharpCode.PythonBinding
string typeName = PythonControlFieldExpression.GetMemberName(memberExpression);
Type type = componentCreator.GetType(typeName);
if (type != null) {
if (type.IsAssignableFrom(typeof(ComponentResourceManager))) {
return componentCreator.CreateInstance(type, new object[0], name, false);
}
List<object> args = deserializer.GetArguments(node);
return componentCreator.CreateInstance(type, args, name, false);
}
@ -400,5 +405,29 @@ namespace ICSharpCode.PythonBinding @@ -400,5 +405,29 @@ namespace ICSharpCode.PythonBinding
bool FoundInitializeComponentMethod {
get { return component != null; }
}
/// <summary>
/// Returns true if the expression is of the form:
///
/// resources.GetObject(...) or
/// resources.GetString(...)
/// </summary>
bool IsResource(MemberExpression memberExpression)
{
string fullName = PythonControlFieldExpression.GetMemberName(memberExpression);
return fullName.StartsWith("resources.", StringComparison.InvariantCultureIgnoreCase);
}
object GetResource(CallExpression callExpression)
{
IResourceReader reader = componentCreator.GetResourceReader(CultureInfo.InvariantCulture);
if (reader != null) {
using (ResourceSet resources = new ResourceSet(reader)) {
List<object> args = deserializer.GetArguments(callExpression);
return resources.GetObject(args[0] as String);
}
}
return null;
}
}
}

45
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControl.cs

@ -52,6 +52,14 @@ namespace ICSharpCode.PythonBinding @@ -52,6 +52,14 @@ namespace ICSharpCode.PythonBinding
/// Generates python code for the InitializeComponent method based on the controls added to the form.
/// </summary>
public string GenerateInitializeComponentMethod(Control control)
{
return GenerateInitializeComponentMethod(control, String.Empty);
}
/// <summary>
/// Generates python code for the InitializeComponent method based on the controls added to the form.
/// </summary>
public string GenerateInitializeComponentMethod(Control control, string rootNamespace)
{
PythonCodeBuilder methodCodeBuilder = new PythonCodeBuilder();
methodCodeBuilder.IndentString = indentString;
@ -62,17 +70,25 @@ namespace ICSharpCode.PythonBinding @@ -62,17 +70,25 @@ namespace ICSharpCode.PythonBinding
codeBuilder.IndentString = indentString;
codeBuilder.IncreaseIndent();
GenerateInitializeComponentMethodBodyInternal(control);
GenerateResources();
PythonDesignerRootComponent rootComponent = GenerateInitializeComponentMethodBodyInternal(control, rootNamespace);
rootComponent.GenerateResources(resourceService);
methodCodeBuilder.Append(codeBuilder.ToString());
return methodCodeBuilder.ToString();
}
/// <summary>
/// Generates the InitializeComponent method body.
/// </summary>
public string GenerateInitializeComponentMethodBody(Control control, int initialIndent)
{
return GenerateInitializeComponentMethodBody(control, String.Empty, initialIndent);
}
/// <summary>
/// Generates the InitializeComponent method body.
/// </summary>
public string GenerateInitializeComponentMethodBody(Control control, string rootNamespace, int initialIndent)
{
codeBuilder = new PythonCodeBuilder();
codeBuilder.IndentString = indentString;
@ -80,15 +96,15 @@ namespace ICSharpCode.PythonBinding @@ -80,15 +96,15 @@ namespace ICSharpCode.PythonBinding
for (int i = 0; i < initialIndent; ++i) {
codeBuilder.IncreaseIndent();
}
GenerateInitializeComponentMethodBodyInternal(control);
GenerateResources();
PythonDesignerRootComponent rootComponent = GenerateInitializeComponentMethodBodyInternal(control, rootNamespace);
rootComponent.GenerateResources(resourceService);
return codeBuilder.ToString();
}
void GenerateInitializeComponentMethodBodyInternal(Control control)
}
PythonDesignerRootComponent GenerateInitializeComponentMethodBodyInternal(Control control, string rootNamespace)
{
PythonDesignerRootComponent rootDesignerComponent = PythonDesignerComponentFactory.CreateDesignerRootComponent(control);
PythonDesignerRootComponent rootDesignerComponent = PythonDesignerComponentFactory.CreateDesignerRootComponent(control, rootNamespace);
rootDesignerComponent.AppendCreateContainerComponents(codeBuilder);
rootDesignerComponent.AppendSupportInitializeComponentsBeginInit(codeBuilder);
rootDesignerComponent.AppendChildComponentsSuspendLayout(codeBuilder);
@ -97,16 +113,7 @@ namespace ICSharpCode.PythonBinding @@ -97,16 +113,7 @@ namespace ICSharpCode.PythonBinding
rootDesignerComponent.AppendChildComponentsResumeLayout(codeBuilder);
rootDesignerComponent.AppendSupportInitializeComponentsEndInit(codeBuilder);
rootDesignerComponent.AppendResumeLayout(codeBuilder);
}
void GenerateResources()
{
if (resourceService == null) {
return;
}
using (IResourceWriter writer = resourceService.GetResourceWriter(CultureInfo.InvariantCulture)) {
}
return rootDesignerComponent;
}
}
}

79
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs

@ -10,8 +10,11 @@ using System.Collections; @@ -10,8 +10,11 @@ using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Drawing;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows.Forms;
namespace ICSharpCode.PythonBinding
@ -27,6 +30,8 @@ namespace ICSharpCode.PythonBinding @@ -27,6 +30,8 @@ namespace ICSharpCode.PythonBinding
static readonly DesignerSerializationVisibility[] contentDesignerVisibility = new DesignerSerializationVisibility[] { DesignerSerializationVisibility.Content };
IEventBindingService eventBindingService;
PythonDesignerComponent parent;
Dictionary<string, object> resources = new Dictionary<string, object>();
List<PythonDesignerComponent> designerContainerComponents;
protected static readonly string[] suspendLayoutMethods = new string[] {"SuspendLayout()"};
protected static readonly string[] resumeLayoutMethods = new string[] {"ResumeLayout(False)", "PerformLayout()"};
@ -238,12 +243,14 @@ namespace ICSharpCode.PythonBinding @@ -238,12 +243,14 @@ namespace ICSharpCode.PythonBinding
/// </summary>
public PythonDesignerComponent[] GetContainerComponents()
{
List<PythonDesignerComponent> components = new List<PythonDesignerComponent>();
ComponentCollection containerComponents = Component.Site.Container.Components;
for (int i = 1; i < containerComponents.Count; ++i) {
components.Add(PythonDesignerComponentFactory.CreateDesignerComponent(this, containerComponents[i]));
if (designerContainerComponents == null) {
designerContainerComponents = new List<PythonDesignerComponent>();
ComponentCollection components = Component.Site.Container.Components;
for (int i = 1; i < components.Count; ++i) {
designerContainerComponents.Add(PythonDesignerComponentFactory.CreateDesignerComponent(this, components[i]));
}
}
return components.ToArray();
return designerContainerComponents.ToArray();
}
/// <summary>
@ -488,6 +495,8 @@ namespace ICSharpCode.PythonBinding @@ -488,6 +495,8 @@ namespace ICSharpCode.PythonBinding
if (controlRef != null) {
codeBuilder.AppendIndentedLine(propertyName + " = " + controlRef);
}
} else if (IsResourcePropertyValue(propertyValue)) {
AppendResourceProperty(codeBuilder, propertyName, propertyValue);
} else {
codeBuilder.AppendIndentedLine(propertyName + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
}
@ -513,6 +522,29 @@ namespace ICSharpCode.PythonBinding @@ -513,6 +522,29 @@ namespace ICSharpCode.PythonBinding
codeBuilder.AppendLine();
}
/// <summary>
/// Appends a property whose value is a resource.
/// </summary>
public void AppendResourceProperty(PythonCodeBuilder codeBuilder, string propertyName, object propertyValue)
{
string resourceName = propertyName.Replace("self._", String.Empty);
codeBuilder.AppendIndented(propertyName);
codeBuilder.Append(" = resources.GetObject(\"");
codeBuilder.Append(resourceName);
codeBuilder.Append("\")");
codeBuilder.AppendLine();
resources.Add(resourceName, propertyValue);
}
/// <summary>
/// Returns true if the property value will be obtained from a resource.
/// </summary>
public static bool IsResourcePropertyValue(object propertyValue)
{
return propertyValue is Image;
}
/// <summary>
/// Appends the properties of the object to the code builder.
/// </summary>
@ -548,6 +580,11 @@ namespace ICSharpCode.PythonBinding @@ -548,6 +580,11 @@ namespace ICSharpCode.PythonBinding
if (addComment && propertiesBuilder.Length > 0) {
AppendComment(codeBuilder);
}
if (resources.Count > 0) {
InsertCreateResourceManagerLine(codeBuilder);
}
codeBuilder.Append(propertiesBuilder.ToString());
}
@ -602,6 +639,16 @@ namespace ICSharpCode.PythonBinding @@ -602,6 +639,16 @@ namespace ICSharpCode.PythonBinding
}
return false;
}
/// <summary>
/// Writes resources for this component to the resource writer.
/// </summary>
public void GenerateResources(IResourceWriter writer)
{
foreach (KeyValuePair<string, object> entry in resources) {
writer.AddResource(entry.Key, entry.Value);
}
}
protected IComponent Component {
get { return component; }
@ -739,5 +786,27 @@ namespace ICSharpCode.PythonBinding @@ -739,5 +786,27 @@ namespace ICSharpCode.PythonBinding
reversedCollection.Reverse();
return reversedCollection;
}
void InsertCreateResourceManagerLine(PythonCodeBuilder codeBuilder)
{
StringBuilder line = new StringBuilder();
line.Append("resources = System.Resources.ResourceManager(\"");
line.Append(GetRootComponentRootResourceName());
line.Append("\", System.Reflection.Assembly.GetEntryAssembly())");
codeBuilder.InsertIndentedLine(line.ToString());
}
string GetRootComponentRootResourceName()
{
PythonDesignerComponent component = parent;
while (component != null) {
if (component.parent == null) {
PythonDesignerRootComponent rootComponent = component as PythonDesignerRootComponent;
return rootComponent.GetResourceRootName();
}
component = component.parent;
}
return String.Empty;
}
}
}

7
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponentFactory.cs

@ -40,7 +40,12 @@ namespace ICSharpCode.PythonBinding @@ -40,7 +40,12 @@ namespace ICSharpCode.PythonBinding
public static PythonDesignerRootComponent CreateDesignerRootComponent(IComponent component)
{
return new PythonDesignerRootComponent(component);
return CreateDesignerRootComponent(component, String.Empty);
}
public static PythonDesignerRootComponent CreateDesignerRootComponent(IComponent component, string rootNamespace)
{
return new PythonDesignerRootComponent(component, rootNamespace);
}
}
}

19
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerGenerator.cs

@ -22,6 +22,7 @@ using ICSharpCode.SharpDevelop; @@ -22,6 +22,7 @@ using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
@ -99,7 +100,14 @@ namespace ICSharpCode.PythonBinding @@ -99,7 +100,14 @@ namespace ICSharpCode.PythonBinding
// Generate the python source code.
PythonControl pythonControl = new PythonControl(NRefactoryToPythonConverter.GetIndentString(textEditorProperties), resourceService);
int indent = method.Region.BeginColumn;
string methodBody = pythonControl.GenerateInitializeComponentMethodBody(component as Control, indent);
if (textEditorProperties.ConvertTabsToSpaces) {
indent = (indent / textEditorProperties.IndentationSize);
if (textEditorProperties.IndentationSize > 1) {
indent += 1;
}
}
string rootNamespace = GetProjectRootNamespace(compilationUnit);
string methodBody = pythonControl.GenerateInitializeComponentMethodBody(component as Control, rootNamespace, indent);
// Merge the code.
DomRegion methodRegion = GetBodyRegionInDocument(method);
@ -292,5 +300,14 @@ namespace ICSharpCode.PythonBinding @@ -292,5 +300,14 @@ namespace ICSharpCode.PythonBinding
{
return ParseFile(this.ViewContent.DesignerCodeFile.FileName, this.ViewContent.DesignerCodeFileContent);
}
static string GetProjectRootNamespace(ICompilationUnit compilationUnit)
{
IProject project = compilationUnit.ProjectContent.Project as IProject;
if (project != null) {
return project.RootNamespace;
}
return String.Empty;
}
}
}

45
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs

@ -8,7 +8,10 @@ @@ -8,7 +8,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;
namespace ICSharpCode.PythonBinding
@ -18,9 +21,17 @@ namespace ICSharpCode.PythonBinding @@ -18,9 +21,17 @@ namespace ICSharpCode.PythonBinding
/// </summary>
public class PythonDesignerRootComponent : PythonDesignerComponent
{
string rootNamespace = String.Empty;
public PythonDesignerRootComponent(IComponent component)
: this(component, String.Empty)
{
}
public PythonDesignerRootComponent(IComponent component, string rootNamespace)
: base(null, component)
{
this.rootNamespace = rootNamespace;
}
public override string GetPropertyOwnerName()
@ -69,13 +80,12 @@ namespace ICSharpCode.PythonBinding @@ -69,13 +80,12 @@ namespace ICSharpCode.PythonBinding
public void AppendSupportInitializeMethodCalls(PythonCodeBuilder codeBuilder, string[] methods)
{
foreach (IComponent containerComponent in Component.Site.Container.Components) {
if (typeof(ISupportInitialize).IsAssignableFrom(containerComponent.GetType())) {
PythonDesignerComponent component = PythonDesignerComponentFactory.CreateDesignerComponent(this, containerComponent);
foreach (PythonDesignerComponent component in GetContainerComponents()) {
if (typeof(ISupportInitialize).IsAssignableFrom(component.GetComponentType())) {
component.AppendMethodCalls(codeBuilder, methods);
}
}
}
}
/// <summary>
/// Reverses the ordering when adding items to the Controls collection.
@ -84,6 +94,31 @@ namespace ICSharpCode.PythonBinding @@ -84,6 +94,31 @@ namespace ICSharpCode.PythonBinding
{
bool reverse = propertyDescriptor.Name == "Controls";
AppendMethodCallWithArrayParameter(codeBuilder, propertyOwnerName, propertyOwner, propertyDescriptor, reverse);
}
}
/// <summary>
/// Writes resources to file.
/// </summary>
public void GenerateResources(IResourceService resourceService)
{
if (resourceService == null) {
return;
}
using (IResourceWriter writer = resourceService.GetResourceWriter(CultureInfo.InvariantCulture)) {
foreach (PythonDesignerComponent component in GetContainerComponents()) {
component.GenerateResources(writer);
}
}
}
public string GetResourceRootName()
{
string componentName = Component.Site.Name;
if (!String.IsNullOrEmpty(rootNamespace)) {
return rootNamespace + "." + componentName;
}
return componentName;
}
}
}

31
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonLanguageProperties.cs

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.CodeDom.Compiler;
using ICSharpCode.SharpDevelop.Dom;
using Microsoft.CSharp;
namespace ICSharpCode.PythonBinding
{
public class PythonLanguageProperties : LanguageProperties
{
static readonly PythonLanguageProperties defaultProperties = new PythonLanguageProperties();
public PythonLanguageProperties() : base(StringComparer.Ordinal)
{
}
public static PythonLanguageProperties Default {
get { return defaultProperties; }
}
public override CodeDomProvider CodeDomProvider {
get { return new CSharpCodeProvider(); }
}
}
}

2
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonProject.cs

@ -43,7 +43,7 @@ namespace ICSharpCode.PythonBinding @@ -43,7 +43,7 @@ namespace ICSharpCode.PythonBinding
/// Gets the language properties associated with this project.
/// </summary>
public override LanguageProperties LanguageProperties {
get { return LanguageProperties.None; }
get { return PythonLanguageProperties.Default; }
}
/// <summary>

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/CreateNewPythonProjectTestFixture.cs

@ -65,7 +65,7 @@ namespace PythonBinding.Tests @@ -65,7 +65,7 @@ namespace PythonBinding.Tests
[Test]
public void ProjectLanguageProperties()
{
Assert.AreEqual(LanguageProperties.None, project.LanguageProperties);
Assert.AreEqual(PythonLanguageProperties.Default, project.LanguageProperties);
}
[Test]

41
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateLocalImageResourceTestFixture.cs

@ -27,6 +27,9 @@ namespace PythonBinding.Tests.Designer @@ -27,6 +27,9 @@ namespace PythonBinding.Tests.Designer
string generatedPythonCode;
MockResourceWriter resourceWriter2;
MockComponentCreator componentCreator2;
Bitmap bitmap;
string rootComponentNoNamespaceResourceRootName;
string rootComponentResourceRootName;
[TestFixtureSetUp]
public void SetUpFixture()
@ -52,17 +55,24 @@ namespace PythonBinding.Tests.Designer @@ -52,17 +55,24 @@ namespace PythonBinding.Tests.Designer
// Add picture box
PictureBox pictureBox = (PictureBox)host.CreateComponent(typeof(PictureBox), "pictureBox1");
pictureBox.Location = new Point(0, 0);
//pictureBox.Image = new Bitmap(10, 10);
bitmap = new Bitmap(10, 10);
pictureBox.Image = bitmap;
pictureBox.Size = new Size(100, 120);
pictureBox.TabIndex = 0;
form.Controls.Add(pictureBox);
PythonControl pythonControl = new PythonControl(" ", componentCreator);
generatedPythonCode = pythonControl.GenerateInitializeComponentMethod(form);
generatedPythonCode = pythonControl.GenerateInitializeComponentMethod(form, "RootNamespace");
// Check that calling the GenerateInitializeComponentMethodBody also generates a resource file.
PythonControl pythonControl2 = new PythonControl(" ", componentCreator2);
pythonControl2.GenerateInitializeComponentMethodBody(form, 0);
PythonDesignerRootComponent rootComponentNoNamespace = new PythonDesignerRootComponent(form);
rootComponentNoNamespaceResourceRootName = rootComponentNoNamespace.GetResourceRootName();
PythonDesignerRootComponent rootComponent = new PythonDesignerRootComponent(form, "MyNamespace");
rootComponentResourceRootName = rootComponent.GetResourceRootName();
}
}
@ -70,15 +80,15 @@ namespace PythonBinding.Tests.Designer @@ -70,15 +80,15 @@ namespace PythonBinding.Tests.Designer
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
//" resources = System.Windows.Forms.ComponentModel(clr.GetType(MainForm))\r\n" +
" resources = System.Resources.ResourceManager(\"RootNamespace.MainForm\", System.Reflection.Assembly.GetEntryAssembly())\r\n" +
" self._pictureBox1 = System.Windows.Forms.PictureBox()\r\n" +
" self._pictureBox1.BeginInit()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # pictureBox1\r\n" +
" # \r\n" +
" self._pictureBox1.Image = resources.GetObject(\"pictureBox1.Image\")\r\n" +
" self._pictureBox1.Location = System.Drawing.Point(0, 0)\r\n" +
//" self._pictureBox1.Image = resources.GetObject(\"pictureBox1.Image\")\r\n" +
" self._pictureBox1.Name = \"pictureBox1\"\r\n" +
" self._pictureBox1.Size = System.Drawing.Size(100, 120)\r\n" +
" self._pictureBox1.TabIndex = 0\r\n" +
@ -132,5 +142,28 @@ namespace PythonBinding.Tests.Designer @@ -132,5 +142,28 @@ namespace PythonBinding.Tests.Designer
Assert.IsTrue(resourceWriter2.IsDisposed);
}
[Test]
public void BitmapAddedToResourceWriter()
{
Assert.IsTrue(Object.ReferenceEquals(bitmap, resourceWriter.GetResource("pictureBox1.Image")));
}
[Test]
public void ResourceWriterHasNonNullPictureBox1ImageResource()
{
Assert.IsNotNull(resourceWriter.GetResource("pictureBox1.Image"));
}
[Test]
public void GetResourceRootName()
{
Assert.AreEqual("MyNamespace.MainForm", rootComponentResourceRootName);
}
[Test]
public void GetResourceRootNameWhenNamespaceIsEmptyString()
{
Assert.AreEqual("MainForm", rootComponentNoNamespaceResourceRootName);
}
}
}

69
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadLocalImageResourceTestFixture.cs

@ -12,6 +12,7 @@ using System.ComponentModel.Design; @@ -12,6 +12,7 @@ using System.ComponentModel.Design;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
@ -24,35 +25,43 @@ namespace PythonBinding.Tests.Designer @@ -24,35 +25,43 @@ namespace PythonBinding.Tests.Designer
public class LoadLocalImageResourceTestFixture : LoadFormTestFixtureBase
{
MockResourceReader reader;
Bitmap bitmap;
public override string PythonCode {
get {
bitmap = new Bitmap(10, 20);
reader = new MockResourceReader();
reader.AddResource("pictureBox1.Image", bitmap);
ComponentCreator.SetResourceReader(reader);
return "class TestForm(System.Windows.Forms.Form):\r\n" +
" def InitializeComponent(self):\r\n" +
// " resources = System.ComponentModel.ComponentResourceManager(clr.GetClrType(TestForm))\r\n" +
// " self._pictureBox1 = System.Windows.Forms.PictureBox()\r\n" +
// " self.SuspendLayout()\r\n" +
// " # \r\n" +
// " # pictureBox1\r\n" +
// " # \r\n" +
// " self._pictureBox1.Location = System.Drawing.Point(0, 0)\r\n" +
// " self._pictureBox1.Name = \"button1\"\r\n" +
// " self._pictureBox1.Size = System.Drawing.Size(10, 10)\r\n" +
// " self._button1.TabIndex = 0\r\n" +
// " self._button1.Text = \"button1\"\r\n" +
// " # \r\n" +
// " # MainForm\r\n" +
// " # \r\n" +
// " self.AcceptButton = self._button1\r\n" +
// " self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" resources = System.Resources.ResourceManager(\"RootNamespace.MainForm\", System.Reflection.Assembly.GetEntryAssembly())\r\n" +
" self._pictureBox1 = System.Windows.Forms.PictureBox()\r\n" +
" self._pictureBox1.BeginInit()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # pictureBox1\r\n" +
" # \r\n" +
" self._pictureBox1.Image = resources.GetObject(\"pictureBox1.Image\")\r\n" +
" self._pictureBox1.Location = System.Drawing.Point(0, 0)\r\n" +
" self._pictureBox1.Name = \"button1\"\r\n" +
" self._pictureBox1.Size = System.Drawing.Size(10, 10)\r\n" +
" self._pictureBox1.TabIndex = 0\r\n" +
" self._pictureBox1.Text = \"button1\"\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" self.Name = \"MainForm\"\r\n" +
// " self.Controls.Add(self._pictureBox1)\r\n" +
// " self.ResumeLayout(False)\r\n";
"";
" self.Controls.Add(self._pictureBox1)\r\n" +
" self._pictureBox1.EndInit()\r\n" +
" self.ResumeLayout(False)\r\n";
}
}
public PictureBox PictureBox1 {
get { return base.Form.Controls[0] as PictureBox; }
}
[Test]
public void ResourceReaderRetrievedFromComponentCreator()
@ -70,6 +79,26 @@ namespace PythonBinding.Tests.Designer @@ -70,6 +79,26 @@ namespace PythonBinding.Tests.Designer
public void ResourceReaderIsDisposed()
{
Assert.IsTrue(reader.IsDisposed);
}
}
[Test]
public void ComponentResourceManagerCreated()
{
CreatedInstance expectedInstance = new CreatedInstance(typeof(ResourceManager), new object[0], "resources", false);
CreatedInstance instance = base.ComponentCreator.CreatedInstances[0];
Assert.AreEqual(expectedInstance, instance);
}
[Test]
public void BitmapAssignedToPictureBoxRetrievedFromResourceReader()
{
Assert.IsTrue(Object.Equals(bitmap, PictureBox1.Image));
}
[Test]
public void PictureBoxImageIsNotNull()
{
Assert.IsNotNull(PictureBox1.Image);
}
}
}

121
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/ProjectRootNamespacePassedToMergeTestFixture.cs

@ -0,0 +1,121 @@ @@ -0,0 +1,121 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
/// <summary>
/// Tests the the project's root namespace is passed to the PythonDesigner
/// </summary>
[TestFixture]
public class ProjectRootNamespacePassedToMergeTestFixture
{
IDocument document;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (TextEditorControl textEditor = new TextEditorControl()) {
document = textEditor.Document;
textEditor.Text = GetTextEditorCode();
PythonParser parser = new PythonParser();
MockProjectContent projectContent = new MockProjectContent();
MockProject project = new MockProject();
project.RootNamespace = "RootNamespace";
projectContent.Project = project;
ICompilationUnit compilationUnit = parser.Parse(projectContent, @"test.py", document.TextContent);
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
IEventBindingService eventBindingService = new MockEventBindingService(host);
Form form = (Form)host.RootComponent;
form.ClientSize = new Size(200, 300);
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
namePropertyDescriptor.SetValue(form, "MainForm");
// Add picture box
PictureBox pictureBox = (PictureBox)host.CreateComponent(typeof(PictureBox), "pictureBox1");
pictureBox.Location = new Point(0, 0);
pictureBox.Image = new Bitmap(10, 10);
pictureBox.Size = new Size(100, 120);
pictureBox.TabIndex = 0;
form.Controls.Add(pictureBox);
MockTextEditorProperties properties = new MockTextEditorProperties();
properties.ConvertTabsToSpaces = true;
properties.IndentationSize = 4;
PythonDesignerGenerator.Merge(form, document, compilationUnit, properties, null);
}
}
}
[Test]
public void GeneratedCode()
{
string expectedCode = "from System.Windows.Forms import Form\r\n" +
"\r\n" +
"class MainForm(Form):\r\n" +
" def __init__(self):\r\n" +
" self.InitializeComponent()\r\n" +
" \r\n" +
" def InitializeComponent(self):\r\n" +
" resources = System.Resources.ResourceManager(\"RootNamespace.MainForm\", System.Reflection.Assembly.GetEntryAssembly())\r\n" +
" self._pictureBox1 = System.Windows.Forms.PictureBox()\r\n" +
" self._pictureBox1.BeginInit()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # pictureBox1\r\n" +
" # \r\n" +
" self._pictureBox1.Image = resources.GetObject(\"pictureBox1.Image\")\r\n" +
" self._pictureBox1.Location = System.Drawing.Point(0, 0)\r\n" +
" self._pictureBox1.Name = \"pictureBox1\"\r\n" +
" self._pictureBox1.Size = System.Drawing.Size(100, 120)\r\n" +
" self._pictureBox1.TabIndex = 0\r\n" +
" self._pictureBox1.TabStop = False\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" self.Controls.Add(self._pictureBox1)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self._pictureBox1.EndInit()\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";
Assert.AreEqual(expectedCode, document.TextContent, document.TextContent);
}
string GetTextEditorCode()
{
return "from System.Windows.Forms import Form\r\n" +
"\r\n" +
"class MainForm(Form):\r\n" +
" def __init__(self):\r\n" +
" self.InitializeComponent()\r\n" +
" \r\n" +
" def InitializeComponent(self):\r\n" +
" pass\r\n";
}
}
}

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -237,6 +237,7 @@ @@ -237,6 +237,7 @@
<Compile Include="Designer\MissingInitializeComponentMethodTestFixture.cs" />
<Compile Include="Designer\NoNewLineAfterInitializeComponentTestFixture.cs" />
<Compile Include="Designer\OneCompatibleMethodTestFixture.cs" />
<Compile Include="Designer\ProjectRootNamespacePassedToMergeTestFixture.cs" />
<Compile Include="Designer\PythonBaseClassTests.cs" />
<Compile Include="Designer\PythonCodeDeserializerTests.cs" />
<Compile Include="Designer\PythonCodeBuilderTests.cs" />
@ -271,6 +272,7 @@ @@ -271,6 +272,7 @@
<Compile Include="Parsing\ParseSingleClassTestFixture.cs" />
<Compile Include="Parsing\InvalidClassTestFixture.cs" />
<Compile Include="PythonBindingAddInFile.cs" />
<Compile Include="PythonLanguagePropertiesTests.cs" />
<Compile Include="PythonOptionsPanelTestFixture.cs" />
<Compile Include="CreateNewPythonProjectTestFixture.cs" />
<Compile Include="PythonSyntaxModeTestFixture.cs" />

24
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonLanguagePropertiesTests.cs

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using ICSharpCode.PythonBinding;
using ICSharpCode.SharpDevelop.Dom;
using NUnit.Framework;
namespace PythonBinding.Tests
{
[TestFixture]
public class PythonLanguagePropertiesTests
{
[Test]
public void HasCodeDomProvider()
{
Assert.IsNotNull(PythonLanguageProperties.Default.CodeDomProvider);
}
}
}

13
src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs

@ -69,9 +69,16 @@ namespace PythonBinding.Tests.Utils @@ -69,9 +69,16 @@ namespace PythonBinding.Tests.Utils
public IComponent GetComponent(string name)
{
foreach (AddedComponent c in addedComponents) {
if (c.Name == name) {
return c.Component;
foreach (AddedComponent addedComponent in addedComponents) {
if (addedComponent.Name == name) {
return addedComponent.Component;
}
}
foreach (CreatedComponent createdComponent in createdComponents) {
if (!String.IsNullOrEmpty(createdComponent.Name)) {
if (createdComponent.Name == name) {
return createdComponent.Component;
}
}
}
return null;

6
src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProject.cs

@ -23,6 +23,7 @@ namespace PythonBinding.Tests.Utils @@ -23,6 +23,7 @@ namespace PythonBinding.Tests.Utils
{
readonly object syncRoot = new object();
string directory = String.Empty;
string rootNamespace = String.Empty;
public MockProject()
{
@ -95,9 +96,8 @@ namespace PythonBinding.Tests.Utils @@ -95,9 +96,8 @@ namespace PythonBinding.Tests.Utils
}
public string RootNamespace {
get { return String.Empty; }
set {
}
get { return rootNamespace; }
set { rootNamespace = value; }
}
public string OutputAssemblyFullPath {

6
src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockProjectContent.cs

@ -36,6 +36,7 @@ namespace PythonBinding.Tests.Utils @@ -36,6 +36,7 @@ namespace PythonBinding.Tests.Utils
string classNameForGetClass;
bool namespaceExistsReturnValue;
bool namespaceExistsCalled;
object project;
public MockProjectContent()
{
@ -205,9 +206,8 @@ namespace PythonBinding.Tests.Utils @@ -205,9 +206,8 @@ namespace PythonBinding.Tests.Utils
}
public object Project {
get {
return null;
}
get { return project; }
set { project = value; }
}
public SystemTypes SystemTypes {

12
src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockResourceReader.cs

@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Resources;
namespace PythonBinding.Tests.Utils
@ -14,24 +15,29 @@ namespace PythonBinding.Tests.Utils @@ -14,24 +15,29 @@ namespace PythonBinding.Tests.Utils
public class MockResourceReader : IResourceReader
{
bool disposed;
Dictionary<string, object> resources = new Dictionary<string, object>();
public MockResourceReader()
{
}
public void AddResource(string name, object value)
{
resources.Add(name, value);
}
public void Close()
{
throw new NotImplementedException();
}
public IDictionaryEnumerator GetEnumerator()
{
throw new NotImplementedException();
return resources.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return null;
return resources.GetEnumerator();
}
public void Dispose()

16
src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockResourceWriter.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Collections.Generic;
using System.Resources;
namespace PythonBinding.Tests.Utils
@ -13,19 +14,28 @@ namespace PythonBinding.Tests.Utils @@ -13,19 +14,28 @@ namespace PythonBinding.Tests.Utils
public class MockResourceWriter : IResourceWriter
{
bool disposed;
Dictionary<string, object> resources = new Dictionary<string, object>();
public MockResourceWriter()
{
}
public object GetResource(string name)
{
if (resources.ContainsKey(name)) {
return resources[name];
}
return null;
}
public void AddResource(string name, string value)
{
throw new NotImplementedException();
resources.Add(name, value);
}
public void AddResource(string name, object value)
{
throw new NotImplementedException();
resources.Add(name, value);
}
public void AddResource(string name, byte[] value)
@ -35,12 +45,10 @@ namespace PythonBinding.Tests.Utils @@ -35,12 +45,10 @@ namespace PythonBinding.Tests.Utils
public void Close()
{
throw new NotImplementedException();
}
public void Generate()
{
throw new NotImplementedException();
}
public void Dispose()

Loading…
Cancel
Save