comparison dwtx/novocode/ScaledImage.d @ 188:e3780acbbf80

Added ported sources from Novocode, thanks to WasserDragoon
author Frank Benoit <benoit@tionex.de>
date Sun, 26 Oct 2008 14:54:39 +0100
parents
children df4e66472aff
comparison
equal deleted inserted replaced
187:293a2f22f944 188:e3780acbbf80
1 /*******************************************************************************
2 * Copyright (c) 2004 Stefan Zeiger 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.novocode.com/legal/epl-v10.html
7 *
8 * Contributors:
9 * Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
10 * IBM Corporation - original SWT CLabel implementation on which this class is based
11 *******************************************************************************/
12
13 module dwtx.novocode.ScaledImage;
14
15 import dwt.DWT;
16 import dwt.graphics.Color;
17 import dwt.graphics.GC;
18 import dwt.graphics.Image;
19 import dwt.graphics.Point;
20 import dwt.graphics.Rectangle;
21 import dwt.widgets.Canvas;
22 import dwt.widgets.Composite;
23 import dwt.widgets.Event;
24 import dwt.widgets.Listener;
25
26
27 /**
28 * An image / gradient component. Under development.
29 *
30 * @author Stefan Zeiger (szeiger@novocode.com)
31 * @since Mar 21, 2005
32 * @version $Id: ScaledImage.java 346 2005-07-11 20:15:57 +0000 (Mon, 11 Jul 2005) szeiger $
33 */
34
35 class ScaledImage : Canvas
36 {
37 private const Rectangle DEFAULT_BOUNDS;
38
39 public static const int IMAGE_PLACEMENT_STRETCH = 0;
40 public static const int IMAGE_PLACEMENT_TILE = 1;
41
42 private Image image;
43
44 private Color[] gradientColors;
45
46 private int[] gradientPercents;
47
48 private bool gradientVertical;
49
50 private int imagePlacement = IMAGE_PLACEMENT_STRETCH;
51
52
53 this(Composite parent, int style)
54 {
55 super(parent, style | DWT.NO_BACKGROUND);
56 this.DEFAULT_BOUNDS = new Rectangle(0, 0, 32, 32);
57
58 addListener(DWT.Paint, dgListener(&onPaint));
59 }
60
61
62 private void onPaint(Event event)
63 {
64 Rectangle rect = getClientArea();
65 GC gc = event.gc;
66 if(image is null
67 || image.getImageData().getTransparencyType() !is DWT.TRANSPARENCY_NONE)
68 {
69
70 if(gradientColors !is null)
71 {
72 // draw a gradient behind the text
73 Color oldBackground = gc.getBackground();
74 if(gradientColors.length is 1)
75 {
76 if(gradientColors[0] !is null) gc.setBackground(gradientColors[0]);
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);
121 }
122 else
123 {
124 if((getStyle() & DWT.NO_BACKGROUND) !is 0)
125 {
126 gc.setBackground(getBackground());
127 gc.fillRectangle(rect);
128 }
129 }
130
131 }
132 if(image !is null)
133 {
134 Rectangle ib = image.getBounds();
135 if(imagePlacement is IMAGE_PLACEMENT_TILE)
136 {
137 int maxStartX = rect.x + rect.width;
138 int maxStartY = rect.y + rect.height;
139 for(int x = rect.x; x < maxStartX; x += ib.width)
140 for(int y = rect.y; y < maxStartY; y += ib.height)
141 event.gc.drawImage(image, x, y);
142 }
143 else // IMAGE_PLACEMENT_STRETCH
144 {
145 event.gc.drawImage(image, ib.x, ib.y, ib.width, ib.height, rect.x,
146 rect.y, rect.width, rect.height);
147 }
148 }
149 }
150
151
152 public void setImage(Image image)
153 {
154 this.image = image;
155 redraw();
156 }
157
158
159 public void setImagePlacement(int imagePlacement)
160 {
161 this.imagePlacement = imagePlacement;
162 redraw();
163 }
164
165
166 public Point computeSize(int wHint, int hHint, bool changed)
167 {
168 checkWidget();
169 Rectangle ib = image !is null ? image.getBounds() : DEFAULT_BOUNDS;
170 if(wHint == DWT.DEFAULT) wHint = ib.width;
171 if(hHint == DWT.DEFAULT) hHint = ib.height;
172 return new Point(wHint, hHint);
173 }
174
175
176 public void setBackground(Color color)
177 {
178 super.setBackground(color);
179 // Are these settings the same as before?
180 if(color !is null && gradientColors is null && gradientPercents is null)
181 {
182 Color background = getBackground();
183 if(color is background)
184 {
185 return;
186 }
187 }
188 gradientColors = null;
189 gradientPercents = null;
190 redraw();
191 }
192
193
194 public void setBackground(Color[] colors, int[] percents)
195 {
196 setBackground(colors, percents, false);
197 }
198
199
200 public void setBackground(Color[] colors, int[] percents, bool vertical)
201 {
202 checkWidget();
203 if(colors !is null)
204 {
205 if(percents is null || percents.length !is colors.length - 1)
206 {
207 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
208 }
209 if(getDisplay().getDepth() < 15)
210 {
211 // Don't use gradients on low color displays
212 colors = [ colors[colors.length - 1] ];
213 percents = [];
214 }
215 for(int i = 0; i < percents.length; i++)
216 {
217 if(percents[i] < 0 || percents[i] > 100)
218 {
219 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
220 }
221 if(i > 0 && percents[i] < percents[i - 1])
222 {
223 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
224 }
225 }
226 }
227
228 // Are these settings the same as before?
229 Color background = getBackground();
230 if((gradientColors !is null) && (colors !is null)
231 && (gradientColors.length is colors.length))
232 {
233 bool same = false;
234 for(int i = 0; i < gradientColors.length; i++)
235 {
236 same = (gradientColors[i] is colors[i])
237 || ((gradientColors[i] is null) && (colors[i] is background))
238 || ((gradientColors[i] is background) && (colors[i] is null));
239 if(!same) break;
240 }
241 if(same)
242 {
243 for(int i = 0; i < gradientPercents.length; i++)
244 {
245 same = gradientPercents[i] is percents[i];
246 if(!same) break;
247 }
248 }
249 if(same && this.gradientVertical is vertical) return;
250 }
251 // Store the new settings
252 if(colors is null)
253 {
254 gradientColors = null;
255 gradientPercents = null;
256 gradientVertical = false;
257 }
258 else
259 {
260 gradientColors = new Color[colors.length];
261 for(int i = 0; i < colors.length; ++i)
262 gradientColors[i] = (colors[i] !is null) ? colors[i] : background;
263 gradientPercents = new int[percents.length];
264 for(int i = 0; i < percents.length; ++i)
265 gradientPercents[i] = percents[i];
266 gradientVertical = vertical;
267 }
268 // Refresh with the new settings
269 redraw();
270 }
271 }