home *** CD-ROM | disk | FTP | other *** search
- /* plotX11.c - all the X11 routines ***/
-
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/Xos.h>
- #include <X11/keysym.h>
- #include <stdio.h>
- #include <strings.h>
- #include <math.h>
-
- #include "common.h"
- #include "contour.h"
- #include "plot.h"
- #include "Xdefs.h"
- #include "Xdefs.h"
- #include "iconX11.ic"
-
- /* colors */
- #define DEFAULT_BORDER_COLOR "Blue"
- #define DEFAULT_BACK_COLOR "MidNightBlue"
- #define DEFAULT_FORE_COLOR "White"
- #define DEFAULT_MOUSE_COLOR "White"
- #define DEFAULT_ICON_COLOR "Red"
- #define BDR_C 0
- #define BACK_C 1
- #define FORE_C 2
- #define MOUSE_C 3
- #define ICON_C 4
- #define LINE1_C 5
- #define LINE2_C 6
- #define LINE3_C 7
- #define LINE4_C 8
- #define LINE5_C 9
- #define MAX_COLORS 10
-
- unsigned long border_pixel; /* color of border */
- unsigned long background_pixel; /* color of background */
- unsigned long foreground_pixel; /* color of foreground */
- unsigned long mouseground_pixel; /* mouse cursor color */
- unsigned long icon_pixel; /* icon color */
- unsigned long colors[MAX_COLORS]; /* color pixel values */
- char *clr_name[MAX_COLORS]; /* color names */
-
- /* line-types */
- # define DOTTED 2
- # define SHORT_DOTTED 2
- # define LONG_DOTTED 2
- # define DASHED 2
- # define DOT_DASHED 4
- # define DASH_DOTTED 4
- # define SHORT_DASHED 2
- # define LONG_DASHED 2
- # define EQUAL_DASHED 2
- # define ODD_DASHED 3
- static unsigned char dash_list_dotted[DOTTED] = {1,6};
- static unsigned char dash_list_short_dotted[SHORT_DOTTED] = {1,3};
- static unsigned char dash_list_long_dotted[LONG_DOTTED] = {1,8};
- static unsigned char dash_list_dashed[DASHED] = {4,4};
- static unsigned char dash_list_dot_dashed[DOT_DASHED] = {3,4,3,1};
- static unsigned char dash_list_dash_dotted[DASH_DOTTED] = {4,3,1,3};
- static unsigned char dash_list_short_dashed[SHORT_DASHED] = {3,5};
- static unsigned char dash_list_equal_dashed[EQUAL_DASHED] = {6,6};
- static unsigned char dash_list_long_dashed[LONG_DASHED] = {7,4};
- static unsigned char dash_list_odd_dashed[ODD_DASHED] = {1,2,3};
-
- /* plot information */
- #define X_ORG 5
- #define Y_ORG 5
- #define X_DIM 800
- #define Y_DIM 700
- #define DEFAULT_BDR_DIM 100
-
- #define X_MKR 4
- #define X_LIN 11
-
- int Xxmin, Xymin, Xxmax, Xymax;
-
- /* Misc */
- #define DEFAULT_BORDER_WIDTH 3
- #define DEFAULT_FONT "8x13"
- #define SMALL_FONT "6x10"
-
- short gray_bits[16] = {
- 0xaaaa, 0x5555, 0xaaaa, 0x5555,
- 0xaaaa, 0x5555, 0xaaaa, 0x5555,
- 0xaaaa, 0x5555, 0xaaaa, 0x5555,
- 0xaaaa, 0x5555, 0xaaaa, 0x5555};
-
- Display *display; /* display */
- int screen; /* screen */
- Window window; /* parent window */
- XFontStruct *font_info; /* font information */
- XFontStruct *lblfont_info; /* label font information */
- XSizeHints size_hints; /* window info for manager */
- GC gc, gcl; /* graphics context */
-
- InitWindow(argc,argv)
- int argc;
- char **argv;
- {
- Pixmap icon_pixmap;
- XGCValues gcvalues;
- char *sprintf(), default_geometry[100];
- int x, y, width, height;
-
- /* Open display first, and get screen info */
- if (!(display = XOpenDisplay(display_name))) {
- fprintf(stderr,"%s:Could not open display %s\n",
- progname,display_name);
- exit(1);
- }
- screen = DefaultScreen(display);
- sprintf(default_geometry,"=%dx%d+%d+%d",X_DIM,Y_DIM,X_ORG,Y_ORG);
-
- /* Merge the options first */
- Merge_Options();
-
- /* allocate the colors */
- Alloc_Colors();
-
- /* Initialize the window */
- if (!XGeometry(display,screen,geometry,default_geometry,border_width,
- 0,0,0,0,&x,&y,&width,&height)) {
- x = X_ORG;
- y = Y_ORG;
- width = X_DIM;
- height = Y_DIM;
- }
- window = XCreateSimpleWindow(display, RootWindow(display,screen),
- x, y, width, height, border_width,
- border_pixel, background_pixel);
-
- /* Initialize size hint property for window manager */
- size_hints.flags = PPosition | PSize | PMinSize;
- size_hints.x = x;
- size_hints.y = y;
- size_hints.width = width;
- size_hints.height = height;
- size_hints.min_width = X_DIM;
- size_hints.min_height = Y_DIM;
-
- /* Create pixmap of depth 1 (bitmap) for icon */
- icon_pixmap = XCreateBitmapFromData(display, window, icon_bitmap_bits,
- icon_bitmap_width, icon_bitmap_height);
-
- /* set properties for window manager */
- XSetStandardProperties(display,window, "Contour Program", "Contour",
- icon_pixmap, argv, argc, &size_hints);
-
- /* Select event types wanted */
- XSelectInput(display,window,ExposureMask | ButtonPressMask |
- KeyPressMask | StructureNotifyMask );
-
- /* Create GC for text and drawing */
- gc = XCreateGC(display, window, 0, &gcvalues);
- gcl= XCreateGC(display, window, 0, &gcvalues);
-
- /* Find a font and load it */
- if (!(font_info = XLoadQueryFont(display,font_name))){
- fprintf(stderr,"%s:Could not open font %s\n",progname,font_name);
- exit(1);
- }
- XSetFont(display, gc, font_info->fid);
- if (!(lblfont_info = XLoadQueryFont(display,SMALL_FONT))){
- fprintf(stderr,"%s:Could not open font %s\n",progname,SMALL_FONT);
- exit(1);
- }
- XSetFont(display, gcl, lblfont_info->fid);
-
- /* specify foreground */
- XSetForeground(display, gc, foreground_pixel);
-
- /* Display the window */
- XMapWindow(display, window);
- }
-
- /* Initialize and merge the various options */
- Merge_Options()
- {
- char *option;
-
- /* process the various options */
- if (reverse < 0) {
- if ((option = XGetDefault(display,progname,"ReverseVideo")) != NULL)
- if (strcmp(option,"on")) reverse = 1;
- }
- if (!geometry) {
- option = XGetDefault(display,progname,"Geometry");
- geometry = option ? option : "=+5+5";
- }
- if (!font_name) {
- /* option = XGetDefault(display,progname,"BodyFont"); */
- font_name = DEFAULT_FONT;
- }
- if (!border_color) {
- option = XGetDefault(display,progname,"Border");
- border_color = option ? option : DEFAULT_BORDER_COLOR;
- }
- if (!back_color) {
- option = XGetDefault(display,progname,"Background");
- back_color = option ? option : DEFAULT_BACK_COLOR;
- }
- if (!fore_color) {
- option = XGetDefault(display,progname,"Foreground");
- fore_color = option ? option : DEFAULT_FORE_COLOR;
- }
- if (!mouse_color) {
- option = XGetDefault(display,progname,"Mouse");
- mouse_color = option ? option : DEFAULT_MOUSE_COLOR;
- }
- if (!icon_color) {
- option = XGetDefault(display,progname,"Icon");
- icon_color = option ? option : DEFAULT_ICON_COLOR;
- }
- if (border_width <= 0) {
- option = XGetDefault(display,progname,"BorderWidth");
- border_width = option ? atoi(option) : DEFAULT_BORDER_WIDTH;
- }
- }
-
- /* allocate the colors on color display */
- Alloc_Colors()
- {
- XColor cdef;
- Colormap cmap;
- int i, depth;
- unsigned long tmp_pixel;
-
- /* get info on depth and colormap */
- depth = DisplayPlanes(display,screen);
- cmap = DefaultColormap(display,screen);
-
- /* associate named colors with pixel values */
- if (depth == 1) { /* one-plane monochrome */
- colors[BDR_C] = BlackPixel(display,screen);
- colors[BACK_C] = BlackPixel(display,screen);
- colors[FORE_C] = WhitePixel(display,screen);
- colors[MOUSE_C] = colors[FORE_C];
- colors[ICON_C] = colors[FORE_C];
- for (i=LINE1_C; i<=LINE5_C; i++) colors[i] = colors[FORE_C];
- } else { /* color */
- clr_name[BDR_C] = border_color;
- clr_name[BACK_C] = back_color;
- clr_name[FORE_C] = fore_color;
- clr_name[MOUSE_C] = mouse_color;
- clr_name[ICON_C] = icon_color;
- clr_name[LINE1_C] = "yellow";
- clr_name[LINE2_C] = "cyan";
- clr_name[LINE3_C] = "green";
- clr_name[LINE4_C] = "red";
- clr_name[LINE5_C] = "blue";
- for (i=0; i<MAX_COLORS; i++)
- if (XAllocNamedColor(display,cmap,clr_name[i],&cdef,&cdef))
- colors[i] = cdef.pixel;
- else {
- fprintf(stderr,"%s: Can't allocate color %s\n",
- progname, clr_name[i]);
- exit(1);
- } /* end if */
- } /* end if */
-
- /* assign pixel values */
- border_pixel = colors[BDR_C];
- background_pixel = colors[BACK_C];
- foreground_pixel = colors[FORE_C];
- mouseground_pixel = colors[MOUSE_C];
- icon_pixel = colors[ICON_C];
-
- /* reverse video */
- if (reverse) {
- tmp_pixel = background_pixel;
- background_pixel = foreground_pixel;
- foreground_pixel = tmp_pixel;
- if (border_pixel == background_pixel)
- border_pixel = foreground_pixel;
- if (mouseground_pixel == background_pixel)
- mouseground_pixel = foreground_pixel;
- for (i=LINE1_C; i<=LINE5_C; i++)
- if (colors[i]==background_pixel) colors[i] = foreground_pixel;
- }
- }
-
- RunOps()
- {
- # define SMALLW 1
- # define OK 0
- XEvent event;
- KeySym keysym;
- XComposeStatus compose;
- char buffer[1];
- int bufsize = 1;
- int window_size = OK;
- int Width, Height;
-
- /* Initialize window size */
- Width = X_DIM;
- Height = Y_DIM;
-
- /* event loop */
- while(1) {
- XNextEvent(display, &event);
- switch (event.type) {
- case Expose:
- /* get rid of all other Expose events on the queue */
- while (XCheckTypedEvent(display, Expose, &event));
- /* window was resized too small to use */
- if (window_size == SMALLW) {
- Width = X_DIM;
- Height = Y_DIM;
- }
- ShowView(Width, Height);
- break;
- case ConfigureNotify:
- /* window has been resized */
- /* change width and height */
- Width = event.xconfigure.width;
- Height = event.xconfigure.height;
- if ((Width < size_hints.min_width) ||
- (Height < size_hints.min_height) )
- window_size = SMALLW;
- else
- window_size = OK;
- break;
- case ButtonPress:
- XUnloadFont(display, font_info->fid);
- XUnloadFont(display, lblfont_info->fid);
- XFreeGC(display, gc);
- XFreeGC(display, gcl);
- XCloseDisplay(display);
- return;
- break;
- case KeyPress:
- XLookupString(&event,buffer,bufsize,&keysym,&compose);
- if ((keysym == XK_q) || (keysym == XK_Q)) {
- XUnloadFont(display, font_info->fid);
- XUnloadFont(display, lblfont_info->fid);
- XFreeGC(display, gc);
- XFreeGC(display, gcl);
- XCloseDisplay(display);
- return;
- }
- break;
- default:
- break;
- } /* end switch */
- } /* end while */
- }
-
- ShowView(Width, Height)
- int Width, Height;
- {
- axesX(Width, Height);
- plotX();
- }
-
- /* draw the axes */
- axesX(Width, Height)
- int Width, Height;
- {
- char *sprintf();
- extern char xlabel[MAXCHAR];
- extern char ylabel[MAXCHAR];
- extern char toplabel[MAXCHAR];
- extern int grid,equalscale;
- extern double xmin, xmax, ymin, ymax;
- extern int xticks,yticks;
-
- int Xwid, Ywid;
- double xdx, ydy, vallbl, ratio;
- int xintv, yintv, xtmp, ytmp;
- char text[100], c;
- int text_len, text_width, text_xpos, text_ypos;
- int font_height, font_width;
- int i;
-
- /* Initialize */
- xdx = (xmax-xmin)/xticks;
- ydy = (ymax-ymin)/yticks;
-
- Xxmin = DEFAULT_BDR_DIM;
- Xymin = DEFAULT_BDR_DIM;
- Xwid = Width - 2*DEFAULT_BDR_DIM;
- Ywid = Height - 2*DEFAULT_BDR_DIM;
- Xxmax = Xxmin + Xwid;
- Xymax = Xymin + Ywid;
- if (equalscale == ON) {
- /* max Xwid = 400, max Ywid = 300 */
- ratio = (ymax-ymin)/(xmax-xmin);
- if (ratio < ((double)Ywid)/((double)Xwid))
- Ywid = ratio*Xwid;
- else
- Xwid = Ywid/ratio;
- Xxmax = Xxmin + Xwid;
- Xymax = Xymin + Ywid;
- }
- xintv = (Xxmax-Xxmin)/xticks;
- yintv = (Xymax-Xymin)/yticks;
-
- /* Draw the main axes */
- XSetLineAttributes(display,gc,1,LineSolid,CapButt,JoinBevel);
- XDrawRectangle(display,window,gc,Xxmin,Xymin,Xwid,Ywid);
-
- /* Labels */
- font_height = font_info->max_bounds.ascent +
- font_info->max_bounds.descent;
- font_width = font_info->max_bounds.rbearing -
- font_info->max_bounds.lbearing;
-
- /* Top Label */
- text_len = strlen(toplabel);
- text_width = XTextWidth(font_info,toplabel,text_len);
- text_xpos = (Xxmin + Xxmax - text_width)/2;
- text_ypos = Xymin - 3*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,toplabel,text_len);
-
- /* X-Axis Label */
- text_len = strlen(xlabel);
- text_width = XTextWidth(font_info,xlabel,text_len);
- text_xpos = (Xxmin + Xxmax - text_width)/2;
- text_ypos = Xymax + 4*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,xlabel,text_len);
-
- /* Y-Axis Label */
- /* Don't know how to rotate, so print character by character */
- text_len = strlen(ylabel);
- text_xpos = Xxmin - 12*font_width;
- text_ypos = (Xymin + Xymax)/2 - text_len*font_height;
- for (i=0; (c=ylabel[i]) != '\0'; i++) {
- text[0] = c;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,1);
- text_ypos += 2*font_height;
- }
-
- /* side labels */
- sprintf(text,"%s","Minimum");
- text_len = strlen(text);
- text_width= XTextWidth(font_info,text,text_len);
- text_xpos = Xxmax + 40 - text_width/2;
- text_ypos = (Xymin + Xymax)/2 - 6*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,text_len);
- sprintf(text,"%.2f",zmin);
- text_len = strlen(text);
- text_width= XTextWidth(font_info,text,text_len);
- text_xpos = Xxmax + 40 - text_width/2;
- text_ypos = (Xymin + Xymax)/2 - 4*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,text_len);
-
- /* side labels */
- sprintf(text,"%s","Maximum");
- text_len = strlen(text);
- text_width= XTextWidth(font_info,text,text_len);
- text_xpos = Xxmax + 40 - text_width/2;
- text_ypos = (Xymin + Xymax)/2 + 4*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,text_len);
- sprintf(text,"%.2f",zmax);
- text_len = strlen(text);
- text_width= XTextWidth(font_info,text,text_len);
- text_xpos = Xxmax + 40 - text_width/2;
- text_ypos = (Xymin + Xymax)/2 + 6*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,text_len);
-
- /* Draw the tick marks and labels */
- for (i=0; i<=xticks; i++) {
- xtmp = Xxmin + i*(Xxmax-Xxmin)/xticks;
- XDrawLine(display,window,gc,xtmp,Xymin,xtmp,Xymin+10);
- XDrawLine(display,window,gc,xtmp,Xymax,xtmp,Xymax-10);
- vallbl = xmin +i*xdx;
- sprintf(text,"%6.2f",vallbl);
- text_len = strlen(text);
- if (text_len > 8) {
- sprintf(text,"%6.2e",vallbl);
- text_len = strlen(text);
- }
- text_width = XTextWidth(font_info,text,text_len);
- text_xpos = xtmp - text_width/2;
- text_ypos = Xymax + 2*font_height;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,text_len);
- }
-
- for (i=0; i<=yticks; i++) {
- ytmp = Xymin + i*(Xymax-Xymin)/yticks;
- XDrawLine(display,window,gc,Xxmin,ytmp,Xxmin+10,ytmp);
- XDrawLine(display,window,gc,Xxmax,ytmp,Xxmax-10,ytmp);
- vallbl = ymax -i*ydy;
- sprintf(text,"%6.2f",vallbl);
- text_len = strlen(text);
- if (text_len > 8) {
- sprintf(text,"%6.2e",vallbl);
- text_len = strlen(text);
- }
- text_width = XTextWidth(font_info,text,text_len);
- text_xpos = Xxmin - text_width - font_width;
- text_ypos = ytmp + font_height/2;
- XDrawString(display,window,gc,text_xpos,text_ypos,text,text_len);
- }
-
- /* Work on the grid */
- if (grid == ON) {
- XSetLineAttributes(display,gc,0,LineOnOffDash,CapButt,JoinBevel);
- XSetDashes(display,gc,0,dash_list_long_dotted,LONG_DOTTED);
- for (i=1; i<xticks; i++) {
- xtmp = Xxmin + xintv*i;
- XDrawLine(display,window,gc,xtmp,Xymin+10,xtmp,Xymax-10);
- }
- for (i=1; i<yticks; i++) {
- ytmp = Xymin + yintv*i;
- XDrawLine(display,window,gc,Xxmin+10,ytmp,Xxmax-10,ytmp);
- }
- XSetLineAttributes(display,gc,0,LineSolid,CapButt,JoinBevel);
- }
- }
-
- /* draw the plot */
- plotX()
- {
- char *sprintf();
- extern int linetypes, contlabel;
- extern double xmin, xmax, ymin, ymax;
- extern plotptr plot_listhead;
-
- XPoint points[MAXPTS];
- nodeptr N;
- plotptr P;
- double idx,jdy,oldlevel;
- char text[100];
- int text_len, text_width, text_xpos, text_ypos, font_height;
- int drawlbl, npts, j, kl=1;
-
- /* initialize */
- font_height = lblfont_info->max_bounds.ascent +
- lblfont_info->max_bounds.descent;
- idx = (Xxmax-Xxmin)/(xmax-xmin);
- jdy = (Xymax-Xymin)/(ymin-ymax); /* because of strange x format */
-
- /* plot */
- if (plot_listhead!=NULL) oldlevel = plot_listhead->level;
- for (P=plot_listhead; P!=NULL; P=P->next) {
- j = 0;
- /* rescale points, save in array */
- for (N=P->nodehead; N!=NULL && j<MAXPTS; N=N->next) {
- points[j].x = (int)((N->pt.x-xmin)*idx) + Xxmin;
- points[j].y = (int)((N->pt.y-ymax)*jdy) + Xymin;
- j++;
- }
- npts = j;
-
- if (j>0) {
- /* plot the points */
- if (oldlevel != P->level) kl++;
- drawlbl = linetypX(kl);
- linecolX(kl);
- XDrawLines(display,window,gc,points,npts,CoordModeOrigin);
- XSetForeground(display,gc,foreground_pixel);
-
- /* label the contours */
- if (contlabel && drawlbl){
- sprintf(text,"%.2f",P->level);
- text_len = strlen(text);
- text_width = XTextWidth(lblfont_info,text,text_len);
- text_xpos = points[(int)(0.5*j)].x - text_width/2;
- text_ypos = points[(int)(0.5*j)].y - font_height/2;
- XDrawString(display,window,gcl,text_xpos,text_ypos,text,text_len);
- }
- }
- oldlevel = P->level;
- }
- }
-
- /* set the line type */
- linetypX(linetyp)
- int linetyp;
- {
- int drawlbl = OFF;
-
- if (linetypes <= 2) linetyp = linetyp % linetypes;
- else if (linetypes == 3) linetyp = linetyp % 4;
-
- switch (linetyp) {
- case 0 : XSetLineAttributes(display,gc,0,LineSolid,CapButt,JoinBevel);
- drawlbl = ON;
- break;
- case 1 : XSetLineAttributes(display,gc,0,LineOnOffDash,CapButt,JoinBevel);
- XSetDashes(display,gc,0,dash_list_dotted,DOTTED);
- break;
- case 2 : XSetLineAttributes(display,gc,0,LineOnOffDash,CapButt,JoinBevel);
- XSetDashes(display,gc,0,dash_list_dashed,DASHED);
- drawlbl = ON;
- break;
- case 3 : XSetLineAttributes(display,gc,0,LineOnOffDash,CapButt,JoinBevel);
- XSetDashes(display,gc,0,dash_list_dotted,DOTTED);
- break;
- default: XSetLineAttributes(display,gc,0,LineSolid,CapButt,JoinBevel);
- break;
- }
- return(drawlbl);
- }
-
- /* choose a linecolor */
- linecolX(linetyp)
- int linetyp;
- {
- extern int linetypes;
-
- if (linetypes <= 2) linetyp = linetyp % linetypes;
- else if (linetypes == 3) linetyp = linetyp % 4;
- switch (linetyp) {
- case 0 : XSetForeground(display,gc,colors[LINE1_C]); break;
- case 1 : XSetForeground(display,gc,colors[LINE2_C]); break;
- case 2 : XSetForeground(display,gc,colors[LINE3_C]); break;
- case 3 : XSetForeground(display,gc,colors[LINE2_C]); break;
- default: XSetForeground(display,gc,colors[LINE1_C]); break;
- }
- }
-