2
// ContextBackendHandler.cs
5
// Lluis Sanchez <lluis@xamarin.com>
6
// Hywel Thomas <hywel.w.thomas@gmail.com>
8
// Copyright (c) 2011 Xamarin Inc
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:
17
// The above copyright notice and this permission notice shall be included in
18
// all copies or substantial portions of the Software.
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
33
namespace Xwt.CairoBackend
35
class CairoContextBackend : IDisposable
37
public double GlobalAlpha = 1;
38
public Cairo.Context Context;
39
public Cairo.Surface TempSurface;
41
public void Dispose ()
43
IDisposable d = Context;
54
public class CairoContextBackendHandler: IContextBackendHandler
56
public CairoContextBackendHandler ()
60
#region IContextBackendHandler implementation
62
public void Save (object backend)
64
CairoContextBackend gc = (CairoContextBackend)backend;
68
public void Restore (object backend)
70
CairoContextBackend gc = (CairoContextBackend)backend;
71
gc.Context.Restore ();
74
public void SetGlobalAlpha (object backend, double alpha)
76
CairoContextBackend gc = (CairoContextBackend) backend;
77
gc.GlobalAlpha = alpha;
80
const double degrees = System.Math.PI / 180d;
82
public void Arc (object backend, double xc, double yc, double radius, double angle1, double angle2)
84
CairoContextBackend gc = (CairoContextBackend)backend;
85
gc.Context.Arc (xc, yc, radius, angle1 * degrees, angle2 * degrees);
88
public void ArcNegative (object backend, double xc, double yc, double radius, double angle1, double angle2)
90
CairoContextBackend gc = (CairoContextBackend)backend;
91
gc.Context.ArcNegative (xc, yc, radius, angle1 * degrees, angle2 * degrees);
94
public void Clip (object backend)
96
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
100
public void ClipPreserve (object backend)
102
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
106
public void ClosePath (object backend)
108
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
112
public void CurveTo (object backend, double x1, double y1, double x2, double y2, double x3, double y3)
114
CairoContextBackend gc = (CairoContextBackend) backend;
115
gc.Context.CurveTo (x1, y1, x2, y2, x3, y3);
118
public void Fill (object backend)
120
var gtkc = (CairoContextBackend) backend;
121
Cairo.Context ctx = gtkc.Context;
122
if (gtkc.GlobalAlpha == 1)
127
ctx.PopGroupToSource ();
128
ctx.PaintWithAlpha (gtkc.GlobalAlpha);
132
public void FillPreserve (object backend)
134
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
138
public void LineTo (object backend, double x, double y)
140
CairoContextBackend gc = (CairoContextBackend) backend;
141
gc.Context.LineTo (x, y);
144
public void MoveTo (object backend, double x, double y)
146
CairoContextBackend gc = (CairoContextBackend) backend;
147
gc.Context.MoveTo (x, y);
150
public void NewPath (object backend)
152
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
156
public void Rectangle (object backend, double x, double y, double width, double height)
158
CairoContextBackend gc = (CairoContextBackend) backend;
159
gc.Context.Rectangle (x, y, width, height);
162
public void RelCurveTo (object backend, double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
164
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
165
ctx.RelCurveTo (dx1, dy1, dx2, dy2, dx3, dy3);
168
public void RelLineTo (object backend, double dx, double dy)
170
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
171
ctx.RelLineTo (dx, dy);
174
public void RelMoveTo (object backend, double dx, double dy)
176
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
177
ctx.RelMoveTo (dx, dy);
180
public void Stroke (object backend)
182
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
186
public void StrokePreserve (object backend)
188
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
189
ctx.StrokePreserve ();
192
public void SetColor (object backend, Xwt.Drawing.Color color)
194
var gtkContext = (CairoContextBackend) backend;
195
gtkContext.Context.Color = new Cairo.Color (color.Red, color.Green, color.Blue, color.Alpha * gtkContext.GlobalAlpha);
198
public void SetLineWidth (object backend, double width)
200
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
201
ctx.LineWidth = width;
204
public void SetLineDash (object backend, double offset, params double[] pattern)
206
Cairo.Context ctx = ((CairoContextBackend) backend).Context;
207
ctx.SetDash (pattern, offset);
210
public void SetPattern (object backend, object p)
212
Cairo.Context ctx = ((CairoContextBackend)backend).Context;
214
ctx.Pattern = (Cairo.Pattern) p;
219
public void SetFont (object backend, Font font)
223
public virtual void DrawTextLayout (object backend, TextLayout layout, double x, double y)
225
Cairo.Context ctx = ((CairoContextBackend)backend).Context;
226
var lb = WidgetRegistry.GetBackend (layout);
227
CairoTextLayoutBackendHandler.Draw (ctx, lb, x, y);
230
public void DrawImage (object backend, object img, double x, double y, double alpha)
232
CairoContextBackend ctx = (CairoContextBackend)backend;
233
SetSourceImage (ctx.Context, img, x, y);
234
alpha = alpha * ctx.GlobalAlpha;
236
ctx.Context.Paint ();
238
ctx.Context.PaintWithAlpha (alpha);
241
protected virtual void SetSourceImage (Cairo.Context ctx, object img, double x, double y)
245
public void DrawImage (object backend, object img, double x, double y, double width, double height, double alpha)
247
CairoContextBackend ctx = (CairoContextBackend)backend;
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;
257
ctx.Context.Paint ();
259
ctx.Context.PaintWithAlpha (alpha);
260
ctx.Context.Restore ();
263
public void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha)
265
CairoContextBackend ctx = (CairoContextBackend)backend;
267
ctx.Context.NewPath();
268
ctx.Context.Rectangle (destRect.X, destRect.Y, destRect.Width, destRect.Height);
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;
277
ctx.Context.Paint ();
279
ctx.Context.PaintWithAlpha (alpha);
280
ctx.Context.Restore ();
283
protected virtual Size GetImageSize (object img)
285
return new Size (0,0);
288
public void Rotate (object backend, double angle)
290
CairoContextBackend gc = (CairoContextBackend)backend;
291
gc.Context.Rotate ((angle * System.Math.PI) / 180);
294
public void Scale (object backend, double scaleX, double scaleY)
296
CairoContextBackend gc = (CairoContextBackend)backend;
297
gc.Context.Scale (scaleX, scaleY);
300
public void Translate (object backend, double tx, double ty)
302
CairoContextBackend gc = (CairoContextBackend)backend;
303
gc.Context.Translate (tx, ty);
306
public void TransformPoint (object backend, ref double x, ref double y)
308
Cairo.Context ctx = ((CairoContextBackend)backend).Context;
309
ctx.TransformPoint (ref x, ref y);
312
public void TransformDistance (object backend, ref double dx, ref double dy)
314
Cairo.Context ctx = ((CairoContextBackend)backend).Context;
315
ctx.TransformDistance (ref dx, ref dy);
318
public void TransformPoints (object backend, Point[] points)
320
Cairo.Context ctx = ((CairoContextBackend)backend).Context;
323
for (int i = 0; i < points.Length; ++i) {
326
ctx.TransformPoint (ref x, ref y);
332
public void TransformDistances (object backend, Distance[] vectors)
334
Cairo.Context ctx = ((CairoContextBackend)backend).Context;
337
for (int i = 0; i < vectors.Length; ++i) {
340
ctx.TransformDistance (ref x, ref y);
346
public void Dispose (object backend)
348
var ctx = (CairoContextBackend) backend;