/*
 * $Id: regis.trm,v 1.10 1995/12/20 21:48:11 drd Exp $
 *
 */

/* GNUPLOT - regis.trm */
/*
 * Copyright (C) 1990 - 1993   
 *
 * 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:
 *  REGIS devices
 *
 * AUTHORS
 *  Colin Kelley, Thomas Williams
 * 
 * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
 * 
 */

#ifndef GOT_DRIVER_H
#include "driver.h"
#endif

#ifdef TERM_REGISTER
register_term(regis)
#endif


#ifdef TERM_PROTO
TERM_PUBLIC void REGISinit __P((void));
TERM_PUBLIC void REGISgraphics __P((void));
TERM_PUBLIC void REGISinit __P((void));
TERM_PUBLIC void REGISgraphics __P((void));
TERM_PUBLIC void REGIStext __P((void));
TERM_PUBLIC void REGISlinetype __P((int linetype));
TERM_PUBLIC void REGISmove __P((unsigned int x, unsigned int y));
TERM_PUBLIC void REGISvector __P((unsigned int x, unsigned int y));
TERM_PUBLIC void REGISput_text __P((unsigned int x, unsigned int y, char *str));
TERM_PUBLIC int REGIStext_angle __P((int ang));
TERM_PUBLIC void REGISreset __P((void));
TERM_PUBLIC void REGISoptions __P((void));

#define REGISXMAX 800             
#define REGISYMAX 440

#define REGISXLAST (REGISXMAX - 1)
#define REGISYLAST (REGISYMAX - 1)

#define REGISVCHAR		20  	
#define REGISHCHAR		9
#define REGISVTIC		8
#define REGISHTIC		6

#define TRUE 1
#define FALSE 0
#endif



#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY

static int REGIS16color=FALSE;

static int REGISang = 0;

TERM_PUBLIC void REGISinit()
{
	fprintf(outfile,"\033[r\033[24;1H");
/*                   1     2
	1. reset scrolling region
	2. locate cursor on bottom line
*/
}


/* thanks to calmasd!dko (Dan O'Neill) for adding S(E) for vt125s */
TERM_PUBLIC void REGISgraphics()
{
    fprintf(outfile,"\033[2J\033P1pS(C0)S(E)T(A0)\n");
/*                   1      2      3    4   5
        1. clear screen
        2. enter ReGIS graphics
        3. turn off graphics diamond cursor
        4. clear graphics screen
        5. character set option.
*/
        (void) REGIStext_angle(0); /* select text size and angle */
}


TERM_PUBLIC void REGIStext()
{
	fprintf(outfile,"\033\\\033[24;1H");
/*	                   1    2
	1. Leave ReGIS graphics mode
 	2. locate cursor on last line of screen
*/
}


TERM_PUBLIC void REGISlinetype(linetype)
int     linetype;
{
      /* This will change color in order G,R,B,G-dot,R-dot,B-dot */

static int in_16_map[15+2] = {7,7,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
static int lt_16_map[15+2] = {1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
static int in_map[9 + 2] = {2, 2, 3, 2, 1, 3, 2, 1, 3, 2, 1};
static int lt_map[9 + 2] = {1, 4, 1, 1, 1, 4, 4, 4, 6, 6, 6};
   
   	if(!REGIS16color)
   	{	if (linetype >= 9)
   			linetype %= 9;
   		fprintf(outfile, "W(I%d)\n", in_map[linetype + 2]);
   		fprintf(outfile, "W(P%d)\n", lt_map[linetype + 2]);
   	}
   	else
   	{	if(linetype >=15)
   			linetype %= 15;
   		fprintf(outfile, "W(I%d)\n", in_16_map[linetype + 2]);
   		fprintf(outfile, "W(P%d)\n", lt_16_map[linetype + 2]);
   	}
}



/*
** The \n was added in the functions below (and above) in order to not cause
** a buffer overflow in devices that can't deal with long line lengths.
** The DEC vt-340 doesn't need this, but Reflection 4, a vt-340 emulator
** for PC's under MS-DOS and Microsoft Windows does need it.
** Also needed for DECterm, a vt-340 emulator for use with Motif 1.0
** on Open/VMS.
** NGB 9/13/93
*/

TERM_PUBLIC void REGISmove(x,y)
unsigned int x,y;
{
	fprintf(outfile,"P[%d,%d]\n",x,REGISYLAST-y);
}


TERM_PUBLIC void REGISvector(x,y)
unsigned int x,y;
{
	fprintf(outfile,"v[]v[%d,%d]\n",x,REGISYLAST - y);
/* the initial v[] is needed to get the first pixel plotted */
}


/* put_text and text_angle by rjl */
TERM_PUBLIC void REGISput_text(x,y,str)
unsigned int x, y;
char *str;
{
	if (REGISang==1)
		REGISmove(x-REGISVCHAR/2-1,y);
	else
		REGISmove(x,y+REGISVCHAR/2-1);
	(void) putc('T',outfile); (void) putc('\'',outfile);
	while (*str) {
		(void) putc(*str,outfile);
		if (*str == '\'')
			(void) putc('\'',outfile);	/* send out another one */
		str++;
	}
	(void) putc('\'',outfile);
	(void) putc('\n',outfile);
}


TERM_PUBLIC int REGIStext_angle(ang)
int ang;
{
	REGISang = ang;
	if (ang == 1)
		fputs("T(D90,S1)\n",outfile);
	else
		fputs("T(D0,S1)\n",outfile);
	return TRUE;
}


TERM_PUBLIC void REGISreset()
{
	fprintf(outfile,"\033[2J\033[24;1H");
}

TERM_PUBLIC void REGISoptions()
{	int i=0;
   	struct value a;

   	if(!END_OF_COMMAND)
   	{	i=(int)real(const_express(&a));
   	}
   	REGIS16color= (i==16);
   	sprintf(term_options,"%s",REGIS16color?"16":"4");
}

#endif


#ifdef TERM_TABLE

TERM_TABLE_START(regis_driver)

      "regis", "REGIS graphics language",
	   REGISXMAX, REGISYMAX, REGISVCHAR, REGISHCHAR, 
	   REGISVTIC, REGISHTIC, REGISoptions, REGISinit, REGISreset, 
	   REGIStext, null_scale, REGISgraphics, REGISmove, REGISvector,
	   REGISlinetype, REGISput_text, REGIStext_angle, 
	   null_justify_text, line_and_point, do_arrow, set_font_null}

#undef LAST_TERM
#define LAST_TERM regis_driver
#endif
#endif /* TERM_PROTO_ONLY */


#ifdef TERM_HELP
START_HELP(regis)
"1 regis",
"?set terminal regis",
"?regis",
" The `regis` terminal device has the option of using 4 or 16 colors.  The",
" default is 4.  For example:",
"",
"        set term regis 16"
END_HELP(regis)
#endif

