~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/DragListener.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Linq;
 
7
using System.Text;
 
8
using System.Windows;
 
9
using System.Windows.Input;
 
10
using System.Diagnostics;
 
11
 
 
12
namespace ICSharpCode.WpfDesign.Designer.Controls
 
13
{
 
14
        public delegate void DragHandler(DragListener drag);
 
15
 
 
16
        public class DragListener
 
17
        {
 
18
                static DragListener()
 
19
                {
 
20
                        InputManager.Current.PostProcessInput += new ProcessInputEventHandler(PostProcessInput);
 
21
                }
 
22
 
 
23
                public DragListener(IInputElement target)
 
24
                {
 
25
                        Target = target;
 
26
                        
 
27
                        Target.PreviewMouseLeftButtonDown += Target_MouseDown;
 
28
                        Target.PreviewMouseMove += Target_MouseMove;
 
29
                        Target.PreviewMouseLeftButtonUp += Target_MouseUp;
 
30
                }
 
31
 
 
32
                static DragListener CurrentListener;
 
33
 
 
34
                static void PostProcessInput(object sender, ProcessInputEventArgs e)
 
35
                {
 
36
                        if (CurrentListener != null) {
 
37
                                var a = e.StagingItem.Input as KeyEventArgs;
 
38
                                if (a != null && a.Key == Key.Escape) {
 
39
                                        Mouse.Capture(null);
 
40
                                        CurrentListener.IsDown = false;
 
41
                                        CurrentListener.IsCanceled = true;
 
42
                                        CurrentListener.Complete();
 
43
                                }
 
44
                        }
 
45
                }
 
46
 
 
47
                void Target_MouseDown(object sender, MouseButtonEventArgs e)
 
48
                {
 
49
                        StartPoint = Mouse.GetPosition(null);
 
50
                        CurrentPoint = StartPoint;
 
51
                        DeltaDelta = new Vector();
 
52
                        IsDown = true;
 
53
                        IsCanceled = false;
 
54
                }
 
55
 
 
56
                void Target_MouseMove(object sender, MouseEventArgs e)
 
57
                {
 
58
                        if (IsDown) {
 
59
                                DeltaDelta = e.GetPosition(null) - CurrentPoint;
 
60
                                CurrentPoint += DeltaDelta;
 
61
 
 
62
                                if (!IsActive) {
 
63
                                        if (Math.Abs(Delta.X) >= SystemParameters.MinimumHorizontalDragDistance ||
 
64
                                            Math.Abs(Delta.Y) >= SystemParameters.MinimumVerticalDragDistance) {
 
65
                                                IsActive = true;
 
66
                                                CurrentListener = this;
 
67
 
 
68
                                                if (Started != null) {
 
69
                                                        Started(this);
 
70
                                                }
 
71
                                        }
 
72
                                }
 
73
 
 
74
                                if (IsActive && Changed != null) {
 
75
                                        Changed(this);
 
76
                                }
 
77
                        }
 
78
                }
 
79
 
 
80
                void Target_MouseUp(object sender, MouseButtonEventArgs e)
 
81
                {
 
82
                        IsDown = false;
 
83
                        if (IsActive) {
 
84
                                Complete();
 
85
                        }
 
86
                }
 
87
 
 
88
                void Complete()
 
89
                {
 
90
                        IsActive = false;
 
91
                        CurrentListener = null;
 
92
 
 
93
                        if (Completed != null) {
 
94
                                Completed(this);
 
95
                        }
 
96
                }
 
97
 
 
98
                public event DragHandler Started;
 
99
                public event DragHandler Changed;
 
100
                public event DragHandler Completed;
 
101
 
 
102
                public IInputElement Target { get; private set; }
 
103
                public Point StartPoint { get; private set; }
 
104
                public Point CurrentPoint { get; private set; }
 
105
                public Vector DeltaDelta { get; private set; }
 
106
                public bool IsActive { get; private set; }
 
107
                public bool IsDown { get; private set; }
 
108
                public bool IsCanceled { get; private set; }
 
109
                
 
110
                public Vector Delta {
 
111
                        get { return CurrentPoint - StartPoint; }
 
112
                }
 
113
        }
 
114
}