diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs index a064db0c92..8231e3986d 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonCodeDeserializer.cs @@ -165,6 +165,7 @@ namespace ICSharpCode.PythonBinding ConstantExpression constantExpression = listItemExpression as ConstantExpression; MemberExpression memberExpression = listItemExpression as MemberExpression; NameExpression nameExpression = listItemExpression as NameExpression; + CallExpression listItemCallExpression = listItemExpression as CallExpression; if (constantExpression != null) { array.SetValue(constantExpression.Value, i); } else if (memberExpression != null) { @@ -172,6 +173,9 @@ namespace ICSharpCode.PythonBinding array.SetValue(componentCreator.GetComponent(name), i); } else if (nameExpression != null) { array.SetValue(componentCreator.GetInstance(nameExpression.Name.ToString()), i); + } else if (listItemCallExpression != null) { + object instance = componentCreator.CreateInstance(arrayType, GetArguments(listItemCallExpression), null, false); + array.SetValue(instance, i); } } return array; @@ -194,6 +198,12 @@ namespace ICSharpCode.PythonBinding } } } + } else { + // Maybe it is a call to a constructor? + type = componentCreator.GetType(field.FullMemberName); + if (type != null) { + return componentCreator.CreateInstance(type, GetArguments(callExpression), null, false); + } } return null; } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs index 23e645bf49..24acf33d5e 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs @@ -33,7 +33,8 @@ namespace ICSharpCode.PythonBinding PythonCodeDeserializer deserializer; ClassDefinition classDefinition; - public PythonComponentWalker(IComponentCreator componentCreator) { + public PythonComponentWalker(IComponentCreator componentCreator) + { this.componentCreator = componentCreator; deserializer = new PythonCodeDeserializer(componentCreator); } @@ -175,7 +176,7 @@ namespace ICSharpCode.PythonBinding PropertyDescriptor propertyDescriptor = componentCreator.GetEventProperty(eventDescriptor); propertyDescriptor.SetValue(currentComponent, eventHandlerName); return false; - } + } /// /// Walks the binary expression which is the right hand side of an assignment statement. @@ -277,6 +278,8 @@ namespace ICSharpCode.PythonBinding throw new PythonComponentWalkerException(String.Format("Could not find type '{0}'.", PythonControlFieldExpression.GetMemberName(memberExpression))); } } + } else if (node.Target is IndexExpression) { + WalkArrayAssignmentRhs(node); } } @@ -356,5 +359,14 @@ namespace ICSharpCode.PythonBinding } return null; } + + /// + /// Walks the right hand side of an assignment when the assignment is an array creation. + /// + void WalkArrayAssignmentRhs(CallExpression callExpression) + { + object array = deserializer.Deserialize(callExpression); + fieldExpression.SetPropertyValue(componentCreator, array); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs index a72a1fe953..9bc6f1a591 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs @@ -543,6 +543,10 @@ namespace ICSharpCode.PythonBinding } } else if (IsResourcePropertyValue(propertyValue)) { AppendResourceProperty(codeBuilder, propertyName, propertyValue); + } else if (propertyValue.GetType().IsArray) { + codeBuilder.AppendIndented(propertyName + " = "); + AppendSystemArray(codeBuilder, GetArrayType(propertyValue).FullName, propertyValue as ICollection, false); + codeBuilder.AppendLine(); } else { codeBuilder.AppendIndentedLine(propertyName + " = " + PythonPropertyValueAssignment.ToString(propertyValue)); } @@ -721,8 +725,18 @@ namespace ICSharpCode.PythonBinding codeBuilder.AppendLine(); } } - + public static void AppendSystemArray(PythonCodeBuilder codeBuilder, string typeName, ICollection components) + { + AppendSystemArray(codeBuilder, typeName, components, true); + } + + /// + /// Appends an array. + /// + /// Indicates that the array is for an AddRange method that + /// requires the code to reference local variables. + public static void AppendSystemArray(PythonCodeBuilder codeBuilder, string typeName, ICollection components, bool localVariables) { if (components.Count > 0) { codeBuilder.Append("System.Array[" + typeName + "]("); @@ -743,8 +757,10 @@ namespace ICSharpCode.PythonBinding codeBuilder.Append(PythonPropertyValueAssignment.ToString(component)); } else if (component is IArrayItem) { codeBuilder.Append(((IArrayItem)component).Name); - } else { + } else if (localVariables) { codeBuilder.Append(GetVariableName(component, i + 1)); + } else { + codeBuilder.Append(PythonPropertyValueAssignment.ToString(component)); } ++i; } @@ -869,5 +885,11 @@ namespace ICSharpCode.PythonBinding AppendProperties(codeBuilder, propertyOwnerName, propertyValue); } } + + static Type GetArrayType(object obj) + { + Type type = obj.GetType(); + return obj.GetType().GetElementType(); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeDateTimeArrayTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeDateTimeArrayTestFixture.cs new file mode 100644 index 0000000000..62b03b32d0 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/DeserializeDateTimeArrayTestFixture.cs @@ -0,0 +1,37 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using System.Windows.Forms; +using ICSharpCode.PythonBinding; +using IronPython.Compiler.Ast; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class DeserializeDateTimeArrayTestFixture : DeserializeAssignmentTestFixtureBase + { + public override string GetPythonCode() + { + return "self.Items = System.Array[System.DateTime](\r\n" + + " [System.DateTime(2010, 2, 3, 0, 0, 0, 0),\r\n" + + " System.DateTime(0)])"; + } + + [Test] + public void DeserializedObjectIsExpectedArray() + { + DateTime[] expectedArray = new DateTime[] {new DateTime(2010, 2, 3), new DateTime(0)}; + Assert.AreEqual(expectedArray, deserializedObject); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateMonthCalendarTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateMonthCalendarTestFixture.cs new file mode 100644 index 0000000000..21ef18d453 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateMonthCalendarTestFixture.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.Windows.Forms; +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class GenerateMonthCalendarTestFixture + { + string generatedPythonCode; + + [TestFixtureSetUp] + public void SetUpFixture() + { + 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 month calendar. + MonthCalendar calendar = (MonthCalendar)host.CreateComponent(typeof(MonthCalendar), "monthCalendar1"); + calendar.TabIndex = 0; + calendar.Location = new Point(0, 0); + calendar.AddMonthlyBoldedDate(new DateTime(2009, 1, 2)); + calendar.AddMonthlyBoldedDate(new DateTime(0)); + + form.Controls.Add(calendar); + + PythonControl pythonForm = new PythonControl(" "); + generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form); + } + } + + [Test] + public void GeneratedCode() + { + string expectedCode = "def InitializeComponent(self):\r\n" + + " self._monthCalendar1 = System.Windows.Forms.MonthCalendar()\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # monthCalendar1\r\n" + + " # \r\n" + + " self._monthCalendar1.Location = System.Drawing.Point(0, 0)\r\n" + + " self._monthCalendar1.MonthlyBoldedDates = System.Array[System.DateTime](\r\n" + + " [System.DateTime(2009, 1, 2, 0, 0, 0, 0),\r\n" + + " System.DateTime(0)])\r\n" + + " self._monthCalendar1.Name = \"monthCalendar1\"\r\n" + + " self._monthCalendar1.TabIndex = 0\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.ClientSize = System.Drawing.Size(200, 300)\r\n" + + " self.Controls.Add(self._monthCalendar1)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n" + + " self.PerformLayout()\r\n"; + + Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadMonthCalendarTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadMonthCalendarTestFixture.cs new file mode 100644 index 0000000000..eeceb7a145 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadMonthCalendarTestFixture.cs @@ -0,0 +1,69 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.IO; +using System.Windows.Forms; + +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class LoadMonthCalendarTestFixture : LoadFormTestFixtureBase + { + public override string PythonCode { + get { + return "class MainForm(System.Windows.Forms.Form):\r\n" + + " def InitializeComponent(self):\r\n" + + " self._monthCalendar1 = System.Windows.Forms.MonthCalendar()\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # monthCalendar1\r\n" + + " # \r\n" + + " self._monthCalendar1.Location = System.Drawing.Point(0, 0)\r\n" + + " self._monthCalendar1.MonthlyBoldedDates = System.Array[System.DateTime](\r\n" + + " [System.DateTime(2009, 1, 2, 0, 0, 0, 0),\r\n" + + " System.DateTime(0)])\r\n" + + " self._monthCalendar1.Name = \"monthCalendar1\"\r\n" + + " self._monthCalendar1.SelectionRange = System.Windows.Forms.SelectionRange(System.DateTime(2009, 8, 4, 0, 0, 0, 0), System.DateTime(2009, 8, 5, 0, 0, 0, 0))\r\n" + + " self._monthCalendar1.TabIndex = 0\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.ClientSize = System.Drawing.Size(200, 300)\r\n" + + " self.Controls.Add(self._monthCalendar1)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n" + + " self.PerformLayout()\r\n"; + } + } + + public MonthCalendar Calendar { + get { return Form.Controls[0] as MonthCalendar; } + } + + [Test] + public void MonthlyBoldedDates() + { + DateTime[] expectedDates = new DateTime[] { new DateTime(2009, 1, 2), new DateTime(0) }; + Assert.AreEqual(expectedDates, Calendar.MonthlyBoldedDates); + } + + [Test] + public void SelectionRange() + { + SelectionRange expectedRange = new SelectionRange(new DateTime(2009, 8, 4, 0, 0, 0, 0), new DateTime(2009, 8, 5, 0, 0, 0, 0)); + Assert.AreEqual(expectedRange.ToString(), Calendar.SelectionRange.ToString()); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index eb1177bcbc..7dfb107873 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -167,6 +167,7 @@ + @@ -209,6 +210,7 @@ + @@ -246,6 +248,7 @@ +