# HG changeset patch # User Frank Benoit # Date 1199695199 -3600 # Node ID 3c2d10c00f61ae5b27f6647190f742ceecdd29ab # Parent b5521a2c27c2b1bb2281c1b32af6775928d7a594 Transform diff -r b5521a2c27c2 -r 3c2d10c00f61 dwt/graphics/Transform.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/graphics/Transform.d Mon Jan 07 09:39:59 2008 +0100 @@ -0,0 +1,366 @@ +/******************************************************************************* + * Copyright (c) 2000, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +module dwt.graphics.Transform; + +import dwt.SWT; +import dwt.internal.Compatibility; +import dwt.internal.cairo.Cairo; +import dwt.internal.gtk.c.cairotypes; +import dwt.graphics.Resource; +import dwt.graphics.Device; + +import tango.text.convert.Format; + +/** + * Instances of this class represent transformation matrices for + * points expressed as (x, y) pairs of floating point numbers. + *

+ * Application code must explicitly invoke the Transform.dispose() + * method to release the operating system resources managed by each instance + * when those instances are no longer required. + *

+ *

+ * This class requires the operating system's advanced graphics subsystem + * which may not be available on some platforms. + *

+ * + * @since 3.1 + */ +public class Transform : Resource { + /** + * the OS resource for the Transform + * (Warning: This field is platform dependent) + *

+ * IMPORTANT: This field is not part of the SWT + * public API. It is marked public only so that it can be shared + * within the packages provided by SWT. It is not available on all + * platforms and should never be accessed from application code. + *

+ */ + public double[] handle; + +/** + * Constructs a new identity Transform. + *

+ * This operation requires the operating system's advanced + * graphics subsystem which may not be available on some + * platforms. + *

+ * + * @param device the device on which to allocate the Transform + * + * @exception IllegalArgumentException + * @exception SWTException + * @exception SWTError + * + * @see #dispose() + */ +public this (Device device) { + this(device, 1, 0, 0, 1, 0, 0); +} + +/** + * Constructs a new Transform given an array of elements that represent the + * matrix that describes the transformation. + *

+ * This operation requires the operating system's advanced + * graphics subsystem which may not be available on some + * platforms. + *

+ * + * @param device the device on which to allocate the Transform + * @param elements an array of floats that describe the transformation matrix + * + * @exception IllegalArgumentException + * @exception SWTException + * @exception SWTError + * + * @see #dispose() + */ +public this(Device device, float[] elements) { + this (device, checkTransform(elements)[0], elements[1], elements[2], elements[3], elements[4], elements[5]); +} + +/** + * Constructs a new Transform given all of the elements that represent the + * matrix that describes the transformation. + *

+ * This operation requires the operating system's advanced + * graphics subsystem which may not be available on some + * platforms. + *

+ * + * @param device the device on which to allocate the Transform + * @param m11 the first element of the first row of the matrix + * @param m12 the second element of the first row of the matrix + * @param m21 the first element of the second row of the matrix + * @param m22 the second element of the second row of the matrix + * @param dx the third element of the first row of the matrix + * @param dy the third element of the second row of the matrix + * + * @exception IllegalArgumentException + * @exception SWTException + * @exception SWTError + * + * @see #dispose() + */ +public this (Device device, float m11, float m12, float m21, float m22, float dx, float dy) { + if (device is null) device = Device.getDevice(); + if (device is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + this.device = device; + device.checkCairo(); + handle = new double[6]; + if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES); + Cairo.cairo_matrix_init( cast(cairo_matrix_t*)handle.ptr, m11, m12, m21, m22, dx, dy); + if (device.tracking) device.new_Object(this); +} + +static float[] checkTransform(float[] elements) { + if (elements == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + return elements; +} + +/** + * Disposes of the operating system resources associated with + * the Transform. Applications must dispose of all Transforms that + * they allocate. + */ +public void dispose() { + if (handle is null) return; + if (device.isDisposed()) return; + handle = null; + if (device.tracking) device.dispose_Object(this); + device = null; +} + +/** + * Fills the parameter with the values of the transformation matrix + * that the receiver represents, in the order {m11, m12, m21, m22, dx, dy}. + * + * @param elements array to hold the matrix values + * + * @exception SWTException + * @exception IllegalArgumentException + */ +public void getElements(float[] elements) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + if (elements is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + elements[0] = cast(float)handle[0]; + elements[1] = cast(float)handle[1]; + elements[2] = cast(float)handle[2]; + elements[3] = cast(float)handle[3]; + elements[4] = cast(float)handle[4]; + elements[5] = cast(float)handle[5]; +} + +/** + * Modifies the receiver such that the matrix it represents becomes the + * the mathematical inverse of the matrix it previously represented. + * + * @exception SWTException + */ +public void invert() { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + if (Cairo.cairo_matrix_invert(cast(cairo_matrix_t*)handle.ptr) != 0) { + SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX); + } +} + +/** + * Returns true if the Transform has been disposed, + * and false otherwise. + *

+ * This method gets the dispose state for the Transform. + * When a Transform has been disposed, it is an error to + * invoke any other method using the Transform. + * + * @return true when the Transform is disposed, and false otherwise + */ +public bool isDisposed() { + return handle is null; +} + +/** + * Returns true if the Transform represents the identity matrix + * and false otherwise. + * + * @return true if the receiver is an identity Transform, and false otherwise + */ +public bool isIdentity() { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + float[] m = new float[6]; + getElements(m); + return m[0] == 1 && m[1] == 0 && m[2] == 0 && m[3] == 1 && m[4] == 0 && m[5] == 0; +} + +/** + * Modifies the receiver such that the matrix it represents becomes the + * the result of multiplying the matrix it previously represented by the + * argument. + * + * @param matrix the matrix to multiply the receiver by + * + * @exception SWTException

+ * @exception IllegalArgumentException + */ +public void multiply(Transform matrix) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + if (matrix == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (matrix.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + Cairo.cairo_matrix_multiply(cast(cairo_matrix_t*)handle.ptr,cast(cairo_matrix_t*)matrix.handle.ptr, cast(cairo_matrix_t*)handle.ptr); +} + +/** + * Modifies the receiver so that it represents a transformation that is + * equivalent to its previous transformation rotated by the specified angle. + * The angle is specified in degrees and for the identity transform 0 degrees + * is at the 3 o'clock position. A positive value indicates a clockwise rotation + * while a negative value indicates a counter-clockwise rotation. + * + * @param angle the angle to rotate the transformation by + * + * @exception SWTException + */ +public void rotate(float angle) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + Cairo.cairo_matrix_rotate(cast(cairo_matrix_t*)handle.ptr, angle * cast(float)Compatibility.PI / 180); +} + +/** + * Modifies the receiver so that it represents a transformation that is + * equivalent to its previous transformation scaled by (scaleX, scaleY). + * + * @param scaleX the amount to scale in the X direction + * @param scaleY the amount to scale in the Y direction + * + * @exception SWTException + */ +public void scale(float scaleX, float scaleY) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + Cairo.cairo_matrix_scale(cast(cairo_matrix_t*)handle.ptr, scaleX, scaleY); +} + +/** + * Modifies the receiver to represent a new transformation given all of + * the elements that represent the matrix that describes that transformation. + * + * @param m11 the first element of the first row of the matrix + * @param m12 the second element of the first row of the matrix + * @param m21 the first element of the second row of the matrix + * @param m22 the second element of the second row of the matrix + * @param dx the third element of the first row of the matrix + * @param dy the third element of the second row of the matrix + * + * @exception SWTException + */ +public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + Cairo.cairo_matrix_init(cast(cairo_matrix_t*)handle.ptr, m11, m12, m21, m22, dx, dy); +} + +/** + * Given an array containing points described by alternating x and y values, + * modify that array such that each point has been replaced with the result of + * applying the transformation represented by the receiver to that point. + * + * @param pointArray an array of alternating x and y values to be transformed + * + * @exception IllegalArgumentException + * @exception SWTException + */ +public void transform(float[] pointArray) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + double dx, dy; + int length = pointArray.length / 2; + for (int i = 0, j = 0; i < length; i++, j += 2) { + dx = pointArray[j]; + dy = pointArray[j + 1]; + Cairo.cairo_matrix_transform_point(cast(cairo_matrix_t*)handle.ptr, &dx, &dy); + pointArray[j] = cast(float)dx; + pointArray[j + 1] = cast(float)dy; + } +} + +/** + * Modifies the receiver so that it represents a transformation that is + * equivalent to its previous transformation translated by (offsetX, offsetY). + * + * @param offsetX the distance to translate in the X direction + * @param offsetY the distance to translate in the Y direction + * + * @exception SWTException + */ +public void translate(float offsetX, float offsetY) { + if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); + Cairo.cairo_matrix_translate(cast(cairo_matrix_t*)handle.ptr, offsetX, offsetY); +} + +/** + * Returns a string containing a concise, human-readable + * description of the receiver. + * + * @return a string representation of the receiver + */ +public char[] toString() { + if (isDisposed()) return "Transform {*DISPOSED*}"; + float[] elements = new float[6]; + getElements(elements); + return Format( "Transform {{{}}", elements ); +} + +} diff -r b5521a2c27c2 -r 3c2d10c00f61 todo.txt --- a/todo.txt Mon Jan 07 09:25:10 2008 +0100 +++ b/todo.txt Mon Jan 07 09:39:59 2008 +0100 @@ -53,7 +53,7 @@ graphics/RGB // OK graphics/TextLayout graphics/TextStyle // OK -graphics/Transform +graphics/Transform // OK widgets/TableColumn widgets/DirectoryDialog