Mercurial > projects > dwt-win
changeset 21:d2e87572b721
TextStyle, Transform
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 26 Jan 2008 16:45:05 +0100 |
parents | 456f604d8a07 |
children | 5f2e72114476 |
files | dwt/graphics/TextStyle.d dwt/graphics/Transform.d dwt/internal/gdip/Gdip.d |
diffstat | 3 files changed, 599 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/graphics/TextStyle.d Sat Jan 26 16:45:05 2008 +0100 @@ -0,0 +1,203 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 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 + * Port to the D programming language: + * Frank Benoit <benoit@tionex.de> + *******************************************************************************/ +module dwt.graphics.TextStyle; + +import dwt.DWT; +import dwt.graphics.Font; +import dwt.graphics.Color; +import dwt.graphics.GlyphMetrics; + +import tango.util.Convert; +/** + * <code>TextStyle</code> defines a set of styles that can be applied + * to a range of text. + * <p> + * The hashCode() method in this class uses the values of the public + * fields to compute the hash value. When storing instances of the + * class in hashed collections, do not modify these fields after the + * object has been inserted. + * </p> + * <p> + * Application code does <em>not</em> need to explicitly release the + * resources managed by each instance when those instances are no longer + * required, and thus no <code>dispose()</code> method is provided. + * </p> + * + * @see TextLayout + * @see Font + * @see Color + * + * @since 3.0 + */ +public class TextStyle { + + /** + * the font of the style + */ + public Font font; + + /** + * the foreground of the style + */ + public Color foreground; + + /** + * the background of the style + */ + public Color background; + + /** + * the underline flag of the style + * + * @since 3.1 + */ + public bool underline; + + /** + * the strikeout flag of the style + * + * @since 3.1 + */ + public bool strikeout; + + /** + * the GlyphMetrics of the style + * + * @since 3.2 + */ + public GlyphMetrics metrics; + + /** + * the baseline rise of the style. + * + * @since 3.2 + */ + public int rise; + +/** + * Create a new text style with the specified font, foreground + * and background. + * + * @param font the font of the style, <code>null</code> if none + * @param foreground the foreground color of the style, <code>null</code> if none + * @param background the background color of the style, <code>null</code> if none + */ +public this (Font font, Color foreground, Color background) { + if (font !is null && font.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT); + if (foreground !is null && foreground.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT); + if (background !is null && background.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT); + this.font = font; + this.foreground = foreground; + this.background = background; +} + +/** + * Compares the argument to the receiver, and returns true + * if they represent the <em>same</em> object using a class + * specific comparison. + * + * @param object the object to compare with this object + * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise + * + * @see #hashCode() + */ +public override int opEquals(Object object) { + if (object is this) return true; + if (object is null) return false; + if (!(cast(TextStyle)object)) return false; + TextStyle style = cast(TextStyle)object; + if (foreground !is null) { + if ( foreground != style.foreground ) return false; + } else if (style.foreground !is null) return false; + if (background !is null) { + if ( background != style.background ) return false; + } else if (style.background !is null) return false; + if (font !is null) { + if (font != style.font) return false; + } else if (style.font !is null) return false; + if (metrics !is null || style.metrics !is null) return false; + if (underline !is style.underline) return false; + if (strikeout !is style.strikeout) return false; + if (rise !is style.rise) return false; + return true; +} + +/** + * Returns an integer hash code for the receiver. Any two + * objects that return <code>true</code> when passed to + * <code>equals</code> must return the same value for this + * method. + * + * @return the receiver's hash + * + * @see #equals(Object) + */ +public override hash_t toHash() { + int hash = 0; + if (foreground !is null) hash ^= foreground.toHash(); + if (background !is null) hash ^= background.toHash(); + if (font !is null) hash ^= font.toHash(); + if (metrics !is null) hash ^= metrics.toHash(); + if (underline) hash ^= hash; + if (strikeout) hash ^= hash; + hash ^= rise; + return hash; +} + +/** + * Returns a string containing a concise, human-readable + * description of the receiver. + * + * @return a string representation of the <code>TextStyle</code> + */ +public char[] toString () { + char[] buffer = "TextStyle {"; + int startLength = buffer.length; + if (font != null) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "font="; + buffer ~= font.toString; + } + if (foreground != null) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "foreground="; + buffer ~= foreground.toString; + } + if (background != null) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "background="; + buffer ~= background.toString; + } + if (underline) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "underlined"; + } + if (strikeout) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "striked out"; + } + if (rise != 0) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "rise="; + buffer ~= to!(char[])(rise); + } + if (metrics != null) { + if (buffer.length > startLength) buffer ~= ", "; + buffer ~= "metrics="; + buffer ~= metrics.toString; + } + buffer ~= "}"; + return buffer; +} + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/graphics/Transform.d Sat Jan 26 16:45:05 2008 +0100 @@ -0,0 +1,351 @@ +/******************************************************************************* + * 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.DWT; +import dwt.DWTError; +import dwt.DWTException; +import dwt.internal.gdip.Gdip; + +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. + * <p> + * Application code must explicitly invoke the <code>Transform.dispose()</code> + * method to release the operating system resources managed by each instance + * when those instances are no longer required. + * </p> + * <p> + * This class requires the operating system's advanced graphics subsystem + * which may not be available on some platforms. + * </p> + * + * @since 3.1 + */ +public class Transform : Resource { + + /** + * the OS resource for the Transform + * (Warning: This field is platform dependent) + * <p> + * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT + * public API. It is marked public only so that it can be shared + * within the packages provided by DWT. It is not available on all + * platforms and should never be accessed from application code. + * </p> + */ + public Matrix* handle; + +/** + * Constructs a new identity Transform. + * <p> + * This operation requires the operating system's advanced + * graphics subsystem which may not be available on some + * platforms. + * </p> + * + * @param device the device on which to allocate the Transform + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> + * </ul> + * @exception DWTException <ul> + * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> + * </ul> + * @exception DWTError <ul> + * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li> + * </ul> + * + * @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. + * <p> + * This operation requires the operating system's advanced + * graphics subsystem which may not be available on some + * platforms. + * </p> + * + * @param device the device on which to allocate the Transform + * @param elements an array of floats that describe the transformation matrix + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device, or the elements array is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the elements array is too small to hold the matrix values</li> + * </ul> + * @exception DWTException <ul> + * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> + * </ul> + * @exception DWTError <ul> + * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li> + * </ul> + * + * @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. + * <p> + * This operation requires the operating system's advanced + * graphics subsystem which may not be available on some + * platforms. + * </p> + * + * @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 <ul> + * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> + * </ul> + * @exception DWTException <ul> + * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> + * </ul> + * @exception DWTError <ul> + * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li> + * </ul> + * + * @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) DWT.error(DWT.ERROR_NULL_ARGUMENT); + this.device = device; + device.checkGDIP(); + handle = Gdip.Matrix_new(m11, m12, m21, m22, dx, dy); + if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); + if (device.tracking) device.new_Object(this); +} + +static float[] checkTransform(float[] elements) { + if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); + if (elements.length < 6) DWT.error(DWT.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; + Gdip.Matrix_delete(handle); + 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 DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the matrix values</li> + * </ul> + */ +public void getElements(float[] elements) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); + if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT); + Gdip.Matrix_GetElements(handle, elements.ptr); +} + +/** + * Modifies the receiver such that the matrix it represents becomes the + * the mathematical inverse of the matrix it previously represented. + * + * @exception DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li> + * </ul> + */ +public void invert() { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + if (Gdip.Matrix_Invert(handle) !is 0) DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX); +} + +/** + * Returns <code>true</code> if the Transform has been disposed, + * and <code>false</code> otherwise. + * <p> + * 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 <code>true</code> when the Transform is disposed, and <code>false</code> otherwise + */ +public bool isDisposed() { + return handle is null; +} + +/** + * Returns <code>true</code> if the Transform represents the identity matrix + * and false otherwise. + * + * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise + */ +public bool isIdentity() { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + return cast(bool) Gdip.Matrix_IsIdentity(handle); +} + +/** + * 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 DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the parameter has been disposed</li> + * </ul> + */ +public void multiply(Transform matrix) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + if (matrix is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); + if (matrix.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); + Gdip.Matrix_Multiply(handle, matrix.handle, Gdip.MatrixOrderPrepend); +} + +/** + * 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 DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + */ +public void rotate(float angle) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + Gdip.Matrix_Rotate(handle, angle, Gdip.MatrixOrderPrepend); +} + +/** + * 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 DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + */ +public void scale(float scaleX, float scaleY) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + Gdip.Matrix_Scale(handle, scaleX, scaleY, Gdip.MatrixOrderPrepend); +} + +/** + * 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 DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + */ +public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + Gdip.Matrix_SetElements(handle, 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 <ul> + * <li>ERROR_NULL_ARGUMENT - if the point array is null</li> + * </ul> + * @exception DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + */ +public void transform(float[] pointArray) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); + Gdip.Matrix_TransformPoints(handle, cast(PointF*)pointArray.ptr, pointArray.length / 2); +} + +/** + * 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 DWTException <ul> + * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> + * </ul> + */ +public void translate(float offsetX, float offsetY) { + if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); + Gdip.Matrix_Translate(handle, offsetX, offsetY, Gdip.MatrixOrderPrepend); +} + +/** + * 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[6] elements; + getElements(elements); + return Format("Transform {{{},{},{},{},{}}", elements [0], elements [1], elements [2], elements [3], elements [4], elements [5] ); +} + +}
--- a/dwt/internal/gdip/Gdip.d Sat Jan 26 14:19:27 2008 +0100 +++ b/dwt/internal/gdip/Gdip.d Sat Jan 26 16:45:05 2008 +0100 @@ -13,6 +13,40 @@ import dwt.internal.Library; import dwt.internal.Platform; +public struct RectF { + public float X; + public float Y; + public float Width; + public float Height; +} + +public struct Rect { + public int X; + public int Y; + public int Width; + public int Height; +} +public struct PointF { + public float X; + public float Y; +} + +public struct ColorPalette { + public int Flags; + public int Count; + public int[1] Entries; +} +public struct BitmapData { + public int Width; + public int Height; + public int Stride; + public int PixelFormat; + public void* Scan0; + public int Reserved; +} + +struct Matrix{} + alias extern(Windows) void function(int level, char *message) DebugEventProc; struct GdiplusStartupInput { @@ -28,7 +62,6 @@ NotificationUnhookProc NotificationUnhook; }; - struct API { static extern(Windows) int GdiplusStartup( uint** token, @@ -165,8 +198,17 @@ public alias API.GdiplusStartup GdiplusStartup; public alias API.GdiplusShutdown GdiplusShutdown; - - + public static extern(Windows) Matrix* Matrix_new(float m11, float m12, float m21, float m22, float dx, float dy); + public static extern(Windows) void Matrix_delete(Matrix*); + public static extern(Windows) int Matrix_GetElements(Matrix*,float* m); + public static extern(Windows) int Matrix_Invert(Matrix*); + public static extern(Windows) int Matrix_IsIdentity(Matrix*); + public static extern(Windows) int Matrix_Multiply(Matrix*,Matrix*,int); + public static extern(Windows) int Matrix_Rotate(Matrix*,float,int); + public static extern(Windows) int Matrix_Scale(Matrix*,float,float,int); + public static extern(Windows) int Matrix_SetElements(Matrix*,float m11, float m12, float m21, float m22, float dx, float dy); + public static extern(Windows) int Matrix_TransformPoints(Matrix*,PointF*, int); + public static extern(Windows) int Matrix_Translate(Matrix*,float, float, float); /++ /** 64 bit */