SelectionEditingBehavior.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Framework / MS / Internal / Ink / SelectionEditingBehavior.cs / 1 / SelectionEditingBehavior.cs

                            //---------------------------------------------------------------------------- 
//
// File: ResizeEditingBehavior .cs
//
// Description: 
//      ResizeEditingBehavior
// 
// Authors: [....] 
//
// Copyright (C) 2003 by Microsoft Corporation.  All rights reserved. 
//
//---------------------------------------------------------------------------
using System;
using System.Diagnostics; 
using System.ComponentModel;
using System.ComponentModel.Design; 
using System.Collections; 
using System.Collections.Generic;
using System.Windows.Input; 
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data; 
using System.Windows.Documents;
using System.Windows.Interop; 
using System.Windows.Navigation; 
using System.Windows.Media;
using System.Windows.Ink; 

namespace MS.Internal.Ink
{
    ///  
    /// SelectionEditingBehavior
    ///  
    internal sealed class SelectionEditingBehavior : EditingBehavior 
    {
 
        //-------------------------------------------------------------------------------
        //
        // Constructors
        // 
        //-------------------------------------------------------------------------------
 
        #region Constructors 

        ///  
        /// SelectionEditingBehavior constructor
        /// 
        internal SelectionEditingBehavior(EditingCoordinator editingCoordinator, InkCanvas inkCanvas)
            : base(editingCoordinator, inkCanvas) 
        {
        } 
 
        #endregion Constructors
 
        //--------------------------------------------------------------------------------
        //
        // Protected Methods
        // 
        //-------------------------------------------------------------------------------
 
        #region Protected Methods 

        ///  
        ///     Attaching to the element, we get attached in StylusDown
        /// 
        protected override void OnActivate()
        { 
            _actionStarted = false;
 
            // Capture the mouse. 
            InitializeCapture();
 
            // Hittest for the grab handle
            MouseDevice mouse = Mouse.PrimaryDevice;

            _hitResult = InkCanvas.SelectionAdorner.SelectionHandleHitTest( 
                    mouse.GetPosition((IInputElement)(InkCanvas.SelectionAdorner)));
 
            Debug.Assert(_hitResult != InkCanvasSelectionHitResult.None); 
            EditingCoordinator.InvalidateBehaviorCursor(this);
 
            // Get the current selection bounds.
            _selectionRect = InkCanvas.GetSelectionBounds( );

            // Set the initial tracking position and rectangle 
            _previousLocation = mouse.GetPosition(InkCanvas.SelectionAdorner);
            _previousRect = _selectionRect; 
 
            // Start the feedback rubber band.
            InkCanvas.InkCanvasSelection.StartFeedbackAdorner(_selectionRect, _hitResult); 

            // Add handlers to the mouse events.
            InkCanvas.SelectionAdorner.AddHandler(Mouse.MouseUpEvent, new MouseButtonEventHandler(OnMouseUp));
            InkCanvas.SelectionAdorner.AddHandler(Mouse.MouseMoveEvent, new MouseEventHandler(OnMouseMove)); 
            InkCanvas.SelectionAdorner.AddHandler(UIElement.LostMouseCaptureEvent,
                new MouseEventHandler(OnLostMouseCapture)); 
 
        }
 
        /// 
        /// Called when the ResizeEditingBehavior is detached
        /// 
        protected override void OnDeactivate() 
        {
            // Remove the mouse event handlers. 
            InkCanvas.SelectionAdorner.RemoveHandler(Mouse.MouseUpEvent, new MouseButtonEventHandler(OnMouseUp)); 
            InkCanvas.SelectionAdorner.RemoveHandler(Mouse.MouseMoveEvent, new MouseEventHandler(OnMouseMove));
            InkCanvas.SelectionAdorner.RemoveHandler(UIElement.LostMouseCaptureEvent, 
                new MouseEventHandler(OnLostMouseCapture));
        }

        protected override void OnCommit(bool commit) 
        {
            // Release the inputs. 
            ReleaseCapture(true, commit); 
        }
 
        protected override Cursor GetCurrentCursor()
        {
            return PenCursorManager.GetSelectionCursor(_hitResult,
                (this.InkCanvas.FlowDirection == FlowDirection.RightToLeft)); 
        }
 
        #endregion Protected Methods 

        //-------------------------------------------------------------------------------- 
        //
        // Private Methods
        //
        //-------------------------------------------------------------------------------- 

        #region Private Methods 
 
        /// 
        /// The handler of the MouseMove event 
        /// 
        /// 
        /// 
        private void OnMouseMove(object sender, MouseEventArgs args) 
        {
            // Get the current mouse location. 
            Point curPoint = args.GetPosition(InkCanvas.SelectionAdorner); 

            // Check if we have a mouse movement at all. 
            if ( !DoubleUtil.AreClose(curPoint.X, _previousLocation.X)
                || !DoubleUtil.AreClose(curPoint.Y, _previousLocation.Y) )
            {
                // We won't start the move until we really see a movement. 
                if ( !_actionStarted )
                { 
                    _actionStarted = true; 
                }
 
                // Get the new rectangle
                Rect newRect = ChangeFeedbackRectangle(curPoint);

                // Update the feedback rubber band and record the current tracking rectangle. 
                InkCanvas.InkCanvasSelection.UpdateFeedbackAdorner(newRect);
                _previousRect = newRect; 
            } 
        }
 
        /// 
        /// The handler of the MouseUp event
        /// 
        ///  
        /// 
        private void OnMouseUp(object sender, MouseButtonEventArgs args) 
        { 
            // We won't start the move until we really see a movement.
            if ( _actionStarted ) 
            {
                _previousRect = ChangeFeedbackRectangle(args.GetPosition(InkCanvas.SelectionAdorner));
            }
 
            Commit(true);
        } 
 
        /// 
        /// InkCanvas.LostStylusCapture handler 
        /// 
        /// 
        /// 
        private void OnLostMouseCapture(object sender, MouseEventArgs args) 
        {
            // If user is editing, we have to commit the current operation and reset our state. 
            if ( EditingCoordinator.UserIsEditing ) 
            {
                ReleaseCapture(false, true); 
            }
        }

 
        /// 
        /// Get the new feedback rectangle based on the location 
        ///  
        /// 
        private Rect ChangeFeedbackRectangle(Point newPoint) 
        {
            // First check to make sure that we are not going past the zero point on
            //  the original element in which we hit the resize handle.
 
            // If moving the left side -- don't go past the right side.
            if ( _hitResult == InkCanvasSelectionHitResult.TopLeft || 
                _hitResult == InkCanvasSelectionHitResult.BottomLeft || 
                _hitResult == InkCanvasSelectionHitResult.Left )
            { 
                if ( newPoint.X > _selectionRect.Right - MinimumHeightWidthSize )
                {
                    newPoint.X = _selectionRect.Right - MinimumHeightWidthSize;
                } 
            }
 
            // If moving the right side -- don't go past the left side. 
            if ( _hitResult == InkCanvasSelectionHitResult.TopRight ||
                _hitResult == InkCanvasSelectionHitResult.BottomRight || 
                _hitResult == InkCanvasSelectionHitResult.Right )
            {
                if ( newPoint.X < _selectionRect.Left + MinimumHeightWidthSize )
                { 
                    newPoint.X = _selectionRect.Left + MinimumHeightWidthSize;
                } 
            } 

            // If moving the top side -- don't go past the bottom side. 
            if ( _hitResult == InkCanvasSelectionHitResult.TopLeft ||
                _hitResult == InkCanvasSelectionHitResult.TopRight ||
                _hitResult == InkCanvasSelectionHitResult.Top )
            { 
                if ( newPoint.Y > _selectionRect.Bottom - MinimumHeightWidthSize )
                { 
                    newPoint.Y = _selectionRect.Bottom - MinimumHeightWidthSize; 
                }
            } 

            // If moving the bottom side -- don't go past the top side.
            if ( _hitResult == InkCanvasSelectionHitResult.BottomLeft ||
                _hitResult == InkCanvasSelectionHitResult.BottomRight || 
                _hitResult == InkCanvasSelectionHitResult.Bottom )
            { 
                if ( newPoint.Y < _selectionRect.Top + MinimumHeightWidthSize ) 
                {
                    newPoint.Y = _selectionRect.Top + MinimumHeightWidthSize; 
                }
            }

            // Get the new boundary of the selection 
            Rect newRect = CalculateRect(newPoint.X - _previousLocation.X, newPoint.Y - _previousLocation.Y);
 
            // Depends on the current grab handle, we record the tracking position accordingly. 
            if ( _hitResult == InkCanvasSelectionHitResult.BottomRight ||
                _hitResult == InkCanvasSelectionHitResult.BottomLeft || 
                _hitResult == InkCanvasSelectionHitResult.TopRight ||
                _hitResult == InkCanvasSelectionHitResult.TopLeft ||
                _hitResult == InkCanvasSelectionHitResult.Selection )
            { 
                _previousLocation.X = newPoint.X;
                _previousLocation.Y = newPoint.Y; 
            } 
            else if ( _hitResult == InkCanvasSelectionHitResult.Left ||
                _hitResult == InkCanvasSelectionHitResult.Right ) 
            {
                _previousLocation.X = newPoint.X;
            }
            else if ( _hitResult == InkCanvasSelectionHitResult.Top || 
                _hitResult == InkCanvasSelectionHitResult.Bottom )
            { 
                _previousLocation.Y = newPoint.Y; 
            }
 
            return newRect;

        }
 
        /// 
        ///     Resize a given element based on the grab handle 
        ///  
        /// 
        ///  
        private Rect CalculateRect(double x, double y)
        {
            Rect newRect = _previousRect;
 
            switch ( _hitResult )
            { 
                case InkCanvasSelectionHitResult.BottomRight: 
                    {
                        newRect = ExtendSelectionRight(newRect, x); 
                        newRect = ExtendSelectionBottom(newRect, y);
                        break;
                    }
                case InkCanvasSelectionHitResult.Bottom: 
                    {
                        newRect = ExtendSelectionBottom(newRect, y); 
                        break; 
                    }
                case InkCanvasSelectionHitResult.BottomLeft: 
                    {
                        newRect = ExtendSelectionLeft(newRect, x);
                        newRect = ExtendSelectionBottom(newRect, y);
                        break; 
                    }
                case InkCanvasSelectionHitResult.TopRight: 
                    { 
                        newRect = ExtendSelectionTop(newRect, y);
                        newRect = ExtendSelectionRight(newRect, x); 
                        break;
                    }
                case InkCanvasSelectionHitResult.Top:
                    { 
                        newRect = ExtendSelectionTop(newRect, y);
                        break; 
                    } 
                case InkCanvasSelectionHitResult.TopLeft:
                    { 
                        newRect = ExtendSelectionTop(newRect, y);
                        newRect = ExtendSelectionLeft(newRect, x);
                        break;
                    } 
                case InkCanvasSelectionHitResult.Left:
                    { 
                        newRect = ExtendSelectionLeft(newRect, x); 
                        break;
                    } 
                case InkCanvasSelectionHitResult.Right:
                    {
                        newRect = ExtendSelectionRight(newRect, x);
                        break; 
                    }
                case InkCanvasSelectionHitResult.Selection: 
                    { 
                        // Translate the rectangle
                        newRect.Offset(x, y); 
                        break;
                    }
                default:
                    { 
                        Debug.Assert(false);
                        break; 
                    } 
            }
 
            return newRect;
        }

        // ExtendSelectionLeft 
        private static Rect ExtendSelectionLeft(Rect rect, double extendBy)
        { 
            Rect newRect = rect; 
            newRect.X += extendBy;
            newRect.Width -= extendBy; 

            return newRect;
        }
 
        // ExtendSelectionTop
        private static Rect ExtendSelectionTop(Rect rect, double extendBy) 
        { 
            Rect newRect = rect;
 
            newRect.Y += extendBy;
            newRect.Height -= extendBy;

            return newRect; 
        }
 
        // ExtendSelectionRight 
        private static Rect ExtendSelectionRight(Rect rect, double extendBy)
        { 
            Rect newRect = rect;

            newRect.Width += extendBy;
 
            return newRect;
        } 
 
        // ExtendSelectionBottom
        private static Rect ExtendSelectionBottom(Rect rect, double extendBy) 
        {
            Rect newRect = rect;

            newRect.Height += extendBy; 

            return newRect; 
        } 

        ///  
        /// Capture Mouse
        /// 
        private void InitializeCapture()
        { 
            Debug.Assert(EditingCoordinator.UserIsEditing == false, "Unexpect UserIsEditng state." );
            EditingCoordinator.UserIsEditing = true; 
            InkCanvas.SelectionAdorner.CaptureMouse(); 
        }
 
        /// 
        /// Release Mouse capture
        /// 
        ///  
        /// 
        private void ReleaseCapture(bool releaseDevice, bool commit) 
        { 
            if ( EditingCoordinator.UserIsEditing )
            { 
                EditingCoordinator.UserIsEditing = false;
                if ( releaseDevice )
                {
                    InkCanvas.SelectionAdorner.ReleaseMouseCapture(); 
                }
 
                SelfDeactivate(); 

                // End the rubber band. If the operation is committed, we set the selection to the tracking rectangle. 
                // Otherwise, reset the selection to the original bounds.
                InkCanvas.InkCanvasSelection.EndFeedbackAdorner(commit ? _previousRect : _selectionRect);
            }
        } 

        #endregion Private Methods 
 
        //-------------------------------------------------------------------------------
        // 
        // Private Fields
        //
        //--------------------------------------------------------------------------------
 
        #region Private Fields
 
        private const double MinimumHeightWidthSize = 16.0; 
        private Point _previousLocation;
        private Rect _previousRect; 
        private Rect _selectionRect;
        private InkCanvasSelectionHitResult _hitResult;
        private bool _actionStarted = false;
 
        #endregion Private Fields
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK