/*
 *
 * This program is the CONFIDENTIAL and PROPRIETARY property of
 * Rialto Software. Any unauthorized use, reproduction or transfer
 * of this program is strictly prohibited.
 *
 * Copyright (c) 1992, 1993 Rialto Software
 *
 * (Subject to limited distribution and restricted disclosure only.)
 * *** ALL RIGHTS RESERVED ***
 *
 * 4179 Woodland Drive
 * Suite 15B
 * Ann Arbor, MI 48103
 *
 * CommonGraphics(TM) Version 2.2
 * CommonGeometry(TM) Version 1.0
 *
 * June 4, 1992 14:30
 *
*/

#define TRUE                      ((bool)          1)
#define FALSE                     ((bool)          0)

#define C_MAX_COLORS                              16
#define C_MAX_TEXT_CHARS                          80
#define C_RESPONSE_STRING_SIZE                   256
#define C_STRUCTURE_NAME_SIZE                     20
#define C_VIEW_NAME_SIZE                          20

typedef unsigned char        bool;

typedef float                real;

typedef real                 t_world_coord[2];

typedef real                 t_ndc_coord[2];

typedef int                  t_device_coord[2];

typedef real                 t_xform[3][2];

typedef unsigned long        t_name_set;

typedef enum {BLACK,
              WHITE,
              RED,  
              GREEN,
              BLUE,
              YELLOW,
              CYAN,
              MAGENTA,
              DARK_GRAY,
              LIGHT_GRAY,
              LIGHT_RED,
              LIGHT_GREEN,
              LIGHT_BLUE,
              BROWN,
              LIGHT_CYAN,
              LIGHT_MAGENTA} t_color;

typedef enum {STRING_TEXT,
              STROKE_TEXT}   t_text_style;

typedef enum {SOLID,
              SHORT_DASH,
              MEDIUM_DASH,
              LONG_DASH,
              PHANTOM,
              CENTERLINE,
              DOT_DASH,
              EXTENDED}      t_line_style;

typedef enum {FILL,
              EMPTY}         t_interior_style;

typedef enum {PLUS,
              ASTERISK,
              CIRCLE_CROSS,
              FILLED_BOX,
              FILLED_CIRCLE,
              NO_MARKER}     t_marker_style;

typedef enum {NO_BORDER,
              BOX_BORDER}    t_view_border_style;

typedef enum {MARKER,
              LINE,
              CIRCLE,
              ARC,
              RECTANGLE,
              ELLIPSE,
              ARROW,
              POLYLINE,
              BEZIER,
              BSPLINE,
              TEXT,
              STRUCTURE_REF} t_primitive;

typedef enum {GEOMETRY,
              NOTE,
              REFERENCE}     t_class;

typedef enum {LOCATE,
              PICK,
              SCREEN,
              STRING}        t_request;

typedef enum {FIRST,
              NEXT}          t_selection;

typedef enum {ON,
              OFF}           t_toggle;

struct t_device
{
   int                       height;
   int                       width;
   real                      aspect_ratio;
   int                       char_height;
   int                       char_width;
   int                       number_colors;
};

struct t_group_elem
{
   char                      structure_name[C_STRUCTURE_NAME_SIZE];
   t_class                   class;
   t_primitive               element_type;
   long                      element_id;
};

struct t_attributes
{
   t_color                   color;
   t_color                   text_color;
   t_line_style              line_style;
   int                       line_width;
   t_interior_style          interior_style;
   long                      element_id;
};

struct t_element
{
   t_primitive               type;
   long                      id;
   t_color                   color;
   t_line_style              style;
   int                       width;
   t_interior_style          interior_style;
   union
   {
      struct
      {
          t_marker_style         marker_style;             /* marker                              */
          t_world_coord          point;
      } marker;

      struct                                               /* line                                */
      {
         t_world_coord       start_pt;
         t_world_coord       end_pt;
      } line;

      struct                                               /* circle                              */
      {
         t_world_coord       center;
         real                radius;
      } circle;

      struct                                               /* arc                                 */
      {
         t_world_coord       center;
         real                radius;
         real                start_angle;
         real                delta_angle;
      } arc;

      struct                                               /* rectangle                           */
      {
         t_world_coord       center;
         real                length;
         real                width;
         real                corner_radius;
         real                angle;
      } rect;

      struct                                               /* ellipse                             */
      {
         t_world_coord       center;
         real                x_radius;
         real                y_radius;
         real                angle;
      } ellipse;

      struct                                               /* arrow                               */
      {
         t_world_coord       pt;
         real                height;
         real                width;
         real                angle;
      } arrow;

      struct                                               /* polyline, bezier and bspline        */
      {                                                    
         t_marker_style      marker_style;                 /* marker and connect only used with   */
         bool                connect;                      /* polyline.                           */
         int                 num_pts;
         t_world_coord       *pt_head;
      } multi_pt;

      struct                                               /* text                                */
      {
         t_world_coord       text_ll;
         char                chars[C_MAX_TEXT_CHARS];
         t_text_style        style;
         real                height;
         real                width;
         real                angle;
      } text;

      struct                                               /* structure reference                 */
      {
         char                name[C_STRUCTURE_NAME_SIZE];
         t_xform             transform;
      } structure_ref;
   } v;
};

struct t_request_buffer
{
   t_request                 type;
   char                      view_name[C_VIEW_NAME_SIZE];
   union
   {
      t_world_coord          location;                     /* locator                             */

      struct                                               /* pick                                */
      {
         char                structure_name[C_STRUCTURE_NAME_SIZE];
         t_class             class;
         t_world_coord       location;
         struct t_element    element;
      } pick;

      t_ndc_coord            ndc_pt;                       /* screen                              */

      char                   str[C_RESPONSE_STRING_SIZE];  /* string                              */
   } v;
};

/*  CommonGraphics  */

extern void cg_initialize (void);
extern void cg_terminate (void);
extern void cg_dump (void);

extern void cg_set_element_identifier (long);
extern void cg_set_interior_style (t_interior_style);
extern void cg_set_line_color (t_color);
extern void cg_set_line_style (t_line_style);
extern void cg_set_line_width (int);
extern void cg_set_text_color (t_color);

extern void cg_draw_arc (t_world_coord, real, real, real);
extern void cg_draw_arrow (t_world_coord, real, real, real);
extern void cg_draw_circle (t_world_coord, real);
extern void cg_draw_ellipse (t_world_coord, real, real, real);
extern void cg_draw_line (t_world_coord, t_world_coord);
extern void cg_draw_marker (t_marker_style, t_world_coord);
extern void cg_draw_polyline (t_marker_style, bool, int, t_world_coord[]);
extern void cg_draw_bspline (int, t_world_coord[]);
extern void cg_draw_rectangle (t_world_coord, real, real, real, real);
extern void cg_draw_text (t_text_style, t_world_coord, char *, real, real,
                          real);

extern void cg_open_view (char *, t_ndc_coord, t_ndc_coord, t_color,
                          t_view_border_style);
extern void cg_delete_view (char *);
extern void cg_delete_all_views (void);
extern void cg_close_view (char *);
extern void cg_set_window (char *, real, real);
extern void cg_pan (char *, t_world_coord);
extern void cg_zoom (char *, t_world_coord, t_world_coord);
extern void cg_post_structure_to_view (char *, char *);
extern void cg_post_all_to_view (char *);
extern void cg_post_to_all_view (char *);
extern void cg_unpost_structure_from_view (char *, char *);
extern void cg_unpost_all_from_view (char *);
extern void cg_unpost_from_all_view (char *);
extern void cg_set_class_visibility (char *, t_class, t_toggle);

extern void cg_open_structure (char *);
extern void cg_open_non_retained_structure (char *);
extern void cg_close_structure (char *);
extern void cg_delete_structure (char *);
extern void cg_delete_all_structures (void);
extern void cg_empty_structure (char *);
extern void cg_change_structure_name (char *, char *);
extern void cg_set_structure_pickability (char *, t_toggle);
extern void cg_set_structure_visibility (char *, t_toggle);
extern void cg_set_structure_highlight (char *, t_toggle);
extern void cg_reference_structure (char *, t_world_coord, real, real);

extern bool cg_inquire_class_visibility (char *, t_class, t_toggle *);
extern bool cg_inquire_name_set (t_name_set, int);
extern bool cg_inquire_open_structure (char *);
extern bool cg_inquire_pick_list (t_selection, struct t_group_elem *);
extern void cg_inquire_primitive_attributes (struct t_attributes *);
extern void cg_inquire_string_response (t_ndc_coord, t_color *, t_color *);
extern bool cg_inquire_structure_exists (char *);
extern bool cg_inquire_structure_visibility (char *, t_toggle *);
extern bool cg_inquire_structures (t_selection, char *);
extern bool cg_inquire_structures_posted_to_view (t_selection, char *, char *);
extern bool cg_inquire_view_attributes (char *, t_color *, t_view_border_style *);
extern bool cg_inquire_view_exists (char *);
extern bool cg_inquire_view_transformation (char *, t_ndc_coord, t_ndc_coord,
                                            t_world_coord, t_world_coord);
extern bool cg_inquire_views (t_selection, char *);
extern void cg_inquire_workstation (struct t_device *);

extern void cg_delete_element (char *, long);
extern void cg_delete_element_range (char *, long, long);
extern void cg_change_reference_name (char *, long, char *, char *);
extern void cg_change_reference_abs (char *, char *, long, t_world_coord,
                                     real, real);
extern void cg_change_reference_rel (char *, char *, long, t_world_coord,
                                     real, real);

extern void cg_add_name_to_set (t_name_set *, int);
extern void cg_add_all_names_to_set (t_name_set *);
extern void cg_remove_name_from_set (t_name_set *, int);
extern void cg_set_string_response (t_ndc_coord, t_color, t_color);
extern void cg_request_input (t_name_set, t_name_set, char *,
                              struct t_request_buffer *);

/*  CommonGeometry  */

extern void cg_point_around_arc (t_world_coord, real, real, t_world_coord);
extern void cg_point_along_line (t_world_coord, t_world_coord, real,
                                 t_world_coord, t_world_coord);
extern void cg_point_midpoint_line (t_world_coord, t_world_coord,
                                   t_world_coord);
extern void cg_line_parallel (t_world_coord, t_world_coord, real,
                              t_world_coord, t_world_coord);
extern void cg_line_perpendicular (t_world_coord, t_world_coord,
                                   t_world_coord, t_world_coord);
extern void cg_line_pt_slope_distance (t_world_coord, real, real,
                                       t_world_coord);
extern void cg_line_pt_angle_distance (t_world_coord, real, real,
                                       t_world_coord);
extern bool cg_line_bisector (t_world_coord, t_world_coord, t_world_coord,
                              t_world_coord, real, t_world_coord,
                              t_world_coord);
extern void cg_line_tan_arc (t_world_coord, t_world_coord, real,
                             t_world_coord, t_world_coord);
extern void cg_line_tan_2arcs (t_world_coord, real, t_world_coord, real,
                               t_world_coord, t_world_coord, t_world_coord,
                               t_world_coord);
extern bool cg_circle_3pts (t_world_coord, t_world_coord, t_world_coord,
                            t_world_coord, real *);
extern bool cg_circle_tan_line_pt_radius (t_world_coord, t_world_coord,
                                          t_world_coord, real, t_world_coord,
                                          t_world_coord);
extern bool cg_circle_tan_line_2pts (t_world_coord, t_world_coord,
                                     t_world_coord, t_world_coord,
                                     t_world_coord, t_world_coord, real *);
extern bool cg_circle_tan_2lines_radius (t_world_coord, t_world_coord,
                                         t_world_coord, t_world_coord, real,
                                         t_world_coord, t_world_coord);
extern bool cg_circle_tan_2lines_pt (t_world_coord, t_world_coord,
                                     t_world_coord, t_world_coord,
                                     t_world_coord, t_world_coord,
                                     t_world_coord, real *);
extern bool cg_circle_tan_3lines (t_world_coord, t_world_coord, t_world_coord,
                                  t_world_coord, real *);
extern bool cg_circle_tan_2arcs_radius (t_world_coord, real, t_world_coord,
                                        real, real, t_world_coord,
                                        t_world_coord);
extern bool cg_circle_tan_2arcs_pt (t_world_coord, real, t_world_coord, real,
                                    t_world_coord, t_world_coord,
                                    t_world_coord, real *);
extern bool cg_circle_tan_arc_line_radius (t_world_coord, real, t_world_coord,
                                           t_world_coord, real, t_world_coord,
                                           t_world_coord);
extern real cg_angle_between_2lines (t_world_coord, t_world_coord,
                                     t_world_coord, t_world_coord);
extern bool cg_intersect_2lines (t_world_coord, t_world_coord,
                                 t_world_coord, t_world_coord, t_world_coord);
extern bool cg_intersect_arc_line (t_world_coord, real, t_world_coord,
                                   t_world_coord, t_world_coord,
                                   t_world_coord);
extern bool cg_intersect_2arcs (t_world_coord, real, t_world_coord, real,
                                t_world_coord, t_world_coord);
extern real cg_distance_between_2pts (t_world_coord, t_world_coord);
extern real cg_distance_pt_to_line (t_world_coord, t_world_coord,
                                    t_world_coord);
extern int  cg_direction_pt_to_line (t_world_coord, t_world_coord,
                                     t_world_coord);

