/* GNUPLOT - grass.trm */
/*-
 * Copyright (C) 1992,1993. James Darrell McCauley
 *
 * 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:
 *  GRASS graphics driver
 *
 * AUTHOR
 *  James Darrell McCauley
 *
 * send your comments or suggestions to (grassp-list@moon.cecer.army.mil).
 *
 */
#include <stdio.h>
/* #include "gis.h" */ /* this causes conflicts with things in term.c */

#define GRASS_XMAX 1000
#define GRASS_YMAX 1000
#define GRASS_VCHAR 5
#define GRASS_HCHAR 5
#define GRASS_VTIC 3
#define GRASS_HTIC 3

#define PNT_SIZE 3
#define TYPE_DOT  -1
#define TYPE_X  0
#define TYPE_PLUS   1
#define TYPE_BOX    2
#define TYPE_DIAMOND    3	/* need type 4 and 5 */
#define TYPE_TRIANGLE 4
#define TYPE_OCTO 5
#define TYPE_ITRIANGLE 6
#define TYPE_FBOX 7
#define TYPE_FTRIANGLE 8
#define TYPE_FITRIANGLE 9
#define TYPE_FOCTO 10

int R__curx, R__cury;

int yoffset;
int xoffset;
int y_max;
int points_buf_x[PNT_SIZE*PNT_SIZE]; /* for filled point types */
int points_buf_y[PNT_SIZE*PNT_SIZE];

GRASS_move (x, y)
  int x, y;
{
  /* R_move_abs (xoffset+x, yoffset-y + y_max); */
  R_move_abs (xoffset+x, yoffset-y);
}

cont_abs (x, y)
  int x, y;
{
  /* R_cont_abs (xoffset+x, xoffset-y + y_max); */
  R_cont_abs (xoffset+x, yoffset-y);
}

GRASS_options ()
{
  options_null ();		/* no options to begin with */
}

GRASS_init ()
{
  char buff[128];
  char window_name[64];
  float size = 3.0;
  int backcolor;
  int dots_per_line;
  int top, b, l, r;
  int textcolor;
  struct termentry *t = &term_tbl[term];

  G_gisinit ("g.gnuplot");

  R_open_driver ();

  D_setup (0); 

  if (D_get_cur_wind (window_name))
    G_fatal_error ("No current window");

  if (D_set_cur_wind (window_name))
    G_fatal_error ("Current window not available");

  /* Set up the screen, conversions, and graphics */
  D_get_screen_window (&top, &b, &l, &r);
/*
  if (D_cell_draw_setup (top, b, l, r))
  {
    sprintf (buff, "Cannot use current window");
    G_fatal_error (buff);
  }
*/
  /* D_set_overlay_mode (1); */

  /* Figure out where to put text */

  R_set_window (top, b, l, r);
  t->xmax = r-l;
  t->ymax = b-top;
  xoffset=l;
  yoffset=b;

  dots_per_line = (int) (size / 100.0 * (float) (t->ymax));
  t->v_char = t->h_char = (int) (.8 * (float) dots_per_line);
  R_text_size (t->h_char, t->v_char);

  t->v_tic = t->h_tic = 4;

  y_max = t->ymax; /* kludge? */

  R__curx = R_screen_top ();
  R__cury = R_screen_bot () + yoffset;

  D_erase_window();
/*
fprintf(stderr,"**********************************************\n");
fprintf(stderr,"DIAGNOSTIC TERMINAL SETUP\n");
fprintf(stderr,"top = %d\tb = %d\tl = %d\tr = %d\n", top,b,l,r);
fprintf(stderr,"name = %s\n", t->name);
fprintf(stderr,"description = %s\n", t->description);
fprintf(stderr,"xmax = %d\t", (int)t->xmax);
fprintf(stderr,"ymax = %d\n", (int)t->ymax);
fprintf(stderr,"v_char = %d\t", (int)t->v_char);
fprintf(stderr,"h_char = %d\n", (int)t->h_char);
fprintf(stderr,"v_tic = %d\t", (int)t->v_tic);
fprintf(stderr,"h_tic = %d\n", (int)t->h_tic);
fprintf(stderr,"**********************************************\n\n");
*/
}

GRASS_reset ()
{
  R_standard_color (D_translate_color ("black"));
  /* D_erase_window(); /* don't clear after g.gnuplot is finished */ 
  R_flush ();
  R_stabilize ();
  R_close_driver ();
}

GRASS_graphics ()
{
  R_flush ();
  R_stabilize ();
  R_standard_color (D_translate_color ("black"));
  D_erase_window();
  return;
}

GRASS_text ()
{
  R_flush ();
  R_stabilize ();
  return;			/* device can't be used as a terminal */
}

GRASS_vector (x, y)
  int x, y;
{
  cont_abs (x, y);
  R_flush ();
  R_stabilize ();
}

GRASS_linetype (lt)
  int lt;
{

  while (lt > 10) lt-=10;

  if (lt <= -2)
    R_standard_color (D_translate_color ("gray"));
  else if (lt == -1)
    R_standard_color (D_translate_color ("white"));
  else if (lt == 0)
    R_standard_color (D_translate_color ("red"));
  else if (lt == 1)
    R_standard_color (D_translate_color ("green"));
  else if (lt == 2)
    R_standard_color (D_translate_color ("magenta"));
  else if (lt == 3)
    R_standard_color (D_translate_color ("brown"));
  else if (lt == 4)
    R_standard_color (D_translate_color ("orange"));
  else if (lt == 5)
    R_standard_color (D_translate_color ("yellow"));
  else if (lt == 6)
    R_standard_color (D_translate_color ("blue"));
  else if (lt == 7)
    R_standard_color (D_translate_color ("violet"));
  else if (lt == 8) 
    R_standard_color (D_translate_color ("indigo"));
  else if (lt == 9)
    R_standard_color (D_translate_color ("gray"));
  else /* if (lt == 10) */
    R_standard_color (D_translate_color ("white"));
  R_flush ();
  R_stabilize ();
  return;
}

/* originally /usr/grass4/src/display/d.label/cmd/label.c */

GRASS_put_text (x, y, str)
  int x, y;
  char *str;
{

  if (strlen (str) == 0)
    return;

  GRASS_move (x, y);
  /* R_standard_color (D_translate_color ("white")); */
  R_text (str);
  R_flush ();
  R_stabilize ();
}

int GRASS_text_angle (ang)
{
  return FALSE;			/* GRASS cannot rotate text */
}

int GRASS_justify_text (mode)
  int mode;
{
  return (FALSE);		/* don't mess with this now */
}


GRASS_point (x, y, point)
{
  switch (point)
  {
  case TYPE_DOT:
    draw_points_dot (x, y);
    break;
  case TYPE_X:
    draw_points_x (x, y);
    break;
  case TYPE_PLUS:
    draw_points_plus (x, y);
    break;
  case TYPE_BOX:
    draw_points_box (x, y);
    break;
  case TYPE_DIAMOND:
    draw_points_diamond (x, y);
    break;
  case TYPE_TRIANGLE:
    draw_points_triangle (x, y);
    break;
  case TYPE_OCTO:
    draw_points_octo (x, y);
    break;
  case TYPE_ITRIANGLE:
    draw_points_itriangle (x, y);
    break;
  case TYPE_FBOX:
    draw_points_fbox (x, y);
    break;
  case TYPE_FTRIANGLE:
    draw_points_ftriangle (x, y);
    break;
  case TYPE_FITRIANGLE:
    draw_points_fitriangle (x, y);
    break;
  case TYPE_FOCTO:
    draw_points_focto (x, y);
    break;
  }
}

/* modified from /usr/grass4/src/display/d.points/cmd/main.c */

draw_points_dot (x, y)
  int x, y;
{
  GRASS_move (x, y);
  cont_abs (x, y);
}

draw_points_diamond (x, y)
  int x, y;
{
  GRASS_move (x, y + PNT_SIZE);
  GRASS_vector (x + PNT_SIZE, y);
  GRASS_vector (x, y - PNT_SIZE);
  GRASS_vector (x - PNT_SIZE, y);
  GRASS_vector (x, y + PNT_SIZE);
}

draw_points_box (x, y)
  int x, y;
{
  GRASS_move (x - PNT_SIZE, y - PNT_SIZE);
  GRASS_vector (x - PNT_SIZE, y + PNT_SIZE);
  GRASS_vector (x + PNT_SIZE, y + PNT_SIZE);
  GRASS_vector (x + PNT_SIZE, y - PNT_SIZE);
  GRASS_vector (x - PNT_SIZE, y - PNT_SIZE);
}

draw_points_fbox (x, y)
  int x, y;
{
  points_buf_x[0] = xoffset + x - PNT_SIZE; 
  points_buf_y[0]= yoffset - (y + PNT_SIZE);
  points_buf_x[1] = xoffset + x + PNT_SIZE; 
  points_buf_y[1]= yoffset - (y + PNT_SIZE);
  points_buf_x[2] = xoffset + x + PNT_SIZE; 
  points_buf_y[2]= yoffset - (y - PNT_SIZE);
  points_buf_x[3] = xoffset + x - PNT_SIZE; 
  points_buf_y[3]= yoffset - (y - PNT_SIZE);
  R_polygon_abs(points_buf_x, points_buf_y, 4 );
}

draw_points_itriangle (x, y)
  int x, y;
{
  GRASS_move (x - PNT_SIZE, y + PNT_SIZE);
  GRASS_vector (x + PNT_SIZE, y + PNT_SIZE);
  GRASS_vector (x , y - PNT_SIZE);
  GRASS_vector (x - PNT_SIZE, y + PNT_SIZE);
}

draw_points_fitriangle (x, y)
  int x, y;
{
  points_buf_x[0] = xoffset + x + PNT_SIZE; 
  points_buf_y[0] = yoffset - (y + PNT_SIZE);
  points_buf_x[1] = xoffset + x ;           
  points_buf_y[1] = yoffset - (y - PNT_SIZE);
  points_buf_x[2] = xoffset + x - PNT_SIZE; 
  points_buf_y[2] = yoffset - (y + PNT_SIZE);
  R_polygon_abs(points_buf_x, points_buf_y, 3 );
}

draw_points_triangle (x, y)
  int x, y;
{
  GRASS_move (x - PNT_SIZE, y - PNT_SIZE);
  GRASS_vector (x , y + PNT_SIZE);
  GRASS_vector (x + PNT_SIZE, y - PNT_SIZE);
  GRASS_vector (x - PNT_SIZE, y - PNT_SIZE);
}

draw_points_ftriangle (x, y)
  int x, y;
{
  points_buf_x[0] = xoffset + x;            
  points_buf_y[0]= yoffset - (y + PNT_SIZE);
  points_buf_x[1] = xoffset + x + PNT_SIZE; 
  points_buf_y[1]= yoffset - (y - PNT_SIZE);
  points_buf_x[2] = xoffset + x - PNT_SIZE; 
  points_buf_y[2]= yoffset - (y - PNT_SIZE);
  R_polygon_abs(points_buf_x, points_buf_y, 3 );
}

draw_points_plus (x, y)
  int x, y;
{
  GRASS_move (x - PNT_SIZE, y);
  GRASS_vector (x + PNT_SIZE, y);
  GRASS_move (x, y - PNT_SIZE);
  GRASS_vector (x, y + PNT_SIZE);
}

/* depends on PNT_SIZE */
draw_points_octo (x, y)
  int x, y;
{
  /* CCW */
  GRASS_move (x - (int) (PNT_SIZE/3), y - PNT_SIZE);   /* 1 */
  GRASS_vector (x + (int) (PNT_SIZE/3), y - PNT_SIZE); /* 2 */
  GRASS_vector (x + PNT_SIZE, y - (int) (PNT_SIZE/3)); /* 3 */
  GRASS_vector (x + PNT_SIZE, y + (int) (PNT_SIZE/3)); /* 4 */
  GRASS_vector (x + (int) (PNT_SIZE/3), y + PNT_SIZE); /* 5 */
  GRASS_vector (x - (int) (PNT_SIZE/3), y + PNT_SIZE); /* 6 */
  GRASS_vector (x - PNT_SIZE, y + (int) (PNT_SIZE/3)); /* 7 */
  GRASS_vector (x - PNT_SIZE, y - (int) (PNT_SIZE/3)); /* 8 */
  GRASS_vector (x - (int) (PNT_SIZE/3), y - PNT_SIZE); /* 1 */
}

/* depends on PNT_SIZE */
draw_points_focto (x, y)
  int x, y;
{
  /* CCW */
  points_buf_x[0] = xoffset + x + (int) (PNT_SIZE/3);
  points_buf_y[0] = yoffset - (y - PNT_SIZE); 
  points_buf_x[1] = xoffset + x + PNT_SIZE;
  points_buf_y[1] = yoffset - (y - (int) (PNT_SIZE/3)); 
  points_buf_x[2] = xoffset + x + PNT_SIZE;
  points_buf_y[2] = yoffset - (y + (int) (PNT_SIZE/3));
  points_buf_x[3] = xoffset + x + (int) (PNT_SIZE/3);
  points_buf_y[3] = yoffset - (y + PNT_SIZE); 
  points_buf_x[4] = xoffset + x - (int) (PNT_SIZE/3);
  points_buf_y[4] = yoffset - (y + PNT_SIZE);
  points_buf_x[5] = xoffset + x - PNT_SIZE;
  points_buf_y[5] = yoffset - (y + (int) (PNT_SIZE/3)); 
  points_buf_x[6] = xoffset + x - PNT_SIZE;
  points_buf_y[6] = yoffset - (y - (int) (PNT_SIZE/3));
  points_buf_x[7] = xoffset + x - (int) (PNT_SIZE/3);
  points_buf_y[7] = yoffset - (y - PNT_SIZE); 
  R_polygon_abs(points_buf_x, points_buf_y, 8 );
}

draw_points_x (x, y)
{
  GRASS_move (x - PNT_SIZE, y - PNT_SIZE);
  GRASS_vector (x + PNT_SIZE, y + PNT_SIZE);
  GRASS_move (x + PNT_SIZE, y - PNT_SIZE);
  GRASS_vector (x - PNT_SIZE, y + PNT_SIZE);
}


/* need to fix */
GRASS_arrow (sx, sy, ex, ey, head)
  int sx, sy, ex, ey, head;
{
  do_arrow (sx, sy, ex, ey, 1);
  return;
}


