~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt.Drawing/Context.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// Context.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using Xwt.Backends;
 
28
using Xwt.Engine;
 
29
 
 
30
namespace Xwt.Drawing
 
31
{
 
32
        public sealed class Context: XwtObject, IDisposable
 
33
        {
 
34
                static IContextBackendHandler handler;
 
35
                Pattern pattern;
 
36
                Font font;
 
37
                double globalAlpha = 1;
 
38
                
 
39
                static Context ()
 
40
                {
 
41
                        handler = WidgetRegistry.CreateSharedBackend<IContextBackendHandler> (typeof(Context));
 
42
                }
 
43
                
 
44
                protected override IBackendHandler BackendHandler {
 
45
                        get {
 
46
                                return handler;
 
47
                        }
 
48
                }
 
49
                
 
50
                internal Context (object backend): base (backend)
 
51
                {
 
52
                }
 
53
                
 
54
                /// <summary>
 
55
                /// Makes a copy of the current state of the Context and saves it on an internal stack of saved states.
 
56
                /// When Restore() is called, it will be restored to the saved state. 
 
57
                /// Multiple calls to Save() and Restore() can be nested; 
 
58
                /// each call to Restore() restores the state from the matching paired save().
 
59
                /// </summary>
 
60
                public void Save ()
 
61
                {
 
62
                        handler.Save (Backend);
 
63
                }
 
64
                
 
65
                public void Restore ()
 
66
                {
 
67
                        handler.Restore (Backend);
 
68
                }
 
69
                
 
70
                public double GlobalAlpha {
 
71
                        get { return globalAlpha; }
 
72
                        set {
 
73
                                globalAlpha = value;
 
74
                                handler.SetGlobalAlpha (Backend, globalAlpha);
 
75
                        }
 
76
                }
 
77
                
 
78
                public void SetColor (Color color)
 
79
                {
 
80
                        handler.SetColor (Backend, color);
 
81
                }
 
82
                
 
83
                /// <summary>
 
84
                /// Adds a circular arc of the given radius to the current path.
 
85
                /// The arc is centered at (xc, yc), 
 
86
                /// begins at angle1 and proceeds in the direction 
 
87
                /// of increasing angles to end at angle2. 
 
88
                /// If angle2 is less than angle1,
 
89
                /// it will be progressively increased by 1 degree until it is greater than angle1.
 
90
                /// If there is a current point, an initial line segment will be added to the path 
 
91
                /// to connect the current point to the beginning of the arc. 
 
92
                /// If this initial line is undesired, 
 
93
                /// it can be avoided by calling NewPath() before calling Arc().
 
94
                /// </summary>
 
95
                /// <param name='xc'>
 
96
                /// Xc.
 
97
                /// </param>
 
98
                /// <param name='yc'>
 
99
                /// Yc.
 
100
                /// </param>
 
101
                /// <param name='radius'>
 
102
                /// Radius.
 
103
                /// </param>
 
104
                /// <param name='angle1'>
 
105
                /// Angle1 in degrees
 
106
                /// </param>
 
107
                /// <param name='angle2'>
 
108
                /// Angle2 in degrees
 
109
                /// </param>
 
110
                public void Arc (double xc, double yc, double radius, double angle1, double angle2)
 
111
                {
 
112
                        handler.Arc (Backend, xc, yc, radius, angle1, angle2);
 
113
                }
 
114
 
 
115
                /// <summary>
 
116
                /// Adds a circular arc of the given radius to the current path.
 
117
                /// The arc is centered at (xc, yc), 
 
118
                /// begins at angle1 and proceeds in the direction 
 
119
                /// of decreasing angles to end at angle2. 
 
120
                /// If angle2 is greater than angle1 it will be progressively decreased
 
121
                /// by 1 degree until it is less than angle1.
 
122
                /// If there is a current point, an initial line segment will be added to the path 
 
123
                /// to connect the current point to the beginning of the arc. 
 
124
                /// If this initial line is undesired, 
 
125
                /// it can be avoided by calling NewPath() before calling ArcNegative().
 
126
                /// </summary>
 
127
                /// <param name='xc'>
 
128
                /// Xc.
 
129
                /// </param>
 
130
                /// <param name='yc'>
 
131
                /// Yc.
 
132
                /// </param>
 
133
                /// <param name='radius'>
 
134
                /// Radius.
 
135
                /// </param>
 
136
                /// <param name='angle1'>
 
137
                /// Angle1 in degrees
 
138
                /// </param>
 
139
                /// <param name='angle2'>
 
140
                /// Angle2 in degrees
 
141
                /// </param>
 
142
                public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
 
143
                {
 
144
                        handler.ArcNegative (Backend, xc, yc, radius, angle1, angle2);
 
145
                }
 
146
                
 
147
                /// <summary>
 
148
                /// Establishes a new clip region by intersecting the current clip region with the current Path 
 
149
                /// as it would be filled by fill() and according to the current fill rule.
 
150
                /// After clip(), the current path will be cleared from the Context.
 
151
                /// The current clip region affects all drawing operations by effectively masking out any changes to the surface 
 
152
                /// that are outside the current clip region.
 
153
                /// Calling clip() can only make the clip region smaller, never larger. 
 
154
                /// But the current clip is part of the graphics state, 
 
155
                /// so a temporary restriction of the clip region can be achieved by calling clip() within a save()/restore() pair. 
 
156
                /// The only other means of increasing the size of the clip region is reset_clip().
 
157
                /// </summary>
 
158
                public void Clip ()
 
159
                {
 
160
                        handler.Clip (Backend);
 
161
                }
 
162
                
 
163
                public void ClipPreserve ()
 
164
                {
 
165
                        handler.ClipPreserve (Backend);
 
166
                }
 
167
                
 
168
                public void ClosePath ()
 
169
                {
 
170
                        handler.ClosePath (Backend);
 
171
                }
 
172
                
 
173
                public void CurveTo (Point p1, Point p2, Point p3)
 
174
                {
 
175
                        CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
 
176
                }
 
177
                
 
178
                /// <summary>
 
179
                /// Adds a cubic Bezier spline to the path from the current point to position (x3, y3) in user-space coordinates, 
 
180
                /// using (x1, y1) and (x2, y2) as the control points. 
 
181
                /// </summary>
 
182
                /// <param name='x1'>
 
183
                /// X1.
 
184
                /// </param>
 
185
                /// <param name='y1'>
 
186
                /// Y1.
 
187
                /// </param>
 
188
                /// <param name='x2'>
 
189
                /// X2.
 
190
                /// </param>
 
191
                /// <param name='y2'>
 
192
                /// Y2.
 
193
                /// </param>
 
194
                /// <param name='x3'>
 
195
                /// X3.
 
196
                /// </param>
 
197
                /// <param name='y3'>
 
198
                /// Y3.
 
199
                /// </param>
 
200
                public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
 
201
                {
 
202
                        handler.CurveTo (Backend, x1, y1, x2, y2, x3, y3);
 
203
                }
 
204
                
 
205
                public void Fill ()
 
206
                {
 
207
                        handler.Fill (Backend);
 
208
                }
 
209
                
 
210
                public void FillPreserve ()
 
211
                {
 
212
                        handler.FillPreserve (Backend);
 
213
                }
 
214
                
 
215
                public void LineTo (Point p)
 
216
                {
 
217
                        LineTo (p.X, p.Y);
 
218
                }
 
219
                
 
220
                public void LineTo (double x, double y)
 
221
                {
 
222
                        handler.LineTo (Backend, x, y);
 
223
                }
 
224
                
 
225
                public void MoveTo (Point p)
 
226
                {
 
227
                        MoveTo (p.X, p.Y);
 
228
                }
 
229
                
 
230
                /// <summary>
 
231
                /// If the current subpath is not empty, begin a new subpath.
 
232
                /// After this call the current point will be (x, y).
 
233
                /// </summary>
 
234
                /// <param name='x'>
 
235
                /// X.
 
236
                /// </param>
 
237
                /// <param name='y'>
 
238
                /// Y.
 
239
                /// </param>
 
240
                public void MoveTo (double x, double y)
 
241
                {
 
242
                        handler.MoveTo (Backend, x, y);
 
243
                }
 
244
                
 
245
                public void NewPath ()
 
246
                {
 
247
                        handler.NewPath (Backend);
 
248
                }
 
249
                
 
250
                public void Rectangle (Rectangle rectangle)
 
251
                {
 
252
                        Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
 
253
                }
 
254
                
 
255
                public void Rectangle (Point p, double width, double height)
 
256
                {
 
257
                        Rectangle (p.X, p.Y, width, height);
 
258
                }
 
259
                
 
260
                public void Rectangle (double x, double y, double width, double height)
 
261
                {
 
262
                        handler.Rectangle (Backend, x, y, width, height);
 
263
                }
 
264
                
 
265
                public void RelCurveTo (Distance d1, Distance d2, Distance d3)
 
266
                {
 
267
                        RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy);
 
268
                }
 
269
                
 
270
                /// <summary>
 
271
                /// Relative-coordinate version of curve_to().
 
272
                /// All offsets are relative to the current point. 
 
273
                /// Adds a cubic Bezier spline to the path from the current point to a point offset 
 
274
                /// from the current point by (dx3, dy3), using points offset by (dx1, dy1) and (dx2, dy2) 
 
275
                /// as the control points. After this call the current point will be offset by (dx3, dy3).
 
276
                /// Given a current point of (x, y), RelCurveTo(dx1, dy1, dx2, dy2, dx3, dy3)
 
277
                /// is logically equivalent to CurveTo(x + dx1, y + dy1, x + dx2, y + dy2, x + dx3, y + dy3).
 
278
                /// </summary>
 
279
                /// <param name='dx1'>
 
280
                /// Dx1.
 
281
                /// </param>
 
282
                /// <param name='dy1'>
 
283
                /// Dy1.
 
284
                /// </param>
 
285
                /// <param name='dx2'>
 
286
                /// Dx2.
 
287
                /// </param>
 
288
                /// <param name='dy2'>
 
289
                /// Dy2.
 
290
                /// </param>
 
291
                /// <param name='dx3'>
 
292
                /// Dx3.
 
293
                /// </param>
 
294
                /// <param name='dy3'>
 
295
                /// Dy3.
 
296
                /// </param>
 
297
                public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
 
298
                {
 
299
                        handler.RelCurveTo (Backend, dx1, dy1, dx2, dy2, dx3, dy3);
 
300
                }
 
301
                
 
302
                public void RelLineTo (Distance d)
 
303
                {
 
304
                        RelLineTo (d.Dx, d.Dy);
 
305
                }
 
306
                
 
307
                /// <summary>
 
308
                /// Adds a line to the path from the current point to a point that 
 
309
                /// is offset from the current point by (dx, dy) in user space. 
 
310
                /// After this call the current point will be offset by (dx, dy).
 
311
                /// Given a current point of (x, y), 
 
312
                /// RelLineTo(dx, dy) is logically equivalent to LineTo(x + dx, y + dy).
 
313
                /// </summary>
 
314
                /// <param name='dx'>
 
315
                /// Dx.
 
316
                /// </param>
 
317
                /// <param name='dy'>
 
318
                /// Dy.
 
319
                /// </param>
 
320
                public void RelLineTo (double dx, double dy)
 
321
                {
 
322
                        handler.RelLineTo (Backend, dx, dy);
 
323
                }
 
324
                
 
325
                /// <summary>
 
326
                /// If the current subpath is not empty, begin a new subpath.
 
327
                /// After this call the current point will offset by (x, y).
 
328
                /// Given a current point of (x, y), 
 
329
                /// RelMoveTo(dx, dy) is logically equivalent to MoveTo(x + dx, y + dy).
 
330
                /// </summary>
 
331
                /// <param name='d'>
 
332
                /// D.
 
333
                /// </param>
 
334
                public void RelMoveTo (Distance d)
 
335
                {
 
336
                        RelMoveTo (d.Dx, d.Dy);
 
337
                }
 
338
                
 
339
                public void RelMoveTo (double dx, double dy)
 
340
                {
 
341
                        handler.RelMoveTo (Backend, dx, dy);
 
342
                }
 
343
                
 
344
                public void Stroke ()
 
345
                {
 
346
                        handler.Stroke (Backend);
 
347
                }
 
348
                
 
349
                public void StrokePreserve ()
 
350
                {
 
351
                        handler.StrokePreserve (Backend);
 
352
                }
 
353
                
 
354
                public void SetLineWidth (double width)
 
355
                {
 
356
                        handler.SetLineWidth (Backend, width);
 
357
                }
 
358
                
 
359
                public void DrawTextLayout (TextLayout layout, Point location)
 
360
                {
 
361
                        handler.DrawTextLayout (Backend, layout, location.X, location.Y);
 
362
                }
 
363
                
 
364
                public void DrawTextLayout (TextLayout layout, double x, double y)
 
365
                {
 
366
                        handler.DrawTextLayout (Backend, layout, x, y);
 
367
                }
 
368
                
 
369
                public void DrawImage (Image img, Point location)
 
370
                {
 
371
                        handler.DrawImage (Backend, GetBackend (img), location.X, location.Y, 1);
 
372
                }
 
373
                
 
374
                public void DrawImage (Image img, double x, double y)
 
375
                {
 
376
                        handler.DrawImage (Backend, GetBackend (img), x, y, 1);
 
377
                }
 
378
                
 
379
                public void DrawImage (Image img, Point location, double alpha)
 
380
                {
 
381
                        handler.DrawImage (Backend, GetBackend (img), location.X, location.Y, alpha);
 
382
                }
 
383
                
 
384
                public void DrawImage (Image img, double x, double y, double alpha)
 
385
                {
 
386
                        handler.DrawImage (Backend, GetBackend (img), x, y, alpha);
 
387
                }
 
388
                
 
389
                public void DrawImage (Image img, Rectangle rect)
 
390
                {
 
391
                        handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, 1);
 
392
                }
 
393
                
 
394
                public void DrawImage (Image img, double x, double y, double width, double height)
 
395
                {
 
396
                        handler.DrawImage (Backend, GetBackend (img), x, y, width, height, 1);
 
397
                }
 
398
                
 
399
                public void DrawImage (Image img, Rectangle rect, double alpha)
 
400
                {
 
401
                        handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, alpha);
 
402
                }
 
403
 
 
404
                public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect)
 
405
                {
 
406
                        handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, 1);
 
407
                }
 
408
 
 
409
                public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect, double alpha)
 
410
                {
 
411
                        handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, alpha);
 
412
                }
 
413
 
 
414
                /// <summary>
 
415
                /// Applies a rotation transformation
 
416
                /// </summary>
 
417
                /// <param name='angle'>
 
418
                /// Angle in degrees
 
419
                /// </param>
 
420
                /// <remarks>
 
421
                /// Modifies the current transformation matrix (CTM) by rotating the user-space axes by angle degrees.
 
422
                /// The rotation of the axes takes places after any existing transformation of user space.
 
423
                /// The rotation direction for positive angles is from the positive X axis toward the positive Y axis.
 
424
                /// </remarks>
 
425
                /// </summary>
 
426
                public void Rotate (double angle)
 
427
                {
 
428
                        handler.Rotate (Backend, angle);
 
429
                }
 
430
                
 
431
                public void Scale (double scaleX, double scaleY)
 
432
                {
 
433
                        handler.Scale (Backend, scaleX, scaleY);
 
434
                }
 
435
                
 
436
                public void Translate (double tx, double ty)
 
437
                {
 
438
                        handler.Translate (Backend, tx, ty);
 
439
                }
 
440
                
 
441
                public void Translate (Point p)
 
442
                {
 
443
                        handler.Translate (Backend, p.X, p.Y);
 
444
                }
 
445
                
 
446
                /// <summary>
 
447
                /// Transforms the point (x, y) by the current transformation matrix (CTM)
 
448
                /// </summary>
 
449
                public void TransformPoint (ref double x, ref double y)
 
450
                {
 
451
                        handler.TransformPoint (Backend, ref x, ref y);
 
452
                }
 
453
 
 
454
                /// <summary>
 
455
                /// Transforms the distance (dx, dy) by the scale and rotation elements (only) of the CTM
 
456
                /// </summary>
 
457
                public void TransformDistance (ref double dx, ref double dy)
 
458
                {
 
459
                        handler.TransformDistance (Backend, ref dx, ref dy);
 
460
                }
 
461
 
 
462
                /// <summary>
 
463
                /// Transforms the array of points by the current transformation matrix (CTM)
 
464
                /// </summary>
 
465
                public void TransformPoints (Point[] points)
 
466
                {
 
467
                        handler.TransformPoints (Backend, points);
 
468
                }
 
469
 
 
470
                /// <summary>
 
471
                /// Transforms the array of distances by the scale and rotation elements (only) of the CTM
 
472
                /// </summary>
 
473
                public void TransformDistances (Distance[] vectors)
 
474
                {
 
475
                        handler.TransformDistances (Backend, vectors);
 
476
                }
 
477
 
 
478
                public void Dispose ()
 
479
                {
 
480
                        handler.Dispose (Backend);
 
481
                }
 
482
                
 
483
                public Pattern Pattern {
 
484
                        get { return pattern; }
 
485
                        set {
 
486
                                pattern = value;
 
487
                                handler.SetPattern (Backend, GetBackend (value));
 
488
                        }
 
489
                }
 
490
                
 
491
                public Font Font {
 
492
                        get { return font; }
 
493
                        set {
 
494
                                font = value;
 
495
                                handler.SetFont (Backend, value);
 
496
                        }
 
497
                }
 
498
                
 
499
                /// <summary>
 
500
                /// Sets the dash pattern to be used by stroke().
 
501
                /// A dash pattern is specified by dashes, an array of positive values. 
 
502
                /// Each value provides the user-space length of altenate "on" and "off" portions of the stroke. 
 
503
                /// The offset specifies an offset into the pattern at which the stroke begins.
 
504
                /// If dashes is empty dashing is disabled. If the size of dashes is 1, 
 
505
                /// a symmetric pattern is assumed with alternating on and off portions of the size specified by the single value in dashes.
 
506
                /// It is invalid for any value in dashes to be negative, or for all values to be 0. 
 
507
                /// If this is the case, an exception will be thrown
 
508
                /// </summary>
 
509
                /// <param name='offset'>
 
510
                /// Offset.
 
511
                /// </param>
 
512
                /// <param name='pattern'>
 
513
                /// Pattern.
 
514
                /// </param>
 
515
                public void SetLineDash (double offset, params double[] pattern)
 
516
                {
 
517
                        handler.SetLineDash (Backend, offset, pattern);
 
518
                }
 
519
        }
 
520
}
 
521