1 /* Copyright (C) 2000, 2003 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
14 #include <gcj/array.h>
15 #include <gnu/gcj/RawData.h>
16 #include <java/lang/String.h>
17 #include <java/awt/Rectangle.h>
19 #include <gnu/gcj/xlib/Display.h>
20 #include <gnu/gcj/xlib/XID.h>
21 #include <gnu/gcj/xlib/Drawable.h>
22 #include <gnu/gcj/xlib/Font.h>
23 #include <gnu/gcj/xlib/XImage.h>
24 #include <gnu/gcj/xlib/XException.h>
25 #include <gnu/gcj/xlib/Clip.h>
26 #include <gnu/gcj/xlib/GC.h>
27 #include <gnu/gcj/xlib/XException.h>
29 typedef java::awt::Rectangle AWTRect
;
30 typedef JArray
<AWTRect
*> AWTRectArray
;
31 typedef std::vector
<XRectangle
> XRectVector
;
33 void gnu::gcj::xlib::GC::initStructure(GC
* copyFrom
)
35 Display
* display
= target
->getDisplay();
36 ::Display
* dpy
= (::Display
*) (display
->display
);
37 ::GC gc
= (::GC
) structure
;
40 // If we haven't already created a GC, create one now
41 ::Drawable drawableXID
= target
->getXID();
42 gc
= XCreateGC(dpy
, drawableXID
, 0, 0);
43 structure
= reinterpret_cast<gnu::gcj::RawData
*>(gc
);
45 throw new XException(JvNewStringLatin1("GC creation failed"));
50 ::GC fromGC
= (::GC
) copyFrom
->structure
;
51 XCopyGC(dpy
, fromGC
, ~0, gc
);
56 void gnu::gcj::xlib::GC::disposeImpl()
58 gnu::gcj::RawData
* lStructure
= structure
;
59 Drawable
* lTargetType
= target
;
61 if ((lStructure
== 0) || (lTargetType
== 0))
67 Display
* display
= lTargetType
->getDisplay();
68 ::Display
* dpy
= (::Display
*) (display
->display
);
69 ::GC gc
= (::GC
) lStructure
;
75 void gnu::gcj::xlib::GC::setForeground(jlong pixel
)
77 Display
* display
= target
->getDisplay();
78 ::Display
* dpy
= (::Display
*) (display
->display
);
79 ::GC gc
= (::GC
) structure
;
80 XSetForeground(dpy
, gc
, pixel
);
84 void gnu::gcj::xlib::GC::setFont(Font
* font
)
86 Display
* display
= target
->getDisplay();
87 ::Display
* dpy
= (::Display
*) (display
->display
);
88 ::GC gc
= (::GC
) structure
;
89 XSetFont(dpy
, gc
, font
->getXID());
93 void gnu::gcj::xlib::GC::drawString(jstring text
, jint x
, jint y
)
95 Display
* display
= target
->getDisplay();
96 ::Display
* dpy
= (::Display
*) (display
->display
);
97 ::Drawable drawableXID
= target
->getXID();
98 ::GC gc
= (::GC
) structure
;
100 jint length
= text
->length();
101 jchar
* txt
= JvGetStringChars(text
);
103 XChar2b xwchars
[length
];
105 // FIXME: Convert to the character set used in the font, which may
106 // or may not be unicode. For now, treat everything as 16-bit and
107 // use character codes directly, which should be OK for unicode or
108 // 8-bit ascii fonts.
110 for (int i
=0; i
<length
; i
++)
112 XChar2b
* xc
= &(xwchars
[i
]);
114 xc
->byte1
= (jc
>> 8) & 0xff;
115 xc
->byte2
= jc
& 0xff;
117 XDrawString16(dpy
, drawableXID
, gc
, x
, y
, xwchars
, length
);
120 void gnu::gcj::xlib::GC::drawLine(jint x1
, jint y1
, jint x2
, jint y2
)
122 Display
* display
= target
->getDisplay();
123 ::Display
* dpy
= (::Display
*) (display
->display
);
124 ::Drawable drawableXID
= target
->getXID();
125 ::GC gc
= (::GC
) structure
;
126 XDrawLine(dpy
, drawableXID
, gc
, x1
, y1
, x2
, y2
);
130 void gnu::gcj::xlib::GC::drawRectangle(jint x
, jint y
, jint w
, jint h
)
132 Display
* display
= target
->getDisplay();
133 ::Display
* dpy
= (::Display
*) (display
->display
);
134 ::Drawable drawableXID
= target
->getXID();
135 ::GC gc
= (::GC
) structure
;
136 XDrawRectangle(dpy
, drawableXID
, gc
, x
, y
, w
, h
);
140 void gnu::gcj::xlib::GC::fillRectangle(jint x
, jint y
, jint w
, jint h
)
142 Display
* display
= target
->getDisplay();
143 ::Display
* dpy
= (::Display
*) (display
->display
);
144 ::Drawable drawableXID
= target
->getXID();
145 ::GC gc
= (::GC
) structure
;
146 XFillRectangle(dpy
, drawableXID
, gc
, x
, y
, w
, h
);
150 void gnu::gcj::xlib::GC::drawArc(jint x
, jint y
, jint w
, jint h
,jint startAngle
, jint arcAngle
)
152 Display
* display
= target
->getDisplay();
153 ::Display
* dpy
= (::Display
*) (display
->display
);
154 ::Drawable drawableXID
= target
->getXID();
155 ::GC gc
= (::GC
) structure
;
156 XDrawArc(dpy
, drawableXID
, gc
, x
, y
, w
, h
, startAngle
* 64, arcAngle
* 64);
159 void gnu::gcj::xlib::GC::fillArc(jint x
, jint y
, jint w
, jint h
,jint startAngle
, jint arcAngle
)
161 Display
* display
= target
->getDisplay();
162 ::Display
* dpy
= (::Display
*) (display
->display
);
163 ::Drawable drawableXID
= target
->getXID();
164 ::GC gc
= (::GC
) structure
;
165 XFillArc(dpy
, drawableXID
, gc
, x
, y
, w
, h
, startAngle
* 64, arcAngle
* 64);
168 void gnu::gcj::xlib::GC::fillPolygon(jintArray xPoints
, jintArray yPoints
,
170 jint translateX
, jint translateY
)
172 Display
* display
= target
->getDisplay();
173 ::Display
* dpy
= (::Display
*) (display
->display
);
174 ::Drawable drawableXID
= target
->getXID();
175 ::GC gc
= (::GC
) structure
;
176 typedef ::XPoint xpoint
;
177 std::vector
<xpoint
> points(nPoints
+1);
178 for (int i
=0; i
<nPoints
; i
++)
180 points
[i
].x
= elements(xPoints
)[i
] + translateX
;
181 points
[i
].y
= elements(yPoints
)[i
] + translateY
;
183 points
[nPoints
] = points
[0];
184 XFillPolygon(dpy
, drawableXID
, gc
, &(points
.front()), nPoints
,
185 Complex
, CoordModeOrigin
);
189 void gnu::gcj::xlib::GC::clearArea(jint x
, jint y
, jint w
, jint h
,
192 Display
* display
= target
->getDisplay();
193 ::Display
* dpy
= (::Display
*) (display
->display
);
194 ::Drawable drawableXID
= target
->getXID();
196 XClearArea(dpy
, drawableXID
, x
, y
, w
, h
,
197 exposures
? True
: False
);
202 void gnu::gcj::xlib::GC::putImage(XImage
* image
,
203 jint srcX
, jint srcY
,
204 jint destX
, jint destY
,
205 jint width
, jint height
)
207 Display
* display
= target
->getDisplay();
208 ::Display
* dpy
= (::Display
*) (display
->display
);
209 ::Drawable drawableXID
= target
->getXID();
210 ::GC gc
= (::GC
) structure
;
211 ::XImage
* ximage
= (::XImage
*) (image
->structure
);
213 XPutImage(dpy
, drawableXID
, gc
, ximage
,
220 void gnu::gcj::xlib::GC::updateClip(AWTRectArray
* rectangles
)
222 int numRect
= JvGetArrayLength(rectangles
);
223 XRectVector
* xrectvector
= new XRectVector(numRect
);
225 for (int i
=0; i
<numRect
; i
++)
227 AWTRect
* awtrect
= elements(rectangles
)[i
];
228 XRectangle
& xrect
= (*xrectvector
)[i
];
230 xrect
.x
= awtrect
->x
;
231 xrect
.y
= awtrect
->y
;
232 xrect
.width
= awtrect
->width
;
233 xrect
.height
= awtrect
->height
;
236 Display
* display
= target
->getDisplay();
237 ::Display
* dpy
= (::Display
*) (display
->display
);
238 ::GC gc
= (::GC
) structure
;
242 int ordering
= Unsorted
;
243 XSetClipRectangles(dpy
, gc
, originX
, originY
,
244 &(xrectvector
->front()), numRect
,
249 void gnu::gcj::xlib::GC::copyArea (gnu::gcj::xlib::Drawable
* source
,
250 jint srcX
, jint srcY
,
251 jint destX
, jint destY
,
252 jint width
, jint height
)
254 Display
* display
= target
->getDisplay ();
255 ::Display
* dpy
= (::Display
*) (display
->display
);
256 ::Drawable drawableXID
= target
->getXID ();
257 ::GC gc
= (::GC
) structure
;
258 ::Drawable srcXID
= source
->getXID ();
260 XCopyArea (dpy
, srcXID
, drawableXID
, gc
, srcX
, srcY
, width
, height
,