cairo: Throw ObjectDisposedException after an object has been disposed

Potentially all these IDisposable classes could be used after being
disposed, which would result in native crashes. We now do an explicit
check and throw an exception in managed land when the object has been
disposed.

This is particularly useful because:
 a/ the native crashes are quite obscure, there no indication that
you're using a disposed object
 b/ Gtk# is passing Context instances to user methods, and disposes them
when the method returns. So if the user code keeps a reference to the
Context, there a good chance it will try to use it after it's disposed.

Other changes in this patch include:
 * Renaming a parameter to be more consistent with the other subsequent
ctor called.
 * Replacing implementation of some [Obsolete()] methods with the call
to the methods they were replaced with, to avoid redundancy and the
need for more CheckDisposed() calls than necessary.
 * Throw ArgumentException when receiving an IntPtr.Zero as a handle,
as a way to protect ourselves from wrapping invalid native pointers,
and throwing ObjectDisposedExceptions because the object was invalid in
the first place.

Signed-off-by: Bertrand Lorentz <bertrand.lorentz@gmail.com>
This commit is contained in:
Andrés G. Aragoneses 2013-11-05 00:22:28 +01:00 committed by Bertrand Lorentz
parent 21b081b6a9
commit 70d1827058
19 changed files with 404 additions and 55 deletions

View File

@ -87,6 +87,9 @@ namespace Cairo {
public Context (IntPtr handle, bool owner) public Context (IntPtr handle, bool owner)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (!owner) if (!owner)
NativeMethods.cairo_reference (handle); NativeMethods.cairo_reference (handle);
@ -122,23 +125,38 @@ namespace Cairo {
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
public void Save () public void Save ()
{ {
CheckDisposed ();
NativeMethods.cairo_save (handle); NativeMethods.cairo_save (handle);
} }
public void Restore () public void Restore ()
{ {
CheckDisposed ();
NativeMethods.cairo_restore (handle); NativeMethods.cairo_restore (handle);
} }
public Antialias Antialias { public Antialias Antialias {
get { return NativeMethods.cairo_get_antialias (handle); } get {
set { NativeMethods.cairo_set_antialias (handle, value); } CheckDisposed ();
return NativeMethods.cairo_get_antialias (handle);
}
set {
CheckDisposed ();
NativeMethods.cairo_set_antialias (handle, value);
}
} }
public Cairo.Status Status { public Cairo.Status Status {
get { get {
CheckDisposed ();
return NativeMethods.cairo_status (handle); return NativeMethods.cairo_status (handle);
} }
} }
@ -151,10 +169,12 @@ namespace Cairo {
public Operator Operator { public Operator Operator {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_operator (handle, value); NativeMethods.cairo_set_operator (handle, value);
} }
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_operator (handle); return NativeMethods.cairo_get_operator (handle);
} }
} }
@ -162,69 +182,80 @@ namespace Cairo {
[Obsolete ("Use SetSourceColor method")] [Obsolete ("Use SetSourceColor method")]
public Color Color { public Color Color {
set { set {
NativeMethods.cairo_set_source_rgba (handle, value.R, value.G, value.B, value.A); SetSourceColor (value);
} }
} }
[Obsolete ("Use SetSourceRGBA method")] [Obsolete ("Use SetSourceRGBA method")]
public Cairo.Color ColorRgb { public Cairo.Color ColorRgb {
set { set {
Color = new Color (value.R, value.G, value.B); SetSourceRGBA (value.R, value.G, value.B, value.A);
} }
} }
public double Tolerance { public double Tolerance {
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_tolerance (handle); return NativeMethods.cairo_get_tolerance (handle);
} }
set { set {
CheckDisposed ();
NativeMethods.cairo_set_tolerance (handle, value); NativeMethods.cairo_set_tolerance (handle, value);
} }
} }
public Cairo.FillRule FillRule { public Cairo.FillRule FillRule {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_fill_rule (handle, value); NativeMethods.cairo_set_fill_rule (handle, value);
} }
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_fill_rule (handle); return NativeMethods.cairo_get_fill_rule (handle);
} }
} }
public double LineWidth { public double LineWidth {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_line_width (handle, value); NativeMethods.cairo_set_line_width (handle, value);
} }
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_line_width (handle); return NativeMethods.cairo_get_line_width (handle);
} }
} }
public Cairo.LineCap LineCap { public Cairo.LineCap LineCap {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_line_cap (handle, value); NativeMethods.cairo_set_line_cap (handle, value);
} }
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_line_cap (handle); return NativeMethods.cairo_get_line_cap (handle);
} }
} }
public Cairo.LineJoin LineJoin { public Cairo.LineJoin LineJoin {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_line_join (handle, value); NativeMethods.cairo_set_line_join (handle, value);
} }
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_line_join (handle); return NativeMethods.cairo_get_line_join (handle);
} }
} }
public void SetDash (double [] dashes, double offset) public void SetDash (double [] dashes, double offset)
{ {
CheckDisposed ();
NativeMethods.cairo_set_dash (handle, dashes, dashes.Length, offset); NativeMethods.cairo_set_dash (handle, dashes, dashes.Length, offset);
} }
@ -251,27 +282,32 @@ namespace Cairo {
public void SetSource (Pattern source) public void SetSource (Pattern source)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source (handle, source.Handle); NativeMethods.cairo_set_source (handle, source.Handle);
} }
public Pattern GetSource () public Pattern GetSource ()
{ {
CheckDisposed ();
var ptr = NativeMethods.cairo_get_source (handle); var ptr = NativeMethods.cairo_get_source (handle);
return Cairo.Pattern.Lookup (ptr, false); return Cairo.Pattern.Lookup (ptr, false);
} }
public double MiterLimit { public double MiterLimit {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_miter_limit (handle, value); NativeMethods.cairo_set_miter_limit (handle, value);
} }
get { get {
CheckDisposed ();
return NativeMethods.cairo_get_miter_limit (handle); return NativeMethods.cairo_get_miter_limit (handle);
} }
} }
public PointD CurrentPoint { public PointD CurrentPoint {
get { get {
CheckDisposed ();
double x, y; double x, y;
NativeMethods.cairo_get_current_point (handle, out x, out y); NativeMethods.cairo_get_current_point (handle, out x, out y);
return new PointD (x, y); return new PointD (x, y);
@ -281,10 +317,7 @@ namespace Cairo {
[Obsolete ("Use GetTarget/SetTarget")] [Obsolete ("Use GetTarget/SetTarget")]
public Cairo.Surface Target { public Cairo.Surface Target {
set { set {
if (handle != IntPtr.Zero) SetTarget (value);
NativeMethods.cairo_destroy (handle);
handle = NativeMethods.cairo_create (value.Handle);
} }
get { get {
@ -294,11 +327,13 @@ namespace Cairo {
public Surface GetTarget () public Surface GetTarget ()
{ {
CheckDisposed ();
return Surface.Lookup (NativeMethods.cairo_get_target (handle), false); return Surface.Lookup (NativeMethods.cairo_get_target (handle), false);
} }
public void SetTarget (Surface target) public void SetTarget (Surface target)
{ {
CheckDisposed ();
if (handle != IntPtr.Zero) if (handle != IntPtr.Zero)
NativeMethods.cairo_destroy (handle); NativeMethods.cairo_destroy (handle);
handle = NativeMethods.cairo_create (target.Handle); handle = NativeMethods.cairo_create (target.Handle);
@ -317,46 +352,57 @@ namespace Cairo {
public ScaledFont GetScaledFont () public ScaledFont GetScaledFont ()
{ {
CheckDisposed ();
return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false); return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false);
} }
public void SetScaledFont (ScaledFont font) public void SetScaledFont (ScaledFont font)
{ {
CheckDisposed ();
NativeMethods.cairo_set_scaled_font (handle, font.Handle); NativeMethods.cairo_set_scaled_font (handle, font.Handle);
} }
public uint ReferenceCount { public uint ReferenceCount {
get { return NativeMethods.cairo_get_reference_count (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_get_reference_count (handle);
}
} }
public void SetSourceColor (Color color) public void SetSourceColor (Color color)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source_rgba (handle, color.R, color.G, color.B, color.A); NativeMethods.cairo_set_source_rgba (handle, color.R, color.G, color.B, color.A);
} }
public void SetSourceRGB (double r, double g, double b) public void SetSourceRGB (double r, double g, double b)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source_rgb (handle, r, g, b); NativeMethods.cairo_set_source_rgb (handle, r, g, b);
} }
public void SetSourceRGBA (double r, double g, double b, double a) public void SetSourceRGBA (double r, double g, double b, double a)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source_rgba (handle, r, g, b, a); NativeMethods.cairo_set_source_rgba (handle, r, g, b, a);
} }
//[Obsolete ("Use SetSource method (with double parameters)")] //[Obsolete ("Use SetSource method (with double parameters)")]
public void SetSourceSurface (Surface source, int x, int y) public void SetSourceSurface (Surface source, int x, int y)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y); NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
} }
public void SetSource (Surface source, double x, double y) public void SetSource (Surface source, double x, double y)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y); NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
} }
public void SetSource (Surface source) public void SetSource (Surface source)
{ {
CheckDisposed ();
NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0); NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0);
} }
@ -364,11 +410,13 @@ namespace Cairo {
public void NewPath () public void NewPath ()
{ {
CheckDisposed ();
NativeMethods.cairo_new_path (handle); NativeMethods.cairo_new_path (handle);
} }
public void NewSubPath () public void NewSubPath ()
{ {
CheckDisposed ();
NativeMethods.cairo_new_sub_path (handle); NativeMethods.cairo_new_sub_path (handle);
} }
@ -379,6 +427,7 @@ namespace Cairo {
public void MoveTo (double x, double y) public void MoveTo (double x, double y)
{ {
CheckDisposed ();
NativeMethods.cairo_move_to (handle, x, y); NativeMethods.cairo_move_to (handle, x, y);
} }
@ -389,6 +438,7 @@ namespace Cairo {
public void LineTo (double x, double y) public void LineTo (double x, double y)
{ {
CheckDisposed ();
NativeMethods.cairo_line_to (handle, x, y); NativeMethods.cairo_line_to (handle, x, y);
} }
@ -399,6 +449,7 @@ namespace Cairo {
public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3) public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
{ {
CheckDisposed ();
NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3); NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3);
} }
@ -409,6 +460,7 @@ namespace Cairo {
public void RelMoveTo (double dx, double dy) public void RelMoveTo (double dx, double dy)
{ {
CheckDisposed ();
NativeMethods.cairo_rel_move_to (handle, dx, dy); NativeMethods.cairo_rel_move_to (handle, dx, dy);
} }
@ -419,6 +471,7 @@ namespace Cairo {
public void RelLineTo (double dx, double dy) public void RelLineTo (double dx, double dy)
{ {
CheckDisposed ();
NativeMethods.cairo_rel_line_to (handle, dx, dy); NativeMethods.cairo_rel_line_to (handle, dx, dy);
} }
@ -429,16 +482,19 @@ namespace Cairo {
public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3) public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
{ {
CheckDisposed ();
NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3); NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3);
} }
public void Arc (double xc, double yc, double radius, double angle1, double angle2) public void Arc (double xc, double yc, double radius, double angle1, double angle2)
{ {
CheckDisposed ();
NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2); NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2);
} }
public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2) public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
{ {
CheckDisposed ();
NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2); NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2);
} }
@ -454,31 +510,37 @@ namespace Cairo {
public void Rectangle (double x, double y, double width, double height) public void Rectangle (double x, double y, double width, double height)
{ {
CheckDisposed ();
NativeMethods.cairo_rectangle (handle, x, y, width, height); NativeMethods.cairo_rectangle (handle, x, y, width, height);
} }
public void ClosePath () public void ClosePath ()
{ {
CheckDisposed ();
NativeMethods.cairo_close_path (handle); NativeMethods.cairo_close_path (handle);
} }
public Path CopyPath () public Path CopyPath ()
{ {
CheckDisposed ();
return new Path (NativeMethods.cairo_copy_path (handle)); return new Path (NativeMethods.cairo_copy_path (handle));
} }
public Path CopyPathFlat () public Path CopyPathFlat ()
{ {
CheckDisposed ();
return new Path (NativeMethods.cairo_copy_path_flat (handle)); return new Path (NativeMethods.cairo_copy_path_flat (handle));
} }
public void AppendPath (Path path) public void AppendPath (Path path)
{ {
CheckDisposed ();
NativeMethods.cairo_append_path (handle, path.Handle); NativeMethods.cairo_append_path (handle, path.Handle);
} }
public void PathExtents (out double x1, out double y1, out double x2, out double y2) public void PathExtents (out double x1, out double y1, out double x2, out double y2)
{ {
CheckDisposed ();
NativeMethods.cairo_path_extents (handle, out x1, out y1, out x2, out y2); NativeMethods.cairo_path_extents (handle, out x1, out y1, out x2, out y2);
} }
@ -487,36 +549,43 @@ namespace Cairo {
#region Painting Methods #region Painting Methods
public void Paint () public void Paint ()
{ {
CheckDisposed ();
NativeMethods.cairo_paint (handle); NativeMethods.cairo_paint (handle);
} }
public void PaintWithAlpha (double alpha) public void PaintWithAlpha (double alpha)
{ {
CheckDisposed ();
NativeMethods.cairo_paint_with_alpha (handle, alpha); NativeMethods.cairo_paint_with_alpha (handle, alpha);
} }
public void Mask (Pattern pattern) public void Mask (Pattern pattern)
{ {
CheckDisposed ();
NativeMethods.cairo_mask (handle, pattern.Handle); NativeMethods.cairo_mask (handle, pattern.Handle);
} }
public void MaskSurface (Surface surface, double surface_x, double surface_y) public void MaskSurface (Surface surface, double surface_x, double surface_y)
{ {
CheckDisposed ();
NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y); NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y);
} }
public void Stroke () public void Stroke ()
{ {
CheckDisposed ();
NativeMethods.cairo_stroke (handle); NativeMethods.cairo_stroke (handle);
} }
public void StrokePreserve () public void StrokePreserve ()
{ {
CheckDisposed ();
NativeMethods.cairo_stroke_preserve (handle); NativeMethods.cairo_stroke_preserve (handle);
} }
public Rectangle StrokeExtents () public Rectangle StrokeExtents ()
{ {
CheckDisposed ();
double x1, y1, x2, y2; double x1, y1, x2, y2;
NativeMethods.cairo_stroke_extents (handle, out x1, out y1, out x2, out y2); NativeMethods.cairo_stroke_extents (handle, out x1, out y1, out x2, out y2);
return new Rectangle (x1, y1, x2 - x1, y2 - y1); return new Rectangle (x1, y1, x2 - x1, y2 - y1);
@ -524,11 +593,13 @@ namespace Cairo {
public void Fill () public void Fill ()
{ {
CheckDisposed ();
NativeMethods.cairo_fill (handle); NativeMethods.cairo_fill (handle);
} }
public Rectangle FillExtents () public Rectangle FillExtents ()
{ {
CheckDisposed ();
double x1, y1, x2, y2; double x1, y1, x2, y2;
NativeMethods.cairo_fill_extents (handle, out x1, out y1, out x2, out y2); NativeMethods.cairo_fill_extents (handle, out x1, out y1, out x2, out y2);
return new Rectangle (x1, y1, x2 - x1, y2 - y1); return new Rectangle (x1, y1, x2 - x1, y2 - y1);
@ -536,6 +607,7 @@ namespace Cairo {
public void FillPreserve () public void FillPreserve ()
{ {
CheckDisposed ();
NativeMethods.cairo_fill_preserve (handle); NativeMethods.cairo_fill_preserve (handle);
} }
@ -543,51 +615,61 @@ namespace Cairo {
public void Clip () public void Clip ()
{ {
CheckDisposed ();
NativeMethods.cairo_clip (handle); NativeMethods.cairo_clip (handle);
} }
public void ClipPreserve () public void ClipPreserve ()
{ {
CheckDisposed ();
NativeMethods.cairo_clip_preserve (handle); NativeMethods.cairo_clip_preserve (handle);
} }
public void ResetClip () public void ResetClip ()
{ {
CheckDisposed ();
NativeMethods.cairo_reset_clip (handle); NativeMethods.cairo_reset_clip (handle);
} }
public bool InStroke (double x, double y) public bool InStroke (double x, double y)
{ {
CheckDisposed ();
return NativeMethods.cairo_in_stroke (handle, x, y); return NativeMethods.cairo_in_stroke (handle, x, y);
} }
public bool InClip (double x, double y) public bool InClip (double x, double y)
{ {
CheckDisposed ();
return NativeMethods.cairo_in_clip (handle, x, y); return NativeMethods.cairo_in_clip (handle, x, y);
} }
public bool InFill (double x, double y) public bool InFill (double x, double y)
{ {
CheckDisposed ();
return NativeMethods.cairo_in_fill (handle, x, y); return NativeMethods.cairo_in_fill (handle, x, y);
} }
public Pattern PopGroup () public Pattern PopGroup ()
{ {
CheckDisposed ();
return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true); return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true);
} }
public void PopGroupToSource () public void PopGroupToSource ()
{ {
CheckDisposed ();
NativeMethods.cairo_pop_group_to_source (handle); NativeMethods.cairo_pop_group_to_source (handle);
} }
public void PushGroup () public void PushGroup ()
{ {
CheckDisposed ();
NativeMethods.cairo_push_group (handle); NativeMethods.cairo_push_group (handle);
} }
public void PushGroup (Content content) public void PushGroup (Content content)
{ {
CheckDisposed ();
NativeMethods.cairo_push_group_with_content (handle, content); NativeMethods.cairo_push_group_with_content (handle, content);
} }
@ -600,80 +682,91 @@ namespace Cairo {
public Surface GetGroupTarget () public Surface GetGroupTarget ()
{ {
CheckDisposed ();
IntPtr surface = NativeMethods.cairo_get_group_target (handle); IntPtr surface = NativeMethods.cairo_get_group_target (handle);
return Surface.Lookup (surface, false); return Surface.Lookup (surface, false);
} }
public void Rotate (double angle) public void Rotate (double angle)
{ {
CheckDisposed ();
NativeMethods.cairo_rotate (handle, angle); NativeMethods.cairo_rotate (handle, angle);
} }
public void Scale (double sx, double sy) public void Scale (double sx, double sy)
{ {
CheckDisposed ();
NativeMethods.cairo_scale (handle, sx, sy); NativeMethods.cairo_scale (handle, sx, sy);
} }
public void Translate (double tx, double ty) public void Translate (double tx, double ty)
{ {
CheckDisposed ();
NativeMethods.cairo_translate (handle, tx, ty); NativeMethods.cairo_translate (handle, tx, ty);
} }
public void Transform (Matrix m) public void Transform (Matrix m)
{ {
CheckDisposed ();
NativeMethods.cairo_transform (handle, m); NativeMethods.cairo_transform (handle, m);
} }
[Obsolete("Use UserToDevice instead")] [Obsolete("Use UserToDevice instead")]
public void TransformPoint (ref double x, ref double y) public void TransformPoint (ref double x, ref double y)
{ {
NativeMethods.cairo_user_to_device (handle, ref x, ref y); UserToDevice (ref x, ref y);
} }
[Obsolete("Use UserToDeviceDistance instead")] [Obsolete("Use UserToDeviceDistance instead")]
public void TransformDistance (ref double dx, ref double dy) public void TransformDistance (ref double dx, ref double dy)
{ {
NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy); UserToDevice (ref dx, ref dy);
} }
[Obsolete("Use DeviceToUser instead")] [Obsolete("Use DeviceToUser instead")]
public void InverseTransformPoint (ref double x, ref double y) public void InverseTransformPoint (ref double x, ref double y)
{ {
NativeMethods.cairo_device_to_user (handle, ref x, ref y); UserToDevice (ref x, ref y);
} }
[Obsolete("Use DeviceToUserDistance instead")] [Obsolete("Use DeviceToUserDistance instead")]
public void InverseTransformDistance (ref double dx, ref double dy) public void InverseTransformDistance (ref double dx, ref double dy)
{ {
NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy); DeviceToUserDistance (ref dx, ref dy);
} }
public void UserToDevice (ref double x, ref double y) public void UserToDevice (ref double x, ref double y)
{ {
CheckDisposed ();
NativeMethods.cairo_user_to_device (handle, ref x, ref y); NativeMethods.cairo_user_to_device (handle, ref x, ref y);
} }
public void UserToDeviceDistance (ref double dx, ref double dy) public void UserToDeviceDistance (ref double dx, ref double dy)
{ {
CheckDisposed ();
NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy); NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
} }
public void DeviceToUser (ref double x, ref double y) public void DeviceToUser (ref double x, ref double y)
{ {
CheckDisposed ();
NativeMethods.cairo_device_to_user (handle, ref x, ref y); NativeMethods.cairo_device_to_user (handle, ref x, ref y);
} }
public void DeviceToUserDistance (ref double dx, ref double dy) public void DeviceToUserDistance (ref double dx, ref double dy)
{ {
CheckDisposed ();
NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy); NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
} }
public Matrix Matrix { public Matrix Matrix {
set { set {
CheckDisposed ();
NativeMethods.cairo_set_matrix (handle, value); NativeMethods.cairo_set_matrix (handle, value);
} }
get { get {
CheckDisposed ();
Matrix m = new Matrix (); Matrix m = new Matrix ();
NativeMethods.cairo_get_matrix (handle, m); NativeMethods.cairo_get_matrix (handle, m);
return m; return m;
@ -682,11 +775,13 @@ namespace Cairo {
public void SetFontSize (double scale) public void SetFontSize (double scale)
{ {
CheckDisposed ();
NativeMethods.cairo_set_font_size (handle, scale); NativeMethods.cairo_set_font_size (handle, scale);
} }
public void IdentityMatrix () public void IdentityMatrix ()
{ {
CheckDisposed ();
NativeMethods.cairo_identity_matrix (handle); NativeMethods.cairo_identity_matrix (handle);
} }
@ -703,20 +798,28 @@ namespace Cairo {
public Matrix FontMatrix { public Matrix FontMatrix {
get { get {
CheckDisposed ();
Matrix m; Matrix m;
NativeMethods.cairo_get_font_matrix (handle, out m); NativeMethods.cairo_get_font_matrix (handle, out m);
return m; return m;
} }
set { NativeMethods.cairo_set_font_matrix (handle, value); } set {
CheckDisposed ();
NativeMethods.cairo_set_font_matrix (handle, value);
}
} }
public FontOptions FontOptions { public FontOptions FontOptions {
get { get {
CheckDisposed ();
FontOptions options = new FontOptions (); FontOptions options = new FontOptions ();
NativeMethods.cairo_get_font_options (handle, options.Handle); NativeMethods.cairo_get_font_options (handle, options.Handle);
return options; return options;
} }
set { NativeMethods.cairo_set_font_options (handle, value.Handle); } set {
CheckDisposed ();
NativeMethods.cairo_set_font_options (handle, value.Handle);
}
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
@ -757,6 +860,8 @@ namespace Cairo {
public void ShowGlyphs (Glyph[] glyphs) public void ShowGlyphs (Glyph[] glyphs)
{ {
CheckDisposed ();
IntPtr ptr; IntPtr ptr;
ptr = FromGlyphToUnManagedMemory (glyphs); ptr = FromGlyphToUnManagedMemory (glyphs);
@ -780,6 +885,8 @@ namespace Cairo {
public void GlyphPath (Glyph[] glyphs) public void GlyphPath (Glyph[] glyphs)
{ {
CheckDisposed ();
IntPtr ptr; IntPtr ptr;
ptr = FromGlyphToUnManagedMemory (glyphs); ptr = FromGlyphToUnManagedMemory (glyphs);
@ -787,11 +894,11 @@ namespace Cairo {
NativeMethods.cairo_glyph_path (handle, ptr, glyphs.Length); NativeMethods.cairo_glyph_path (handle, ptr, glyphs.Length);
Marshal.FreeHGlobal (ptr); Marshal.FreeHGlobal (ptr);
} }
public FontExtents FontExtents { public FontExtents FontExtents {
get { get {
CheckDisposed ();
FontExtents f_extents; FontExtents f_extents;
NativeMethods.cairo_font_extents (handle, out f_extents); NativeMethods.cairo_font_extents (handle, out f_extents);
return f_extents; return f_extents;
@ -800,6 +907,7 @@ namespace Cairo {
public void CopyPage () public void CopyPage ()
{ {
CheckDisposed ();
NativeMethods.cairo_copy_page (handle); NativeMethods.cairo_copy_page (handle);
} }
@ -821,21 +929,25 @@ namespace Cairo {
public FontFace GetContextFontFace () public FontFace GetContextFontFace ()
{ {
CheckDisposed ();
return Cairo.FontFace.Lookup (NativeMethods.cairo_get_font_face (handle), false); return Cairo.FontFace.Lookup (NativeMethods.cairo_get_font_face (handle), false);
} }
public void SetContextFontFace (FontFace value) public void SetContextFontFace (FontFace value)
{ {
CheckDisposed ();
NativeMethods.cairo_set_font_face (handle, value == null ? IntPtr.Zero : value.Handle); NativeMethods.cairo_set_font_face (handle, value == null ? IntPtr.Zero : value.Handle);
} }
public void SelectFontFace (string family, FontSlant slant, FontWeight weight) public void SelectFontFace (string family, FontSlant slant, FontWeight weight)
{ {
CheckDisposed ();
NativeMethods.cairo_select_font_face (handle, family, slant, weight); NativeMethods.cairo_select_font_face (handle, family, slant, weight);
} }
public void ShowPage () public void ShowPage ()
{ {
CheckDisposed ();
NativeMethods.cairo_show_page (handle); NativeMethods.cairo_show_page (handle);
} }
@ -860,26 +972,31 @@ namespace Cairo {
public void ShowText (string str) public void ShowText (string str)
{ {
CheckDisposed ();
NativeMethods.cairo_show_text (handle, TerminateUtf8 (str)); NativeMethods.cairo_show_text (handle, TerminateUtf8 (str));
} }
public void ShowText (byte[] utf8) public void ShowText (byte[] utf8)
{ {
CheckDisposed ();
NativeMethods.cairo_show_text (handle, TerminateUtf8 (utf8)); NativeMethods.cairo_show_text (handle, TerminateUtf8 (utf8));
} }
public void TextPath (string str) public void TextPath (string str)
{ {
CheckDisposed ();
NativeMethods.cairo_text_path (handle, TerminateUtf8 (str)); NativeMethods.cairo_text_path (handle, TerminateUtf8 (str));
} }
public void TextPath (byte[] utf8) public void TextPath (byte[] utf8)
{ {
CheckDisposed ();
NativeMethods.cairo_text_path (handle, TerminateUtf8 (utf8)); NativeMethods.cairo_text_path (handle, TerminateUtf8 (utf8));
} }
public TextExtents TextExtents (string s) public TextExtents TextExtents (string s)
{ {
CheckDisposed ();
TextExtents extents; TextExtents extents;
NativeMethods.cairo_text_extents (handle, TerminateUtf8 (s), out extents); NativeMethods.cairo_text_extents (handle, TerminateUtf8 (s), out extents);
return extents; return extents;
@ -887,6 +1004,7 @@ namespace Cairo {
public TextExtents TextExtents(byte[] utf8) public TextExtents TextExtents(byte[] utf8)
{ {
CheckDisposed ();
TextExtents extents; TextExtents extents;
NativeMethods.cairo_text_extents (handle, TerminateUtf8 (utf8), out extents); NativeMethods.cairo_text_extents (handle, TerminateUtf8 (utf8), out extents);
return extents; return extents;
@ -894,6 +1012,8 @@ namespace Cairo {
public TextExtents GlyphExtents (Glyph[] glyphs) public TextExtents GlyphExtents (Glyph[] glyphs)
{ {
CheckDisposed ();
IntPtr ptr = FromGlyphToUnManagedMemory (glyphs); IntPtr ptr = FromGlyphToUnManagedMemory (glyphs);
TextExtents extents; TextExtents extents;
@ -911,7 +1031,10 @@ namespace Cairo {
} }
public bool HasCurrentPoint { public bool HasCurrentPoint {
get { return NativeMethods.cairo_has_current_point (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_has_current_point (handle);
}
} }
} }
} }

View File

@ -41,11 +41,15 @@ namespace Cairo
internal Device (IntPtr handle) internal Device (IntPtr handle)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = NativeMethods.cairo_device_reference (handle); this.handle = NativeMethods.cairo_device_reference (handle);
} }
public Status Acquire () public Status Acquire ()
{ {
CheckDisposed ();
return NativeMethods.cairo_device_acquire (handle); return NativeMethods.cairo_device_acquire (handle);
} }
@ -57,27 +61,42 @@ namespace Cairo
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
public void Finish () public void Finish ()
{ {
CheckDisposed ();
NativeMethods.cairo_device_finish (handle); NativeMethods.cairo_device_finish (handle);
} }
public void Flush () public void Flush ()
{ {
CheckDisposed ();
NativeMethods.cairo_device_flush (handle); NativeMethods.cairo_device_flush (handle);
} }
public void Release () public void Release ()
{ {
CheckDisposed ();
NativeMethods.cairo_device_release (handle); NativeMethods.cairo_device_release (handle);
} }
public Status Status { public Status Status {
get { return NativeMethods.cairo_device_status (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_device_status (handle);
}
} }
public DeviceType Type { public DeviceType Type {
get { return NativeMethods.cairo_device_get_type (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_device_get_type (handle);
}
} }
} }

View File

@ -68,6 +68,12 @@ namespace Cairo
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
[Obsolete] [Obsolete]
public FontFace (IntPtr handle) : this (handle, true) public FontFace (IntPtr handle) : this (handle, true)
{ {
@ -75,6 +81,9 @@ namespace Cairo
public FontFace (IntPtr handle, bool owned) public FontFace (IntPtr handle, bool owned)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (!owned) if (!owned)
NativeMethods.cairo_font_face_reference (handle); NativeMethods.cairo_font_face_reference (handle);
@ -90,18 +99,23 @@ namespace Cairo
public Status Status { public Status Status {
get { get {
CheckDisposed ();
return NativeMethods.cairo_font_face_status (handle); return NativeMethods.cairo_font_face_status (handle);
} }
} }
public FontType FontType { public FontType FontType {
get { get {
CheckDisposed ();
return NativeMethods.cairo_font_face_get_type (handle); return NativeMethods.cairo_font_face_get_type (handle);
} }
} }
public uint ReferenceCount { public uint ReferenceCount {
get { return NativeMethods.cairo_font_face_get_reference_count (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_font_face_get_reference_count (handle);
}
} }
} }
} }

View File

@ -45,6 +45,9 @@ namespace Cairo
internal FontOptions (IntPtr handle) internal FontOptions (IntPtr handle)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (CairoDebug.Enabled) if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle); CairoDebug.OnAllocated (handle);
@ -52,6 +55,7 @@ namespace Cairo
public FontOptions Copy () public FontOptions Copy ()
{ {
CheckDisposed ();
return new FontOptions (NativeMethods.cairo_font_options_copy (handle)); return new FontOptions (NativeMethods.cairo_font_options_copy (handle));
} }
@ -79,6 +83,12 @@ namespace Cairo
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
public static bool operator == (FontOptions options, FontOptions other) public static bool operator == (FontOptions options, FontOptions other)
{ {
return Equals (options, other); return Equals (options, other);
@ -112,31 +122,59 @@ namespace Cairo
{ {
if (other == null) if (other == null)
throw new ArgumentNullException ("other"); throw new ArgumentNullException ("other");
CheckDisposed ();
NativeMethods.cairo_font_options_merge (handle, other.Handle); NativeMethods.cairo_font_options_merge (handle, other.Handle);
} }
public Antialias Antialias { public Antialias Antialias {
get { return NativeMethods.cairo_font_options_get_antialias (handle); } get {
set { NativeMethods.cairo_font_options_set_antialias (handle, value); } CheckDisposed ();
return NativeMethods.cairo_font_options_get_antialias (handle);
}
set {
CheckDisposed ();
NativeMethods.cairo_font_options_set_antialias (handle, value);
}
} }
public HintMetrics HintMetrics { public HintMetrics HintMetrics {
get { return NativeMethods.cairo_font_options_get_hint_metrics (handle);} get {
set { NativeMethods.cairo_font_options_set_hint_metrics (handle, value); } CheckDisposed ();
return NativeMethods.cairo_font_options_get_hint_metrics (handle);
}
set {
CheckDisposed ();
NativeMethods.cairo_font_options_set_hint_metrics (handle, value);
}
} }
public HintStyle HintStyle { public HintStyle HintStyle {
get { return NativeMethods.cairo_font_options_get_hint_style (handle);} get {
set { NativeMethods.cairo_font_options_set_hint_style (handle, value); } CheckDisposed ();
return NativeMethods.cairo_font_options_get_hint_style (handle);
}
set {
CheckDisposed ();
NativeMethods.cairo_font_options_set_hint_style (handle, value);
}
} }
public Status Status { public Status Status {
get { return NativeMethods.cairo_font_options_status (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_font_options_status (handle);
}
} }
public SubpixelOrder SubpixelOrder { public SubpixelOrder SubpixelOrder {
get { return NativeMethods.cairo_font_options_get_subpixel_order (handle);} get {
set { NativeMethods.cairo_font_options_set_subpixel_order (handle, value); } CheckDisposed ();
return NativeMethods.cairo_font_options_get_subpixel_order (handle);
}
set {
CheckDisposed ();
NativeMethods.cairo_font_options_set_subpixel_order (handle, value);
}
} }
} }
} }

View File

@ -44,6 +44,7 @@ namespace Cairo {
public int ColorStopCount { public int ColorStopCount {
get { get {
CheckDisposed ();
int cnt; int cnt;
NativeMethods.cairo_pattern_get_color_stop_count (Handle, out cnt); NativeMethods.cairo_pattern_get_color_stop_count (Handle, out cnt);
return cnt; return cnt;
@ -52,12 +53,14 @@ namespace Cairo {
public Status AddColorStop (double offset, Color c) public Status AddColorStop (double offset, Color c)
{ {
CheckDisposed ();
NativeMethods.cairo_pattern_add_color_stop_rgba (Handle, 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, Color c) public Status AddColorStopRgb (double offset, Color c)
{ {
CheckDisposed ();
NativeMethods.cairo_pattern_add_color_stop_rgb (Handle, 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

@ -70,11 +70,16 @@ namespace Cairo {
} }
public int Width { public int Width {
get { return NativeMethods.cairo_image_surface_get_width (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_image_surface_get_width (Handle); }
} }
public int Height { public int Height {
get { return NativeMethods.cairo_image_surface_get_height (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_image_surface_get_height (Handle);
}
} }
public byte[] Data { public byte[] Data {
@ -89,16 +94,23 @@ namespace Cairo {
public IntPtr DataPtr { public IntPtr DataPtr {
get { get {
CheckDisposed ();
return NativeMethods.cairo_image_surface_get_data (Handle); return NativeMethods.cairo_image_surface_get_data (Handle);
} }
} }
public Format Format { public Format Format {
get { return NativeMethods.cairo_image_surface_get_format (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_image_surface_get_format (Handle);
}
} }
public int Stride { public int Stride {
get { return NativeMethods.cairo_image_surface_get_stride (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_image_surface_get_stride (Handle);
}
} }
} }
} }

View File

@ -44,6 +44,7 @@ namespace Cairo {
public PointD[] LinearPoints { public PointD[] LinearPoints {
get { get {
CheckDisposed ();
double x0, y0, x1, y1; double x0, y0, x1, y1;
PointD[] points = new PointD [2]; PointD[] points = new PointD [2];

View File

@ -43,21 +43,25 @@ namespace Cairo {
public void BeginPageSetup () public void BeginPageSetup ()
{ {
CheckDisposed ();
NativeMethods.cairo_ps_surface_dsc_begin_page_setup (Handle); NativeMethods.cairo_ps_surface_dsc_begin_page_setup (Handle);
} }
public void BeginSetup () public void BeginSetup ()
{ {
CheckDisposed ();
NativeMethods.cairo_ps_surface_dsc_begin_setup (Handle); NativeMethods.cairo_ps_surface_dsc_begin_setup (Handle);
} }
public void DscComment (string comment) public void DscComment (string comment)
{ {
CheckDisposed ();
NativeMethods.cairo_ps_surface_dsc_comment (Handle, comment); NativeMethods.cairo_ps_surface_dsc_comment (Handle, comment);
} }
public void SetSize (double width, double height) public void SetSize (double width, double height)
{ {
CheckDisposed ();
NativeMethods.cairo_ps_surface_set_size (Handle, width, height); NativeMethods.cairo_ps_surface_set_size (Handle, width, height);
} }
} }

View File

@ -40,6 +40,9 @@ namespace Cairo {
internal Path (IntPtr handle) internal Path (IntPtr handle)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (CairoDebug.Enabled) if (CairoDebug.Enabled)
CairoDebug.OnAllocated (handle); CairoDebug.OnAllocated (handle);

View File

@ -64,6 +64,9 @@ namespace Cairo {
internal Pattern (IntPtr handle, bool owned) internal Pattern (IntPtr handle, bool owned)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
Handle = handle; Handle = handle;
if (!owned) if (!owned)
NativeMethods.cairo_pattern_reference (handle); NativeMethods.cairo_pattern_reference (handle);
@ -85,6 +88,7 @@ namespace Cairo {
[Obsolete] [Obsolete]
protected void Reference () protected void Reference ()
{ {
CheckDisposed ();
NativeMethods.cairo_pattern_reference (pattern); NativeMethods.cairo_pattern_reference (pattern);
} }
@ -106,6 +110,12 @@ namespace Cairo {
Handle = IntPtr.Zero; Handle = IntPtr.Zero;
} }
protected void CheckDisposed ()
{
if (Handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
[Obsolete ("Use Dispose()")] [Obsolete ("Use Dispose()")]
public void Destroy () public void Destroy ()
{ {
@ -114,21 +124,32 @@ namespace Cairo {
public Status Status public Status Status
{ {
get { return NativeMethods.cairo_pattern_status (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_pattern_status (Handle);
}
} }
public Extend Extend public Extend Extend
{ {
get { return NativeMethods.cairo_pattern_get_extend (Handle); } get {
set { NativeMethods.cairo_pattern_set_extend (Handle, value); } CheckDisposed ();
return NativeMethods.cairo_pattern_get_extend (Handle);
}
set {
CheckDisposed ();
NativeMethods.cairo_pattern_set_extend (Handle, value);
}
} }
public Matrix Matrix { public Matrix Matrix {
set { set {
CheckDisposed ();
NativeMethods.cairo_pattern_set_matrix (Handle, value); NativeMethods.cairo_pattern_set_matrix (Handle, value);
} }
get { get {
CheckDisposed ();
Matrix m = new Matrix (); Matrix m = new Matrix ();
NativeMethods.cairo_pattern_get_matrix (Handle, m); NativeMethods.cairo_pattern_get_matrix (Handle, m);
return m; return m;
@ -148,7 +169,10 @@ namespace Cairo {
} }
public PatternType PatternType { public PatternType PatternType {
get { return NativeMethods.cairo_pattern_get_type (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_pattern_get_type (Handle);
}
} }
} }
} }

View File

@ -43,6 +43,7 @@ namespace Cairo {
public void SetSize (double width, double height) public void SetSize (double width, double height)
{ {
CheckDisposed ();
NativeMethods.cairo_pdf_surface_set_size (Handle, width, height); NativeMethods.cairo_pdf_surface_set_size (Handle, width, height);
} }
} }

View File

@ -52,6 +52,9 @@ namespace Cairo
public Region (IntPtr handle, bool owned) public Region (IntPtr handle, bool owned)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (!owned) if (!owned)
NativeMethods.cairo_region_reference (handle); NativeMethods.cairo_region_reference (handle);
@ -75,6 +78,7 @@ namespace Cairo
public Region Copy () public Region Copy ()
{ {
CheckDisposed ();
return new Region (NativeMethods.cairo_region_copy (Handle), true); return new Region (NativeMethods.cairo_region_copy (Handle), true);
} }
@ -101,6 +105,12 @@ namespace Cairo
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
public override bool Equals (object obj) public override bool Equals (object obj)
{ {
return (obj is Region) && NativeMethods.cairo_region_equal (Handle, (obj as Region).Handle); return (obj is Region) && NativeMethods.cairo_region_equal (Handle, (obj as Region).Handle);
@ -112,11 +122,14 @@ namespace Cairo
} }
public Status Status { public Status Status {
get { return NativeMethods.cairo_region_status (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_region_status (Handle); }
} }
public RectangleInt Extents { public RectangleInt Extents {
get { get {
CheckDisposed ();
RectangleInt result; RectangleInt result;
NativeMethods.cairo_region_get_extents (Handle, out result); NativeMethods.cairo_region_get_extents (Handle, out result);
return result; return result;
@ -124,72 +137,90 @@ namespace Cairo
} }
public int NumRectangles { public int NumRectangles {
get { return NativeMethods.cairo_region_num_rectangles (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_region_num_rectangles (Handle);
}
} }
public RectangleInt GetRectangle (int nth) public RectangleInt GetRectangle (int nth)
{ {
CheckDisposed ();
RectangleInt val; RectangleInt val;
NativeMethods.cairo_region_get_rectangle (Handle, nth, out val); NativeMethods.cairo_region_get_rectangle (Handle, nth, out val);
return val; return val;
} }
public bool IsEmpty { public bool IsEmpty {
get { return NativeMethods.cairo_region_is_empty (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_region_is_empty (Handle);
}
} }
public RegionOverlap ContainsPoint (RectangleInt rectangle) public RegionOverlap ContainsPoint (RectangleInt rectangle)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_contains_rectangle (Handle, ref rectangle); return NativeMethods.cairo_region_contains_rectangle (Handle, ref rectangle);
} }
public bool ContainsPoint (int x, int y) public bool ContainsPoint (int x, int y)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_contains_point (Handle, x, y); return NativeMethods.cairo_region_contains_point (Handle, x, y);
} }
public void Translate (int dx, int dy) public void Translate (int dx, int dy)
{ {
CheckDisposed ();
NativeMethods.cairo_region_translate (Handle, dx, dy); NativeMethods.cairo_region_translate (Handle, dx, dy);
} }
public Status Subtract (Region other) public Status Subtract (Region other)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_subtract (Handle, other.Handle); return NativeMethods.cairo_region_subtract (Handle, other.Handle);
} }
public Status SubtractRectangle (RectangleInt rectangle) public Status SubtractRectangle (RectangleInt rectangle)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_subtract_rectangle (Handle, ref rectangle); return NativeMethods.cairo_region_subtract_rectangle (Handle, ref rectangle);
} }
public Status Intersect (Region other) public Status Intersect (Region other)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_intersect (Handle, other.Handle); return NativeMethods.cairo_region_intersect (Handle, other.Handle);
} }
public Status IntersectRectangle (RectangleInt rectangle) public Status IntersectRectangle (RectangleInt rectangle)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_intersect_rectangle (Handle, ref rectangle); return NativeMethods.cairo_region_intersect_rectangle (Handle, ref rectangle);
} }
public Status Union (Region other) public Status Union (Region other)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_union (Handle, other.Handle); return NativeMethods.cairo_region_union (Handle, other.Handle);
} }
public Status UnionRectangle (RectangleInt rectangle) public Status UnionRectangle (RectangleInt rectangle)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_union_rectangle (Handle, ref rectangle); return NativeMethods.cairo_region_union_rectangle (Handle, ref rectangle);
} }
public Status Xor (Region other) public Status Xor (Region other)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_xor (Handle, other.Handle); return NativeMethods.cairo_region_xor (Handle, other.Handle);
} }
public Status XorRectangle (RectangleInt rectangle) public Status XorRectangle (RectangleInt rectangle)
{ {
CheckDisposed ();
return NativeMethods.cairo_region_xor_rectangle (Handle, ref rectangle); return NativeMethods.cairo_region_xor_rectangle (Handle, ref rectangle);
} }
} }

View File

@ -34,6 +34,9 @@ namespace Cairo {
internal ScaledFont (IntPtr handle, bool owner) internal ScaledFont (IntPtr handle, bool owner)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (!owner) if (!owner)
NativeMethods.cairo_scaled_font_reference (handle); NativeMethods.cairo_scaled_font_reference (handle);
@ -59,6 +62,7 @@ namespace Cairo {
public FontExtents FontExtents { public FontExtents FontExtents {
get { get {
CheckDisposed ();
FontExtents extents; FontExtents extents;
NativeMethods.cairo_scaled_font_extents (handle, out extents); NativeMethods.cairo_scaled_font_extents (handle, out extents);
return extents; return extents;
@ -67,6 +71,7 @@ namespace Cairo {
public Matrix FontMatrix { public Matrix FontMatrix {
get { get {
CheckDisposed ();
Matrix m; Matrix m;
NativeMethods.cairo_scaled_font_get_font_matrix (handle, out m); NativeMethods.cairo_scaled_font_get_font_matrix (handle, out m);
return m; return m;
@ -75,12 +80,14 @@ namespace Cairo {
public FontType FontType { public FontType FontType {
get { get {
CheckDisposed ();
return NativeMethods.cairo_scaled_font_get_type (handle); return NativeMethods.cairo_scaled_font_get_type (handle);
} }
} }
public TextExtents GlyphExtents (Glyph[] glyphs) public TextExtents GlyphExtents (Glyph[] glyphs)
{ {
CheckDisposed ();
IntPtr ptr = Context.FromGlyphToUnManagedMemory (glyphs); IntPtr ptr = Context.FromGlyphToUnManagedMemory (glyphs);
TextExtents extents; TextExtents extents;
@ -92,7 +99,10 @@ namespace Cairo {
public Status Status public Status Status
{ {
get { return NativeMethods.cairo_scaled_font_status (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_scaled_font_status (handle);
}
} }
public void Dispose () public void Dispose ()
@ -113,9 +123,16 @@ namespace Cairo {
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
[Obsolete] [Obsolete]
protected void Reference () protected void Reference ()
{ {
CheckDisposed ();
NativeMethods.cairo_scaled_font_reference (handle); NativeMethods.cairo_scaled_font_reference (handle);
} }
} }

View File

@ -62,6 +62,7 @@ namespace Cairo {
public Color Color { public Color Color {
get { get {
CheckDisposed ();
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 (Handle, out red, out green, out blue, out alpha);
return new Color (red, green, blue, alpha); return new Color (red, green, blue, alpha);

View File

@ -51,12 +51,15 @@ namespace Cairo {
} }
[Obsolete] [Obsolete]
protected Surface (IntPtr ptr) : this (ptr, true) protected Surface (IntPtr handle) : this (handle, true)
{ {
} }
protected Surface (IntPtr handle, bool owner) protected Surface (IntPtr handle, bool owner)
{ {
if (handle == IntPtr.Zero)
throw new ArgumentException ("handle should not be NULL", "handle");
this.handle = handle; this.handle = handle;
if (!owner) if (!owner)
NativeMethods.cairo_surface_reference (handle); NativeMethods.cairo_surface_reference (handle);
@ -151,24 +154,34 @@ namespace Cairo {
handle = IntPtr.Zero; handle = IntPtr.Zero;
} }
protected void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
public Status Finish () public Status Finish ()
{ {
CheckDisposed ();
NativeMethods.cairo_surface_finish (handle); NativeMethods.cairo_surface_finish (handle);
return Status; return Status;
} }
public void Flush () public void Flush ()
{ {
CheckDisposed ();
NativeMethods.cairo_surface_flush (handle); NativeMethods.cairo_surface_flush (handle);
} }
public void MarkDirty () public void MarkDirty ()
{ {
CheckDisposed ();
NativeMethods.cairo_surface_mark_dirty (Handle); NativeMethods.cairo_surface_mark_dirty (Handle);
} }
public void MarkDirty (Rectangle rectangle) public void MarkDirty (Rectangle rectangle)
{ {
CheckDisposed ();
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);
} }
@ -180,12 +193,14 @@ namespace Cairo {
public PointD DeviceOffset { public PointD DeviceOffset {
get { get {
CheckDisposed ();
double x, y; double x, y;
NativeMethods.cairo_surface_get_device_offset (handle, 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 {
CheckDisposed ();
NativeMethods.cairo_surface_set_device_offset (handle, value.X, value.Y); NativeMethods.cairo_surface_set_device_offset (handle, value.X, value.Y);
} }
} }
@ -198,11 +213,13 @@ namespace Cairo {
public void SetFallbackResolution (double x, double y) public void SetFallbackResolution (double x, double y)
{ {
CheckDisposed ();
NativeMethods.cairo_surface_set_fallback_resolution (handle, x, y); NativeMethods.cairo_surface_set_fallback_resolution (handle, x, y);
} }
public void WriteToPng (string filename) public void WriteToPng (string filename)
{ {
CheckDisposed ();
NativeMethods.cairo_surface_write_to_png (handle, filename); NativeMethods.cairo_surface_write_to_png (handle, filename);
} }
@ -214,19 +231,29 @@ namespace Cairo {
} }
public Status Status { public Status Status {
get { return NativeMethods.cairo_surface_status (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_surface_status (handle);
}
} }
public Content Content { public Content Content {
get { return NativeMethods.cairo_surface_get_content (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_surface_get_content (handle);
}
} }
public SurfaceType SurfaceType { public SurfaceType SurfaceType {
get { return NativeMethods.cairo_surface_get_type (handle); } get {
CheckDisposed ();
return NativeMethods.cairo_surface_get_type (handle);
}
} }
public uint ReferenceCount { public uint ReferenceCount {
get { return NativeMethods.cairo_surface_get_reference_count (handle); } get {
return NativeMethods.cairo_surface_get_reference_count (handle); }
} }
} }
} }

View File

@ -43,8 +43,14 @@ namespace Cairo {
} }
public Filter Filter { public Filter Filter {
set { NativeMethods.cairo_pattern_set_filter (Handle, value); } set {
get { return NativeMethods.cairo_pattern_get_filter (Handle); } CheckDisposed ();
NativeMethods.cairo_pattern_set_filter (Handle, value);
}
get {
CheckDisposed ();
return NativeMethods.cairo_pattern_get_filter (Handle);
}
} }
} }
} }

View File

@ -43,6 +43,7 @@ namespace Cairo {
public void RestrictToVersion (SvgVersion version) public void RestrictToVersion (SvgVersion version)
{ {
CheckDisposed ();
NativeMethods.cairo_svg_surface_restrict_to_version (Handle, version); NativeMethods.cairo_svg_surface_restrict_to_version (Handle, version);
} }
} }

View File

@ -48,6 +48,7 @@ namespace Cairo {
public void SetSize (int width, int height) public void SetSize (int width, int height)
{ {
CheckDisposed ();
NativeMethods.cairo_xcb_surface_set_size (Handle, width, height); NativeMethods.cairo_xcb_surface_set_size (Handle, width, height);
} }
} }

View File

@ -55,40 +55,63 @@ namespace Cairo {
public void SetDrawable (IntPtr drawable, int width, int height) public void SetDrawable (IntPtr drawable, int width, int height)
{ {
CheckDisposed ();
NativeMethods.cairo_xlib_surface_set_drawable (Handle, 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)
{ {
CheckDisposed ();
NativeMethods.cairo_xlib_surface_set_size (Handle, 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 (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_depth (Handle);
}
} }
public IntPtr Display { public IntPtr Display {
get { return NativeMethods.cairo_xlib_surface_get_display (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_display (Handle);
}
} }
public IntPtr Drawable { public IntPtr Drawable {
get { return NativeMethods.cairo_xlib_surface_get_drawable (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_drawable (Handle);
}
} }
public int Height { public int Height {
get { return NativeMethods.cairo_xlib_surface_get_height (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_height (Handle);
}
} }
public IntPtr Screen { public IntPtr Screen {
get { return NativeMethods.cairo_xlib_surface_get_screen (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_screen (Handle);
}
} }
public IntPtr Visual { public IntPtr Visual {
get { return NativeMethods.cairo_xlib_surface_get_visual (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_visual (Handle);
}
} }
public int Width { public int Width {
get { return NativeMethods.cairo_xlib_surface_get_width (Handle); } get {
CheckDisposed ();
return NativeMethods.cairo_xlib_surface_get_width (Handle);
}
} }
} }