1 /*
2  * XPilot, a multiplayer gravity war game.  Copyright (C) 1991-2001 by
3  *
4  *      Bj�rn Stabell        <bjoern@xpilot.org>
5  *      Ken Ronny Schouten   <ken@xpilot.org>
6  *      Bert Gijsbers        <bert@xpilot.org>
7  *      Dick Balaska         <dick@xpilot.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 /***************************************************************************\
25 *  winX11.c - X11 to Windoze converter										*
26 *																			*
27 *  This file is the direct interface to the X11 library.					*
28 *  Any function that has a unix man page belongs in this file.				*
29 \***************************************************************************/
30 
31 #include "winX.h"
32 #include "winX_.h"
33 #include "draw.h"
34 
35 #include "../error.h"
36 #include "../../client/NT/winClient.h"	/* This needs to be removed */
37 
38 const int top = 0;
39 /*****************************************************************************/
XFillRectangle(Display * dpy,Drawable d,GC gc,int x,int y,unsigned int w,unsigned int h)40 XFillRectangle(Display * dpy, Drawable d, GC gc, int x, int y,
41 	       unsigned int w, unsigned int h)
42 {
43 
44     HDC hDC = xid[d].hwnd.hBmpDC;
45     RECT r;
46     r.left = x;
47     r.top = y;
48     r.right = x + w;
49     r.bottom = y + h;
50     FillRect(hDC, &r, WinXGetBrush(xid[gc].hgc.xgcv.foreground));
51 
52     return (0);
53 }
54 
55 /*****************************************************************************/
XDrawRectangle(Display * dpy,Drawable d,GC gc,int x,int y,unsigned int w,unsigned int h)56 XDrawRectangle(Display * dpy, Drawable d, GC gc, int x, int y,
57 	       unsigned int w, unsigned int h)
58 {
59 
60     HDC hDC = xid[d].hwnd.hBmpDC;
61     MoveToEx(hDC, x, y, NULL);
62     LineTo(hDC, x + w, y);
63     LineTo(hDC, x + w, y + h);
64     LineTo(hDC, x, y + h);
65     LineTo(hDC, x, y);
66 
67     return (0);
68 }
69 
XFillRectangles(Display * dpy,Drawable d,GC gc,XRectangle * rects,int nrectangles)70 XFillRectangles(Display * dpy, Drawable d, GC gc,
71 		XRectangle * rects, int nrectangles)
72 {
73     int i;
74     HDC hDC = xid[d].hwnd.hBmpDC;
75     HBRUSH hBrush = WinXGetBrush(xid[gc].hgc.xgcv.foreground);
76     RECT r;
77 
78     for (i = 0; i < nrectangles; i++, rects++) {
79 	r.left = rects->x;
80 	r.top = rects->y;
81 	r.right = rects->x + rects->width;
82 	r.bottom = rects->y + rects->height;
83 	FillRect(hDC, &r, hBrush);
84     }
85     return (0);
86 }
87 
88 /*****************************************************************************/
XDrawLine(Display * dpy,Drawable d,GC gc,int x1,int y1,int x2,int y2)89 XDrawLine(Display * dpy, Drawable d, GC gc, int x1, int y1, int x2, int y2)
90 {
91     HDC hDC = xid[d].hwnd.hBmpDC;
92     MoveToEx(hDC, x1, y1, NULL);
93     LineTo(hDC, x2, y2);
94     return (0);
95 }
96 
97 /*****************************************************************************/
XDrawLines(Display * dpy,Drawable d,GC gc,XPoint * points,int npoints,int mode)98 XDrawLines(Display * dpy, Drawable d, GC gc, XPoint * points,
99 	   int npoints, int mode)
100 {
101     int i = 0;
102     HDC hDC = xid[d].hwnd.hBmpDC;
103 
104     if (mode == CoordModePrevious) {
105 	int x, y;
106 	x = points->x;
107 	y = points->y;
108 	MoveToEx(hDC, x, y, NULL);
109 	points++;
110 	for (i = 1; i < npoints; i++, points++) {
111 	    x += points->x;
112 	    y += points->y;
113 	    LineTo(hDC, x, y);
114 	}
115 
116     } else {
117 	MoveToEx(hDC, points->x, points->y, NULL);
118 	points++;
119 	for (i = 1; i < npoints; i++, points++) {
120 	    LineTo(hDC, points->x, points->y);
121 	}
122     }
123     return (0);
124 }
125 
126 /*****************************************************************************/
XDrawSegments(Display * dpy,Drawable d,GC gc,XSegment * segments,int nsegments)127 XDrawSegments(Display * dpy, Drawable d, GC gc,
128 	      XSegment * segments, int nsegments)
129 {
130     int i;
131     HDC hDC = xid[d].hwnd.hBmpDC;
132 
133     for (i = 0; i < nsegments; i++, segments++) {
134 	MoveToEx(hDC, segments->x1, segments->y1, NULL);
135 	LineTo(hDC, segments->x2, segments->y2);
136     }
137 
138     return (0);
139 }
140 
141 /*****************************************************************************/
XDrawPoint(Display * dpy,Drawable d,GC gc,int x,int y)142 XDrawPoint(Display * dpy, Drawable d, GC gc, int x, int y)
143 {
144     HDC hDC = xid[d].hwnd.hBmpDC;
145     SetPixelV(hDC, x, y, WinXPColour(xid[gc].hgc.xgcv.foreground));
146     return (0);
147 }
148 
149 /*****************************************************************************/
XDrawPoints(Display * dpy,Drawable d,GC gc,XPoint * points,int npoints,int mode)150 XDrawPoints(Display * dpy, Drawable d, GC gc,
151 	    XPoint * points, int npoints, int mode)
152 {
153     int i;
154     HDC hDC = xid[d].hwnd.hBmpDC;
155     COLORREF col = WinXPColour(xid[gc].hgc.xgcv.foreground);
156 
157     for (i = 0; i < npoints; i++, points++)
158 	SetPixelV(hDC, points->x, points->y, col);
159 
160     return (0);
161 }
162 
163 /*****************************************************************************/
XFillPolygon(Display * dpy,Drawable d,GC gc,XPoint * points,int npoints,int shape,int mode)164 XFillPolygon(Display * dpy, Drawable d, GC gc, XPoint * points,
165 	     int npoints, int shape, int mode)
166 {
167     int i;
168     int px1, px2, py1, py2;	// bounding box for the polygon
169     HDC hdc = xid[d].hwnd.hBmpDC;
170 
171     /*
172      * As Windows 95/98/ME doesn't support textured brushes with texture size
173      * over 8x8, I'll provide my own routine for painting textured polygons.
174      * It first creates a clipping region that restricts GDI operations to the
175      * area of the polygon. Then it blits the texture bitmap over the polygon
176      * so that it gets fully covered.
177      */
178 
179     if (!BeginPath(hdc))
180 	return -1;
181 
182     px1 = px2 = points->x;
183     py1 = py2 = points->y;
184     if (!MoveToEx(hdc, points->x, points->y, NULL))
185 	return -1;
186     points++;
187 
188     for (i = 1; i < npoints; i++, points++) {
189 	if (!LineTo(hdc, points->x, points->y))
190 	    return -1;
191 	if (points->x < px1)
192 	    px1 = points->x;
193 	else if (points->x > px2)
194 	    px2 = points->x;
195 	if (points->y < py1)
196 	    py1 = points->y;
197 	else if (points->y > py2)
198 	    py2 = points->y;
199     }
200 
201     if (!EndPath(hdc))
202 	return -1;
203 
204 
205     if (xid[gc].hgc.xgcv.fill_style != FillTiled) {
206 
207 	if (!StrokeAndFillPath(hdc))
208 	    return -1;
209 
210     } else {
211 
212 	int x, y;
213 	int sx, sy;		// where to start blitting
214 	SIZE bs;		// bitmap dimensions
215 	HBITMAP hBmp = (HBITMAP) xid[gc].hgc.xgcv.tile;
216 	extern HDC itemsDC;	// from winX.c
217 
218 	if (!GetBitmapDimensionEx(hBmp, &bs))
219 	    return -1;
220 	if (!SelectObject(itemsDC, hBmp))
221 	    return -1;
222 	if (!SelectPalette(itemsDC, myPal, FALSE))
223 	    return -1;
224 	if (RealizePalette(itemsDC) == GDI_ERROR)
225 	    return -1;
226 
227 	sx = xid[gc].hgc.xgcv.ts_x_origin - px1;
228 	sx = (sx > 0) ? px1 + sx % bs.cx - bs.cx : px1 - sx % bs.cx;
229 
230 	sy = xid[gc].hgc.xgcv.ts_y_origin - py1;
231 	sy = (sy > 0) ? py1 + sy % bs.cy - bs.cy : py1 - sy % bs.cy;
232 
233 	if (!SelectClipPath(hdc, RGN_AND))
234 	    return -1;
235 
236 	for (x = sx; x < px2; x += bs.cx) {
237 	    for (y = sy; y < py2; y += bs.cy) {
238 		//XDrawRectangle(dpy, d, gc, x, y, bs.cx, bs.cy);
239 		BitBlt(hdc, x, y, bs.cx, bs.cy, itemsDC, 0, 0, SRCCOPY);
240 	    }
241 	}
242 
243 	SelectClipRgn(hdc, NULL);
244     }
245 
246     return (0);
247 }
248 
249 /*****************************************************************************/
XDrawArc(Display * dpy,Drawable d,GC gc,int x,int y,unsigned int width,unsigned int height,int angle1,int angle2)250 XDrawArc(Display * dpy, Drawable d, GC gc, int x, int y,
251 	 unsigned int width, unsigned int height, int angle1, int angle2)
252 {
253     HDC hDC = xid[d].hwnd.hBmpDC;
254     MoveToEx(hDC, x + width, y + height / 2, NULL);
255     if (bWinNT)
256 	AngleArc(hDC, x + width / 2, y + height / 2, width / 2,
257 		 (float) 0.0, (float) (angle2 / 64.0));
258     else
259 	AngleArc2(hDC, x + width / 2, y + height / 2, width / 2, 0.0,
260 		  angle2 / 64.0, FALSE);
261     return (0);
262 }
263 
264 /*****************************************************************************/
XDrawArcs(Display * dpy,Drawable d,GC gc,XArc * arcs,int narcs)265 XDrawArcs(Display * dpy, Drawable d, GC gc, XArc * arcs, int narcs)
266 {
267     int i;
268     HDC hDC = xid[d].hwnd.hBmpDC;
269 
270     for (i = 0; i < narcs; i++, arcs++) {
271 	MoveToEx(hDC, arcs->x + arcs->width, arcs->y + arcs->height / 2,
272 		 NULL);
273 	if (bWinNT)
274 	    AngleArc(hDC, arcs->x + arcs->width / 2,
275 		     arcs->y + arcs->height / 2, arcs->width / 2,
276 		     (float) 0.0, (float) (arcs->angle2 / 64.0));
277 	else
278 	    AngleArc2(hDC, arcs->x + arcs->width / 2,
279 		      arcs->y + arcs->height / 2, arcs->width / 2, 0.0,
280 		      arcs->angle2 / 64.0, FALSE);
281     }
282     return (0);
283 }
284 
285 /*****************************************************************************/
XFillArc(Display * dpy,Drawable d,GC gc,int x,int y,unsigned int width,unsigned int height,int angle1,int angle2)286 XFillArc(Display * dpy, Drawable d, GC gc, int x, int y,
287 	 unsigned int width, unsigned int height, int angle1, int angle2)
288 {
289 
290     HDC hDC = xid[d].hwnd.hBmpDC;
291 
292     // 2 separate bits 'cause Win95 doesn't put Arc into current path
293     // We use Pie instead.
294     if (bWinNT) {
295 	BeginPath(hDC);
296 	MoveToEx(hDC, x + width, y + height / 2, NULL);
297 	AngleArc(hDC, x + width / 2, y + height / 2, width / 2,
298 		 (float) 0.0, (float) (angle2 / 64.0));
299 	EndPath(hDC);
300 	StrokeAndFillPath(hDC);
301     } else {
302 	MoveToEx(hDC, x + width, y + height / 2, NULL);
303 	AngleArc2(hDC, x + width / 2, y + height / 2, width / 2, 0.0,
304 		  angle2 / 64.0, TRUE);
305     }
306     return (0);
307 }
308 
309 /*****************************************************************************/
XDrawString(Display * dpy,Drawable d,GC gc,int x,int y,const char * string,int length)310 XDrawString(Display * dpy, Drawable d, GC gc, int x, int y,
311 	    const char *string, int length)
312 {
313     HDC hDC = xid[d].hwnd.hBmpDC;
314     HFONT hOldFont = NULL;
315 
316     hOldFont = SelectObject(hDC, xid[gc].hgc.hfont);
317     SetTextColor(hDC, WinXPColour(xid[gc].hgc.xgcv.foreground));
318     TextOut(hDC, x, y - xid[xid[gc].hgc.xgcv.font].font.font->ascent,
319 	    string, length);
320     if (hOldFont)
321 	SelectObject(hDC, hOldFont);
322 
323     return (0);
324 }
325 
326 /*****************************************************************************/
XTextWidth(XFontStruct * font,const char * string,int length)327 XTextWidth(XFontStruct * font, const char *string, int length)
328 {
329     HDC hDC = xid[top].hwnd.hBmpDC;
330     SIZE size;
331     XID i, f;
332     for (f = 0; f < MAX_XIDS; f++) {
333 	if (xid[f].type == XIDTYPE_FONT && xid[f].font.font == font)
334 	    break;
335     }
336     if (f == MAX_XIDS) {
337 	Trace("Huh? Can't match font for string <%s>\n", string);
338     } else {
339 	for (i = 0; i < MAX_XIDS; i++)
340 	    if (xid[i].type == XIDTYPE_HDC)
341 		if (xid[i].hgc.xgcv.font == f)
342 		    break;
343     }
344     if (i == MAX_XIDS) {
345 	Trace("Huh? Can't find a GC for font %d, string=<%s>\n", f,
346 	      string);
347     } else {
348 	hDC = xid[xid[i].hgc.xidhwnd].hwnd.hBmpDC;
349 	SelectObject(hDC, xid[i].hgc.hfont);
350     }
351 
352     GetTextExtentPoint32(hDC, string, length, &size);
353     return (size.cx);
354 }
355 
356 /*****************************************************************************/
XChangeGC(Display * dpy,GC gc,unsigned long valuemask,XGCValues * values)357 XChangeGC(Display * dpy, GC gc, unsigned long valuemask,
358 	  XGCValues * values)
359 {
360     XGCValues *xgcv;
361     if (xid[gc].type != XIDTYPE_HDC)
362 	return 0;
363 
364     xgcv = &xid[gc].hgc.xgcv;
365 
366     if (valuemask & GCFunction)
367 	xgcv->function = values->function;
368     if (valuemask & GCPlaneMask)
369 	xgcv->plane_mask = values->plane_mask;
370     if (valuemask & GCForeground)
371 	xgcv->foreground = values->foreground;
372     if (valuemask & GCBackground)
373 	xgcv->background = values->background;
374     if (valuemask & GCLineWidth)
375 	xgcv->line_width = values->line_width;
376     if (valuemask & GCLineStyle)
377 	xgcv->line_style = values->line_style;
378     if (valuemask & GCCapStyle)
379 	xgcv->cap_style = values->cap_style;
380     if (valuemask & GCJoinStyle)
381 	xgcv->join_style = values->join_style;
382     if (valuemask & GCFillStyle)
383 	xgcv->fill_style = values->fill_style;
384     if (valuemask & GCFillRule)
385 	xgcv->fill_rule = values->fill_rule;
386     if (valuemask & GCTile)
387 	xgcv->tile = values->tile;
388     if (valuemask & GCStipple)
389 	xgcv->stipple = values->stipple;
390     if (valuemask & GCTileStipXOrigin)
391 	xgcv->ts_x_origin = values->ts_x_origin;
392     if (valuemask & GCTileStipYOrigin)
393 	xgcv->ts_y_origin = values->ts_y_origin;
394     if (valuemask & GCFont)
395 	xgcv->font = values->font;
396     if (valuemask & GCSubwindowMode)
397 	xgcv->subwindow_mode = values->subwindow_mode;
398     if (valuemask & GCGraphicsExposures)
399 	xgcv->graphics_exposures = values->graphics_exposures;
400     if (valuemask & GCClipXOrigin)
401 	xgcv->clip_x_origin = values->clip_x_origin;
402     if (valuemask & GCClipYOrigin)
403 	xgcv->clip_y_origin = values->clip_y_origin;
404     if (valuemask & GCDashOffset)
405 	xgcv->dash_offset = values->dash_offset;
406     if (valuemask & GCArcMode)
407 	xgcv->arc_mode = values->arc_mode;
408 
409     if (valuemask & (GCForeground | GCLineWidth | GCLineStyle))
410 	WinXSelectPen(gc);
411 
412     return (1);
413 }
414 
415 /*****************************************************************************/
XGetGCValues(Display * dpy,GC gc,unsigned long valuemask,XGCValues * values)416 int XGetGCValues(Display * dpy, GC gc, unsigned long valuemask,
417 		 XGCValues * values)
418 {
419     XGCValues *xgcv;
420     if (xid[gc].type != XIDTYPE_HDC)
421 	return 0;
422 
423     xgcv = &xid[gc].hgc.xgcv;
424 
425     if (valuemask & GCFunction)
426 	values->function = xgcv->function;
427     if (valuemask & GCPlaneMask)
428 	values->plane_mask = xgcv->plane_mask;
429     if (valuemask & GCForeground)
430 	values->foreground = xgcv->foreground;
431     if (valuemask & GCBackground)
432 	values->background = xgcv->background;
433     if (valuemask & GCLineWidth)
434 	values->line_width = xgcv->line_width;
435     if (valuemask & GCLineStyle)
436 	values->line_style = xgcv->line_style;
437     if (valuemask & GCCapStyle)
438 	values->cap_style = xgcv->cap_style;
439     if (valuemask & GCJoinStyle)
440 	values->join_style = xgcv->join_style;
441     if (valuemask & GCFillStyle)
442 	values->fill_style = xgcv->fill_style;
443     if (valuemask & GCFillRule)
444 	values->fill_rule = xgcv->fill_rule;
445     if (valuemask & GCTile)
446 	values->tile = xgcv->tile;
447     if (valuemask & GCStipple)
448 	values->stipple = xgcv->stipple;
449     if (valuemask & GCTileStipXOrigin)
450 	values->ts_x_origin = xgcv->ts_x_origin;
451     if (valuemask & GCTileStipYOrigin)
452 	values->ts_y_origin = xgcv->ts_y_origin;
453     if (valuemask & GCFont)
454 	values->font = xgcv->font;
455     if (valuemask & GCSubwindowMode)
456 	values->subwindow_mode = xgcv->subwindow_mode;
457     if (valuemask & GCGraphicsExposures)
458 	values->graphics_exposures = xgcv->graphics_exposures;
459     if (valuemask & GCClipXOrigin)
460 	values->clip_x_origin = xgcv->clip_x_origin;
461     if (valuemask & GCClipYOrigin)
462 	values->clip_y_origin = xgcv->clip_y_origin;
463     if (valuemask & GCDashOffset)
464 	values->dash_offset = xgcv->dash_offset;
465     if (valuemask & GCArcMode)
466 	values->arc_mode = xgcv->arc_mode;
467 
468 
469     if (valuemask & GCFunction) {
470 	values->function = GXcopy;
471     }
472     if (valuemask & GCBackground) {
473 	values->background = BLACK;	/* always black */
474     }
475 
476     return 1;
477 }
478 
479 /*****************************************************************************/
XSetLineAttributes(Display * dpy,GC gc,unsigned int lwidth,int lstyle,int cap_style,int join_style)480 XSetLineAttributes(Display * dpy, GC gc, unsigned int lwidth,
481 		   int lstyle, int cap_style, int join_style)
482 {
483     XGCValues *xgcv;
484 
485     if (xid[gc].type != XIDTYPE_HDC)
486 	return -1;
487 
488     xgcv = &xid[gc].hgc.xgcv;
489     xgcv->line_width = lwidth;
490     xgcv->line_style = lstyle;
491     xgcv->cap_style = cap_style;
492     xgcv->join_style = join_style;
493 
494     WinXSelectPen(gc);
495 
496     return (0);
497 }
498 
499 /*****************************************************************************/
XCopyArea(Display * dpy,Drawable src,Drawable dest,GC gc,int src_x,int src_y,unsigned int width,unsigned int height,int dest_x,int dest_y)500 XCopyArea(Display * dpy, Drawable src, Drawable dest, GC gc,
501 	  int src_x, int src_y, unsigned int width, unsigned int height,
502 	  int dest_x, int dest_y)
503 {
504     return (0);
505 }
506 
507 /*****************************************************************************/
XSetTile(Display * dpy,GC gc,Pixmap tile)508 XSetTile(Display * dpy, GC gc, Pixmap tile)
509 {
510     if (xid[gc].type != XIDTYPE_HDC)
511 	return -1;
512     xid[gc].hgc.xgcv.tile = tile;
513     return (0);
514 }
515 
516 /*****************************************************************************/
XSetTSOrigin(Display * dpy,GC gc,int ts_x_origin,int ts_y_origin)517 XSetTSOrigin(Display * dpy, GC gc, int ts_x_origin, int ts_y_origin)
518 {
519     if (xid[gc].type != XIDTYPE_HDC)
520 	return -1;
521     xid[gc].hgc.xgcv.ts_x_origin = ts_x_origin;
522     xid[gc].hgc.xgcv.ts_y_origin = ts_y_origin;
523     return (0);
524 }
525 
526 /*****************************************************************************/
XSetFillStyle(Display * dpy,GC gc,int fill_style)527 XSetFillStyle(Display * dpy, GC gc, int fill_style)
528 {
529     if (xid[gc].type != XIDTYPE_HDC)
530 	return -1;
531     xid[gc].hgc.xgcv.fill_style = fill_style;
532     return (0);
533 }
534 
535 /*****************************************************************************/
XSetFunction(Display * dpy,GC gc,int function)536 XSetFunction(Display * dpy, GC gc, int function)
537 {
538     if (xid[gc].type != XIDTYPE_HDC)
539 	return -1;
540     xid[gc].hgc.xgcv.function = function;
541     return (0);
542 }
543 
544 /*****************************************************************************/
XBell(Display * dpy,int percent)545 XBell(Display * dpy, int percent)
546 {
547     MessageBeep(MB_ICONEXCLAMATION);
548     return (0);
549 }
550 
551 /*****************************************************************************/
XFlush(Display * dpy)552 XFlush(Display * dpy)
553 {
554     return (0);
555 }
556 
557 /*****************************************************************************/
XCreatePixmap(Display * dpy,Drawable d,unsigned int width,unsigned int height,unsigned int depth)558 XCreatePixmap(Display * dpy, Drawable d,
559 	      unsigned int width, unsigned int height, unsigned int depth)
560 {
561     XID txid;
562     HDC hDC = xid[d].hwnd.hBmpDC;
563     HDC newDC = CreateCompatibleDC(NULL);
564 //      HDC             hScreenDC = GetDC(xid[draw].hwnd.hWnd);
565     HDC hScreenDC = GetDC(xid[top].hwnd.hWnd);
566     HBITMAP hbm = CreateCompatibleBitmap(hScreenDC, width, height);
567 
568 //      ReleaseDC(xid[draw].hwnd.hWnd, hScreenDC);
569     ReleaseDC(xid[top].hwnd.hWnd, hScreenDC);
570 
571     if (!hbm) {
572 	error("Can't create pixmap\n");
573 	return (0);
574     }
575     SelectObject(newDC, hbm);
576     SelectPalette(newDC, myPal, FALSE);
577     RealizePalette(newDC);
578 
579     txid = GetFreeXid();
580     xid[txid].type = XIDTYPE_PIXMAP;
581     xid[txid].hpix.hbm = hbm;
582     xid[txid].hpix.hDC = newDC;
583 #if 0
584     if (++max_xid > MAX_XIDS) {
585 	error("Too many XIDS!\n");
586 	max_xid--;
587     }
588     return (max_xid - 1);
589 #endif
590     return (txid);
591 }
592 
593 /*****************************************************************************/
XFreePixmap(Display * dpy,Pixmap pixmap)594 XFreePixmap(Display * dpy, Pixmap pixmap)
595 {
596     return (0);
597 }
598 
599 /*****************************************************************************/
XSetPlaneMask(Display * dpy,GC gc,unsigned long plane_mask)600 XSetPlaneMask(Display * dpy, GC gc, unsigned long plane_mask)
601 {
602     if (xid[gc].type != XIDTYPE_HDC)
603 	return -1;
604     xid[gc].hgc.xgcv.plane_mask = plane_mask;
605     return (0);
606 }
607 
608 /*****************************************************************************/
XClearWindow(Display * dpy,Window w)609 XClearWindow(Display * dpy, Window w)
610 {
611     HWND hWnd = xid[w].hwnd.hWnd;
612     HDC hDC = xid[w].hwnd.hBmpDC;
613     RECT rect;
614 
615     GetClientRect(hWnd, &rect);
616     FillRect(hDC, &rect, WinXGetBrush(xid[w].hwnd.bgcolor));
617     return (0);
618 }
619 
620 /*****************************************************************************/
XClearArea(Display * d,Window w,int x,int y,unsigned int width,unsigned int height,Bool exposures)621 XClearArea(Display * d, Window w, int x, int y,
622 	   unsigned int width, unsigned int height, Bool exposures)
623 {
624     HWND hWnd = xid[w].hwnd.hWnd;
625     HDC hDC = xid[w].hwnd.hBmpDC;
626     RECT rect;
627 
628     rect.left = x;
629     rect.top = y;
630     rect.right = x + width;
631     rect.bottom = y + height;
632     FillRect(hDC, &rect, WinXGetBrush(xid[w].hwnd.bgcolor));
633     return (0);
634 }
635 
636 /*****************************************************************************/
XStoreName(Display * dpy,Window w,const char * window_name)637 XStoreName(Display * dpy, Window w, const char *window_name)
638 {
639     SetWindowText(xid[w].hwnd.hWnd, window_name);
640     return (0);
641 }
642 
643 /*****************************************************************************/
XLookupKeysym(XKeyEvent * key_event,int index)644 XLookupKeysym(XKeyEvent * key_event, int index)
645 {
646     return (key_event->keycode);
647 }
648 
649 /*****************************************************************************/
XQueryFont(Display * dpy,XID font_ID)650 XFontStruct *XQueryFont(Display * dpy, XID font_ID)
651 {
652     return (xid[xid[font_ID].hgc.xgcv.font].font.font);
653 }
654 
655 /*****************************************************************************/
XFreeFontInfo(char ** names,XFontStruct * free_info,int count)656 XFreeFontInfo(char **names, XFontStruct *free_info, int count)
657 {
658 	return (0);
659 }
660 
661 
662 /*****************************************************************************/
XSetFont(Display * dpy,GC gc,Font font)663 XSetFont(Display * dpy, GC gc, Font font)
664 {
665     if (xid[gc].type != XIDTYPE_HDC)
666 	return -1;
667     xid[gc].hgc.hfont = xid[font].font.font->hFont;
668     xid[gc].hgc.xgcv.font = font;
669     return (0);
670 }
671 
672 /*****************************************************************************/
XCreateSimpleWindow_(Display * dpy,Window parent,int x,int y,unsigned int width,unsigned int height,unsigned int border_width,unsigned long border,unsigned long background,const char * file,const int line)673 Window XCreateSimpleWindow_(Display * dpy, Window parent, int x, int y,
674 			    unsigned int width, unsigned int height,
675 			    unsigned int border_width,
676 			    unsigned long border, unsigned long background
677 #ifdef	_DEBUG
678 			    , const char *file, const int line
679 #endif
680     )
681 {
682     HWND hWnd;
683     XID txid;
684     int wstyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
685 
686     txid = GetFreeXid();
687     xid[txid].hwnd.type = XIDTYPE_HWND;
688     xid[txid].hwnd.hBmp = NULL;
689     xid[txid].hwnd.hBmpDC = NULL;
690     xid[txid].hwnd.hWnd = NULL;
691     xid[txid].hwnd.event_mask = ButtonPressMask | ButtonReleaseMask
692 	| LeaveWindowMask | EnterWindowMask;
693     xid[txid].hwnd.event_mask = -1;	// hell, let's take em all!
694     xid[txid].hwnd.mouseover = 0;	// mouse not over this window
695     xid[txid].hwnd.bgcolor = background;
696     xid[txid].hwnd.notmine = FALSE;
697     xid[txid].hwnd.drawtype = DT_1;
698 
699 #ifdef	_DEBUG
700     strncpy(xid[txid].any.file,
701 	    strrchr(file, '\\') + 1, WINXFILELENGTH - 1);
702     xid[txid].any.line = line;
703 #endif
704 
705     switch (border_width) {
706     case 1:
707 	wstyle =
708 	    WS_BORDER | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
709 	    WS_CLIPCHILDREN;
710 	break;
711     case 2:
712 	wstyle =
713 	    WS_DLGFRAME | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
714 	    WS_CLIPCHILDREN;
715 	break;
716     case 3:
717 	wstyle = WS_DLGFRAME | WS_POPUP | WS_VISIBLE;
718 	wstyle =
719 	    WS_DLGFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU;
720 	y += GetSystemMetrics(SM_CYCAPTION);
721 	break;
722     }
723     Trace("XCreateSimpleWindow_: %d p=%d, %d/%d %d/%d b_width=%d\n", txid,
724 	  parent, x, y, width, height, border_width);
725     hWnd = xid[txid].hwnd.hWnd =
726 	CreateWindow("XPilotWin", "", wstyle, x, y, width, height,
727 		     xid[parent].hwnd.hWnd, NULL, hInstance,
728 		     (LPVOID) txid);
729     if (!hWnd)
730 	error("Can't open window\n");
731     else {
732 	HDC hDC = GetDC(hWnd);
733 	SelectPalette(hDC, myPal, FALSE);
734 	RealizePalette(hDC);
735 	SetBkMode(hDC, OPAQUE);
736 	ReleaseDC(hWnd, hDC);
737     }
738 #if 0
739     if (++max_xid > MAX_XIDS) {
740 	error("Too many XIDS!\n");
741 	max_xid--;
742     }
743     return (max_xid - 1);
744 #endif
745     return (txid);
746 }
747 
748 /*****************************************************************************/
XCreateWindow_(Display * dpy,Window parent,int x,int y,unsigned int width,unsigned int height,unsigned int border_width,int depth,unsigned int c_class,Visual * visual,unsigned long valuemask,XSetWindowAttributes * attributes,const char * file,const int line)749 Window XCreateWindow_(Display * dpy, Window parent, int x, int y,
750 		      unsigned int width, unsigned int height,
751 		      unsigned int border_width, int depth,
752 		      unsigned int c_class, Visual * visual,
753 		      unsigned long valuemask,
754 		      XSetWindowAttributes * attributes
755 #ifdef	_DEBUG
756 		      , const char *file, const int line
757 #endif
758     )
759 {
760     Window w = XCreateSimpleWindow_(dpy, parent, x, y, width, height,
761 				    border_width, 0, 0
762 #ifdef	_DEBUG
763 				    , file, line
764 #endif
765 	);
766     xid[w].hwnd.event_mask = attributes->event_mask;
767     return (w);
768 }
769 
WinXCreateWinDC_(Window w,const char * file,const int line)770 GC WinXCreateWinDC_(Window w
771 #ifdef	_DEBUG
772 		    , const char *file, const int line
773 #endif
774     )
775 {
776     XID txid;
777     txid = GetFreeXid();
778     xid[txid].hgc.type = XIDTYPE_HDC;
779     xid[txid].hgc.xidhwnd = w;
780 
781 #ifdef	_DEBUG
782     strncpy(xid[txid].any.file,
783 	    strrchr(file, '\\') + 1, WINXFILELENGTH - 1);
784     xid[txid].any.line = line;
785 #endif
786 
787 #if 0
788     if (++max_xid > MAX_XIDS) {
789 	error("Too many XIDS!\n");
790 	max_xid--;
791     }
792     return (max_xid - 1);
793 #endif
794     return (txid);
795 }
796 
797 /*****************************************************************************/
XSetForeground(Display * dpy,GC gc,unsigned long foreground)798 XSetForeground(Display * dpy, GC gc, unsigned long foreground)
799 {
800 #if 0
801 #ifdef PENS_OF_PLENTY
802     if (xid[gc].type == XIDTYPE_PIXMAP) {
803 	HDC hDC = xid[gc].hpix.hDC;
804 	SetTextColor(hDC, WinXPColour(foreground));
805 	SelectObject(hDC, objs[foreground].brush);
806 	SelectObject(hDC, objs[foreground].pen);
807     } else {
808 	int xidno = xid[gc].hgc.xidhwnd;
809 	xid[xidno].hwnd.cur_color = foreground;
810 	WinXSetPen(xidno);
811     }
812 #else
813     HDC hDC;
814     Window w;
815     cur_color = foreground;
816     if (xid[gc].type == XIDTYPE_PIXMAP) {
817 	hDC = xid[gc].hpix.hDC;
818 	SetTextColor(hDC, WinXPColour(cur_color));
819 	SelectObject(hDC, objs[cur_color].pen);
820 	SelectObject(hDC, objs[cur_color].brush);
821     } else {
822 	w = xid[gc].hgc.xidhwnd;
823 	if (xid[w].hwnd.drawtype == DT_1) {
824 	    hDC = xid[w].hwnd.hBmpDC;
825 	    SetTextColor(hDC, WinXPColour(cur_color));
826 	    SelectObject(hDC, objs[cur_color].pen);
827 	    SelectObject(hDC, objs[cur_color].brush);
828 	} else {		/* DT_2 */
829 
830 	    SetTextColor(xid[w].hwnd.hBmpDCa[0], WinXPColour(cur_color));
831 	    SelectObject(xid[w].hwnd.hBmpDCa[0], objs[cur_color].pen);
832 	    SelectObject(xid[w].hwnd.hBmpDCa[0], objs[cur_color].brush);
833 	    SetTextColor(xid[w].hwnd.hBmpDCa[1], WinXPColour(cur_color));
834 	    SelectObject(xid[w].hwnd.hBmpDCa[1], objs[cur_color].pen);
835 	    SelectObject(xid[w].hwnd.hBmpDCa[1], objs[cur_color].brush);
836 	}
837     }
838 #endif
839 #endif
840     //if (xid[gc].type == XIDTYPE_HDC) {
841 
842     xid[gc].hgc.xgcv.foreground = foreground;
843     WinXSelectPen(gc);
844     WinXSelectBrush(gc);
845 /*	} else {
846 		// ouch, terrible hack, but fixing this properly would be difficult
847 		HDC hDC;
848 		extern HPEN pens[256][10][3];
849 		extern HBRUSH brushes[256];
850 		hDC = xid[gc].hpix.hDC;
851 		SelectObject(hDC, pens[foreground][0][0]);
852 		SelectObject(hDC, brushes[foreground]);
853 	}
854 */
855     return (0);
856 }
857 
858 
859 /*****************************************************************************/
XDestroyWindow(Display * dpy,Window w)860 XDestroyWindow(Display * dpy, Window w)
861 {
862     WinXFree(w);
863     return (0);
864 }
865 
866 /*****************************************************************************/
XUnmapWindow(Display * dpy,Window w)867 XUnmapWindow(Display * dpy, Window w)
868 {
869     ShowWindow(xid[w].hwnd.hWnd, SW_HIDE);
870     return (0);
871 }
872 
873 /*****************************************************************************/
XMapWindow(Display * dpy,Window w)874 XMapWindow(Display * dpy, Window w)
875 {
876     //ShowWindow(xid[w].hwnd.hWnd, SW_SHOW);
877     XMapRaised(dpy, w);
878     return (0);
879 }
880 
881 /*****************************************************************************/
XMapSubwindows(Display * dpy,Window w)882 XMapSubwindows(Display * dpy, Window w)
883 {
884     ShowWindow(xid[w].hwnd.hWnd, SW_SHOW);
885     return (0);
886 }
887 
888 /*****************************************************************************/
XMapRaised(Display * dpy,Window w)889 XMapRaised(Display * dpy, Window w)
890 {
891     ShowWindow(xid[w].hwnd.hWnd, SW_SHOW);
892     //SetWindowPos(xid[w].hwnd.hWnd, HWND_TOPMOST,
893     //      0, 0, 0, 0,
894     //      SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
895     BringWindowToTop(xid[w].hwnd.hWnd);
896     return (0);
897 }
898 
899 /*****************************************************************************/
XMoveWindow(Display * dpy,Window w,int x,int y)900 XMoveWindow(Display * dpy, Window w, int x, int y)
901 {
902     RECT rect;
903     GetClientRect(xid[w].hwnd.hWnd, &rect);
904     MoveWindow(xid[w].hwnd.hWnd, x, y, rect.right, rect.bottom, TRUE);
905     return (0);
906 }
907 
908 /*****************************************************************************/
XMoveResizeWindow(Display * dpy,Window w,int x,int y,unsigned int width,unsigned int height)909 XMoveResizeWindow(Display * dpy, Window w, int x, int y,
910 		  unsigned int width, unsigned int height)
911 {
912     HWND hWnd = xid[w].hwnd.hWnd;
913 
914     SetWindowPos(hWnd, NULL, x, y, width, height, SWP_NOZORDER);
915     return (0);
916 }
917 
918 /*****************************************************************************/
XSetDashes(Display * dpy,GC gc,int dash_offset,const char * dash_list,int n)919 XSetDashes(Display * dpy, GC gc, int dash_offset,
920 	   const char *dash_list, int n)
921 {
922     if (xid[gc].type != XIDTYPE_HDC)
923 	return -1;
924     xid[gc].hgc.xgcv.dash_offset = dash_offset;
925     xid[gc].hgc.xgcv.dashes = dash_list[0];
926 
927     xid[gc].hgc.xgcv.line_style = LineOnOffDash;
928 
929     WinXSelectPen(gc);
930     return (0);
931 }
932 
933 /*****************************************************************************/
XSelectInput(Display * dpy,Window w,long event_mask)934 XSelectInput(Display * dpy, Window w, long event_mask)
935 {
936     xid[w].hwnd.event_mask =
937 	event_mask | ButtonPressMask | ButtonReleaseMask;
938     return (0);
939 }
940 
941 
942 /*****************************************************************************/
DefaultRootWindow(Display * dpy)943 Window DefaultRootWindow(Display * dpy)
944 {
945 /*	return(top); */
946     return (0);
947 }
948 
949 /*****************************************************************************/
DisplayWidth(Display * dpy,int screen_number)950 int DisplayWidth(Display * dpy, int screen_number)
951 {
952     RECT rect;
953     GetWindowRect(GetDesktopWindow(), &rect);
954     GetWindowRect(xid[top].hwnd.hWnd, &rect);
955     return (rect.right);
956 }
957 
958 /*****************************************************************************/
DisplayHeight(Display * dpy,int screen_number)959 int DisplayHeight(Display * dpy, int screen_number)
960 {
961     RECT rect;
962     GetWindowRect(GetDesktopWindow(), &rect);
963     GetWindowRect(xid[top].hwnd.hWnd, &rect);
964     return (rect.bottom);
965 }
966 
967 /*****************************************************************************/
XCheckIfEvent(Display * dpy,XEvent * event_return,Bool (* predicate)(),XPointer arg)968 Bool XCheckIfEvent(Display * dpy, XEvent * event_return,
969 		   Bool(*predicate) (), XPointer arg)
970 {
971     return (FALSE);
972 }
973 
974 /*****************************************************************************/
XSetIconName(Display * dpy,Window w,const char * icon_name)975 XSetIconName(Display * dpy, Window w, const char *icon_name)
976 {
977     return (0);
978 }
979 
980 /*****************************************************************************/
XSetTransientForHint(Display * dpy,Window w,Window prop_window)981 XSetTransientForHint(Display * dpy, Window w, Window prop_window)
982 {
983     return (0);
984 }
985 
986 /*****************************************************************************/
XChangeWindowAttributes(Display * dpy,Window w,unsigned long valuemask,XSetWindowAttributes * attributes)987 XChangeWindowAttributes(Display * dpy, Window w, unsigned long valuemask,
988 			XSetWindowAttributes * attributes)
989 {
990     return (0);
991 }
992 
993 /*****************************************************************************/
XSetWindowBackground(Display * dpy,Window w,unsigned long background_pixel)994 XSetWindowBackground(Display * dpy, Window w,
995 		     unsigned long background_pixel)
996 {				/* TODO */
997     return (0);
998 }
999 
1000 /*****************************************************************************/
XGetWindowAttributes(Display * dpy,Window w,XWindowAttributes * attributes)1001 XGetWindowAttributes(Display * dpy, Window w,
1002 		     XWindowAttributes * attributes)
1003 {
1004     RECT rect;
1005     GetClientRect(xid[w].hwnd.hWnd, &rect);
1006     attributes->x = attributes->y = 0;
1007     attributes->width = rect.right;
1008     attributes->height = rect.bottom;
1009     return (0);
1010 }
1011 
1012 /*****************************************************************************/
XGContextFromGC(GC gc)1013 GContext XGContextFromGC(GC gc)
1014 {
1015     return (gc);
1016 }
1017 
1018 /*****************************************************************************/
XGrabPointer(Display * display,Window w,Bool owner_events,unsigned int event_mask,int pointer_mode,int keyboard_mode,Window confine_to,Cursor cursor,Time time)1019 int XGrabPointer(Display * display, Window w, Bool owner_events,
1020 		 unsigned int event_mask, int pointer_mode,
1021 		 int keyboard_mode, Window confine_to, Cursor cursor,
1022 		 Time time)
1023 {
1024     SetCapture(xid[w].hwnd.hWnd);
1025     xid[w].hwnd.event_mask = event_mask;	/* this could be a macro, */
1026 //      WinXSetEventMask(w, event_mask);
1027     return (0);
1028 }
1029 
1030 /*****************************************************************************/
XUngrabPointer(Display * display,Time time)1031 XUngrabPointer(Display * display, Time time)
1032 {
1033     ReleaseCapture();
1034     return (0);
1035 }
1036 
1037 /*****************************************************************************/
XWarpPointer(Display * display,Window src_w,Window dest_w,int src_x,int src_y,unsigned int src_width,unsigned int src_height,int dest_x,int dest_y)1038 XWarpPointer(Display * display, Window src_w, Window dest_w,
1039 	     int src_x, int src_y,
1040 	     unsigned int src_width, unsigned int src_height,
1041 	     int dest_x, int dest_y)
1042 {
1043 #ifdef _WINDOWS
1044     SetCursorPos(dest_x, dest_y);
1045 #else
1046     RECT rect;
1047     GetWindowRect(xid[dest_w].hwnd.hWnd, &rect);
1048     SetCursorPos(rect.left + dest_x, rect.top + dest_y);
1049 #endif
1050     return (0);
1051 }
1052 
1053 /*****************************************************************************/
XDefineCursor(Display * d,Window w,Cursor c)1054 XDefineCursor(Display * d, Window w, Cursor c)
1055 {
1056     if (c == None)
1057 	ShowCursor(TRUE);
1058     else
1059 	ShowCursor(FALSE);
1060     return (0);
1061 }
1062 
1063 /*****************************************************************************/
DefaultDepth(Display * d,int screen)1064 int DefaultDepth(Display * d, int screen)
1065 {
1066     return (8);			// assume 256 colors (bad assumption but OK for now)
1067 }
1068 
1069 /*****************************************************************************/
XSync(Display * display,Bool discard)1070 int XSync(Display * display, Bool discard)
1071 {
1072     return 0;
1073 }
1074