2008-10-09 22:35:02 +00:00
|
|
|
//
|
|
|
|
// Mono.Cairo.Surface.cs
|
|
|
|
//
|
|
|
|
// Authors:
|
|
|
|
// Duncan Mak
|
|
|
|
// Miguel de Icaza.
|
|
|
|
// Alp Toker
|
|
|
|
//
|
|
|
|
// (C) Ximian Inc, 2003.
|
|
|
|
// (C) Novell, Inc. 2003.
|
|
|
|
//
|
|
|
|
// This is an OO wrapper API for the Cairo API
|
|
|
|
//
|
|
|
|
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
2013-07-26 16:45:26 +00:00
|
|
|
//
|
2008-10-09 22:35:02 +00:00
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
2013-07-26 16:45:26 +00:00
|
|
|
//
|
2008-10-09 22:35:02 +00:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
namespace Cairo {
|
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
public class Surface : IDisposable
|
|
|
|
{
|
|
|
|
[Obsolete]
|
2008-10-09 22:35:02 +00:00
|
|
|
protected static Hashtable surfaces = new Hashtable ();
|
2013-07-26 16:45:26 +00:00
|
|
|
|
|
|
|
IntPtr handle = IntPtr.Zero;
|
2008-10-09 22:35:02 +00:00
|
|
|
|
2013-03-05 22:53:49 +00:00
|
|
|
[Obsolete]
|
2008-10-09 22:35:02 +00:00
|
|
|
protected Surface()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
[Obsolete]
|
2013-11-04 23:22:28 +00:00
|
|
|
protected Surface (IntPtr handle) : this (handle, true)
|
2008-10-09 22:35:02 +00:00
|
|
|
{
|
2013-07-26 16:45:26 +00:00
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
protected Surface (IntPtr handle, bool owner)
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
if (handle == IntPtr.Zero)
|
|
|
|
throw new ArgumentException ("handle should not be NULL", "handle");
|
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
this.handle = handle;
|
|
|
|
if (!owner)
|
|
|
|
NativeMethods.cairo_surface_reference (handle);
|
|
|
|
if (CairoDebug.Enabled)
|
|
|
|
CairoDebug.OnAllocated (handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Surface Lookup (IntPtr surface, bool owned)
|
2008-10-09 22:35:02 +00:00
|
|
|
{
|
|
|
|
SurfaceType st = NativeMethods.cairo_surface_get_type (surface);
|
|
|
|
switch (st) {
|
|
|
|
case SurfaceType.Image:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new ImageSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.Xlib:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new XlibSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.Xcb:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new XcbSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.Glitz:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new GlitzSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.Win32:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new Win32Surface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.Pdf:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new PdfSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.PS:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new PSSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.DirectFB:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new DirectFBSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
case SurfaceType.Svg:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new SvgSurface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
default:
|
2013-07-26 16:45:26 +00:00
|
|
|
return new Surface (surface, owned);
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
[Obsolete ("Use an ImageSurface constructor instead.")]
|
2013-07-26 16:45:26 +00:00
|
|
|
public static Cairo.Surface CreateForImage (
|
|
|
|
ref byte[] data, Cairo.Format format, int width, int height, int stride)
|
|
|
|
{
|
|
|
|
IntPtr p = NativeMethods.cairo_image_surface_create_for_data (
|
|
|
|
data, format, width, height, stride);
|
|
|
|
|
|
|
|
return new Cairo.Surface (p, true);
|
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
|
|
|
|
[Obsolete ("Use an ImageSurface constructor instead.")]
|
2013-07-26 16:45:26 +00:00
|
|
|
public static Cairo.Surface CreateForImage (
|
|
|
|
Cairo.Format format, int width, int height)
|
|
|
|
{
|
|
|
|
IntPtr p = NativeMethods.cairo_image_surface_create (
|
|
|
|
format, width, height);
|
2008-10-09 22:35:02 +00:00
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
return new Cairo.Surface (p, true);
|
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
|
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
public Cairo.Surface CreateSimilar (
|
|
|
|
Cairo.Content content, int width, int height)
|
|
|
|
{
|
|
|
|
IntPtr p = NativeMethods.cairo_surface_create_similar (
|
|
|
|
this.Handle, content, width, height);
|
2008-10-09 22:35:02 +00:00
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
return new Cairo.Surface (p, true);
|
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
|
|
|
|
~Surface ()
|
|
|
|
{
|
|
|
|
Dispose (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//[Obsolete ("Use Context.SetSource() followed by Context.Paint()")]
|
2013-07-26 16:45:26 +00:00
|
|
|
public void Show (Context gr, double x, double y)
|
2008-10-09 22:35:02 +00:00
|
|
|
{
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_set_source_surface (gr.Handle, handle, x, y);
|
2008-10-09 22:35:02 +00:00
|
|
|
NativeMethods.cairo_paint (gr.Handle);
|
|
|
|
}
|
|
|
|
|
2011-02-18 05:00:12 +00:00
|
|
|
public void Dispose ()
|
2008-10-09 22:35:02 +00:00
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
2013-03-05 22:53:49 +00:00
|
|
|
if (!disposing || CairoDebug.Enabled)
|
2013-07-26 16:45:26 +00:00
|
|
|
CairoDebug.OnDisposed<Surface> (handle, disposing);
|
2013-03-05 22:53:49 +00:00
|
|
|
|
2013-11-04 23:14:33 +00:00
|
|
|
if (handle == IntPtr.Zero)
|
2013-07-26 16:45:26 +00:00
|
|
|
return;
|
2008-10-09 22:35:02 +00:00
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_destroy (handle);
|
|
|
|
handle = IntPtr.Zero;
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
2013-11-04 23:22:28 +00:00
|
|
|
protected void CheckDisposed ()
|
|
|
|
{
|
|
|
|
if (handle == IntPtr.Zero)
|
|
|
|
throw new ObjectDisposedException ("Object has already been disposed");
|
|
|
|
}
|
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
public Status Finish ()
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_finish (handle);
|
2008-10-09 22:35:02 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
public void Flush ()
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_flush (handle);
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
public void MarkDirty ()
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2008-10-09 22:35:02 +00:00
|
|
|
NativeMethods.cairo_surface_mark_dirty (Handle);
|
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
public void MarkDirty (Rectangle rectangle)
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2008-10-09 22:35:02 +00:00
|
|
|
NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:45:26 +00:00
|
|
|
public IntPtr Handle {
|
|
|
|
get {
|
|
|
|
return handle;
|
2011-02-18 05:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
public PointD DeviceOffset {
|
|
|
|
get {
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2008-10-09 22:35:02 +00:00
|
|
|
double x, y;
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_get_device_offset (handle, out x, out y);
|
2008-10-09 22:35:02 +00:00
|
|
|
return new PointD (x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_set_device_offset (handle, value.X, value.Y);
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
|
|
|
[Obsolete ("Use Dispose()")]
|
2008-10-09 22:35:02 +00:00
|
|
|
public void Destroy()
|
|
|
|
{
|
2013-07-26 16:45:26 +00:00
|
|
|
Dispose ();
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFallbackResolution (double x, double y)
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_set_fallback_resolution (handle, x, y);
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteToPng (string filename)
|
|
|
|
{
|
2013-11-04 23:22:28 +00:00
|
|
|
CheckDisposed ();
|
2013-07-26 16:45:26 +00:00
|
|
|
NativeMethods.cairo_surface_write_to_png (handle, filename);
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
[Obsolete ("Use Handle instead.")]
|
2013-07-26 16:45:26 +00:00
|
|
|
public IntPtr Pointer {
|
|
|
|
get {
|
|
|
|
return handle;
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
}
|
|
|
|
|
2008-10-09 22:35:02 +00:00
|
|
|
public Status Status {
|
2013-11-04 23:22:28 +00:00
|
|
|
get {
|
|
|
|
CheckDisposed ();
|
|
|
|
return NativeMethods.cairo_surface_status (handle);
|
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Content Content {
|
2013-11-04 23:22:28 +00:00
|
|
|
get {
|
|
|
|
CheckDisposed ();
|
|
|
|
return NativeMethods.cairo_surface_get_content (handle);
|
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public SurfaceType SurfaceType {
|
2013-11-04 23:22:28 +00:00
|
|
|
get {
|
|
|
|
CheckDisposed ();
|
|
|
|
return NativeMethods.cairo_surface_get_type (handle);
|
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public uint ReferenceCount {
|
2013-11-04 23:22:28 +00:00
|
|
|
get {
|
|
|
|
return NativeMethods.cairo_surface_get_reference_count (handle); }
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|
2013-07-26 16:45:26 +00:00
|
|
|
}
|
2008-10-09 22:35:02 +00:00
|
|
|
}
|