more fix on Ec/Ev.
[gss-tcad.git] / src / graphic / xgraph.cpp
blobba7bbb56903975208c13d08ef91ae73c43aaffbc
1 /*-----------------------------------------------------------------------------
3 FILE: xgraph.cpp
5 PROJECT:
7 AUTHOR: Kevin M. Kramer
8 J. Joshua Feng
10 REVISION HISTORY
11 DATE INITIALS DESCRIPTION
12 -------- -------- --------------------------------------------------------
13 12/15/93 KMK initial implementation
14 02/04/97 JJF Modification (Adding LUT functions)
15 12/12/05 gdiso Support rainbow color
16 DESCRIPTION:
18 This module contains functions implement a small subset of the Borland BGI
19 functions under X windows. Hence this module should not be included when
20 compiling under Borland C++ enviroments.
22 -----------------------------------------------------------------------------*/
23 #include "config.h"
24 #ifdef HAVE_X11
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xos.h>
29 #include <X11/keysym.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
35 #include "xgraph.h"
37 typedef unsigned char BYTE; /* b */
38 typedef unsigned char UCHAR; /* uch */
39 typedef int BOOL; /* f */
40 typedef int BOOLEAN; /* f */
41 typedef unsigned int WORD; /* w */
42 typedef unsigned int UINT; /* u */
43 typedef unsigned long DWORD; /* dw */
44 typedef char * PSTR; /* psz */
45 typedef int * PINT; /* pn */
46 typedef int * PBOOL; /* pf */
47 typedef unsigned int * PWORD; /* pw */
48 typedef long * PLONG; /* pl */
49 typedef unsigned long * PDWORD; /* pdw */
50 typedef void * PVOID; /* pv */
51 typedef double REAL; /* */
52 typedef double * PREAL; /* */
55 static int nScreen;
56 static int cDisplayWidth;
57 static int cDisplayHeight;
58 static int cXresolution;
59 static int cYresolution;
60 static int nXorigin;
61 static int nYorigin;
62 static int cColorDepth;
63 static int cMaxColor;
64 /* static int cStyle; */
65 static int nFillColor;
66 static int nCurrentColor;
67 static int cArg;
68 static int cTextWidth;
69 static int cTextHeight;
70 static UINT cBorderWidth;
71 static DWORD dwForeground;
72 static DWORD dwBackground;
73 static char *pszDisplayName;
74 static char **apszArg;
75 static GC **gc;
76 static Display *display;
77 static Window window;
78 static Visual *visual;
79 static XFontStruct *xfont;
80 static Colormap colormap;
81 static Pixmap pixmapIcon;
82 static XSizeHints sizehints;
83 static XPoint axpoint[128]; /* used by poly filled */
84 static XEvent xevent;
86 static FILE *filePS = NULL;
88 #define ICON_BITMAP_WIDTH 40
89 #define ICON_BITMAP_HEIGHT 40
90 static char szIconBitmap[] = /* SGFramework Icon */
91 { 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00,
96 0xff, 0xff, 0xff, 0xff, 0xff,
97 0xff, 0xff, 0xff, 0xff, 0xff,
98 0x00, 0x00, 0xff, 0x00, 0x00,
99 0x00, 0x00, 0xff, 0x00, 0x00,
100 0x00, 0x00, 0xff, 0x00, 0x00,
101 0x00, 0x00, 0xff, 0x00, 0x00,
102 0x00, 0x00, 0xff, 0x00, 0x00,
103 0x00, 0x00, 0xff, 0x00, 0x00,
104 0x00, 0x00, 0xff, 0x00, 0x00,
105 0x00, 0x00, 0xff, 0x00, 0x00,
106 0x00, 0x00, 0xff, 0x00, 0x00,
107 0x00, 0x00, 0xff, 0x00, 0x00,
108 0x00, 0x00, 0xff, 0x00, 0x00,
109 0x00, 0x00, 0xff, 0x00, 0x00,
110 0x00, 0x00, 0xff, 0x00, 0x00,
111 0x00, 0x00, 0xff, 0x00, 0x00,
112 0x00, 0x00, 0xff, 0x00, 0x00,
113 0x00, 0x00, 0xff, 0x00, 0x00,
114 0x00, 0x00, 0xff, 0x00, 0x00,
115 0x00, 0x00, 0xff, 0x00, 0x00,
116 0x00, 0x00, 0xff, 0x00, 0x00,
117 0x00, 0x00, 0xff, 0x00, 0x00,
118 0x00, 0x00, 0xff, 0x00, 0x00,
119 0x00, 0x00, 0xff, 0x00, 0x00,
120 0x00, 0x00, 0xff, 0x00, 0x00,
121 0x00, 0x00, 0xff, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00, 0x00
133 static unsigned int rc[][3] =
135 0, 0, 65535,
136 0, 29298, 65535,
137 0, 41377, 65535,
138 0, 50629, 65535,
139 0, 58596, 65535,
140 0, 65535, 65535,
141 0, 65535, 56797,
142 0, 65535, 46003,
143 0, 65535, 32639,
144 0, 65535, 0,
145 32125, 65535, 0,
146 46260, 65535, 0,
147 56540, 65535, 0,
148 65535, 65535, 0,
149 65535, 59881, 0,
150 65535, 53199, 0,
151 65535, 45746, 0,
152 65535, 38036, 0,
153 65535, 26471, 0,
154 65535, 0, 0
157 void GetGC (GC *, char *);
158 void GetGCRainbow (GC *, unsigned short, unsigned short, unsigned short);
159 /*****************************************************************************/
160 /************************ POSTSCRIPT FUNCTIONS *****************************/
161 /*****************************************************************************/
164 /******************************************************************************
165 * Description: This function opens the postscipt file.
167 int GRPrintOpen ( const char *pszName, int nResFactor )
169 char szFileName[128];
170 int i;
172 sprintf(szFileName, "%s", pszName);
173 filePS = fopen(szFileName, "wt");
174 if(!filePS) return 1;
176 fprintf(filePS,
177 "%%!PS-Adobe-1.0\n"
178 "%%%%Creator: SGFramework\n"
179 "%%%%Title: %s\n"
180 "%%%%Pages: (atend)\n"
181 "%%%%BoundingBox: 72 162 540 640 \n"
182 "%%%%EndComments\n\n", pszName);
183 fprintf(filePS,
184 "%%! SGFramework prolog Version 1.1\n"
185 "%%%%\n"
186 "\n"
187 "/setlc {/linecolor exch def} def\n"
188 "/setfc {/fillcolor exch def} def\n"
189 "\n"
190 "/colors [\n");
192 fprintf(filePS,
193 " [1.0 1.0 1.0] %% white\n"
194 " [0.0 0.0 1.0] %% blue\n"
195 " [0.0 1.0 0.0] %% green\n"
196 " [0.0 1.0 1.0] %% cyan\n"
197 " [1.0 0.0 0.0] %% red\n"
198 " [1.0 1.0 0.2] %% magenta\n"
199 " [0.5 0.4 0.3] %% brown\n"
200 " [0.7 0.7 0.7] %% light gray\n"
201 " [0.3 0.3 0.3] %% dark gray\n"
202 " [0.2 0.2 1.0] %% light blue\n"
203 " [0.2 1.0 0.2] %% light green\n"
204 " [0.5 1.0 1.0] %% light cyan\n"
205 " [1.0 0.2 0.2] %% light red\n"
206 " [1.0 1.0 0.5] %% light magenta\n"
207 " [1.0 1.0 0.0] %% yellow\n"
208 " [0.0 0.0 0.0] %% black\n");
209 fprintf(filePS,
210 " [0.000000 0.000000 1.000000]\n"
211 " [0.000000 0.447059 1.000000]\n"
212 " [0.000000 0.631373 1.000000]\n"
213 " [0.000000 0.772549 1.000000]\n"
214 " [0.000000 0.894118 1.000000]\n"
215 " [0.000000 1.000000 1.000000]\n"
216 " [0.000000 1.000000 0.866667]\n"
217 " [0.000000 1.000000 0.701961]\n"
218 " [0.000000 1.000000 0.498039]\n"
219 " [0.000000 1.000000 0.000000]\n"
220 " [0.490196 1.000000 0.000000]\n"
221 " [0.705882 1.000000 0.000000]\n"
222 " [0.862745 1.000000 0.000000]\n"
223 " [1.000000 1.000000 0.000000]\n"
224 " [1.000000 0.913725 0.000000]\n"
225 " [1.000000 0.811765 0.000000]\n"
226 " [1.000000 0.698039 0.000000]\n"
227 " [1.000000 0.580392 0.000000]\n"
228 " [1.000000 0.403922 0.000000]\n"
229 " [1.000000 0.000000 0.000000]\n" );
231 fprintf(filePS,
232 " [0.0 0.0 0.0] %% black\n"
233 "] def\n"
234 "\n"
235 "/usecolor {colors exch get aload pop setrgbcolor} def\n"
236 "\n"
237 "/drawpoly\n"
238 "{ 2 sub /points exch def \n"
239 " newpath moveto lineto \n"
240 " { points 0 ne {lineto /points points 1 sub def} {exit} ifelse\n"
241 " } loop\n"
242 " linecolor usecolor stroke\n"
243 "} def\n"
244 "\n"
245 "/fillpoly\n"
246 "{ 2 sub /points exch def\n"
247 " newpath moveto lineto\n"
248 " { points 0 ne {lineto /points points 1 sub def} {exit} ifelse\n"
249 " } loop\n"
250 " closepath gsave fillcolor usecolor fill grestore\n"
251 " linecolor usecolor stroke\n"
252 "} def\n"
253 "\n"
254 "/line {newpath moveto lineto linecolor usecolor stroke} def\n"
255 "\n"
256 "/circle\n"
257 "{ /r exch def \n"
258 " /y exch def\n"
259 " /x exch def\n"
260 " newpath\n"
261 " x r add y moveto\n"
262 " x y r 0 360 arc\n"
263 " linecolor usecolor stroke\n"
264 "} def\n"
265 "\n"
266 "/putpixel {0.1 circle} def\n"
267 "\n"
268 "/outtextxy\n"
269 "{ 3 1 roll moveto\n"
270 " gsave 1 -1 scale\n"
271 " linecolor usecolor\n"
272 " show grestore\n"
273 "} def\n"
274 "\n"
275 "/initialize\n");
277 fprintf(filePS, "{ /Helvetica findfont %d scalefont setfont\n", cTextHeight * nResFactor);
278 fprintf(filePS, " %f -%f scale\n", 1./nResFactor, 1./nResFactor);
279 fprintf(filePS, " 1.0 setlinewidth\n");
280 fprintf(filePS, " 0 -%d translate\n", 792 * nResFactor);
281 fprintf(filePS,
282 " 15 setlc\n"
283 " 0 setfc\n"
284 "} def\n"
285 "\n"
286 "%%%%EndProlog\n"
287 "\n"
288 "initialize\n");
290 cTextWidth *= nResFactor;
291 cTextHeight *= nResFactor;
292 return 0;
296 /******************************************************************************
297 * Description: This function closes the postscipt file.
299 void GRPrintClose ( )
301 fprintf(filePS, "showpage\n");
302 fclose(filePS);
303 filePS = NULL;
306 /*****************************************************************************/
307 /************************** PUBLIC FUNCTIONS *******************************/
308 /*****************************************************************************/
311 /******************************************************************************
312 * Description: This function initializes the cArg and apszArg variables.
314 void setcmdargs ( int c, char **apsz )
316 cArg = c;
317 apszArg = apsz;
321 /******************************************************************************
322 * Description: This function initializes the graphics system
324 void GRInitGraphics ( int TextWidth, int TextHeight )
326 /* initialize variables */
327 /* Font can be "7x14", "9x15", .... */
328 char szFontName[8]; /* font name */
329 sprintf(szFontName, "%dx%d", TextWidth, TextHeight);
330 pszDisplayName = NULL;
331 cBorderWidth = 4;
333 /* ------------------ connect to X server ------------------------ */
334 if ((display = XOpenDisplay(pszDisplayName)) == NULL)
336 fprintf(stdout, "ERROR: cannot connect to X server %s\n",
337 XDisplayName(pszDisplayName));
338 exit(2);
341 #if defined(DEBUG)
342 fprintf(stdout, "connected to X server\n");
343 #endif
345 /* fetch the default screen handle as well as the screen dimensions */
346 nScreen = DefaultScreen(display);
347 cDisplayWidth = DisplayWidth(display, nScreen);
348 cDisplayHeight = DisplayHeight(display, nScreen);
350 #if defined(DEBUG)
351 fprintf(stdout, "screen handle = %d\n", nScreen);
352 fprintf(stdout, "display width = %d\n", cDisplayWidth);
353 fprintf(stdout, "display height = %d\n", cDisplayHeight);
354 #endif
356 /* load the font */
357 if ((xfont = XLoadQueryFont(display, szFontName)) == NULL)
359 fprintf(stdout, "ERROR: cannot open %s font\n", szFontName);
360 exit(2);
362 cTextWidth = TextWidth;
364 #if defined(DEBUG)
365 fprintf(stdout, "loaded %s font\n", szFontName);
366 #endif
370 /******************************************************************************
371 * Description: This function sets the current viewport for graphics output.
373 void GROpenGraphWin ( char *pszWindowTitle, char *pszIconTitle,
374 int left, int top, int right, int bottom,
375 PFREDRAW pf )
377 int i; /* loop index */
378 char ColorName[8];
380 /* Origin is at top left corner */
381 nXorigin = left;
382 nYorigin = top;
383 cXresolution = right - left;
384 cYresolution = bottom - top;
386 /* ---------------------- create window ------------------------- */
387 dwForeground = WhitePixel(display, nScreen);
388 dwBackground = BlackPixel(display, nScreen);
389 window = XCreateSimpleWindow( display, RootWindow(display, nScreen),
390 nXorigin, nYorigin, cXresolution, cYresolution, cBorderWidth,
391 dwForeground, dwBackground);
393 #if defined(DEBUG)
394 fprintf(stdout, "graphics screen coordinates (%d,%d) to (%d,%d)\n",
395 left, top, right, bottom);
396 #endif
398 /* load window colors */
399 cColorDepth = DisplayPlanes(display, nScreen);
400 visual = DefaultVisual(display, nScreen);
402 /* Macro returns the default colormap ID */
403 colormap = DefaultColormap(display, nScreen);
405 #if defined(DEBUG)
406 fprintf(stdout, "display visual = %d\n", visual);
407 fprintf(stdout, "display planes = %d\n", cColorDepth);
408 #endif
410 /* -------------- Set LUT (Look Up Table) for colors -------------- */
411 /* monochrome screen : Hardware doesn't support colors */
412 if (cColorDepth == 1)
414 cMaxColor = 1;
415 gc = (GC **) malloc(2 * sizeof(GC *));
416 for(i = 0; i < 2; ++i) gc[i] = (GC *) malloc(sizeof(GC));
417 GetGC(gc[0], "black"); /* black */
418 GetGC(gc[1], "white"); /* white */
421 /* color screen : Hardware can show different sytles */
422 else
423 switch (cMaxColor)
425 case 99: gc = (GC **) malloc(100 * sizeof(GC *));
426 for(i = 0; i < 100; ++i)
428 gc[i] = (GC *) malloc(sizeof(GC));
429 sprintf(ColorName, "Gray%d", i);
430 GetGC(gc[i], ColorName); /* grey level */
432 break;
433 default: cMaxColor = 15;
434 gc = (GC **) malloc((16+Rainbow_Color_NUM) * sizeof(GC *));
435 for(i = 0; i < 16+Rainbow_Color_NUM; ++i)
436 gc[i] = (GC *) malloc(sizeof(GC));
437 GetGC(gc[0], "black"); /* black */
438 GetGC(gc[1], "blue"); /* blue */
439 GetGC(gc[2], "green"); /* green */
440 GetGC(gc[3], "cyan"); /* cyan */
441 GetGC(gc[4], "red"); /* red */
442 GetGC(gc[5], "magenta"); /* magenta */
443 GetGC(gc[6], "brown"); /* brown */
444 GetGC(gc[7], "gray"); /* light gray */
445 GetGC(gc[8], "dark slate gray"); /* dark gray */
446 GetGC(gc[9], "light blue"); /* light blue */
447 GetGC(gc[10], "lime green"); /* light green */
448 GetGC(gc[11], "slate blue"); /* light cyan */
449 GetGC(gc[12], "medium violet red"); /* light red */
450 GetGC(gc[13], "orange red"); /* light magenta */
451 GetGC(gc[14], "yellow"); /* yellow */
452 GetGC(gc[15], "white"); /* white */
453 for(i = 0; i <Rainbow_Color_NUM; ++i)
454 GetGCRainbow (gc[15+i+1], rc[i][0],rc[i][1],rc[i][2]);
456 break;
459 nFillColor = cMaxColor;
460 nCurrentColor = cMaxColor;
462 #if defined(DEBUG)
463 fprintf(stdout, "loaded colors, maximum number of colors = %d\n", cMaxColor);
464 #endif
466 /* create an icon */
467 pixmapIcon = XCreateBitmapFromData(display, window, szIconBitmap,
468 ICON_BITMAP_WIDTH, ICON_BITMAP_HEIGHT);
470 #if defined(DEBUG)
471 fprintf(stdout, "created pixel map\n");
472 #endif
474 /* ----------------- initialize window properties --------------------- */
475 sizehints.flags = USPosition | USSize | PPosition | PSize | PMinSize;
476 sizehints.x = nXorigin;
477 sizehints.y = nYorigin;
478 sizehints.width = cXresolution;
479 sizehints.height = cYresolution;
480 sizehints.min_width = cXresolution;
481 sizehints.min_height = cYresolution;
482 XSetStandardProperties(display, window, pszWindowTitle, pszIconTitle,
483 pixmapIcon, apszArg, cArg, &sizehints);
484 XSelectInput(display, window, ExposureMask | KeyPressMask | EnterWindowMask |
485 ButtonPressMask | Button1MotionMask | StructureNotifyMask);
486 XMapWindow(display, window);
487 cTextHeight = xfont->ascent + xfont->descent;
489 #if defined(DEBUG)
490 fprintf(stdout, "window name = %s\n", pszWindowTitle);
491 fprintf(stdout, "icon name = %s\n", pszIconTitle);
492 fprintf(stdout, "number of command line arguments = %d\n", cArg);
493 #endif
497 /******************************************************************************
498 * Description: This function clears the current viewport
500 void GRClearGraphWin ( void )
502 XClearWindow(display, window);
506 /******************************************************************************
507 * Description: This function shuts down the graphics system
509 void GRFreeGraphics ( void )
511 int i; /* loop index */
513 /* unload the font */
514 //XUnloadFont(display, xfont->fid);
515 XFreeFont(display, xfont);
516 /* unload the colors */
517 for(i = 0; i <= cMaxColor; ++i)
519 XFreeGC(display, *gc[i]);
520 free(gc[i]);
522 if(cMaxColor==15)
523 for(i = 0; i < Rainbow_Color_NUM; ++i)
525 XFreeGC(display, *gc[cMaxColor+1+i]);
526 free(gc[cMaxColor+1+i]);
528 free(gc);
529 /* close the display */
530 XCloseDisplay(display);
534 /******************************************************************************
535 * Description: This function sends all queued requests to the server.
537 void flushdisplay ( void )
539 XFlush(display);
541 #if defined(DEBUG)
542 fprintf(stdout, "sent all queued requests to the server\n");
543 #endif
547 /******************************************************************************
548 * Description: This function returns an event.
550 int getevent ( short int *keycode, int *width, int *height, int *x, int *y)
552 char buffer[10];
553 KeySym keysym;
554 XComposeStatus compose;
555 int count;
557 /* get the event from the X server */
558 XWindowEvent(display, window, ButtonPressMask | KeyPressMask | ExposureMask | StructureNotifyMask |
559 EnterWindowMask | LeaveWindowMask | Button1MotionMask, &xevent);
561 switch (xevent.type)
563 case Expose:
564 while (XCheckTypedEvent(display, Expose, &xevent));
565 return (XCheckWindowEvent(display, window, ButtonPress, &xevent)) ?
566 BUTTONPRESS : EXPOSE;
568 case EnterNotify:
569 case SelectionNotify:
570 XRaiseWindow(display, window);
571 XSetInputFocus(display, window, RevertToNone, CurrentTime);
572 break;
574 case LeaveNotify:
575 break;
577 case ConfigureNotify:
578 *width = xevent.xconfigure.width;
579 *height = xevent.xconfigure.height;
580 return(CONFIGURENOTIFY);
582 case KeyPress:
583 count = XLookupString(&xevent.xkey, buffer, sizeof(buffer),
584 &keysym, &compose);
585 buffer[count] = 0;
586 *keycode = (char) 0;
589 if (keysym == XK_Left) *keycode = LEFTARROW;
590 else if (keysym == XK_Right) *keycode = RIGHTARROW;
591 else if (keysym == XK_Up) *keycode = UPARROW;
592 else if (keysym == XK_Down) *keycode = DOWNARROW;
593 else if (keysym == XK_Escape) *keycode = ESCAPE;
594 else if (keysym == XK_BackSpace) *keycode = BACKSPACE;
595 else if ((keysym >= XK_space) &&
596 (keysym <= XK_asciitilde)) *keycode = buffer[0];
597 else *keycode = xevent.xkey.keycode;
599 while (*keycode == 0);
600 return KEYPRESS;
602 case ButtonPress:
603 *x=xevent.xbutton.x;
604 *y=xevent.xbutton.y;
605 return BUTTONPRESS;
606 case MotionNotify:
607 *x=xevent.xmotion.x;
608 *y=xevent.xmotion.y;
609 return BUTTONMOTION;
610 case ButtonRelease:
611 *x=xevent.xbutton.x;
612 *y=xevent.xbutton.y;
613 return BUTTONRELEASE;
615 return OTHEREVENT;
619 /******************************************************************************
620 * Description: This function returns the maximum x screen coordinate
622 int GRGetMaxX ( void )
624 return (cXresolution <= 0) ? cDisplayWidth : cXresolution;
628 /******************************************************************************
629 * Description: This function returns the maximum y screen coordinate
631 int GRGetMaxY ( void )
633 return (cYresolution <= 0) ? cDisplayHeight : cYresolution;
637 /******************************************************************************
638 * Description: This function sets the current drawing color.
640 void GRSetColor ( int color )
642 nCurrentColor = color;
643 if (filePS) fprintf(filePS, "%d setlc\n", color);
647 /******************************************************************************
648 * Description: This function sets the fill pattern and color.
650 void GRSetFillColor ( int color, int pattern )
652 /* X window fill options are FillSolid, FillTiled, */
653 /* FillStippled and FillOpaqueStipplied */
655 nFillColor = color;
657 if (filePS) fprintf(filePS, "%d setfc\n", color);
658 else
660 switch (pattern)
662 case PS_SOLID:
663 XSetFillStyle(display, *gc[color], FillSolid);
664 break;
666 default:
667 XSetFillStyle(display, *gc[color], FillTiled);
668 break;
674 /******************************************************************************
675 * Description: This function plots a pixel at a specified point.
677 void GRPutPixel ( int x, int y, int color )
679 if (filePS) fprintf(filePS, "%d %d putpixel\n", x, y);
680 else XDrawPoint(display, window, *gc[color], x, y);
684 /******************************************************************************
685 * Description: This function draws a line between two specified points.
687 void GRLine ( int x1, int y1, int x2, int y2 )
689 if (filePS) fprintf(filePS, "%d %d %d %d line\n", x1, y1, x2, y2);
690 else XDrawLine(display, window, *gc[nCurrentColor], x1, y1, x2, y2);
694 /******************************************************************************
695 * Description: This function draws a circle of the given radius with its
696 * center at (x,y).
698 void GRCircle ( int x, int y, int r )
700 if (filePS) fprintf(filePS, "%d %d %d circle\n", x, y, r);
701 else XDrawArc(display, window, *gc[nCurrentColor], x - r, y - r,
702 2 * r, 2 * r, 0, 23040);
706 /******************************************************************************
707 * Description: This function draws the outline of a polygon.
709 void GRDrawPoly ( int numpoints, int *polypoints )
711 int i; /* loop index */
713 if (filePS)
715 for (i = 0; i < numpoints; ++i)
716 fprintf(filePS, "%d %d ", polypoints[2*i], polypoints[2*i+1]);
717 fprintf(filePS, "%d drawpoly\n", numpoints);
719 else
721 for (i = 0; i < numpoints - 1; ++i)
722 GRLine(polypoints[2*i] , polypoints[2*i+1],
723 polypoints[2*i+2], polypoints[2*i+3]);
728 /******************************************************************************
729 * Description: This function draws and fills a polygon.
731 void GRFillPoly ( int numpoints, int *polypoints )
733 XPoint *pxpoint; /* pointer to x point structure */
734 int i,j; /* loop indices */
737 if (filePS)
739 for (i = 0; i< numpoints; ++i)
740 fprintf(filePS, "%d %d ", polypoints[2*i], polypoints[2*i+1]);
741 fprintf(filePS, "%d fillpoly\n", numpoints);
743 else
745 for (i = 0, j = 0, pxpoint = axpoint; i < numpoints; ++i, ++pxpoint)
747 pxpoint->x = polypoints[j++];
748 pxpoint->y = polypoints[j++];
751 /* connect end pooints, if not */
752 if ((axpoint[0].x != axpoint[numpoints-1].x) ||
753 (axpoint[0].y != axpoint[numpoints-1].y))
755 axpoint[numpoints].x = polypoints[0];
756 axpoint[numpoints].y = polypoints[1];
757 ++numpoints;
760 /* x fill mode may be complex, nonconvex or convex */
761 XFillPolygon(display, window, *gc[nFillColor], axpoint, numpoints - 1,
762 Nonconvex, CoordModeOrigin);
763 XDrawLines(display, window, *gc[nCurrentColor], axpoint, numpoints,
764 CoordModeOrigin);
769 /******************************************************************************
770 * Description: This function displays a string at a specified location.
772 void GRText ( int x, int y, char *textstring )
774 if (filePS) fprintf(filePS, "%d %d (%s) outtextxy\n", x, y + cTextHeight, textstring);
775 else XDrawString(display, window, *gc[nCurrentColor], x, y + cTextHeight,
776 textstring, strlen(textstring));
778 #if defined(DEBUG)
779 fprintf(stdout, "displayed \"%s\" at (%d,%d)\n", textstring, x, y);
780 #endif
784 /******************************************************************************
785 * Description: This function setup the color LUT (Look Up Table) style.
786 * And return the maxmum color value.
788 int GRSetLUT ( int monitorstyle )
790 if (monitorstyle) cMaxColor = 99; /* graylevel */
791 else cMaxColor = 15; /* color */
793 return cMaxColor;
798 /*****************************************************************************/
799 /************************** PRIVATE FUNCTIONS ******************************/
800 /*****************************************************************************/
803 /******************************************************************************
804 * Description: This function allocates a color.
806 void GetGC ( GC *gc, char *pszColorName )
808 DWORD dwValueMask;
809 DWORD dwForeground;
810 XGCValues values;
811 XColor xcolorDef;
812 XColor xcolorRGB;
814 XAllocNamedColor(display, colormap, pszColorName, &xcolorDef, &xcolorRGB);
815 dwForeground = xcolorDef.pixel;
816 dwValueMask = GCForeground | GCBackground;
817 values.foreground = dwForeground;
818 values.background = dwBackground;
819 *gc = XCreateGC(display, window, dwValueMask, &values);
820 XSetFont(display, *gc, xfont->fid);
823 /******************************************************************************
824 * Description: This function allocates a rainbow color.
826 void GetGCRainbow (GC *gc, unsigned short R, unsigned short G,unsigned short B)
828 DWORD dwValueMask;
829 DWORD dwForeground;
830 XGCValues values;
831 XColor xcolorDef;
832 XColor xcolorRGB;
833 xcolorDef.red = R;
834 xcolorDef.green = G;
835 xcolorDef.blue = B;
836 xcolorDef.flags = 0;
837 XAllocColor(display, colormap, &xcolorDef);
838 dwForeground = xcolorDef.pixel;
839 dwValueMask = GCForeground | GCBackground;
840 values.foreground = dwForeground;
841 values.background = dwBackground;
842 *gc = XCreateGC(display, window, dwValueMask, &values);
843 XSetFont(display, *gc, xfont->fid);
848 /*****************************************************************************/
849 /************************** DUMP SCREEN FUNCTIONS **************************/
850 /*****************************************************************************/
851 #ifdef HAVE_TIFF
852 #include <tiffio.h>
853 int GRSaveScreen (const char * tiff_file, int width, int height)
856 TIFF *fout= TIFFOpen(tiff_file, "w");
857 if(!fout) return 1;
859 int sampleperpixel = 3; //RGB channel
860 XImage *ximage = XGetImage(display, window, 0, 0, width, height, AllPlanes, XYPixmap);
861 char *image=new char [width*height*sampleperpixel];
863 //it seems the XQueryColors has the limit of query size of 65535 colors.
864 XColor *color=new XColor[65535];
865 int lines=65535/width;
866 int currentlines;
867 int count = 0;
869 for (int i = 0; i < height; i+=lines)
871 if(i+lines>=height) currentlines=height-i-1;
872 else currentlines=lines;
873 for (int j = 0; j < currentlines; j++)
874 for (int k = 0; k < width; k++)
876 unsigned long c;
877 c = XGetPixel(ximage, k, i+j); // X pixel value
878 color[k+j*width].pixel = c;
880 XQueryColors(display, colormap, color, currentlines*width);
881 for (int j = 0; j < currentlines; j++)
882 for (int k = 0; k < width; k++)
884 char r, g, b;
885 r = color[k+j*width].red >> 8;
886 g = color[k+j*width].green >> 8;
887 b = color[k+j*width].blue >> 8;
889 image[count++] = r;
890 image[count++] = g;
891 image[count++] = b;
894 delete [] color;
896 TIFFSetField(fout, TIFFTAG_IMAGEWIDTH, width); // set the width of the image
897 TIFFSetField(fout, TIFFTAG_IMAGELENGTH, height); // set the height of the image
898 TIFFSetField(fout, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel); // set number of channels per pixel
899 TIFFSetField(fout, TIFFTAG_BITSPERSAMPLE, 8); // set the size of the channels
900 TIFFSetField(fout, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); // set the origin of the image.
901 TIFFSetField(fout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
902 TIFFSetField(fout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
903 TIFFSetField(fout, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
905 tsize_t linebytes = sampleperpixel*width; // length in memory of one row of pixel in the image.
906 // We set the strip size of the file to be size of one row of pixels
907 TIFFSetField(fout, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(fout, width*sampleperpixel));
908 //Now writing image to the file one strip at a time
909 for (int row = 0; row < height; row++)
911 // check the index here, and figure out why not using h*linebytes
912 if(TIFFWriteScanline(fout, &image[row*linebytes], row, 0) < 0)
913 break;
915 TIFFClose(fout);
916 delete [] image;
917 XDestroyImage(ximage);
919 return 0;
921 #else
922 int GRSaveScreen (const char * tiff_file, int width, int height)
924 return 0;
926 #endif
928 #endif