/* Hello, Emacs, this is -*-C-*-
 * $Id: vws.trm,v 1.19 2007/10/16 21:19:45 sfeam Exp $
 *
 */

/* GNUPLOT - vws.trm */

/*[
 * Copyright 1990 - 1993, 1998, 2004
 *
 * 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 complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 * This file is included by ../term.c.
 *
 * AUTHORS
 *   Walter Speth
 *           BITNET: SPETH@DBNPIB5
 *
 * send your comments or suggestions to (gnuplot-info@lists.sourceforge.net).
 *
 */
/*
 * adapted to the new terminal layout by Stefan Bodewig (Dec. 1995)
 * adapted to VAX VMS 4.7, VAX C 2.4 and activated the INTENSITY and COLOR
 * monitor features (Lucas P. Hart, June 1997)
 */

#include "driver.h"

#ifdef TERM_REGISTER
register_term(vws)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void VWS_init __PROTO((void));
TERM_PUBLIC void VWS_reset __PROTO((void));
TERM_PUBLIC void VWS_graphics __PROTO((void));
TERM_PUBLIC void VWS_text __PROTO((void));
TERM_PUBLIC void VWS_move __PROTO((unsigned int x, unsigned int y));
TERM_PUBLIC void VWS_vector __PROTO((unsigned int x, unsigned int y));
TERM_PUBLIC void VWS_linetype __PROTO((int lt));
TERM_PUBLIC void VWS_put_text __PROTO((unsigned int x, unsigned int y, const char *str));
TERM_PUBLIC int VWS_text_angle __PROTO((int ang));
TERM_PUBLIC int VWS_justify_text __PROTO((enum JUSTIFY mode));
TERM_PUBLIC void VWS_point __PROTO((unsigned int x, unsigned int y, int point));
TERM_PUBLIC void VWS_arrow __PROTO((unsigned int sx, unsigned int sy,
				    unsigned int ex, unsigned int ey, int head));

#define VWS_XMAX 1024
#define VWS_YMAX 780		/* VAXstation 2000 is 864 */
#define VWS_VCHAR 25
#define VWS_HCHAR 15
#define VWS_VTIC 10
#define VWS_HTIC 10
#endif /* TERM_PROTO */

#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY
void VWS_resize_ast __PROTO((void));

#include <ssdef.h>
/* */
#include <descrip.h>

#include <uisentry.h>
#include <uisusrdef.h>

#define DEFAULT_ATTR 0
#define OVER_ATTR 1
#define ERAS_ATTR 2
#define BIS_ATTR 3
#define COLOR_ATTR 4
#define TEXT_ATTR 5
#define LINE_ATTR 6
#define BACK_ATTR 7

float current_x, current_y;

int vd_id, wd_id;
int vcm_id, ws_type;

static $DESCRIPTOR(ws_devname, "SYS$WORKSTATION");
static $DESCRIPTOR(vd_title, "gnuplot");

static float x0, y0, xsiz, ysiz, wc_xmin_new, wc_ymin_new, wc_xmax_new, wc_ymax_new, wc_xmin,
wc_ymin, wc_xmax, wc_ymax, vd_width, vd_height;

void
VWS_resize_ast()
{
    uis$resize_window(&vd_id, &wd_id, &x0, &y0, &xsiz, &ysiz,
		      &wc_xmin, &wc_ymin, &wc_xmax, &wc_ymax);
    replotrequest();
}

/****************************************************************************/
TERM_PUBLIC void
VWS_init()
{
    int i;
    int cattr;
    $DESCRIPTOR(a_font, "DTABER0003WK00PG0001UZZZZ02A000");
    const int i0 = 0, i1 = 1, i2 = 2, i3 = 3, i4 = 4, i5 = 5, i6 = 6, i7 = 7, i8 = 8;
    const float p0 = 0.0, p1 = 0.1, p2 = 0.2, p3 = 0.3, p4 = 0.4;
    const float p5 = 0.5, p6 = 0.6, p7 = 0.7, p8 = 0.8, p9 = 0.9;
    const int n1 = 0xFFFFFFFF;
    const int uis$c_mode_over = UIS$C_MODE_OVER;
    const int uis$c_mode_eras = UIS$C_MODE_ERAS;
    const int uis$c_mode_bis = UIS$C_MODE_BIS;
    const float vws_hchar = (float) VWS_HCHAR;
    const float vws_vchar = (float) VWS_VCHAR;
    const int default_attr = DEFAULT_ATTR;
    const int over_attr = OVER_ATTR;
    const int eras_attr = ERAS_ATTR;
    const int bis_attr = BIS_ATTR;
    const int color_attr = COLOR_ATTR;
    const int text_attr = TEXT_ATTR;
    const int line_attr = LINE_ATTR;
    const int back_attr = BACK_ATTR;

/* lph:  the similar notation may lead to some confusion
 * attributes are grouped in "attribute block" data structures
 * which are referenced by block number
 * attribute block 0 contains default attribute settings
 *
 * To customize settings, a function will read the source
 * attribute block, set the particular attribute, and
 * write to the destination attribute block.
 * Both the block number and attribute are integers, so it
 * is easy to confuse an attribute and an attribute blcok
 */

/*  lph:  Color map corresponding to an example in
 *  MicroVMS Graphics Progamming Guide (V3.0) */

    const float r_map[8] = { 0.40, 0.50, 0.50, 0.00, 0.25, 0.90, 0.80, 0.35 };
    const float g_map[8] = { 0.30, 0.50, 0.25, 0.70, 0.25, 0.50, 0.30, 0.65 };
    const float b_map[8] = { 0.00, 0.50, 0.50, 0.30, 0.90, 0.00, 0.00, 0.95 };

/* the array format is easier to customize */

    vd_width = 14;
    vd_height = 10;		/* aspect sqrt(2) as DIN A paper */

    wc_xmin = 0.0;
    wc_ymin = 0.0;
    wc_xmax = (float) VWS_XMAX;
    wc_ymax = (float) VWS_YMAX;

    vcm_id = uis$create_color_map(&i8);

    vd_id = uis$create_display(&wc_xmin, &wc_ymin, &wc_xmax, &wc_ymax,
			       &vd_width, &vd_height, &vcm_id);
    uis$get_hw_color_info(&ws_devname, &ws_type);

/* lph: ws's color table for monochrome  */
/* by default,  color table index 0 is backgound, and 1 is foregound */
/* a color {index into the color table} is assigned to the active line */
/* in VWS_linetype */

    if (ws_type == UIS$C_DEV_MONO) {
	uis$set_color(&vd_id, &i0, &p5, &p5, &p5);
	uis$set_color(&vd_id, &i1, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i2, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i3, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i4, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i5, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i6, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i7, &p0, &p0, &p0);
    } else if (ws_type == UIS$C_DEV_INTENSITY) {
        /* Intensity scale, untested */
	uis$set_color(&vd_id, &i2, &p2, &p2, &p2);
	uis$set_color(&vd_id, &i3, &p3, &p3, &p3);
	uis$set_color(&vd_id, &i4, &p4, &p4, &p4);
	uis$set_color(&vd_id, &i5, &p5, &p5, &p5);
	uis$set_color(&vd_id, &i6, &p6, &p6, &p6);
	uis$set_color(&vd_id, &i7, &p7, &p7, &p7);
    }
    /* ws: perhaps better for color terms (which I do not have) */

    else if (ws_type == UIS$C_DEV_COLOR) {
/* */
	uis$set_color(&vd_id, &i0, &p0, &p0, &p0);
	uis$set_color(&vd_id, &i1, &p9, &p0, &p0);
	uis$set_color(&vd_id, &i2, &p0, &p9, &p0);
	uis$set_color(&vd_id, &i3, &p9, &p9, &p0);
	uis$set_color(&vd_id, &i4, &p0, &p0, &p9);
	uis$set_color(&vd_id, &i5, &p9, &p0, &p9);
	uis$set_color(&vd_id, &i6, &p0, &p9, &p9);
	uis$set_color(&vd_id, &i7, &p9, &p9, &p9);
	/*
	   * lph: Another way to set up the color table is to use arrays,
	   * which simplifies customizing the appearance.
	   *
	   uis$set_colors(&vd_id, &i0, &i8, &r_map, &g_map, &b_map);
	 */
    }
    uis$disable_display_list(&vd_id);

    wd_id = uis$create_window(&vd_id, &ws_devname, &vd_title,
			      &wc_xmin, &wc_ymin, &wc_xmax, &wc_ymax,
			      &vd_width, &vd_height);

    uis$set_resize_ast(&vd_id, &wd_id, &VWS_resize_ast, &i0,
		       &x0, &y0, &xsiz, &ysiz,
		       &wc_xmin_new, &wc_ymin_new, &wc_xmax_new, &wc_ymax_new);
/*
 * lph: This sets up three different attribute blocks, each having a different
 * mode, but they are never utilized and it is not clear when they might
 * be of use.
 * The attribute blocks used for writing are line_attr, text_attr.
 *
  uis$set_writing_mode(&vd_id, &default_attr, &over_attr, &uis$c_mode_over);
  uis$set_writing_mode(&vd_id, &default_attr, &eras_attr, &uis$c_mode_eras);
  uis$set_writing_mode(&vd_id, &default_attr, &bis_attr, &uis$c_mode_bis);
 */

/* lph:  this was commented out;  it changes the COLOR_ATTR BLOCK and
 * the following BLOCKS
 *
if (ws_type >=  UIS$C_DEV_INTENSITY) {
  for (i=0; i<8; i++) {
  cattr = COLOR_ATTR+i;
  uis$set_writing_index(&vd_id, &default_attr, &cattr, &i);
  }
  }
 *
  */

/* lph: default value for the background index into the virtual color
 * table is 0, and the back_attr block is never used
 *
  uis$set_background_index(&vd_id, &default_attr, &back_attr, &i0);
 *
 * the rest look OK, setting up the two attribute blocks
 */
    uis$set_writing_mode(&vd_id, &default_attr, &text_attr, &uis$c_mode_over);
    uis$set_font(&vd_id, &text_attr, &text_attr, &a_font);
    uis$set_char_size(&vd_id, &text_attr, &text_attr, &i0, &vws_hchar, &vws_vchar);
    uis$set_line_style(&vd_id, &line_attr, &line_attr, &n1);
}

/****************************************************************************/

TERM_PUBLIC void
VWS_reset()
{
    uis$delete_display(&vd_id);
}

/****************************************************************************/
TERM_PUBLIC void
VWS_graphics()
{
    uis$erase(&vd_id, &wc_xmin, &wc_ymin, &wc_xmax, &wc_ymax);
}

/****************************************************************************/
TERM_PUBLIC void
VWS_text()
{
}

/****************************************************************************/
TERM_PUBLIC void
VWS_move(unsigned int x, unsigned int y)
{
    current_x = (float) (x);
    current_y = (float) (y);
}

/****************************************************************************/
TERM_PUBLIC void
VWS_vector(unsigned int x, unsigned int y)
{
    int col;
    int line_attr = LINE_ATTR;
    float fx, fy;
    fx = (float) x;
    fy = (float) y;

    uis$line(&vd_id, &line_attr, &current_x, &current_y, &fx, &fy);
    VWS_move(x, y);
}

/****************************************************************************/
TERM_PUBLIC void
VWS_linetype(int lt)
{
    const int n1 = 0xFFFFFFFF;
    /* indices into the color map */
    const int i[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    const float p15 = 2.0, p20 = 3.0, p10 = 1.0;
    long int lstyle[9] =
    {0xffffffff,
     0Xff00ff00,
     0xffffff00,
     0xffff0000,
     0xf0f0f0f0,
     0Xfff0fff0,
     0xf000f000,
     0xa5a5a5af,
     0xf00f00f0
    };
    int line_attr = LINE_ATTR;
    /* use multiplier of normal line width; default is width in pixels */
    const int uis$c_width_world = UIS$C_WIDTH_WORLD;

    if (lt < 0) {
	switch (lt) {
	case LT_AXIS:		/* used for axis */
	    uis$set_line_style(&vd_id, &line_attr, &line_attr, &n1);
	    uis$set_line_width(&vd_id, &line_attr, &line_attr, &p15,
			       &uis$c_width_world);
	    if (ws_type >= UIS$C_DEV_INTENSITY) {
		uis$set_writing_index(&vd_id, &line_attr, &line_attr, &i[1]);
	    }
	    break;
	case LT_BLACK:		/* the borders */
	    uis$set_line_style(&vd_id, &line_attr, &line_attr, &n1);
	    uis$set_line_width(&vd_id, &line_attr, &line_attr, &p20,
			       &uis$c_width_world);
	    if (ws_type >= UIS$C_DEV_INTENSITY) {
		uis$set_writing_index(&vd_id, &line_attr, &line_attr, &i[1]);
	    }
	    break;
	default:		/* else a thick line ? */
	    uis$set_line_style(&vd_id, &line_attr, &line_attr, &n1);
	    uis$set_line_width(&vd_id, &line_attr, &line_attr, &p20,
			       &uis$c_width_world);
	    if (ws_type >= UIS$C_DEV_INTENSITY) {
		uis$set_writing_index(&vd_id, &line_attr, &line_attr,
				      &i[1 + ((-lt) % 7)]);
	    }
	    break;
	}
    } else {
/* assign the color index (excluding the background index, 0*/
	if (ws_type >= UIS$C_DEV_INTENSITY) {
	    uis$set_writing_index(&vd_id, &line_attr, &line_attr, &i[1 + lt % 7]);
	}
/* assign the line style
    uis$set_line_style(&vd_id,&line_attr,&line_attr,&lstyle[lt % 8]);
 * ws: makes part of curve disappear on my workstation
 * lph: also observed w/ VS2000 and 4 plane graphics coprocesssor; line
 * types change appropriately, but has problems as pixels/line segment
 * decreases.  Better with full screen display and "set samples" smaller
 * but start out with only solid lines
  */
	uis$set_line_style(&vd_id, &line_attr, &line_attr, &n1);

	uis$set_line_width(&vd_id, &line_attr, &line_attr, &p10,
			   &uis$c_width_world);
    }
}

/****************************************************************************/
static int justify_mode = CENTRE, up;

TERM_PUBLIC void
VWS_put_text(unsigned int x, unsigned int y, const char *str)
{
    float fx, fy, thih, twid;
    int text_attr = TEXT_ATTR;

    /* uis$text parameter is descriptor string not character string */

    struct dsc$descriptor_s textline =
    {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, ""};


    textline.dsc$a_pointer = str;
    textline.dsc$w_length = strlen(textline.dsc$a_pointer);
    uis$measure_text(&vd_id, &text_attr, &textline, &twid, &thih);
    fx = (float) x;
    fy = (float) y;

    switch (justify_mode) {
    case LEFT:
	fy += thih / 2.;
	break;
    case RIGHT:
	fy += thih / 2.;
	fx -= twid;
	break;

    case CENTRE:
	fy += thih / 2.;
	fx -= twid / 2;
	break;
    };

    uis$text(&vd_id, &text_attr, &textline, &fx, &fy);
    /* write to Example Viewport window */


}
/****************************************************************************/
TERM_PUBLIC int
VWS_text_angle(int ang)
{
    float degrees;
    int text_attr = TEXT_ATTR;
    up = (ang ? 1 : 0);
    degrees = 90. * up;
    uis$set_text_slope(&vd_id, &text_attr, &text_attr, &degrees);
    return TRUE;
}
/****************************************************************************/
TERM_PUBLIC int
VWS_justify_text(enum JUSTIFY mode)
{
    justify_mode = mode;
    return TRUE;
}
/****************************************************************************/
TERM_PUBLIC void
VWS_point(unsigned int x, unsigned int y, int point)
{
    do_point(x, y, point);
}

/****************************************************************************/
TERM_PUBLIC void
VWS_arrow(
    unsigned int sx, unsigned int sy,
    unsigned int ex, unsigned int ey,
    int head)
{
    do_arrow(sx, sy, ex, ey, head);
}

#endif /* TERM_BODY */

#ifdef TERM_TABLE

TERM_TABLE_START(vws_driver)
    "VWS", "VAX Windowing System (UIS)",
    VWS_XMAX, VWS_YMAX, VWS_VCHAR, VWS_HCHAR,
    VWS_VTIC, VWS_HTIC, options_null, VWS_init, VWS_reset,
    VWS_text, null_scale, VWS_graphics, VWS_move, VWS_vector,
    VWS_linetype, VWS_put_text, VWS_text_angle,
    VWS_justify_text, do_point, do_arrow, set_font_null
TERM_TABLE_END(vws_driver)

#undef LAST_TERM
#define LAST_TERM vws_driver

#endif /* TERM_TABLE */
#endif /* TERM_PROTO_ONLY */

#ifdef TERM_HELP
START_HELP(VWS)
"1 VWS",
"?commands set terminal VWS",
"?set terminal VWS",
"?set term VWS",
"?terminal VWS",
"?term VWS",
"?VWS",
" The `VWS` terminal driver supports the VAX Windowing System.  It has",
" no options.  It will sense the display type (monochrome, gray scale,",
" or color.)  All line styles are plotted as solid lines."
END_HELP(VWS)
#endif /* TERM_HELP */
