2015-01-14 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libjava / gnu / awt / xlib / XGraphics.java
blob215c04dc197b5bf11f873a4bdc45deac30c98b14
1 /* Copyright (C) 2000, 2003, 2004 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.awt.xlib;
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.Pixmap;
24 import gnu.gcj.xlib.Visual;
25 import gnu.awt.j2d.DirectRasterGraphics;
26 import gnu.awt.j2d.MappedRaster;
28 public class XGraphics implements Cloneable, DirectRasterGraphics
30 static class XRaster extends MappedRaster
32 XImage ximage;
34 public XRaster(WritableRaster raster, XImage ximage, ColorModel cm)
36 super(raster, cm);
37 this.ximage = ximage;
41 GC context;
42 XGraphicsConfiguration config;
43 Rectangle clipBounds;
45 XFontMetrics metrics;
48 public Object clone()
50 try
52 XGraphics gfxCopy = (XGraphics) super.clone();
53 gfxCopy.context = context.create();
55 return gfxCopy;
57 catch (CloneNotSupportedException ex)
59 // This should never happen.
60 throw new InternalError ();
64 public void dispose()
66 GC lContext = context;
67 context = null;
68 config = null;
69 clipBounds = null;
70 metrics = null;
72 if (lContext != null)
74 lContext.dispose();
78 public XGraphics(Drawable drawable, XGraphicsConfiguration config)
80 context = GC.create(drawable);
81 this.config = config;
84 public void setColor(Color color)
86 if (color != null)
87 context.setForeground(config.getPixel(color));
90 public void setPaintMode()
92 throw new UnsupportedOperationException("not implemented");
95 public void setXORMode(Color c1)
97 throw new UnsupportedOperationException("not implemented");
100 public void setFont(Font font)
102 if (font == null)
103 return;
104 if ((metrics != null) && font.equals(metrics.getFont()))
105 return;
106 metrics = config.getXFontMetrics(font);
107 if (metrics != null)
108 context.setFont(metrics.xfont);
111 public FontMetrics getFontMetrics(Font font)
113 if ((metrics != null) && font.equals(metrics.getFont()))
114 return metrics;
116 return config.getXFontMetrics(font);
119 public void setClip(int x, int y, int width, int height)
121 Rectangle[] rects = { new Rectangle(x, y, width, height) };
122 context.setClipRectangles(rects);
125 public void setClip(Shape clip)
127 /* TODO: create a special RectangleUnion shape that can be
128 used to draw advantage of the GCs ability to set multiple
129 rectangles.
132 /* FIXME: creating all these objects is wasteful and can be
133 costly in the long run, since this code is run at every
134 expose. */
135 Rectangle newClipBounds = clip.getBounds();
137 /* FIXME: decide whether this test code is worth anything
138 * (as of 2004-01-29, it prints frequently)
139 if ((clipBounds != null) && !clipBounds.contains(newClipBounds))
141 System.err.println("warning: old clip ("+ clipBounds +") does " +
142 "not fully contain new clip (" +
143 newClipBounds + ")");
146 clipBounds = newClipBounds;
147 Rectangle[] rects = { clipBounds };
148 context.setClipRectangles(rects);
151 public void copyArea(int x, int y, int width, int height, int
152 dx, int dy)
154 throw new UnsupportedOperationException("not implemented");
157 public void drawLine(int x1, int y1, int x2, int y2)
159 context.drawLine(x1, y1, x2, y2);
162 public void drawRect(int x, int y, int width, int height)
164 throw new UnsupportedOperationException("not implemented yet");
167 public void fillRect(int x, int y, int width, int height)
169 context.fillRectangle(x, y, width, height);
172 public void drawArc(int x, int y, int width, int height, int
173 startAngle, int arcAngle)
175 context.drawArc (x, y, width, height, startAngle, arcAngle);
178 public void fillArc(int x, int y, int width, int height, int
179 startAngle, int arcAngle)
181 context.fillArc (x, y, width, height, startAngle, arcAngle);
184 public void drawPolyline(int[] xPoints, int[] yPoints, int
185 nPoints)
187 throw new UnsupportedOperationException("not implemented");
190 public void drawPolygon(int[] xPoints, int[] yPoints, int
191 nPoints)
193 throw new UnsupportedOperationException("not implemented");
196 public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints,
197 int translateX, int translateY)
199 context.fillPolygon(xPoints, yPoints, nPoints, translateX, translateY);
202 public void drawString(String str, int x, int y)
204 context.drawString(str, x, y);
207 public boolean drawImage(Image img, int x, int y,
208 ImageObserver observer)
210 if (img instanceof XOffScreenImage)
212 // FIXME: have to enforce clip, or is it OK as-is?
213 XOffScreenImage offScreenImage = (XOffScreenImage) img;
214 Pixmap pixmap = offScreenImage.getPixmap ();
215 context.copyArea (pixmap, 0, 0, x, y,
216 offScreenImage.getWidth (), offScreenImage.getHeight ());
217 return true;
219 if (clipBounds == null)
220 return false; // ***FIXME***
222 if (!(img instanceof BufferedImage))
224 throw new AWTError("unknown image class");
227 BufferedImage bimg = (BufferedImage) img;
229 XImage ximg = (XImage) bimg.getProperty("gnu.gcj.xlib.XImage");
230 if (ximg == null)
232 System.err.println("FIXME: skipping null XImage, should " +
233 "really do on the spot conversion");
234 return false;
238 +------------------
239 | clip
240 | +---------+
241 | img | |
242 | +--+-------+ |
243 | | | | |
244 | | | | |
245 | | +-------+-+
246 | | |
247 | +----------+
250 int iLeft = Math.max(x, clipBounds.x);
251 int iTop = Math.max(y, clipBounds.y);
252 int iRight = Math.min(x + bimg.getWidth(),
253 clipBounds.x + clipBounds.width);
254 int iBottom = Math.min(y + bimg.getHeight(),
255 clipBounds.y + clipBounds.height);
257 int srcX = iLeft - x;
258 int srcY = iTop - y;
260 int width = iRight - iLeft;
261 int height = iBottom - iTop;
263 if ((width > 0) && (height > 0))
264 context.putImage(ximg, srcX, srcY, iLeft, iTop, width, height);
266 return true;
269 public MappedRaster mapRaster(Rectangle bounds)
271 Visual visual = config.getVisual();
272 XImage ximage = new XImage(visual, bounds.width, bounds.height,
273 false // do not auto allocate memory
276 WritableRaster raster =
277 config.createRasterForXImage(ximage,
278 new Point(bounds.x, bounds.y));
280 DataBuffer dataB = raster.getDataBuffer();
281 XGraphicsConfiguration.attachData(ximage, dataB, 0);
283 Drawable drawable = context.getDrawable();
285 // TODO: restrict to clipping
287 Rectangle mBounds = drawable.copyIntoXImage(ximage, bounds, 0, 0);
289 return new XRaster(raster, ximage, config.imageCM);
293 public void unmapRaster(MappedRaster mappedRaster)
295 XRaster xraster = (XRaster) mappedRaster;
296 XImage ximage = xraster.ximage;
297 Raster raster = xraster.getRaster();
298 int x = raster.getMinX();
299 int y = raster.getMinY();
300 int width = raster.getWidth();
301 int height = raster.getHeight();
303 context.putImage(ximage, 0, 0, x, y, width, height);