DragValidator.cs :  » 2.6.4-mono-.net-core » System.Windows » System » Windows » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.Windows 
System.Windows » System » Windows » Controls » DragValidator.cs
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Diagnostics;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace System.Windows.Controls{
    /// <summary>
    /// Class to encapsulate drag behavior for a UIElement
    /// </summary>
    internal class DragValidator
    {
        private UIElement _targetElement;
        private Point _start;
        private bool _draggingActive;

        /// <summary>
        /// Occurs when a drag operation has started 
        /// </summary>
        public event EventHandler<DragStartedEventArgs> DragStartedEvent;
        
        /// <summary>
        /// Occurs when a drag operation has completed
        /// </summary>
        public event EventHandler<DragCompletedEventArgs> DragCompletedEvent;
        
        /// <summary>
        /// Occurs when a drag operation has progressed
        /// </summary>
        public event EventHandler<DragDeltaEventArgs> DragDeltaEvent;

        /// <summary>
        /// Create an instance of the DragValidator class
        /// </summary>
        /// <param name="targetElement">UIElement that represents the source of the drag operation</param>
        public DragValidator(UIElement targetElement)
        {
            Debug.Assert(targetElement != null);

            _targetElement = targetElement;

            _targetElement.MouseLeftButtonDown += new MouseButtonEventHandler(TargetElement_MouseLeftButtonDown);
            _targetElement.MouseLeftButtonUp += new MouseButtonEventHandler(TargetElement_MouseLeftButtonUp);
            _targetElement.MouseMove += new MouseEventHandler(TargetElement_MouseMove);
        }

        /// <summary>
        /// Handle the MouseMove event for the UIElement to update the drag operation
        /// </summary>
        private void TargetElement_MouseMove(object sender, MouseEventArgs e)
        {
            if (_draggingActive)
            {
                OnDragDelta(e);
            }
        }

        /// <summary>
        /// Handle the MouseLeftButtonUp event for the UIElement to complete the drag operation
        /// </summary>
        private void TargetElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _targetElement.ReleaseMouseCapture();
            _draggingActive = false;
            OnDragCompleted(e, false);
        }

        /// <summary>
        /// Handle the MouseLeftButtonDown event for the UIElement to start a drag operation
        /// </summary>
        private void TargetElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _start = e.GetPosition(_targetElement);
            if (_targetElement.RenderTransform != null)
            {
                _start = _targetElement.RenderTransform.Transform(_start);
            }

            _draggingActive = _targetElement.CaptureMouse();

            // Only start the drag operation if CaptureMouse succeeded
            if (_draggingActive)
            {
                OnDragStarted();
            }
        }

        /// <summary>
        /// Invoke the DragStartedEvent handlers
        /// </summary>
        private void OnDragStarted()
        {
            EventHandler<DragStartedEventArgs> handler = this.DragStartedEvent;
            if (handler != null)
            {
                handler(_targetElement, new DragStartedEventArgs(0.0, 0.0));
            }
        }

        /// <summary>
        /// Invoke the DragDeltaEvent handlers
        /// </summary>
        private void OnDragDelta(MouseEventArgs e)
        {
            EventHandler<DragDeltaEventArgs> handler = this.DragDeltaEvent;
            if (handler != null)
            {
                Point p = e.GetPosition(_targetElement);
                if (_targetElement.RenderTransform != null)
                {
                    p = _targetElement.RenderTransform.Transform(p);
                }
                handler(_targetElement, new DragDeltaEventArgs(p.X - _start.X, p.Y - _start.Y));
            }
        }
        
        /// <summary>
        /// Invoke the DragCompletedEvent handlers
        /// </summary>
        private void OnDragCompleted(MouseEventArgs e, bool canceled)
        {
            EventHandler<DragCompletedEventArgs> handler = this.DragCompletedEvent;
            if (handler != null)
            {
                Point endPoint;
                if (canceled)
                {
                    endPoint = new Point();
                }
                else
                {
                    Point p = e.GetPosition(_targetElement);
                    if (_targetElement.RenderTransform != null)
                    {
                        p = _targetElement.RenderTransform.Transform(p);
                    }
                    endPoint = new Point(p.X - _start.X, p.Y - _start.Y);
                }
                handler(_targetElement, new DragCompletedEventArgs(endPoint.X, endPoint.Y, canceled));
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.