comparison Chronosv2/source/Presentation/Windows/RubberbandAdorner.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.Documents;
28 using System.Windows.Input;
29 using System.Windows.Media;
30 using Chronos.Presentation.Windows.Controls;
31
32 namespace Chronos.Presentation.Windows
33 {
34 /// <summary>
35 /// Addorner for the rubber band selection
36 /// </summary>
37 public class RubberbandAdorner : Adorner
38 {
39 #region · Fields ·
40
41 private Point? startPoint;
42 private Point? endPoint;
43 private Pen rubberbandPen;
44 private Desktop desktop;
45 private Brush backgroundBrush;
46
47 #endregion
48
49 #region · Constructors ·
50
51 /// <summary>
52 /// Initializes a new instance of the <see cref="RubberbandAdorner"/> class.
53 /// </summary>
54 /// <param name="canvas">The canvas.</param>
55 /// <param name="dragStartPoint">The drag start point.</param>
56 public RubberbandAdorner(Desktop canvas, Point? dragStartPoint)
57 : base(canvas)
58 {
59 ColorConverter cconverter = new ColorConverter();
60
61 this.desktop = canvas;
62 this.startPoint = dragStartPoint;
63 this.rubberbandPen = new Pen(new SolidColorBrush((Color)cconverter.ConvertFrom("#FF7AA3D4")), 1);
64 this.rubberbandPen.DashStyle = new DashStyle();
65 this.backgroundBrush = new SolidColorBrush((Color)cconverter.ConvertFrom("#FFC5D5E9"));
66 this.backgroundBrush.Opacity = 0.40;
67 }
68
69 #endregion
70
71 #region · Protected Methods ·
72
73 /// <summary>
74 /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseMove"/> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
75 /// </summary>
76 /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs"/> that contains the event data.</param>
77 protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
78 {
79 if (e.LeftButton == MouseButtonState.Pressed)
80 {
81 if (!this.IsMouseCaptured)
82 {
83 this.CaptureMouse();
84 }
85
86 endPoint = e.GetPosition(this);
87
88 this.UpdateSelection();
89 this.InvalidateVisual();
90 }
91 else
92 {
93 if (this.IsMouseCaptured)
94 {
95 this.ReleaseMouseCapture();
96 }
97 }
98
99 e.Handled = true;
100 }
101
102 /// <summary>
103 /// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseUp"/> routed event
104 /// reaches an element in its route that is derived from this class.
105 /// Implement this method to add class handling for this event.
106 /// </summary>
107 /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data.
108 /// The event data reports that the mouse button was released.</param>
109 protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
110 {
111 // release mouse capture
112 if (this.IsMouseCaptured)
113 {
114 this.ReleaseMouseCapture();
115 }
116
117 // remove this adorner from adorner layer
118 AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this.desktop);
119 if (adornerLayer != null)
120 {
121 adornerLayer.Remove(this);
122 }
123
124 e.Handled = true;
125 }
126
127 /// <summary>
128 /// When overridden in a derived class, participates in rendering operations that are directed by the layout system. The rendering instructions for this element are not used directly when this method is invoked, and are instead preserved for later asynchronous use by layout and drawing.
129 /// </summary>
130 /// <param name="drawingContext">The drawing instructions for a specific element. This context is provided to the layout system.</param>
131 protected override void OnRender(DrawingContext drawingContext)
132 {
133 base.OnRender(drawingContext);
134
135 // without a background the OnMouseMove event would not be fired!
136 // Alternative: implement a Canvas as a child of this adorner, like
137 // the ConnectionAdorner does.
138 drawingContext.DrawRectangle(Brushes.Transparent, null, new Rect(RenderSize));
139
140 if (this.startPoint.HasValue && this.endPoint.HasValue)
141 {
142 drawingContext.DrawRectangle(this.backgroundBrush, rubberbandPen, new Rect(this.startPoint.Value, this.endPoint.Value));
143 }
144 }
145
146 #endregion
147
148 #region · Private Methods ·
149
150 private void UpdateSelection()
151 {
152 desktop.SelectionService.ClearSelection();
153
154 Rect rubberBand = new Rect(startPoint.Value, endPoint.Value);
155
156 foreach (UIElement item in desktop.Children)
157 {
158 Rect itemRect = VisualTreeHelper.GetDescendantBounds(item);
159 Rect itemBounds = item.TransformToAncestor(desktop).TransformBounds(itemRect);
160
161 if (rubberBand.Contains(itemBounds))
162 {
163 if (item is ISelectable)
164 {
165 ISelectable di = item as ISelectable;
166
167 if (di.ParentId == Guid.Empty)
168 {
169 desktop.SelectionService.AddToSelection(di);
170 }
171 }
172 }
173 }
174 }
175
176 #endregion
177 }
178 }