PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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