Update Cairo samples for the latest API changes

This commit is contained in:
Bertrand Lorentz 2013-07-26 17:39:23 -05:00
parent 796cd1e8a1
commit ebc5c048b4
2 changed files with 24 additions and 18 deletions

View File

@ -33,16 +33,19 @@ class CairoSample : DrawingArea
int CHECK_SIZE = 32; int CHECK_SIZE = 32;
cr.Save (); cr.Save ();
Surface check = cr.Target.CreateSimilar (Content.Color, 2 * CHECK_SIZE, 2 * CHECK_SIZE); Surface check;
using (var target = cr.GetTarget ()) {
check = target.CreateSimilar (Content.Color, 2 * CHECK_SIZE, 2 * CHECK_SIZE);
}
// draw the check // draw the check
using (Context cr2 = new Context (check)) { using (Context cr2 = new Context (check)) {
cr2.Operator = Operator.Source; cr2.Operator = Operator.Source;
cr2.Color = new Color (0.4, 0.4, 0.4); cr2.SetSourceRGB (0.4, 0.4, 0.4);
cr2.Rectangle (0, 0, 2 * CHECK_SIZE, 2 * CHECK_SIZE); cr2.Rectangle (0, 0, 2 * CHECK_SIZE, 2 * CHECK_SIZE);
cr2.Fill (); cr2.Fill ();
cr2.Color = new Color (0.7, 0.7, 0.7); cr2.SetSourceRGB (0.7, 0.7, 0.7);
cr2.Rectangle (x, y, CHECK_SIZE, CHECK_SIZE); cr2.Rectangle (x, y, CHECK_SIZE, CHECK_SIZE);
cr2.Fill (); cr2.Fill ();
@ -53,12 +56,12 @@ class CairoSample : DrawingArea
// Fill the whole surface with the check // Fill the whole surface with the check
SurfacePattern check_pattern = new SurfacePattern (check); SurfacePattern check_pattern = new SurfacePattern (check);
check_pattern.Extend = Extend.Repeat; check_pattern.Extend = Extend.Repeat;
cr.Source = check_pattern; cr.SetSource (check_pattern);
cr.Rectangle (0, 0, width, height); cr.Rectangle (0, 0, width, height);
cr.Fill (); cr.Fill ();
check_pattern.Destroy (); check_pattern.Dispose ();
check.Destroy (); check.Dispose ();
cr.Restore (); cr.Restore ();
} }
@ -66,16 +69,16 @@ class CairoSample : DrawingArea
{ {
double subradius = radius * (2 / 3.0 - 0.1); double subradius = radius * (2 / 3.0 - 0.1);
cr.Color = new Color (1.0, 0.0, 0.0, alpha); cr.SetSourceRGBA (1.0, 0.0, 0.0, alpha);
OvalPath (cr, xc + radius / 3.0 * Math.Cos (Math.PI * 0.5), yc - radius / 3.0 * Math.Sin (Math.PI * 0.5), subradius, subradius); OvalPath (cr, xc + radius / 3.0 * Math.Cos (Math.PI * 0.5), yc - radius / 3.0 * Math.Sin (Math.PI * 0.5), subradius, subradius);
cr.Fill (); cr.Fill ();
cr.Color = new Color (0.0, 1.0, 0.0, alpha); cr.SetSourceRGBA (0.0, 1.0, 0.0, alpha);
OvalPath (cr, xc + radius / 3.0 * Math.Cos (Math.PI * (0.5 + 2 / 0.3)), yc - radius / 3.0 * Math.Sin (Math.PI * (0.5 + 2 / 0.3)), subradius, subradius); OvalPath (cr, xc + radius / 3.0 * Math.Cos (Math.PI * (0.5 + 2 / 0.3)), yc - radius / 3.0 * Math.Sin (Math.PI * (0.5 + 2 / 0.3)), subradius, subradius);
cr.Fill (); cr.Fill ();
cr.Color = new Color (0.0, 0.0, 1.0, alpha); cr.SetSourceRGBA (0.0, 0.0, 1.0, alpha);
OvalPath (cr, xc + radius / 3.0 * Math.Cos (Math.PI * (0.5 + 4 / 0.3)), yc - radius / 3.0 * Math.Sin (Math.PI * (0.5 + 4 / 0.3)), subradius, subradius); OvalPath (cr, xc + radius / 3.0 * Math.Cos (Math.PI * (0.5 + 4 / 0.3)), yc - radius / 3.0 * Math.Sin (Math.PI * (0.5 + 4 / 0.3)), subradius, subradius);
cr.Fill (); cr.Fill ();
} }
@ -85,16 +88,19 @@ class CairoSample : DrawingArea
int xc = width / 2; int xc = width / 2;
int yc = height / 2; int yc = height / 2;
Surface overlay = cr.Target.CreateSimilar (Content.ColorAlpha, width, height); Surface overlay, punch, circles;
Surface punch = cr.Target.CreateSimilar (Content.Alpha, width, height); using (var target = cr.GetTarget ()) {
Surface circles = cr.Target.CreateSimilar (Content.ColorAlpha, width, height); overlay = target.CreateSimilar (Content.ColorAlpha, width, height);
punch = target.CreateSimilar (Content.Alpha, width, height);
circles = target.CreateSimilar (Content.ColorAlpha, width, height);
}
FillChecks (cr, 0, 0, width, height); FillChecks (cr, 0, 0, width, height);
cr.Save (); cr.Save ();
// Draw a black circle on the overlay // Draw a black circle on the overlay
using (Context cr_overlay = new Context (overlay)) { using (Context cr_overlay = new Context (overlay)) {
cr_overlay.Color = new Color (0.0, 0.0, 0.0); cr_overlay.SetSourceRGB (0.0, 0.0, 0.0);
OvalPath (cr_overlay, xc, yc, radius, radius); OvalPath (cr_overlay, xc, yc, radius, radius);
cr_overlay.Fill (); cr_overlay.Fill ();
@ -123,9 +129,9 @@ class CairoSample : DrawingArea
cr.SetSourceSurface (overlay, 0, 0); cr.SetSourceSurface (overlay, 0, 0);
cr.Paint (); cr.Paint ();
overlay.Destroy (); overlay.Dispose ();
punch.Destroy (); punch.Dispose ();
circles.Destroy (); circles.Dispose ();
} }
protected override bool OnDrawn (Cairo.Context ctx) protected override bool OnDrawn (Cairo.Context ctx)

View File

@ -140,7 +140,7 @@ namespace GtkDemo
Widget widget = o as Widget; Widget widget = o as Widget;
if (surface != null) if (surface != null)
surface.Destroy (); surface.Dispose ();
var allocation = widget.Allocation; var allocation = widget.Allocation;