Exemple #1
0
 private void activate(bool active)
 {
     if (active != this.activated)
     {
         this.activated = active;
         if (activated)
         {
             if (Activated != null)
             {
                 Activated.Invoke(this);
             }
         }
         else
         {
             if (Deactivated != null)
             {
                 Deactivated.Invoke(this);
             }
         }
         if (FocusChanged != null)
         {
             FocusChanged.Invoke(this);
         }
     }
 }
Exemple #2
0
 public void EnterBackground()
 {
     _isActive = false;
     if (Deactivated != null)
     {
         Deactivated.Invoke(this, null);
     }
 }
    public void StopInteraction()
    {
        plateMesh.materials[0].DisableKeyword("_EMISSION");
        InvertAllLights();

        wasPressed = false;
        Deactivated.Invoke();
    }
Exemple #4
0
 public void EnterBackground()
 {
     if (_isActive)
     {
         _isActive = false;
         view.Pause();
         Accelerometer.Pause();
         if (Deactivated != null)
         {
             Deactivated.Invoke(this, null);
         }
     }
 }
    void Update()
    {
        var axis = Input.GetAxis(_gripButton);

        if (!Active && axis >= ACTIVATION_THRESHOLD)
        {
            Active = true;
            Activated?.Invoke(this);
        }
        else if (Active && axis <= DEACTIVATION_THRESHOLD)
        {
            Active = false;
            Deactivated.Invoke(this);
        }
    }
Exemple #6
0
        private void OnDeactivate(RevaleeRequestException exception)
        {
            Trace.TraceError("A Revalee recurring task could not be scheduled.");

            if (_CurrentState.TransitionToInactive())
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(2000);
                    this.Start();
                });

                Deactivated.Invoke(this, new DeactivationEventArgs(exception));
            }
        }
    override public void Interact()
    {
        soundEvent.Play();

        if (isPressed)
        {
            Deactivated.Invoke();
            SetCylinderColor(offColor);
        }
        else
        {
            Activated.Invoke();
            SetCylinderColor(onColor);
        }

        isPressed = !isPressed;

        if (buttonLight)
        {
            buttonLight.enabled = !isPressed;
        }
    }
 public void Deactivate()
 {
     Deactivated?.Invoke(this, EventArgs.Empty);
 }
Exemple #9
0
 /// <summary>
 /// Raises the Deactivated event. Override this method to add code to handle when the game loses focus.
 /// </summary>
 /// <param name="sender">The Game.</param>
 /// <param name="args">Arguments for the Deactivated event.</param>
 protected virtual void OnDeactivated(object sender, EventArgs args)
 {
     Deactivated?.Invoke(this, args);
 }
 private void RaiseDeactivated(DeactivationEventArgs e)
 {
     Deactivated?.Invoke(this, e);
 }
Exemple #11
0
        protected virtual unsafe IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)GetMessageTime());

            RawInputEventArgs e = null;

            switch ((WindowsMessage)msg)
            {
            case WindowsMessage.WM_ACTIVATE:
            {
                var wa = (WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case WindowActivate.WA_ACTIVE:
                case WindowActivate.WA_CLICKACTIVE:
                {
                    Activated?.Invoke();
                    break;
                }

                case WindowActivate.WA_INACTIVE:
                {
                    Deactivated?.Invoke();
                    break;
                }
                }

                return(IntPtr.Zero);
            }

            case WindowsMessage.WM_NCCALCSIZE:
            {
                if (ToInt32(wParam) == 1 && !HasFullDecorations)
                {
                    return(IntPtr.Zero);
                }

                break;
            }

            case WindowsMessage.WM_CLOSE:
            {
                bool?preventClosing = Closing?.Invoke();
                if (preventClosing == true)
                {
                    return(IntPtr.Zero);
                }

                break;
            }

            case WindowsMessage.WM_DESTROY:
            {
                //Window doesn't exist anymore
                _hwnd = IntPtr.Zero;
                //Remove root reference to this class, so unmanaged delegate can be collected
                s_instances.Remove(this);
                Closed?.Invoke();

                _mouseDevice.Dispose();
                _touchDevice?.Dispose();
                //Free other resources
                Dispose();
                return(IntPtr.Zero);
            }

            case WindowsMessage.WM_DPICHANGED:
            {
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = Marshal.PtrToStructure <RECT>(lParam);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);
                SetWindowPos(hWnd,
                             IntPtr.Zero,
                             newDisplayRect.left,
                             newDisplayRect.top,
                             newDisplayRect.right - newDisplayRect.left,
                             newDisplayRect.bottom - newDisplayRect.top,
                             SetWindowPosFlags.SWP_NOZORDER |
                             SetWindowPosFlags.SWP_NOACTIVATE);
                return(IntPtr.Zero);
            }

            case WindowsMessage.WM_KEYDOWN:
            case WindowsMessage.WM_SYSKEYDOWN:
            {
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)),
                    WindowsKeyboardDevice.Instance.Modifiers);
                break;
            }

            case WindowsMessage.WM_MENUCHAR:
            {
                // mute the system beep
                return((IntPtr)((int)MenuCharParam.MNC_CLOSE << 16));
            }

            case WindowsMessage.WM_KEYUP:
            case WindowsMessage.WM_SYSKEYUP:
            {
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)),
                    WindowsKeyboardDevice.Instance.Modifiers);
                break;
            }

            case WindowsMessage.WM_CHAR:
            {
                // Ignore control chars
                if (ToInt32(wParam) >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp, _owner,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;
            }

            case WindowsMessage.WM_LBUTTONDOWN:
            case WindowsMessage.WM_RBUTTONDOWN:
            case WindowsMessage.WM_MBUTTONDOWN:
            case WindowsMessage.WM_XBUTTONDOWN:
            {
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }

                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    (WindowsMessage)msg switch
                    {
                        WindowsMessage.WM_LBUTTONDOWN => RawPointerEventType.LeftButtonDown,
                        WindowsMessage.WM_RBUTTONDOWN => RawPointerEventType.RightButtonDown,
                        WindowsMessage.WM_MBUTTONDOWN => RawPointerEventType.MiddleButtonDown,
                        WindowsMessage.WM_XBUTTONDOWN =>
                        HighWord(ToInt32(wParam)) == 1 ?
                        RawPointerEventType.XButton1Down :
                        RawPointerEventType.XButton2Down
                    },
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;
            }
Exemple #12
0
        protected virtual IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool unicode = UnmanagedMethods.IsWindowUnicode(hWnd);

            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)UnmanagedMethods.GetMessageTime());

            RawInputEventArgs e = null;

            WindowsMouseDevice.Instance.CurrentWindow = this;

            switch ((UnmanagedMethods.WindowsMessage)msg)
            {
            case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
                var wa = (UnmanagedMethods.WindowActivate)((int)wParam & 0xffff);

                switch (wa)
                {
                case UnmanagedMethods.WindowActivate.WA_ACTIVE:
                case UnmanagedMethods.WindowActivate.WA_CLICKACTIVE:
                    _isActive = true;
                    Activated?.Invoke();
                    break;

                case UnmanagedMethods.WindowActivate.WA_INACTIVE:
                    _isActive = false;
                    Deactivated?.Invoke();
                    break;
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DESTROY:
                if (Closed != null)
                {
                    UnmanagedMethods.UnregisterClass(_className, Marshal.GetHINSTANCE(GetType().Module));
                    Closed();
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_KEYDOWN:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYDOWN:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey((int)wParam), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_KEYUP:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYUP:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey((int)wParam), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_CHAR:
                // Ignore control chars
                if (wParam.ToInt32() >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp,
                                                  new string((char)wParam.ToInt32(), 1));
                }

                break;

            ////case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
            ////case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
            ////case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
            ////    e = new RawMouseEventArgs(
            ////        WindowsMouseDevice.Instance,
            ////        timestamp,
            ////        _owner,
            ////        msg == (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN
            ////            ? RawMouseEventType.LeftButtonDown
            ////            : msg == (int)UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN
            ////                ? RawMouseEventType.RightButtonDown
            ////                : RawMouseEventType.MiddleButtonDown,
            ////        new Point(0, 0), GetMouseModifiers(wParam));
            ////    break;
            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN
                            ? RawMouseEventType.LeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN
                                ? RawMouseEventType.RightButtonDown
                                : RawMouseEventType.MiddleButtonDown,
                    PointFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONUP:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONUP
                            ? RawMouseEventType.LeftButtonUp
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONUP
                                ? RawMouseEventType.RightButtonUp
                                : RawMouseEventType.MiddleButtonUp,
                    PointFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEMOVE:
                if (!_trackingMouse)
                {
                    var tm = new UnmanagedMethods.TRACKMOUSEEVENT
                    {
                        cbSize      = Marshal.SizeOf(typeof(UnmanagedMethods.TRACKMOUSEEVENT)),
                        dwFlags     = 2,
                        hwndTrack   = _hwnd,
                        dwHoverTime = 0,
                    };

                    UnmanagedMethods.TrackMouseEvent(ref tm);
                }

                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawMouseEventType.Move,
                    PointFromLParam(lParam), GetMouseModifiers(wParam));

                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    ScreenToClient(PointFromLParam(lParam)),
                    new Vector(0, ((int)wParam >> 16) / wheelDelta), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSELEAVE:
                _trackingMouse = false;
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawMouseEventType.LeaveWindow,
                    new Point(), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_PAINT:
                if (Paint != null)
                {
                    UnmanagedMethods.PAINTSTRUCT ps;

                    if (UnmanagedMethods.BeginPaint(_hwnd, out ps) != IntPtr.Zero)
                    {
                        UnmanagedMethods.RECT r;
                        UnmanagedMethods.GetUpdateRect(_hwnd, out r, false);
                        Paint(new Rect(r.left, r.top, r.right - r.left, r.bottom - r.top));
                        UnmanagedMethods.EndPaint(_hwnd, ref ps);
                    }
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_SIZE:
                if (Resized != null)
                {
                    var clientSize = new Size((int)lParam & 0xffff, (int)lParam >> 16);
                    Resized(clientSize);
                }

                return(IntPtr.Zero);
            }

            if (e != null && Input != null)
            {
                Input(e);

                if (msg >= 161 && msg <= 173)
                {
                    return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
                }

                return(IntPtr.Zero);
            }

            return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
        }
Exemple #13
0
        protected virtual IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool unicode = UnmanagedMethods.IsWindowUnicode(hWnd);

            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)UnmanagedMethods.GetMessageTime());

            RawInputEventArgs e = null;

            WindowsMouseDevice.Instance.CurrentWindow = this;

            switch ((UnmanagedMethods.WindowsMessage)msg)
            {
            case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
                var wa = (UnmanagedMethods.WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case UnmanagedMethods.WindowActivate.WA_ACTIVE:
                case UnmanagedMethods.WindowActivate.WA_CLICKACTIVE:
                    Activated?.Invoke();
                    break;

                case UnmanagedMethods.WindowActivate.WA_INACTIVE:
                    Deactivated?.Invoke();
                    break;
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DESTROY:
                //Window doesn't exist anymore
                _hwnd = IntPtr.Zero;
                //Remove root reference to this class, so unmanaged delegate can be collected
                s_instances.Remove(this);
                Closed?.Invoke();
                //Free other resources
                Dispose();
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DPICHANGED:
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = Marshal.PtrToStructure <UnmanagedMethods.RECT>(lParam);
                Position = new Point(newDisplayRect.left, newDisplayRect.top);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_KEYDOWN:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYDOWN:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_KEYUP:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYUP:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_CHAR:
                // Ignore control chars
                if (ToInt32(wParam) >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN
                            ? RawMouseEventType.LeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN
                                ? RawMouseEventType.RightButtonDown
                                : RawMouseEventType.MiddleButtonDown,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONUP:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONUP
                            ? RawMouseEventType.LeftButtonUp
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONUP
                                ? RawMouseEventType.RightButtonUp
                                : RawMouseEventType.MiddleButtonUp,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEMOVE:
                if (!_trackingMouse)
                {
                    var tm = new UnmanagedMethods.TRACKMOUSEEVENT
                    {
                        cbSize      = Marshal.SizeOf <UnmanagedMethods.TRACKMOUSEEVENT>(),
                        dwFlags     = 2,
                        hwndTrack   = _hwnd,
                        dwHoverTime = 0,
                    };

                    UnmanagedMethods.TrackMouseEvent(ref tm);
                }

                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawMouseEventType.Move,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));

                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    PointToClient(PointFromLParam(lParam)),
                    new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEHWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    PointToClient(PointFromLParam(lParam)),
                    new Vector(-(ToInt32(wParam) >> 16) / wheelDelta, 0), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSELEAVE:
                _trackingMouse = false;
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawMouseEventType.LeaveWindow,
                    new Point(), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN
                            ? RawMouseEventType.NonClientLeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN
                                ? RawMouseEventType.RightButtonDown
                                : RawMouseEventType.MiddleButtonDown,
                    new Point(0, 0), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_PAINT:
                UnmanagedMethods.PAINTSTRUCT ps;

                if (UnmanagedMethods.BeginPaint(_hwnd, out ps) != IntPtr.Zero)
                {
                    var f = Scaling;
                    var r = ps.rcPaint;
                    Paint?.Invoke(new Rect(r.left / f, r.top / f, (r.right - r.left) / f, (r.bottom - r.top) / f));
                    UnmanagedMethods.EndPaint(_hwnd, ref ps);
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_SIZE:
                if (Resized != null &&
                    (wParam == (IntPtr)UnmanagedMethods.SizeCommand.Restored ||
                     wParam == (IntPtr)UnmanagedMethods.SizeCommand.Maximized))
                {
                    var clientSize = new Size(ToInt32(lParam) & 0xffff, ToInt32(lParam) >> 16);
                    Resized(clientSize / Scaling);
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_MOVE:
                PositionChanged?.Invoke(new Point((short)(ToInt32(lParam) & 0xffff), (short)(ToInt32(lParam) >> 16)));
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DISPLAYCHANGE:
                (Screen as ScreenImpl)?.InvalidateScreensCache();
                return(IntPtr.Zero);
            }
#if USE_MANAGED_DRAG
            if (_managedDrag.PreprocessInputEvent(ref e))
            {
                return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
            }
#endif

            if (e != null && Input != null)
            {
                Input(e);

                if (e.Handled)
                {
                    return(IntPtr.Zero);
                }
            }

            return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
        }
Exemple #14
0
 private void OnDeactivated()
 {
     Deactivated?.Invoke(this, EventArgs.Empty);
 }
 public void OnDeactivated()
 {
     Deactivated?.Invoke(this);
 }
 protected virtual void OnDeactivated(EventArgs e)
 {
     Deactivated?.Invoke(this, e);
 }
Exemple #17
0
        protected virtual IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool unicode = UnmanagedMethods.IsWindowUnicode(hWnd);

            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)UnmanagedMethods.GetMessageTime());

            RawInputEventArgs e = null;

            WindowsMouseDevice.Instance.CurrentWindow = this;

            switch ((UnmanagedMethods.WindowsMessage)msg)
            {
            case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
                var wa = (UnmanagedMethods.WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case UnmanagedMethods.WindowActivate.WA_ACTIVE:
                case UnmanagedMethods.WindowActivate.WA_CLICKACTIVE:
                    Activated?.Invoke();
                    break;

                case UnmanagedMethods.WindowActivate.WA_INACTIVE:
                    Deactivated?.Invoke();
                    break;
                }

                return(IntPtr.Zero);

            case WindowsMessage.WM_NCCALCSIZE:
                if (ToInt32(wParam) == 1 && !_decorated)
                {
                    return(IntPtr.Zero);
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_CLOSE:
                bool?preventClosing = Closing?.Invoke();
                if (preventClosing == true)
                {
                    return(IntPtr.Zero);
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_DESTROY:
                //Window doesn't exist anymore
                _hwnd = IntPtr.Zero;
                //Remove root reference to this class, so unmanaged delegate can be collected
                s_instances.Remove(this);
                Closed?.Invoke();
                if (_parent != null)
                {
                    _parent._disabledBy.Remove(this);
                    _parent.UpdateEnabled();
                }
                //Free other resources
                Dispose();
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DPICHANGED:
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = Marshal.PtrToStructure <UnmanagedMethods.RECT>(lParam);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);
                SetWindowPos(hWnd,
                             IntPtr.Zero,
                             newDisplayRect.left,
                             newDisplayRect.top,
                             newDisplayRect.right - newDisplayRect.left,
                             newDisplayRect.bottom - newDisplayRect.top,
                             SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_KEYDOWN:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYDOWN:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_KEYUP:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYUP:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_CHAR:
                // Ignore control chars
                if (ToInt32(wParam) >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                e = new RawPointerEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN
                            ? RawPointerEventType.LeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN
                                ? RawPointerEventType.RightButtonDown
                                : RawPointerEventType.MiddleButtonDown,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONUP:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                e = new RawPointerEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONUP
                            ? RawPointerEventType.LeftButtonUp
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONUP
                                ? RawPointerEventType.RightButtonUp
                                : RawPointerEventType.MiddleButtonUp,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEMOVE:
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }
                if (!_trackingMouse)
                {
                    var tm = new UnmanagedMethods.TRACKMOUSEEVENT
                    {
                        cbSize      = Marshal.SizeOf <UnmanagedMethods.TRACKMOUSEEVENT>(),
                        dwFlags     = 2,
                        hwndTrack   = _hwnd,
                        dwHoverTime = 0,
                    };

                    UnmanagedMethods.TrackMouseEvent(ref tm);
                }

                e = new RawPointerEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawPointerEventType.Move,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));

                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    PointToClient(PointFromLParam(lParam)),
                    new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEHWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    PointToClient(PointFromLParam(lParam)),
                    new Vector(-(ToInt32(wParam) >> 16) / wheelDelta, 0), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSELEAVE:
                _trackingMouse = false;
                e = new RawPointerEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawPointerEventType.LeaveWindow,
                    new Point(), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
                e = new RawPointerEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN
                            ? RawPointerEventType.NonClientLeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN
                                ? RawPointerEventType.RightButtonDown
                                : RawPointerEventType.MiddleButtonDown,
                    new Point(0, 0), GetMouseModifiers(wParam));
                break;

            case WindowsMessage.WM_TOUCH:
                var touchInputs = new TOUCHINPUT[wParam.ToInt32()];
                if (GetTouchInputInfo(lParam, (uint)wParam.ToInt32(), touchInputs, Marshal.SizeOf <TOUCHINPUT>()))
                {
                    foreach (var touchInput in touchInputs)
                    {
                        Input?.Invoke(new RawTouchEventArgs(_touchDevice, touchInput.Time,
                                                            _owner,
                                                            touchInput.Flags.HasFlag(TouchInputFlags.TOUCHEVENTF_UP) ?
                                                            RawPointerEventType.TouchEnd :
                                                            touchInput.Flags.HasFlag(TouchInputFlags.TOUCHEVENTF_DOWN) ?
                                                            RawPointerEventType.TouchBegin :
                                                            RawPointerEventType.TouchUpdate,
                                                            PointToClient(new PixelPoint(touchInput.X / 100, touchInput.Y / 100)),
                                                            WindowsKeyboardDevice.Instance.Modifiers,
                                                            touchInput.Id));
                    }
                    CloseTouchInputHandle(lParam);
                    return(IntPtr.Zero);
                }

                break;

            case WindowsMessage.WM_NCPAINT:
                if (!_decorated)
                {
                    return(IntPtr.Zero);
                }
                break;

            case WindowsMessage.WM_NCACTIVATE:
                if (!_decorated)
                {
                    return(new IntPtr(1));
                }
                break;

            case UnmanagedMethods.WindowsMessage.WM_PAINT:
                using (_rendererLock.Lock())
                {
                    UnmanagedMethods.PAINTSTRUCT ps;
                    if (UnmanagedMethods.BeginPaint(_hwnd, out ps) != IntPtr.Zero)
                    {
                        var f = Scaling;
                        var r = ps.rcPaint;
                        Paint?.Invoke(new Rect(r.left / f, r.top / f, (r.right - r.left) / f,
                                               (r.bottom - r.top) / f));
                        UnmanagedMethods.EndPaint(_hwnd, ref ps);
                    }
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_SIZE:
                using (_rendererLock.Lock())
                {
                    // Do nothing here, just block until the pending frame render is completed on the render thread
                }
                var size = (UnmanagedMethods.SizeCommand)wParam;

                if (Resized != null &&
                    (size == UnmanagedMethods.SizeCommand.Restored ||
                     size == UnmanagedMethods.SizeCommand.Maximized))
                {
                    var clientSize = new Size(ToInt32(lParam) & 0xffff, ToInt32(lParam) >> 16);
                    Resized(clientSize / Scaling);
                }

                var windowState = size == SizeCommand.Maximized ? WindowState.Maximized
                        : (size == SizeCommand.Minimized ? WindowState.Minimized : WindowState.Normal);

                if (windowState != _lastWindowState)
                {
                    _lastWindowState = windowState;
                    WindowStateChanged?.Invoke(windowState);
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_MOVE:
                PositionChanged?.Invoke(new PixelPoint((short)(ToInt32(lParam) & 0xffff), (short)(ToInt32(lParam) >> 16)));
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_GETMINMAXINFO:

                MINMAXINFO mmi = Marshal.PtrToStructure <UnmanagedMethods.MINMAXINFO>(lParam);

                if (_minSize.Width > 0)
                {
                    mmi.ptMinTrackSize.X = (int)((_minSize.Width * Scaling) + BorderThickness.Left + BorderThickness.Right);
                }

                if (_minSize.Height > 0)
                {
                    mmi.ptMinTrackSize.Y = (int)((_minSize.Height * Scaling) + BorderThickness.Top + BorderThickness.Bottom);
                }

                if (!Double.IsInfinity(_maxSize.Width) && _maxSize.Width > 0)
                {
                    mmi.ptMaxTrackSize.X = (int)((_maxSize.Width * Scaling) + BorderThickness.Left + BorderThickness.Right);
                }

                if (!Double.IsInfinity(_maxSize.Height) && _maxSize.Height > 0)
                {
                    mmi.ptMaxTrackSize.Y = (int)((_maxSize.Height * Scaling) + BorderThickness.Top + BorderThickness.Bottom);
                }

                Marshal.StructureToPtr(mmi, lParam, true);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DISPLAYCHANGE:
                (Screen as ScreenImpl)?.InvalidateScreensCache();
                return(IntPtr.Zero);
            }

#if USE_MANAGED_DRAG
            if (_managedDrag.PreprocessInputEvent(ref e))
            {
                return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
            }
#endif

            if (e != null && Input != null)
            {
                Input(e);

                if (e.Handled)
                {
                    return(IntPtr.Zero);
                }
            }

            using (_rendererLock.Lock())
                return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
        }
Exemple #18
0
        protected virtual IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            bool unicode = UnmanagedMethods.IsWindowUnicode(hWnd);

            const double wheelDelta = 120.0;
            uint         timestamp  = unchecked ((uint)UnmanagedMethods.GetMessageTime());

            RawInputEventArgs e = null;

            WindowsMouseDevice.Instance.CurrentWindow = this;

            switch ((UnmanagedMethods.WindowsMessage)msg)
            {
            case UnmanagedMethods.WindowsMessage.WM_ACTIVATE:
                var wa = (UnmanagedMethods.WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case UnmanagedMethods.WindowActivate.WA_ACTIVE:
                case UnmanagedMethods.WindowActivate.WA_CLICKACTIVE:
                    _isActive = true;
                    Activated?.Invoke();
                    break;

                case UnmanagedMethods.WindowActivate.WA_INACTIVE:
                    _isActive = false;
                    Deactivated?.Invoke();
                    break;
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DESTROY:
                if (Closed != null)
                {
                    UnmanagedMethods.UnregisterClass(_className, Marshal.GetHINSTANCE(GetType().Module));
                    Closed();
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_DPICHANGED:
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = (UnmanagedMethods.RECT)Marshal.PtrToStructure(lParam, typeof(UnmanagedMethods.RECT));
                Position = new Point(newDisplayRect.left, newDisplayRect.top);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);
                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_KEYDOWN:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYDOWN:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_KEYUP:
            case UnmanagedMethods.WindowsMessage.WM_SYSKEYUP:
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam)), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_CHAR:
                // Ignore control chars
                if (ToInt32(wParam) >= 32)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONDOWN:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONDOWN
                            ? RawMouseEventType.LeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONDOWN
                                ? RawMouseEventType.RightButtonDown
                                : RawMouseEventType.MiddleButtonDown,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_LBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_RBUTTONUP:
            case UnmanagedMethods.WindowsMessage.WM_MBUTTONUP:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_LBUTTONUP
                            ? RawMouseEventType.LeftButtonUp
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_RBUTTONUP
                                ? RawMouseEventType.RightButtonUp
                                : RawMouseEventType.MiddleButtonUp,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEMOVE:
                if (!_trackingMouse)
                {
                    var tm = new UnmanagedMethods.TRACKMOUSEEVENT
                    {
                        cbSize      = Marshal.SizeOf(typeof(UnmanagedMethods.TRACKMOUSEEVENT)),
                        dwFlags     = 2,
                        hwndTrack   = _hwnd,
                        dwHoverTime = 0,
                    };

                    UnmanagedMethods.TrackMouseEvent(ref tm);
                }

                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawMouseEventType.Move,
                    DipFromLParam(lParam), GetMouseModifiers(wParam));

                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    ScreenToClient(DipFromLParam(lParam)),
                    new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSEHWHEEL:
                e = new RawMouseWheelEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    ScreenToClient(DipFromLParam(lParam)),
                    new Vector(-(ToInt32(wParam) >> 16) / wheelDelta, 0), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_MOUSELEAVE:
                _trackingMouse = false;
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    RawMouseEventType.LeaveWindow,
                    new Point(), WindowsKeyboardDevice.Instance.Modifiers);
                break;

            case UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN:
            case UnmanagedMethods.WindowsMessage.WM_NCMBUTTONDOWN:
                e = new RawMouseEventArgs(
                    WindowsMouseDevice.Instance,
                    timestamp,
                    _owner,
                    msg == (int)UnmanagedMethods.WindowsMessage.WM_NCLBUTTONDOWN
                            ? RawMouseEventType.NonClientLeftButtonDown
                            : msg == (int)UnmanagedMethods.WindowsMessage.WM_NCRBUTTONDOWN
                                ? RawMouseEventType.RightButtonDown
                                : RawMouseEventType.MiddleButtonDown,
                    new Point(0, 0), GetMouseModifiers(wParam));
                break;

            case UnmanagedMethods.WindowsMessage.WM_PAINT:
                if (Paint != null)
                {
                    UnmanagedMethods.PAINTSTRUCT ps;

                    if (UnmanagedMethods.BeginPaint(_hwnd, out ps) != IntPtr.Zero)
                    {
                        UnmanagedMethods.RECT r;
                        UnmanagedMethods.GetUpdateRect(_hwnd, out r, false);
                        var f = Scaling;
                        Paint(new Rect(r.left / f, r.top / f, (r.right - r.left) / f, (r.bottom - r.top) / f));
                        UnmanagedMethods.EndPaint(_hwnd, ref ps);
                    }
                }

                return(IntPtr.Zero);

            case UnmanagedMethods.WindowsMessage.WM_SIZE:
                if (Resized != null &&
                    (wParam == (IntPtr)UnmanagedMethods.SizeCommand.Restored ||
                     wParam == (IntPtr)UnmanagedMethods.SizeCommand.Maximized))
                {
                    var clientSize = new Size(ToInt32(lParam) & 0xffff, ToInt32(lParam) >> 16);
                    Resized(clientSize / Scaling);
                }

                return(IntPtr.Zero);
            }

            if (e != null && Input != null)
            {
                Input(e);

                if (e.Handled)
                {
                    return(IntPtr.Zero);
                }
            }

            return(UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam));
        }
Exemple #19
0
 /// <summary>
 /// Raises the <see cref="Deactivated"/> event.
 /// </summary>
 /// <param name="eventArgs">
 /// <see cref="DeactivationEventArgs"/> object that provides the arguments for the event.
 /// </param>
 /// <remarks>
 /// <strong>Notes to Inheritors:</strong> When overriding <see cref="OnDeactivated"/> in a
 /// derived class, be sure to call the base class's <see cref="OnDeactivated"/> method so
 /// that registered delegates receive the event.
 /// </remarks>
 protected virtual void OnDeactivated(DeactivationEventArgs eventArgs)
 {
     Deactivated?.Invoke(this, eventArgs);
 }
Exemple #20
0
        /// <summary>
        /// Handles a deactivated notification from <see cref="IWindowBaseImpl.Deactivated"/>.
        /// </summary>
        private void HandleDeactivated()
        {
            IsActive = false;

            Deactivated?.Invoke(this, EventArgs.Empty);
        }
Exemple #21
0
        public override void EnterMessageLoop(bool runInBackground)
        {
            IntPtr wmDeleteWindow = XInternAtom(_display, "WM_DELETE_WINDOW", false);

            XSetWMProtocols(_display, _window, new IntPtr[] { wmDeleteWindow }, 1);

            IntPtr e       = System.Runtime.InteropServices.Marshal.AllocHGlobal(24 * sizeof(long));
            bool   running = true;

            while (running)
            {
                while (XPending(_display) != 0)
                {
                    XNextEvent(_display, e);

                    if (XFilterEvent(e, IntPtr.Zero))
                    {
                        continue;
                    }

                    XAnyEvent anyEvent = (XAnyEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XAnyEvent));

                    switch ((Event)anyEvent.type)
                    {
                    case Event.ClientMessage:
                    {
                        XClientMessageEvent clientEvent = (XClientMessageEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XClientMessageEvent));
                        if (clientEvent.l0 == wmDeleteWindow)
                        {
                            bool cancel = false;
                            Closing?.Invoke(this, ref cancel);
                            if (!cancel)
                            {
                                Closed?.Invoke(this);
                                running = false;
                            }
                        }
                        break;
                    }

                    case Event.ConfigureNotify:
                    {
                        XConfigureEvent configureEvent = (XConfigureEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XConfigureEvent));

                        LocationChanged?.Invoke(this, configureEvent.x, configureEvent.y);
                        SizeChanged?.Invoke(this, configureEvent.width, configureEvent.height);
                        break;
                    }

                    case Event.FocusIn:
                    {
                        Activated?.Invoke(this);
                        break;
                    }

                    case Event.FocusOut:
                    {
                        Deactivated?.Invoke(this);
                        break;
                    }

                    case Event.MotionNotify:
                    {
                        XMotionEvent motionEvent = (XMotionEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XMotionEvent));
                        MouseMove?.Invoke(this, motionEvent.x, motionEvent.y);
                        break;
                    }

                    case Event.ButtonPress:
                    {
                        XButtonEvent buttonEvent = (XButtonEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XButtonEvent));

                        switch ((Button)buttonEvent.button)
                        {
                        case Button.Button1:
                        {
                            MouseButtonDown?.Invoke(this, buttonEvent.x, buttonEvent.y, MouseButton.Left);
                            break;
                        }

                        case Button.Button2:
                        {
                            MouseButtonDown?.Invoke(this, buttonEvent.x, buttonEvent.y, MouseButton.Middle);
                            break;
                        }

                        case Button.Button3:
                        {
                            MouseButtonDown?.Invoke(this, buttonEvent.x, buttonEvent.y, MouseButton.Right);
                            break;
                        }

                        case Button.Button4:
                        {
                            MouseWheel?.Invoke(this, buttonEvent.x, buttonEvent.y, 120);
                            break;
                        }

                        case Button.Button5:
                        {
                            MouseWheel?.Invoke(this, buttonEvent.x, buttonEvent.y, -120);
                            break;
                        }
                        }
                        break;
                    }

                    case Event.ButtonRelease:
                    {
                        XButtonEvent buttonEvent = (XButtonEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XButtonEvent));

                        switch ((Button)buttonEvent.button)
                        {
                        case Button.Button1:
                        {
                            MouseButtonUp?.Invoke(this, buttonEvent.x, buttonEvent.y, MouseButton.Left);
                            break;
                        }

                        case Button.Button2:
                        {
                            MouseButtonUp?.Invoke(this, buttonEvent.x, buttonEvent.y, MouseButton.Middle);
                            break;
                        }

                        case Button.Button3:
                        {
                            MouseButtonUp?.Invoke(this, buttonEvent.x, buttonEvent.y, MouseButton.Right);
                            break;
                        }
                        }
                        break;
                    }

                    case Event.KeyPress:
                    {
                        XKeyEvent keyEvent = (XKeyEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XKeyEvent));
                        KeySym    ks       = XLookupKeysym(ref keyEvent, 0);
                        Key       key      = NoesisKey(ks);
                        if (key != Key.None)
                        {
                            KeyDown?.Invoke(this, key);
                        }

                        if (_xic != IntPtr.Zero)
                        {
                            Status status = 0;
                            byte[] buffer = new byte[256];
                            KeySym ret_ks = (KeySym)0;
                            int    size   = Xutf8LookupString(_xic, e, buffer, 255, ref ret_ks, ref status);
                            if (size > 0 && ((int)status == XLookupChars || (int)status == XLookupBoth))
                            {
                                buffer[size] = 0;
                                Decoder decoder   = _utf8.GetDecoder();
                                char[]  text      = new char[256];
                                int     bytesUsed = 0;
                                int     charsUsed = 0;
                                bool    completed = false;
                                decoder.Convert(buffer, 0, size, text, 0, 255, true, out bytesUsed, out charsUsed, out completed);
                                for (int i = 0; i < charsUsed; ++i)
                                {
                                    Char?.Invoke(this, text[i]);
                                }
                            }
                        }
                        break;
                    }

                    case Event.KeyRelease:
                    {
                        XKeyEvent keyEvent = (XKeyEvent)System.Runtime.InteropServices.Marshal.PtrToStructure(e, typeof(XKeyEvent));
                        KeySym    ks       = XLookupKeysym(ref keyEvent, 0);
                        Key       key      = NoesisKey(ks);
                        if (key != Key.None)
                        {
                            KeyUp?.Invoke(this, key);
                        }
                        break;
                    }
                    }
                }

                Render?.Invoke(this);
            }
            System.Runtime.InteropServices.Marshal.FreeHGlobal(e);

            XCloseDisplay(_display);
        }
 public void Hide()
 {
     Dispatcher.UIThread.Post(() => Deactivated?.Invoke(), DispatcherPriority.Input);
 }
Exemple #23
0
 protected void RaiseDeactivated()
 {
     using (Context.Activate().Scoped()) {
         Deactivated?.Invoke();
     }
 }
        protected virtual unsafe IntPtr AppWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            const double      wheelDelta = 120.0;
            uint              timestamp  = unchecked ((uint)GetMessageTime());
            RawInputEventArgs e          = null;
            var shouldTakeFocus          = false;

            switch ((WindowsMessage)msg)
            {
            case WindowsMessage.WM_ACTIVATE:
            {
                var wa = (WindowActivate)(ToInt32(wParam) & 0xffff);

                switch (wa)
                {
                case WindowActivate.WA_ACTIVE:
                case WindowActivate.WA_CLICKACTIVE:
                {
                    Activated?.Invoke();
                    UpdateInputMethod(GetKeyboardLayout(0));
                    break;
                }

                case WindowActivate.WA_INACTIVE:
                {
                    Deactivated?.Invoke();
                    break;
                }
                }

                return(IntPtr.Zero);
            }

            case WindowsMessage.WM_NCCALCSIZE:
            {
                if (ToInt32(wParam) == 1 && !HasFullDecorations || _isClientAreaExtended)
                {
                    return(IntPtr.Zero);
                }

                break;
            }

            case WindowsMessage.WM_CLOSE:
            {
                bool?preventClosing = Closing?.Invoke();
                if (preventClosing == true)
                {
                    return(IntPtr.Zero);
                }

                BeforeCloseCleanup(false);

                // Used to distinguish between programmatic and regular close requests.
                _isCloseRequested = true;

                break;
            }

            case WindowsMessage.WM_DESTROY:
            {
                //Window doesn't exist anymore
                _hwnd = IntPtr.Zero;
                //Remove root reference to this class, so unmanaged delegate can be collected
                s_instances.Remove(this);
                Closed?.Invoke();

                _mouseDevice.Dispose();
                _touchDevice?.Dispose();
                //Free other resources
                Dispose();
                return(IntPtr.Zero);
            }

            case WindowsMessage.WM_DPICHANGED:
            {
                var dpi            = ToInt32(wParam) & 0xffff;
                var newDisplayRect = Marshal.PtrToStructure <RECT>(lParam);
                _scaling = dpi / 96.0;
                ScalingChanged?.Invoke(_scaling);

                using (SetResizeReason(PlatformResizeReason.DpiChange))
                {
                    SetWindowPos(hWnd,
                                 IntPtr.Zero,
                                 newDisplayRect.left,
                                 newDisplayRect.top,
                                 newDisplayRect.right - newDisplayRect.left,
                                 newDisplayRect.bottom - newDisplayRect.top,
                                 SetWindowPosFlags.SWP_NOZORDER |
                                 SetWindowPosFlags.SWP_NOACTIVATE);
                }

                return(IntPtr.Zero);
            }

            case WindowsMessage.WM_KEYDOWN:
            case WindowsMessage.WM_SYSKEYDOWN:
            {
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyDown,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)),
                    WindowsKeyboardDevice.Instance.Modifiers);
                break;
            }

            case WindowsMessage.WM_SYSCOMMAND:
                // Disable system handling of Alt/F10 menu keys.
                if ((SysCommands)wParam == SysCommands.SC_KEYMENU && HighWord(ToInt32(lParam)) <= 0)
                {
                    return(IntPtr.Zero);
                }
                break;

            case WindowsMessage.WM_MENUCHAR:
            {
                // mute the system beep
                return((IntPtr)((int)MenuCharParam.MNC_CLOSE << 16));
            }

            case WindowsMessage.WM_KEYUP:
            case WindowsMessage.WM_SYSKEYUP:
            {
                e = new RawKeyEventArgs(
                    WindowsKeyboardDevice.Instance,
                    timestamp,
                    _owner,
                    RawKeyEventType.KeyUp,
                    KeyInterop.KeyFromVirtualKey(ToInt32(wParam), ToInt32(lParam)),
                    WindowsKeyboardDevice.Instance.Modifiers);
                break;
            }

            case WindowsMessage.WM_CHAR:
            {
                // Ignore control chars and chars that were handled in WM_KEYDOWN.
                if (ToInt32(wParam) >= 32 && !_ignoreWmChar)
                {
                    e = new RawTextInputEventArgs(WindowsKeyboardDevice.Instance, timestamp, _owner,
                                                  new string((char)ToInt32(wParam), 1));
                }

                break;
            }

            case WindowsMessage.WM_LBUTTONDOWN:
            case WindowsMessage.WM_RBUTTONDOWN:
            case WindowsMessage.WM_MBUTTONDOWN:
            case WindowsMessage.WM_XBUTTONDOWN:
            {
                shouldTakeFocus = ShouldTakeFocusOnClick;
                if (ShouldIgnoreTouchEmulatedMessage())
                {
                    break;
                }

                e = new RawPointerEventArgs(
                    _mouseDevice,
                    timestamp,
                    _owner,
                    (WindowsMessage)msg switch
                    {
                        WindowsMessage.WM_LBUTTONDOWN => RawPointerEventType.LeftButtonDown,
                        WindowsMessage.WM_RBUTTONDOWN => RawPointerEventType.RightButtonDown,
                        WindowsMessage.WM_MBUTTONDOWN => RawPointerEventType.MiddleButtonDown,
                        WindowsMessage.WM_XBUTTONDOWN =>
                        HighWord(ToInt32(wParam)) == 1 ?
                        RawPointerEventType.XButton1Down :
                        RawPointerEventType.XButton2Down
                    },
                    DipFromLParam(lParam), GetMouseModifiers(wParam));
                break;
            }
Exemple #25
0
 protected virtual void OnDeactivated()
 {
     _onDeactivated.Invoke();
     Deactivated?.Invoke(this, EventArgs.Empty);
 }