[comp.sources.x] v08i090: Gnuplot 2.0 X11 drivers, Part01/01

thompson_r@hpmtlx.HP.COM ($Robert_Thompson) (08/22/90)

Submitted-by: thompson_r@hpmtlx.HP.COM ($Robert_Thompson)
Posting-number: Volume 8, Issue 90
Archive-name: gnuplot.patch/part01


This is a set of diffs and terminal drivers for X11.  With this addition to
the gnuplot terminal family, you can display a graph on your X11 screen.
The plot is scaled according to the window size (without affecting the
'size' parameter).  You can also view more than one plot at a time by
simply specifying the terminal type after you have a plot.  For example:

	gnuplot> set term x11
	gnuplot> plot sin(x)
	gnuplot> set term x11
	gnuplot> plot cos(x)

This will create two windows having the sin and cosine plots, respectively.
You cannot, at this time, remove the first plot from the screen without
closing the window (from mwm for example).  

	The terminal drivers have been written for color and monochrome
displays.  You can install one or the other as is (installing both could be
done as well, with some mods).

	
<--------------------------- CUT HERE --------------------------------->
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by  <thompson_r@hpmtlrt> on Fri Jul 13 13:43:20 1990
#
# This archive contains:
#	makefile.diff	term.c.diff	X11.trm.color	X11.trm.mono	
#

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

echo x - makefile.diff
cat >makefile.diff <<'@EOF'
*** makefile.old	Fri Jul 13 13:27:48 1990
--- makefile	Fri Jul 13 13:27:39 1990
***************
*** 10,14
  
  # directory where to install executables on 'make install'
! DEST=
  # directory for installing man page on 'make man_install'
  MANDEST=

--- 10,14 -----
  
  # directory where to install executables on 'make install'
! DEST=/usr/contrib/bin
  # directory for installing man page on 'make man_install'
  MANDEST=.
***************
*** 12,16
  DEST=
  # directory for installing man page on 'make man_install'
! MANDEST=
  # where to install help file gnuplot.gih
  HELPDEST= 

--- 12,16 -----
  DEST=/usr/contrib/bin
  # directory for installing man page on 'make man_install'
! MANDEST=.
  # where to install help file gnuplot.gih
  HELPDEST= /usr/contrib/lib/gnuplot.gih
***************
*** 14,18
  MANDEST=
  # where to install help file gnuplot.gih
! HELPDEST= 
  
  # -DVFORK if you have vfork()

--- 14,18 -----
  MANDEST=.
  # where to install help file gnuplot.gih
! HELPDEST= /usr/contrib/lib/gnuplot.gih
  
  # -DVFORK if you have vfork()
***************
*** 38,41
  # -DHP75        HP7580, and probably other HPs
  # -DHPGL                HP7475 and (hopefully) lots of others
  # -DHPLJET              HP Laserjet
  # -DIMAGEN      Imagen laser printers (300dpi) (requires -Iterm also)

--- 38,42 -----
  # -DHP75        HP7580, and probably other HPs
  # -DHPGL                HP7475 and (hopefully) lots of others
+ # -DX11		X11 support
  # -DHPLJET              HP Laserjet
  # -DIMAGEN      Imagen laser printers (300dpi) (requires -Iterm also)
***************
*** 59,62
  	-DUNIXPLOT -DV384
  
  OBJS = command.o eval.o graphics.o help.o internal.o misc.o parse.o\
      plot.o scanner.o setshow.o standard.o term.o util.o

--- 60,66 -----
  	-DUNIXPLOT -DV384
  
+ TERMFLAGS =  -Iterm -DHPGL -DHPLJET -DPOSTSCRIPT -DTEK -DHP2648 \
+ 	-DHP26 -DX11
+ 
  OBJS = command.o eval.o graphics.o help.o internal.o misc.o parse.o\
      plot.o scanner.o setshow.o standard.o term.o util.o
***************
*** 72,76
  CSOURCE6 = term/post.trm term/pc.trm term/qms.trm term/regis.trm \
  	term/tek.trm term/unixpc.trm term/unixplot.trm term/v384.trm \
! 	term/imPcodes.h term/imagen.trm term/object.h term/fig.trm
  # not C code, but still needed
  ETC = README README.gnutex makefile.unx makefile.vms linkopt.vms \

--- 76,81 -----
  CSOURCE6 = term/post.trm term/pc.trm term/qms.trm term/regis.trm \
  	term/tek.trm term/unixpc.trm term/unixplot.trm term/v384.trm \
! 	term/imPcodes.h term/imagen.trm term/object.h term/fig.trm \
! 	term/X11.trm
  # not C code, but still needed
  ETC = README README.gnutex makefile.unx makefile.vms linkopt.vms \
***************
*** 101,104
  
  # -lplot iff you have -DUNIXPLOT
  # -lsuntool -lsunwindow -lpixrect  iff you have -DSUN
  # -lgl_s if IRIS4D

--- 106,110 -----
  
  # -lplot iff you have -DUNIXPLOT
+ # -lX11 if you have -DX11
  # -lsuntool -lsunwindow -lpixrect  iff you have -DSUN
  # -lgl_s if IRIS4D
***************
*** 103,107
  # -lsuntool -lsunwindow -lpixrect  iff you have -DSUN
  # -lgl_s if IRIS4D
! LIBS = -lm 
  
  gnuplot: $(OBJS) version.o

--- 109,113 -----
  # -lsuntool -lsunwindow -lpixrect  iff you have -DSUN
  # -lgl_s if IRIS4D
! LIBS = -lm -lX11 
  
  gnuplot: $(OBJS) version.o
@EOF

chmod 666 makefile.diff

echo x - term.c.diff
cat >term.c.diff <<'@EOF'
*** term.c.old	Fri Jul 13 13:28:38 1990
--- term.c	Fri Jul 13 13:27:57 1990
***************
*** 376,379
  #endif /* UNIXPLOT */
  
  /* Dummy functions for unavailable features */
  

--- 376,383 -----
  #endif /* UNIXPLOT */
  
+ #ifdef X11
+ #include "term/X11.trm"
+ #endif /* X11 */
+ 
  /* Dummy functions for unavailable features */
  
***************
*** 610,613
  	   HPGL_text, null_scale, HPGL_graphics, HPGL_move, HPGL_vector,
  	   HPGL_linetype, HPGL_put_text, HPGL_text_angle, 
  	   null_justify_text, do_point, do_arrow}
  #endif

--- 614,626 -----
  	   HPGL_text, null_scale, HPGL_graphics, HPGL_move, HPGL_vector,
  	   HPGL_linetype, HPGL_put_text, HPGL_text_angle, 
+ 	   null_justify_text, do_point, do_arrow}
+ #endif
+ 
+ #ifdef X11
+     ,{"x11", "X Windows Support (V11R3)",
+ 	   X11_XMAX, X11_YMAX, X11_VCHAR, X11_HCHAR,
+ 	   X11_VTIC, X11_HTIC, X11_init, X11_reset,
+ 	   X11_text, null_scale, X11_graphics, X11_move, X11_vector,
+ 	   X11_linetype, X11_put_text, X11_text_angle, 
  	   null_justify_text, do_point, do_arrow}
  #endif
@EOF

chmod 666 term.c.diff

echo x - X11.trm.color
cat >X11.trm.color <<'@EOF'
/* GNUPLOT - X11.trm */
/*
 * Copyright (C) 1990   
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or 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.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *  
 * This software  is provided "as is" without express or implied warranty.
 * 
 * This file is included by ../term.c.
 *
 * This terminal driver supports:
 *  X Windows Version 11, revision 3
 *
 * AUTHOR
 *  Robert Thompson
 *  Hewlett Packard - Manufacturing Test Division, Loveland Colorado
 * 
 * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
 * 
 */

/********************************************************************
 *
 *  THIS IS FOR COLOR WORKSTATIONS ONLY.  
 *
 ********************************************************************/

#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/cursorfont.h>

#define X11_FONT 	"vtsingle"	/* Font Name to use		*/
#define X11_CURSOR	XC_hand2

#define X11_XMAX 900
#define X11_YMAX 850

#define X11_XLAST (1)
#define X11_YLAST (1)

#define X11_VCHAR	13		/* Don't forget to change this 	*/
#define X11_HCHAR	8		/* with the font		*/
#define X11_VTIC	(X11_YMAX/85)		
#define X11_HTIC	(X11_XMAX/90)		

int X11_ang = 0;

extern float	xsize, ysize;
int		changed_by_X11trm;

static char	X11_FOREGROUND[][20] = {
		"green",		/* pen 1	*/
		"cyan",			/* pen 2	*/
		"blue",			/* pen 3	*/
		"magenta",		/* pen 4	*/
		"yellow",		/* pen 5	*/
		"red",			/* pen 6	*/
		"white",		/* pen 7	*/
		"orchid",		/* pen 8	*/
		"orange",		/* pen 9	*/
		"pink",			/* pen 10	*/
		"SkyBlue",		/* pen 11	*/
		"LimeGreen",		/* pen 12	*/
		"LightSteelBlue",	/* pen 13	*/
		"aquamarine"		/* pen 14	*/
		};

#define NUMCOLORS	14
#define X11_BACKGROUND	"DarkSlateGray"	/* choose a neutral bg	*/

Window		win;	/* The window that we will be using	*/
Display		*dis;	/* And the display			*/
GC		gc;	/* The graphics context			*/
XFontStruct	*font;	/* The font structure to use		*/
Colormap	cmap;	/* The colormap				*/
XColor 		fgc[NUMCOLORS],		/* An array of colors	*/
		bgc,	/* And the background color		*/
		dummy;	/* Duh					*/

int		lastx, lasty, lastw, lasth;
unsigned int	width, height;

X11_init()
{
	char			*getenv();
	register int		cn;
	XSetWindowAttributes	attr;
	unsigned int		x, y, bw, d;
	Window			root;

	dis 		= XOpenDisplay(getenv("DISPLAY"));
	font		= XLoadQueryFont(dis,X11_FONT);
	attr.cursor 	= XCreateFontCursor(dis,X11_CURSOR);
	attr.backing_store = Always;
	attr.backing_planes = AllPlanes;

	win 		= XCreateSimpleWindow(dis,
				RootWindow(dis,DefaultScreen(dis)),
				0,
				0,
				X11_XMAX+10,
				X11_YMAX+10,
				2,
				WhitePixel(dis,DefaultScreen(dis)),
				BlackPixel(dis,DefaultScreen(dis)));
				
	gc		= DefaultGC(dis,DefaultScreen(dis));
	cmap		= DefaultColormap(dis,DefaultScreen(dis));

	if (XAllocNamedColor(dis,cmap,X11_BACKGROUND,&bgc,&dummy)
	==BadColor)
		{
		printf ("Could not allocate color %s\n",X11_BACKGROUND);
		exit(0);
		}

	for (cn = 0;cn < NUMCOLORS;cn++)
		{
		if (XAllocNamedColor(dis,cmap,X11_FOREGROUND[cn],
		&fgc[cn],&dummy)==BadColor) {
		printf ("Could not allocate color %s\n",X11_FOREGROUND[cn]);
		}
	}

	XChangeWindowAttributes(dis,win,
			CWCursor|CWBackingStore|CWBackingPlanes,&attr);
	XSetWindowBackground(dis,win,bgc);

	XSetFont(dis,gc,font->fid);
	XSetForeground(dis,gc,fgc[1].pixel);

	XSetIconName(dis,win,"Gnuplot");
	XStoreName(dis,win,"Gnuplot Plotting Surface");

	XMapWindow(dis,win);
	XFlush(dis);

	XGetGeometry(dis,win,&root,&x,&y,&width,&height,&bw,&d);

	lastw = width;
	lasth = height;
}

X11_graphics()
{
    	register struct termentry *t = &term_tbl[term];
	unsigned int			x, y, bw, d;
	Window				root;

	XGetGeometry(dis,win,&root,&x,&y,&width,&height,&bw,&d);

	if (lastw != width || lasth != height)
		{
		register struct termentry *t = &term_tbl[term];

		t->xmax = (unsigned int)(width);
		t->ymax = (unsigned int)(height);
		}

	XClearWindow(dis,win);
	XSetForeground(dis,gc,BlackPixel(dis,DefaultScreen(dis)));
	XDrawImageString(dis,win,gc,
		width-X11_HCHAR*14,X11_VCHAR+2," Plotting ",10);
	XFlush(dis);
	X11_ang = 0;
}

X11_text()
{
	XClearArea(dis,win, 
		width-X11_HCHAR*14,2,
		X11_HCHAR*10,X11_VCHAR+4,True);
	XFlush(dis);
}


X11_linetype(linetype)
int linetype;
{
	if (linetype < 0)
		XSetForeground(dis,gc,WhitePixel(dis,DefaultScreen(dis)));
	else
		XSetForeground(dis,gc,fgc[linetype%NUMCOLORS].pixel);
}

X11_move(x,y)
int x,y;
{
	lastx = x;
	lasty = height-y;
}


X11_vector(x,y)
int x,y;
{
	XDrawLine(dis,win,gc, lastx, lasty, x, height -y );
	XFlush(dis);

	lastx = x;
	lasty = height-y;
}


X11_put_text(x,y,str)
int x, y;
char *str;
{
	XDrawString(dis,win,gc,x,height-y,str,strlen(str));
	XFlush(dis);
}

int X11_text_angle(ang)
int ang;
{
	return FALSE;
}

X11_reset()
{
	XDestroyWindow(dis,win);
	XFreeColormap(dis,cmap);
}

@EOF

chmod 666 X11.trm.color

echo x - X11.trm.mono
cat >X11.trm.mono <<'@EOF'
/* GNUPLOT - X11.trm */
/*
 * Copyright (C) 1990   
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or 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.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *  
 * This software  is provided "as is" without express or implied warranty.
 * 
 * This file is included by ../term.c.
 *
 * This terminal driver supports:
 *  X Windows Version 11, revision 3
 *
 * AUTHOR
 *  Robert Thompson
 *  Hewlett Packard - Manufacturing Test Division, Loveland Colorado
 * 
 * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
 * 
 */

/********************************************************************
 *
 *  THIS IS FOR MONOCHROME WORKSTATIONS ONLY.  
 *
 ********************************************************************/

#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/cursorfont.h>

#define X11_FONT 	"vtsingle"	/* Font Name to use		*/
#define X11_CURSOR	XC_hand2

#define X11_XMAX 900
#define X11_YMAX 850

#define X11_XLAST (1)
#define X11_YLAST (1)

#define X11_VCHAR	13		/* Don't forget to change this 	*/
#define X11_HCHAR	8		/* with the font		*/
#define X11_VTIC	(X11_YMAX/85)		
#define X11_HTIC	(X11_XMAX/90)		

int X11_ang = 0;

extern float	xsize, ysize;
int		changed_by_X11trm;

Window		win;	/* The window that we will be using	*/
Display		*dis;	/* And the display			*/
GC		gc;	/* The graphics context			*/
XFontStruct	*font;	/* The font structure to use		*/

int		lastx, lasty, lastw, lasth;
unsigned int	width, height;

X11_init()
{
	char			*getenv();
	register int		cn;
	XSetWindowAttributes	attr;
	unsigned int		x, y, bw, d;
	Window			root;

	dis 		= XOpenDisplay(getenv("DISPLAY"));
	font		= XLoadQueryFont(dis,X11_FONT);
	attr.cursor 	= XCreateFontCursor(dis,X11_CURSOR);
	attr.backing_store = Always;
	attr.backing_planes = AllPlanes;

	win 		= XCreateSimpleWindow(dis,
				RootWindow(dis,DefaultScreen(dis)),
				0,
				0,
				X11_XMAX+10,
				X11_YMAX+10,
				2,
				WhitePixel(dis,DefaultScreen(dis)),
				BlackPixel(dis,DefaultScreen(dis)));
				
	gc		= DefaultGC(dis,DefaultScreen(dis));

	XChangeWindowAttributes(dis,win,
			CWCursor|CWBackingStore|CWBackingPlanes,&attr);

	XSetFont(dis,gc,font->fid);
	XSetForeground(dis,gc,WhitePixel(dis,DefaultScreen(dis));

	XSetIconName(dis,win,"Gnuplot");
	XStoreName(dis,win,"Gnuplot Plotting Surface");

	XMapWindow(dis,win);
	XFlush(dis);

	XGetGeometry(dis,win,&root,&x,&y,&width,&height,&bw,&d);

	lastw = width;
	lasth = height;
}

X11_graphics()
{
    	register struct termentry *t = &term_tbl[term];
	unsigned int			x, y, bw, d;
	Window				root;

	XGetGeometry(dis,win,&root,&x,&y,&width,&height,&bw,&d);

	if (lastw != width || lasth != height)
		{
		register struct termentry *t = &term_tbl[term];

		t->xmax = (unsigned int)(width);
		t->ymax = (unsigned int)(height);
		}

	XClearWindow(dis,win);
	XSetForeground(dis,gc,BlackPixel(dis,DefaultScreen(dis)));
	XDrawImageString(dis,win,gc,
		width-X11_HCHAR*14,X11_VCHAR+2," Plotting ",10);
	XFlush(dis);
	X11_ang = 0;
}

X11_text()
{
	XClearArea(dis,win, 
		width-X11_HCHAR*14,2,
		X11_HCHAR*10,X11_VCHAR+4,True);
	XFlush(dis);
}


X11_linetype(linetype)
int linetype;
{
		XSetForeground(dis,gc,WhitePixel(dis,DefaultScreen(dis)));
}

X11_move(x,y)
int x,y;
{
	lastx = x;
	lasty = height-y;
}


X11_vector(x,y)
int x,y;
{
	XDrawLine(dis,win,gc, lastx, lasty, x, height -y );
	XFlush(dis);

	lastx = x;
	lasty = height-y;
}


X11_put_text(x,y,str)
int x, y;
char *str;
{
	XDrawString(dis,win,gc,x,height-y,str,strlen(str));
	XFlush(dis);
}

int X11_text_angle(ang)
int ang;
{
	return FALSE;
}

X11_reset()
{
	XDestroyWindow(dis,win);
}

@EOF

chmod 666 X11.trm.mono

exit 0

dan
----------------------------------------------------
O'Reilly && Associates   argv@sun.com / argv@ora.com
Opinions expressed reflect those of the author only.