using MuchInfo.Chart.Data.EnumTypes; using MuchInfo.Chart.Infrastructure.Helpers; using MuchInfo.Chart.WPF.Controls.Utilities; using MuchInfo.Chart.WPF.Helpers; using MuchInfo.Chart.WPF.Primitives.Drawing; using MuchInfo.Chart.WPF.Primitives.Interfaces; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace MuchInfo.Chart.WPF.Controls.Drawing { /// /// DrawingSelectionGrab.xaml 的交互逻辑 /// public partial class DrawingSelectionGrab : UserControl { #region Fields private Chart _chart; private IDrawingTool _drawingTool; private bool _flag1; private bool _flag2; private bool _isMouseEnter; private Point _position; #endregion Fields #region Constructors //private bool DrawingSelectionGrab_854; public DrawingSelectionGrab() { this._isMouseEnter = false; this._flag1 = false; this.InitializeComponent(); this.mSelectionLine.Cursor = (Cursors.Hand); } #endregion Constructors #region Methods #region Public Methods public void SetPosition(Point p1, Point p2) { var newP1 = AdjustPointByPaintArea(p1); var newP2 = AdjustPointByPaintArea(p2); this.mSelectionLine.X1 = newP1.X; this.mSelectionLine.Y1 = newP1.Y; this.mSelectionLine.X2 = newP2.X; this.mSelectionLine.Y2 = newP2.Y; } public void Setup(IDrawingTool aTool, Chart theChart) { this._drawingTool = aTool; this._chart = theChart; } #endregion Public Methods #region Internal Methods internal Point AdjustPointByPaintArea(Point point) { var result = new Point() { X = point.X, Y = point.Y }; var paintArea = this.GetPaintAreaCanvas(); if (paintArea == null) return result; var x = paintArea.ActualWidth; var y = paintArea.ActualHeight - 6; result.X = result.X < 0 ? 0 : result.X; result.Y = result.Y < 0 ? 0 : result.Y; result.X = result.X > x ? x : result.X; result.Y = result.Y > y ? y : result.Y; return result; } #endregion Internal Methods #region Private Methods /// /// Builds the context menu. /// private void ShowContextMenu() { var contextMenu = new ContextMenu(); contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit))); bool flag = this._drawingTool is DrawingTrendLine; if (flag) { contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_CreateParallelLine), null, new EventHandler(this.CreateParallel))); } ////flag = Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true); //flag = (this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()); //if (flag) //{ // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetAlert)); //} //flag = false;// Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingHorizontalLine && ((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true); //if (flag) //{ // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetPriceAlert)); //} contextMenu.Items.Add(new Separator()); contextMenu.Items.Add(MenuItemHelper.CreateMenuItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveTool))); this.ContextMenu = contextMenu; this.ContextMenu.IsOpen = true; } /// /// Builds the popup menu. /// /// The instance containing the event data. private void ShowPopupMenu(MouseEventArgs e) { var popupMenu = new PopupMenu(this._chart.RootPanel) { FontSize = this._drawingTool.GetChartFontSize() }; popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Edit), ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowToolEdit)); bool flag = this._drawingTool is DrawingTrendLine; if (flag) { popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_CreateParallelLine), null, new EventHandler(this.CreateParallel)); } ////flag = Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true); //flag = (this._drawingTool is DrawingTrendLine && ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3758()); //if (flag) //{ // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetAlert)); //} //flag = false;// Conversions.ToBoolean((!Conversions.ToBoolean(this._drawingTool is DrawingHorizontalLine && ((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3532()) || !Conversions.ToBoolean(AlertManager.AlertsAllowed())) ? false : true); //if (flag) //{ // PopupMenu.AddItem("Set Alert", ImageHelper.GetImage("SymbolBolt.png"), new EventHandler(this.SetPriceAlert)); //} popupMenu.AddSeperator(); popupMenu.AddItem(LanguageManager.FindResource(LanguageConst.DrawingTool_MenuItem_Remove), ImageHelper.GetImage("DeleteHS.png"), new EventHandler(this.RemoveTool)); var offset = new Point(-5.0, -5.0); popupMenu.Show(e, offset); } private void CreateParallel(object sender, EventArgs e) { var trendLine = _drawingTool as DrawingTrendLine; if (trendLine == null) return; trendLine.CreateParallel(); } /// /// 获取ChartPane的PaintArea Canvas /// /// PaintArea Canvas private FrameworkElement GetPaintAreaCanvas() { var parent = this.Parent as FrameworkElement; if (parent == null) return null; var grandparent = parent.Parent as FrameworkElement; if (grandparent == null) return null; var paintArea = grandparent.Parent as FrameworkElement; return paintArea; } private void MovePlot(Point curPos) { if (_drawingTool == null) return; var newPositon = AdjustPointByPaintArea(curPos); var paintArea = this.GetPaintAreaCanvas(); if (paintArea != null) { var maxPoint = new Point() { X = paintArea.ActualWidth, Y = paintArea.ActualHeight - 6 }; this._drawingTool.MovePlot(this._position, newPositon, maxPoint); } else { this._drawingTool.MovePlot(this._position, newPositon); } } private void mSelectionLine_LostMouseCapture(object sender, MouseEventArgs e) { this._chart.LockPainting = false; this._flag1 = false; this._chart.Refresh(); this.UpdateSelectionLine(); } private void mSelectionLine_MouseEnter(object sender, MouseEventArgs e) { if (this._chart.PointerType != PointerType.Drawing) { this._isMouseEnter = true; this.UpdateSelectionLine(); } else { this.mSelectionLine.Cursor = Cursors.None; } } private void mSelectionLine_MouseLeave(object sender, MouseEventArgs e) { this._isMouseEnter = false; this.UpdateSelectionLine(); } private void mSelectionLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { bool flag = this._chart.PointerType == PointerType.Drawing; if (!flag) { this._chart.LockPainting = true; this._position = e.GetPosition((UIElement)this.mSelectionLine.Parent); this._flag1 = true; this._flag2 = false; this.mSelectionLine.CaptureMouse(); flag = (this._drawingTool != null); if (flag) { this._drawingTool.PrepareToMove(); } } } private void mSelectionLine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this._chart.LockPainting = false; bool flag = this._drawingTool != null && !this._flag2; if (flag) { var flag2 = this._chart.PointerType == PointerType.Erase; if (flag2) { this._drawingTool.Delete(); } else { //右键菜单 //this.BuildPopupMenu(e); } } else { bool flag2 = this._drawingTool != null; if (flag2) { this.MovePlot(e.GetPosition((UIElement)this.mSelectionLine.Parent)); flag2 = (this._drawingTool.OwnerSpec != null); if (flag2) { this._drawingTool.OwnerSpec.ToolChanged(this._drawingTool); } this._flag1 = false; //不用重新画图,MouseMove处理了MovePlot //this._chart.Refresh(); } } this.mSelectionLine.ReleaseMouseCapture(); this._flag1 = false; this._flag2 = false; this.UpdateSelectionLine(); } private void mSelectionLine_MouseMove(object sender, MouseEventArgs e) { if (this._chart != null && this._chart.PointerType == PointerType.Erase) { this._chart.SetCursorImagePos(true, e); } var flag = this._flag1; if (flag) { Point position = e.GetPosition((UIElement)this.mSelectionLine.Parent); float num = GeometryHelper.DistanceBetweenPoints(this._position, position); flag = (num > 8f); if (flag) { this._flag2 = true; } flag = this._flag2; if (flag) { this.MovePlot(position); } } } /// /// Up弹出Popup,PopupManager里用Down关闭Popup /// /// The source of the event. /// The instance containing the event data. private void mSelectionLine_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { this._chart.LockPainting = false; if (this._drawingTool == null) return; ////使用上下文菜单代替Popup,以免以免父控件有右键菜单 //this.ShowContextMenu(); //修改popup父容器,即每个Chart提供RootPanel,不使用PopupRoot和TrueRootVisual //因为应用程序可能开多个图表窗口 this.ShowPopupMenu(e); e.Handled = true; } //private void SetAlert(object sender, EventArgs e) //{ // try // { // ((DrawingTrendLine)this._drawingTool).drawingTrendLine_3759((MouseEventArgs)e); // } // catch (Exception expr_3F) // { // ProjectData.SetProjectError(expr_3F); // ProjectData.ClearProjectError(); // } //} //private void SetPriceAlert(object sender, EventArgs e) //{ // try // { // //((DrawingHorizontalLine)this._drawingTool).drawingHorizontalLine_3533((MouseEventArgs)e); // } // catch (Exception expr_3F) // { // ProjectData.SetProjectError(expr_3F); // ProjectData.ClearProjectError(); // } //} private void RemoveTool(object sender, EventArgs e) { this._drawingTool.Delete(); } private void ShowToolEdit(object sender, EventArgs e) { this._drawingTool.ShowEditor((MouseEventArgs)e); } private void UpdateSelectionLine() { bool flag = this._isMouseEnter || this._flag1; if (flag) { this.mSelectionLine.Stroke = (new SolidColorBrush(Color.FromArgb(125, 125, 125, 125))); switch (this._chart.PointerType) { case PointerType.Erase: this.mSelectionLine.Cursor = Cursors.Arrow; break; case PointerType.Drawing: this.mSelectionLine.Cursor = Cursors.None; break; default: this.mSelectionLine.Cursor = Cursors.Hand; break; } } else { this.mSelectionLine.Stroke = (new SolidColorBrush(Colors.Transparent)); } } #endregion Private Methods #endregion Methods } }