Example #1
0
        public void Step1_SetControl(Control control)
        {
            m_control = control;

            var form = m_control.FindForm();
            form.KeyDown += delegate(object sender, KeyEventArgs e) {
                if (e.KeyCode == Keys.ShiftKey)
                {
                    m_shiftDown = true;
                }
                if (e.KeyCode == Keys.ControlKey)
                {
                    m_ctrlDown = true;
                }
            };
            form.KeyUp += delegate(object sender, KeyEventArgs e) {
                if (e.KeyCode == Keys.ShiftKey)
                {
                    m_shiftDown = false;
                }
                if (e.KeyCode == Keys.Control)
                {
                    m_ctrlDown = false;
                }
            };

            m_control.MouseDown += delegate(object o, MouseEventArgs args) {
                // Change keyboard modifier state.
                var shift = m_shiftDown;
                var ctrl = m_ctrlDown;
                if (shift) {
                    m_modifierState |= MouseToolAction.ModifierKey.Shift;
                }
                if (ctrl) {
                    m_modifierState |= MouseToolAction.ModifierKey.Control;
                }

                // Change mouse button state.
                var button = args.Button;
                switch (button) {
                    case MouseButtons.Left: m_mouseState |= MouseToolAction.MouseButton.Left; break;
                    case MouseButtons.Middle: m_mouseState |= MouseToolAction.MouseButton.Middle; break;
                    case MouseButtons.Right: m_mouseState |= MouseToolAction.MouseButton.Right; break;
                }

                var viewTransform = this.CurrentViewToControlMatrix;
                m_mouseDownViewTransform = viewTransform;

                // Set mouse down locations.
                float px = args.X;
                float py = args.Y;
                this.m_mouseDownControlPosition = new PointD (px, py);
                var inv = viewTransform.Clone () as Matrix;
                inv.Invert ();
                var ps = new PointF[]{new PointF(px, py)};
                inv.TransformPoints(ps);
                px = ps[0].X;
                py = ps[0].Y;
                this.m_mouseDownViewPosition = new PointD (px, py);

                // If pressing a mouse button, complete the previous mouse tool action.
                if (this.m_currentMouseToolAction != null) {
                    if (this.m_currentMouseToolAction.MouseUp != null) {
                        this.m_currentMouseToolAction.MouseUp (px, py);
                    }
                }

                // Find the mouse tool action that matches the current state.
                m_currentMouseToolAction = null;
                foreach (var mouseToolAction in m_mouseToolActions) {
                    if (mouseToolAction.Button == m_mouseState &&
                        mouseToolAction.Modifier == m_modifierState) {
                        this.m_currentMouseToolAction = mouseToolAction;
                        if (this.m_currentMouseToolAction.MouseDown != null) {
                            this.m_currentMouseToolAction.MouseDown (px, py);
                        }

                        return;
                    }
                }
            };
            m_control.MouseUp += delegate(object o, MouseEventArgs args) {
                // Change keyboard modifier state.
                var shift = m_shiftDown;
                var ctrl = m_ctrlDown;
                if (shift) {
                    m_modifierState &= ~MouseToolAction.ModifierKey.Shift;
                }
                if (ctrl) {
                    m_modifierState &= ~MouseToolAction.ModifierKey.Control;
                }

                // Change mouse button state.
                var button = args.Button;
                switch (button) {
                    case MouseButtons.Left: m_mouseState &= ~MouseToolAction.MouseButton.Left; break;
                    case MouseButtons.Middle: m_mouseState &= ~MouseToolAction.MouseButton.Middle; break;
                    case MouseButtons.Right: m_mouseState &= ~MouseToolAction.MouseButton.Right; break;
                }

                m_mouseDownViewTransform = this.CurrentViewToControlMatrix;

                float px = args.X;
                float py = args.Y;
                var inv = this.m_mouseDownViewTransform.Clone () as Matrix;
                inv.Invert ();
                var ps = new PointF[]{new PointF(px, py)};
                inv.TransformPoints(ps);
                px = ps[0].X;
                py = ps[0].Y;

                if (this.m_currentMouseToolAction != null) {
                    if (this.m_currentMouseToolAction.MouseUp != null) {
                        this.m_currentMouseToolAction.MouseUp (px, py);
                    }
                }

                this.m_currentMouseToolAction = null;
            };
            m_control.MouseMove += delegate(object o, MouseEventArgs args) {
                if (ReferenceEquals (this.m_mouseDownViewTransform, null)) {return;}

                float px = args.X;
                float py = args.Y;
                var inv = this.m_mouseDownViewTransform.Clone () as Matrix;
                inv.Invert ();
                var ps = new PointF[]{new PointF(px, py)};
                inv.TransformPoints (ps);
                px = ps[0].X;
                py = ps[0].Y;
                if (this.m_currentMouseToolAction != null) {
                    if (this.m_currentMouseToolAction.MouseMove != null) {
                        this.m_currentMouseToolAction.MouseMove (px, py);
                    }
                }
            };
        }
Example #2
0
        public void Step1_SetControl(Widget control)
        {
            m_control = control;
            m_control.ExposeEvent += (o, args) => {
                using (var context = Gdk.CairoHelper.Create (args.Event.Window)) {
                    CairoFillModule.Fill(context, m_control, new Cairo.Color(1, 1, 1));
                    context.Antialias = Cairo.Antialias.Subpixel;
                    context.Save ();
                    context.Transform (this.CurrentViewTransform);
                    if (m_renderDelegate != null) {
                        m_renderDelegate (context);
                    }
                    context.Restore ();
                    CairoCanvasViewModule.Draw (context,
                                                this.CurrentControlBounds,
                                                this.CurrentView);
                }
            };

            m_control.ButtonPressEvent += delegate(object o, Gtk.ButtonPressEventArgs args) {
                // Change keyboard modifier state.
                var shift = (args.Event.State & ModifierType.ShiftMask) == ModifierType.ShiftMask;
                var ctrl = (args.Event.State & ModifierType.ControlMask) == ModifierType.ControlMask;
                if (shift) {
                    m_modifierState |= MouseToolAction.ModifierKey.Shift;
                }
                if (ctrl) {
                    m_modifierState |= MouseToolAction.ModifierKey.Control;
                }

                // Change mouse button state.
                var button = args.Event.Button;
                switch (button) {
                    case 1: m_mouseState |= MouseToolAction.MouseButton.Left; break;
                    case 2: m_mouseState |= MouseToolAction.MouseButton.Middle; break;
                    case 3: m_mouseState |= MouseToolAction.MouseButton.Right; break;
                }

                var viewTransform = this.CurrentViewTransform;
                m_mouseDownViewTransform = viewTransform;

                // Set mouse down locations.
                var px = args.Event.X;
                var py = args.Event.Y;
                this.m_mouseDownControlPosition = new Cairo.PointD (px, py);
                var inv = viewTransform.Clone () as Matrix;
                inv.Invert ();
                inv.TransformPoint (ref px, ref py);
                this.m_mouseDownViewPosition = new Cairo.PointD (px, py);

                // If pressing a mouse button, complete the previous mouse tool action.
                if (this.m_currentMouseToolAction != null) {
                    if (this.m_currentMouseToolAction.MouseUp != null) {
                        this.m_currentMouseToolAction.MouseUp (px, py);
                    }
                }

                // Find the mouse tool action that matches the current state.
                m_currentMouseToolAction = null;
                foreach (var mouseToolAction in m_mouseToolActions) {
                    if (mouseToolAction.Button == m_mouseState &&
                        mouseToolAction.Modifier == m_modifierState) {
                        this.m_currentMouseToolAction = mouseToolAction;
                        if (this.m_currentMouseToolAction.MouseDown != null) {
                            this.m_currentMouseToolAction.MouseDown (px, py);
                        }

                        return;
                    }
                }
            };
            m_control.ButtonReleaseEvent += delegate(object o, Gtk.ButtonReleaseEventArgs args) {
                // Change keyboard modifier state.
                var shift = (args.Event.State & ModifierType.ShiftMask) == ModifierType.ShiftMask;
                var ctrl = (args.Event.State & ModifierType.ControlMask) == ModifierType.ControlMask;
                if (shift) {
                    m_modifierState &= ~MouseToolAction.ModifierKey.Shift;
                }
                if (ctrl) {
                    m_modifierState &= ~MouseToolAction.ModifierKey.Control;
                }

                // Change mouse button state.
                var button = args.Event.Button;
                switch (button) {
                    case 1: m_mouseState &= ~MouseToolAction.MouseButton.Left; break;
                    case 2: m_mouseState &= ~MouseToolAction.MouseButton.Middle; break;
                    case 3: m_mouseState &= ~MouseToolAction.MouseButton.Right; break;
                }

                var px = args.Event.X;
                var py = args.Event.Y;
                var inv = this.m_mouseDownViewTransform.Clone () as Matrix;
                inv.Invert ();
                inv.TransformPoint (ref px, ref py);

                if (this.m_currentMouseToolAction != null) {
                    if (this.m_currentMouseToolAction.MouseUp != null) {
                        this.m_currentMouseToolAction.MouseUp (px, py);
                    }
                }

                this.m_currentMouseToolAction = null;
            };
            m_control.MotionNotifyEvent += delegate(object o, Gtk.MotionNotifyEventArgs args) {
                if (ReferenceEquals (this.m_mouseDownViewTransform, null)) {return;}

                var px = args.Event.X;
                var py = args.Event.Y;
                var inv = this.m_mouseDownViewTransform.Clone () as Matrix;
                inv.Invert ();
                inv.TransformPoint (ref px, ref py);
                if (this.m_currentMouseToolAction != null) {
                    if (this.m_currentMouseToolAction.MouseMove != null) {
                        this.m_currentMouseToolAction.MouseMove (px, py);
                    }
                }
            };

            m_control.AddEvents ((int)(
                EventMask.ButtonPressMask
                | EventMask.ButtonReleaseMask
                | EventMask.PointerMotionMask));
        }