moel@391: // -------------------------------------------------------------------------------------------------------------------- moel@391: // moel@391: // The MIT License (MIT) moel@391: // moel@391: // Copyright (c) 2012 Oystein Bjorke moel@391: // moel@391: // Permission is hereby granted, free of charge, to any person obtaining a moel@391: // copy of this software and associated documentation files (the moel@391: // "Software"), to deal in the Software without restriction, including moel@391: // without limitation the rights to use, copy, modify, merge, publish, moel@391: // distribute, sublicense, and/or sell copies of the Software, and to moel@391: // permit persons to whom the Software is furnished to do so, subject to moel@391: // the following conditions: moel@391: // moel@391: // The above copyright notice and this permission notice shall be included moel@391: // in all copies or substantial portions of the Software. moel@391: // moel@391: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS moel@391: // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF moel@391: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. moel@391: // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY moel@391: // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, moel@391: // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE moel@391: // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. moel@391: // moel@391: // -------------------------------------------------------------------------------------------------------------------- moel@391: using System; moel@391: using System.Collections.Generic; moel@391: using System.ComponentModel; moel@391: using System.Drawing; moel@391: using System.Windows.Forms; moel@391: using OxyPlot; moel@391: moel@391: namespace Oxyplot.WindowsForms moel@391: { moel@391: public class PlotControl : Control, IPlotControl moel@391: { moel@391: public List MouseActions { get; private set; } moel@391: moel@391: private readonly PanAction panAction; moel@391: private readonly SliderAction sliderAction; moel@391: private readonly ZoomAction zoomAction; moel@391: private Rectangle zoomRectangle; moel@391: moel@391: public PlotControl() moel@391: { moel@391: // InitializeComponent(); moel@391: DoubleBuffered = true; moel@391: Model = new PlotModel(); moel@391: moel@391: panAction = new PanAction(this); moel@391: zoomAction = new ZoomAction(this); moel@391: sliderAction = new SliderAction(this); moel@391: moel@391: MouseActions = new List(); moel@391: MouseActions.Add(panAction); moel@391: MouseActions.Add(zoomAction); moel@391: MouseActions.Add(sliderAction); moel@391: } moel@391: moel@391: private PlotModel model; moel@391: moel@391: [Browsable(false), DefaultValue(null)] moel@391: public PlotModel Model moel@391: { moel@391: get { return model; } moel@391: set moel@391: { moel@391: model = value; moel@391: Refresh(); moel@391: } moel@391: } moel@391: moel@391: public override void Refresh() moel@391: { moel@391: if (model != null) moel@391: model.UpdateData(); moel@391: base.Refresh(); moel@391: } moel@391: moel@391: protected override void OnResize(EventArgs e) moel@391: { moel@391: base.OnResize(e); moel@391: Invalidate(); moel@391: } moel@391: moel@391: protected override void OnPaint(PaintEventArgs e) moel@391: { moel@391: base.OnPaint(e); moel@391: moel@391: var rc = new GraphicsRenderContext(this, e.Graphics, e.ClipRectangle); moel@391: if (model != null) moel@391: model.Render(rc); moel@391: if (zoomRectangle != Rectangle.Empty) moel@391: { moel@391: using (var zoomBrush = new SolidBrush(Color.FromArgb(0x40, 0xFF, 0xFF, 0x00))) moel@391: using (var zoomPen = new Pen(Color.Black)) moel@391: { moel@391: zoomPen.DashPattern = new float[] { 3, 1 }; moel@391: e.Graphics.FillRectangle(zoomBrush, zoomRectangle); moel@391: e.Graphics.DrawRectangle(zoomPen, zoomRectangle); moel@391: } moel@391: } moel@391: } moel@391: moel@391: protected override void OnKeyDown(KeyEventArgs e) moel@391: { moel@391: base.OnKeyDown(e); moel@391: if (e.KeyCode == Keys.A) moel@391: { moel@391: ZoomAll(); moel@391: } moel@391: } moel@391: moel@391: public void ZoomAll() moel@391: { moel@391: foreach (var a in Model.Axes) moel@391: a.Reset(); moel@391: Refresh(); moel@391: } moel@391: moel@391: protected override void OnMouseDown(MouseEventArgs e) moel@391: { moel@391: base.OnMouseDown(e); moel@391: moel@391: Focus(); moel@391: Capture = true; moel@391: moel@391: bool control = Control.ModifierKeys == Keys.Control; moel@391: bool shift = Control.ModifierKeys == Keys.Shift; moel@391: moel@391: var button = OxyMouseButton.Left; moel@391: if (e.Button == MouseButtons.Middle) moel@391: button = OxyMouseButton.Middle; moel@391: if (e.Button == MouseButtons.Right) moel@391: button = OxyMouseButton.Right; moel@391: if (e.Button == MouseButtons.XButton1) moel@391: button = OxyMouseButton.XButton1; moel@391: if (e.Button == MouseButtons.XButton2) moel@391: button = OxyMouseButton.XButton2; moel@391: moel@391: var p = new ScreenPoint(e.X, e.Y); moel@391: foreach (var a in MouseActions) moel@391: a.OnMouseDown(p, button, e.Clicks, control, shift); moel@391: } moel@391: moel@391: protected override void OnMouseMove(MouseEventArgs e) moel@391: { moel@391: base.OnMouseMove(e); moel@391: bool control = Control.ModifierKeys == Keys.Control; moel@391: bool shift = Control.ModifierKeys == Keys.Shift; moel@391: var p = new ScreenPoint(e.X, e.Y); moel@391: foreach (var a in MouseActions) moel@391: a.OnMouseMove(p, control, shift); moel@391: } moel@391: moel@391: protected override void OnMouseUp(MouseEventArgs e) moel@391: { moel@391: base.OnMouseUp(e); moel@391: foreach (var a in MouseActions) moel@391: a.OnMouseUp(); moel@391: Capture = false; moel@391: } moel@391: moel@391: protected override void OnMouseWheel(MouseEventArgs e) moel@391: { moel@391: base.OnMouseWheel(e); moel@391: bool control = Control.ModifierKeys == Keys.Control; moel@391: bool shift = Control.ModifierKeys == Keys.Shift; moel@391: var p = new ScreenPoint(e.X, e.Y); moel@391: foreach (var a in MouseActions) moel@391: a.OnMouseWheel(p, e.Delta, control, shift); moel@391: } moel@391: moel@391: public void GetAxesFromPoint(ScreenPoint pt, out AxisBase xaxis, out AxisBase yaxis) moel@391: { moel@391: Model.GetAxesFromPoint(pt, out xaxis, out yaxis); moel@391: } moel@391: moel@391: public DataSeries GetSeriesFromPoint(ScreenPoint pt, double limit) moel@391: { moel@391: return Model.GetSeriesFromPoint(pt, limit); moel@391: } moel@391: moel@391: public void Refresh(bool refreshData) moel@391: { moel@391: if (refreshData) moel@391: Model.UpdateData(); moel@391: Invalidate(); moel@391: } moel@391: moel@391: public void Pan(AxisBase axis, double dx) moel@391: { moel@391: axis.Pan(dx); moel@391: } moel@391: moel@391: public void Reset(AxisBase axis) moel@391: { moel@391: axis.Reset(); moel@391: } moel@391: moel@391: public void Zoom(AxisBase axis, double p1, double p2) moel@391: { moel@391: axis.Zoom(p1, p2); moel@391: } moel@391: moel@391: public void ZoomAt(AxisBase axis, double factor, double x) moel@391: { moel@391: axis.ZoomAt(factor, x); moel@391: } moel@391: moel@391: public OxyRect GetPlotArea() moel@391: { moel@391: return Model.PlotArea; moel@391: } moel@391: moel@391: public void ShowSlider(DataSeries s, DataPoint dp) moel@391: { moel@391: } moel@391: moel@391: public void HideSlider() moel@391: { moel@391: } moel@391: moel@391: public void ShowZoomRectangle(OxyRect r) moel@391: { moel@391: zoomRectangle = new Rectangle((int)r.Left, (int)r.Top, (int)r.Width, (int)r.Height); moel@391: Invalidate(); moel@391: } moel@391: moel@391: public void HideZoomRectangle() moel@391: { moel@391: zoomRectangle = Rectangle.Empty; moel@391: Invalidate(); moel@391: } moel@391: } moel@391: }