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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/ContextBackendHandler.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
// ContextBackendHandler.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
//       Alex Corrado <corrado@xamarin.com>
 
7
//
 
8
// Copyright (c) 2011 Xamarin Inc
 
9
// 
 
10
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
11
// of this software and associated documentation files (the "Software"), to deal
 
12
// in the Software without restriction, including without limitation the rights
 
13
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
14
// copies of the Software, and to permit persons to whom the Software is
 
15
// furnished to do so, subject to the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be included in
 
18
// all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
22
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
23
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
24
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
25
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
26
// THE SOFTWARE.
 
27
 
 
28
using System;
 
29
using Xwt.Backends;
 
30
using Xwt.Engine;
 
31
using MonoMac.AppKit;
 
32
using Xwt.Drawing;
 
33
using MonoMac.Foundation;
 
34
using MonoMac.CoreGraphics;
 
35
using System.Drawing;
 
36
 
 
37
namespace Xwt.Mac
 
38
{
 
39
        class CGContextBackend {
 
40
                public CGContext Context;
 
41
                public SizeF Size;
 
42
                public GradientInfo Gradient;
 
43
                public CGAffineTransform? InverseViewTransform;
 
44
        }
 
45
 
 
46
        public class ContextBackendHandler: IContextBackendHandler
 
47
        {
 
48
                const double degrees = System.Math.PI / 180d;
 
49
 
 
50
                public void Save (object backend)
 
51
                {
 
52
                        ((CGContextBackend)backend).Context.SaveState ();
 
53
                }
 
54
                
 
55
                public void Restore (object backend)
 
56
                {
 
57
                        ((CGContextBackend)backend).Context.RestoreState ();
 
58
                }
 
59
 
 
60
                public void SetGlobalAlpha (object backend, double alpha)
 
61
                {
 
62
                        ((CGContextBackend)backend).Context.SetAlpha ((float)alpha);
 
63
                }
 
64
 
 
65
                public void Arc (object backend, double xc, double yc, double radius, double angle1, double angle2)
 
66
                {
 
67
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
68
                        ctx.AddArc ((float)xc, (float)yc, (float)radius, (float)(angle1 * degrees), (float)(angle2 * degrees), false);
 
69
                }
 
70
 
 
71
                public void ArcNegative (object backend, double xc, double yc, double radius, double angle1, double angle2)
 
72
                {
 
73
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
74
                        ctx.AddArc ((float)xc, (float)yc, (float)radius, (float)(angle1 * degrees), (float)(angle2 * degrees), true);
 
75
                }
 
76
 
 
77
                public void Clip (object backend)
 
78
                {
 
79
                        ((CGContextBackend)backend).Context.Clip ();
 
80
                }
 
81
 
 
82
                public void ClipPreserve (object backend)
 
83
                {
 
84
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
85
                        using (CGPath oldPath = ctx.CopyPath ()) {
 
86
                                ctx.Clip ();
 
87
                                ctx.AddPath (oldPath);
 
88
                        }
 
89
                }
 
90
                
 
91
                public void ClosePath (object backend)
 
92
                {
 
93
                        ((CGContextBackend)backend).Context.ClosePath ();
 
94
                }
 
95
 
 
96
                public void CurveTo (object backend, double x1, double y1, double x2, double y2, double x3, double y3)
 
97
                {
 
98
                        ((CGContextBackend)backend).Context.AddCurveToPoint ((float)x1, (float)y1, (float)x2, (float)y2, (float)x3, (float)y3);
 
99
                }
 
100
 
 
101
                public void Fill (object backend)
 
102
                {
 
103
                        CGContextBackend gc = (CGContextBackend)backend;
 
104
                        CGContext ctx = gc.Context;
 
105
                        SetupContextForDrawing (ctx);
 
106
 
 
107
                        if (gc.Gradient != null)
 
108
                                GradientBackendHandler.Draw (ctx, gc.Gradient);
 
109
                        else
 
110
                                ctx.DrawPath (CGPathDrawingMode.Fill);
 
111
                }
 
112
 
 
113
                public void FillPreserve (object backend)
 
114
                {
 
115
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
116
                        using (CGPath oldPath = ctx.CopyPath ()) {
 
117
                                Fill (backend);
 
118
                                ctx.AddPath (oldPath);
 
119
                        }
 
120
                }
 
121
 
 
122
                public void LineTo (object backend, double x, double y)
 
123
                {
 
124
                        ((CGContextBackend)backend).Context.AddLineToPoint ((float)x, (float)y);
 
125
                }
 
126
 
 
127
                public void MoveTo (object backend, double x, double y)
 
128
                {
 
129
                        ((CGContextBackend)backend).Context.MoveTo ((float)x, (float)y);
 
130
                }
 
131
 
 
132
                public void NewPath (object backend)
 
133
                {
 
134
                        ((CGContextBackend)backend).Context.BeginPath ();
 
135
                }
 
136
 
 
137
                public void Rectangle (object backend, double x, double y, double width, double height)
 
138
                {
 
139
                        ((CGContextBackend)backend).Context.AddRect (new RectangleF ((float)x, (float)y, (float)width, (float)height));
 
140
                }
 
141
 
 
142
                public void RelCurveTo (object backend, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
 
143
                {
 
144
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
145
                        PointF p = ctx.GetPathCurrentPoint ();
 
146
                        ctx.AddCurveToPoint ((float)(p.X + dx1), (float)(p.Y + dy1), (float)(p.X + dx2), (float)(p.Y + dy2), (float)(p.X + dx3), (float)(p.Y + dy3));
 
147
                }
 
148
 
 
149
                public void RelLineTo (object backend, double dx, double dy)
 
150
                {
 
151
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
152
                        PointF p = ctx.GetPathCurrentPoint ();
 
153
                        ctx.AddLineToPoint ((float)(p.X + dx), (float)(p.Y + dy));
 
154
                }
 
155
 
 
156
                public void RelMoveTo (object backend, double dx, double dy)
 
157
                {
 
158
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
159
                        PointF p = ctx.GetPathCurrentPoint ();
 
160
                        ctx.MoveTo ((float)(p.X + dx), (float)(p.Y + dy));
 
161
                }
 
162
 
 
163
                public void Stroke (object backend)
 
164
                {
 
165
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
166
                        SetupContextForDrawing (ctx);
 
167
                        ctx.DrawPath (CGPathDrawingMode.Stroke);
 
168
                }
 
169
 
 
170
                public void StrokePreserve (object backend)
 
171
                {
 
172
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
173
                        SetupContextForDrawing (ctx);
 
174
                        using (CGPath oldPath = ctx.CopyPath ()) {
 
175
                                ctx.DrawPath (CGPathDrawingMode.Stroke);
 
176
                                ctx.AddPath (oldPath);
 
177
                        }
 
178
                }
 
179
                
 
180
                public void SetColor (object backend, Xwt.Drawing.Color color)
 
181
                {
 
182
                        CGContextBackend gc = (CGContextBackend)backend;
 
183
                        gc.Gradient = null;
 
184
                        CGContext ctx = gc.Context;
 
185
                        ctx.SetFillColorSpace (Util.DeviceRGBColorSpace);
 
186
                        ctx.SetStrokeColorSpace (Util.DeviceRGBColorSpace);
 
187
                        ctx.SetFillColor ((float)color.Red, (float)color.Green, (float)color.Blue, (float)color.Alpha);
 
188
                        ctx.SetStrokeColor ((float)color.Red, (float)color.Green, (float)color.Blue, (float)color.Alpha);
 
189
                }
 
190
                
 
191
                public void SetLineWidth (object backend, double width)
 
192
                {
 
193
                        ((CGContextBackend)backend).Context.SetLineWidth ((float)width);
 
194
                }
 
195
                
 
196
                public void SetLineDash (object backend, double offset, params double[] pattern)
 
197
                {
 
198
                        float[] array = new float[pattern.Length];
 
199
                        for (int n=0; n<pattern.Length; n++)
 
200
                                array [n] = (float) pattern[n];
 
201
                        if (array.Length == 0)
 
202
                                array = new float [] { 1 };
 
203
                        ((CGContextBackend)backend).Context.SetLineDash ((float)offset, array);
 
204
                }
 
205
                
 
206
                public void SetPattern (object backend, object p)
 
207
                {
 
208
                        CGContextBackend gc = (CGContextBackend)backend;
 
209
                        gc.Gradient = p as GradientInfo;
 
210
                        if (gc.Gradient != null || !(p is CGPattern))
 
211
                                return;
 
212
                        CGContext ctx = gc.Context;
 
213
                        CGPattern pattern = (CGPattern)p;
 
214
                        float[] alpha = new[] { 1.0f };
 
215
                        ctx.SetFillColorSpace (Util.PatternColorSpace);
 
216
                        ctx.SetStrokeColorSpace (Util.PatternColorSpace);
 
217
                        ctx.SetFillPattern (pattern, alpha);
 
218
                        ctx.SetStrokePattern (pattern, alpha);
 
219
                }
 
220
                
 
221
                public void SetFont (object backend, Xwt.Drawing.Font font)
 
222
                {
 
223
                        ((CGContextBackend)backend).Context.SelectFont (font.Family, (float)font.Size, CGTextEncoding.FontSpecific);
 
224
                }
 
225
                
 
226
                public void DrawTextLayout (object backend, TextLayout layout, double x, double y)
 
227
                {
 
228
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
229
                        SetupContextForDrawing (ctx);
 
230
                        TextLayoutBackendHandler.Draw (ctx, WidgetRegistry.GetBackend (layout), x, y);
 
231
                }
 
232
                
 
233
                public void DrawImage (object backend, object img, double x, double y, double alpha)
 
234
                {
 
235
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
236
                        NSImage image = (NSImage)img;
 
237
                        var rect = new RectangleF (PointF.Empty, image.Size);
 
238
                        ctx.SaveState ();
 
239
                        ctx.SetAlpha ((float)alpha);
 
240
                        ctx.TranslateCTM ((float)x, (float)y + rect.Height);
 
241
                        ctx.ScaleCTM (1f, -1f);
 
242
                        ctx.DrawImage (rect, image.AsCGImage (RectangleF.Empty, null, null));
 
243
                        ctx.RestoreState ();
 
244
                }
 
245
                
 
246
                public void DrawImage (object backend, object img, double x, double y, double width, double height, double alpha)
 
247
                {
 
248
                        var srcRect = new Rectangle (Point.Zero, ((NSImage)img).Size.ToXwtSize ());
 
249
                        var destRect = new Rectangle (x, y, width, height);
 
250
                        DrawImage (backend, img, srcRect, destRect, alpha);
 
251
                }
 
252
 
 
253
                public void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha)
 
254
                {
 
255
                        CGContext ctx = ((CGContextBackend)backend).Context;
 
256
                        NSImage image = (NSImage) img;
 
257
                        var rect = new RectangleF (0, 0, (float)destRect.Width, (float)destRect.Height);
 
258
                        ctx.SaveState ();
 
259
                        ctx.SetAlpha ((float)alpha);
 
260
                        ctx.TranslateCTM ((float)destRect.X, (float)destRect.Y + rect.Height);
 
261
                        ctx.ScaleCTM (1f, -1f);
 
262
                        ctx.DrawImage (rect, image.AsCGImage (RectangleF.Empty, null, null).WithImageInRect (srcRect.ToRectangleF ()));
 
263
                        ctx.RestoreState ();
 
264
                }
 
265
                
 
266
                public void Rotate (object backend, double angle)
 
267
                {
 
268
                        ((CGContextBackend)backend).Context.RotateCTM ((float)(angle * degrees));
 
269
                }
 
270
                
 
271
                public void Scale (object backend, double scaleX, double scaleY)
 
272
                {
 
273
                        ((CGContextBackend)backend).Context.ScaleCTM ((float)scaleX, (float)scaleY);
 
274
                }
 
275
                
 
276
                public void Translate (object backend, double tx, double ty)
 
277
                {
 
278
                        ((CGContextBackend)backend).Context.TranslateCTM ((float)tx, (float)ty);
 
279
                }
 
280
                
 
281
                public void TransformPoint (object backend, ref double x, ref double y)
 
282
                {
 
283
                        CGAffineTransform t = GetContextTransform ((CGContextBackend)backend);
 
284
 
 
285
                        PointF p = t.TransformPoint (new PointF ((float)x, (float)y));
 
286
                        x = p.X;
 
287
                        y = p.Y;
 
288
                }
 
289
 
 
290
                public void TransformDistance (object backend, ref double dx, ref double dy)
 
291
                {
 
292
                        CGAffineTransform t = GetContextTransform ((CGContextBackend)backend);
 
293
                        // remove translational elements from CTM
 
294
                        t.x0 = 0;
 
295
                        t.y0 = 0;
 
296
 
 
297
                        PointF p = t.TransformPoint (new PointF ((float)dx, (float)dy));
 
298
                        dx = p.X;
 
299
                        dy = p.Y;
 
300
                }
 
301
 
 
302
                public void TransformPoints (object backend, Point[] points)
 
303
                {
 
304
                        CGAffineTransform t = GetContextTransform ((CGContextBackend)backend);
 
305
 
 
306
                        PointF p;
 
307
                        for (int i = 0; i < points.Length; ++i) {
 
308
                                p = t.TransformPoint (new PointF ((float)points[i].X, (float)points[i].Y));
 
309
                                points[i].X = p.X;
 
310
                                points[i].Y = p.Y;
 
311
                        }
 
312
                }
 
313
 
 
314
                public void TransformDistances (object backend, Distance[] vectors)
 
315
                {
 
316
                        CGAffineTransform t = GetContextTransform ((CGContextBackend)backend);
 
317
                        t.x0 = 0;
 
318
                        t.y0 = 0;
 
319
                        PointF p;
 
320
                        for (int i = 0; i < vectors.Length; ++i) {
 
321
                                p = t.TransformPoint (new PointF ((float)vectors[i].Dx, (float)vectors[i].Dy));
 
322
                                vectors[i].Dx = p.X;
 
323
                                vectors[i].Dy = p.Y;
 
324
                        }
 
325
                }
 
326
 
 
327
                public void Dispose (object backend)
 
328
                {
 
329
                        ((CGContextBackend)backend).Context.Dispose ();
 
330
                }
 
331
 
 
332
                static CGAffineTransform GetContextTransform (CGContextBackend gc)
 
333
                {
 
334
                        CGAffineTransform t = gc.Context.GetCTM ();
 
335
 
 
336
                        // The CTM returned above actually includes the full view transform.
 
337
                        //  We only want the transform that is applied to the context, so concat
 
338
                        //  the inverse of the view transform to nullify that part.
 
339
                        if (gc.InverseViewTransform.HasValue)
 
340
                                t.Multiply (gc.InverseViewTransform.Value);
 
341
 
 
342
                        return t;
 
343
                }
 
344
 
 
345
                static void SetupContextForDrawing (CGContext ctx)
 
346
                {
 
347
                        if (ctx.IsPathEmpty ())
 
348
                                return;
 
349
 
 
350
                        // setup pattern drawing to better match the behavior of Cairo
 
351
                        var drawPoint = ctx.GetCTM ().TransformPoint (ctx.GetPathBoundingBox ().Location);
 
352
                        var patternPhase = new SizeF (drawPoint.X, drawPoint.Y);
 
353
                        if (patternPhase != SizeF.Empty)
 
354
                                ctx.SetPatternPhase (patternPhase);
 
355
                }
 
356
        }
 
357
}
 
358