|
|
|
@ -5,45 +5,31 @@ using Avalonia.Controls; |
|
|
|
using Avalonia.Input; |
|
|
|
using Avalonia.VisualTree; |
|
|
|
using Avalonia.Win32.Interop; |
|
|
|
using Control = System.Windows.Forms.Control; |
|
|
|
using WinFormsControl = System.Windows.Forms.Control; |
|
|
|
|
|
|
|
namespace Avalonia.Win32.Embedding |
|
|
|
{ |
|
|
|
[ToolboxItem(true)] |
|
|
|
public class WinFormsAvaloniaControlHost : Control |
|
|
|
public class WinFormsAvaloniaControlHost : WinFormsControl |
|
|
|
{ |
|
|
|
private EmbeddableControl _child; |
|
|
|
private readonly EmbeddableControlRoot _root = new EmbeddableControlRoot(); |
|
|
|
|
|
|
|
public EmbeddableControl Child |
|
|
|
public WinFormsAvaloniaControlHost() |
|
|
|
{ |
|
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint, true); |
|
|
|
UnmanagedMethods.SetParent(_root.PlatformImpl.Handle.Handle, Handle); |
|
|
|
_root.Prepare(); |
|
|
|
if (_root.IsFocused) |
|
|
|
FocusManager.Instance.Focus(null); |
|
|
|
_root.GotFocus += RootGotFocus; |
|
|
|
_root.PlatformImpl.LostFocus += PlatformImpl_LostFocus; |
|
|
|
FixPosition(); |
|
|
|
} |
|
|
|
|
|
|
|
public Avalonia.Controls.Control Content |
|
|
|
{ |
|
|
|
get { return _child; } |
|
|
|
set |
|
|
|
{ |
|
|
|
if (value !=null && value.PlatformImpl.Handle.HandleDescriptor != PlatformConstants.WindowHandleType) |
|
|
|
throw new ArgumentException("Invalid widget for embedding, don't know what to do with " + |
|
|
|
value.PlatformImpl.Handle.HandleDescriptor); |
|
|
|
if (_child != null) |
|
|
|
{ |
|
|
|
_child.GotFocus -= _child_GotFocus; |
|
|
|
_child.PlatformImpl.LostFocus -= PlatformImpl_LostFocus; |
|
|
|
_child.PlatformImpl.Hide(); |
|
|
|
Unfocus(); |
|
|
|
UnmanagedMethods.SetParent(_child.PlatformImpl.Handle.Handle, EmbeddedWindowImpl.DefaultParentWindow); |
|
|
|
} |
|
|
|
_child = value; |
|
|
|
if (_child != null) |
|
|
|
{ |
|
|
|
UnmanagedMethods.SetParent(_child.PlatformImpl.Handle.Handle, Handle); |
|
|
|
_child.Prepare(); |
|
|
|
if (_child.IsFocused) |
|
|
|
FocusManager.Instance.Focus(null); |
|
|
|
_child.GotFocus += _child_GotFocus; |
|
|
|
_child.PlatformImpl.LostFocus += PlatformImpl_LostFocus; |
|
|
|
FixPosition(); |
|
|
|
if(Focused) |
|
|
|
UnmanagedMethods.SetFocus(_child.PlatformImpl.Handle.Handle); |
|
|
|
} |
|
|
|
} |
|
|
|
get { return (Avalonia.Controls.Control)_root.Content; } |
|
|
|
set { _root.Content = value; } |
|
|
|
} |
|
|
|
|
|
|
|
void Unfocus() |
|
|
|
@ -54,7 +40,7 @@ namespace Avalonia.Win32.Embedding |
|
|
|
while (focused.VisualParent != null) |
|
|
|
focused = focused.VisualParent; |
|
|
|
|
|
|
|
if (focused == _child) |
|
|
|
if (focused == _root) |
|
|
|
KeyboardDevice.Instance.SetFocusedElement(null, NavigationMethod.Unspecified, InputModifiers.None); |
|
|
|
} |
|
|
|
|
|
|
|
@ -65,32 +51,30 @@ namespace Avalonia.Win32.Embedding |
|
|
|
|
|
|
|
protected override void Dispose(bool disposing) |
|
|
|
{ |
|
|
|
Child = null; |
|
|
|
if (disposing) |
|
|
|
_root.Dispose(); |
|
|
|
base.Dispose(disposing); |
|
|
|
} |
|
|
|
|
|
|
|
private void _child_GotFocus(object sender, Interactivity.RoutedEventArgs e) |
|
|
|
private void RootGotFocus(object sender, Interactivity.RoutedEventArgs e) |
|
|
|
{ |
|
|
|
UnmanagedMethods.SetFocus(_child.PlatformImpl.Handle.Handle); |
|
|
|
UnmanagedMethods.SetFocus(_root.PlatformImpl.Handle.Handle); |
|
|
|
} |
|
|
|
|
|
|
|
protected override void OnGotFocus(EventArgs e) |
|
|
|
{ |
|
|
|
if (_child != null) |
|
|
|
UnmanagedMethods.SetFocus(_child.PlatformImpl.Handle.Handle); |
|
|
|
if (_root != null) |
|
|
|
UnmanagedMethods.SetFocus(_root.PlatformImpl.Handle.Handle); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void FixPosition() |
|
|
|
{ |
|
|
|
if (_child != null && Width > 0 && Height > 0) |
|
|
|
UnmanagedMethods.MoveWindow(_child.PlatformImpl.Handle.Handle, 0, 0, Width, Height, true); |
|
|
|
if (_root != null && Width > 0 && Height > 0) |
|
|
|
UnmanagedMethods.MoveWindow(_root.PlatformImpl.Handle.Handle, 0, 0, Width, Height, true); |
|
|
|
} |
|
|
|
|
|
|
|
public WinFormsAvaloniaControlHost() |
|
|
|
{ |
|
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint, true); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected override void OnResize(EventArgs e) |
|
|
|
{ |
|
|
|
@ -100,8 +84,7 @@ namespace Avalonia.Win32.Embedding |
|
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs e) |
|
|
|
{ |
|
|
|
if (Child == null) |
|
|
|
base.OnPaint(e); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|