/*
 * Copyright 1993 by Sun Microsystems, Inc.
 * All Rights reserved.
 */

#pragma ident	"@(#)ow_olit.c	3.4	94/05/25 SMI"	/* SOURCE	*/


/* ow_olit.c - attach XGL Window Raster to OLIT stub widget */

/*
 * This program demonstrates how to create an XGL Window Raster given
 * an OLIT stub widget.
 */

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xol/OpenLook.h>
#include <Xol/DrawArea.h>

#include <xgl/xgl.h>

/*
 * need these global so that expose procedure can get to them 
 */
static Xgl_object	ctx = NULL;		/* XGL Context object */
static Xgl_object	sys_st;			/* XGL System State object */
static Xgl_object	win_ras = NULL;		/* Xgl window raster */


main (
    int			argc,
    char		*argv[])
{
    Xgl_X_window	xgl_x_win;		/* XGL-X data structure */
    Xgl_obj_desc	win_desc;		/* XGL window raster struct */

    Widget		toplevel;		/* Widget top-level */
    Widget		drawable;		/* Drawable widget */
    XtAppContext	app;

    Window		window;			/* X window for canvas */
    Display		*display;		/* pointer to X display */

    void		repaint();		/* OLIT repaint procedure */

    /*
     * Initialize toolkits 
     */
    OlToolkitInitialize((XtPointer)NULL);
    toplevel = XtVaAppInitialize (&app, "ow_olit",
		(XrmOptionDescList)NULL, 0,
		&argc, argv, (String *)NULL,
		XtNtitle, "Simple OLIT Drawable",
		XtVaTypedArg, XtNvisual, XtRString,
		"PseudoColor", sizeof("PseudoColor"),
		NULL);

    /*
     * Create a draw area widget for rendering graphics
     * and add expose callback to draw area widget
     */
    drawable = XtVaCreateManagedWidget("drawable",
		drawAreaWidgetClass, toplevel,
		XtNlayout, OL_IGNORE,
		XtNheight, 512,
		XtNwidth,  512,
		NULL);
    XtAddCallback(drawable, XtNexposeCallback, repaint,
		  (XtPointer) NULL);

    XtRealizeWidget(toplevel);

    /* get X stuff */
    display = (Display *) XtDisplay (drawable);
    window = (Window) XtWindow (drawable);

    /* put X stuff into XGL data structure */
    xgl_x_win.X_display = (void *) display;
    xgl_x_win.X_window  = (Xgl_usgn32) window;
    xgl_x_win.X_screen  = (int) DefaultScreen (display);

    /* wait for the window */
    sleep (2);

    /* create XGL System State object */
    sys_st = xgl_open (NULL);

    /* create Window Raster Device using OLIT widget */
    win_desc.win_ras.type = XGL_WIN_X;
    win_desc.win_ras.desc = &xgl_x_win;
    win_ras = xgl_object_create (sys_st, XGL_WIN_RAS, &win_desc, NULL);
 
    /* create XGL graphics Context object using the Window Raster object */
    ctx = xgl_object_create (sys_st, XGL_2D_CTX, NULL,
                             XGL_CTX_DEVICE, win_ras,
                             XGL_CTX_DEFERRAL_MODE, XGL_DEFER_ASAP,
                             NULL);

    /* go into Xt main loop and wait for expose events */
    XtAppMainLoop(app);

    exit (0);
}

/*
 * Xgl repaint procedure called on expose events
 */
static
void
repaint ()
{
    Xgl_pt_list		pl;		/* polygon point structure */
    Xgl_pt_i2d		pts_i2d[3];	/* integer 2d points */

    xgl_window_raster_resize(win_ras);

    /* clear draw area to default background color of black */
    xgl_context_new_frame (ctx);

    /* set up data points for a triangle - XGL will close triangle */
    pts_i2d[0].x = 25;
    pts_i2d[0].y = 50;
    pts_i2d[1].x = 50;
    pts_i2d[1].y = 400;
    pts_i2d[2].x = 250;
    pts_i2d[2].y = 375;

    /* fill XGL polygon data structure */
    pl.pt_type = XGL_PT_I2D;
    pl.num_pts = 3;
    pl.bbox = 0;
    pl.pts.i2d = &pts_i2d[0];

    /* draw a polygon in the default color of white */
    xgl_polygon (ctx, XGL_FACET_NONE, 0, 0, 1, &pl);
}
