From 4191d2b45a07311ad1c6362cfce8ac11ccbc624a Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Mon, 29 Jun 2009 18:10:32 +0000 Subject: [PATCH] Bitmap resources on the main form are now supported in the python forms designer. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4365 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/PythonDesignerComponent.cs | 3 +- .../Src/PythonDesignerRootComponent.cs | 1 + .../GenerateFormResourcesTestFixture.cs | 80 +++++++++++++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormResourcesTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs index f268f45654..bb67484906 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs @@ -528,6 +528,7 @@ namespace ICSharpCode.PythonBinding public void AppendResourceProperty(PythonCodeBuilder codeBuilder, string propertyName, object propertyValue) { string resourceName = propertyName.Replace("self._", String.Empty); + resourceName = resourceName.Replace("self.", "$this."); codeBuilder.AppendIndented(propertyName); codeBuilder.Append(" = resources.GetObject(\""); codeBuilder.Append(resourceName); @@ -803,7 +804,7 @@ namespace ICSharpCode.PythonBinding string GetRootComponentRootResourceName() { - PythonDesignerComponent component = parent; + PythonDesignerComponent component = this; while (component != null) { if (component.parent == null) { PythonDesignerRootComponent rootComponent = component as PythonDesignerRootComponent; diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs index f5794af192..5c1f6f341e 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs @@ -109,6 +109,7 @@ namespace ICSharpCode.PythonBinding foreach (PythonDesignerComponent component in GetContainerComponents()) { component.GenerateResources(writer); } + GenerateResources(writer); } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormResourcesTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormResourcesTestFixture.cs new file mode 100644 index 0000000000..ee56b683df --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormResourcesTestFixture.cs @@ -0,0 +1,80 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.ComponentModel.Design.Serialization; +using System.Drawing; +using System.Globalization; +using System.Windows.Forms; +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class GenerateFormResourceTestFixture + { + MockResourceWriter resourceWriter; + MockComponentCreator componentCreator; + string generatedPythonCode; + Bitmap bitmap; + + [TestFixtureSetUp] + public void SetUpFixture() + { + resourceWriter = new MockResourceWriter(); + componentCreator = new MockComponentCreator(); + componentCreator.SetResourceWriter(resourceWriter); + + 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"); + + // Set bitmap as form background image. + bitmap = new Bitmap(10, 10); + form.BackgroundImage = bitmap; + + PythonControl pythonControl = new PythonControl(" ", componentCreator); + generatedPythonCode = pythonControl.GenerateInitializeComponentMethod(form, "RootNamespace"); + } + } + + [Test] + public void GeneratedCode() + { + string expectedCode = "def InitializeComponent(self):\r\n" + + " resources = System.Resources.ResourceManager(\"RootNamespace.MainForm\", System.Reflection.Assembly.GetEntryAssembly())\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.BackgroundImage = resources.GetObject(\"$this.BackgroundImage\")\r\n" + + " self.ClientSize = System.Drawing.Size(200, 300)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n" + + " self.PerformLayout()\r\n"; + + Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode); + } + + [Test] + public void BitmapAddedToResourceWriter() + { + Assert.IsTrue(Object.ReferenceEquals(bitmap, resourceWriter.GetResource("$this.BackgroundImage"))); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 1b465bc473..5fe0f6aea4 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -190,6 +190,7 @@ +