forked from oxyplot/oxyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotView.cs
More file actions
462 lines (408 loc) · 14.9 KB
/
Copy pathPlotView.cs
File metadata and controls
462 lines (408 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlotView.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Represents a control that displays a <see cref="PlotModel" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.GtkSharp
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Gdk;
using Gtk;
/// <summary>
/// Represents a control that displays a <see cref="PlotModel" />.
/// </summary>
[Serializable]
public partial class PlotView : DrawingArea, IPlotView
{
/// <summary>
/// The category for the properties of this control.
/// </summary>
private const string OxyPlotCategory = "OxyPlot";
/// <summary>
/// The invalidate lock.
/// </summary>
private readonly object invalidateLock = new object();
/// <summary>
/// The model lock.
/// </summary>
private readonly object modelLock = new object();
/// <summary>
/// The rendering lock.
/// </summary>
private readonly object renderingLock = new object();
/// <summary>
/// The render context.
/// </summary>
private readonly GraphicsRenderContext renderContext;
/// <summary>
/// The current model (holding a reference to this plot view).
/// </summary>
[NonSerialized]
private PlotModel currentModel;
/// <summary>
/// The is model invalidated.
/// </summary>
private bool isModelInvalidated;
/// <summary>
/// The model.
/// </summary>
private PlotModel model;
/// <summary>
/// The update data flag.
/// </summary>
private bool updateDataFlag = true;
/// <summary>
/// The zoom rectangle.
/// </summary>
private OxyRect? zoomRectangle;
/// <summary>
/// The default controller
/// </summary>
private IPlotController defaultController;
/// <summary>
/// Initializes a new instance of the <see cref="PlotView" /> class.
/// </summary>
public PlotView()
{
this.renderContext = new GraphicsRenderContext();
// ReSharper disable DoNotCallOverridableMethodsInConstructor
this.DoubleBuffered = true;
// ReSharper restore DoNotCallOverridableMethodsInConstructor
this.PanCursor = new Cursor(CursorType.Hand1);
this.ZoomRectangleCursor = new Cursor(CursorType.Sizing);
this.ZoomHorizontalCursor = new Cursor(CursorType.SbHDoubleArrow);
this.ZoomVerticalCursor = new Cursor(CursorType.SbVDoubleArrow);
this.AddEvents((int)(EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.EnterNotifyMask | EventMask.LeaveNotifyMask | EventMask.ScrollMask | EventMask.KeyPressMask | EventMask.PointerMotionMask));
this.CanFocus = true;
}
/// <summary>
/// Gets or sets the model.
/// </summary>
[Browsable(false)]
[DefaultValue(null)]
[Category(OxyPlotCategory)]
public PlotModel Model
{
get
{
return this.model;
}
set
{
if (this.model != value)
{
this.model = value;
this.OnModelChanged();
}
}
}
/// <summary>
/// Gets the actual <see cref="OxyPlot.Model" /> of the control.
/// </summary>
Model IView.ActualModel
{
get
{
return this.Model;
}
}
/// <summary>
/// Gets the actual <see cref="PlotModel" /> of the control.
/// </summary>
public PlotModel ActualModel
{
get
{
return this.Model;
}
}
/// <summary>
/// Gets the actual controller.
/// </summary>
/// <value>
/// The actual <see cref="IController" />.
/// </value>
IController IView.ActualController
{
get
{
return this.ActualController;
}
}
/// <summary>
/// Gets the coordinates of the client area of the view.
/// </summary>
public OxyRect ClientArea
{
get
{
return new OxyRect(0, 0, Allocation.Width, Allocation.Height);
}
}
/// <summary>
/// Gets the actual plot controller.
/// </summary>
/// <value>The actual plot controller.</value>
public IPlotController ActualController
{
get
{
return this.Controller ?? (this.defaultController ?? (this.defaultController = new PlotController()));
}
}
/// <summary>
/// Gets or sets the plot controller.
/// </summary>
/// <value>The controller.</value>
public IPlotController Controller { get; set; }
/// <summary>
/// Gets or sets the pan cursor.
/// </summary>
[Category(OxyPlotCategory)]
public Cursor PanCursor { get; set; }
/// <summary>
/// Gets or sets the horizontal zoom cursor.
/// </summary>
[Category(OxyPlotCategory)]
public Cursor ZoomHorizontalCursor { get; set; }
/// <summary>
/// Gets or sets the rectangle zoom cursor.
/// </summary>
[Category(OxyPlotCategory)]
public Cursor ZoomRectangleCursor { get; set; }
/// <summary>
/// Gets or sets the vertical zoom cursor.
/// </summary>
[Category(OxyPlotCategory)]
public Cursor ZoomVerticalCursor { get; set; }
/// <summary>
/// Hides the tracker.
/// </summary>
public void HideTracker()
{
}
/// <summary>
/// Hides the zoom rectangle.
/// </summary>
public void HideZoomRectangle()
{
this.zoomRectangle = null;
this.QueueDraw();
}
/// <summary>
/// Invalidates the plot (not blocking the UI thread)
/// </summary>
/// <param name="updateData">if set to <c>true</c>, all data collections will be updated.</param>
public void InvalidatePlot(bool updateData)
{
lock (this.invalidateLock)
{
this.isModelInvalidated = true;
this.updateDataFlag = this.updateDataFlag || updateData;
}
this.QueueDraw();
}
/// <summary>
/// Called when the Model property has been changed.
/// </summary>
public void OnModelChanged()
{
lock (this.modelLock)
{
if (this.currentModel != null)
{
((IPlotModel)this.currentModel).AttachPlotView(null);
this.currentModel = null;
}
if (this.Model != null)
{
((IPlotModel)this.Model).AttachPlotView(this);
this.currentModel = this.Model;
}
}
this.InvalidatePlot(true);
}
/// <summary>
/// Sets the cursor type.
/// </summary>
/// <param name="cursorType">The cursor type.</param>
public void SetCursorType(OxyPlot.CursorType cursorType)
{
switch (cursorType)
{
case OxyPlot.CursorType.Pan:
this.GdkWindow.Cursor = this.PanCursor;
break;
case OxyPlot.CursorType.ZoomRectangle:
this.GdkWindow.Cursor = this.ZoomRectangleCursor;
break;
case OxyPlot.CursorType.ZoomHorizontal:
this.GdkWindow.Cursor = this.ZoomHorizontalCursor;
break;
case OxyPlot.CursorType.ZoomVertical:
this.GdkWindow.Cursor = this.ZoomVerticalCursor;
break;
default:
this.GdkWindow.Cursor = new Cursor(CursorType.Arrow);
break;
}
}
/// <summary>
/// Shows the tracker.
/// </summary>
/// <param name="data">The data.</param>
public void ShowTracker(TrackerHitResult data)
{
// not implemented for GtkSharp
}
/// <summary>
/// Shows the zoom rectangle.
/// </summary>
/// <param name="rectangle">The rectangle.</param>
public void ShowZoomRectangle(OxyRect rectangle)
{
this.zoomRectangle = rectangle;
this.QueueDraw();
}
/// <summary>
/// Sets the clipboard text.
/// </summary>
/// <param name="text">The text.</param>
public void SetClipboardText(string text)
{
try
{
// todo: can't get the following solution to work
// http://stackoverflow.com/questions/5707990/requested-clipboard-operation-did-not-succeed
this.GetClipboard(Gdk.Selection.Clipboard).Text = text;
}
catch (ExternalException)
{
// Requested Clipboard operation did not succeed.
// MessageBox.Show(this, ee.Message, "OxyPlot");
}
}
/// <summary>
/// Called when the mouse button is pressed.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
protected override bool OnButtonPressEvent(EventButton e)
{
this.GrabFocus();
return this.ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs());
}
/// <summary>
/// Called on mouse move events.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
protected override bool OnMotionNotifyEvent(EventMotion e)
{
return this.ActualController.HandleMouseMove(this, e.ToMouseEventArgs());
}
/// <summary>
/// Called when the mouse button is released.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
protected override bool OnButtonReleaseEvent(EventButton e)
{
return this.ActualController.HandleMouseUp(this, e.ToMouseUpEventArgs());
}
/// <summary>
/// Called when the mouse wheel is scrolled.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
protected override bool OnScrollEvent(EventScroll e)
{
return this.ActualController.HandleMouseWheel(this, e.ToMouseWheelEventArgs());
}
/// <summary>
/// Called when the mouse enters the widget.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
protected override bool OnEnterNotifyEvent(EventCrossing e)
{
return this.ActualController.HandleMouseEnter(this, e.ToMouseEventArgs());
}
/// <summary>
/// Called when the mouse leaves the widget.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
protected override bool OnLeaveNotifyEvent(EventCrossing e)
{
return this.ActualController.HandleMouseLeave(this, e.ToMouseEventArgs());
}
/// <summary>
/// Called on KeyPress event.
/// </summary>
/// <param name="e">An instance that contains the event data.</param>
/// <returns>True if event was handled?</returns>
protected override bool OnKeyPressEvent(EventKey e)
{
return this.ActualController.HandleKeyDown(this, e.ToKeyEventArgs());
}
/// <summary>
/// Draws the plot to a cairo context within the specified bounds.
/// </summary>
/// <param name="cr">The cairo context to use for drawing.</param>
void DrawPlot (Cairo.Context cr)
{
try
{
lock (this.invalidateLock)
{
if (this.isModelInvalidated)
{
if (this.model != null)
{
((IPlotModel)this.model).Update(this.updateDataFlag);
this.updateDataFlag = false;
}
this.isModelInvalidated = false;
}
}
lock (this.renderingLock)
{
this.renderContext.SetGraphicsTarget(cr);
if (this.model != null)
{
if (!this.model.Background.IsUndefined())
{
this.renderContext.DrawRectangle(Allocation.ToOxyRect(), this.model.Background, OxyColors.Undefined, 0);
}
((IPlotModel)this.model).Render(this.renderContext, Allocation.Width, Allocation.Height);
}
if (this.zoomRectangle.HasValue)
{
this.renderContext.DrawRectangle(this.zoomRectangle.Value, OxyColor.FromArgb(0x40, 0xFF, 0xFF, 0x00), OxyColors.Transparent, 1.0);
}
}
}
catch (Exception paintException)
{
var trace = new StackTrace(paintException);
Debug.WriteLine(paintException);
Debug.WriteLine(trace);
// using (var font = new Font("Arial", 10))
{
// int width; int height;
// this.GetSizeRequest(out width, out height);
Debug.Assert(false, "OxyPlot paint exception: " + paintException.Message);
// g.ResetTransform();
// g.DrawString(, font, Brushes.Red, width / 2, height / 2, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
}
}
}
}
}