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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.CairoBackend/CairoContextBackendHandler.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
//       Hywel Thomas <hywel.w.thomas@gmail.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 Xwt.Drawing;
 
32
 
 
33
namespace Xwt.CairoBackend
 
34
{
 
35
        class CairoContextBackend : IDisposable
 
36
        {
 
37
                public double GlobalAlpha = 1;
 
38
                public Cairo.Context Context;
 
39
                public Cairo.Surface TempSurface;
 
40
 
 
41
                public void Dispose ()
 
42
                {
 
43
                        IDisposable d = Context;
 
44
                        if (d != null) {
 
45
                                d.Dispose ();
 
46
                        }
 
47
                        d = TempSurface;
 
48
                        if (d != null) {
 
49
                                d.Dispose ();
 
50
                        }
 
51
                }
 
52
        }
 
53
        
 
54
        public class CairoContextBackendHandler: IContextBackendHandler
 
55
        {
 
56
                public CairoContextBackendHandler ()
 
57
                {
 
58
                }
 
59
 
 
60
                #region IContextBackendHandler implementation
 
61
                
 
62
                public void Save (object backend)
 
63
                {
 
64
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
65
                        gc.Context.Save ();
 
66
                }
 
67
                
 
68
                public void Restore (object backend)
 
69
                {
 
70
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
71
                        gc.Context.Restore ();
 
72
                }
 
73
                
 
74
                public void SetGlobalAlpha (object backend, double alpha)
 
75
                {
 
76
                        CairoContextBackend gc = (CairoContextBackend) backend;
 
77
                        gc.GlobalAlpha = alpha;
 
78
                }
 
79
                
 
80
                const double degrees = System.Math.PI / 180d;
 
81
 
 
82
                public void Arc (object backend, double xc, double yc, double radius, double angle1, double angle2)
 
83
                {
 
84
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
85
                        gc.Context.Arc (xc, yc, radius, angle1 * degrees, angle2 * degrees);
 
86
                }
 
87
 
 
88
                public void ArcNegative (object backend, double xc, double yc, double radius, double angle1, double angle2)
 
89
                {
 
90
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
91
                        gc.Context.ArcNegative (xc, yc, radius, angle1 * degrees, angle2 * degrees);
 
92
                }
 
93
 
 
94
                public void Clip (object backend)
 
95
                {
 
96
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
97
                        ctx.Clip ();
 
98
                }
 
99
 
 
100
                public void ClipPreserve (object backend)
 
101
                {
 
102
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
103
                        ctx.ClipPreserve ();
 
104
                }
 
105
 
 
106
                public void ClosePath (object backend)
 
107
                {
 
108
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
109
                        ctx.ClosePath ();
 
110
                }
 
111
 
 
112
                public void CurveTo (object backend, double x1, double y1, double x2, double y2, double x3, double y3)
 
113
                {
 
114
                        CairoContextBackend gc = (CairoContextBackend) backend;
 
115
                        gc.Context.CurveTo (x1, y1, x2, y2, x3, y3);
 
116
                }
 
117
 
 
118
                public void Fill (object backend)
 
119
                {
 
120
                        var gtkc = (CairoContextBackend) backend;
 
121
                        Cairo.Context ctx = gtkc.Context;
 
122
                        if (gtkc.GlobalAlpha == 1)
 
123
                                ctx.Fill ();
 
124
                        else {
 
125
                                ctx.PushGroup ();
 
126
                                ctx.Fill ();
 
127
                                ctx.PopGroupToSource ();
 
128
                                ctx.PaintWithAlpha (gtkc.GlobalAlpha);
 
129
                        }
 
130
                }
 
131
 
 
132
                public void FillPreserve (object backend)
 
133
                {
 
134
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
135
                        ctx.FillPreserve ();
 
136
                }
 
137
 
 
138
                public void LineTo (object backend, double x, double y)
 
139
                {
 
140
                        CairoContextBackend gc = (CairoContextBackend) backend;
 
141
                        gc.Context.LineTo (x, y);
 
142
                }
 
143
 
 
144
                public void MoveTo (object backend, double x, double y)
 
145
                {
 
146
                        CairoContextBackend gc = (CairoContextBackend) backend;
 
147
                        gc.Context.MoveTo (x, y);
 
148
                }
 
149
 
 
150
                public void NewPath (object backend)
 
151
                {
 
152
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
153
                        ctx.NewPath ();
 
154
                }
 
155
 
 
156
                public void Rectangle (object backend, double x, double y, double width, double height)
 
157
                {
 
158
                        CairoContextBackend gc = (CairoContextBackend) backend;
 
159
                        gc.Context.Rectangle (x, y, width, height);
 
160
                }
 
161
 
 
162
                public void RelCurveTo (object backend, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
 
163
                {
 
164
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
165
                        ctx.RelCurveTo (dx1, dy1, dx2, dy2, dx3, dy3);
 
166
                }
 
167
 
 
168
                public void RelLineTo (object backend, double dx, double dy)
 
169
                {
 
170
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
171
                        ctx.RelLineTo (dx, dy);
 
172
                }
 
173
 
 
174
                public void RelMoveTo (object backend, double dx, double dy)
 
175
                {
 
176
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
177
                        ctx.RelMoveTo (dx, dy);
 
178
                }
 
179
 
 
180
                public void Stroke (object backend)
 
181
                {
 
182
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
183
                        ctx.Stroke ();
 
184
                }
 
185
 
 
186
                public void StrokePreserve (object backend)
 
187
                {
 
188
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
189
                        ctx.StrokePreserve ();
 
190
                }
 
191
 
 
192
                public void SetColor (object backend, Xwt.Drawing.Color color)
 
193
                {
 
194
                        var gtkContext = (CairoContextBackend) backend;
 
195
                        gtkContext.Context.Color = new Cairo.Color (color.Red, color.Green, color.Blue, color.Alpha * gtkContext.GlobalAlpha);
 
196
                }
 
197
                
 
198
                public void SetLineWidth (object backend, double width)
 
199
                {
 
200
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
201
                        ctx.LineWidth = width;
 
202
                }
 
203
                
 
204
                public void SetLineDash (object backend, double offset, params double[] pattern)
 
205
                {
 
206
                        Cairo.Context ctx = ((CairoContextBackend) backend).Context;
 
207
                        ctx.SetDash (pattern, offset);
 
208
                }
 
209
                
 
210
                public void SetPattern (object backend, object p)
 
211
                {
 
212
                        Cairo.Context ctx = ((CairoContextBackend)backend).Context;
 
213
                        if (p != null)
 
214
                                ctx.Pattern = (Cairo.Pattern) p;
 
215
                        else
 
216
                                ctx.Pattern = null;
 
217
                }
 
218
                
 
219
                public void SetFont (object backend, Font font)
 
220
                {
 
221
                }
 
222
                
 
223
                public virtual void DrawTextLayout (object backend, TextLayout layout, double x, double y)
 
224
                {
 
225
                        Cairo.Context ctx = ((CairoContextBackend)backend).Context;
 
226
                        var lb = WidgetRegistry.GetBackend (layout);
 
227
                        CairoTextLayoutBackendHandler.Draw (ctx, lb, x, y);
 
228
                }
 
229
                
 
230
                public void DrawImage (object backend, object img, double x, double y, double alpha)
 
231
                {
 
232
                        CairoContextBackend ctx = (CairoContextBackend)backend;
 
233
                        SetSourceImage (ctx.Context, img, x, y);
 
234
                        alpha = alpha * ctx.GlobalAlpha;
 
235
                        if (alpha == 1)
 
236
                                ctx.Context.Paint ();
 
237
                        else
 
238
                                ctx.Context.PaintWithAlpha (alpha);
 
239
                }
 
240
                
 
241
                protected virtual void SetSourceImage (Cairo.Context ctx, object img, double x, double y)
 
242
                {
 
243
                }
 
244
                
 
245
                public void DrawImage (object backend, object img, double x, double y, double width, double height, double alpha)
 
246
                {
 
247
                        CairoContextBackend ctx = (CairoContextBackend)backend;
 
248
                        ctx.Context.Save ();
 
249
                        var s = GetImageSize (img);
 
250
                        double sx = ((double) width) / s.Width;
 
251
                        double sy = ((double) height) / s.Height;
 
252
                        ctx.Context.Translate (x, y);
 
253
                        ctx.Context.Scale (sx, sy);
 
254
                        SetSourceImage (ctx.Context, img, 0, 0);
 
255
                        alpha = alpha * ctx.GlobalAlpha;
 
256
                        if (alpha == 1)
 
257
                                ctx.Context.Paint ();
 
258
                        else
 
259
                                ctx.Context.PaintWithAlpha (alpha);
 
260
                        ctx.Context.Restore ();
 
261
                }
 
262
                
 
263
                public void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha)
 
264
                {
 
265
                        CairoContextBackend ctx = (CairoContextBackend)backend;
 
266
                        ctx.Context.Save ();
 
267
                        ctx.Context.NewPath();
 
268
                        ctx.Context.Rectangle (destRect.X, destRect.Y, destRect.Width, destRect.Height);
 
269
                        ctx.Context.Clip ();
 
270
                        ctx.Context.Translate (destRect.X-srcRect.X, destRect.Y-srcRect.Y);
 
271
                        double sx = destRect.Width / srcRect.Width;
 
272
                        double sy = destRect.Height / srcRect.Height;
 
273
                        ctx.Context.Scale (sx, sy);
 
274
                        SetSourceImage (ctx.Context, img, 0, 0);
 
275
                        alpha = alpha * ctx.GlobalAlpha;
 
276
                        if (alpha == 1)
 
277
                                ctx.Context.Paint ();
 
278
                        else
 
279
                                ctx.Context.PaintWithAlpha (alpha);
 
280
                        ctx.Context.Restore ();
 
281
                }
 
282
                
 
283
                protected virtual Size GetImageSize (object img)
 
284
                {
 
285
                        return new Size (0,0);
 
286
                }
 
287
                
 
288
        public void Rotate (object backend, double angle)
 
289
                {
 
290
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
291
                        gc.Context.Rotate ((angle * System.Math.PI) / 180);
 
292
                }
 
293
                
 
294
                public void Scale (object backend, double scaleX, double scaleY)
 
295
                {
 
296
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
297
                        gc.Context.Scale (scaleX, scaleY);
 
298
                }
 
299
                
 
300
                public void Translate (object backend, double tx, double ty)
 
301
                {
 
302
                        CairoContextBackend gc = (CairoContextBackend)backend;
 
303
                        gc.Context.Translate (tx, ty);
 
304
                }
 
305
 
 
306
                public void TransformPoint (object backend, ref double x, ref double y)
 
307
                {
 
308
                        Cairo.Context ctx = ((CairoContextBackend)backend).Context;
 
309
                        ctx.TransformPoint (ref x, ref y);
 
310
                }
 
311
 
 
312
                public void TransformDistance (object backend, ref double dx, ref double dy)
 
313
                {
 
314
                        Cairo.Context ctx = ((CairoContextBackend)backend).Context;
 
315
                        ctx.TransformDistance (ref dx, ref dy);
 
316
                }
 
317
 
 
318
                public void TransformPoints (object backend, Point[] points)
 
319
                {
 
320
                        Cairo.Context ctx = ((CairoContextBackend)backend).Context;
 
321
 
 
322
                        double x, y;
 
323
                        for (int i = 0; i < points.Length; ++i) {
 
324
                                x = points[i].X;
 
325
                                y = points[i].Y;
 
326
                                ctx.TransformPoint (ref x, ref y);
 
327
                                points[i].X = x;
 
328
                                points[i].Y = y;
 
329
                        }
 
330
                }
 
331
 
 
332
                public void TransformDistances (object backend, Distance[] vectors)
 
333
                {
 
334
                        Cairo.Context ctx = ((CairoContextBackend)backend).Context;
 
335
 
 
336
                        double x, y;
 
337
                        for (int i = 0; i < vectors.Length; ++i) {
 
338
                                x = vectors[i].Dx;
 
339
                                y = vectors[i].Dy;
 
340
                                ctx.TransformDistance (ref x, ref y);
 
341
                                vectors[i].Dx = x;
 
342
                                vectors[i].Dy = y;
 
343
                        }
 
344
                }
 
345
 
 
346
                public void Dispose (object backend)
 
347
                {
 
348
                        var ctx = (CairoContextBackend) backend;
 
349
                        ctx.Dispose ();
 
350
                }
 
351
                #endregion
 
352
        }
 
353
}
 
354