Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/Windows/Controls/DesktopElement.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 /* | |
2 The MIT License | |
3 | |
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/ | |
5 | |
6 Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 of this software and associated documentation files (the "Software"), to deal | |
8 in the Software without restriction, including without limitation the rights | |
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 copies of the Software, and to permit persons to whom the Software is | |
11 furnished to do so, subject to the following conditions: | |
12 | |
13 The above copyright notice and this permission notice shall be included in | |
14 all copies or substantial portions of the Software. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 THE SOFTWARE. | |
23 */ | |
24 | |
25 using System; | |
26 using System.Windows; | |
27 using System.Windows.Controls; | |
28 using System.Windows.Input; | |
29 using Chronos.Extensions.Windows; | |
30 using Chronos.Presentation.Core.Windows; | |
31 | |
32 namespace Chronos.Presentation.Windows.Controls | |
33 { | |
34 /// <summary> | |
35 /// Base class for <see cref="Desktop"/> elements (shortcuts, widgets,...) | |
36 /// </summary> | |
37 [TemplatePart(Name = DesktopElement.PART_Dragger, Type = typeof(FrameworkElement))] | |
38 public abstract class DesktopElement | |
39 : ContentControl, IDesktopElement, IActiveAware | |
40 { | |
41 #region · Constants · | |
42 | |
43 protected const string PART_Dragger = "PART_Dragger"; | |
44 protected const int ResizeSideThichness = 5; | |
45 protected const int ResizeCornerSize = 5; | |
46 | |
47 #endregion | |
48 | |
49 #region · Dependency Properties · | |
50 | |
51 /// <summary> | |
52 /// Identifies the Id dependency property. | |
53 /// </summary> | |
54 public static readonly DependencyProperty IdProperty = | |
55 DependencyProperty.Register("Id", typeof(Guid), typeof(DesktopElement), | |
56 new FrameworkPropertyMetadata(Guid.NewGuid())); | |
57 | |
58 /// <summary> | |
59 /// Identifies the WindowStartupLocation dependency property. | |
60 /// </summary> | |
61 public static readonly DependencyProperty StartupLocationProperty = | |
62 DependencyProperty.Register("StartupLocation", typeof(StartupPosition), typeof(WindowElement), | |
63 new FrameworkPropertyMetadata(StartupPosition.CenterParent, FrameworkPropertyMetadataOptions.AffectsArrange)); | |
64 | |
65 /// <summary> | |
66 /// Identifies the CanResize dependency property. | |
67 /// </summary> | |
68 public static readonly DependencyProperty CanResizeProperty = | |
69 DependencyProperty.Register("CanResize", typeof(bool), typeof(DesktopElement), | |
70 new FrameworkPropertyMetadata(true)); | |
71 | |
72 /// <summary> | |
73 /// Identifies the CanDrag dependency property. | |
74 /// </summary> | |
75 public static readonly DependencyProperty CanDragProperty = | |
76 DependencyProperty.Register("CanDrag", typeof(bool), typeof(DesktopElement), | |
77 new FrameworkPropertyMetadata(true)); | |
78 | |
79 /// <summary> | |
80 /// Identifies the CanClose dependency property. | |
81 /// </summary> | |
82 public static readonly DependencyProperty CanCloseProperty = | |
83 DependencyProperty.Register("CanClose", typeof(bool), typeof(DesktopElement), | |
84 new FrameworkPropertyMetadata(true)); | |
85 | |
86 /// <summary> | |
87 /// Identifies the ConstraintToParent dependency property. | |
88 /// </summary> | |
89 public static readonly DependencyProperty ConstraintToParentProperty = | |
90 DependencyProperty.Register("ConstraintToParent", typeof(bool), typeof(DesktopElement), | |
91 new FrameworkPropertyMetadata(false)); | |
92 | |
93 /// <summary> | |
94 /// Identifies the IsActive dependency property | |
95 /// </summary> | |
96 public static readonly DependencyProperty IsActiveProperty = | |
97 DependencyProperty.Register("IsActive", | |
98 typeof(bool), | |
99 typeof(DesktopElement), | |
100 new FrameworkPropertyMetadata(false)); | |
101 | |
102 #endregion | |
103 | |
104 #region · Static Constructors · | |
105 | |
106 /// <summary> | |
107 /// Initializes the <see cref="DesktopElement"/> class. | |
108 /// </summary> | |
109 static DesktopElement() | |
110 { | |
111 // set the key to reference the style for this control | |
112 FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata( | |
113 typeof(DesktopElement), new FrameworkPropertyMetadata(typeof(DesktopElement))); | |
114 } | |
115 | |
116 #endregion | |
117 | |
118 #region · Static Routed Events · | |
119 | |
120 /// <summary> | |
121 /// Occurs when a desktop element is activated | |
122 /// </summary> | |
123 public static readonly RoutedEvent ActivatedEvent = EventManager. | |
124 RegisterRoutedEvent("Activated", RoutingStrategy.Bubble, | |
125 typeof(RoutedEventHandler), typeof(DesktopElement)); | |
126 | |
127 /// <summary> | |
128 /// Occurs when a desktop element is deactivated | |
129 /// </summary> | |
130 public static readonly RoutedEvent DeactivatedEvent = EventManager. | |
131 RegisterRoutedEvent("Deactivated", RoutingStrategy.Bubble, | |
132 typeof(RoutedEventHandler), typeof(DesktopElement)); | |
133 | |
134 #endregion | |
135 | |
136 #region · Events · | |
137 | |
138 /// <summary> | |
139 /// Occurs when the element becomes activated | |
140 /// </summary> | |
141 public event RoutedEventHandler Activated | |
142 { | |
143 add { base.AddHandler(DesktopElement.ActivatedEvent, value); } | |
144 remove { base.RemoveHandler(DesktopElement.ActivatedEvent, value); } | |
145 } | |
146 | |
147 /// <summary> | |
148 /// Occurs when the element becomes deactivated | |
149 /// </summary> | |
150 public event RoutedEventHandler Deactivated | |
151 { | |
152 add { base.AddHandler(DesktopElement.DeactivatedEvent, value); } | |
153 remove { base.RemoveHandler(DesktopElement.DeactivatedEvent, value); } | |
154 } | |
155 | |
156 #endregion | |
157 | |
158 #region · Fields · | |
159 | |
160 private Panel parent; | |
161 private FrameworkElement partDragger; | |
162 private DragOrResizeStatus previewDragOrResizeStatus; | |
163 private DragOrResizeStatus dragOrResizeStatus; | |
164 private Point startMousePosition; | |
165 private Point previousMousePosition; | |
166 private Point oldPosition; | |
167 private Size originalSize; | |
168 private Size previousSize; | |
169 private bool isInitialized; | |
170 private bool oldCanResize; | |
171 | |
172 #endregion | |
173 | |
174 #region · Properties · | |
175 | |
176 /// <summary> | |
177 /// Gets or sets the element id. | |
178 /// </summary> | |
179 /// <value>The id.</value> | |
180 public Guid Id | |
181 { | |
182 get { return (Guid)base.GetValue(IdProperty); } | |
183 set { base.SetValue(IdProperty, value); } | |
184 } | |
185 | |
186 /// <summary> | |
187 /// Gets the window parent control | |
188 /// </summary> | |
189 public new Panel Parent | |
190 { | |
191 get { return this.parent; } | |
192 set { this.parent = value; } | |
193 } | |
194 | |
195 /// <summary> | |
196 /// Gets or sets the position of the window when first shown. | |
197 /// </summary> | |
198 public StartupPosition StartupLocation | |
199 { | |
200 get { return (StartupPosition)base.GetValue(StartupLocationProperty); } | |
201 set { base.SetValue(StartupLocationProperty, value); } | |
202 } | |
203 | |
204 /// <summary> | |
205 /// Gets or sets a value indicating whether the element can be resized | |
206 /// </summary> | |
207 public bool CanResize | |
208 { | |
209 get { return (bool)base.GetValue(CanResizeProperty); } | |
210 set { base.SetValue(CanResizeProperty, value); } | |
211 } | |
212 | |
213 /// <summary> | |
214 /// Gets a value indicating whether the element can be dragged. | |
215 /// </summary> | |
216 /// <value><c>true</c> if this instance can drag; otherwise, <c>false</c>.</value> | |
217 public virtual bool CanDrag | |
218 { | |
219 get { return (bool)base.GetValue(CanDragProperty); } | |
220 set { base.SetValue(CanDragProperty, value); } | |
221 } | |
222 | |
223 /// <summary> | |
224 /// Gets a value indicating whether the element can be closed. | |
225 /// </summary> | |
226 /// <value><c>true</c> if this instance can be closed; otherwise, <c>false</c>.</value> | |
227 public virtual bool CanClose | |
228 { | |
229 get { return (bool)base.GetValue(CanCloseProperty); } | |
230 set { base.SetValue(CanCloseProperty, value); } | |
231 } | |
232 | |
233 /// <summary> | |
234 /// Gets a value indicating whether the element should be constrained to parent. | |
235 /// </summary> | |
236 /// <value><c>true</c> if this instance can drag; otherwise, <c>false</c>.</value> | |
237 public virtual bool ConstraintToParent | |
238 { | |
239 get { return (bool)base.GetValue(ConstraintToParentProperty); } | |
240 set { base.SetValue(ConstraintToParentProperty, value); } | |
241 } | |
242 | |
243 /// <summary> | |
244 /// Gets or sets a value indicating whether the element is active. | |
245 /// </summary> | |
246 /// <value> | |
247 /// <c>true</c> if this instance is active; otherwise, <c>false</c>. | |
248 /// </value> | |
249 public virtual bool IsActive | |
250 { | |
251 get { return (bool)base.GetValue(IsActiveProperty); } | |
252 } | |
253 | |
254 #endregion | |
255 | |
256 #region · Protected Properties · | |
257 | |
258 /// <summary> | |
259 /// Gets the current drag or resize status | |
260 /// </summary> | |
261 protected DragOrResizeStatus DragOrResizeStatus | |
262 { | |
263 get { return this.dragOrResizeStatus; } | |
264 } | |
265 | |
266 /// <summary> | |
267 /// Gets or sets the old element position. | |
268 /// </summary> | |
269 protected Point OldPosition | |
270 { | |
271 get { return this.oldPosition; } | |
272 set { this.oldPosition = value; } | |
273 } | |
274 | |
275 /// <summary> | |
276 /// Gets or sets the original element size | |
277 /// </summary> | |
278 protected Size OriginalSize | |
279 { | |
280 get { return this.originalSize; } | |
281 set { this.originalSize = value; } | |
282 } | |
283 | |
284 /// <summary> | |
285 /// Gets the parent <see cref="Desktop"/>. | |
286 /// </summary> | |
287 /// <value>The parent desktop.</value> | |
288 protected Desktop ParentDesktop | |
289 { | |
290 //get { return this.GetParent<Desktop>(); } | |
291 get { return this.parent as Desktop; } | |
292 } | |
293 | |
294 protected FrameworkElement PartDragger | |
295 { | |
296 get { return this.partDragger; } | |
297 set { this.partDragger = value; } | |
298 } | |
299 | |
300 #endregion | |
301 | |
302 #region · Constructors · | |
303 | |
304 /// <summary> | |
305 /// Initializes a new instance of the <see cref="DesktopElement"/> class. | |
306 /// </summary> | |
307 protected DesktopElement() | |
308 { | |
309 this.previewDragOrResizeStatus = DragOrResizeStatus.None; | |
310 this.dragOrResizeStatus = DragOrResizeStatus.None; | |
311 this.startMousePosition = new Point(); | |
312 this.oldCanResize = this.CanResize; | |
313 } | |
314 | |
315 #endregion | |
316 | |
317 #region · Methods · | |
318 | |
319 public override void OnApplyTemplate() | |
320 { | |
321 base.OnApplyTemplate(); | |
322 | |
323 this.PartDragger = this.GetTemplateChild(DesktopElement.PART_Dragger) as FrameworkElement; | |
324 } | |
325 | |
326 /// <summary> | |
327 /// Attempts to bring the element to the foreground and activates it. | |
328 /// </summary> | |
329 public void Activate() | |
330 { | |
331 this.OnActivated(); | |
332 } | |
333 | |
334 /// <summary> | |
335 /// Deactivates the element | |
336 /// </summary> | |
337 public void Deactivate() | |
338 { | |
339 this.OnDeactivated(); | |
340 } | |
341 | |
342 /// <summary> | |
343 /// Closes the desktop element | |
344 /// </summary> | |
345 public virtual void Close() | |
346 { | |
347 if (this.Parent != null) | |
348 { | |
349 this.Parent.Children.Remove(this); | |
350 } | |
351 | |
352 // Clean up | |
353 this.Id = Guid.Empty; | |
354 this.previewDragOrResizeStatus = DragOrResizeStatus.None; | |
355 this.dragOrResizeStatus = DragOrResizeStatus.None; | |
356 this.startMousePosition = new Point(); | |
357 this.previousMousePosition = new Point(); | |
358 this.oldPosition = new Point(); | |
359 this.previousSize = Size.Empty; | |
360 this.originalSize = Size.Empty; | |
361 this.isInitialized = false; | |
362 this.oldCanResize = false; | |
363 this.partDragger = null; | |
364 this.parent = null; | |
365 this.Content = null; | |
366 this.DataContext = null; | |
367 } | |
368 | |
369 #endregion | |
370 | |
371 #region · Protected Methods · | |
372 | |
373 /// <summary> | |
374 /// Raises the <see cref="E:System.Windows.FrameworkElement.SizeChanged"/> event, using the specified information as part of the eventual event data. | |
375 /// </summary> | |
376 /// <param name="sizeInfo">Details of the old and new size involved in the change.</param> | |
377 protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) | |
378 { | |
379 base.OnRenderSizeChanged(sizeInfo); | |
380 | |
381 if (!this.isInitialized) | |
382 { | |
383 this.RefreshCalculatedVisualProperties(); | |
384 | |
385 this.OriginalSize = new Size(this.ActualWidth, this.ActualHeight); | |
386 this.isInitialized = true; | |
387 } | |
388 } | |
389 | |
390 protected void RefreshCalculatedVisualProperties() | |
391 { | |
392 if (!DesignMode.IsInDesignMode) | |
393 { | |
394 switch (this.StartupLocation) | |
395 { | |
396 case StartupPosition.CenterParent: | |
397 if (!this.ConstraintToParent && | |
398 this.GetParent<Window>() != null) | |
399 { | |
400 this.MoveElement | |
401 ( | |
402 (this.GetParent<Window>().ActualWidth - this.ActualWidth) / 2, | |
403 (this.GetParent<Window>().ActualHeight - this.ActualHeight) / 2 - (this.GetParent<Window>().ActualHeight - this.Parent.ActualHeight) | |
404 ); | |
405 } | |
406 else if (this.Parent != null) | |
407 { | |
408 this.MoveElement | |
409 ( | |
410 (this.Parent.ActualWidth - this.ActualWidth) / 2, | |
411 (this.Parent.ActualHeight - this.ActualHeight) / 2 | |
412 ); | |
413 } | |
414 break; | |
415 | |
416 case StartupPosition.Manual: | |
417 break; | |
418 | |
419 case StartupPosition.WindowsDefaultLocation: | |
420 this.MoveElement(5, 5); | |
421 break; | |
422 } | |
423 } | |
424 } | |
425 | |
426 /// <summary> | |
427 /// Raises the <see cref="DesktopElement.Activated"/> event. | |
428 /// </summary> | |
429 protected virtual void OnActivated() | |
430 { | |
431 if (!(bool)this.GetValue(DesktopElement.IsActiveProperty)) | |
432 { | |
433 if (this.ParentDesktop != null) | |
434 { | |
435 this.ParentDesktop.OnActivatedElement(this.Id); | |
436 this.ParentDesktop.BringToFront(this); | |
437 } | |
438 | |
439 this.GiveFocus(); | |
440 | |
441 base.SetValue(DesktopElement.IsActiveProperty, true); | |
442 | |
443 RoutedEventArgs e = new RoutedEventArgs(DesktopElement.ActivatedEvent, this); | |
444 base.RaiseEvent(e); | |
445 } | |
446 } | |
447 | |
448 /// <summary> | |
449 /// Raises the <see cref="DesktopElement.Deactivated"/> event. | |
450 /// </summary> | |
451 protected virtual void OnDeactivated() | |
452 { | |
453 if (this.IsActive) | |
454 { | |
455 base.SetValue(DesktopElement.IsActiveProperty, false); | |
456 | |
457 RoutedEventArgs e = new RoutedEventArgs(DesktopElement.DeactivatedEvent, this); | |
458 base.RaiseEvent(e); | |
459 } | |
460 } | |
461 | |
462 /// <summary> | |
463 /// Focuses the element | |
464 /// </summary> | |
465 protected virtual void GiveFocus() | |
466 { | |
467 this.SetFocus(); | |
468 } | |
469 | |
470 #endregion | |
471 | |
472 #region · Protected Element Move/Resize Methods · | |
473 | |
474 protected virtual void MoveElement(double x, double y) | |
475 { | |
476 this.Move(x, y); | |
477 } | |
478 | |
479 protected virtual void MoveElementLeft(double value) | |
480 { | |
481 this.MoveLeft(value); | |
482 } | |
483 | |
484 protected virtual void MoveElementTop(double value) | |
485 { | |
486 this.MoveTop(value); | |
487 } | |
488 | |
489 protected virtual void AdjustBounds(Point mousePosition) | |
490 { | |
491 Point position = this.GetPosition(); | |
492 | |
493 if (this.Parent != null) | |
494 { | |
495 Vector changeFromStart = Point.Subtract(mousePosition, this.startMousePosition); | |
496 | |
497 if (this.dragOrResizeStatus == DragOrResizeStatus.Drag) | |
498 { | |
499 if (this.CanDrag) | |
500 { | |
501 double x = position.X + changeFromStart.X; | |
502 double y = position.Y + changeFromStart.Y; | |
503 | |
504 if (this.ConstraintToParent) | |
505 { | |
506 if (x < 0) | |
507 { | |
508 x = 0; | |
509 } | |
510 if (y < 0) | |
511 { | |
512 y = 0; | |
513 } | |
514 if (y + this.ActualHeight > this.ParentDesktop.ActualHeight) | |
515 { | |
516 y = this.ParentDesktop.ActualHeight - this.ActualHeight; | |
517 } | |
518 if (x + this.ActualWidth > this.ParentDesktop.ActualWidth) | |
519 { | |
520 x = this.ParentDesktop.ActualWidth - this.ActualWidth; | |
521 } | |
522 } | |
523 | |
524 if (x != position.X || y != position.Y) | |
525 { | |
526 this.MoveElement(x, y); | |
527 } | |
528 } | |
529 } | |
530 else | |
531 { | |
532 Size size = this.RenderSize; | |
533 Vector changeFromPrevious = Point.Subtract(mousePosition, this.previousMousePosition); | |
534 | |
535 if (this.dragOrResizeStatus.IsOnRight) | |
536 { | |
537 if (size.Width + changeFromPrevious.X > this.MinWidth) | |
538 { | |
539 size.Width += changeFromPrevious.X; | |
540 } | |
541 } | |
542 else if (this.dragOrResizeStatus.IsOnLeft) | |
543 { | |
544 if (size.Width - changeFromStart.X > this.MinWidth) | |
545 { | |
546 this.MoveElementLeft(position.X + changeFromStart.X); | |
547 size.Width -= changeFromStart.X; | |
548 } | |
549 } | |
550 | |
551 if (this.dragOrResizeStatus.IsOnBottom) | |
552 { | |
553 if (size.Height + changeFromPrevious.Y > this.MinHeight) | |
554 { | |
555 size.Height += changeFromPrevious.Y; | |
556 } | |
557 } | |
558 else if (this.dragOrResizeStatus.IsOnTop) | |
559 { | |
560 if (size.Height - changeFromStart.Y > this.MinHeight) | |
561 { | |
562 this.MoveElementTop(position.Y + changeFromStart.Y); | |
563 size.Height -= changeFromStart.Y; | |
564 } | |
565 } | |
566 | |
567 this.Width = size.Width; | |
568 this.Height = size.Height; | |
569 } | |
570 } | |
571 } | |
572 | |
573 #endregion | |
574 | |
575 #region · Mouse Handling Methods · | |
576 | |
577 /// <summary> | |
578 /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.PreviewMouseDown"/> attached routed event | |
579 /// reaches an element in its route that is derived from this class. | |
580 /// Implement this method to add class handling for this event. | |
581 /// </summary> | |
582 /// <param name="e"> | |
583 /// The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data. | |
584 /// The event data reports that one or more mouse buttons were pressed. | |
585 /// </param> | |
586 protected override void OnPreviewMouseDown(MouseButtonEventArgs e) | |
587 { | |
588 if (!this.IsActive) | |
589 { | |
590 this.OnActivated(); | |
591 } | |
592 | |
593 base.OnPreviewMouseDown(e); | |
594 } | |
595 | |
596 /// <summary> | |
597 /// Invoked when an unhandled <see cref="E:System.Windows.UIElement.PreviewMouseLeftButtonDown"/> routed event | |
598 /// reaches an element in its route that is derived from this class. | |
599 /// Implement this method to add class handling for this event. | |
600 /// </summary> | |
601 /// <param name="e"> | |
602 /// The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data. | |
603 /// The event data reports that the left mouse button was pressed. | |
604 /// </param> | |
605 protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) | |
606 { | |
607 if (!e.Handled | |
608 && e.ClickCount == 1 | |
609 && e.Source == this) | |
610 { | |
611 if (this.dragOrResizeStatus == DragOrResizeStatus.None && | |
612 this.previewDragOrResizeStatus != DragOrResizeStatus.None) | |
613 { | |
614 e.Handled = true; | |
615 | |
616 this.dragOrResizeStatus = this.previewDragOrResizeStatus; | |
617 this.startMousePosition = this.previousMousePosition = e.GetPosition(this); | |
618 | |
619 this.CaptureMouse(); | |
620 } | |
621 } | |
622 | |
623 base.OnPreviewMouseLeftButtonDown(e); | |
624 } | |
625 | |
626 /// <summary> | |
627 /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.PreviewMouseMove"/> attached event | |
628 /// reaches an element in its route that is derived from this class. | |
629 /// Implement this method to add class handling for this event. | |
630 /// </summary> | |
631 /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs"/> that contains the event data.</param> | |
632 protected override void OnPreviewMouseMove(MouseEventArgs e) | |
633 { | |
634 if (this.dragOrResizeStatus == DragOrResizeStatus.None) | |
635 { | |
636 // http://www.switchonthecode.com/tutorials/wpf-snippet-reliably-getting-the-mouse-position | |
637 Point point = e.GetPosition(this); | |
638 | |
639 this.previewDragOrResizeStatus = this.GetDragOrResizeMode(point); | |
640 | |
641 if (!this.CanResize | |
642 && this.previewDragOrResizeStatus != DragOrResizeStatus.Drag | |
643 && this.previewDragOrResizeStatus != DragOrResizeStatus.None) | |
644 { | |
645 this.previewDragOrResizeStatus = DragOrResizeStatus.None; | |
646 } | |
647 | |
648 this.SetResizeCursor(this.previewDragOrResizeStatus); | |
649 } | |
650 else if (this.IsMouseCaptured) | |
651 { | |
652 if (e.MouseDevice.LeftButton == MouseButtonState.Pressed) | |
653 { | |
654 // http://www.switchonthecode.com/tutorials/wpf-snippet-reliably-getting-the-mouse-position | |
655 Point point = e.GetPosition(this); | |
656 | |
657 if (Math.Abs(point.X - this.previousMousePosition.X) > SystemParameters.MinimumHorizontalDragDistance || | |
658 Math.Abs(point.Y - this.previousMousePosition.Y) > SystemParameters.MinimumVerticalDragDistance) | |
659 { | |
660 e.Handled = true; | |
661 | |
662 this.AdjustBounds(point); | |
663 this.previousMousePosition = point; | |
664 } | |
665 } | |
666 else | |
667 { | |
668 this.CancelDragOrResize(); | |
669 } | |
670 } | |
671 else | |
672 { | |
673 this.CancelDragOrResize(); | |
674 } | |
675 | |
676 base.OnPreviewMouseMove(e); | |
677 } | |
678 | |
679 /// <summary> | |
680 /// Invoked when an unhandled <see cref="E:System.Windows.UIElement.MouseLeftButtonUp"/> routed event reaches an element | |
681 /// in its route that is derived from this class. Implement this method to add class handling for this event. | |
682 /// </summary> | |
683 /// <param name="e"> | |
684 /// The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data. The event data reports | |
685 /// that the left mouse button was released. | |
686 /// </param> | |
687 protected override void OnPreviewMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e) | |
688 { | |
689 if (this.IsMouseCaptured) | |
690 { | |
691 e.Handled = true; | |
692 this.CancelDragOrResize(); | |
693 } | |
694 | |
695 base.OnPreviewMouseLeftButtonUp(e); | |
696 } | |
697 | |
698 #endregion | |
699 | |
700 #region · Drag and Resize Methods · | |
701 | |
702 private DragOrResizeStatus GetDragOrResizeMode(Point position) | |
703 { | |
704 DragOrResizeStatus status = DragOrResizeStatus.None; | |
705 | |
706 if (this.CanDrag && | |
707 this.partDragger != null && | |
708 this.partDragger.IsMouseOver) | |
709 { | |
710 status = DragOrResizeStatus.Drag; | |
711 } | |
712 else if (this.CanResize) | |
713 { | |
714 if (position.X <= ResizeSideThichness) // left | |
715 { | |
716 status = this.GetLeftDragStatus(position); | |
717 } | |
718 else if (this.ActualWidth - position.X <= ResizeSideThichness) // right | |
719 { | |
720 status = this.GetRightDragStatus(position); | |
721 } | |
722 else if (position.Y <= ResizeSideThichness) // top | |
723 { | |
724 status = this.GetTopDragStatus(position); | |
725 } | |
726 else if (this.ActualHeight - position.Y <= ResizeSideThichness) // bottom | |
727 { | |
728 status = this.GetBottomDragStatus(position); | |
729 } | |
730 } | |
731 | |
732 return status; | |
733 } | |
734 | |
735 private DragOrResizeStatus GetBottomDragStatus(Point position) | |
736 { | |
737 DragOrResizeStatus status; | |
738 | |
739 if (position.X <= ResizeCornerSize) | |
740 { | |
741 status = DragOrResizeStatus.BottomLeft; | |
742 } | |
743 else if (this.ActualWidth - position.X <= ResizeCornerSize) | |
744 { | |
745 status = DragOrResizeStatus.BottomRight; | |
746 } | |
747 else | |
748 { | |
749 status = DragOrResizeStatus.BottomCenter; | |
750 } | |
751 | |
752 return status; | |
753 } | |
754 | |
755 private DragOrResizeStatus GetTopDragStatus(Point position) | |
756 { | |
757 DragOrResizeStatus status; | |
758 | |
759 if (position.X <= ResizeCornerSize) | |
760 { | |
761 status = DragOrResizeStatus.TopLeft; | |
762 } | |
763 else if (this.ActualWidth - position.X <= ResizeCornerSize) | |
764 { | |
765 status = DragOrResizeStatus.TopRight; | |
766 } | |
767 else | |
768 { | |
769 status = DragOrResizeStatus.TopCenter; | |
770 } | |
771 | |
772 return status; | |
773 } | |
774 | |
775 private DragOrResizeStatus GetRightDragStatus(Point position) | |
776 { | |
777 DragOrResizeStatus status; | |
778 | |
779 if (position.Y <= ResizeCornerSize) | |
780 { | |
781 status = DragOrResizeStatus.TopRight; | |
782 } | |
783 else if (this.ActualHeight - position.Y <= ResizeCornerSize) | |
784 { | |
785 status = DragOrResizeStatus.BottomRight; | |
786 } | |
787 else | |
788 { | |
789 status = DragOrResizeStatus.MiddleRight; | |
790 } | |
791 | |
792 return status; | |
793 } | |
794 | |
795 private DragOrResizeStatus GetLeftDragStatus(Point position) | |
796 { | |
797 DragOrResizeStatus status; | |
798 | |
799 if (position.Y <= ResizeCornerSize) | |
800 { | |
801 status = DragOrResizeStatus.TopLeft; | |
802 } | |
803 else if (this.ActualHeight - position.Y <= ResizeCornerSize) | |
804 { | |
805 status = DragOrResizeStatus.BottomLeft; | |
806 } | |
807 else | |
808 { | |
809 status = DragOrResizeStatus.MiddleLeft; | |
810 } | |
811 | |
812 return status; | |
813 } | |
814 | |
815 private void CancelDragOrResize() | |
816 { | |
817 this.Cursor = null; | |
818 this.dragOrResizeStatus = DragOrResizeStatus.None; | |
819 this.previewDragOrResizeStatus = DragOrResizeStatus.None; | |
820 | |
821 this.ReleaseMouseCapture(); | |
822 } | |
823 | |
824 private void SetResizeCursor(DragOrResizeStatus resizeStatus) | |
825 { | |
826 if (this.CanResize || this.CanDrag) | |
827 { | |
828 if (resizeStatus.IsDragging) | |
829 { | |
830 this.Cursor = null; | |
831 } | |
832 else if (resizeStatus.IsOnTopLeftOrBottomRight) | |
833 { | |
834 this.Cursor = Cursors.SizeNWSE; | |
835 } | |
836 else if (resizeStatus.IsOnTopRightOrBottomLeft) | |
837 { | |
838 this.Cursor = Cursors.SizeNESW; | |
839 } | |
840 else if (resizeStatus.IsOnTopRightOrBottomLeft) | |
841 { | |
842 this.Cursor = Cursors.SizeNESW; | |
843 } | |
844 else if (resizeStatus.IsOnTopCenterOrBottomCenter) | |
845 { | |
846 this.Cursor = Cursors.SizeNS; | |
847 } | |
848 else if (resizeStatus.IsOnMiddleLeftOrMiddleRight) | |
849 { | |
850 this.Cursor = Cursors.SizeWE; | |
851 } | |
852 else if (this.Cursor != null) | |
853 { | |
854 this.Cursor = null; | |
855 } | |
856 } | |
857 else if (this.Cursor != null) | |
858 { | |
859 this.Cursor = null; | |
860 } | |
861 } | |
862 | |
863 #endregion | |
864 } | |
865 } |