PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/CBR/CBR/Components/Designer/ResizeThumb.cs

#
C# | 82 lines | 72 code | 10 blank | 0 comment | 9 complexity | 47b3b20d9e37c1afabc7f8823f4088fa MD5 | raw file
 1using System;
 2using System.Windows;
 3using System.Windows.Controls;
 4using System.Windows.Controls.Primitives;
 5using System.Windows.Media;
 6
 7namespace CBR.Components.Designer
 8{
 9    public class ResizeThumb : Thumb
10    {
11        private DesignerItem designerItem;
12        private DesignerCanvas designerCanvas;
13
14        public ResizeThumb()
15        {
16            DragStarted += new DragStartedEventHandler(this.ResizeThumb_DragStarted);
17            DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta);
18        }
19
20        private void ResizeThumb_DragStarted(object sender, DragStartedEventArgs e)
21        {
22            this.designerItem = DataContext as DesignerItem;
23
24            if (this.designerItem != null)
25            {
26                this.designerCanvas = VisualTreeHelper.GetParent(this.designerItem) as DesignerCanvas;
27            }
28        }
29
30        private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
31        {
32            if (this.designerItem != null && this.designerCanvas != null && this.designerItem.IsSelected)
33            {
34                double minLeft = double.MaxValue;
35                double minTop = double.MaxValue;
36                double minDeltaHorizontal = double.MaxValue;
37                double minDeltaVertical = double.MaxValue;
38                double dragDeltaVertical, dragDeltaHorizontal;
39
40                foreach (DesignerItem item in this.designerCanvas.SelectedItems)
41                {
42                    minLeft = Math.Min(Canvas.GetLeft(item), minLeft);
43                    minTop = Math.Min(Canvas.GetTop(item), minTop);
44
45                    minDeltaVertical = Math.Min(minDeltaVertical, item.ActualHeight - item.MinHeight);
46                    minDeltaHorizontal = Math.Min(minDeltaHorizontal, item.ActualWidth - item.MinWidth);
47                }
48
49                foreach (DesignerItem item in this.designerCanvas.SelectedItems)
50                {
51                    switch (VerticalAlignment)
52                    {
53                        case VerticalAlignment.Bottom:
54                            dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
55                            item.Height = item.ActualHeight - dragDeltaVertical;
56                            break;
57                        case VerticalAlignment.Top:
58                            dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
59                            Canvas.SetTop(item, Canvas.GetTop(item) + dragDeltaVertical);
60                            item.Height = item.ActualHeight - dragDeltaVertical;
61                            break;
62                    }
63
64                    switch (HorizontalAlignment)
65                    {
66                        case HorizontalAlignment.Left:
67                            dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
68                            Canvas.SetLeft(item, Canvas.GetLeft(item) + dragDeltaHorizontal);
69                            item.Width = item.ActualWidth - dragDeltaHorizontal;
70                            break;
71                        case HorizontalAlignment.Right:
72                            dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
73                            item.Width = item.ActualWidth - dragDeltaHorizontal;
74                            break;
75                    }
76                }
77
78                e.Handled = true;
79            }
80        }
81    }
82}