You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
10 KiB

using GummingCommon;
using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using static GummingCommon.AppEnum;
namespace Gumming
{
/// <summary>
/// Image control which can be dragged and resized at runtime
/// </summary>
public class DraggableField : Border, IDraggableBase
{
#region Events
public event DraggableImageDragHandler Drag;
public event DraggableImageResizeHandler Resize;
#endregion
#region Member Variables
private Point _previousLocation;
//private Transform _previousTransform;
private Point _imagePosition;
private Point _imageSize;
private UIElement _container;
private bool _doubleConfirm;
private TextBlock _TextArea;
public TextBlock TextArea
{
get
{
return _TextArea;
}
set
{
_TextArea = value;
}
}
public PrintControlType ControlType { get; set; }
/// <summary>
/// ControlField
/// </summary>
public string ControlField { get; set; }
/// <summary>
/// ControlFormat
/// </summary>
public string ControlFormat
{
get
{
return TextArea.Text;
}
set
{
TextArea.Text = value;
}
}
#endregion
#region Constructors
/// <summary>
/// Static constructor
/// </summary>
static DraggableField()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DraggableField), new FrameworkPropertyMetadata(typeof(Border)));
}
/// <summary>
/// Default constructor
/// </summary>
public DraggableField(UIElement container, string controltype = null, string field = null, string format = null)
{
TextArea = new TextBlock();
TextArea.VerticalAlignment = VerticalAlignment.Top;
TextArea.HorizontalAlignment = HorizontalAlignment.Left;
this.Child = TextArea;
ControlType = (PrintControlType)Enum.Parse(typeof(PrintControlType), controltype);
ControlField = field;
ControlFormat = format;
this.MinHeight = 20;
this.MinWidth = 20;
_container = container;
this.Background = new SolidColorBrush(Colors.White);
}
public void SetFont(string font = null, decimal? fontsize = null, string fontstyle = null)
{
if (fontsize != null)
{
TextArea.FontSize = (double)fontsize;
TextArea.FontFamily = new FontFamily(font);
var propertyInfo = typeof(FontWeights).GetProperty(fontstyle,
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.IgnoreCase);
TextArea.FontWeight = (FontWeight)propertyInfo.GetValue(null, null);
}
}
#endregion
#region Methods
protected void OnDrag(DraggableImageDragEventArgs e)
{
DraggableImageDragHandler handler = Drag;
if (handler != null) handler(this, e);
}
protected void OnResize(DraggableImageResizeEventArgs e)
{
DraggableImageResizeHandler handler = Resize;
if (handler != null) handler(this, e);
}
#endregion
#region Overrides
protected override void OnMouseEnter(MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.CaptureMouse();
}
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(MouseEventArgs e)
{
this.ReleaseMouseCapture();
base.OnMouseLeave(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
this.ReleaseMouseCapture();
base.OnMouseLeftButtonUp(e);
this.Cursor = Cursors.Arrow;
_doubleConfirm = false;
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
this.CaptureMouse();
base.OnPreviewMouseDown(e);
Point currentLocation = e.MouseDevice.GetPosition(_container);
_previousLocation = currentLocation;
_imagePosition.X = (double)this.GetValue(Canvas.LeftProperty);
_imagePosition.Y = (double)this.GetValue(Canvas.TopProperty);
double width = !double.IsNaN(this.ActualWidth) ? this.ActualWidth : this.Width;
double height = !double.IsNaN(this.ActualHeight) ? this.ActualHeight : this.Height;
_imageSize.X = width;
_imageSize.Y = height;
//_previousTransform = this.RenderTransform;
Point locaion = e.MouseDevice.GetPosition(this);
bool stretchRight = (locaion.X >= width - 3) ? true : false;
bool stretchBottom = (locaion.Y >= height - 3) ? true : false;
//set cursor to show stretch direction
if (stretchRight && stretchBottom)
{
this.Cursor = Cursors.SizeNWSE;
}
else if (stretchRight)
{
this.Cursor = Cursors.SizeWE;
}
else if (stretchBottom)
{
this.Cursor = Cursors.SizeNS;
}
else
{
this.Cursor = Cursors.Hand;
}
_doubleConfirm = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
Point currentLocation = e.MouseDevice.GetPosition(_container);
//var move = new TranslateTransform(
// currentLocation.X - _previousLocation.X, currentLocation.Y - _previousLocation.Y);
double width = !double.IsNaN(this.ActualWidth) ? this.ActualWidth : this.Width;
double height = !double.IsNaN(this.ActualHeight) ? this.ActualHeight : this.Height;
if (e.LeftButton == MouseButtonState.Pressed && _doubleConfirm)
{
if (this.Cursor == Cursors.Hand)
{
/*
var group = new TransformGroup();
if (_previousTransform != null)
{
group.Children.Add(_previousTransform);
}
group.Children.Add(move);
this.RenderTransform = group;
* */
double chnageX = currentLocation.X - _previousLocation.X;
double chnageY = currentLocation.Y - _previousLocation.Y;
this.SetValue(Canvas.LeftProperty, _imagePosition.X + chnageX);
this.SetValue(Canvas.TopProperty, _imagePosition.Y + chnageY);
// Invoke drag event
OnDrag(new DraggableImageDragEventArgs(currentLocation, _previousLocation, chnageX, chnageY));
}
else if (this.Cursor == Cursors.SizeNWSE)
{
double chnageX = currentLocation.X - _previousLocation.X;
double chnageY = currentLocation.Y - _previousLocation.Y;
this.Width = _imageSize.X + chnageX < 1 ? 10 : _imageSize.X + chnageX;
this.Height = _imageSize.Y + chnageY < 1 ? 10 : _imageSize.Y + chnageY;
}
else if (this.Cursor == Cursors.SizeWE)
{
double chnageX = currentLocation.X - _previousLocation.X;
this.Width = _imageSize.X + chnageX < 1 ? 10 : _imageSize.X + chnageX;
}
else if (this.Cursor == Cursors.SizeNS)
{
double chnageY = currentLocation.Y - _previousLocation.Y;
this.Height = _imageSize.Y + chnageY < 1 ? 10 : _imageSize.Y + chnageY;
}
}
else
{
this.Cursor = Cursors.Arrow;
Point locaion = e.MouseDevice.GetPosition(this);
bool stretchRight = (locaion.X >= width - 3) ? true : false;
bool stretchBottom = (locaion.Y >= height - 3) ? true : false;
//set cursor to show stretch direction
if (stretchRight && stretchBottom)
{
this.Cursor = Cursors.SizeNWSE;
}
else if (stretchRight)
{
this.Cursor = Cursors.SizeWE;
}
else if (stretchBottom)
{
this.Cursor = Cursors.SizeNS;
}
else
{
this.Cursor = Cursors.Hand;
}
}
base.OnMouseMove(e);
}
public override string ToString()
{
string details = string.Empty;
double width = !double.IsNaN(this.ActualWidth) ? this.ActualWidth : this.Width;
double height = !double.IsNaN(this.ActualHeight) ? this.ActualHeight : this.Height;
if (this.Cursor == Cursors.Hand)
{
Window wnd = Window.GetWindow(this);
Point location = this.TranslatePoint(new Point(0, 0), wnd);
details = " : X = " + location.X + ", Y = " + location.Y;
}
else if (this.Cursor == Cursors.SizeWE)
{
details = " : Width2 = " + width;
}
else if (this.Cursor == Cursors.SizeNS)
{
details = " : Height2 = " + height;
}
return this.Name + details;
}
#endregion
}
}