2 Copyright (C) 1998, 1999, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
39 package gnu
.java
.awt
.peer
.gtk
;
41 import gnu
.classpath
.Configuration
;
43 import java
.awt
.Color
;
44 import java
.awt
.Dimension
;
46 import java
.awt
.FontMetrics
;
47 import java
.awt
.Graphics
;
48 import java
.awt
.Image
;
49 import java
.awt
.Rectangle
;
50 import java
.awt
.Shape
;
51 import java
.awt
.SystemColor
;
52 import java
.awt
.image
.ImageObserver
;
53 import java
.text
.AttributedCharacterIterator
;
55 public class GdkGraphics
extends Graphics
59 if (Configuration
.INIT_LOAD_LIBRARY
)
61 System
.loadLibrary("gtkpeer");
66 static native void initStaticState();
67 private final int native_state
= GtkGenericPeer
.getUniqueInteger ();
69 Color color
, xorColor
;
70 GtkComponentPeer component
;
71 Font font
= new Font ("Dialog", Font
.PLAIN
, 12);
78 static final int GDK_COPY
= 0, GDK_XOR
= 2;
80 native void initState (GtkComponentPeer component
);
81 native void initStateUnlocked (GtkComponentPeer component
);
82 native void initState (int width
, int height
);
83 native void initFromImage (GtkImage image
);
84 native void copyState (GdkGraphics g
);
86 GdkGraphics (GdkGraphics g
)
89 xorColor
= g
.xorColor
;
92 font
= new Font ("Dialog", Font
.PLAIN
, 12);
93 clip
= new Rectangle (g
.clip
);
94 component
= g
.component
;
99 GdkGraphics (int width
, int height
)
101 initState (width
, height
);
103 clip
= new Rectangle (0, 0, width
, height
);
104 font
= new Font ("Dialog", Font
.PLAIN
, 12);
107 GdkGraphics (GtkImage image
)
110 initFromImage (image
);
112 clip
= new Rectangle (0, 0,
113 image
.getWidth(null), image
.getHeight(null));
114 font
= new Font ("Dialog", Font
.PLAIN
, 12);
117 GdkGraphics (GtkComponentPeer component
)
119 this.component
= component
;
122 if (component
.isRealized ())
123 initComponentGraphics ();
125 connectSignals (component
);
128 void initComponentGraphics ()
130 initState (component
);
131 color
= component
.awtComponent
.getForeground ();
134 Dimension d
= component
.awtComponent
.getSize ();
135 clip
= new Rectangle (0, 0, d
.width
, d
.height
);
138 // called back by native side: realize_cb
139 void initComponentGraphicsUnlocked ()
141 initStateUnlocked (component
);
142 color
= component
.awtComponent
.getForeground ();
145 Dimension d
= component
.awtComponent
.getSize ();
146 clip
= new Rectangle (0, 0, d
.width
, d
.height
);
149 native void connectSignals (GtkComponentPeer component
);
151 public native void clearRect(int x
, int y
, int width
, int height
);
153 public void clipRect (int x
, int y
, int width
, int height
)
155 if (component
!= null && ! component
.isRealized ())
158 clip
= clip
.intersection (new Rectangle (x
, y
, width
, height
));
159 setClipRectangle (clip
.x
, clip
.y
, clip
.width
, clip
.height
);
162 public native void copyArea(int x
, int y
, int width
, int height
,
165 public Graphics
create ()
167 return new GdkGraphics (this);
170 public native void dispose();
172 public boolean drawImage (Image img
, int x
, int y
,
173 Color bgcolor
, ImageObserver observer
)
175 return drawImage(img
, x
, y
, img
.getWidth(null), img
.getHeight(null),
179 public boolean drawImage (Image img
, int x
, int y
, ImageObserver observer
)
181 return drawImage (img
, x
, y
, null, observer
);
184 public boolean drawImage (Image img
, int x
, int y
, int width
, int height
,
185 Color bgcolor
, ImageObserver observer
)
187 if (img
instanceof GtkImage
)
188 return ((GtkImage
)img
).drawImage (this, x
, y
, width
, height
,
191 return (new GtkImage(img
.getSource())).drawImage (this, x
, y
,
196 public boolean drawImage (Image img
, int x
, int y
, int width
, int height
,
197 ImageObserver observer
)
199 return drawImage (img
, x
, y
, width
, height
, null, observer
);
202 public boolean drawImage (Image img
, int dx1
, int dy1
, int dx2
, int dy2
,
203 int sx1
, int sy1
, int sx2
, int sy2
,
204 Color bgcolor
, ImageObserver observer
)
206 if (img
instanceof GtkImage
)
207 return ((GtkImage
)img
).drawImage(this, dx1
, dy1
, dx2
, dy2
,
208 sx1
, sy1
, sx2
, sy2
, bgcolor
, observer
);
210 return (new GtkImage(img
.getSource())).drawImage(this, dx1
, dy1
,
216 public boolean drawImage (Image img
, int dx1
, int dy1
, int dx2
, int dy2
,
217 int sx1
, int sy1
, int sx2
, int sy2
,
218 ImageObserver observer
)
220 return drawImage (img
, dx1
, dy1
, dx2
, dy2
,
225 public native void drawLine(int x1
, int y1
, int x2
, int y2
);
227 public native void drawArc(int x
, int y
, int width
, int height
,
228 int startAngle
, int arcAngle
);
229 public native void fillArc(int x
, int y
, int width
, int height
,
230 int startAngle
, int arcAngle
);
231 public native void drawOval(int x
, int y
, int width
, int height
);
232 public native void fillOval(int x
, int y
, int width
, int height
);
234 public native void drawPolygon(int[] xPoints
, int[] yPoints
, int nPoints
);
235 public native void fillPolygon(int[] xPoints
, int[] yPoints
, int nPoints
);
237 public native void drawPolyline(int[] xPoints
, int[] yPoints
, int nPoints
);
239 public native void drawRect(int x
, int y
, int width
, int height
);
240 public native void fillRect(int x
, int y
, int width
, int height
);
242 GdkFontPeer
getFontPeer()
244 return (GdkFontPeer
) getFont().getPeer();
247 native void drawString (GdkFontPeer f
, String str
, int x
, int y
);
248 public void drawString (String str
, int x
, int y
)
250 drawString(getFontPeer(), str
, x
, y
);
254 public void drawString (AttributedCharacterIterator ci
, int x
, int y
)
256 throw new Error ("not implemented");
259 public void drawRoundRect(int x
, int y
, int width
, int height
,
260 int arcWidth
, int arcHeight
)
262 if (arcWidth
> width
)
264 if (arcHeight
> height
)
267 int xx
= x
+ width
- arcWidth
;
268 int yy
= y
+ height
- arcHeight
;
270 drawArc (x
, y
, arcWidth
, arcHeight
, 90, 90);
271 drawArc (xx
, y
, arcWidth
, arcHeight
, 0, 90);
272 drawArc (xx
, yy
, arcWidth
, arcHeight
, 270, 90);
273 drawArc (x
, yy
, arcWidth
, arcHeight
, 180, 90);
275 int y1
= y
+ arcHeight
/ 2;
276 int y2
= y
+ height
- arcHeight
/ 2;
277 drawLine (x
, y1
, x
, y2
);
278 drawLine (x
+ width
, y1
, x
+ width
, y2
);
280 int x1
= x
+ arcWidth
/ 2;
281 int x2
= x
+ width
- arcWidth
/ 2;
282 drawLine (x1
, y
, x2
, y
);
283 drawLine (x1
, y
+ height
, x2
, y
+ height
);
286 public void fillRoundRect (int x
, int y
, int width
, int height
,
287 int arcWidth
, int arcHeight
)
289 if (arcWidth
> width
)
291 if (arcHeight
> height
)
294 int xx
= x
+ width
- arcWidth
;
295 int yy
= y
+ height
- arcHeight
;
297 fillArc (x
, y
, arcWidth
, arcHeight
, 90, 90);
298 fillArc (xx
, y
, arcWidth
, arcHeight
, 0, 90);
299 fillArc (xx
, yy
, arcWidth
, arcHeight
, 270, 90);
300 fillArc (x
, yy
, arcWidth
, arcHeight
, 180, 90);
302 fillRect (x
, y
+ arcHeight
/ 2, width
, height
- arcHeight
+ 1);
303 fillRect (x
+ arcWidth
/ 2, y
, width
- arcWidth
+ 1, height
);
306 public Shape
getClip ()
308 return getClipBounds ();
311 public Rectangle
getClipBounds ()
316 return clip
.getBounds();
319 public Color
getColor ()
324 public Font
getFont ()
329 public FontMetrics
getFontMetrics (Font font
)
331 return new GdkFontMetrics (font
);
334 native void setClipRectangle (int x
, int y
, int width
, int height
);
336 public void setClip (int x
, int y
, int width
, int height
)
338 if ((component
!= null && ! component
.isRealized ())
345 clip
.height
= height
;
347 setClipRectangle (x
, y
, width
, height
);
350 public void setClip (Rectangle clip
)
352 setClip (clip
.x
, clip
.y
, clip
.width
, clip
.height
);
355 public void setClip (Shape clip
)
360 Dimension d
= component
.awtComponent
.getSize();
361 setClip(new Rectangle (0, 0, d
.width
, d
.height
));
364 setClip(clip
.getBounds());
367 private native void setFGColor(int red
, int green
, int blue
);
369 public void setColor (Color c
)
376 if (xorColor
== null) /* paint mode */
377 setFGColor (color
.getRed (), color
.getGreen (), color
.getBlue ());
379 setFGColor (color
.getRed () ^ xorColor
.getRed (),
380 color
.getGreen () ^ xorColor
.getGreen (),
381 color
.getBlue () ^ xorColor
.getBlue ());
384 public void setFont (Font font
)
390 native void setFunction (int gdk_func
);
392 public void setPaintMode ()
396 setFunction (GDK_COPY
);
397 setFGColor (color
.getRed (), color
.getGreen (), color
.getBlue ());
400 public void setXORMode (Color c
)
404 setFunction (GDK_XOR
);
405 setFGColor (color
.getRed () ^ xorColor
.getRed (),
406 color
.getGreen () ^ xorColor
.getGreen (),
407 color
.getBlue () ^ xorColor
.getBlue ());
410 public native void translateNative(int x
, int y
);
412 public void translate (int x
, int y
)
414 if (component
!= null && ! component
.isRealized ())
420 translateNative (x
, y
);