home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 334_01 / gnuplot.x11 < prev    next >
Text File  |  1991-02-05  |  9KB  |  259 lines

  1. /*-----------------------------------------------------------------------------
  2.  *   gnuplot_x11 - X11 outboard terminal driver for gnuplot 2
  3.  *
  4.  *   Requires installation of companion inboard x11 driver in gnuplot/term.c
  5.  *
  6.  *   Acknowledgements: 
  7.  *      Chris Peterson (MIT) - original Xlib gnuplot support (and Xaw examples)
  8.  *      Dana Chee (Bellcore)  - mods to original support for gnuplot 2.0
  9.  *      Arthur Smith (Cornell) - graphical-label-widget idea (xplot)
  10.  *      Hendri Hondorp (University of Twente, The Netherlands) - Motif xgnuplot
  11.  *
  12.  *   This code is provided as is and with no warranties of any kind.
  13.  *       
  14.  *   Ed Kubaitis - Computing Services Office -  University of Illinois, Urbana
  15.  *---------------------------------------------------------------------------*/
  16.  
  17. #include <stdio.h>
  18. #include <signal.h>
  19. #include <X11/Intrinsic.h>
  20. #include <X11/StringDefs.h>
  21. #include <Label.h>          /* use -Idir for location on your system */
  22. #ifdef MOTIF
  23. #include <Xm.h>             /* use -Idir for location on your system */
  24. #define LabelWC xmLabelWidgetClass
  25. #define LabelBPM XmNbackgroundPixmap
  26. #else
  27. #define LabelWC labelWidgetClass
  28. #define LabelBPM XtNbitmap
  29. #endif
  30.  
  31. #define Color (D>1)
  32. #define Ncolors 11
  33. unsigned long colors[Ncolors];
  34. char color_keys[Ncolors][30] =   { "text", "border", "axis", 
  35.    "line1", "line2", "line3", "line4", "line5", "line6", "line7", "line8" };
  36. char color_values[Ncolors][30] = { "black", "black", "black", 
  37.    "red",  "green", "blue",  "magenta", "cyan", "sienna", "orange", "coral" };
  38.  
  39. char dashes[10][5] = { {0}, {1,6,0}, 
  40.    {0}, {4,2,0}, {1,3,0}, {4,4,0}, {1,5,0}, {4,4,4,1,0}, {4,2,0}, {1,3,0}
  41.    };
  42.  
  43. Widget w_top, w_label; Window win; Display *dpy;
  44. Pixmap pixmap;  GC gc = (GC)NULL;
  45. Dimension W = 640 , H = 450;  int D;
  46. Arg args[5];
  47. static void gnuplot(), resize();
  48.  
  49. int cx=0, cy=0, vchar, nc = 0;
  50. double xscale, yscale;
  51. #define X(x) (Dimension) (x * xscale)
  52. #define Y(y) (Dimension) ((4095-y) * yscale)
  53. enum JUSTIFY { LEFT, CENTRE, RIGHT } jmode;
  54. #define Nbuf 1024
  55. char buf[Nbuf];
  56. String *commands = NULL;
  57.  
  58. typedef struct {       /* See "X Toolkit Intrinsics Programming Manual"      */
  59.   XFontStruct *font;   /* Nye and O'Reilly, O'Reilly & Associates, pp. 80-85 */
  60.   unsigned long fg;
  61.   unsigned long bg;
  62.   } RValues, *RVptr; 
  63. RValues rv;
  64.  
  65. XtResource resources[] = {
  66.    { XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  67.      XtOffset(RVptr, font), XtRString, "fixed" },
  68.    { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel), 
  69.      XtOffset(RVptr, fg), XtRString, XtDefaultForeground },
  70.    { XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel), 
  71.      XtOffset(RVptr, bg), XtRString, XtDefaultBackground },
  72.    };
  73.  
  74. /*-----------------------------------------------------------------------------
  75.  *   main program - fire up application and callbacks
  76.  *---------------------------------------------------------------------------*/
  77.  
  78. main(argc, argv) int argc; char *argv[]; {
  79.  
  80.    signal(SIGINT, SIG_IGN);
  81.    signal(SIGTSTP, SIG_IGN);
  82.  
  83.    /* initialize application */
  84.    w_top = XtInitialize("gnuplot", "Gnuplot", NULL, 0, &argc, argv);
  85.    XtSetArg(args[0], XtNwidth, W);
  86.    XtSetArg(args[1], XtNheight, H);
  87.    w_label = XtCreateManagedWidget ("", LabelWC, w_top, args, (Cardinal)2);
  88.    XtRealizeWidget(w_top);
  89.  
  90.    /* extract needed information */
  91.    dpy = XtDisplay(w_top); win = XtWindow(w_label);
  92.    D = DisplayPlanes(dpy,DefaultScreen(dpy));
  93.    if (Color) {
  94.       char option[20], *value; 
  95.       XColor used, exact; int n;
  96.  
  97.       for(n=0; n<Ncolors; n++) {
  98.      strcpy(option, color_keys[n]);
  99.      strcat(option, "Color");
  100.      value = XGetDefault(dpy, "gnuplot", option);
  101.      if (!value) { value = color_values[n]; }
  102.      if (XAllocNamedColor(dpy, DefaultColormap(dpy,0), value, &used,&exact))
  103.         colors[n] = used.pixel; 
  104.      else {
  105.         fprintf(stderr, "gnuplot: cannot allocate %s:%s\n", option, value);
  106.         fprintf(stderr, "gnuplot: assuming %s:black\n", option);
  107.         colors[n] = BlackPixel(dpy,0);
  108.         }
  109.      }
  110.       }
  111.    XtSetArg(args[0], XtNwidth, &W);
  112.    XtSetArg(args[1], XtNheight,&H);
  113.    XtGetValues(w_label, args, (Cardinal)2);
  114.    XtGetApplicationResources(w_top, &rv, resources, XtNumber(resources),NULL,0);
  115.    vchar = (rv.font->ascent + rv.font->descent);
  116.  
  117.    /* add callbacks on input-from-gnuplot-on-stdin & window-resized */
  118.    XtAddInput(0, XtInputReadMask, gnuplot, NULL);
  119.    XtAddEventHandler(w_label, StructureNotifyMask, FALSE, resize, NULL);
  120.  
  121.    XtMainLoop();
  122.    }
  123.  
  124. /*-----------------------------------------------------------------------------
  125.  *   display - display accumulated commands from inboard driver
  126.  *---------------------------------------------------------------------------*/
  127.  
  128. display() {
  129.    int n, x, y, sw, sl, lt, width, type;
  130.    char *buf, *str;
  131.  
  132.    /* set scaling factor between internal driver & window geometry */
  133.    xscale = (double)W / 4096.;  yscale = (double)H / 4096.;  
  134.  
  135.    /* create new pixmap & GC */
  136.    if (gc) { XFreeGC(dpy, gc); XFreePixmap(dpy, pixmap); }
  137.    pixmap = XCreatePixmap(dpy, RootWindow(dpy,DefaultScreen(dpy)), W, H, D);
  138.    gc = XCreateGC(dpy, pixmap, 0, NULL);
  139.    XSetFont(dpy, gc, rv.font->fid);
  140.  
  141.    /* erase pixmap */
  142. #ifndef MOTIF
  143.    if (Color) { /* Athena needs different erase for color and mono */
  144. #endif
  145.       XSetForeground(dpy, gc, rv.bg);
  146.       XFillRectangle(dpy, pixmap, gc, 0, 0, W, H);
  147.       XSetForeground(dpy, gc, rv.fg);
  148.       XSetBackground(dpy, gc, rv.bg);
  149. #ifndef MOTIF
  150.       }
  151.    else {  
  152.       XSetFunction(dpy, gc, GXxor);
  153.       XCopyArea(dpy, pixmap, pixmap, gc, 0, 0, W, H, 0, 0);
  154.       XSetFunction(dpy, gc, GXcopyInverted);
  155.       }
  156. #endif
  157.  
  158.    /* connect new pixmap to label widget */
  159.    XtSetArg(args[0], LabelBPM, pixmap);
  160.    XtSetValues(w_label, args, (Cardinal)1);
  161.  
  162.    /* loop over accumulated commands from inboard driver */
  163.    for (n=0; n<nc; n++) {
  164.       buf = commands[n];
  165.  
  166.       /*   X11_vector(x,y) - draw vector  */
  167.       if (*buf == 'V') { 
  168.      sscanf(buf, "V%4d%4d", &x, &y);  
  169.      XDrawLine(dpy, pixmap, gc, X(cx), Y(cy), X(x), Y(y));
  170.      cx = x; cy = y;
  171.      }
  172.  
  173.       /*   X11_move(x,y) - move  */
  174.       else if (*buf == 'M') 
  175.      sscanf(buf, "M%4d%4d", &cx, &cy);  
  176.  
  177.       /*   X11_put_text(x,y,str) - draw text   */
  178.       else if (*buf == 'T') { 
  179.      sscanf(buf, "T%4d%4d", &x, &y);  
  180.      str = buf + 9; sl = strlen(str) - 1;
  181.      sw = XTextWidth(rv.font, str, sl);
  182.      switch(jmode) {
  183.         case LEFT:   sw = 0;     break;
  184.         case CENTRE: sw = -sw/2; break;
  185.         case RIGHT:  sw = -sw;   break;
  186.         }
  187.      if (!Color) 
  188.         XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
  189.      else { 
  190.         XSetForeground(dpy, gc, colors[0]);
  191.         XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
  192.         XSetForeground(dpy, gc, colors[lt+1]);
  193.         }
  194.      }
  195.  
  196.       /*   X11_justify_text(mode) - set text justification mode  */
  197.       else if (*buf == 'J') 
  198.      sscanf(buf, "J%4d", &jmode);
  199.  
  200.       /*   X11_linetype(type) - set line type  */
  201.       else if (*buf == 'L') { 
  202.      sscanf(buf, "L%4d", <);
  203.      lt = (lt+2)%10;
  204.      width = (lt == 0) ? 2 : 0;
  205.      if (Color) {
  206.         if (lt != 1) 
  207.            type = LineSolid;
  208.         else {
  209.            type = LineOnOffDash;
  210.            XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
  211.            }
  212.         XSetForeground(dpy, gc, colors[lt+1]);
  213.         }
  214.      else {
  215.         type  = (lt == 0 || lt == 2) ? LineSolid : LineOnOffDash;
  216.         if (dashes[lt][0])
  217.            XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
  218.         }
  219.      XSetLineAttributes( dpy,gc, width, type, CapButt, JoinBevel);
  220.      }
  221.       }
  222.  
  223.    /* trigger expose events to display pixmap */
  224.    XClearArea(dpy, win, 0, 0, 0, 0, True);
  225.    }
  226.  
  227. /*-----------------------------------------------------------------------------
  228.  *   gnuplot - Xt callback on input from gnuplot inboard X11 driver
  229.  *   resize - Xt callback when window resized
  230.  *---------------------------------------------------------------------------*/
  231.  
  232. static void
  233. gnuplot(cd, s, id) char *cd; int *s; XtInputId *id; {
  234.  
  235.    while (fgets(buf, Nbuf, stdin)) {
  236.      if (*buf == 'G') {                           /* enter graphics mode */
  237.      if (commands) {
  238.         int n; for (n=0; n<nc; n++) XtFree(commands[n]);
  239.         XtFree(commands);
  240.         }
  241.      commands = NULL; nc = 0;
  242.          }
  243.       else if (*buf == 'E') { display(); break; } /* leave graphics mode */
  244.       else if (*buf == 'R') { exit(0); }          /* leave X11/x11 mode  */
  245.       else {
  246.      commands = (String *)XtRealloc(commands, (nc+1) * sizeof(String));
  247.      commands[nc++] = XtNewString(buf);
  248.      }
  249.       }
  250.    if (feof(stdin) || ferror(stdin)) exit(0);
  251.    }
  252.  
  253. static void
  254. resize(w, cd, e) Widget w; char *cd; XConfigureEvent *e; {
  255.    if (e->type != ConfigureNotify) return;
  256.    W = e->width; H = e->height;
  257.    display(); 
  258.    }
  259.