1 /* Copyright (C) 2000, 2003  Free Software Foundation
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 #include <vector>
10 
11 #include <X11/Xlib.h>
12 
13 #include <gcj/cni.h>
14 #include <gcj/array.h>
15 #include <gnu/gcj/RawData.h>
16 #include <java/lang/String.h>
17 #include <java/awt/Rectangle.h>
18 
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>
28 
29 typedef java::awt::Rectangle AWTRect;
30 typedef JArray<AWTRect*> AWTRectArray;
31 typedef std::vector<XRectangle> XRectVector;
32 
initStructure(GC * copyFrom)33 void gnu::gcj::xlib::GC::initStructure(GC* copyFrom)
34 {
35   Display* display = target->getDisplay();
36   ::Display* dpy = (::Display*) (display->display);
37   ::GC gc = (::GC) structure;
38   if (gc == 0)
39   {
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);
44     if (gc == 0)
45       throw new XException(JvNewStringLatin1("GC creation failed"));
46   }
47 
48   if (copyFrom != 0)
49     {
50       ::GC fromGC = (::GC) copyFrom->structure;
51       XCopyGC(dpy, fromGC, ~0, gc);
52       // no fast fail
53     }
54 }
55 
disposeImpl()56 void gnu::gcj::xlib::GC::disposeImpl()
57 {
58   gnu::gcj::RawData* lStructure = structure;
59   Drawable* lTargetType = target;
60 
61   if ((lStructure == 0) || (lTargetType == 0))
62     return;
63 
64   structure = 0;
65   target = 0;
66 
67   Display* display = lTargetType->getDisplay();
68   ::Display* dpy = (::Display*) (display->display);
69   ::GC gc = (::GC) lStructure;
70 
71   XFreeGC(dpy, gc);
72   // no fast fail
73 }
74 
setForeground(jlong pixel)75 void gnu::gcj::xlib::GC::setForeground(jlong pixel)
76 {
77   Display* display = target->getDisplay();
78   ::Display* dpy = (::Display*) (display->display);
79   ::GC gc = (::GC) structure;
80   XSetForeground(dpy, gc, pixel);
81   // no fast fail
82 }
83 
setFont(Font * font)84 void gnu::gcj::xlib::GC::setFont(Font* font)
85 {
86   Display* display = target->getDisplay();
87   ::Display* dpy = (::Display*) (display->display);
88   ::GC gc = (::GC) structure;
89   XSetFont(dpy, gc, font->getXID());
90   // no fast fail
91 }
92 
drawString(jstring text,jint x,jint y)93 void gnu::gcj::xlib::GC::drawString(jstring text, jint x, jint y)
94 {
95   Display* display = target->getDisplay();
96   ::Display* dpy = (::Display*) (display->display);
97   ::Drawable drawableXID = target->getXID();
98   ::GC gc = (::GC) structure;
99 
100   jint length = text->length();
101   jchar* txt = JvGetStringChars(text);
102 
103   XChar2b xwchars[length];
104 
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.
109 
110   for (int i=0; i<length; i++)
111     {
112       XChar2b* xc = &(xwchars[i]);
113       jchar jc = txt[i];
114       xc->byte1 = (jc >> 8) & 0xff;
115       xc->byte2 = jc & 0xff;
116     }
117   XDrawString16(dpy, drawableXID, gc, x, y, xwchars, length);
118 }
119 
drawPoint(jint x,jint y)120 void gnu::gcj::xlib::GC::drawPoint(jint x, jint y)
121 {
122   Display* display = target->getDisplay();
123   ::Display* dpy = (::Display*) (display->display);
124   ::Drawable drawableXID = target->getXID();
125   ::GC gc = (::GC) structure;
126   XDrawPoint (dpy, drawableXID, gc, x, y);
127 }
128 
drawLine(jint x1,jint y1,jint x2,jint y2)129 void gnu::gcj::xlib::GC::drawLine(jint x1, jint y1, jint x2, jint y2)
130 {
131   Display* display = target->getDisplay();
132   ::Display* dpy = (::Display*) (display->display);
133   ::Drawable drawableXID = target->getXID();
134   ::GC gc = (::GC) structure;
135   XDrawLine(dpy, drawableXID, gc, x1, y1, x2, y2);
136   // no fast fail
137 }
138 
drawRectangle(jint x,jint y,jint w,jint h)139 void gnu::gcj::xlib::GC::drawRectangle(jint x, jint y, jint w, jint h)
140 {
141   Display* display = target->getDisplay();
142   ::Display* dpy = (::Display*) (display->display);
143   ::Drawable drawableXID = target->getXID();
144   ::GC gc = (::GC) structure;
145   XDrawRectangle(dpy, drawableXID, gc, x, y, w, h);
146   // no fast fail
147 }
148 
fillRectangle(jint x,jint y,jint w,jint h)149 void gnu::gcj::xlib::GC::fillRectangle(jint x, jint y, jint w, jint h)
150 {
151   Display* display = target->getDisplay();
152   ::Display* dpy = (::Display*) (display->display);
153   ::Drawable drawableXID = target->getXID();
154   ::GC gc = (::GC) structure;
155   XFillRectangle(dpy, drawableXID, gc, x, y, w, h);
156   // no fast fail
157 }
158 
drawArc(jint x,jint y,jint w,jint h,jint startAngle,jint arcAngle)159 void gnu::gcj::xlib::GC::drawArc(jint x, jint y, jint w, jint h,jint startAngle, jint arcAngle)
160 {
161   Display* display = target->getDisplay();
162   ::Display* dpy = (::Display*) (display->display);
163   ::Drawable drawableXID = target->getXID();
164   ::GC gc = (::GC) structure;
165   XDrawArc(dpy, drawableXID, gc, x, y, w, h, startAngle * 64, arcAngle * 64);
166 }
167 
fillArc(jint x,jint y,jint w,jint h,jint startAngle,jint arcAngle)168 void gnu::gcj::xlib::GC::fillArc(jint x, jint y, jint w, jint h,jint startAngle, jint arcAngle)
169 {
170   Display* display = target->getDisplay();
171   ::Display* dpy = (::Display*) (display->display);
172   ::Drawable drawableXID = target->getXID();
173   ::GC gc = (::GC) structure;
174   XFillArc(dpy, drawableXID, gc, x, y, w, h, startAngle * 64, arcAngle * 64);
175 }
176 
fillPolygon(jintArray xPoints,jintArray yPoints,jint nPoints,jint translateX,jint translateY)177 void gnu::gcj::xlib::GC::fillPolygon(jintArray xPoints, jintArray yPoints,
178 				     jint nPoints,
179 				     jint translateX, jint translateY)
180 {
181   Display* display = target->getDisplay();
182   ::Display* dpy = (::Display*) (display->display);
183   ::Drawable drawableXID = target->getXID();
184   ::GC gc = (::GC) structure;
185   typedef ::XPoint xpoint;
186   std::vector<xpoint> points(nPoints+1);
187   for (int i=0; i<nPoints; i++)
188     {
189       points[i].x = elements(xPoints)[i] + translateX;
190       points[i].y = elements(yPoints)[i] + translateY;
191     }
192   points[nPoints] = points[0];
193   XFillPolygon(dpy, drawableXID, gc, &(points.front()), nPoints,
194 	       Complex, CoordModeOrigin);
195   // no fast fail
196 }
197 
clearArea(jint x,jint y,jint w,jint h,jboolean exposures)198 void gnu::gcj::xlib::GC::clearArea(jint x, jint y, jint w, jint h,
199 				   jboolean exposures)
200 {
201   Display* display = target->getDisplay();
202   ::Display* dpy = (::Display*) (display->display);
203   ::Drawable drawableXID = target->getXID();
204 
205   XClearArea(dpy, drawableXID, x, y, w, h,
206 	     exposures ? True : False);
207   // no fast fail
208 }
209 
210 
putImage(XImage * image,jint srcX,jint srcY,jint destX,jint destY,jint width,jint height)211 void gnu::gcj::xlib::GC::putImage(XImage* image,
212 				  jint srcX, jint srcY,
213 				  jint destX, jint destY,
214 				  jint width, jint height)
215 {
216   Display* display = target->getDisplay();
217   ::Display* dpy = (::Display*) (display->display);
218   ::Drawable drawableXID = target->getXID();
219   ::GC gc = (::GC) structure;
220   ::XImage* ximage = (::XImage*) (image->structure);
221 
222   XPutImage(dpy, drawableXID, gc, ximage,
223 	    srcX, srcY,
224 	    destX, destY,
225 	    width, height);
226   // no fast fail
227 }
228 
updateClip(AWTRectArray * rectangles)229 void gnu::gcj::xlib::GC::updateClip(AWTRectArray* rectangles)
230 {
231   int numRect = JvGetArrayLength(rectangles);
232   XRectVector* xrectvector = new XRectVector(numRect);
233 
234   for (int i=0; i<numRect; i++)
235   {
236     AWTRect* awtrect = elements(rectangles)[i];
237     XRectangle& xrect = (*xrectvector)[i];
238 
239     xrect.x      = awtrect->x;
240     xrect.y      = awtrect->y;
241     xrect.width  = awtrect->width;
242     xrect.height = awtrect->height;
243   }
244 
245   Display* display = target->getDisplay();
246   ::Display* dpy = (::Display*) (display->display);
247   ::GC gc = (::GC) structure;
248 
249   int originX = 0;
250   int originY = 0;
251   int ordering = Unsorted;
252   XSetClipRectangles(dpy, gc, originX, originY,
253 		     &(xrectvector->front()), numRect,
254 		     ordering);
255   delete xrectvector;
256 }
257 
copyArea(gnu::gcj::xlib::Drawable * source,jint srcX,jint srcY,jint destX,jint destY,jint width,jint height)258 void gnu::gcj::xlib::GC::copyArea (gnu::gcj::xlib::Drawable * source,
259 				  jint srcX, jint srcY,
260 				  jint destX, jint destY,
261 				  jint width, jint height)
262 {
263   Display* display = target->getDisplay ();
264   ::Display* dpy = (::Display*) (display->display);
265   ::Drawable drawableXID = target->getXID ();
266   ::GC gc = (::GC) structure;
267   ::Drawable srcXID = source->getXID ();
268 
269   XCopyArea (dpy, srcXID, drawableXID, gc, srcX, srcY, width, height,
270     destX, destY);
271 }
272