Browse Source

Code now generated for controls on a form in the python forms designer.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3910 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
52c92ff5ec
  1. 60
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs
  2. 68
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTextBoxFormTestFixture.cs
  3. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

60
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs

@ -70,14 +70,12 @@ namespace ICSharpCode.PythonBinding @@ -70,14 +70,12 @@ namespace ICSharpCode.PythonBinding
/// </summary>
public PropertyDescriptorCollection GetSerializableProperties(object obj)
{
System.Console.WriteLine("GetSerializableProperties");
List<PropertyDescriptor> properties = new List<PropertyDescriptor>();
Attribute[] filter = new Attribute[] { DesignOnlyAttribute.No };
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj, filter).Sort()) {
if (property.SerializationVisibility == DesignerSerializationVisibility.Visible) {
if (property.ShouldSerializeValue(obj)) {
properties.Add(property);
System.Console.WriteLine("Property.Name: " + property.Name);
}
}
}
@ -98,39 +96,63 @@ namespace ICSharpCode.PythonBinding @@ -98,39 +96,63 @@ namespace ICSharpCode.PythonBinding
/// <summary>
/// Generates python code for the form's InitializeComponent method.
/// </summary>
/// <remarks>
/// Note that when the form is loaded in the designer the Name property appears twice:
///
/// Property.ComponentType: System.Windows.Forms.Design.DesignerExtenders+NameExtenderProvider
/// Property.SerializationVisibility: Hidden
/// Property.IsBrowsable: True
///
/// Property.ComponentType: System.Windows.Forms.Design.ControlDesigner
/// Property.SerializationVisibility: Visible
/// Property.IsBrowsable: False
/// </remarks>
void AppendForm(Form form)
{
AppendComment(form.Name);
foreach (Control control in form.Controls) {
AppendControl(control);
}
AppendControl(form, false);
foreach (Control control in form.Controls) {
AppendIndentedLine("self.Controls.Add(self." + control.Name + ")");
}
}
foreach (PropertyDescriptor property in GetSerializableProperties(form)) {
AppendProperty(form, property);
void AppendControl(Control control)
{
AppendControl(control, true);
}
/// <summary>
/// Generates python code for the control.
/// </summary>
void AppendControl(Control control, bool addControlNameToProperty)
{
AppendComment(control.Name);
string propertyOwnerName = String.Empty;
if (addControlNameToProperty) {
propertyOwnerName = control.Name;
}
foreach (PropertyDescriptor property in GetSerializableProperties(control)) {
AppendProperty(propertyOwnerName, control, property);
}
}
/// <summary>
/// Appends a property to the InitializeComponents method.
/// </summary>
void AppendProperty(object obj, PropertyDescriptor propertyDescriptor)
void AppendProperty(string propertyOwnerName, object obj, PropertyDescriptor propertyDescriptor)
{
object propertyValue = propertyDescriptor.GetValue(obj);
if (propertyValue == null) {
return;
}
AppendIndentedLine("self." + propertyDescriptor.Name + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
string propertyName = GetPropertyName(propertyOwnerName, propertyDescriptor.Name);
AppendIndentedLine(propertyName + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
}
static string GetPropertyName(string propertyOwnerName, string propertyName)
{
if (String.IsNullOrEmpty(propertyOwnerName)) {
return "self." + propertyName;
}
return "self." + propertyOwnerName + "." + propertyName;
}
/// <summary>
/// Appends the comment lines before the control has its properties set.
/// </summary>

68
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTextBoxFormTestFixture.cs

@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
// <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.Drawing;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
[TestFixture]
public class GenerateTextBoxFormTestFixture
{
string generatedPythonCode;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (Form form = new Form()) {
form.Name = "MainForm";
form.ClientSize = new Size(284, 264);
TextBox textBox = new TextBox();
textBox.Name = "textBox1";
textBox.Size = new Size(110, 20);
textBox.TabIndex = 1;
textBox.Location = new Point(10, 10);
form.Controls.Add(textBox);
string indentString = " ";
PythonForm pythonForm = new PythonForm(indentString);
generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form);
}
}
[Test]
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # textBox1\r\n" +
" # \r\n" +
" self.textBox1.Location = System.Drawing.Point(10, 10)\r\n" +
" self.textBox1.Name = \"textBox1\"\r\n" +
" self.textBox1.Size = System.Drawing.Size(110, 20)\r\n" +
" self.textBox1.TabIndex = 1\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Visible = False\r\n" +
" self.Controls.Add(self.textBox1)\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";
Assert.AreEqual(expectedCode, generatedPythonCode);
}
}
}

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

@ -162,6 +162,7 @@ @@ -162,6 +162,7 @@
<Compile Include="Designer\GenerateMinSizeFormTestFixture.cs" />
<Compile Include="Designer\GenerateRightToLeftFormTestFixture.cs" />
<Compile Include="Designer\GenerateSimpleFormTestFixture.cs" />
<Compile Include="Designer\GenerateTextBoxFormTestFixture.cs" />
<Compile Include="Designer\GeneratorMergeFindsInitializeComponentsTestFixture.cs" />
<Compile Include="Designer\IgnoreDesignTimePropertiesTestFixture.cs" />
<Compile Include="Designer\InsertNewEventHandlerTestFixture.cs" />

Loading…
Cancel
Save