<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;

namespace HelixToolkit.Wpf.SharpDX.Controls
{
    public class WinformHostExtend : WindowsFormsHost
    {
        protected UIElement ParentControl { set; get; }
        public WinformHostExtend()
        {
            ChildChanged += OnChildChanged;
        }

        private void OnChildChanged(object sender, ChildChangedEventArgs childChangedEventArgs)
        {
            var previousChild = childChangedEventArgs.PreviousChild as Control;
            if (previousChild != null)
            {
                previousChild.MouseDown -= OnMouseDown;
                previousChild.MouseWheel -= OnMouseWheel;
                previousChild.MouseUp -= OnMouseUp;
            }
            if (Child != null)
            {
                Child.MouseDown += OnMouseDown;
                Child.MouseWheel += OnMouseWheel;
                Child.MouseUp += OnMouseUp;
            }
        }

        private void OnMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            RaiseEvent(new MouseWheelEventArgs(Mouse.PrimaryDevice, 0, e.Delta)
            {
                RoutedEvent = Mouse.MouseWheelEvent,
                Source = this,
            });
        }

        private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs mouseEventArgs)
        {
            MouseButton? wpfButton = ConvertToWpf(mouseEventArgs.Button);
            if (!wpfButton.HasValue)
                return;
            if (ParentControl != null)
            {
                Mouse.Capture(ParentControl, CaptureMode.Element);
                ParentControl.ReleaseMouseCapture();
            }
            else
            {
                this.CaptureMouse();
                this.ReleaseMouseCapture();
            }
            RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, wpfButton.Value)
            {
                RoutedEvent = Mouse.MouseDownEvent,
                Source = this,
            });
        }
        private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs mouseEventArgs)
        {
            MouseButton? wpfButton = ConvertToWpf(mouseEventArgs.Button);
            if (!wpfButton.HasValue)
                return;
            if (ParentControl != null)
            {
                Mouse.Capture(ParentControl, CaptureMode.Element);
                ParentControl.ReleaseMouseCapture();
            }
            else
            {
                this.CaptureMouse();
                this.ReleaseMouseCapture();
            }
            ParentControl.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, wpfButton.Value)
            {
                RoutedEvent = Mouse.MouseUpEvent,
                Source = this,
            });
        }
        private MouseButton? ConvertToWpf(MouseButtons winformButton)
        {
            switch (winformButton)
            {
                case MouseButtons.Left:
                    return MouseButton.Left;
                case MouseButtons.None:
                    return null;
                case MouseButtons.Right:
                    return MouseButton.Right;
                case MouseButtons.Middle:
                    return MouseButton.Middle;
                case MouseButtons.XButton1:
                    return MouseButton.XButton1;
                case MouseButtons.XButton2:
                    return MouseButton.XButton2;<commit_msg>Implement mouse enter/leave/move event for WinformHostExtend<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;

namespace HelixToolkit.Wpf.SharpDX.Controls
{
    public class WinformHostExtend : WindowsFormsHost
    {
        protected UIElement ParentControl { set; get; }
        public WinformHostExtend()
        {
            ChildChanged += OnChildChanged;
        }

        private void OnChildChanged(object sender, ChildChangedEventArgs childChangedEventArgs)
        {
            var previousChild = childChangedEventArgs.PreviousChild as Control;
            if (previousChild != null)
            {
                previousChild.MouseDown -= OnMouseDown;
                previousChild.MouseWheel -= OnMouseWheel;
                previousChild.MouseMove -= OnMouseMove;
                previousChild.MouseUp -= OnMouseUp;
                previousChild.MouseEnter -= OnMouseEnter;
                previousChild.MouseLeave -= OnMouseLeave;
            }
            if (Child != null)
            {
                Child.MouseDown += OnMouseDown;
                Child.MouseWheel += OnMouseWheel;
                Child.MouseMove += OnMouseMove;
                Child.MouseUp += OnMouseUp;
                Child.MouseEnter += OnMouseEnter;
                Child.MouseLeave += OnMouseLeave;
            }
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            Capture();
            RaiseEvent(new System.Windows.Input.MouseEventArgs(Mouse.PrimaryDevice, DateTime.Now.Millisecond)
            {
                RoutedEvent = Mouse.MouseLeaveEvent,
                Source = this,
            });
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            Capture();
            RaiseEvent(new System.Windows.Input.MouseEventArgs(Mouse.PrimaryDevice, DateTime.Now.Millisecond)
            {
                RoutedEvent = Mouse.MouseEnterEvent,
                Source = this,
            });
        }

        private System.Drawing.Point prevP;
        private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if(prevP == e.Location) { return; }
            prevP = e.Location;
            Capture();
            RaiseEvent(new System.Windows.Input.MouseEventArgs(Mouse.PrimaryDevice, DateTime.Now.Millisecond)
            {
                RoutedEvent = Mouse.MouseMoveEvent,
                Source = this,
            });
        }

        private void OnMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            RaiseEvent(new MouseWheelEventArgs(Mouse.PrimaryDevice, DateTime.Now.Millisecond, e.Delta)
            {
                RoutedEvent = Mouse.MouseWheelEvent,
                Source = this,
            });
        }

        private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs mouseEventArgs)
        {
            MouseButton? wpfButton = ConvertToWpf(mouseEventArgs.Button);
            if (!wpfButton.HasValue)
                return;
            Capture();
            RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, DateTime.Now.Millisecond, wpfButton.Value)
            {
                RoutedEvent = Mouse.MouseDownEvent,
                Source = this,
            });
        }
        private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs mouseEventArgs)
        {
            MouseButton? wpfButton = ConvertToWpf(mouseEventArgs.Button);
            if (!wpfButton.HasValue)
                return;
            Capture();
            RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, DateTime.Now.Millisecond, wpfButton.Value)
            {
                RoutedEvent = Mouse.MouseUpEvent,
                Source = this,
            });
        }

        /// <summary>
        /// Has to do this, otherwise the mouse point is wrong in mouse event.
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private void Capture()
        {
            this.CaptureMouse();
            this.ReleaseMouseCapture();
        }

        private MouseButton? ConvertToWpf(MouseButtons winformButton)
        {
            switch (winformButton)
            {
                case MouseButtons.Left:
                    return MouseButton.Left;
                case MouseButtons.None:
                    return null;
                case MouseButtons.Right:
                    return MouseButton.Right;
                case MouseButtons.Middle:
                    return MouseButton.Middle;
                case MouseButtons.XButton1:
                    return MouseButton.XButton1;
                case MouseButtons.XButton2:
                    return MouseButton.XButton2;
{'fraction_non_alphanumeric': 0.07492426007923561, 'fraction_numerical': 0.001281752505243533, 'mean_word_length': 1.6143770941212305, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2012995', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2012, 'n_words': 397}