cairo: Introduce the concept of reference ownership

Also:
  * improving naming consistency
  * obsolete old/broken stuff
  * clean up tabs/space mix

NOTE: this removes the wrapper caches for Pattern and Surface as
there was no reliable way to clear them.

This is a merge from changes in Mono.Cairo in mono 3.2.
This commit is contained in:
Michael Hutchinson 2013-07-26 11:45:26 -05:00 committed by Bertrand Lorentz
parent 5a78a5d177
commit 38d1a3f13e
24 changed files with 773 additions and 828 deletions

File diff suppressed because it is too large Load diff

View file

@ -36,11 +36,8 @@ namespace Cairo {
} }
public DirectFBSurface (IntPtr dfb, IntPtr dfb_surface) public DirectFBSurface (IntPtr dfb, IntPtr dfb_surface)
: base (NativeMethods.cairo_directfb_surface_create (dfb, dfb_surface), true)
{ {
surface = NativeMethods.cairo_directfb_surface_create (dfb, dfb_surface);
lock (surfaces.SyncRoot) {
surfaces [surface] = this;
}
} }
} }
} }

View file

@ -38,14 +38,11 @@ namespace Cairo
{ {
IntPtr handle; IntPtr handle;
internal static FontFace Lookup (IntPtr handle) internal static FontFace Lookup (IntPtr handle, bool owner)
{ {
if (handle == IntPtr.Zero) if (handle == IntPtr.Zero)
return null; return null;
return new FontFace (handle, owner);
NativeMethods.cairo_font_face_reference (handle);
return new FontFace (handle);
} }
~FontFace () ~FontFace ()
@ -71,10 +68,16 @@ namespace Cairo
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
// TODO: make non-public when all entry points are complete in binding [Obsolete]
public FontFace (IntPtr handle) public FontFace (IntPtr handle) : this (handle, true)
{
}
public FontFace (IntPtr handle, bool owned)
{ {
this.handle = handle; this.handle = handle;
if (!owned)
NativeMethods.cairo_font_face_reference (handle);
if (CairoDebug.Enabled) if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle); CairoDebug.OnAllocated (handle);
} }

View file

@ -34,9 +34,8 @@ namespace Cairo
{ {
IntPtr handle; IntPtr handle;
public FontOptions () public FontOptions () : this (NativeMethods.cairo_font_options_create ())
{ {
handle = NativeMethods.cairo_font_options_create ();
} }
~FontOptions () ~FontOptions ()
@ -59,7 +58,7 @@ namespace Cairo
[Obsolete ("Use Dispose()")] [Obsolete ("Use Dispose()")]
public void Destroy () public void Destroy ()
{ {
Dispose (); NativeMethods.cairo_font_options_destroy (handle);
} }
public void Dispose () public void Dispose ()

View file

@ -36,11 +36,8 @@ namespace Cairo {
} }
public GlitzSurface (IntPtr glitz_surface) public GlitzSurface (IntPtr glitz_surface)
: base (NativeMethods.cairo_glitz_surface_create (glitz_surface), true)
{ {
surface = NativeMethods.cairo_glitz_surface_create (glitz_surface);
lock (surfaces.SyncRoot) {
surfaces [surface] = this;
}
} }
} }
} }

View file

@ -33,10 +33,11 @@ namespace Cairo {
public class Gradient : Pattern public class Gradient : Pattern
{ {
protected Gradient (IntPtr handle) : base (handle) protected Gradient (IntPtr handle, bool owned) : base (handle, owned)
{ {
} }
[Obsolete]
protected Gradient () protected Gradient ()
{ {
} }
@ -44,20 +45,20 @@ namespace Cairo {
public int ColorStopCount { public int ColorStopCount {
get { get {
int cnt; int cnt;
NativeMethods.cairo_pattern_get_color_stop_count (pattern, out cnt); NativeMethods.cairo_pattern_get_color_stop_count (Handle, out cnt);
return cnt; return cnt;
} }
} }
public Status AddColorStop (double offset, Cairo.Color c) public Status AddColorStop (double offset, Color c)
{ {
NativeMethods.cairo_pattern_add_color_stop_rgba (pattern, offset, c.R, c.G, c.B, c.A); NativeMethods.cairo_pattern_add_color_stop_rgba (Handle, offset, c.R, c.G, c.B, c.A);
return Status; return Status;
} }
public Status AddColorStopRgb (double offset, Cairo.Color c) public Status AddColorStopRgb (double offset, Color c)
{ {
NativeMethods.cairo_pattern_add_color_stop_rgb (pattern, offset, c.R, c.G, c.B); NativeMethods.cairo_pattern_add_color_stop_rgb (Handle, offset, c.R, c.G, c.B);
return Status; return Status;
} }
} }

View file

@ -37,60 +37,49 @@ using System.Runtime.InteropServices;
namespace Cairo { namespace Cairo {
public class ImageSurface : Surface public class ImageSurface : Surface
{ {
internal ImageSurface (IntPtr handle, bool owns) : base (handle, owns) internal ImageSurface (IntPtr handle, bool owns) : base (handle, owns)
{ {
} }
public ImageSurface (Format format, int width, int height) public ImageSurface (Format format, int width, int height)
: base (NativeMethods.cairo_image_surface_create (format, width, height), true)
{ {
surface = NativeMethods.cairo_image_surface_create (format, width, height);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
[Obsolete ("Use ImageSurface (byte[] data, Cairo.Format format, int width, int height, int stride)")] [Obsolete ("Use ImageSurface (byte[] data, Cairo.Format format, int width, int height, int stride)")]
public ImageSurface (ref byte[] data, Cairo.Format format, int width, int height, int stride) :this (data, format, width, height, stride) public ImageSurface (ref byte[] data, Cairo.Format format, int width, int height, int stride)
: this (data, format, width, height, stride)
{ {
} }
public ImageSurface (byte[] data, Cairo.Format format, int width, int height, int stride) public ImageSurface (byte[] data, Format format, int width, int height, int stride)
: base (NativeMethods.cairo_image_surface_create_for_data (data, format, width, height, stride), true)
{ {
surface = NativeMethods.cairo_image_surface_create_for_data (data, format, width, height, stride);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public ImageSurface (IntPtr data, Cairo.Format format, int width, int height, int stride) public ImageSurface (IntPtr data, Format format, int width, int height, int stride)
: base (NativeMethods.cairo_image_surface_create_for_data (data, format, width, height, stride), true)
{ {
surface = NativeMethods.cairo_image_surface_create_for_data (data, format, width, height, stride);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public ImageSurface (string filename) public ImageSurface (string filename)
: base (NativeMethods.cairo_image_surface_create_from_png (filename), true)
{ {
surface = NativeMethods.cairo_image_surface_create_from_png (filename);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public int Width { public int Width {
get { return NativeMethods.cairo_image_surface_get_width (surface); } get { return NativeMethods.cairo_image_surface_get_width (Handle); }
} }
public int Height { public int Height {
get { return NativeMethods.cairo_image_surface_get_height (surface); } get { return NativeMethods.cairo_image_surface_get_height (Handle); }
} }
public byte[] Data { public byte[] Data {
get { get {
IntPtr ptr = NativeMethods.cairo_image_surface_get_data (surface); IntPtr ptr = NativeMethods.cairo_image_surface_get_data (Handle);
int length = Height * Stride; int length = Height * Stride;
byte[] data = new byte[length]; byte[] data = new byte[length];
Marshal.Copy (ptr, data, 0, length); Marshal.Copy (ptr, data, 0, length);
@ -100,16 +89,16 @@ namespace Cairo {
public IntPtr DataPtr { public IntPtr DataPtr {
get { get {
return NativeMethods.cairo_image_surface_get_data (surface); return NativeMethods.cairo_image_surface_get_data (Handle);
} }
} }
public Format Format { public Format Format {
get { return NativeMethods.cairo_image_surface_get_format (surface); } get { return NativeMethods.cairo_image_surface_get_format (Handle); }
} }
public int Stride { public int Stride {
get { return NativeMethods.cairo_image_surface_get_stride (surface); } get { return NativeMethods.cairo_image_surface_get_stride (Handle); }
} }
} }
} }

View file

@ -33,28 +33,27 @@ namespace Cairo {
public class LinearGradient : Gradient public class LinearGradient : Gradient
{ {
internal LinearGradient (IntPtr handle) : base (handle) internal LinearGradient (IntPtr handle, bool owned) : base (handle, owned)
{ {
} }
public LinearGradient (double x0, double y0, double x1, double y1) public LinearGradient (double x0, double y0, double x1, double y1)
: base (NativeMethods.cairo_pattern_create_linear (x0, y0, x1, y1), true)
{ {
pattern = NativeMethods.cairo_pattern_create_linear (x0, y0, x1, y1);
} }
public PointD[] LinearPoints { public PointD[] LinearPoints {
get { get {
double x0, y0, x1, y1; double x0, y0, x1, y1;
PointD[] points = new PointD [2]; PointD[] points = new PointD [2];
NativeMethods.cairo_pattern_get_linear_points (pattern, out x0, out y0, out x1, out y1); NativeMethods.cairo_pattern_get_linear_points (Handle, out x0, out y0, out x1, out y1);
points[0] = new PointD (x0, y0); points[0] = new PointD (x0, y0);
points[1] = new PointD (x1, y1); points[1] = new PointD (x1, y1);
return points; return points;
} }
} }
} }
} }

View file

@ -36,9 +36,9 @@ using System.Runtime.InteropServices;
namespace Cairo { namespace Cairo {
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public class Matrix : ICloneable public class Matrix : ICloneable
{ {
public double Xx; public double Xx;
public double Yx; public double Yx;
public double Xy; public double Xy;
@ -53,7 +53,7 @@ namespace Cairo {
this.Yy = yy; this.X0 = x0; this.Y0 = y0; this.Yy = yy; this.X0 = x0; this.Y0 = y0;
} }
public Matrix () public Matrix ()
{ {
this.InitIdentity (); this.InitIdentity ();
} }
@ -63,11 +63,11 @@ namespace Cairo {
return (this == new Matrix ()); return (this == new Matrix ());
} }
public void InitIdentity () public void InitIdentity ()
{ {
// this.Init(1,0,0,1,0,0); // this.Init(1,0,0,1,0,0);
NativeMethods.cairo_matrix_init_identity (this); NativeMethods.cairo_matrix_init_identity (this);
} }
public void Init (double xx, double yx, double xy, double yy, public void Init (double xx, double yx, double xy, double yy,
double x0, double y0) double x0, double y0)
@ -87,37 +87,37 @@ namespace Cairo {
NativeMethods.cairo_matrix_translate (this, tx, ty); NativeMethods.cairo_matrix_translate (this, tx, ty);
} }
public void InitScale (double sx, double sy) public void InitScale (double sx, double sy)
{ {
//this.Init (sx, 0, 0, sy, 0, 0); //this.Init (sx, 0, 0, sy, 0, 0);
NativeMethods.cairo_matrix_init_scale (this, sx, sy); NativeMethods.cairo_matrix_init_scale (this, sx, sy);
} }
public void Scale (double sx, double sy) public void Scale (double sx, double sy)
{ {
NativeMethods.cairo_matrix_scale (this, sx, sy); NativeMethods.cairo_matrix_scale (this, sx, sy);
} }
public void InitRotate (double radians) public void InitRotate (double radians)
{ {
/* /*
double s, c; double s, c;
s = Math.Sin (radians); s = Math.Sin (radians);
c = Math.Cos (radians); c = Math.Cos (radians);
this.Init (c, s, -s, c, 0, 0); this.Init (c, s, -s, c, 0, 0);
*/ */
NativeMethods.cairo_matrix_init_rotate (this, radians); NativeMethods.cairo_matrix_init_rotate (this, radians);
} }
public void Rotate (double radians) public void Rotate (double radians)
{ {
NativeMethods.cairo_matrix_rotate (this, radians); NativeMethods.cairo_matrix_rotate (this, radians);
} }
public Cairo.Status Invert () public Cairo.Status Invert ()
{ {
return NativeMethods.cairo_matrix_invert (this); return NativeMethods.cairo_matrix_invert (this);
} }
public void Multiply (Matrix b) public void Multiply (Matrix b)
{ {
@ -132,14 +132,14 @@ namespace Cairo {
} }
public void TransformDistance (ref double dx, ref double dy) public void TransformDistance (ref double dx, ref double dy)
{ {
NativeMethods.cairo_matrix_transform_distance (this, ref dx, ref dy); NativeMethods.cairo_matrix_transform_distance (this, ref dx, ref dy);
} }
public void TransformPoint (ref double x, ref double y) public void TransformPoint (ref double x, ref double y)
{ {
NativeMethods.cairo_matrix_transform_point (this, ref x, ref y); NativeMethods.cairo_matrix_transform_point (this, ref x, ref y);
} }
public override String ToString () public override String ToString ()
@ -189,5 +189,5 @@ namespace Cairo {
return this.MemberwiseClone (); return this.MemberwiseClone ();
} }
} }
} }

View file

@ -37,31 +37,28 @@ namespace Cairo {
} }
public PSSurface (string filename, double width, double height) public PSSurface (string filename, double width, double height)
: base (NativeMethods.cairo_ps_surface_create (filename, width, height), true)
{ {
surface = NativeMethods.cairo_ps_surface_create (filename, width, height);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public void BeginPageSetup () public void BeginPageSetup ()
{ {
NativeMethods.cairo_ps_surface_dsc_begin_page_setup (surface); NativeMethods.cairo_ps_surface_dsc_begin_page_setup (Handle);
} }
public void BeginSetup () public void BeginSetup ()
{ {
NativeMethods.cairo_ps_surface_dsc_begin_setup (surface); NativeMethods.cairo_ps_surface_dsc_begin_setup (Handle);
} }
public void DscComment (string comment) public void DscComment (string comment)
{ {
NativeMethods.cairo_ps_surface_dsc_comment (surface, comment); NativeMethods.cairo_ps_surface_dsc_comment (Handle, comment);
} }
public void SetSize (double width, double height) public void SetSize (double width, double height)
{ {
NativeMethods.cairo_ps_surface_set_size (surface, width, height); NativeMethods.cairo_ps_surface_set_size (Handle, width, height);
} }
} }
} }

View file

@ -34,9 +34,9 @@ using Cairo;
namespace Cairo { namespace Cairo {
public class Path : IDisposable public class Path : IDisposable
{ {
internal IntPtr handle = IntPtr.Zero; IntPtr handle = IntPtr.Zero;
internal Path (IntPtr handle) internal Path (IntPtr handle)
{ {
@ -50,6 +50,7 @@ namespace Cairo {
Dispose (false); Dispose (false);
} }
public IntPtr Handle { get { return handle; } }
public void Dispose () public void Dispose ()
{ {
@ -62,7 +63,7 @@ namespace Cairo {
if (!disposing || CairoDebug.Enabled) if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Path> (handle, disposing); CairoDebug.OnDisposed<Path> (handle, disposing);
if (!disposing|| handle == IntPtr.Zero) if (!disposing || handle == IntPtr.Zero)
return; return;
NativeMethods.cairo_path_destroy (handle); NativeMethods.cairo_path_destroy (handle);

View file

@ -32,47 +32,41 @@ using System.Collections;
namespace Cairo { namespace Cairo {
public class Pattern : IDisposable public class Pattern : IDisposable
{ {
[Obsolete]
protected IntPtr pattern = IntPtr.Zero; protected IntPtr pattern = IntPtr.Zero;
public static Pattern Lookup (IntPtr pattern) public static Pattern Lookup (IntPtr pattern, bool owner)
{ {
if (pattern == IntPtr.Zero) if (pattern == IntPtr.Zero)
return null; return null;
object x = patterns [pattern];
if (x != null)
return (Pattern) x;
PatternType pt = NativeMethods.cairo_pattern_get_type (pattern); PatternType pt = NativeMethods.cairo_pattern_get_type (pattern);
switch (pt) { switch (pt) {
case PatternType.Solid: case PatternType.Solid:
return new SolidPattern (pattern); return new SolidPattern (pattern, owner);
case PatternType.Surface: case PatternType.Surface:
return new SurfacePattern (pattern); return new SurfacePattern (pattern, owner);
case PatternType.Linear: case PatternType.Linear:
return new LinearGradient (pattern); return new LinearGradient (pattern, owner);
case PatternType.Radial: case PatternType.Radial:
return new RadialGradient (pattern); return new RadialGradient (pattern, owner);
default: default:
return new Pattern (pattern); return new Pattern (pattern, owner);
} }
} }
protected Pattern () [Obsolete]
{ protected Pattern ()
}
static Hashtable patterns = new Hashtable ();
internal Pattern (IntPtr handle)
{ {
lock (patterns){ }
patterns [handle] = this;
}
internal Pattern (IntPtr handle, bool owned)
{
Handle = handle; Handle = handle;
if (!owned)
NativeMethods.cairo_pattern_reference (handle);
if (CairoDebug.Enabled) if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle); CairoDebug.OnAllocated (handle);
} }
@ -82,16 +76,17 @@ namespace Cairo {
Dispose (false); Dispose (false);
} }
[Obsolete ("Use the SurfacePattern constructor")] [Obsolete ("Use the SurfacePattern constructor")]
public Pattern (Surface surface) public Pattern (Surface surface)
{ : this ( NativeMethods.cairo_pattern_create_for_surface (surface.Handle), true)
pattern = NativeMethods.cairo_pattern_create_for_surface (surface.Handle); {
} }
protected void Reference () [Obsolete]
{ protected void Reference ()
NativeMethods.cairo_pattern_reference (pattern); {
} NativeMethods.cairo_pattern_reference (pattern);
}
public void Dispose () public void Dispose ()
{ {
@ -109,52 +104,52 @@ namespace Cairo {
NativeMethods.cairo_pattern_destroy (Handle); NativeMethods.cairo_pattern_destroy (Handle);
Handle = IntPtr.Zero; Handle = IntPtr.Zero;
lock (patterns){
patterns.Remove (this);
}
} }
[Obsolete ("Use Dispose()")] [Obsolete ("Use Dispose()")]
public void Destroy () public void Destroy ()
{ {
NativeMethods.cairo_pattern_destroy (pattern); Dispose ();
}
public Extend Extend {
get { return NativeMethods.cairo_pattern_get_extend (pattern); }
set { NativeMethods.cairo_pattern_set_extend (pattern, value); }
} }
public Status Status public Status Status
{ {
get { return NativeMethods.cairo_pattern_status (pattern); } get { return NativeMethods.cairo_pattern_status (Handle); }
} }
public Matrix Matrix { public Extend Extend
set { {
NativeMethods.cairo_pattern_set_matrix (pattern, value); get { return NativeMethods.cairo_pattern_get_extend (Handle); }
set { NativeMethods.cairo_pattern_set_extend (Handle, value); }
}
public Matrix Matrix {
set {
NativeMethods.cairo_pattern_set_matrix (Handle, value);
} }
get { get {
Matrix m = new Matrix (); Matrix m = new Matrix ();
NativeMethods.cairo_pattern_get_matrix (pattern, m); NativeMethods.cairo_pattern_get_matrix (Handle, m);
return m; return m;
} }
} }
public IntPtr Handle { #pragma warning disable 612
get { return pattern; } public IntPtr Handle {
private set { pattern = value; } get { return pattern; }
} private set { pattern = value; }
}
#pragma warning restore 612
[Obsolete ("Replaced by Handle property")] [Obsolete]
public IntPtr Pointer { public IntPtr Pointer {
get { return Handle; } get { return pattern; }
} }
public PatternType PatternType { public PatternType PatternType {
get { return NativeMethods.cairo_pattern_get_type (pattern); } get { return NativeMethods.cairo_pattern_get_type (Handle); }
} }
} }
} }

View file

@ -37,16 +37,13 @@ namespace Cairo {
} }
public PdfSurface (string filename, double width, double height) public PdfSurface (string filename, double width, double height)
: base (NativeMethods.cairo_pdf_surface_create (filename, width, height), true)
{ {
surface = NativeMethods.cairo_pdf_surface_create (filename, width, height);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public void SetSize (double width, double height) public void SetSize (double width, double height)
{ {
NativeMethods.cairo_pdf_surface_set_size (surface, width, height); NativeMethods.cairo_pdf_surface_set_size (Handle, width, height);
} }
} }
} }

View file

@ -33,13 +33,13 @@ namespace Cairo {
public class RadialGradient : Gradient public class RadialGradient : Gradient
{ {
internal RadialGradient (IntPtr handle) : base (handle) internal RadialGradient (IntPtr handle, bool owned) : base (handle, owned)
{ {
} }
public RadialGradient (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1) public RadialGradient (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1)
: base (NativeMethods.cairo_pattern_create_radial (cx0, cy0, radius0, cx1, cy1, radius1), true)
{ {
pattern = NativeMethods.cairo_pattern_create_radial (cx0, cy0, radius0, cx1, cy1, radius1);
} }
} }
} }

View file

@ -32,15 +32,17 @@ namespace Cairo {
{ {
protected IntPtr handle = IntPtr.Zero; protected IntPtr handle = IntPtr.Zero;
internal ScaledFont (IntPtr handle) internal ScaledFont (IntPtr handle, bool owner)
{ {
this.handle = handle; this.handle = handle;
if (!owner)
NativeMethods.cairo_scaled_font_reference (handle);
if (CairoDebug.Enabled) if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle); CairoDebug.OnAllocated (handle);
} }
public ScaledFont (FontFace fontFace, Matrix matrix, Matrix ctm, FontOptions options) public ScaledFont (FontFace fontFace, Matrix matrix, Matrix ctm, FontOptions options)
: this (NativeMethods.cairo_scaled_font_create (fontFace.Handle, matrix, ctm, options.Handle)) : this (NativeMethods.cairo_scaled_font_create (fontFace.Handle, matrix, ctm, options.Handle), true)
{ {
} }
@ -49,19 +51,19 @@ namespace Cairo {
Dispose (false); Dispose (false);
} }
public IntPtr Handle { public IntPtr Handle {
get { get {
return handle; return handle;
} }
} }
public FontExtents FontExtents { public FontExtents FontExtents {
get { get {
FontExtents extents; FontExtents extents;
NativeMethods.cairo_scaled_font_extents (handle, out extents); NativeMethods.cairo_scaled_font_extents (handle, out extents);
return extents; return extents;
} }
} }
public Matrix FontMatrix { public Matrix FontMatrix {
get { get {
@ -111,10 +113,11 @@ namespace Cairo {
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
protected void Reference () [Obsolete]
{ protected void Reference ()
NativeMethods.cairo_scaled_font_reference (handle); {
} NativeMethods.cairo_scaled_font_reference (handle);
}
} }
} }

View file

@ -33,41 +33,40 @@ namespace Cairo {
public class SolidPattern : Pattern public class SolidPattern : Pattern
{ {
internal SolidPattern (IntPtr handle) : base (handle) internal SolidPattern (IntPtr handle, bool owned) : base (handle, owned)
{ {
} }
public SolidPattern (Color color) public SolidPattern (Color color)
: base (NativeMethods.cairo_pattern_create_rgba (color.R, color.G, color.B, color.A), true)
{ {
pattern = NativeMethods.cairo_pattern_create_rgba (color.R, color.G, color.B, color.A);
} }
public SolidPattern (double r, double g, double b) public SolidPattern (double r, double g, double b)
: base (NativeMethods.cairo_pattern_create_rgb (r, g, b), true)
{ {
pattern = NativeMethods.cairo_pattern_create_rgb (r, g, b);
} }
public SolidPattern (double r, double g, double b, double a) public SolidPattern (double r, double g, double b, double a)
: base (NativeMethods.cairo_pattern_create_rgba (r, g, b, a), true)
{ {
NativeMethods.cairo_pattern_create_rgba (r, g, b, a);
} }
public SolidPattern (Color color, bool solid) public SolidPattern (Color color, bool solid)
: base (solid
? NativeMethods.cairo_pattern_create_rgb (color.R, color.G, color.B)
: NativeMethods.cairo_pattern_create_rgba (color.R, color.G, color.B, color.A),
true)
{ {
if (solid)
pattern = NativeMethods.cairo_pattern_create_rgb (color.R, color.G, color.B);
else
pattern = NativeMethods.cairo_pattern_create_rgba (color.R, color.G, color.B, color.A);
} }
public Color Color { public Color Color {
get { get {
double red, green, blue, alpha; double red, green, blue, alpha;
NativeMethods.cairo_pattern_get_rgba (Handle, out red, out green, out blue, out alpha);
NativeMethods.cairo_pattern_get_rgba (pattern, out red, out green, out blue, out alpha);
return new Color (red, green, blue, alpha); return new Color (red, green, blue, alpha);
} }
} }
} }
} }

View file

@ -39,96 +39,87 @@ using System.Collections;
namespace Cairo { namespace Cairo {
public class Surface : IDisposable public class Surface : IDisposable
{ {
[Obsolete]
protected static Hashtable surfaces = new Hashtable (); protected static Hashtable surfaces = new Hashtable ();
internal IntPtr surface = IntPtr.Zero;
IntPtr handle = IntPtr.Zero;
[Obsolete] [Obsolete]
protected Surface() protected Surface()
{ {
} }
protected Surface (IntPtr ptr, bool owns) [Obsolete]
{ protected Surface (IntPtr ptr) : this (ptr, true)
surface = ptr;
lock (surfaces.SyncRoot){
surfaces [ptr] = this;
}
if (!owns)
NativeMethods.cairo_surface_reference (ptr);
if (CairoDebug.Enabled)
CairoDebug.OnAllocated (ptr);
}
static internal Surface LookupExternalSurface (IntPtr p)
{ {
lock (surfaces.SyncRoot){
object o = surfaces [p];
if (o == null){
return new Surface (p, false);
}
return (Surface) o;
}
} }
public static Surface LookupSurface (IntPtr surface) protected Surface (IntPtr handle, bool owner)
{
this.handle = handle;
if (!owner)
NativeMethods.cairo_surface_reference (handle);
if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle);
}
public static Surface Lookup (IntPtr surface, bool owned)
{ {
SurfaceType st = NativeMethods.cairo_surface_get_type (surface); SurfaceType st = NativeMethods.cairo_surface_get_type (surface);
switch (st) { switch (st) {
case SurfaceType.Image: case SurfaceType.Image:
return new ImageSurface (surface, true); return new ImageSurface (surface, owned);
case SurfaceType.Xlib: case SurfaceType.Xlib:
return new XlibSurface (surface, true); return new XlibSurface (surface, owned);
case SurfaceType.Xcb: case SurfaceType.Xcb:
return new XcbSurface (surface, true); return new XcbSurface (surface, owned);
case SurfaceType.Glitz: case SurfaceType.Glitz:
return new GlitzSurface (surface, true); return new GlitzSurface (surface, owned);
case SurfaceType.Win32: case SurfaceType.Win32:
return new Win32Surface (surface, true); return new Win32Surface (surface, owned);
case SurfaceType.Pdf: case SurfaceType.Pdf:
return new PdfSurface (surface, true); return new PdfSurface (surface, owned);
case SurfaceType.PS: case SurfaceType.PS:
return new PSSurface (surface, true); return new PSSurface (surface, owned);
case SurfaceType.DirectFB: case SurfaceType.DirectFB:
return new DirectFBSurface (surface, true); return new DirectFBSurface (surface, owned);
case SurfaceType.Svg: case SurfaceType.Svg:
return new SvgSurface (surface, true); return new SvgSurface (surface, owned);
default: default:
return Surface.LookupExternalSurface (surface); return new Surface (surface, owned);
} }
} }
[Obsolete ("Use an ImageSurface constructor instead.")] [Obsolete ("Use an ImageSurface constructor instead.")]
public static Cairo.Surface CreateForImage ( public static Cairo.Surface CreateForImage (
ref byte[] data, Cairo.Format format, int width, int height, int stride) ref byte[] data, Cairo.Format format, int width, int height, int stride)
{ {
IntPtr p = NativeMethods.cairo_image_surface_create_for_data ( IntPtr p = NativeMethods.cairo_image_surface_create_for_data (
data, format, width, height, stride); data, format, width, height, stride);
return new Cairo.Surface (p, true); return new Cairo.Surface (p, true);
} }
[Obsolete ("Use an ImageSurface constructor instead.")] [Obsolete ("Use an ImageSurface constructor instead.")]
public static Cairo.Surface CreateForImage ( public static Cairo.Surface CreateForImage (
Cairo.Format format, int width, int height) Cairo.Format format, int width, int height)
{ {
IntPtr p = NativeMethods.cairo_image_surface_create ( IntPtr p = NativeMethods.cairo_image_surface_create (
format, width, height); format, width, height);
return new Cairo.Surface (p, true); return new Cairo.Surface (p, true);
} }
public Cairo.Surface CreateSimilar ( public Cairo.Surface CreateSimilar (
Cairo.Content content, int width, int height) Cairo.Content content, int width, int height)
{ {
IntPtr p = NativeMethods.cairo_surface_create_similar ( IntPtr p = NativeMethods.cairo_surface_create_similar (
this.Handle, content, width, height); this.Handle, content, width, height);
return new Cairo.Surface (p, true); return new Cairo.Surface (p, true);
} }
~Surface () ~Surface ()
{ {
@ -138,7 +129,7 @@ namespace Cairo {
//[Obsolete ("Use Context.SetSource() followed by Context.Paint()")] //[Obsolete ("Use Context.SetSource() followed by Context.Paint()")]
public void Show (Context gr, double x, double y) public void Show (Context gr, double x, double y)
{ {
NativeMethods.cairo_set_source_surface (gr.Handle, surface, x, y); NativeMethods.cairo_set_source_surface (gr.Handle, handle, x, y);
NativeMethods.cairo_paint (gr.Handle); NativeMethods.cairo_paint (gr.Handle);
} }
@ -151,27 +142,24 @@ namespace Cairo {
protected virtual void Dispose (bool disposing) protected virtual void Dispose (bool disposing)
{ {
if (!disposing || CairoDebug.Enabled) if (!disposing || CairoDebug.Enabled)
CairoDebug.OnDisposed<Surface> (surface, disposing); CairoDebug.OnDisposed<Surface> (handle, disposing);
if (!disposing|| surface == IntPtr.Zero) if (!disposing || handle == IntPtr.Zero)
return; return;
lock (surfaces.SyncRoot) NativeMethods.cairo_surface_destroy (handle);
surfaces.Remove (surface); handle = IntPtr.Zero;
NativeMethods.cairo_surface_destroy (surface);
surface = IntPtr.Zero;
} }
public Status Finish () public Status Finish ()
{ {
NativeMethods.cairo_surface_finish (surface); NativeMethods.cairo_surface_finish (handle);
return Status; return Status;
} }
public void Flush () public void Flush ()
{ {
NativeMethods.cairo_surface_flush (surface); NativeMethods.cairo_surface_flush (handle);
} }
public void MarkDirty () public void MarkDirty ()
@ -184,67 +172,61 @@ namespace Cairo {
NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height); NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
} }
public IntPtr Handle { public IntPtr Handle {
get {
return surface;
}
}
public Device Device {
get { get {
IntPtr dev = NativeMethods.cairo_surface_get_device (surface); return handle;
return dev == IntPtr.Zero ? null : new Device (dev);
} }
} }
public PointD DeviceOffset { public PointD DeviceOffset {
get { get {
double x, y; double x, y;
NativeMethods.cairo_surface_get_device_offset (surface, out x, out y); NativeMethods.cairo_surface_get_device_offset (handle, out x, out y);
return new PointD (x, y); return new PointD (x, y);
} }
set { set {
NativeMethods.cairo_surface_set_device_offset (surface, value.X, value.Y); NativeMethods.cairo_surface_set_device_offset (handle, value.X, value.Y);
} }
} }
[Obsolete ("Use Dispose()")]
public void Destroy() public void Destroy()
{ {
Dispose (true); Dispose ();
} }
public void SetFallbackResolution (double x, double y) public void SetFallbackResolution (double x, double y)
{ {
NativeMethods.cairo_surface_set_fallback_resolution (surface, x, y); NativeMethods.cairo_surface_set_fallback_resolution (handle, x, y);
} }
public void WriteToPng (string filename) public void WriteToPng (string filename)
{ {
NativeMethods.cairo_surface_write_to_png (surface, filename); NativeMethods.cairo_surface_write_to_png (handle, filename);
} }
[Obsolete ("Use Handle instead.")] [Obsolete ("Use Handle instead.")]
public IntPtr Pointer { public IntPtr Pointer {
get { get {
return surface; return handle;
} }
} }
public Status Status { public Status Status {
get { return NativeMethods.cairo_surface_status (surface); } get { return NativeMethods.cairo_surface_status (handle); }
} }
public Content Content { public Content Content {
get { return NativeMethods.cairo_surface_get_content (surface); } get { return NativeMethods.cairo_surface_get_content (handle); }
} }
public SurfaceType SurfaceType { public SurfaceType SurfaceType {
get { return NativeMethods.cairo_surface_get_type (surface); } get { return NativeMethods.cairo_surface_get_type (handle); }
} }
public uint ReferenceCount { public uint ReferenceCount {
get { return NativeMethods.cairo_surface_get_reference_count (surface); } get { return NativeMethods.cairo_surface_get_reference_count (handle); }
} }
} }
} }

View file

@ -33,18 +33,18 @@ namespace Cairo {
public class SurfacePattern : Pattern public class SurfacePattern : Pattern
{ {
internal SurfacePattern (IntPtr handle) : base (handle) internal SurfacePattern (IntPtr handle, bool owned) : base (handle, owned)
{ {
} }
public SurfacePattern (Surface surface) public SurfacePattern (Surface surface)
: base (NativeMethods.cairo_pattern_create_for_surface (surface.Handle), true)
{ {
pattern = NativeMethods.cairo_pattern_create_for_surface (surface.Handle);
} }
public Filter Filter { public Filter Filter {
set { NativeMethods.cairo_pattern_set_filter (pattern, value); } set { NativeMethods.cairo_pattern_set_filter (Handle, value); }
get { return NativeMethods.cairo_pattern_get_filter (pattern); } get { return NativeMethods.cairo_pattern_get_filter (Handle); }
} }
} }
} }

View file

@ -37,16 +37,13 @@ namespace Cairo {
} }
public SvgSurface (string filename, double width, double height) public SvgSurface (string filename, double width, double height)
: base (NativeMethods.cairo_svg_surface_create (filename, width, height), true)
{ {
surface = NativeMethods.cairo_svg_surface_create (filename, width, height);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public void RestrictToVersion (SvgVersion version) public void RestrictToVersion (SvgVersion version)
{ {
NativeMethods.cairo_svg_surface_restrict_to_version (surface, version); NativeMethods.cairo_svg_surface_restrict_to_version (Handle, version);
} }
} }
} }

View file

@ -37,12 +37,8 @@ namespace Cairo {
} }
public Win32Surface (IntPtr hdc) public Win32Surface (IntPtr hdc)
: base (NativeMethods.cairo_win32_surface_create (hdc), true)
{ {
surface = NativeMethods.cairo_win32_surface_create (hdc);
lock (surfaces.SyncRoot) {
surfaces [surface] = this;
}
} }
} }
} }

View file

@ -36,24 +36,19 @@ namespace Cairo {
} }
public XcbSurface (IntPtr connection, uint drawable, IntPtr visual, int width, int height) public XcbSurface (IntPtr connection, uint drawable, IntPtr visual, int width, int height)
: base (NativeMethods.cairo_xcb_surface_create (connection, drawable, visual, width, height), true)
{ {
surface = NativeMethods.cairo_xcb_surface_create (connection, drawable, visual, width, height);
lock (surfaces.SyncRoot) {
surfaces [surface] = this;
}
} }
public static XcbSurface FromBitmap (IntPtr connection, uint bitmap, IntPtr screen, int width, int height) public static XcbSurface FromBitmap (IntPtr connection, uint bitmap, IntPtr screen, int width, int height)
{ {
IntPtr ptr; IntPtr ptr = NativeMethods.cairo_xcb_surface_create_for_bitmap (connection, bitmap, screen, width, height);
ptr = NativeMethods.cairo_xcb_surface_create_for_bitmap (connection, bitmap, screen, width, height);
return new XcbSurface (ptr, true); return new XcbSurface (ptr, true);
} }
public void SetSize (int width, int height) public void SetSize (int width, int height)
{ {
NativeMethods.cairo_xcb_surface_set_size (surface, width, height); NativeMethods.cairo_xcb_surface_set_size (Handle, width, height);
} }
} }
} }

View file

@ -39,11 +39,8 @@ namespace Cairo {
public class XlibSurface : Surface public class XlibSurface : Surface
{ {
public XlibSurface (IntPtr display, IntPtr drawable, IntPtr visual, int width, int height) public XlibSurface (IntPtr display, IntPtr drawable, IntPtr visual, int width, int height)
: base (NativeMethods.cairo_xlib_surface_create (display, drawable, visual, width, height), true)
{ {
surface = NativeMethods.cairo_xlib_surface_create (display, drawable, visual, width, height);
lock (surfaces.SyncRoot){
surfaces [surface] = this;
}
} }
public XlibSurface (IntPtr ptr, bool own) : base (ptr, own) public XlibSurface (IntPtr ptr, bool own) : base (ptr, own)
@ -52,48 +49,46 @@ namespace Cairo {
public static XlibSurface FromBitmap (IntPtr display, IntPtr bitmap, IntPtr screen, int width, int height) public static XlibSurface FromBitmap (IntPtr display, IntPtr bitmap, IntPtr screen, int width, int height)
{ {
IntPtr ptr; IntPtr ptr = NativeMethods.cairo_xlib_surface_create_for_bitmap (display, bitmap, screen, width, height);
ptr = NativeMethods.cairo_xlib_surface_create_for_bitmap (display, bitmap, screen, width, height);
return new XlibSurface(ptr, true); return new XlibSurface(ptr, true);
} }
public void SetDrawable (IntPtr drawable, int width, int height) public void SetDrawable (IntPtr drawable, int width, int height)
{ {
NativeMethods.cairo_xlib_surface_set_drawable (surface, drawable, width, height); NativeMethods.cairo_xlib_surface_set_drawable (Handle, drawable, width, height);
} }
public void SetSize (int width, int height) public void SetSize (int width, int height)
{ {
NativeMethods.cairo_xlib_surface_set_size (surface, width, height); NativeMethods.cairo_xlib_surface_set_size (Handle, width, height);
} }
public int Depth { public int Depth {
get { return NativeMethods.cairo_xlib_surface_get_depth (surface); } get { return NativeMethods.cairo_xlib_surface_get_depth (Handle); }
} }
public IntPtr Display { public IntPtr Display {
get { return NativeMethods.cairo_xlib_surface_get_display (surface); } get { return NativeMethods.cairo_xlib_surface_get_display (Handle); }
} }
public IntPtr Drawable { public IntPtr Drawable {
get { return NativeMethods.cairo_xlib_surface_get_drawable (surface); } get { return NativeMethods.cairo_xlib_surface_get_drawable (Handle); }
} }
public int Height { public int Height {
get { return NativeMethods.cairo_xlib_surface_get_height (surface); } get { return NativeMethods.cairo_xlib_surface_get_height (Handle); }
} }
public IntPtr Screen { public IntPtr Screen {
get { return NativeMethods.cairo_xlib_surface_get_screen (surface); } get { return NativeMethods.cairo_xlib_surface_get_screen (Handle); }
} }
public IntPtr Visual { public IntPtr Visual {
get { return NativeMethods.cairo_xlib_surface_get_visual (surface); } get { return NativeMethods.cairo_xlib_surface_get_visual (Handle); }
} }
public int Width { public int Width {
get { return NativeMethods.cairo_xlib_surface_get_width (surface); } get { return NativeMethods.cairo_xlib_surface_get_width (Handle); }
} }
} }

View file

@ -7,5 +7,5 @@
<symbol type="manual" cname="cairo_font_options_t" name="Cairo.FontOptions"/> <symbol type="manual" cname="cairo_font_options_t" name="Cairo.FontOptions"/>
<symbol type="manual" cname="cairo_region_t" name="Cairo.Region"/> <symbol type="manual" cname="cairo_region_t" name="Cairo.Region"/>
<symbol type="marshal" cname="cairo_pattern_t" name="Cairo.Pattern" marshal_type="IntPtr" call_fmt="{0}.Handle" from_fmt="Cairo.Pattern.Lookup ({0})" /> <symbol type="marshal" cname="cairo_pattern_t" name="Cairo.Pattern" marshal_type="IntPtr" call_fmt="{0}.Handle" from_fmt="Cairo.Pattern.Lookup ({0})" />
<symbol type="marshal" cname="cairo_surface_t" name="Cairo.Surface" marshal_type="IntPtr" call_fmt="{0}.Handle" from_fmt="Cairo.Surface.LookupSurface ({0})" /> <symbol type="marshal" cname="cairo_surface_t" name="Cairo.Surface" marshal_type="IntPtr" call_fmt="{0}.Handle" from_fmt="Cairo.Surface.Lookup ({0}, true)" />
</api> </api>

View file

@ -41,7 +41,7 @@ namespace Gdk {
public Cairo.Pattern BackgroundPattern { public Cairo.Pattern BackgroundPattern {
get { get {
IntPtr raw_ret = gdk_window_get_background_pattern(Handle); IntPtr raw_ret = gdk_window_get_background_pattern(Handle);
Cairo.Pattern ret = Cairo.Pattern.Lookup (raw_ret); Cairo.Pattern ret = Cairo.Pattern.Lookup (raw_ret, true);
return ret; return ret;
} }
set { set {