home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume30 / gnuplot3 / patch02g / patch.2
Encoding:
Text File  |  1992-06-19  |  27.5 KB  |  825 lines

  1. diff -rc gnuplot/gnuplot_x11.c gnuplot3.2/gnuplot_x11.c
  2. *** gnuplot/gnuplot_x11.c    Mon Sep  9 13:13:19 1991
  3. --- gnuplot3.2/gnuplot_x11.c    Tue May  5 21:42:20 1992
  4. ***************
  5. *** 1,3 ****
  6. --- 1,7 ----
  7. + #ifndef lint
  8. + static char *RCSid = "$Id: gnuplot_x11.c,v 3.26 92/03/24 22:35:52 woo Exp Locker: woo $";
  9. + #endif
  10.   /*-----------------------------------------------------------------------------
  11.    *   gnuplot_x11 - X11 outboard terminal driver for gnuplot 3
  12.    *
  13. ***************
  14. *** 4,130 ****
  15.    *   Requires installation of companion inboard x11 driver in gnuplot/term.c
  16.    *
  17.    *   Acknowledgements: 
  18. !  *      Chris Peterson (MIT) - original Xlib gnuplot support (and Xaw examples)
  19. !  *      Dana Chee (Bellcore)  - mods to original support for gnuplot 2.0
  20. !  *      Arthur Smith (Cornell) - graphical-label-widget idea (xplot)
  21. !  *      Hendri Hondorp (University of Twente, The Netherlands) - Motif xgnuplot
  22.    *
  23.    *   This code is provided as is and with no warranties of any kind.
  24.    *       
  25. !  *   Ed Kubaitis - Computing Services Office -  University of Illinois, Urbana
  26.    *---------------------------------------------------------------------------*/
  27.    
  28.   #include <stdio.h>
  29.   #include <signal.h>
  30. - #include <X11/Intrinsic.h>
  31. - #include <X11/StringDefs.h>
  32. - #include <Label.h>          /* use -Idir for location on your system */
  33. - #ifdef MOTIF
  34. - #include <Xm.h>             /* use -Idir for location on your system */
  35. - #define LabelWC xmLabelWidgetClass
  36. - #define LabelBPM XmNbackgroundPixmap
  37. - #else
  38. - #define LabelWC labelWidgetClass
  39. - #define LabelBPM XtNbitmap
  40. - #endif
  41.   
  42. ! #define Color (D>1)
  43. ! #define Ncolors 11
  44.   unsigned long colors[Ncolors];
  45. - char color_keys[Ncolors][30] =   { "text", "border", "axis", 
  46. -    "line1", "line2", "line3", "line4", "line5", "line6", "line7", "line8" };
  47. - char color_values[Ncolors][30] = { "black", "black", "black", 
  48. -    "red",  "green", "blue",  "magenta", "cyan", "sienna", "orange", "coral" };
  49.   
  50.   char dashes[10][5] = { {0}, {1,6,0}, 
  51.      {0}, {4,2,0}, {1,3,0}, {4,4,0}, {1,5,0}, {4,4,4,1,0}, {4,2,0}, {1,3,0}
  52.      };
  53.   
  54. ! Widget w_top, w_label; Window win; Display *dpy;
  55. ! Pixmap pixmap;  GC gc = (GC)NULL;
  56. ! Dimension W = 640 , H = 450;  int D;
  57. ! Arg args[5];
  58. ! static void gnuplot(), resize();
  59.   
  60.   int cx=0, cy=0, vchar, nc = 0, ncalloc = 0;
  61.   double xscale, yscale;
  62. ! #define X(x) (Dimension) (x * xscale)
  63. ! #define Y(y) (Dimension) ((4095-y) * yscale)
  64.   enum JUSTIFY { LEFT, CENTRE, RIGHT } jmode;
  65.   #define Nbuf 1024
  66. ! char buf[Nbuf];
  67. ! String *commands = NULL;
  68.   
  69. ! typedef struct {       /* See "X Toolkit Intrinsics Programming Manual"      */
  70. !   XFontStruct *font;   /* Nye and O'Reilly, O'Reilly & Associates, pp. 80-85 */
  71. !   unsigned long fg;
  72. !   unsigned long bg;
  73. !   } RValues, *RVptr; 
  74. ! RValues rv;
  75.   
  76. - XtResource resources[] = {
  77. -    { XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  78. -      XtOffset(RVptr, font), XtRString, "fixed" },
  79. -    { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel), 
  80. -      XtOffset(RVptr, fg), XtRString, XtDefaultForeground },
  81. -    { XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel), 
  82. -      XtOffset(RVptr, bg), XtRString, XtDefaultBackground },
  83. -    };
  84.   
  85.   /*-----------------------------------------------------------------------------
  86. !  *   main program - fire up application and callbacks
  87.    *---------------------------------------------------------------------------*/
  88.   
  89.   main(argc, argv) int argc; char *argv[]; {
  90.   
  91. !    signal(SIGINT, SIG_IGN);
  92. ! #ifdef SIGTSTP
  93. !    signal(SIGTSTP, SIG_IGN);
  94. ! #endif
  95.   
  96. !    /* initialize application */
  97. !    w_top = XtInitialize("gnuplot", "Gnuplot", NULL, 0, &argc, argv);
  98. !    XtSetArg(args[0], XtNwidth, W);
  99. !    XtSetArg(args[1], XtNheight, H);
  100. !    w_label = XtCreateManagedWidget ("", LabelWC, w_top, args, (Cardinal)2);
  101. !    XtRealizeWidget(w_top);
  102.   
  103. !    /* extract needed information */
  104. !    dpy = XtDisplay(w_top); win = XtWindow(w_label);
  105. !    D = DisplayPlanes(dpy,DefaultScreen(dpy));
  106. !    if (Color) {
  107. !       char option[20], *value; 
  108. !       XColor used, exact; int n;
  109.   
  110. !       for(n=0; n<Ncolors; n++) {
  111. !      strcpy(option, color_keys[n]);
  112. !      strcat(option, "Color");
  113. !      value = XGetDefault(dpy, "gnuplot", option);
  114. !      if (!value) { value = color_values[n]; }
  115. !      if (XAllocNamedColor(dpy, DefaultColormap(dpy,0), value, &used,&exact))
  116. !         colors[n] = used.pixel; 
  117. !      else {
  118. !         fprintf(stderr, "gnuplot: cannot allocate %s:%s\n", option, value);
  119. !         fprintf(stderr, "gnuplot: assuming %s:black\n", option);
  120. !         colors[n] = BlackPixel(dpy,0);
  121.           }
  122.        }
  123.         }
  124. !    XtSetArg(args[0], XtNwidth, &W);
  125. !    XtSetArg(args[1], XtNheight,&H);
  126. !    XtGetValues(w_label, args, (Cardinal)2);
  127. !    XtGetApplicationResources(w_top, &rv, resources, XtNumber(resources),NULL,0);
  128. !    vchar = (rv.font->ascent + rv.font->descent);
  129.   
  130. !    /* add callbacks on input-from-gnuplot-on-stdin & window-resized */
  131. !    XtAddInput(0, XtInputReadMask, gnuplot, NULL);
  132. !    XtAddEventHandler(w_label, StructureNotifyMask, FALSE, resize, NULL);
  133.   
  134. !    XtMainLoop();
  135.      }
  136.   
  137.   /*-----------------------------------------------------------------------------
  138. !  *   display - display accumulated commands from inboard driver
  139.    *---------------------------------------------------------------------------*/
  140.   
  141.   display() {
  142. --- 8,186 ----
  143.    *   Requires installation of companion inboard x11 driver in gnuplot/term.c
  144.    *
  145.    *   Acknowledgements: 
  146. !  *      Chris Peterson (MIT)
  147. !  *      Dana Chee (Bellcore) 
  148. !  *      Arthur Smith (Cornell)
  149. !  *      Hendri Hondorp (University of Twente, The Netherlands)
  150. !  *      Bill Kucharski (Solbourne)
  151. !  *      Charlie Kline (University of Illinois)
  152. !  *      O'Reilly & Associates: X Window System - Volumes 1 & 2
  153.    *
  154.    *   This code is provided as is and with no warranties of any kind.
  155.    *       
  156. !  *   Ed Kubaitis (ejk@uiuc.edu)
  157. !  *   Computing & Communications Services Office 
  158. !  *   University of Illinois, Urbana
  159.    *---------------------------------------------------------------------------*/
  160. + #include <X11/Xos.h>
  161. + #include <X11/Xlib.h>
  162. + #include <X11/Xutil.h>
  163. + #include <X11/Xatom.h>
  164. + #include <X11/Xresource.h>
  165.    
  166.   #include <stdio.h>
  167.   #include <signal.h>
  168.   
  169. ! #ifndef FD_SET
  170. ! #ifndef OLD_SELECT
  171. ! #include <sys/select.h>
  172. ! #else   /* OLD_SELECT */
  173. ! #define FD_SET(n, p)    ((p)->fds_bits[0] |= (1 << ((n) % 32)))
  174. ! #define FD_CLR(n, p)    ((p)->fds_bits[0] &= ~(1 << ((n) % 32)))
  175. ! #define FD_ISSET(n, p)  ((p)->fds_bits[0] & (1 << ((n) % 32)))
  176. ! #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
  177. ! #endif  /* OLD_SELECT */
  178. ! #endif  /* FD_SET */
  179. ! #include <errno.h>
  180. ! extern int errno;
  181. ! #define FallbackFont "fixed"
  182. ! #define Ncolors 13
  183.   unsigned long colors[Ncolors];
  184.   
  185.   char dashes[10][5] = { {0}, {1,6,0}, 
  186.      {0}, {4,2,0}, {1,3,0}, {4,4,0}, {1,5,0}, {4,4,4,1,0}, {4,2,0}, {1,3,0}
  187.      };
  188.   
  189. ! Display *dpy; int scr; Window win, root;
  190. ! Visual *vis; GC gc = (GC)0; Pixmap pixmap; XFontStruct *font;
  191. ! unsigned int W = 640, H = 450; int D, gX = 100, gY = 100;
  192.   
  193. + Bool Mono = 0, Gray = 0, Rv = 0, Clear = 0;
  194. + char Name[64] = "gnuplot";
  195. + char Class[64] = "Gnuplot";
  196.   int cx=0, cy=0, vchar, nc = 0, ncalloc = 0;
  197.   double xscale, yscale;
  198. ! #define X(x) (int) (x * xscale)
  199. ! #define Y(y) (int) ((4095-y) * yscale)
  200.   enum JUSTIFY { LEFT, CENTRE, RIGHT } jmode;
  201.   #define Nbuf 1024
  202. ! char buf[Nbuf], **commands = (char **)0;
  203.   
  204. ! FILE *X11_ipc = stdin;
  205. ! char X11_ipcpath[32];
  206.   
  207.   
  208.   /*-----------------------------------------------------------------------------
  209. !  *   main program 
  210.    *---------------------------------------------------------------------------*/
  211.   
  212.   main(argc, argv) int argc; char *argv[]; {
  213.   
  214. !    preset(argc, argv);
  215. !    mainloop();
  216. !    exit(0);
  217.   
  218. !    }
  219.   
  220. ! /*-----------------------------------------------------------------------------
  221. !  *   mainloop - process X events and input from gnuplot
  222. !  *
  223. !  *   On systems with a fully implemented select(), select is used (without
  224. !  *   timeout) to sense both input from the X server network connection and
  225. !  *   pipe input from gnuplot. On platforms with an incomplete or faulty 
  226. !  *   select(), select (with timeout) is used for the server, and a temporary 
  227. !  *   file rather than a pipe is used for gnuplot input.
  228. !  *---------------------------------------------------------------------------*/
  229.   
  230. ! mainloop() {
  231. !    int nf, nfds, cn = ConnectionNumber(dpy), in = fileno(X11_ipc);
  232. !    struct timeval timeout, *timer = (struct timeval *)0;
  233. !    fd_set rset, tset;
  234. !    unsigned long all = 0xffffffff;
  235. !    XEvent xe;
  236. !    FD_ZERO(&rset);
  237. !    FD_SET(cn, &rset);
  238. ! #ifndef CRIPPLED_SELECT
  239. !    FD_SET(in, &rset);
  240. !    nfds = (cn > in) ? cn + 1 : in + 1;
  241. ! #else  /* CRIPPLED_SELECT */
  242. !    timeout.tv_sec = 1;
  243. !    timeout.tv_usec = 0;
  244. !    timer = &timeout;
  245. !    sprintf(X11_ipcpath, "/tmp/Gnuplot_%d", getppid());
  246. !    nfds = cn + 1;
  247. ! #endif /* CRIPPLED_SELECT */
  248. !    while(1) {
  249. !       tset = rset;
  250. !       nf = select(nfds, &tset, (fd_set *)0, (fd_set *)0, timer);
  251. !       if (nf < 0) {
  252. !      if (errno == EINTR) continue;
  253. !      fprintf(stderr, "gnuplot: select failed. errno:%d\n", errno);
  254. !      exit(1);
  255. !      }
  256. !       nf > 0 && XNoOp(dpy);
  257. !       if (FD_ISSET(cn, &tset)) {
  258. !      while (XCheckMaskEvent(dpy, all, &xe)) {
  259. !         (xe.type == ConfigureNotify)  && resize(&xe); 
  260.           }
  261.        }
  262. + #ifndef CRIPPLED_SELECT
  263. +       FD_ISSET(in, &tset) && accept();
  264. + #else  /* CRIPPLED_SELECT */
  265. +       if ((X11_ipc = fopen(X11_ipcpath, "r"))) {
  266. +      unlink(X11_ipcpath);
  267. +      accept();
  268. +      fclose(X11_ipc);
  269. +      }
  270. + #endif /* CRIPPLED_SELECT */
  271.         }
  272. !    }
  273.   
  274. ! /*-----------------------------------------------------------------------------
  275. !  *   accept - accept & record new plot from gnuplot inboard X11 driver
  276. !  *---------------------------------------------------------------------------*/
  277.   
  278. ! accept() {
  279. !    while (fgets(buf, Nbuf, X11_ipc)) {
  280. !      if (*buf == 'G') {                           /* enter graphics mode */
  281. !      if (commands) {
  282. !         int n; for (n=0; n<nc; n++) free(commands[n]);
  283. !         free(commands);
  284. !         }
  285. !      commands = (char **)0; nc = ncalloc = 0;
  286. !          }
  287. !       else if (*buf == 'E') { display(); break; } /* leave graphics mode */
  288. !       else if (*buf == 'R') { exit(0); }          /* leave X11/x11 mode  */
  289. !       else {                                      /* record command      */
  290. !      char *p;
  291. !      if (nc >= ncalloc) {
  292. !         ncalloc = ncalloc*2 + 1;
  293. !         commands = (commands)
  294. !            ? (char **)realloc(commands, ncalloc * sizeof(char *))
  295. !            : (char **)malloc(sizeof(char *));
  296. !         }
  297. !      p = (char *)malloc((unsigned)strlen(buf)+1);
  298. !      if (!commands || !p) {
  299. !         fprintf(stderr, "gnuplot: can't get memory. X11 aborted.\n");
  300. !         exit(1);
  301. !         }
  302. !      commands[nc++] = strcpy(p, buf);
  303. !      }
  304. !       }
  305. !    if (feof(X11_ipc) || ferror(X11_ipc)) exit(1);
  306.      }
  307.   
  308.   /*-----------------------------------------------------------------------------
  309. !  *   display - display last plot from gnuplot inboard X11 driver
  310.    *---------------------------------------------------------------------------*/
  311.   
  312.   display() {
  313. ***************
  314. *** 131,166 ****
  315.      int n, x, y, sw, sl, lt, width, type;
  316.      char *buf, *str;
  317.   
  318.      /* set scaling factor between internal driver & window geometry */
  319.      xscale = (double)W / 4096.;  yscale = (double)H / 4096.;  
  320.   
  321.      /* create new pixmap & GC */
  322.      if (gc) { XFreeGC(dpy, gc); XFreePixmap(dpy, pixmap); }
  323. !    pixmap = XCreatePixmap(dpy, RootWindow(dpy,DefaultScreen(dpy)), W, H, D);
  324. !    gc = XCreateGC(dpy, pixmap, 0, NULL);
  325. !    XSetFont(dpy, gc, rv.font->fid);
  326.   
  327. !    /* erase pixmap */
  328. ! #ifndef MOTIF
  329. !    if (Color) { /* Athena needs different erase for color and mono */
  330. ! #endif
  331. !       XSetForeground(dpy, gc, rv.bg);
  332. !       XFillRectangle(dpy, pixmap, gc, 0, 0, W, H);
  333. !       XSetForeground(dpy, gc, rv.fg);
  334. !       XSetBackground(dpy, gc, rv.bg);
  335. ! #ifndef MOTIF
  336. !       }
  337. !    else {  
  338. !       XSetFunction(dpy, gc, GXxor);
  339. !       XCopyArea(dpy, pixmap, pixmap, gc, 0, 0, W, H, 0, 0);
  340. !       XSetFunction(dpy, gc, GXcopyInverted);
  341. !       }
  342. ! #endif
  343.   
  344. !    /* connect new pixmap to label widget */
  345. !    XtSetArg(args[0], LabelBPM, pixmap);
  346. !    XtSetValues(w_label, args, (Cardinal)1);
  347.   
  348.      /* loop over accumulated commands from inboard driver */
  349.      for (n=0; n<nc; n++) {
  350.         buf = commands[n];
  351. --- 187,217 ----
  352.      int n, x, y, sw, sl, lt, width, type;
  353.      char *buf, *str;
  354.   
  355. +    if (!nc) return;
  356.      /* set scaling factor between internal driver & window geometry */
  357.      xscale = (double)W / 4096.;  yscale = (double)H / 4096.;  
  358.   
  359.      /* create new pixmap & GC */
  360.      if (gc) { XFreeGC(dpy, gc); XFreePixmap(dpy, pixmap); }
  361. !    pixmap = XCreatePixmap(dpy, root, W, H, D);
  362. !    gc = XCreateGC(dpy, pixmap, 0, (XGCValues *)0);
  363. !    XSetFont(dpy, gc, font->fid);
  364.   
  365. !    /* set pixmap background */
  366. !    XSetForeground(dpy, gc, colors[0]);
  367. !    XFillRectangle(dpy, pixmap, gc, 0, 0, W, H);
  368. !    XSetBackground(dpy, gc, colors[0]);
  369.   
  370. !    /* set new pixmap as window background */
  371. !    XSetWindowBackgroundPixmap(dpy, win, pixmap);
  372.   
  373. +    /* momentarily clear the window first if requested */
  374. +    if (Clear) {
  375. +       XClearWindow(dpy, win);
  376. +       XFlush(dpy);
  377. +       }
  378.      /* loop over accumulated commands from inboard driver */
  379.      for (n=0; n<nc; n++) {
  380.         buf = commands[n];
  381. ***************
  382. *** 180,198 ****
  383.         else if (*buf == 'T') { 
  384.        sscanf(buf, "T%4d%4d", &x, &y);  
  385.        str = buf + 9; sl = strlen(str) - 1;
  386. !      sw = XTextWidth(rv.font, str, sl);
  387.        switch(jmode) {
  388.           case LEFT:   sw = 0;     break;
  389.           case CENTRE: sw = -sw/2; break;
  390.           case RIGHT:  sw = -sw;   break;
  391.           }
  392. !      if (!Color) 
  393. !         XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
  394. !      else { 
  395. !         XSetForeground(dpy, gc, colors[0]);
  396. !         XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
  397. !         XSetForeground(dpy, gc, colors[lt+1]);
  398. !         }
  399.        }
  400.   
  401.         /*   X11_justify_text(mode) - set text justification mode  */
  402. --- 231,245 ----
  403.         else if (*buf == 'T') { 
  404.        sscanf(buf, "T%4d%4d", &x, &y);  
  405.        str = buf + 9; sl = strlen(str) - 1;
  406. !      sw = XTextWidth(font, str, sl);
  407.        switch(jmode) {
  408.           case LEFT:   sw = 0;     break;
  409.           case CENTRE: sw = -sw/2; break;
  410.           case RIGHT:  sw = -sw;   break;
  411.           }
  412. !      XSetForeground(dpy, gc, colors[2]);
  413. !      XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
  414. !      XSetForeground(dpy, gc, colors[lt+3]);
  415.        }
  416.   
  417.         /*   X11_justify_text(mode) - set text justification mode  */
  418. ***************
  419. *** 204,210 ****
  420.        sscanf(buf, "L%4d", <);
  421.        lt = (lt%8)+2;
  422.        width = (lt == 0) ? 2 : 0;
  423. !      if (Color) {
  424.           if (lt != 1) 
  425.              type = LineSolid;
  426.           else {
  427. --- 251,257 ----
  428.        sscanf(buf, "L%4d", <);
  429.        lt = (lt%8)+2;
  430.        width = (lt == 0) ? 2 : 0;
  431. !      if (!Mono) {
  432.           if (lt != 1) 
  433.              type = LineSolid;
  434.           else {
  435. ***************
  436. *** 211,217 ****
  437.              type = LineOnOffDash;
  438.              XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
  439.              }
  440. !         XSetForeground(dpy, gc, colors[lt+1]);
  441.           }
  442.        else {
  443.           type  = (lt == 0 || lt == 2) ? LineSolid : LineOnOffDash;
  444. --- 258,264 ----
  445.              type = LineOnOffDash;
  446.              XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
  447.              }
  448. !         XSetForeground(dpy, gc, colors[lt+3]);
  449.           }
  450.        else {
  451.           type  = (lt == 0 || lt == 2) ? LineSolid : LineOnOffDash;
  452. ***************
  453. *** 222,263 ****
  454.        }
  455.         }
  456.   
  457. !    /* trigger expose events to display pixmap */
  458. !    XClearArea(dpy, win, 0, 0, 0, 0, True);
  459.      }
  460.   
  461.   /*-----------------------------------------------------------------------------
  462. !  *   gnuplot - Xt callback on input from gnuplot inboard X11 driver
  463. !  *   resize - Xt callback when window resized
  464.    *---------------------------------------------------------------------------*/
  465.   
  466. ! static void
  467. ! gnuplot(cd, s, id) char *cd; int *s; XtInputId *id; {
  468.   
  469. !    while (fgets(buf, Nbuf, stdin)) {
  470. !      if (*buf == 'G') {                           /* enter graphics mode */
  471. !      if (commands) {
  472. !         int n; for (n=0; n<nc; n++) XtFree(commands[n]);
  473. !         XtFree(commands);
  474.           }
  475. -      commands = NULL; nc = ncalloc = 0;
  476. -          }
  477. -       else if (*buf == 'E') { display(); break; } /* leave graphics mode */
  478. -       else if (*buf == 'R') { exit(0); }          /* leave X11/x11 mode  */
  479. -       else {
  480. -      if (nc >= ncalloc) {
  481. -         ncalloc = ncalloc*2 + 1;
  482. -         commands = (String *)XtRealloc(commands, ncalloc * sizeof(String));
  483. -         }
  484. -      commands[nc++] = XtNewString(buf);
  485.        }
  486.         }
  487. !    if (feof(stdin) || ferror(stdin)) exit(0);
  488.      }
  489.   
  490. ! static void
  491. ! resize(w, cd, e) Widget w; char *cd; XConfigureEvent *e; {
  492. !    if (e->type != ConfigureNotify) return;
  493. !    W = e->width; H = e->height;
  494. !    display(); 
  495.      }
  496. --- 269,584 ----
  497.        }
  498.         }
  499.   
  500. !    /* trigger exposure of background pixmap */
  501. !    XClearWindow(dpy,win);
  502. !    XFlush(dpy);
  503.      }
  504.   
  505.   /*-----------------------------------------------------------------------------
  506. !  *   resize - rescale last plot if window resized
  507.    *---------------------------------------------------------------------------*/
  508.   
  509. ! Bool init = True;
  510.   
  511. ! resize(xce) XConfigureEvent *xce; {
  512. !    if (!init || xce->width != W || xce->height != H) {
  513. !       W = xce->width; H = xce->height;
  514. !       display();
  515. !       init = True;
  516. !       }
  517. !    }
  518. ! /*-----------------------------------------------------------------------------
  519. !  *   preset - determine options, open display, create window
  520. !  *---------------------------------------------------------------------------*/
  521. ! #define On(v) ( !strcmp(v,"on") || !strcmp(v,"true") || \
  522. !         !strcmp(v,"On") || !strcmp(v,"True") )
  523. ! #define AppDefDir "/usr/lib/X11/app-defaults"
  524. ! #ifndef MAXHOSTNAMELEN
  525. ! #define MAXHOSTNAMELEN 64
  526. ! #endif
  527. ! static XrmDatabase dbCmd, dbApp, dbDef, dbEnv, db = (XrmDatabase)0;
  528. ! char *pr_GetR(), *getenv(), *type[20];
  529. ! XrmValue value;
  530. ! #define Nopt 25
  531. ! static XrmOptionDescRec options[] = {
  532. !    {"-mono",             ".mono",             XrmoptionNoArg,   "on" },
  533. !    {"-gray",             ".gray",             XrmoptionNoArg,   "on" },
  534. !    {"-clear",            ".clear",            XrmoptionNoArg,   "on" },
  535. !    {"-display",          ".display",          XrmoptionSepArg,  NULL },
  536. !    {"-name",             ".name",             XrmoptionSepArg,  NULL },
  537. !    {"-geometry",         "*geometry",         XrmoptionSepArg,  NULL },
  538. !    {"-background",       "*background",       XrmoptionSepArg,  NULL },
  539. !    {"-bg",               "*background",       XrmoptionSepArg,  NULL },
  540. !    {"-foreground",       "*foreground",       XrmoptionSepArg,  NULL },
  541. !    {"-fg",               "*foreground",       XrmoptionSepArg,  NULL },
  542. !    {"-bordercolor",      "*bordercolor",      XrmoptionSepArg,  NULL },
  543. !    {"-bd",               "*bordercolor",      XrmoptionSepArg,  NULL },
  544. !    {"-borderwidth",      ".borderwidth",      XrmoptionSepArg,  NULL },
  545. !    {"-bw",               ".borderwidth",      XrmoptionSepArg,  NULL },
  546. !    {"-font",             "*font",             XrmoptionSepArg,  NULL },
  547. !    {"-fn",               "*font",             XrmoptionSepArg,  NULL },
  548. !    {"-reverse",          "*reverseVideo",     XrmoptionNoArg,   "on" },
  549. !    {"-rv",               "*reverseVideo",     XrmoptionNoArg,   "on" },
  550. !    {"+rv",               "*reverseVideo",     XrmoptionNoArg,   "off"},
  551. !    {"-iconic",           "*iconic",           XrmoptionNoArg,   "on" },
  552. !    {"-synchronous",      "*synchronous",      XrmoptionNoArg,   "on" },
  553. !    {"-xnllanguage",      "*xnllanguage",      XrmoptionSepArg,  NULL },
  554. !    {"-selectionTimeout", "*selectionTimeout", XrmoptionSepArg,  NULL },
  555. !    {"-title",            ".title",            XrmoptionSepArg,  NULL },
  556. !    {"-xrm",              NULL,                XrmoptionResArg,  NULL },
  557. !    };
  558. ! preset(argc, argv) int argc; char *argv[]; {
  559. !    int Argc = argc; char **Argv = argv;
  560. !    char *display = getenv("DISPLAY"),  *home = getenv("HOME");
  561. !    char *server_defaults, *env, buf[256];
  562. !    /*---set to ignore ^C and ^Z----------------------------------------------*/
  563. !    signal(SIGINT, SIG_IGN);
  564. ! #ifdef SIGTSTP
  565. !    signal(SIGTSTP, SIG_IGN);
  566. ! #endif
  567. !    /*---prescan arguments for "-name"----------------------------------------*/
  568. !    while(++Argv, --Argc > 0) {
  569. !       if (!strcmp(*Argv, "-name") && Argc > 1) {
  570. !      strncpy(Name, Argv[1], 64);
  571. !      strncpy(Class, Argv[1], 64);
  572. !      if (Class[0] >= 'a' && Class[0] <= 'z') Class[0] -= 0x20;
  573. !      }
  574. !       }
  575. !    Argc = argc; Argv = argv;
  576. !    /*---parse command line---------------------------------------------------*/
  577. !    XrmInitialize();
  578. !    XrmParseCommand(&dbCmd, options, Nopt, Name, &Argc, Argv);
  579. !    if (Argc > 1) {
  580. !       fprintf(stderr, "\ngnuplot: bad option: %s\n", Argv[1]);
  581. !       fprintf(stderr, "gnuplot: X11 aborted.\n");
  582. !       exit(1);
  583. !       }
  584. !    if (pr_GetR(dbCmd, ".display")) display = value.addr;
  585. !    /*---open display---------------------------------------------------------*/
  586. !    dpy = XOpenDisplay(display); 
  587. !    if (!dpy) {
  588. !       fprintf(stderr, "\ngnuplot: unable to open display '%s'\n", display);
  589. !       fprintf(stderr, "gnuplot: X11 aborted.\n");
  590. !       exit(1);
  591. !       }
  592. !    scr = DefaultScreen(dpy);
  593. !    vis = DefaultVisual(dpy,scr);
  594. !    D = DefaultDepth(dpy,scr);
  595. !    root = DefaultRootWindow(dpy);
  596. !    server_defaults = XResourceManagerString(dpy);
  597. !    /*---get application defaults--(subset of Xt processing)------------------*/
  598. !    sprintf(buf, "%s/%s", AppDefDir, "Gnuplot");
  599. !    dbApp = XrmGetFileDatabase(buf);
  600. !    XrmMergeDatabases(dbApp, &db);
  601. !    /*---get server or ~/.Xdefaults-------------------------------------------*/
  602. !    if (server_defaults)
  603. !       dbDef = XrmGetStringDatabase(server_defaults);
  604. !    else {
  605. !       sprintf(buf, "%s/.Xdefaults", home);
  606. !       dbDef = XrmGetFileDatabase(buf);
  607. !       }
  608. !    XrmMergeDatabases(dbDef, &db);
  609. !    /*---get XENVIRONMENT or  ~/.Xdefaults-hostname---------------------------*/
  610. !    if (env = getenv("XENVIRONMENT")) 
  611. !       dbEnv = XrmGetFileDatabase(env);
  612. !    else {
  613. !       char *p, host[MAXHOSTNAMELEN];
  614. !       if (gethostname(host, MAXHOSTNAMELEN) < 0) {
  615. !      fprintf(stderr, "gnuplot: gethostname failed. X11 aborted.\n");
  616. !      exit(1);
  617. !      }
  618. !       if (p = index(host, '.')) *p = '\0';
  619. !       sprintf(buf, "%s/.Xdefaults-%s", home, host);
  620. !       dbEnv = XrmGetFileDatabase(buf);
  621. !       }
  622. !    XrmMergeDatabases(dbEnv, &db);
  623. !    /*---merge command line options-------------------------------------------*/
  624. !    XrmMergeDatabases(dbCmd, &db);
  625. !    /*---determine geometry, font and colors----------------------------------*/
  626. !    pr_geometry();
  627. !    pr_font();
  628. !    pr_color();
  629. !    /*---create window--------------------------------------------------------*/
  630. !    pr_window();
  631. !    } 
  632. ! /*-----------------------------------------------------------------------------
  633. !  *   pr_GetR - get resource from database using "-name" option (if any)
  634. !  *---------------------------------------------------------------------------*/
  635. ! char *
  636. ! pr_GetR(db, resource) XrmDatabase db; char *resource; {
  637. !    char name[128], class[128], *rc;
  638. !    strcpy(name, Name); strcat(name, resource);
  639. !    strcpy(class, Class); strcat(class, resource);
  640. !    rc = XrmGetResource(db, name, class, type, &value)
  641. !       ? (char *)value.addr 
  642. !       : (char *)0;
  643. !    return(rc);
  644. !    }
  645. ! /*-----------------------------------------------------------------------------
  646. !  *   pr_color - determine color values
  647. !  *---------------------------------------------------------------------------*/
  648. ! char color_keys[Ncolors][30] =   { 
  649. !    "background", "bordercolor", "text", "border", "axis", 
  650. !    "line1", "line2", "line3",  "line4", 
  651. !    "line5", "line6", "line7",  "line8" 
  652. !    };
  653. ! char color_values[Ncolors][30] = { 
  654. !    "white", "black",  "black",  "black",  "black", 
  655. !    "red",   "green",  "blue",   "magenta", 
  656. !    "cyan",  "sienna", "orange", "coral" 
  657. !    };
  658. ! char gray_values[Ncolors][30] = { 
  659. !    "black",   "white",  "white",  "gray50", "gray50",
  660. !    "gray100", "gray60", "gray80", "gray40", 
  661. !    "gray90",  "gray50", "gray70", "gray30" 
  662. !    };
  663. ! pr_color() {
  664. !    unsigned long black = BlackPixel(dpy, scr), white = WhitePixel(dpy,scr);
  665. !    char option[20], *v, *type = (Gray) ? "Gray" : "Color";
  666. !    XColor used, exact;
  667. !    Colormap cmap;
  668. !    int n;
  669. !    pr_GetR(db, ".mono")         && On(value.addr) && Mono++;
  670. !    pr_GetR(db, ".gray")         && On(value.addr) && Gray++;
  671. !    pr_GetR(db, ".reverseVideo") && On(value.addr) && Rv++;
  672. !    if (!Gray && (vis->class == GrayScale || vis->class == StaticGray)) Mono++;
  673. !    if (!Mono) {
  674. !       cmap = DefaultColormap(dpy, scr);
  675. !       for (n=0; n<Ncolors; n++) {
  676. !      strcpy(option, ".");
  677. !      strcat(option, color_keys[n]);
  678. !      (n > 1) && strcat(option, type);
  679. !      v = pr_GetR(db, option) 
  680. !          ? value.addr
  681. !          : ((Gray) ? gray_values[n] : color_values[n]);
  682. !      if (XAllocNamedColor(dpy, cmap, v, &used, &exact))
  683. !         colors[n] = used.pixel;
  684. !      else {
  685. !         fprintf(stderr, "\ngnuplot: can't allocate %s:%s\n", option, v);
  686. !         fprintf(stderr, "gnuplot: reverting to monochrome\n");
  687. !         Mono++; break;
  688.           }
  689.        }
  690.         }
  691. !    if (Mono) {
  692. !       colors[0] = (Rv) ? black : white ;
  693. !       for (n=1; n<Ncolors; n++)  colors[n] = (Rv) ? white : black;
  694. !       }
  695.      }
  696.   
  697. ! /*-----------------------------------------------------------------------------
  698. !  *   pr_font - determine font          
  699. !  *---------------------------------------------------------------------------*/
  700. ! pr_font() {
  701. !    char *fontname = pr_GetR(db, ".font");
  702. !    if (!fontname) fontname = FallbackFont;
  703. !    font = XLoadQueryFont(dpy, fontname);
  704. !    if (!font) {
  705. !       fprintf(stderr, "\ngnuplot: can't load font '%s'\n", fontname);
  706. !       fprintf(stderr, "gnuplot: using font '%s' instead.\n", FallbackFont);
  707. !       font = XLoadQueryFont(dpy, FallbackFont);
  708. !       if (!font) {
  709. !      fprintf(stderr, "gnuplot: can't load font '%s'\n", FallbackFont);
  710. !      fprintf(stderr, "gnuplot: no useable font - X11 aborted.\n");
  711. !          exit(1);
  712. !      }
  713. !       }
  714. !    vchar = font->ascent + font->descent;
  715. !    }
  716. ! /*-----------------------------------------------------------------------------
  717. !  *   pr_geometry - determine window geometry      
  718. !  *---------------------------------------------------------------------------*/
  719. ! pr_geometry() {
  720. !    char *geometry = pr_GetR(db, ".geometry");
  721. !    int x, y, flags;
  722. !    unsigned int w, h; 
  723. !    if (geometry) {
  724. !       flags = XParseGeometry(geometry, &x, &y, &w, &h);
  725. !       if (flags & WidthValue)  W = w;
  726. !       if (flags & HeightValue) H = h;
  727. !       if (flags & XValue) {
  728. !      if (flags & XNegative) x += DisplayWidth(dpy,scr);
  729. !      gX = x;
  730. !      }
  731. !       if (flags & YValue) {
  732. !      if (flags & YNegative) y += DisplayHeight(dpy,scr);
  733. !      gY = y;
  734. !      }
  735. !       }
  736. !    }
  737. ! /*-----------------------------------------------------------------------------
  738. !  *   pr_window - create window 
  739. !  *---------------------------------------------------------------------------*/
  740. ! pr_window() {
  741. !    char *title =  pr_GetR(db, ".title");
  742. !    XSizeHints hints;
  743. !    win = XCreateSimpleWindow(dpy, root, gX, gY, W, H, 2, colors[1], colors[0]);
  744. !    pr_GetR(db, ".clear") && On(value.addr) && Clear++;
  745. !    hints.flags = PPosition;
  746. !    hints.x = gX; hints.y = gY;
  747. !    XSetNormalHints(dpy, win, &hints);
  748. !    if (pr_GetR(db, ".iconic") && On(value.addr)) {
  749. !       XWMHints wmh;
  750. !       wmh.flags = StateHint ;
  751. !       wmh.initial_state = IconicState;
  752. !       XSetWMHints(dpy, win, &wmh);
  753. !       } 
  754. !    XStoreName(dpy, win, ((title) ? title : Class));
  755. !    XSelectInput(dpy, win, StructureNotifyMask);
  756. !    XMapWindow(dpy, win);
  757. !    
  758.      }
  759.