]>
Commit | Line | Data |
---|---|---|
f826d595 | 1 | /* Copyright (C) 2000, 2003 Free Software Foundation |
2622c79d RR |
2 | |
3 | This file is part of libgcj. | |
4 | ||
5 | This software is copyrighted work licensed under the terms of the | |
6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for | |
7 | details. */ | |
8 | ||
9 | package gnu.awt.xlib; | |
10 | ||
11 | import java.awt.*; | |
12 | import java.awt.image.WritableRaster; | |
13 | import java.awt.image.Raster; | |
14 | import java.awt.image.DataBuffer; | |
15 | import java.awt.image.ColorModel; | |
16 | import java.awt.image.ImageObserver; | |
17 | import java.awt.image.BufferedImage; | |
18 | import gnu.gcj.xlib.GC; | |
19 | import gnu.gcj.xlib.XImage; | |
20 | import gnu.gcj.xlib.Drawable; | |
21 | import gnu.gcj.xlib.Window; | |
22 | import gnu.gcj.xlib.Drawable; | |
23 | import gnu.gcj.xlib.Visual; | |
24 | import gnu.awt.j2d.DirectRasterGraphics; | |
25 | import gnu.awt.j2d.MappedRaster; | |
26 | ||
27 | public class XGraphics implements Cloneable, DirectRasterGraphics | |
28 | { | |
29 | static class XRaster extends MappedRaster | |
30 | { | |
31 | XImage ximage; | |
32 | ||
33 | public XRaster(WritableRaster raster, XImage ximage, ColorModel cm) | |
34 | { | |
35 | super(raster, cm); | |
36 | this.ximage = ximage; | |
37 | } | |
38 | } | |
39 | ||
40 | GC context; | |
41 | XGraphicsConfiguration config; | |
42 | Rectangle clipBounds; | |
43 | ||
44 | XFontMetrics metrics; | |
45 | ||
46 | ||
47 | public Object clone() | |
48 | { | |
49e58846 AG |
49 | try |
50 | { | |
51 | XGraphics gfxCopy = (XGraphics) super.clone(); | |
52 | gfxCopy.context = context.create(); | |
53 | ||
54 | return gfxCopy; | |
55 | } | |
56 | catch (CloneNotSupportedException ex) | |
57 | { | |
58 | // This should never happen. | |
59 | throw new InternalError (); | |
60 | } | |
2622c79d RR |
61 | } |
62 | ||
63 | public void dispose() | |
64 | { | |
65 | GC lContext = context; | |
66 | context = null; | |
67 | config = null; | |
68 | clipBounds = null; | |
9e9e2042 | 69 | metrics = null; |
2622c79d RR |
70 | |
71 | if (lContext != null) | |
9e9e2042 SG |
72 | { |
73 | lContext.dispose(); | |
74 | } | |
2622c79d RR |
75 | } |
76 | ||
77 | public XGraphics(Drawable drawable, XGraphicsConfiguration config) | |
78 | { | |
9e9e2042 | 79 | context = GC.create(drawable); |
2622c79d RR |
80 | this.config = config; |
81 | } | |
82 | ||
83 | public void setColor(Color color) | |
84 | { | |
85 | context.setForeground(config.getPixel(color)); | |
86 | } | |
87 | ||
88 | public void setPaintMode() | |
89 | { | |
90 | throw new UnsupportedOperationException("not implemented"); | |
91 | } | |
92 | ||
93 | public void setXORMode(Color c1) | |
94 | { | |
95 | throw new UnsupportedOperationException("not implemented"); | |
96 | } | |
97 | ||
98 | public void setFont(Font font) | |
99 | { | |
100 | if ((metrics != null) && font.equals(metrics.getFont())) return; | |
101 | ||
102 | metrics = config.getXFontMetrics(font); | |
103 | context.setFont(metrics.xfont); | |
104 | } | |
105 | ||
106 | public FontMetrics getFontMetrics(Font font) | |
107 | { | |
108 | if ((metrics != null) && font.equals(metrics.getFont())) | |
109 | return metrics; | |
110 | ||
111 | return config.getXFontMetrics(font); | |
112 | } | |
113 | ||
114 | public void setClip(int x, int y, int width, int height) | |
115 | { | |
116 | Rectangle[] rects = { new Rectangle(x, y, width, height) }; | |
117 | context.setClipRectangles(rects); | |
118 | } | |
119 | ||
120 | public void setClip(Shape clip) | |
121 | { | |
122 | /* TODO: create a special RectangleUnion shape that can be | |
123 | used to draw advantage of the GCs ability to set multiple | |
124 | rectangles. | |
125 | */ | |
126 | ||
127 | /* FIXME: creating all these objects is wasteful and can be | |
128 | costly in the long run, since this code is run at every | |
129 | expose. */ | |
130 | Rectangle newClipBounds = clip.getBounds(); | |
131 | ||
132 | if ((clipBounds != null) && !clipBounds.contains(newClipBounds)) | |
133 | { | |
134 | System.err.println("warning: old clip ("+ clipBounds +") does " + | |
135 | "not fully contain new clip (" + | |
136 | newClipBounds + ")"); | |
137 | } | |
138 | clipBounds = newClipBounds; | |
139 | Rectangle[] rects = { clipBounds }; | |
140 | context.setClipRectangles(rects); | |
141 | } | |
142 | ||
143 | public void copyArea(int x, int y, int width, int height, int | |
144 | dx, int dy) | |
145 | { | |
146 | throw new UnsupportedOperationException("not implemented"); | |
147 | } | |
148 | ||
149 | public void drawLine(int x1, int y1, int x2, int y2) | |
150 | { | |
151 | context.drawLine(x1, y1, x2, y2); | |
152 | } | |
153 | ||
154 | public void drawRect(int x, int y, int width, int height) | |
155 | { | |
156 | throw new UnsupportedOperationException("not implemented yet"); | |
157 | } | |
158 | ||
159 | public void fillRect(int x, int y, int width, int height) | |
160 | { | |
161 | context.fillRectangle(x, y, width, height); | |
162 | } | |
163 | ||
164 | public void drawArc(int x, int y, int width, int height, int | |
165 | startAngle, int arcAngle) | |
166 | { | |
a28853b0 | 167 | context.drawArc (x, y, width, height, startAngle, arcAngle); |
2622c79d RR |
168 | } |
169 | ||
170 | public void fillArc(int x, int y, int width, int height, int | |
171 | startAngle, int arcAngle) | |
172 | { | |
a28853b0 | 173 | context.fillArc (x, y, width, height, startAngle, arcAngle); |
2622c79d RR |
174 | } |
175 | ||
176 | public void drawPolyline(int[] xPoints, int[] yPoints, int | |
177 | nPoints) | |
178 | { | |
179 | throw new UnsupportedOperationException("not implemented"); | |
180 | } | |
181 | ||
182 | public void drawPolygon(int[] xPoints, int[] yPoints, int | |
183 | nPoints) | |
184 | { | |
185 | throw new UnsupportedOperationException("not implemented"); | |
186 | } | |
187 | ||
f826d595 SG |
188 | public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints, |
189 | int translateX, int translateY) | |
2622c79d | 190 | { |
f826d595 | 191 | context.fillPolygon(xPoints, yPoints, nPoints, translateX, translateY); |
2622c79d RR |
192 | } |
193 | ||
194 | public void drawString(String str, int x, int y) | |
195 | { | |
196 | context.drawString(str, x, y); | |
197 | } | |
198 | ||
199 | public boolean drawImage(Image img, int x, int y, | |
200 | ImageObserver observer) | |
201 | { | |
202 | if (clipBounds == null) | |
203 | return false; // ***FIXME*** | |
204 | ||
205 | if (!(img instanceof BufferedImage)) | |
206 | { | |
207 | throw new AWTError("unknown image class"); | |
208 | } | |
209 | ||
210 | BufferedImage bimg = (BufferedImage) img; | |
211 | ||
212 | XImage ximg = (XImage) bimg.getProperty("gnu.gcj.xlib.XImage"); | |
213 | if (ximg == null) | |
214 | { | |
215 | System.err.println("FIXME: skipping null XImage, should " + | |
216 | "really do on the spot conversion"); | |
217 | return false; | |
218 | } | |
219 | ||
220 | /* | |
221 | +------------------ | |
222 | | clip | |
223 | | +---------+ | |
224 | | img | | | |
225 | | +--+-------+ | | |
226 | | | | | | | |
227 | | | | | | | |
228 | | | +-------+-+ | |
229 | | | | | |
230 | | +----------+ | |
231 | */ | |
232 | ||
233 | int iLeft = Math.max(x, clipBounds.x); | |
234 | int iTop = Math.max(y, clipBounds.y); | |
235 | int iRight = Math.min(x + bimg.getWidth(), | |
236 | clipBounds.x + clipBounds.width); | |
237 | int iBottom = Math.min(y + bimg.getHeight(), | |
238 | clipBounds.y + clipBounds.height); | |
239 | ||
240 | int srcX = iLeft - x; | |
241 | int srcY = iTop - y; | |
242 | ||
243 | int width = iRight - iLeft; | |
244 | int height = iBottom - iTop; | |
245 | ||
246 | if ((width > 0) && (height > 0)) | |
247 | context.putImage(ximg, srcX, srcY, iLeft, iTop, width, height); | |
248 | ||
249 | return true; | |
250 | } | |
251 | ||
252 | public MappedRaster mapRaster(Rectangle bounds) | |
253 | { | |
254 | Visual visual = config.getVisual(); | |
255 | XImage ximage = new XImage(visual, bounds.width, bounds.height, | |
256 | false // do not auto allocate memory | |
257 | ); | |
258 | ||
259 | WritableRaster raster = | |
260 | config.createRasterForXImage(ximage, | |
261 | new Point(bounds.x, bounds.y)); | |
262 | ||
263 | DataBuffer dataB = raster.getDataBuffer(); | |
264 | XGraphicsConfiguration.attachData(ximage, dataB, 0); | |
265 | ||
266 | Drawable drawable = context.getDrawable(); | |
267 | ||
268 | // TODO: restrict to clipping | |
269 | ||
270 | Rectangle mBounds = drawable.copyIntoXImage(ximage, bounds, 0, 0); | |
271 | ||
272 | return new XRaster(raster, ximage, config.imageCM); | |
273 | } | |
274 | ||
275 | ||
276 | public void unmapRaster(MappedRaster mappedRaster) | |
277 | { | |
278 | XRaster xraster = (XRaster) mappedRaster; | |
279 | XImage ximage = xraster.ximage; | |
280 | Raster raster = xraster.getRaster(); | |
281 | int x = raster.getMinX(); | |
282 | int y = raster.getMinY(); | |
283 | int width = raster.getWidth(); | |
284 | int height = raster.getHeight(); | |
285 | ||
286 | context.putImage(ximage, 0, 0, x, y, width, height); | |
287 | } | |
288 | } |