Browse Source

Python forms designer now calls all methods and explicit interface methods when loading InitializeComponent method.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4729 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
b747259b4e
  1. 37
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs
  2. 10
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs
  3. 78
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CallBeginInitOnLoadTestFixture.cs
  4. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj
  5. 48
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/SupportInitCustomControl.cs

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

@ -292,11 +292,6 @@ namespace ICSharpCode.PythonBinding @@ -292,11 +292,6 @@ namespace ICSharpCode.PythonBinding
/// </summary>
void WalkMethodCall(CallExpression node)
{
if (node.Args.Length == 0) {
// Ignore method calls with no parameters.
return;
}
// Try to get the object being called. Try the form first then
// look for other controls.
object member = PythonControlFieldExpression.GetMember(component, node);
@ -308,10 +303,40 @@ namespace ICSharpCode.PythonBinding @@ -308,10 +303,40 @@ namespace ICSharpCode.PythonBinding
// Execute the method on the object.
if (member != null) {
object[] args = deserializer.GetArguments(node).ToArray();
member.GetType().InvokeMember(field.MethodName, BindingFlags.InvokeMethod, Type.DefaultBinder, member, args);
InvokeMethod(member, field.MethodName, args);
}
}
void InvokeMethod(object obj, string name, object[] args)
{
Type type = obj.GetType();
try {
type.InvokeMember(name, BindingFlags.InvokeMethod, Type.DefaultBinder, obj, args);
} catch (MissingMethodException ex) {
// Look for an explicitly implemented interface.
MethodInfo method = FindInterfaceMethod(type, name);
if (method != null) {
method.Invoke(obj, args);
} else {
throw ex;
}
}
}
/// <summary>
/// Looks for an explicitly implemented interface.
/// </summary>
MethodInfo FindInterfaceMethod(Type type, string name)
{
string nameMatch = "." + name;
foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)) {
if (method.Name.EndsWith(nameMatch)) {
return method;
}
}
return null;
}
/// <summary>
/// Creates a new instance with the specified name.
/// </summary>

10
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs

@ -329,6 +329,9 @@ namespace ICSharpCode.PythonBinding @@ -329,6 +329,9 @@ namespace ICSharpCode.PythonBinding
obj = componentCreator.GetInstance(variableName);
if (obj == null) {
obj = GetInheritedObject(memberName, componentCreator.RootComponent);
if ((obj == null) && !IsSelfReference) {
obj = componentCreator.GetInstance(fullMemberName);
}
}
}
@ -352,7 +355,10 @@ namespace ICSharpCode.PythonBinding @@ -352,7 +355,10 @@ namespace ICSharpCode.PythonBinding
public static object GetMember(object obj, CallExpression expression)
{
string[] memberNames = GetMemberNames(expression.Target as MemberExpression);
return GetMember(obj, memberNames, 1, memberNames.Length - 2);
if (ContainsSelfReference(memberNames)) {
return GetMember(obj, memberNames, 1, memberNames.Length - 2);
}
return null;
}
/// <summary>
@ -361,7 +367,7 @@ namespace ICSharpCode.PythonBinding @@ -361,7 +367,7 @@ namespace ICSharpCode.PythonBinding
/// <param name="startIndex">The point at which to start looking in the memberNames.</param>
/// <param name="endIndex">The last memberNames item to look at.</param>
static object GetMember(object obj, string[] memberNames, int startIndex, int endIndex)
{
{
for (int i = startIndex; i <= endIndex; ++i) {
Type type = obj.GetType();
string name = memberNames[i];

78
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CallBeginInitOnLoadTestFixture.cs

@ -0,0 +1,78 @@ @@ -0,0 +1,78 @@
// <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
{
/// <summary>
/// Tests that the control's BeginInit and EndInit methods are called.
/// </summary>
[TestFixture]
public class CallBeginInitOnLoadTestFixture : LoadFormTestFixtureBase
{
public override string PythonCode {
get {
ComponentCreator.AddType("PythonBinding.Tests.Utils.SupportInitCustomControl", typeof(SupportInitCustomControl));
return "class TestForm(System.Windows.Forms.Form):\r\n" +
" def InitializeComponent(self):\r\n" +
" self._control = PythonBinding.Tests.Utils.SupportInitCustomControl()\r\n" +
" self._control.BeginInit()\r\n" +
" localVariable = PythonBinding.Tests.Utils.SupportInitCustomControl()\r\n" +
" localVariable.BeginInit()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # TestForm\r\n" +
" # \r\n" +
" self.AccessibleRole = System.Windows.Forms.AccessibleRole.None\r\n" +
" self.Controls.Add(self._control)\r\n" +
" self.Name = \"TestForm\"\r\n" +
" self._control.EndInit()\r\n" +
" localVariable.EndInit()\r\n" +
" self.ResumeLayout(False)\r\n";
}
}
public SupportInitCustomControl Control {
get { return Form.Controls[0] as SupportInitCustomControl; }
}
public SupportInitCustomControl LocalControl {
get { return base.ComponentCreator.GetInstance("localVariable") as SupportInitCustomControl; }
}
[Test]
public void BeginInitCalled()
{
Assert.IsTrue(Control.IsBeginInitCalled);
}
[Test]
public void EndInitCalled()
{
Assert.IsTrue(Control.IsEndInitCalled);
}
[Test]
public void BeginInitCalledOnLocalVariable()
{
Assert.IsTrue(LocalControl.IsBeginInitCalled);
}
[Test]
public void EndInitCalledOnLocalVariable()
{
Assert.IsTrue(LocalControl.IsEndInitCalled);
}
}
}

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

@ -162,6 +162,7 @@ @@ -162,6 +162,7 @@
<Compile Include="Converter\XmlDocCommentConversionTestFixture.cs" />
<Compile Include="DebugPythonCommandTestFixture.cs" />
<Compile Include="Designer\AppendNullPropertyValueTestFixture.cs" />
<Compile Include="Designer\CallBeginInitOnLoadTestFixture.cs" />
<Compile Include="Designer\ConvertCustomClassUsingTypeConverterTestFixture.cs" />
<Compile Include="Designer\CursorTypeResolutionTestFixture.cs" />
<Compile Include="Designer\DeserializeAssignmentTestFixtureBase.cs" />
@ -374,6 +375,7 @@ @@ -374,6 +375,7 @@
<Compile Include="Parsing\AddInvalidSourceSpanToErrorSinkTestFixture.cs" />
<Compile Include="Utils\NullPropertyUserControl.cs" />
<Compile Include="Utils\PythonParserHelper.cs" />
<Compile Include="Utils\SupportInitCustomControl.cs" />
<EmbeddedResource Include="Designer\App.ico" />
<None Include="TODO.txt" />
</ItemGroup>

48
src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/SupportInitCustomControl.cs

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
// <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.ComponentModel;
using System.Windows.Forms;
namespace PythonBinding.Tests.Utils
{
public class SupportInitCustomControl : UserControl, ISupportInitialize
{
bool beginInitCalled;
bool endInitCalled;
public SupportInitCustomControl()
{
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public bool IsBeginInitCalled {
get { return beginInitCalled; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public bool IsEndInitCalled {
get { return endInitCalled; }
}
/// <summary>
/// Deliberately making the interface explicit for the BeginInit method but not the EndInit method.
/// </summary>
void ISupportInitialize.BeginInit()
{
beginInitCalled = true;
}
public void EndInit()
{
endInitCalled = true;
}
}
}
Loading…
Cancel
Save