1 /* GdkGraphics.java 2 Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 19 02111-1307 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package gnu.java.awt.peer.gtk; 40 41 import java.awt.Color; 42 import java.awt.Dimension; 43 import java.awt.Font; 44 import java.awt.FontMetrics; 45 import java.awt.Graphics; 46 import java.awt.Image; 47 import java.awt.Rectangle; 48 import java.awt.Shape; 49 import java.awt.image.ImageObserver; 50 import java.text.AttributedCharacterIterator; 51 52 public class GdkGraphics extends Graphics 53 { 54 private final int native_state = GtkGenericPeer.getUniqueInteger(); 55 56 Color color, xorColor; 57 GtkComponentPeer component; 58 Font font; 59 Rectangle clip; 60 61 int xOffset = 0; 62 int yOffset = 0; 63 64 static final int GDK_COPY = 0, GDK_XOR = 2; 65 initState(GtkComponentPeer component)66 native int[] initState (GtkComponentPeer component); initState(int width, int height)67 native void initState (int width, int height); copyState(GdkGraphics g)68 native void copyState (GdkGraphics g); 69 GdkGraphics(GdkGraphics g)70 GdkGraphics (GdkGraphics g) 71 { 72 color = g.color; 73 xorColor = g.xorColor; 74 font = g.font; 75 clip = new Rectangle (g.clip); 76 component = g.component; 77 78 copyState (g); 79 } 80 GdkGraphics(int width, int height)81 GdkGraphics (int width, int height) 82 { 83 initState (width, height); 84 color = Color.black; 85 clip = new Rectangle (0, 0, width, height); 86 font = new Font ("Dialog", Font.PLAIN, 10); 87 } 88 GdkGraphics(GtkComponentPeer component)89 GdkGraphics (GtkComponentPeer component) 90 { 91 this.component = component; 92 int rgb[] = initState (component); 93 color = new Color (rgb[0], rgb[1], rgb[2]); 94 font = new Font ("Dialog", Font.PLAIN, 10); 95 Dimension d = component.awtComponent.getSize (); 96 clip = new Rectangle (0, 0, d.width, d.height); 97 } 98 clearRect(int x, int y, int width, int height)99 public native void clearRect (int x, int y, int width, int height); 100 clipRect(int x, int y, int width, int height)101 public void clipRect (int x, int y, int width, int height) 102 { 103 clip = clip.intersection (new Rectangle (x, y, width, height)); 104 setClipRectangle (clip.x, clip.y, clip.width, clip.height); 105 } 106 copyArea(int x, int y, int width, int height, int dx, int dy)107 native public void copyArea (int x, int y, int width, int height, 108 int dx, int dy); 109 create()110 public Graphics create () 111 { 112 return new GdkGraphics (this); 113 } 114 115 // public Graphics create (int x, int y, int width, int height) 116 // { 117 // GdkGraphics g = new GdkGraphics (this); 118 // System.out.println ("translating by: " + x +" " + y); 119 // g.translate (x, y); 120 // g.clipRect (0, 0, width, height); 121 122 // return g; 123 // } 124 dispose()125 native public void dispose (); 126 copyPixmap(Graphics g, int x, int y, int width, int height)127 native void copyPixmap (Graphics g, int x, int y, int width, int height); drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)128 public boolean drawImage (Image img, int x, int y, 129 Color bgcolor, ImageObserver observer) 130 { 131 if (img instanceof GtkOffScreenImage) 132 { 133 copyPixmap (img.getGraphics (), 134 x, y, img.getWidth (null), img.getHeight (null)); 135 return true; 136 } 137 138 GtkImage image = (GtkImage) img; 139 new GtkImagePainter (image, this, x, y, -1, -1, bgcolor); 140 return image.isLoaded (); 141 } 142 drawImage(Image img, int x, int y, ImageObserver observer)143 public boolean drawImage (Image img, int x, int y, ImageObserver observer) 144 { 145 if (img instanceof GtkOffScreenImage) 146 { 147 copyPixmap (img.getGraphics (), 148 x, y, img.getWidth (null), img.getHeight (null)); 149 return true; 150 } 151 152 return drawImage (img, x, y, component.getBackground (), observer); 153 } 154 drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)155 public boolean drawImage (Image img, int x, int y, int width, int height, 156 Color bgcolor, ImageObserver observer) 157 { 158 if (img instanceof GtkOffScreenImage) 159 { 160 throw new RuntimeException (); 161 } 162 163 GtkImage image = (GtkImage) img; 164 new GtkImagePainter (image, this, x, y, width, height, bgcolor); 165 return image.isLoaded (); 166 } 167 drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)168 public boolean drawImage (Image img, int x, int y, int width, int height, 169 ImageObserver observer) 170 { 171 return drawImage (img, x, y, width, height, component.getBackground (), 172 observer); 173 } 174 drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)175 public boolean drawImage (Image img, int dx1, int dy1, int dx2, int dy2, 176 int sx1, int sy1, int sx2, int sy2, 177 Color bgcolor, ImageObserver observer) 178 { 179 if (img instanceof GtkOffScreenImage) 180 { 181 throw new RuntimeException (); 182 } 183 184 GtkImage image = (GtkImage) img; 185 new GtkImagePainter (image, this, dx1, dy1, dx2, dy2, 186 sx1, sy1, sx2, sy2, bgcolor); 187 return image.isLoaded (); 188 } 189 drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)190 public boolean drawImage (Image img, int dx1, int dy1, int dx2, int dy2, 191 int sx1, int sy1, int sx2, int sy2, 192 ImageObserver observer) 193 { 194 return drawImage (img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, 195 component.getBackground (), observer); 196 } 197 drawLine(int x1, int y1, int x2, int y2)198 native public void drawLine (int x1, int y1, int x2, int y2); 199 drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)200 native public void drawArc (int x, int y, int width, int height, 201 int startAngle, int arcAngle); fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)202 native public void fillArc (int x, int y, int width, int height, 203 int startAngle, int arcAngle); drawOval(int x, int y, int width, int height)204 native public void drawOval(int x, int y, int width, int height); fillOval(int x, int y, int width, int height)205 native public void fillOval(int x, int y, int width, int height); 206 drawPolygon(int[] xPoints, int[] yPoints, int nPoints)207 native public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints); fillPolygon(int[] xPoints, int[] yPoints, int nPoints)208 native public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints); 209 drawPolyline(int[] xPoints, int[] yPoints, int nPoints)210 native public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints); 211 drawRect(int x, int y, int width, int height)212 native public void drawRect(int x, int y, int width, int height); fillRect(int x, int y, int width, int height)213 native public void fillRect (int x, int y, int width, int height); 214 drawString(String str, int x, int y, String fname, int size)215 native void drawString (String str, int x, int y, String fname, int size); drawString(String str, int x, int y)216 public void drawString (String str, int x, int y) 217 { 218 drawString (str, x, y, font.getName(), font.getSize()); 219 } 220 drawString(AttributedCharacterIterator ci, int x, int y)221 public void drawString (AttributedCharacterIterator ci, int x, int y) 222 { 223 throw new Error ("not implemented"); 224 } 225 drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)226 public void drawRoundRect(int x, int y, int width, int height, 227 int arcWidth, int arcHeight) 228 { 229 // System.out.println ("drawRoundRect called [UNIMPLEMENTED]"); 230 } 231 fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)232 public void fillRoundRect (int x, int y, int width, int height, 233 int arcWidth, int arcHeight) 234 { 235 // System.out.println ("fillRoundRect called [UNIMPLEMENTED]"); 236 } 237 getClip()238 public Shape getClip () 239 { 240 return getClipBounds (); 241 } 242 getClipBounds()243 public Rectangle getClipBounds () 244 { 245 // System.out.println ("returning CLIP: " + clip); 246 return new Rectangle (clip.x, clip.y, clip.width, clip.height); 247 } 248 getColor()249 public Color getColor () 250 { 251 return color; 252 } 253 getFont()254 public Font getFont () 255 { 256 return font; 257 } 258 getFontMetrics(Font font)259 public FontMetrics getFontMetrics (Font font) 260 { 261 return new GdkFontMetrics (font); 262 } 263 setClipRectangle(int x, int y, int width, int height)264 native void setClipRectangle (int x, int y, int width, int height); 265 setClip(int x, int y, int width, int height)266 public void setClip (int x, int y, int width, int height) 267 { 268 clip.x = x; 269 clip.y = y; 270 clip.width = width; 271 clip.height = height; 272 273 setClipRectangle (x, y, width, height); 274 } 275 setClip(Rectangle clip)276 public void setClip (Rectangle clip) 277 { 278 setClip (clip.x, clip.y, clip.width, clip.height); 279 } 280 setClip(Shape clip)281 public void setClip (Shape clip) 282 { 283 setClip (clip.getBounds ()); 284 } 285 setFGColor(int red, int green, int blue)286 native private void setFGColor (int red, int green, int blue); 287 setColor(Color c)288 public void setColor (Color c) 289 { 290 color = c; 291 292 if (xorColor == null) /* paint mode */ 293 setFGColor (color.getRed (), color.getGreen (), color.getBlue ()); 294 else /* xor mode */ 295 setFGColor (color.getRed () ^ xorColor.getRed (), 296 color.getGreen () ^ xorColor.getGreen (), 297 color.getBlue () ^ xorColor.getBlue ()); 298 } 299 setFont(Font font)300 public void setFont (Font font) 301 { 302 this.font = font; 303 } 304 setFunction(int gdk_func)305 native void setFunction (int gdk_func); 306 setPaintMode()307 public void setPaintMode () 308 { 309 xorColor = null; 310 311 setFunction (GDK_COPY); 312 setFGColor (color.getRed (), color.getGreen (), color.getBlue ()); 313 } 314 setXORMode(Color c)315 public void setXORMode (Color c) 316 { 317 xorColor = c; 318 319 setFunction (GDK_XOR); 320 setFGColor (color.getRed () ^ xorColor.getRed (), 321 color.getGreen () ^ xorColor.getGreen (), 322 color.getBlue () ^ xorColor.getBlue ()); 323 } 324 translateNative(int x, int y)325 native public void translateNative (int x, int y); 326 translate(int x, int y)327 public void translate (int x, int y) 328 { 329 clip.x -= x; 330 clip.y -= y; 331 332 translateNative (x, y); 333 } 334 } 335