xwin.h 12.7 KB
Newer Older
1 2 3
/*
 * @(#)xwin.h
 *
4
 * Copyright 2005 - 2017  David A. Bagley, bagleyd AT verizon.net
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * This program is distributed in the hope that it will be "useful",
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
20

21
/* Header file for xwin */
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#ifndef _xwin_h
#define _xwin_h

#define NUM_DEGREES 360

#ifdef WINVER
#define STRICT

#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

38
#if ((WINVER > 0x030a) && !defined(GCL_HBRBACKGROUND) && !defined(GCLP_HBRBACKGROUND))
39 40 41 42 43 44 45 46 47
#undef WINVER
#define WINVER 0x030a
#endif
#if (WINVER <= 0x030a)		/* if WINDOWS 3.1 or less */
#define Position int
#define SETBACK(h,b) (void)SetClassWord(h,GCW_HBRBACKGROUND,(WORD)b)
#define MoveTO(h,x,y,lp) MoveTo(h,x,y)
#else
#define Position long
48 49 50
#ifdef GCLP_HBRBACKGROUND
#define SETBACK(h,b) (void)SetClassLongA(h,GCLP_HBRBACKGROUND,(LONG)(intptr_t)b)
#else
51
#define SETBACK(h,b) (void)SetClassLong(h,GCL_HBRBACKGROUND,(LONG)b)
52
#endif
53 54 55 56 57 58 59 60 61 62 63 64 65 66
#define MoveTO(h,x,y,lp) MoveToEx(h,x,y,lp)
#endif
#ifndef Point
#define Point POINT
#endif
#ifndef Boolean
#define Boolean BOOL
#endif
#ifndef True
#define True TRUE
#endif
#ifndef False
#define False FALSE
#endif
67 68 69
#ifndef None
#define None NULL
#endif
70 71 72
#ifndef XtPointer
#define XtPointer void *
#endif
73 74
#define GC COLORREF  /* Equate apples to oranges */
#define Pixmap HBITMAP
75
#define FULL_CIRCLE NUM_DEGREES
76

Jonathan Carter's avatar
Jonathan Carter committed
77 78
#undef RGB
#define RGB(r,g,b) ((COLORREF)(((DWORD)(r)|((DWORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
79 80 81 82 83 84 85 86 87
#define FLUSH(w) ;
#define DC(w,dr) ((dr==0)?w->core.hDC:w->core.memDC)
#define LINE(w,x1,y1,x2,y2) (void)MoveTO(w,x1,y1,NULL); \
  (void)LineTo(w,x2,y2)
#define DRAWLINE(w,dr,c,x1,y1,x2,y2) \
  if (dr) w->core.hOldBitmap = \
  (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID, 1, c); \
  w->core.hOldPen = (HPEN) SelectObject(DC(w,dr), w->core.hPen); \
88
  SetPixel(DC(w,dr),x2,y2,c); \
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  LINE(DC(w,dr),x1,y1,x2,y2); \
  (void) SelectObject(DC(w,dr), w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define RECTANGLE(w,x,y,l,h) (void)Rectangle(w,x,y,x+l,y+h)
#define DRAWRECTANGLE(w,dr,c,x,y,l,h) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID,1,c); \
  w->core.hOldPen = (HPEN) SelectObject(DC(w,dr),w->core.hPen); \
  RECTANGLE(DC(w,dr),x,y,(l)+1,(h)+1); \
  (void) SelectObject(DC(w,dr),w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define FILLRECTANGLE(w,dr,c,x,y,l,h) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID,1,c); \
  w->core.hOldPen = (HPEN) SelectObject(DC(w,dr),w->core.hPen); \
  w->core.hBrush = CreateSolidBrush(c); \
  w->core.hOldBrush = (HBRUSH) SelectObject(DC(w,dr),w->core.hBrush); \
108
  if (l==1&&h==1) SetPixel(DC(w,dr),x,y,c); \
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  RECTANGLE(DC(w,dr),x,y,l,h); \
  (void) SelectObject(DC(w,dr),w->core.hOldBrush); \
  (void) DeleteObject(w->core.hBrush); \
  (void) SelectObject(DC(w,dr),w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define FILLRECTANGLE2(w,dr,c,cl,x,y,l,h) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID,1,cl); \
  w->core.hOldPen = (HPEN) SelectObject(w->core.hDC, w->core.hPen); \
  w->core.hBrush = CreateSolidBrush(c); \
  w->core.hOldBrush = (HBRUSH) SelectObject(w->core.hDC, w->core.hBrush); \
  RECTANGLE(DC(w,dr),x,y,(l)+1,(h)+1); \
  (void) SelectObject(w->core.hDC, w->core.hOldBrush); \
  (void) DeleteObject(w->core.hBrush); \
  (void) SelectObject(w->core.hDC, w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define CIRCLE(w,r,x,y) if(r>0) \
 (void)Ellipse(w,x-r+1,y-r+1,x+r,y+r)
#define DRAWCIRCLE(w,dr,c,d,x,y) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID,1,c); \
  w->core.hOldPen = (HPEN) SelectObject(DC(w,dr),w->core.hPen); \
  w->core.hBrush = CreateSolidBrush(c); \
  w->core.hOldBrush = (HBRUSH) SelectObject(DC(w,dr),w->core.hBrush); \
135
  CIRCLE(DC(w,dr),(((d+(d+1)%2)>>1)+1),(x),(y)); \
136 137 138 139 140 141 142 143 144 145 146
  (void) SelectObject(DC(w,dr),w->core.hOldBrush); \
  (void) DeleteObject(w->core.hBrush); \
  (void) SelectObject(DC(w,dr),w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define FILLCIRCLE(w,dr,c,d,x,y) \
  w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID,1,c); \
  w->core.hOldPen = (HPEN) SelectObject(DC(w,dr),w->core.hPen); \
  w->core.hBrush = CreateSolidBrush(c); \
  w->core.hOldBrush = (HBRUSH) SelectObject(DC(w,dr),w->core.hBrush); \
147
  CIRCLE(DC(w,dr),(((d+(d+1)%2)>>1)+1),(x),(y)); \
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  (void) SelectObject(DC(w,dr),w->core.hOldBrush); \
  (void) DeleteObject(w->core.hBrush); \
  (void) SelectObject(DC(w,dr),w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define ARC(w,x,y,l,h,a1,a2) if (a2 > 0) Arc(w,x,y,x+l,y+h, \
  (int) (cos(M_PI * (a1) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1) / 180) * (h) + y + ((h)>>1)), \
  (int) (cos(M_PI * (a1+a2) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1+a2) / 180) * (h) + y + ((h)>>1))); \
  else Arc(w,x,y,x+l,y+h, \
  (int) (cos(M_PI * (a1+a2) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1+a2) / 180) * (h) + y + ((h)>>1)), \
  (int) (cos(M_PI * (a1) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1) / 180) * (h) + y + ((h)>>1)))
#define DRAWARC(w,dr,c,t,x,y,l,h,a1,a2) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  w->core.hPen = CreatePen(PS_SOLID,t,c); \
  w->core.hOldPen = (HPEN) SelectObject(DC(w,dr),w->core.hPen); \
  ARC(DC(w,dr),x,y,l,h,a1,a2); \
  (void) SelectObject(DC(w,dr),w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define DRAWARC2(w,dr,c,t,x,y,l,h,a1,a2) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
173 174
  {LOGBRUSH lb; lb.lbStyle = BS_SOLID; lb.lbColor = c; lb.lbHatch = 0; \
  w->core.hPen = \
175 176 177 178 179
  ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT, (unsigned int) t, \
  &lb, 0, NULL); \
  w->core.hOldPen = (HPEN) SelectObject(w->core.hDC, w->core.hPen); \
  ARC(DC(w,dr),x,y,l,h,a1,a2); \
  (void) SelectObject(w->core.hDC, w->core.hOldPen); \
180
  (void) DeleteObject(w->core.hPen); } \
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define PIE(w,x,y,l,h,a1,a2) if (a2 > 0) Pie(w->core.hDC,x,y,x+l,y+h, \
  (int) (cos(M_PI * (a1) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1) / 180) * (h) + y + ((h)>>1)), \
  (int) (cos(M_PI * (a1+a2) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1+a2) / 180) * (h) + y + ((h)>>1))); \
  else Pie(w->core.hDC,x,y,x+l,y+h, \
  (int) (cos(M_PI * (a1+a2) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1+a2) / 180) * (h) + y + ((h)>>1)), \
  (int) (cos(M_PI * (a1) / 180) * (l) + x + ((l)>>1)), \
  (int) (-sin(M_PI * (a1) / 180) * (h) + y + ((h)>>1)))
#define DRAWPIE(w,c,x,y,l,h,a1,a2) w->core.hPen = CreatePen(PS_SOLID, 1, c); \
  w->core.hOldPen = (HPEN) SelectObject(w->core.hDC, w->core.hPen); \
  w->core.hBrush = CreateSolidBrush(c); \
  w->core.hOldBrush = (HBRUSH) SelectObject(w->core.hDC, w->core.hBrush); \
  PIE(w,x,y,l,h,a1,a2); \
  (void) SelectObject(w->core.hDC, w->core.hOldBrush); \
  (void) DeleteObject(w->core.hBrush); \
  (void) SelectObject(w->core.hDC, w->core.hOldPen); \
  (void) DeleteObject(w->core.hPen)
#define DRAWTEXT(w,dr,c,x,y,s,l) \
  if (dr) w->core.hOldBitmap = (HBITMAP) SelectObject(w->core.memDC,dr); \
  (void) SetTextColor(DC(w,dr), c); \
  (void) SetBkMode(DC(w,dr), TRANSPARENT); \
  (void) TextOut(DC(w,dr), (x)-3, (y)-11, (LPCSTR) s, l); \
  if (dr) (void) SelectObject(w->core.memDC,w->core.hOldBitmap)
#define POLYGON(w,dr,c,lc,l,n,v,o) \
  if (dr) (w)->core.hOldBitmap = (HBITMAP) SelectObject((w)->core.memDC,dr); \
  FILLPOLYGON(DC((w),dr),&((w)->core.hPen),&((w)->core.hOldPen), \
  &((w)->core.hBrush),&((w)->core.hOldBrush),c,lc, \
  l,n,o); \
  if (dr) (void) SelectObject((w)->core.memDC,(w)->core.hOldBitmap)
213
#define POLYGON2 POLYGON
214 215 216 217 218 219
#define POLYLINE(w,dr,c,l,n,o) \
  if (dr) (w)->core.hOldBitmap = (HBITMAP) SelectObject((w)->core.memDC,dr); \
  DRAWPOLYLINE(DC((w),dr),&((w)->core.hPen),&((w)->core.hOldPen), \
  c,l,n,o); \
  if (dr) (void) SelectObject((w)->core.memDC,(w)->core.hOldBitmap)

220 221 222
extern void FILLPOLYGON(HDC hDC, HPEN *hPen, HPEN *hOldPen,
	HBRUSH *hBrush, HBRUSH *hOldBrush,
	GC color, GC lineColor, const POINT *poly, int n,
223
	Boolean origin);
224 225
extern void DRAWPOLYLINE(HDC hDC, HPEN *hPen, HPEN *hOldPen,
	GC color, const POINT *poly, int n,
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	Boolean origin);

#define DISPLAY_INFO(s) (void) MessageBox(w->core.hWnd, s, "Info", MB_OK)
#if 0
#define DISPLAY_WARNING(s) (void) fprintf(stderr, s)
#else
#define DISPLAY_WARNING(s) (void) MessageBox(w->core.hWnd, (LPCSTR) s, \
  "Warning", MB_OK)
#endif
#define DISPLAY_ERROR(s) (void) fprintf(stderr, "%s\n", s); exit(1)

typedef struct _CorePart {
	HWND        hWnd;
	HDC         hDC;
	HINSTANCE   hInstance;
	Position    width, height;
	HDC         memDC;
	HPEN        hOldPen, hPen;
	HBRUSH      hOldBrush, hBrush;
	HBITMAP     hOldBitmap;
246
	HMENU       hMenu;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
} CorePart;

#else

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#ifdef VMS
#include <unixlib.h>
#else
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif
#if HAVE_FCNTL_H
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include <X11/IntrinsicP.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/CoreP.h>
#include <X11/Xos.h>

273 274 275 276 277
#if 1
#define VOID
#else
#define VOID (void)
#endif
278 279 280
#ifndef Point
#define Point XPoint
#endif
281 282 283
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
284
#define MULT 64
285 286
#define FULL_CIRCLE (NUM_DEGREES*MULT)
#define FLUSH(w) VOID XFlush(XtDisplay(w))
287
#define DR(w,dr) ((dr==0)?XtWindow(w):dr)
288
#define DRAWLINE(w,dr,c,x1,y1,x2,y2) VOID XDrawLine(XtDisplay(w),DR(w,dr),\
289
  c,x1,y1,x2,y2)
290
#define DRAWRECTANGLE(w,dr,c,i,j,l,h) VOID XDrawRectangle(XtDisplay(w),\
291
  DR(w,dr),c,i,j,l,h)
292
#define FILLRECTANGLE(w,dr,c,i,j,l,h) VOID XFillRectangle(XtDisplay(w),\
293
  DR(w,dr),c,i,j,l,h)
294
#define FILLRECTANGLE2(w,dr,c,cl,i,j,l,h) VOID XFillRectangle(XtDisplay(w),\
295
  DR(w,dr),c,i,j,l,h);\
296 297 298 299 300
  VOID XDrawRectangle(XtDisplay(w),DR(w,dr),cl,i,j,l,h)
#define DRAWCIRCLE(w,dr,c,d,x,y) if(d>0) VOID XDrawArc(XtDisplay(w),\
  DR(w,dr),c,x-((d)>>1),y-((d)>>1),d,d,0,FULL_CIRCLE)
#define FILLCIRCLE(w,dr,c,d,x,y) if(d>0) VOID XFillArc(XtDisplay(w),\
  DR(w,dr),c,x-((d)>>1),y-((d)>>1),d,d,0,FULL_CIRCLE)
301 302
#define DRAWARC(w,dr,c,t,x,y,l,h,a1,a2) \
  if(t>1)XSetLineAttributes(XtDisplay(w),c,t,LineSolid,CapNotLast,JoinRound);\
303
  VOID XDrawArc(XtDisplay(w),DR(w,dr),c,x,y,l,h,(a1)*MULT,(a2)*MULT);\
304
  if(t>1)XSetLineAttributes(XtDisplay(w),c,1,LineSolid,CapNotLast,JoinRound)
305
#define DRAWTEXT(w,dr,c,x,y,s,l) VOID XDrawString(XtDisplay(w),DR(w,dr),\
306
  c,x,y,s,l)
307 308 309 310 311
#define POLYGON(w,dr,c,lc,l,n,v,o) VOID XFillPolygon(XtDisplay(w),DR(w,dr),\
  c,l,n,(v)?Convex:Complex,(o)?CoordModeOrigin:CoordModePrevious); \
  VOID XDrawLines(XtDisplay(w),DR(w,dr),lc,\
  l,(n)+1,(o)?CoordModeOrigin:CoordModePrevious)
#define POLYGON2(w,dr,c,lc,l,n,v,o) VOID XFillPolygon(XtDisplay(w),\
312
  DR(w,dr),c,l,n,\
313 314 315 316 317
  (v) ? Convex:Complex, (o)?CoordModeOrigin:CoordModePrevious);\
  VOID XDrawLines(XtDisplay(w),DR(w,dr),lc,\
  l,n,(o)?CoordModeOrigin:CoordModePrevious)
#define POLYLINE(w,dr,c,l,n,o) VOID XDrawLines(XtDisplay(w),DR(w,dr),c,\
  l,n,(o)?CoordModeOrigin:CoordModePrevious)
318 319 320 321 322 323 324

#define DISPLAY_INFO(s) XtWarning(s)
#define DISPLAY_WARNING(s) XtWarning(s)
#define DISPLAY_ERROR(s) XtError(s)

#endif

325 326
extern void intCat(char **string, const char *var1, const int var2);
extern void stringCat(char **string, const char *var1, const char *var2);
Jonathan Carter's avatar
Jonathan Carter committed
327
extern void intCpy(char *string, const int var);
328 329 330 331 332 333 334

#define ABS(a) (((a)<0)?(-(a)):(a))
#define SIGN(a) (((a)<0)?(-1):1)
#define MIN(a,b) (((int)(a)<(int)(b))?(int)(a):(int)(b))
#define MAX(a,b) (((int)(a)>(int)(b))?(int)(a):(int)(b))

#endif /* _xwin_h */