Mercurial > projects > dwt-addons
annotate dwtx/ui/internal/forms/widgets/FormImages.d @ 200:eb3414669eb0 default tip
fix for dmd 1.041 and tango 0.99.8
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 28 Mar 2009 03:09:57 +0100 |
parents | 04b47443bb01 |
children |
rev | line source |
---|---|
75 | 1 /******************************************************************************* |
90 | 2 * Copyright (c) 2007, 2008 IBM Corporation and others. |
75 | 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 | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.ui.internal.forms.widgets.FormImages; | |
14 | |
15 //import dwtx.ui.internal.forms.widgets.FormImages; | |
16 | |
17 import dwt.graphics.Color; | |
18 import dwt.graphics.GC; | |
19 import dwt.graphics.Image; | |
90 | 20 import dwt.graphics.RGB; |
75 | 21 import dwt.widgets.Display; |
22 | |
23 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
24 import dwtx.dwtxhelper.Collection; |
75 | 25 |
26 public class FormImages { | |
27 private static FormImages instance; | |
28 | |
29 public static FormImages getInstance() { | |
30 if (instance is null) | |
31 instance = new FormImages(); | |
32 return instance; | |
33 } | |
34 | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
35 private HashMap images; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
36 private HashMap ids; |
75 | 37 |
38 private this() { | |
39 } | |
40 | |
41 private abstract class ImageIdentifier { | |
42 Display fDisplay; | |
90 | 43 RGB[] fRGBs; |
75 | 44 int fLength; |
45 | |
46 this(Display display, Color[] colors, int length) { | |
47 fDisplay = display; | |
90 | 48 fRGBs = new RGB[colors.length]; |
49 for (int i = 0; i < colors.length; i++) { | |
50 Color color = colors[i]; | |
51 fRGBs[i] = color is null ? null : color.getRGB(); | |
52 } | |
75 | 53 fLength = length; |
54 } | |
55 | |
56 public bool equals(Object obj) { | |
57 if (null !is cast(ImageIdentifier)obj ) { | |
58 ImageIdentifier id = cast(ImageIdentifier)obj; | |
90 | 59 if (id.fRGBs.length is fRGBs.length) { |
76 | 60 bool result = id.fDisplay.opEquals(fDisplay) && id.fLength is fLength; |
90 | 61 for (int i = 0; i < fRGBs.length && result; i++) { |
62 result = result && id.fRGBs[i].opEquals(fRGBs[i]); | |
75 | 63 } |
64 return result; | |
65 } | |
66 } | |
67 return false; | |
68 } | |
69 | |
70 public override hash_t toHash() { | |
71 int hash = fDisplay.toHash(); | |
90 | 72 for (int i = 0; i < fRGBs.length; i++) |
73 hash = hash * 7 + fRGBs[i].toHash(); | |
75 | 74 hash = hash * 7 + fLength; |
75 return hash; | |
76 } | |
77 } | |
78 | |
79 private class SimpleImageIdentifier : ImageIdentifier{ | |
80 private int fTheight; | |
81 private int fMarginHeight; | |
82 | |
83 this (Display display, Color color1, Color color2, | |
84 int realtheight, int theight, int marginHeight) { | |
85 super(display, [color1, color2], realtheight); | |
86 fTheight = theight; | |
87 fMarginHeight = marginHeight; | |
88 } | |
89 | |
90 public bool equals(Object obj) { | |
91 if (null !is cast(SimpleImageIdentifier)obj ) { | |
92 SimpleImageIdentifier id = cast(SimpleImageIdentifier) obj; | |
93 if (super.equals(obj) && | |
94 id.fTheight is fTheight && id.fMarginHeight is fMarginHeight) | |
95 return true; | |
96 } | |
97 return false; | |
98 } | |
99 | |
100 public override hash_t toHash() { | |
101 int hash = super.toHash(); | |
102 hash = hash * 7 + (new Integer(fTheight)).toHash(); | |
103 hash = hash * 7 + (new Integer(fMarginHeight)).toHash(); | |
104 return hash; | |
105 } | |
106 } | |
107 | |
108 private class ComplexImageIdentifier : ImageIdentifier { | |
90 | 109 RGB fBgRGB; |
75 | 110 bool fVertical; |
111 int[] fPercents; | |
112 | |
113 public this(Display display, Color[] colors, int length, | |
114 int[] percents, bool vertical, Color bg) { | |
115 super(display, colors, length); | |
90 | 116 fBgRGB = bg is null ? null : bg.getRGB(); |
75 | 117 fVertical = vertical; |
118 fPercents = percents; | |
119 } | |
120 | |
121 public bool equals(Object obj) { | |
122 if (null !is cast(ComplexImageIdentifier)obj ) { | |
123 ComplexImageIdentifier id = cast(ComplexImageIdentifier) obj; | |
124 if (super.equals(obj) && | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
76
diff
changeset
|
125 id.fVertical is fVertical && ArrayEquals(id.fPercents, fPercents)) { |
90 | 126 if ((id.fBgRGB is null && fBgRGB is null) || |
127 (id.fBgRGB !is null && id.fBgRGB.opEquals(fBgRGB))) | |
75 | 128 return true; |
129 // if the only thing that isn't the same is the background color | |
130 // still return true if it does not matter (percents add up to 100) | |
131 int sum = 0; | |
132 for (int i = 0; i < fPercents.length; i++) | |
133 sum += fPercents[i]; | |
134 if (sum >= 100) | |
135 return true; | |
136 } | |
137 } | |
138 return false; | |
139 } | |
140 | |
141 public override hash_t toHash() { | |
142 int hash = super.toHash(); | |
143 hash = hash * 7 + (new Boolean(fVertical)).toHash(); | |
144 for (int i = 0; i < fPercents.length; i++) | |
145 hash = hash * 7 + (new Integer(fPercents[i])).toHash(); | |
146 return hash; | |
147 } | |
148 } | |
149 | |
150 private class ImageReference { | |
151 private Image fImage; | |
152 private int fCount; | |
153 | |
154 public this(Image image) { | |
155 fImage = image; | |
156 fCount = 1; | |
157 } | |
158 | |
159 public Image getImage() { | |
160 return fImage; | |
161 } | |
162 // returns a bool indicating if all clients of this image are finished | |
163 // a true result indicates the underlying image should be disposed | |
164 public bool decCount() { | |
165 return --fCount is 0; | |
166 } | |
167 public void incCount() { | |
168 fCount++; | |
169 } | |
170 } | |
171 | |
172 public Image getGradient(Display display, Color color1, Color color2, | |
173 int realtheight, int theight, int marginHeight) { | |
174 checkHashMaps(); | |
175 ImageIdentifier id = new SimpleImageIdentifier(display, color1, color2, realtheight, theight, marginHeight); | |
176 ImageReference result = cast(ImageReference) images.get(id); | |
177 if (result !is null && !result.getImage().isDisposed()) { | |
178 result.incCount(); | |
179 return result.getImage(); | |
180 } | |
181 Image image = createGradient(display, color1, color2, realtheight, theight, marginHeight); | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
182 images.put(id, new ImageReference(image)); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
183 ids.put(image, id); |
75 | 184 return image; |
185 } | |
186 | |
187 public Image getGradient(Display display, Color[] colors, int[] percents, | |
188 int length, bool vertical, Color bg) { | |
189 checkHashMaps(); | |
190 ImageIdentifier id = new ComplexImageIdentifier(display, colors, length, percents, vertical, bg); | |
191 ImageReference result = cast(ImageReference) images.get(id); | |
192 if (result !is null && !result.getImage().isDisposed()) { | |
193 result.incCount(); | |
194 return result.getImage(); | |
195 } | |
196 Image image = createGradient(display, colors, percents, length, vertical, bg); | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
197 images.put(id, new ImageReference(image)); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
198 ids.put(image, id); |
75 | 199 return image; |
200 } | |
201 | |
202 public bool markFinished(Image image) { | |
203 checkHashMaps(); | |
204 ImageIdentifier id = cast(ImageIdentifier)ids.get(image); | |
205 if (id !is null) { | |
206 ImageReference ref_ = cast(ImageReference) images.get(id); | |
207 if (ref_ !is null) { | |
208 if (ref_.decCount()) { | |
209 images.remove(id); | |
210 ids.remove(ref_.getImage()); | |
211 ref_.getImage().dispose(); | |
212 validateHashMaps(); | |
213 } | |
214 return true; | |
215 } | |
216 } | |
217 // if the image was not found, dispose of it for the caller | |
218 image.dispose(); | |
219 return false; | |
220 } | |
221 | |
222 private void checkHashMaps() { | |
223 if (images is null) | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
224 images = new HashMap(); |
75 | 225 if (ids is null) |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
226 ids = new HashMap(); |
75 | 227 } |
228 | |
229 private void validateHashMaps() { | |
230 if (images.size() is 0) | |
231 images = null; | |
232 if (ids.size() is 0) | |
233 ids = null; | |
234 } | |
235 | |
236 private Image createGradient(Display display, Color color1, Color color2, | |
237 int realtheight, int theight, int marginHeight) { | |
238 Image image = new Image(display, 1, realtheight); | |
239 image.setBackground(color1); | |
240 GC gc = new GC(image); | |
241 gc.setBackground(color1); | |
242 gc.fillRectangle(0, 0, 1, realtheight); | |
243 gc.setForeground(color2); | |
244 gc.setBackground(color1); | |
245 gc.fillGradientRectangle(0, marginHeight + 2, 1, theight - 2, true); | |
246 gc.dispose(); | |
247 return image; | |
248 } | |
249 | |
250 private Image createGradient(Display display, Color[] colors, int[] percents, | |
251 int length, bool vertical, Color bg) { | |
252 int width = vertical ? 1 : length; | |
253 int height = vertical ? length : 1; | |
254 Image gradient = new Image(display, Math.max(width, 1), Math | |
255 .max(height, 1)); | |
256 GC gc = new GC(gradient); | |
257 drawTextGradient(gc, width, height, colors, percents, vertical, bg); | |
258 gc.dispose(); | |
259 return gradient; | |
260 } | |
261 | |
262 private void drawTextGradient(GC gc, int width, int height, Color[] colors, | |
263 int[] percents, bool vertical, Color bg) { | |
264 final Color oldBackground = gc.getBackground(); | |
265 if (colors.length is 1) { | |
266 if (colors[0] !is null) | |
267 gc.setBackground(colors[0]); | |
268 gc.fillRectangle(0, 0, width, height); | |
269 } else { | |
270 final Color oldForeground = gc.getForeground(); | |
271 Color lastColor = colors[0]; | |
272 if (lastColor is null) | |
273 lastColor = oldBackground; | |
274 int pos = 0; | |
275 for (int i = 0; i < percents.length; ++i) { | |
276 gc.setForeground(lastColor); | |
277 lastColor = colors[i + 1]; | |
278 if (lastColor is null) | |
279 lastColor = oldBackground; | |
280 gc.setBackground(lastColor); | |
281 if (vertical) { | |
282 int gradientHeight = percents[i] * height / 100; | |
283 | |
284 gc.fillGradientRectangle(0, pos, width, gradientHeight, | |
285 true); | |
286 pos += gradientHeight; | |
287 } else { | |
288 int gradientWidth = percents[i] * height / 100; | |
289 | |
290 gc.fillGradientRectangle(pos, 0, gradientWidth, height, | |
291 false); | |
292 pos += gradientWidth; | |
293 } | |
294 } | |
295 if (vertical && pos < height) { | |
296 if (bg !is null) | |
297 gc.setBackground(bg); | |
298 gc.fillRectangle(0, pos, width, height - pos); | |
299 } | |
300 if (!vertical && pos < width) { | |
301 if (bg !is null) | |
302 gc.setBackground(bg); | |
303 gc.fillRectangle(pos, 0, width - pos, height); | |
304 } | |
305 gc.setForeground(oldForeground); | |
306 } | |
307 } | |
308 } |