/* polyline.c 1.1 */
/*
    Copyright 1992, 1993 O'Reilly and Associates, Inc.  Permission to
    use, copy, and modify this program is hereby granted, as long as
    this copyright notice appears in each copy of the program source
    code.
*/

#include "book_utils.h"

static PEXCoord2D solid[]   = {{0.3, 0.8}, {0.7, 0.8}};
static PEXCoord2D dashed[]  = {{0.3, 0.6}, {0.7, 0.6}};
static PEXCoord2D dotted[]  = {{0.3, 0.4}, {0.7, 0.4}};
static PEXCoord   dotdash[] = {{0.3, 0.2, 0.5}, {0.7, 0.2, 0.5}};

void
redraw( dpy, window, renderer )
    Display	*dpy;
    Window	window;
    PEXRenderer	renderer;
{
    PEXColor	color;
    
    XClearWindow( dpy, window );
    
    PEXBeginRendering( dpy, window, renderer ); {
	SET_COLOR( 1, 1, 1, color );
	PEXSetLineColor( dpy, renderer, PEXOCRender,
			 PEXColorTypeRGB, &color );
	PEXSetLineWidth( dpy, renderer, PEXOCRender, 2.0 );
	
	PEXSetLineType( dpy, renderer, PEXOCRender, PEXLineTypeSolid );
	PEXPolyline2D( dpy, renderer, PEXOCRender, 2, solid );
	
	PEXSetLineType( dpy, renderer, PEXOCRender, PEXLineTypeDashed );
	PEXPolyline2D( dpy, renderer, PEXOCRender, 2, dashed );
	
	PEXSetLineType( dpy, renderer, PEXOCRender, PEXLineTypeDotted );
	PEXPolyline2D( dpy, renderer, PEXOCRender, 2, dotted );
	
	PEXSetLineType( dpy, renderer, PEXOCRender, PEXLineTypeDashDot );
	PEXPolyline( dpy, renderer, PEXOCRender, 2, dotdash );
    }
    PEXEndRendering( dpy, renderer, True );
    XFlush( dpy );
}

#define WIN_GEOM	"500x500+50+50"

main( argc, argv )
    int		argc;
    char	*argv[];
{
    Display			*dpy;
    Window			window;
    XEvent			event;
    XVisualInfo			vis_info;
    XColor			bkgd_color;
    XStandardColormap		cmap_info;
    PEXColorApproxEntry		capx_info;
    PEXRenderer			renderer;
    PEXRendererAttributes	rattrs;
    PEXExtensionInfo		*pexinfo;
    int				done = 0;
    
    /* Open a display and initialize PEX. */
    dpy = ora_init_pex( argv, &pexinfo );
    if ( !dpy ) exit(1);
    
    /* Determine the best visual to use. */
    ora_find_best_visual( dpy, &vis_info );
    
    /* Get a standard colormap for the visual. */
    if ( !ora_get_standard_colormap( dpy, &vis_info, &cmap_info ) ) {
	fprintf ( stderr, "Cannot find a standard colormap\n" );
	exit(1);
    }
    ora_set_stdcmap_approx( &vis_info, &cmap_info, &capx_info );

    /* Create the window, giving it a gray background. */
    bkgd_color.red = bkgd_color.green = bkgd_color.blue = 0x5555;
    bkgd_color.flags = DoRed | DoGreen | DoBlue;
    window = ora_create_window( dpy, &vis_info, &cmap_info, WIN_GEOM,
			        &bkgd_color );
    
    /* Set up the PEX renderer, after first assuring that the PEX */
    /* immediate-mode subset is supported by the server. */
    if ( IMMED_MODE_SPT( pexinfo ) ) {
	renderer = ora_setup_renderer( dpy, window, &capx_info,
				       &rattrs );
    } else {
	fprintf( stderr, "No immediate mode.\n" );
	exit(1);
    }
    
    /* Read and respond to X events. */
    XSelectInput( dpy, window, ExposureMask | ButtonPressMask );
    while ( !done ) {
	XNextEvent( dpy, &event );
	switch ( event.type ) {
	case Expose:
	    /* Flush remaining Expose events, then redraw. */
	    while ( XCheckTypedWindowEvent( dpy, window,
					    Expose, &event ) )
		; /* empty statement */
	    redraw( dpy, window, renderer );
	    break;
	case ButtonPress:
	    done = 1;
	    break;
	}
    }

    XCloseDisplay( dpy );
    return 0;
}
