comparison dwtx/novocode/ScaledImage.d @ 190:df4e66472aff

novocode line endings, indention
author Frank Benoit <benoit@tionex.de>
date Sun, 26 Oct 2008 15:04:41 +0100
parents e3780acbbf80
children
comparison
equal deleted inserted replaced
189:71ca5bcf2307 190:df4e66472aff
32 * @version $Id: ScaledImage.java 346 2005-07-11 20:15:57 +0000 (Mon, 11 Jul 2005) szeiger $ 32 * @version $Id: ScaledImage.java 346 2005-07-11 20:15:57 +0000 (Mon, 11 Jul 2005) szeiger $
33 */ 33 */
34 34
35 class ScaledImage : Canvas 35 class ScaledImage : Canvas
36 { 36 {
37 private const Rectangle DEFAULT_BOUNDS; 37 private const Rectangle DEFAULT_BOUNDS;
38 38
39 public static const int IMAGE_PLACEMENT_STRETCH = 0; 39 public static const int IMAGE_PLACEMENT_STRETCH = 0;
40 public static const int IMAGE_PLACEMENT_TILE = 1; 40 public static const int IMAGE_PLACEMENT_TILE = 1;
41 41
42 private Image image; 42 private Image image;
43 43
44 private Color[] gradientColors; 44 private Color[] gradientColors;
45 45
46 private int[] gradientPercents; 46 private int[] gradientPercents;
47 47
48 private bool gradientVertical; 48 private bool gradientVertical;
49 49
50 private int imagePlacement = IMAGE_PLACEMENT_STRETCH; 50 private int imagePlacement = IMAGE_PLACEMENT_STRETCH;
51 51
52 52
53 this(Composite parent, int style) 53 this(Composite parent, int style)
54 { 54 {
55 super(parent, style | DWT.NO_BACKGROUND); 55 super(parent, style | DWT.NO_BACKGROUND);
56 this.DEFAULT_BOUNDS = new Rectangle(0, 0, 32, 32); 56 this.DEFAULT_BOUNDS = new Rectangle(0, 0, 32, 32);
57 57
58 addListener(DWT.Paint, dgListener(&onPaint)); 58 addListener(DWT.Paint, dgListener(&onPaint));
59 } 59 }
60 60
61 61
62 private void onPaint(Event event) 62 private void onPaint(Event event)
63 { 63 {
64 Rectangle rect = getClientArea(); 64 Rectangle rect = getClientArea();
65 GC gc = event.gc; 65 GC gc = event.gc;
66 if(image is null 66 if(image is null
67 || image.getImageData().getTransparencyType() !is DWT.TRANSPARENCY_NONE) 67 || image.getImageData().getTransparencyType() !is DWT.TRANSPARENCY_NONE)
68 { 68 {
69 69
70 if(gradientColors !is null) 70 if(gradientColors !is null)
71 { 71 {
72 // draw a gradient behind the text 72 // draw a gradient behind the text
73 Color oldBackground = gc.getBackground(); 73 Color oldBackground = gc.getBackground();
74 if(gradientColors.length is 1) 74 if(gradientColors.length is 1)
75 { 75 {
76 if(gradientColors[0] !is null) gc.setBackground(gradientColors[0]); 76 if(gradientColors[0] !is null) gc.setBackground(gradientColors[0]);
77 gc.fillRectangle(0, 0, rect.width, rect.height); 77 gc.fillRectangle(0, 0, rect.width, rect.height);
78 }
79 else
80 {
81 Color oldForeground = gc.getForeground();
82 Color lastColor = gradientColors[0];
83 if(lastColor is null) lastColor = oldBackground;
84 int pos = 0;
85 for(int i = 0; i < gradientPercents.length; ++i)
86 {
87 gc.setForeground(lastColor);
88 lastColor = gradientColors[i + 1];
89 if(lastColor is null) lastColor = oldBackground;
90 gc.setBackground(lastColor);
91 if(gradientVertical)
92 {
93 int gradientHeight = (gradientPercents[i] * rect.height / 100)
94 - pos;
95 gc.fillGradientRectangle(0, pos, rect.width, gradientHeight,
96 true);
97 pos += gradientHeight;
98 }
99 else
100 {
101 int gradientWidth = (gradientPercents[i] * rect.width / 100)
102 - pos;
103 gc.fillGradientRectangle(pos, 0, gradientWidth, rect.height,
104 false);
105 pos += gradientWidth;
106 }
107 }
108 if(gradientVertical && pos < rect.height)
109 {
110 gc.setBackground(getBackground());
111 gc.fillRectangle(0, pos, rect.width, rect.height - pos);
112 }
113 if(!gradientVertical && pos < rect.width)
114 {
115 gc.setBackground(getBackground());
116 gc.fillRectangle(pos, 0, rect.width - pos, rect.height);
117 }
118 gc.setForeground(oldForeground);
119 }
120 gc.setBackground(oldBackground);
78 } 121 }
79 else 122 else
80 { 123 {
81 Color oldForeground = gc.getForeground(); 124 if((getStyle() & DWT.NO_BACKGROUND) !is 0)
82 Color lastColor = gradientColors[0]; 125 {
83 if(lastColor is null) lastColor = oldBackground; 126 gc.setBackground(getBackground());
84 int pos = 0; 127 gc.fillRectangle(rect);
85 for(int i = 0; i < gradientPercents.length; ++i) 128 }
86 { 129 }
87 gc.setForeground(lastColor);
88 lastColor = gradientColors[i + 1];
89 if(lastColor is null) lastColor = oldBackground;
90 gc.setBackground(lastColor);
91 if(gradientVertical)
92 {
93 int gradientHeight = (gradientPercents[i] * rect.height / 100)
94 - pos;
95 gc.fillGradientRectangle(0, pos, rect.width, gradientHeight,
96 true);
97 pos += gradientHeight;
98 }
99 else
100 {
101 int gradientWidth = (gradientPercents[i] * rect.width / 100)
102 - pos;
103 gc.fillGradientRectangle(pos, 0, gradientWidth, rect.height,
104 false);
105 pos += gradientWidth;
106 }
107 }
108 if(gradientVertical && pos < rect.height)
109 {
110 gc.setBackground(getBackground());
111 gc.fillRectangle(0, pos, rect.width, rect.height - pos);
112 }
113 if(!gradientVertical && pos < rect.width)
114 {
115 gc.setBackground(getBackground());
116 gc.fillRectangle(pos, 0, rect.width - pos, rect.height);
117 }
118 gc.setForeground(oldForeground);
119 }
120 gc.setBackground(oldBackground);
121 }
122 else
123 {
124 if((getStyle() & DWT.NO_BACKGROUND) !is 0)
125 {
126 gc.setBackground(getBackground());
127 gc.fillRectangle(rect);
128 }
129 }
130 130
131 } 131 }
132 if(image !is null) 132 if(image !is null)
133 { 133 {
134 Rectangle ib = image.getBounds(); 134 Rectangle ib = image.getBounds();
135 if(imagePlacement is IMAGE_PLACEMENT_TILE) 135 if(imagePlacement is IMAGE_PLACEMENT_TILE)
136 { 136 {
137 int maxStartX = rect.x + rect.width; 137 int maxStartX = rect.x + rect.width;
138 int maxStartY = rect.y + rect.height; 138 int maxStartY = rect.y + rect.height;
139 for(int x = rect.x; x < maxStartX; x += ib.width) 139 for(int x = rect.x; x < maxStartX; x += ib.width)
140 for(int y = rect.y; y < maxStartY; y += ib.height) 140 for(int y = rect.y; y < maxStartY; y += ib.height)
141 event.gc.drawImage(image, x, y); 141 event.gc.drawImage(image, x, y);
142 } 142 }
143 else // IMAGE_PLACEMENT_STRETCH 143 else // IMAGE_PLACEMENT_STRETCH
144 { 144 {
145 event.gc.drawImage(image, ib.x, ib.y, ib.width, ib.height, rect.x, 145 event.gc.drawImage(image, ib.x, ib.y, ib.width, ib.height, rect.x,
146 rect.y, rect.width, rect.height); 146 rect.y, rect.width, rect.height);
147 } 147 }
148 } 148 }
149 } 149 }
150 150
151 151
152 public void setImage(Image image) 152 public void setImage(Image image)
153 { 153 {
154 this.image = image; 154 this.image = image;
155 redraw(); 155 redraw();
156 } 156 }
157 157
158 158
159 public void setImagePlacement(int imagePlacement) 159 public void setImagePlacement(int imagePlacement)
160 { 160 {
161 this.imagePlacement = imagePlacement; 161 this.imagePlacement = imagePlacement;
162 redraw(); 162 redraw();
163 } 163 }
164 164
165 165
166 public Point computeSize(int wHint, int hHint, bool changed) 166 public Point computeSize(int wHint, int hHint, bool changed)
167 { 167 {
168 checkWidget(); 168 checkWidget();
169 Rectangle ib = image !is null ? image.getBounds() : DEFAULT_BOUNDS; 169 Rectangle ib = image !is null ? image.getBounds() : DEFAULT_BOUNDS;
170 if(wHint == DWT.DEFAULT) wHint = ib.width; 170 if(wHint == DWT.DEFAULT) wHint = ib.width;
171 if(hHint == DWT.DEFAULT) hHint = ib.height; 171 if(hHint == DWT.DEFAULT) hHint = ib.height;
172 return new Point(wHint, hHint); 172 return new Point(wHint, hHint);
173 } 173 }
174 174
175 175
176 public void setBackground(Color color) 176 public void setBackground(Color color)
177 { 177 {
178 super.setBackground(color); 178 super.setBackground(color);
179 // Are these settings the same as before? 179 // Are these settings the same as before?
180 if(color !is null && gradientColors is null && gradientPercents is null) 180 if(color !is null && gradientColors is null && gradientPercents is null)
181 { 181 {
182 Color background = getBackground(); 182 Color background = getBackground();
183 if(color is background) 183 if(color is background)
184 { 184 {
185 return; 185 return;
186 } 186 }
187 } 187 }
188 gradientColors = null; 188 gradientColors = null;
189 gradientPercents = null; 189 gradientPercents = null;
190 redraw(); 190 redraw();
191 } 191 }
192 192
193 193
194 public void setBackground(Color[] colors, int[] percents) 194 public void setBackground(Color[] colors, int[] percents)
195 { 195 {
196 setBackground(colors, percents, false); 196 setBackground(colors, percents, false);
197 } 197 }
198 198
199 199
200 public void setBackground(Color[] colors, int[] percents, bool vertical) 200 public void setBackground(Color[] colors, int[] percents, bool vertical)
201 { 201 {
202 checkWidget(); 202 checkWidget();
203 if(colors !is null) 203 if(colors !is null)
204 { 204 {
205 if(percents is null || percents.length !is colors.length - 1) 205 if(percents is null || percents.length !is colors.length - 1)
206 { 206 {
207 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 207 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
208 } 208 }
209 if(getDisplay().getDepth() < 15) 209 if(getDisplay().getDepth() < 15)
210 { 210 {
211 // Don't use gradients on low color displays 211 // Don't use gradients on low color displays
212 colors = [ colors[colors.length - 1] ]; 212 colors = [ colors[colors.length - 1] ];
213 percents = []; 213 percents = [];
214 } 214 }
215 for(int i = 0; i < percents.length; i++) 215 for(int i = 0; i < percents.length; i++)
216 { 216 {
217 if(percents[i] < 0 || percents[i] > 100) 217 if(percents[i] < 0 || percents[i] > 100)
218 { 218 {
219 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 219 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
220 } 220 }
221 if(i > 0 && percents[i] < percents[i - 1]) 221 if(i > 0 && percents[i] < percents[i - 1])
222 { 222 {
223 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 223 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
224 } 224 }
225 } 225 }
226 } 226 }
227 227
228 // Are these settings the same as before? 228 // Are these settings the same as before?
229 Color background = getBackground(); 229 Color background = getBackground();
230 if((gradientColors !is null) && (colors !is null) 230 if((gradientColors !is null) && (colors !is null)
231 && (gradientColors.length is colors.length)) 231 && (gradientColors.length is colors.length))
232 { 232 {
233 bool same = false; 233 bool same = false;
234 for(int i = 0; i < gradientColors.length; i++) 234 for(int i = 0; i < gradientColors.length; i++)
235 { 235 {
236 same = (gradientColors[i] is colors[i]) 236 same = (gradientColors[i] is colors[i])
237 || ((gradientColors[i] is null) && (colors[i] is background)) 237 || ((gradientColors[i] is null) && (colors[i] is background))
238 || ((gradientColors[i] is background) && (colors[i] is null)); 238 || ((gradientColors[i] is background) && (colors[i] is null));
239 if(!same) break; 239 if(!same) break;
240 } 240 }
241 if(same) 241 if(same)
242 { 242 {
243 for(int i = 0; i < gradientPercents.length; i++) 243 for(int i = 0; i < gradientPercents.length; i++)
244 { 244 {
245 same = gradientPercents[i] is percents[i]; 245 same = gradientPercents[i] is percents[i];
246 if(!same) break; 246 if(!same) break;
247 } 247 }
248 } 248 }
249 if(same && this.gradientVertical is vertical) return; 249 if(same && this.gradientVertical is vertical) return;
250 } 250 }
251 // Store the new settings 251 // Store the new settings
252 if(colors is null) 252 if(colors is null)
253 { 253 {
254 gradientColors = null; 254 gradientColors = null;
255 gradientPercents = null; 255 gradientPercents = null;
256 gradientVertical = false; 256 gradientVertical = false;
257 } 257 }
258 else 258 else
259 { 259 {
260 gradientColors = new Color[colors.length]; 260 gradientColors = new Color[colors.length];
261 for(int i = 0; i < colors.length; ++i) 261 for(int i = 0; i < colors.length; ++i)
262 gradientColors[i] = (colors[i] !is null) ? colors[i] : background; 262 gradientColors[i] = (colors[i] !is null) ? colors[i] : background;
263 gradientPercents = new int[percents.length]; 263 gradientPercents = new int[percents.length];
264 for(int i = 0; i < percents.length; ++i) 264 for(int i = 0; i < percents.length; ++i)
265 gradientPercents[i] = percents[i]; 265 gradientPercents[i] = percents[i];
266 gradientVertical = vertical; 266 gradientVertical = vertical;
267 } 267 }
268 // Refresh with the new settings 268 // Refresh with the new settings
269 redraw(); 269 redraw();
270 } 270 }
271 } 271 }