Mercurial > projects > dwt-win
annotate dwt/graphics/Pattern.d @ 212:ab60f3309436
reverted the char[] to String and use the an alias.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:12:38 +0200 |
parents | e10cbfc977d2 |
children | 36f5cb12e1a2 |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
26 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
25 | 12 *******************************************************************************/ |
13 module dwt.graphics.Pattern; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.DWTError; | |
17 import dwt.DWTException; | |
18 import dwt.internal.gdip.Gdip; | |
19 import dwt.internal.win32.OS; | |
20 | |
27 | 21 import dwt.graphics.Resource; |
22 import dwt.graphics.Color; | |
23 import dwt.graphics.GC; | |
24 import dwt.graphics.Device; | |
25 import dwt.graphics.Image; | |
26 | |
27 import tango.text.convert.Format; | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
81
diff
changeset
|
28 import dwt.dwthelper.utils; |
27 | 29 |
25 | 30 /** |
31 * Instances of this class represent patterns to use while drawing. Patterns | |
32 * can be specified either as bitmaps or gradients. | |
33 * <p> | |
34 * Application code must explicitly invoke the <code>Pattern.dispose()</code> | |
35 * method to release the operating system resources managed by each instance | |
36 * when those instances are no longer required. | |
37 * </p> | |
38 * <p> | |
39 * This class requires the operating system's advanced graphics subsystem | |
40 * which may not be available on some platforms. | |
41 * </p> | |
42 * | |
43 * @since 3.1 | |
44 */ | |
27 | 45 public class Pattern : Resource { |
25 | 46 |
47 /** | |
48 * the OS resource for the Pattern | |
49 * (Warning: This field is platform dependent) | |
50 * <p> | |
51 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT | |
52 * public API. It is marked public only so that it can be shared | |
53 * within the packages provided by DWT. It is not available on all | |
54 * platforms and should never be accessed from application code. | |
55 * </p> | |
56 */ | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
57 public Gdip.Brush handle; |
25 | 58 |
59 /** | |
60 * Constructs a new Pattern given an image. Drawing with the resulting | |
61 * pattern will cause the image to be tiled over the resulting area. | |
62 * <p> | |
63 * This operation requires the operating system's advanced | |
64 * graphics subsystem which may not be available on some | |
65 * platforms. | |
66 * </p> | |
67 * | |
68 * @param device the device on which to allocate the pattern | |
69 * @param image the image that the pattern will draw | |
70 * | |
71 * @exception IllegalArgumentException <ul> | |
72 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, or the image is null</li> | |
73 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
74 * </ul> | |
75 * @exception DWTException <ul> | |
76 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> | |
77 * </ul> | |
78 * @exception DWTError <ul> | |
79 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> | |
80 * </ul> | |
81 * | |
82 * @see #dispose() | |
83 */ | |
27 | 84 public this(Device device, Image image) { |
25 | 85 if (device is null) device = Device.getDevice(); |
86 if (device is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
87 if (image is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
88 if (image.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
89 this.device = device; | |
90 device.checkGDIP(); | |
91 int[] gdipImage = image.createGdipImage(); | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
92 auto img = cast(Gdip.Image)gdipImage[0]; |
25 | 93 int width = Gdip.Image_GetWidth(img); |
94 int height = Gdip.Image_GetHeight(img); | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
95 handle = cast(Gdip.Brush)Gdip.TextureBrush_new(img, Gdip.WrapModeTile, 0, 0, width, height); |
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
96 Gdip.Bitmap_delete( cast(Gdip.Bitmap)img); |
25 | 97 if (gdipImage[1] !is 0) { |
27 | 98 auto hHeap = OS.GetProcessHeap (); |
99 OS.HeapFree(hHeap, 0, cast(void*)gdipImage[1]); | |
25 | 100 } |
27 | 101 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
25 | 102 if (device.tracking) device.new_Object(this); |
103 } | |
104 | |
105 /** | |
106 * Constructs a new Pattern that represents a linear, two color | |
107 * gradient. Drawing with the pattern will cause the resulting area to be | |
108 * tiled with the gradient specified by the arguments. | |
109 * <p> | |
110 * This operation requires the operating system's advanced | |
111 * graphics subsystem which may not be available on some | |
112 * platforms. | |
113 * </p> | |
114 * | |
115 * @param device the device on which to allocate the pattern | |
116 * @param x1 the x coordinate of the starting corner of the gradient | |
117 * @param y1 the y coordinate of the starting corner of the gradient | |
118 * @param x2 the x coordinate of the ending corner of the gradient | |
119 * @param y2 the y coordinate of the ending corner of the gradient | |
120 * @param color1 the starting color of the gradient | |
121 * @param color2 the ending color of the gradient | |
122 * | |
123 * @exception IllegalArgumentException <ul> | |
124 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, | |
125 * or if either color1 or color2 is null</li> | |
126 * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li> | |
127 * </ul> | |
128 * @exception DWTException <ul> | |
129 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> | |
130 * </ul> | |
131 * @exception DWTError <ul> | |
132 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> | |
133 * </ul> | |
134 * | |
135 * @see #dispose() | |
136 */ | |
27 | 137 public this(Device device, float x1, float y1, float x2, float y2, Color color1, Color color2) { |
25 | 138 this(device, x1, y1, x2, y2, color1, 0xFF, color2, 0xFF); |
139 } | |
140 | |
141 /** | |
142 * Constructs a new Pattern that represents a linear, two color | |
143 * gradient. Drawing with the pattern will cause the resulting area to be | |
144 * tiled with the gradient specified by the arguments. | |
145 * <p> | |
146 * This operation requires the operating system's advanced | |
147 * graphics subsystem which may not be available on some | |
148 * platforms. | |
149 * </p> | |
150 * | |
151 * @param device the device on which to allocate the pattern | |
152 * @param x1 the x coordinate of the starting corner of the gradient | |
153 * @param y1 the y coordinate of the starting corner of the gradient | |
154 * @param x2 the x coordinate of the ending corner of the gradient | |
155 * @param y2 the y coordinate of the ending corner of the gradient | |
156 * @param color1 the starting color of the gradient | |
157 * @param alpha1 the starting alpha value of the gradient | |
158 * @param color2 the ending color of the gradient | |
159 * @param alpha2 the ending alpha value of the gradient | |
160 * | |
161 * @exception IllegalArgumentException <ul> | |
162 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, | |
163 * or if either color1 or color2 is null</li> | |
164 * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li> | |
165 * </ul> | |
166 * @exception DWTException <ul> | |
167 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> | |
168 * </ul> | |
169 * @exception DWTError <ul> | |
170 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> | |
171 * </ul> | |
172 * | |
173 * @see #dispose() | |
174 * | |
175 * @since 3.2 | |
176 */ | |
27 | 177 public this(Device device, float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) { |
25 | 178 if (device is null) device = Device.getDevice(); |
179 if (device is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
180 if (color1 is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
181 if (color1.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
182 if (color2 is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
183 if (color2.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
184 this.device = device; | |
185 device.checkGDIP(); | |
27 | 186 auto colorRef1 = color1.handle; |
25 | 187 int rgb = ((colorRef1 >> 16) & 0xFF) | (colorRef1 & 0xFF00) | ((colorRef1 & 0xFF) << 16); |
27 | 188 auto foreColor = Gdip.Color_new((alpha1 & 0xFF) << 24 | rgb); |
25 | 189 if (x1 is x2 && y1 is y2) { |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
190 handle = cast(Gdip.Brush)Gdip.SolidBrush_new(foreColor); |
27 | 191 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
25 | 192 } else { |
27 | 193 auto colorRef2 = color2.handle; |
25 | 194 rgb = ((colorRef2 >> 16) & 0xFF) | (colorRef2 & 0xFF00) | ((colorRef2 & 0xFF) << 16); |
27 | 195 auto backColor = Gdip.Color_new((alpha2 & 0xFF) << 24 | rgb); |
196 Gdip.PointF p1; | |
25 | 197 p1.X = x1; |
198 p1.Y = y1; | |
27 | 199 Gdip.PointF p2; |
25 | 200 p2.X = x2; |
201 p2.Y = y2; | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
202 handle = cast(Gdip.Brush)Gdip.LinearGradientBrush_new(p1, p2, foreColor, backColor); |
27 | 203 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
204 if (alpha1 != 0xFF || alpha2 !is 0xFF) { |
27 | 205 int a = cast(int)((alpha1 & 0xFF) * 0.5f + (alpha2 & 0xFF) * 0.5f); |
206 int r = cast(int)(((colorRef1 & 0xFF) >> 0) * 0.5f + ((colorRef2 & 0xFF) >> 0) * 0.5f); | |
207 int g = cast(int)(((colorRef1 & 0xFF00) >> 8) * 0.5f + ((colorRef2 & 0xFF00) >> 8) * 0.5f); | |
208 int b = cast(int)(((colorRef1 & 0xFF0000) >> 16) * 0.5f + ((colorRef2 & 0xFF0000) >> 16) * 0.5f); | |
209 auto midColor = Gdip.Color_new(a << 24 | r << 16 | g << 8 | b); | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
210 Gdip.ARGB[3] c; |
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
211 c[0] = foreColor; |
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
212 c[1] = midColor; |
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
213 c[2] = backColor; |
27 | 214 float[3] f; |
215 f[0] = 0; | |
216 f[1] = 0.5f; | |
217 f[2] = 1; | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
218 Gdip.LinearGradientBrush_SetInterpolationColors( cast(Gdip.LinearGradientBrush)handle, c, f, 3); |
25 | 219 Gdip.Color_delete(midColor); |
220 } | |
221 Gdip.Color_delete(backColor); | |
222 } | |
223 Gdip.Color_delete(foreColor); | |
224 if (device.tracking) device.new_Object(this); | |
225 } | |
226 | |
227 /** | |
228 * Disposes of the operating system resources associated with | |
229 * the Pattern. Applications must dispose of all Patterns that | |
230 * they allocate. | |
231 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
27
diff
changeset
|
232 override public void dispose() { |
27 | 233 if (handle is null) return; |
25 | 234 if (device.isDisposed()) return; |
235 int type = Gdip.Brush_GetType(handle); | |
236 switch (type) { | |
237 case Gdip.BrushTypeSolidColor: | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
238 Gdip.SolidBrush_delete(cast(Gdip.SolidBrush)handle); |
25 | 239 break; |
240 case Gdip.BrushTypeHatchFill: | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
241 Gdip.HatchBrush_delete(cast(Gdip.HatchBrush)handle); |
25 | 242 break; |
243 case Gdip.BrushTypeLinearGradient: | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
244 Gdip.LinearGradientBrush_delete(cast(Gdip.LinearGradientBrush)handle); |
25 | 245 break; |
246 case Gdip.BrushTypeTextureFill: | |
53
0405e18fec7f
Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
John Reimer <terminal.node@gmail.com
parents:
48
diff
changeset
|
247 Gdip.TextureBrush_delete(cast(Gdip.TextureBrush)handle); |
25 | 248 break; |
81 | 249 default: |
25 | 250 } |
27 | 251 handle = null; |
25 | 252 if (device.tracking) device.dispose_Object(this); |
253 device = null; | |
254 } | |
255 | |
256 /** | |
257 * Returns <code>true</code> if the Pattern has been disposed, | |
258 * and <code>false</code> otherwise. | |
259 * <p> | |
260 * This method gets the dispose state for the Pattern. | |
261 * When a Pattern has been disposed, it is an error to | |
262 * invoke any other method using the Pattern. | |
263 * | |
264 * @return <code>true</code> when the Pattern is disposed, and <code>false</code> otherwise | |
265 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
27
diff
changeset
|
266 override public bool isDisposed() { |
27 | 267 return handle is null; |
25 | 268 } |
269 | |
270 /** | |
271 * Returns a string containing a concise, human-readable | |
272 * description of the receiver. | |
273 * | |
274 * @return a string representation of the receiver | |
275 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
81
diff
changeset
|
276 override public String toString() { |
25 | 277 if (isDisposed()) return "Pattern {*DISPOSED*}"; |
27 | 278 return Format( "Pattern {{{}}", handle ); |
25 | 279 } |
280 | |
281 } |