External/OxyPlot/OxyPlot.WindowsForms/PlotControl.cs
author moel.mich
Sat, 08 Jun 2013 16:53:22 +0000
changeset 391 5be8f2773237
permissions -rw-r--r--
Added the source code of OxyPlot as of commit d190d7748a73 (6.5.2013).
moel@391
     1
// --------------------------------------------------------------------------------------------------------------------
moel@391
     2
// <copyright file="PlotControl.cs" company="OxyPlot">
moel@391
     3
//   The MIT License (MIT)
moel@391
     4
//
moel@391
     5
//   Copyright (c) 2012 Oystein Bjorke
moel@391
     6
//
moel@391
     7
//   Permission is hereby granted, free of charge, to any person obtaining a
moel@391
     8
//   copy of this software and associated documentation files (the
moel@391
     9
//   "Software"), to deal in the Software without restriction, including
moel@391
    10
//   without limitation the rights to use, copy, modify, merge, publish,
moel@391
    11
//   distribute, sublicense, and/or sell copies of the Software, and to
moel@391
    12
//   permit persons to whom the Software is furnished to do so, subject to
moel@391
    13
//   the following conditions:
moel@391
    14
//
moel@391
    15
//   The above copyright notice and this permission notice shall be included
moel@391
    16
//   in all copies or substantial portions of the Software.
moel@391
    17
//
moel@391
    18
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
moel@391
    19
//   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
moel@391
    20
//   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
moel@391
    21
//   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
moel@391
    22
//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
moel@391
    23
//   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
moel@391
    24
//   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
moel@391
    25
// </copyright>
moel@391
    26
// --------------------------------------------------------------------------------------------------------------------
moel@391
    27
using System;
moel@391
    28
using System.Collections.Generic;
moel@391
    29
using System.ComponentModel;
moel@391
    30
using System.Drawing;
moel@391
    31
using System.Windows.Forms;
moel@391
    32
using OxyPlot;
moel@391
    33
moel@391
    34
namespace Oxyplot.WindowsForms
moel@391
    35
{
moel@391
    36
    public class PlotControl : Control, IPlotControl
moel@391
    37
    {
moel@391
    38
        public List<MouseAction> MouseActions { get; private set; }
moel@391
    39
moel@391
    40
        private readonly PanAction panAction;
moel@391
    41
        private readonly SliderAction sliderAction;
moel@391
    42
        private readonly ZoomAction zoomAction;
moel@391
    43
        private Rectangle zoomRectangle;
moel@391
    44
moel@391
    45
        public PlotControl()
moel@391
    46
        {
moel@391
    47
            //    InitializeComponent();
moel@391
    48
            DoubleBuffered = true;
moel@391
    49
            Model = new PlotModel();
moel@391
    50
moel@391
    51
            panAction = new PanAction(this);
moel@391
    52
            zoomAction = new ZoomAction(this);
moel@391
    53
            sliderAction = new SliderAction(this);
moel@391
    54
moel@391
    55
            MouseActions = new List<MouseAction>();
moel@391
    56
            MouseActions.Add(panAction);
moel@391
    57
            MouseActions.Add(zoomAction);
moel@391
    58
            MouseActions.Add(sliderAction);
moel@391
    59
        }
moel@391
    60
moel@391
    61
        private PlotModel model;
moel@391
    62
moel@391
    63
        [Browsable(false), DefaultValue(null)]
moel@391
    64
        public PlotModel Model
moel@391
    65
        {
moel@391
    66
            get { return model; }
moel@391
    67
            set
moel@391
    68
            {
moel@391
    69
                model = value;
moel@391
    70
                Refresh();
moel@391
    71
            }
moel@391
    72
        }
moel@391
    73
moel@391
    74
        public override void Refresh()
moel@391
    75
        {
moel@391
    76
            if (model != null)
moel@391
    77
                model.UpdateData();
moel@391
    78
            base.Refresh();
moel@391
    79
        }
moel@391
    80
moel@391
    81
        protected override void OnResize(EventArgs e)
moel@391
    82
        {
moel@391
    83
            base.OnResize(e);
moel@391
    84
            Invalidate();
moel@391
    85
        }
moel@391
    86
moel@391
    87
        protected override void OnPaint(PaintEventArgs e)
moel@391
    88
        {
moel@391
    89
            base.OnPaint(e);
moel@391
    90
moel@391
    91
            var rc = new GraphicsRenderContext(this, e.Graphics, e.ClipRectangle);
moel@391
    92
            if (model != null)
moel@391
    93
                model.Render(rc);
moel@391
    94
            if (zoomRectangle != Rectangle.Empty)
moel@391
    95
            {
moel@391
    96
                using (var zoomBrush = new SolidBrush(Color.FromArgb(0x40, 0xFF, 0xFF, 0x00)))
moel@391
    97
                using (var zoomPen = new Pen(Color.Black))
moel@391
    98
                {
moel@391
    99
                    zoomPen.DashPattern = new float[] { 3, 1 };
moel@391
   100
                    e.Graphics.FillRectangle(zoomBrush, zoomRectangle);
moel@391
   101
                    e.Graphics.DrawRectangle(zoomPen, zoomRectangle);
moel@391
   102
                }
moel@391
   103
            }
moel@391
   104
        }
moel@391
   105
moel@391
   106
        protected override void OnKeyDown(KeyEventArgs e)
moel@391
   107
        {
moel@391
   108
            base.OnKeyDown(e);
moel@391
   109
            if (e.KeyCode == Keys.A)
moel@391
   110
            {
moel@391
   111
                ZoomAll();
moel@391
   112
            }
moel@391
   113
        }
moel@391
   114
moel@391
   115
        public void ZoomAll()
moel@391
   116
        {
moel@391
   117
            foreach (var a in Model.Axes)
moel@391
   118
                a.Reset();
moel@391
   119
            Refresh();
moel@391
   120
        }
moel@391
   121
moel@391
   122
        protected override void OnMouseDown(MouseEventArgs e)
moel@391
   123
        {
moel@391
   124
            base.OnMouseDown(e);
moel@391
   125
moel@391
   126
            Focus();
moel@391
   127
            Capture = true;
moel@391
   128
moel@391
   129
            bool control = Control.ModifierKeys == Keys.Control;
moel@391
   130
            bool shift = Control.ModifierKeys == Keys.Shift;
moel@391
   131
moel@391
   132
            var button = OxyMouseButton.Left;
moel@391
   133
            if (e.Button == MouseButtons.Middle)
moel@391
   134
                button = OxyMouseButton.Middle;
moel@391
   135
            if (e.Button == MouseButtons.Right)
moel@391
   136
                button = OxyMouseButton.Right;
moel@391
   137
            if (e.Button == MouseButtons.XButton1)
moel@391
   138
                button = OxyMouseButton.XButton1;
moel@391
   139
            if (e.Button == MouseButtons.XButton2)
moel@391
   140
                button = OxyMouseButton.XButton2;
moel@391
   141
moel@391
   142
            var p = new ScreenPoint(e.X, e.Y);
moel@391
   143
            foreach (var a in MouseActions)
moel@391
   144
                a.OnMouseDown(p, button, e.Clicks, control, shift);
moel@391
   145
        }
moel@391
   146
moel@391
   147
        protected override void OnMouseMove(MouseEventArgs e)
moel@391
   148
        {
moel@391
   149
            base.OnMouseMove(e);
moel@391
   150
            bool control = Control.ModifierKeys == Keys.Control;
moel@391
   151
            bool shift = Control.ModifierKeys == Keys.Shift;
moel@391
   152
            var p = new ScreenPoint(e.X, e.Y);
moel@391
   153
            foreach (var a in MouseActions)
moel@391
   154
                a.OnMouseMove(p, control, shift);
moel@391
   155
        }
moel@391
   156
moel@391
   157
        protected override void OnMouseUp(MouseEventArgs e)
moel@391
   158
        {
moel@391
   159
            base.OnMouseUp(e);
moel@391
   160
            foreach (var a in MouseActions)
moel@391
   161
                a.OnMouseUp();
moel@391
   162
            Capture = false;
moel@391
   163
        }
moel@391
   164
moel@391
   165
        protected override void OnMouseWheel(MouseEventArgs e)
moel@391
   166
        {
moel@391
   167
            base.OnMouseWheel(e);
moel@391
   168
            bool control = Control.ModifierKeys == Keys.Control;
moel@391
   169
            bool shift = Control.ModifierKeys == Keys.Shift;
moel@391
   170
            var p = new ScreenPoint(e.X, e.Y);
moel@391
   171
            foreach (var a in MouseActions)
moel@391
   172
                a.OnMouseWheel(p, e.Delta, control, shift);
moel@391
   173
        }
moel@391
   174
moel@391
   175
        public void GetAxesFromPoint(ScreenPoint pt, out AxisBase xaxis, out AxisBase yaxis)
moel@391
   176
        {
moel@391
   177
            Model.GetAxesFromPoint(pt, out xaxis, out yaxis);
moel@391
   178
        }
moel@391
   179
moel@391
   180
        public DataSeries GetSeriesFromPoint(ScreenPoint pt, double limit)
moel@391
   181
        {
moel@391
   182
            return Model.GetSeriesFromPoint(pt, limit);
moel@391
   183
        }
moel@391
   184
moel@391
   185
        public void Refresh(bool refreshData)
moel@391
   186
        {
moel@391
   187
            if (refreshData)
moel@391
   188
                Model.UpdateData();
moel@391
   189
            Invalidate();
moel@391
   190
        }
moel@391
   191
moel@391
   192
        public void Pan(AxisBase axis, double dx)
moel@391
   193
        {
moel@391
   194
            axis.Pan(dx);
moel@391
   195
        }
moel@391
   196
moel@391
   197
        public void Reset(AxisBase axis)
moel@391
   198
        {
moel@391
   199
            axis.Reset();
moel@391
   200
        }
moel@391
   201
moel@391
   202
        public void Zoom(AxisBase axis, double p1, double p2)
moel@391
   203
        {
moel@391
   204
            axis.Zoom(p1, p2);
moel@391
   205
        }
moel@391
   206
moel@391
   207
        public void ZoomAt(AxisBase axis, double factor, double x)
moel@391
   208
        {
moel@391
   209
            axis.ZoomAt(factor, x);
moel@391
   210
        }
moel@391
   211
moel@391
   212
        public OxyRect GetPlotArea()
moel@391
   213
        {
moel@391
   214
            return Model.PlotArea;
moel@391
   215
        }
moel@391
   216
moel@391
   217
        public void ShowSlider(DataSeries s, DataPoint dp)
moel@391
   218
        {
moel@391
   219
        }
moel@391
   220
moel@391
   221
        public void HideSlider()
moel@391
   222
        {
moel@391
   223
        }
moel@391
   224
moel@391
   225
        public void ShowZoomRectangle(OxyRect r)
moel@391
   226
        {
moel@391
   227
            zoomRectangle = new Rectangle((int)r.Left, (int)r.Top, (int)r.Width, (int)r.Height);
moel@391
   228
            Invalidate();
moel@391
   229
        }
moel@391
   230
moel@391
   231
        public void HideZoomRectangle()
moel@391
   232
        {
moel@391
   233
            zoomRectangle = Rectangle.Empty;
moel@391
   234
            Invalidate();
moel@391
   235
        }
moel@391
   236
    }
moel@391
   237
}