Blame view

sources/RoboforkApp/Controls/ResizeThumb.cs 3.4 KB
729be9a6d   doan   New Project
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
  using System;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Controls.Primitives;
  using System.Windows.Media;
  
  namespace RoboforkApp
  {
      public class ResizeThumb : Thumb
      {
          private DesignerItem designerItem;
          private DesignerCanvas designerCanvas;
  
          public ResizeThumb()
          {
              DragStarted += new DragStartedEventHandler(this.ResizeThumb_DragStarted);
              DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta);
          }
  
          private void ResizeThumb_DragStarted(object sender, DragStartedEventArgs e)
          {
              this.designerItem = DataContext as DesignerItem;
  
              if (this.designerItem != null)
              {
                  this.designerCanvas = VisualTreeHelper.GetParent(this.designerItem) as DesignerCanvas;
              }
          }
  
          private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
          {
              if (this.designerItem != null && this.designerCanvas != null && this.designerItem.IsSelected)
              {
                  double minLeft = double.MaxValue;
                  double minTop = double.MaxValue;
                  double minDeltaHorizontal = double.MaxValue;
                  double minDeltaVertical = double.MaxValue;
                  double dragDeltaVertical, dragDeltaHorizontal;
  
                  foreach (DesignerItem item in this.designerCanvas.SelectedItems)
                  {
                      minLeft = Math.Min(Canvas.GetLeft(item), minLeft);
                      minTop = Math.Min(Canvas.GetTop(item), minTop);
  
                      minDeltaVertical = Math.Min(minDeltaVertical, item.ActualHeight - item.MinHeight);
                      minDeltaHorizontal = Math.Min(minDeltaHorizontal, item.ActualWidth - item.MinWidth);
                  }
  
                  foreach (DesignerItem item in this.designerCanvas.SelectedItems)
                  {
                      switch (VerticalAlignment)
                      {
                          case VerticalAlignment.Bottom:
                              dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
                              item.Height = item.ActualHeight - dragDeltaVertical;
                              break;
                          case VerticalAlignment.Top:
                              dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
                              Canvas.SetTop(item, Canvas.GetTop(item) + dragDeltaVertical);
                              item.Height = item.ActualHeight - dragDeltaVertical;
                              break;
                      }
  
                      switch (HorizontalAlignment)
                      {
                          case HorizontalAlignment.Left:
                              dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
                              Canvas.SetLeft(item, Canvas.GetLeft(item) + dragDeltaHorizontal);
                              item.Width = item.ActualWidth - dragDeltaHorizontal;
                              break;
                          case HorizontalAlignment.Right:
                              dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
                              item.Width = item.ActualWidth - dragDeltaHorizontal;
                              break;
                      }
                  }
  
                  e.Handled = true;
              }
          }
      }
  }