aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/MaterialDesignInXamlToolkit-master/MaterialDesignThemes.Wpf/ClockItemButton.cs
blob: 7bc8f6aaf9d05c2897b326ec4e8fac9fc61f5ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace MaterialDesignThemes.Wpf
{	

	[TemplatePart(Name = ThumbPartName, Type = typeof(Thumb))]
	public class ClockItemButton : ToggleButton
	{
		public const string ThumbPartName = "PART_Thumb";
		
		public static readonly DependencyProperty CentreXProperty = DependencyProperty.Register(
            nameof(CentreX), typeof (double), typeof (ClockItemButton), new PropertyMetadata(default(double)));

		public double CentreX
		{
			get { return (double) GetValue(CentreXProperty); }
			set { SetValue(CentreXProperty, value); }
		}

		public static readonly DependencyProperty CentreYProperty = DependencyProperty.Register(
            nameof(CentreY), typeof (double), typeof (ClockItemButton), new PropertyMetadata(default(double)));

		public double CentreY
		{
			get { return (double) GetValue(CentreYProperty); }
			set { SetValue(CentreYProperty, value); }
		}

		private static readonly DependencyPropertyKey XPropertyKey =
			DependencyProperty.RegisterReadOnly(
				"X", typeof(double), typeof(ClockItemButton),
				new PropertyMetadata(default(double)));

		public static readonly DependencyProperty XProperty =
			XPropertyKey.DependencyProperty;

		public double X
		{
			get { return (double)GetValue(XProperty); }
			private set { SetValue(XPropertyKey, value); }
		}

		private static readonly DependencyPropertyKey YPropertyKey =
			DependencyProperty.RegisterReadOnly(
				"Y", typeof(double), typeof(ClockItemButton),
				new PropertyMetadata(default(double)));

		public static readonly DependencyProperty YProperty =
			YPropertyKey.DependencyProperty;

		private Thumb _thumb;

		public double Y
		{
			get { return (double)GetValue(YProperty); }
			private set { SetValue(YPropertyKey, value); }
		}

		public override void OnApplyTemplate()
		{
			if (_thumb != null)
			{
                _thumb.DragStarted -= ThumbOnDragStarted;
				_thumb.DragDelta -= ThumbOnDragDelta;
				_thumb.DragCompleted -= ThumbOnDragCompleted;
			}

			_thumb = GetTemplateChild(ThumbPartName) as Thumb;

			if (_thumb != null)
			{
                _thumb.DragStarted += ThumbOnDragStarted;
                _thumb.DragDelta += ThumbOnDragDelta;
				_thumb.DragCompleted += ThumbOnDragCompleted;
            }

			base.OnApplyTemplate();
		}

		public static readonly RoutedEvent DragDeltaEvent =
			EventManager.RegisterRoutedEvent(
				"DragDelta",
				RoutingStrategy.Bubble,
				typeof (DragDeltaEventHandler),
				typeof (ClockItemButton));

		private static void OnDragDelta(
			DependencyObject d, double horizontalChange, double verticalChange)
		{
			var instance = (ClockItemButton) d;
			var dragDeltaEventArgs = new DragDeltaEventArgs(horizontalChange, verticalChange)
			{
				RoutedEvent = DragDeltaEvent,
				Source = d
			};

			instance.RaiseEvent(dragDeltaEventArgs);			
		}

        public static readonly RoutedEvent DragStartedEvent =
            EventManager.RegisterRoutedEvent(
                "DragStarted",
                RoutingStrategy.Bubble,
                typeof(DragStartedEventHandler),
                typeof(ClockItemButton));

		public static readonly RoutedEvent DragCompletedEvent =
			EventManager.RegisterRoutedEvent(
				"DragCompleted",
				RoutingStrategy.Bubble,
				typeof (DragCompletedEventHandler),
				typeof (ClockItemButton));

        private static void OnDragStarted(DependencyObject d, double horizontalOffset, double verticalOffset)
        {
            var instance = (ClockItemButton)d;
            var dragStartedEventArgs = new DragStartedEventArgs(horizontalOffset, verticalOffset)
            {
                RoutedEvent = DragStartedEvent,
                Source = d
            };

            instance.RaiseEvent(dragStartedEventArgs);
        }

		private static void OnDragCompleted(DependencyObject d, double horizontalChange, double verticalChange, bool canceled)
		{
			var instance = (ClockItemButton)d;
			var dragCompletedEventArgs = new DragCompletedEventArgs(horizontalChange, verticalChange, canceled)
			{
				RoutedEvent = DragCompletedEvent,
				Source = d
			};

			instance.RaiseEvent(dragCompletedEventArgs);
		}		

		protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
		{
			if (_thumb == null) return;
				
			base.OnPreviewMouseLeftButtonDown(e);
			if (!IsChecked.HasValue || !IsChecked.Value)
			{
				OnToggle();				
			}
		}

		/// <summary> 
		/// This override method is called when the control is clicked by mouse or keyboard
		/// </summary> 
		protected override void OnClick()
		{
			if (_thumb == null)
				base.OnClick();			
		}

        private void ThumbOnDragStarted(object sender, DragStartedEventArgs dragStartedEventArgs)
        {
            //Get the absolute position of where the drag operation started
            OnDragStarted(this, CentreX + dragStartedEventArgs.HorizontalOffset - Width / 2.0, CentreY + dragStartedEventArgs.VerticalOffset - Height / 2.0);
        }

		private void ThumbOnDragDelta(object sender, DragDeltaEventArgs dragDeltaEventArgs)
		{
			OnDragDelta(this, dragDeltaEventArgs.HorizontalChange, dragDeltaEventArgs.VerticalChange);
		}

		private void ThumbOnDragCompleted(object sender, DragCompletedEventArgs dragCompletedEventArgs)
		{
			OnDragCompleted(this, dragCompletedEventArgs.HorizontalChange, dragCompletedEventArgs.VerticalChange, dragCompletedEventArgs.Canceled);
		}

		protected override Size ArrangeOverride(Size finalSize)
		{		
			Dispatcher.BeginInvoke(new Action(() =>
			{
				X = CentreX - finalSize.Width/2;
				Y = CentreY - finalSize.Height/2;
			}));

			return base.ArrangeOverride(finalSize);
		}
	}
}