Mercurial > projects > dwt-win
annotate dwt/graphics/Pattern.d @ 213:36f5cb12e1a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 17 May 2008 17:34:28 +0200 |
parents | ab60f3309436 |
children | fd9c62a2998e |
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 { |
213 | 46 alias Resource.init_ init_; |
25 | 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) { |
213 | 85 super(device); |
25 | 86 if (image is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
87 if (image.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
213 | 88 this.device.checkGDIP(); |
89 int /*long*/[] gdipImage = image.createGdipImage(); | |
90 auto img = cast(Gdip.Image) gdipImage[0]; | |
25 | 91 int width = Gdip.Image_GetWidth(img); |
92 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
|
93 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
|
94 Gdip.Bitmap_delete( cast(Gdip.Bitmap)img); |
25 | 95 if (gdipImage[1] !is 0) { |
27 | 96 auto hHeap = OS.GetProcessHeap (); |
97 OS.HeapFree(hHeap, 0, cast(void*)gdipImage[1]); | |
25 | 98 } |
27 | 99 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
213 | 100 init_(); |
25 | 101 } |
102 | |
103 /** | |
104 * Constructs a new Pattern that represents a linear, two color | |
105 * gradient. Drawing with the pattern will cause the resulting area to be | |
106 * tiled with the gradient specified by the arguments. | |
107 * <p> | |
108 * This operation requires the operating system's advanced | |
109 * graphics subsystem which may not be available on some | |
110 * platforms. | |
111 * </p> | |
112 * | |
113 * @param device the device on which to allocate the pattern | |
114 * @param x1 the x coordinate of the starting corner of the gradient | |
115 * @param y1 the y coordinate of the starting corner of the gradient | |
116 * @param x2 the x coordinate of the ending corner of the gradient | |
117 * @param y2 the y coordinate of the ending corner of the gradient | |
118 * @param color1 the starting color of the gradient | |
119 * @param color2 the ending color of the gradient | |
120 * | |
121 * @exception IllegalArgumentException <ul> | |
122 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, | |
123 * or if either color1 or color2 is null</li> | |
124 * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li> | |
125 * </ul> | |
126 * @exception DWTException <ul> | |
127 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> | |
128 * </ul> | |
129 * @exception DWTError <ul> | |
130 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> | |
131 * </ul> | |
132 * | |
133 * @see #dispose() | |
134 */ | |
27 | 135 public this(Device device, float x1, float y1, float x2, float y2, Color color1, Color color2) { |
25 | 136 this(device, x1, y1, x2, y2, color1, 0xFF, color2, 0xFF); |
137 } | |
138 | |
139 /** | |
140 * Constructs a new Pattern that represents a linear, two color | |
141 * gradient. Drawing with the pattern will cause the resulting area to be | |
142 * tiled with the gradient specified by the arguments. | |
143 * <p> | |
144 * This operation requires the operating system's advanced | |
145 * graphics subsystem which may not be available on some | |
146 * platforms. | |
147 * </p> | |
148 * | |
149 * @param device the device on which to allocate the pattern | |
150 * @param x1 the x coordinate of the starting corner of the gradient | |
151 * @param y1 the y coordinate of the starting corner of the gradient | |
152 * @param x2 the x coordinate of the ending corner of the gradient | |
153 * @param y2 the y coordinate of the ending corner of the gradient | |
154 * @param color1 the starting color of the gradient | |
155 * @param alpha1 the starting alpha value of the gradient | |
156 * @param color2 the ending color of the gradient | |
157 * @param alpha2 the ending alpha value of the gradient | |
158 * | |
159 * @exception IllegalArgumentException <ul> | |
160 * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, | |
161 * or if either color1 or color2 is null</li> | |
162 * <li>ERROR_INVALID_ARGUMENT - if either color1 or color2 has been disposed</li> | |
163 * </ul> | |
164 * @exception DWTException <ul> | |
165 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li> | |
166 * </ul> | |
167 * @exception DWTError <ul> | |
168 * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained</li> | |
169 * </ul> | |
170 * | |
171 * @see #dispose() | |
172 * | |
173 * @since 3.2 | |
174 */ | |
27 | 175 public this(Device device, float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) { |
213 | 176 super(device); |
25 | 177 if (color1 is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
178 if (color1.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
179 if (color2 is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
180 if (color2.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
213 | 181 this.device.checkGDIP(); |
27 | 182 auto colorRef1 = color1.handle; |
25 | 183 int rgb = ((colorRef1 >> 16) & 0xFF) | (colorRef1 & 0xFF00) | ((colorRef1 & 0xFF) << 16); |
27 | 184 auto foreColor = Gdip.Color_new((alpha1 & 0xFF) << 24 | rgb); |
25 | 185 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
|
186 handle = cast(Gdip.Brush)Gdip.SolidBrush_new(foreColor); |
27 | 187 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); |
25 | 188 } else { |
27 | 189 auto colorRef2 = color2.handle; |
25 | 190 rgb = ((colorRef2 >> 16) & 0xFF) | (colorRef2 & 0xFF00) | ((colorRef2 & 0xFF) << 16); |
27 | 191 auto backColor = Gdip.Color_new((alpha2 & 0xFF) << 24 | rgb); |
192 Gdip.PointF p1; | |
25 | 193 p1.X = x1; |
194 p1.Y = y1; | |
27 | 195 Gdip.PointF p2; |
25 | 196 p2.X = x2; |
197 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
|
198 handle = cast(Gdip.Brush)Gdip.LinearGradientBrush_new(p1, p2, foreColor, backColor); |
27 | 199 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
|
200 if (alpha1 != 0xFF || alpha2 !is 0xFF) { |
27 | 201 int a = cast(int)((alpha1 & 0xFF) * 0.5f + (alpha2 & 0xFF) * 0.5f); |
202 int r = cast(int)(((colorRef1 & 0xFF) >> 0) * 0.5f + ((colorRef2 & 0xFF) >> 0) * 0.5f); | |
203 int g = cast(int)(((colorRef1 & 0xFF00) >> 8) * 0.5f + ((colorRef2 & 0xFF00) >> 8) * 0.5f); | |
204 int b = cast(int)(((colorRef1 & 0xFF0000) >> 16) * 0.5f + ((colorRef2 & 0xFF0000) >> 16) * 0.5f); | |
205 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
|
206 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
|
207 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
|
208 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
|
209 c[2] = backColor; |
27 | 210 float[3] f; |
211 f[0] = 0; | |
212 f[1] = 0.5f; | |
213 f[2] = 1; | |
213 | 214 Gdip.LinearGradientBrush_SetInterpolationColors( cast(Gdip.LinearGradientBrush)handle, c.ptr, f.ptr, 3); |
25 | 215 Gdip.Color_delete(midColor); |
216 } | |
217 Gdip.Color_delete(backColor); | |
218 } | |
219 Gdip.Color_delete(foreColor); | |
213 | 220 init_(); |
25 | 221 } |
222 | |
213 | 223 void destroy() { |
25 | 224 int type = Gdip.Brush_GetType(handle); |
225 switch (type) { | |
226 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
|
227 Gdip.SolidBrush_delete(cast(Gdip.SolidBrush)handle); |
25 | 228 break; |
229 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
|
230 Gdip.HatchBrush_delete(cast(Gdip.HatchBrush)handle); |
25 | 231 break; |
232 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
|
233 Gdip.LinearGradientBrush_delete(cast(Gdip.LinearGradientBrush)handle); |
25 | 234 break; |
235 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
|
236 Gdip.TextureBrush_delete(cast(Gdip.TextureBrush)handle); |
25 | 237 break; |
81 | 238 default: |
25 | 239 } |
27 | 240 handle = null; |
25 | 241 } |
242 | |
243 /** | |
244 * Returns <code>true</code> if the Pattern has been disposed, | |
245 * and <code>false</code> otherwise. | |
246 * <p> | |
247 * This method gets the dispose state for the Pattern. | |
248 * When a Pattern has been disposed, it is an error to | |
249 * invoke any other method using the Pattern. | |
250 * | |
251 * @return <code>true</code> when the Pattern is disposed, and <code>false</code> otherwise | |
252 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
27
diff
changeset
|
253 override public bool isDisposed() { |
27 | 254 return handle is null; |
25 | 255 } |
256 | |
257 /** | |
258 * Returns a string containing a concise, human-readable | |
259 * description of the receiver. | |
260 * | |
261 * @return a string representation of the receiver | |
262 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
81
diff
changeset
|
263 override public String toString() { |
25 | 264 if (isDisposed()) return "Pattern {*DISPOSED*}"; |
27 | 265 return Format( "Pattern {{{}}", handle ); |
25 | 266 } |
267 | |
268 } |