/*-
 * Copyright (c) 1999,2000 Qube Software, Ltd.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of the source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Any redistribution solely in binary form must conspicuously
 *    reproduce the following disclaimer in documentation provided with the
 *    binary redistribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'', WITHOUT ANY WARRANTIES, EXPRESS
 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  LICENSOR SHALL
 * NOT BE LIABLE FOR ANY LOSS OR DAMAGES RESULTING FROM THE USE OF THIS
 * SOFTWARE, EITHER ALONE OR IN COMBINATION WITH ANY OTHER SOFTWARE.
 *
 * 	$Id: windowed_x11.cpp,v 1.4 2000/06/19 15:56:58 n_hibma Exp $
 */

// Standard Toolkit include files
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

// Public include files for widgets used
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Paned.h>

// Q Xt Widget wrapper
#include "XtQ.h"

#include "maths.h"
#include "draw.h"
#include "windowed.h"
#include "qsys.h"

#define GUI_X11
#include "draw_os.h"

// Global variables
XtAppContext app_context;

void
pollWindowEvents()
{
    XEvent xevent;

    while (XtAppPending(app_context)) {
	XtAppNextEvent(app_context, &xevent);
	XtDispatchEvent(&xevent);
    }
}


// Quit button callback function
void
quit_cb(Widget widget, XtPointer client_data, XtPointer call_data)
{
	quit_ = 1;
}

// Resize callback function
void
resize_cb(Widget widget, XtPointer client_data, XtPointer call_data)
{
    Dimension width, height;

    XtVaGetValues(widget, XtNwidth, &width, XtNheight, &height, NULL);

    QIF_EnterMutex(lock_);
    width_ = width;
    height_ = height;
    resize_ = true;
    QIF_ExitMutex(lock_);
}

int
main(int argc, char** argv)
{
    Widget topLevel, pane, quit, qw;

    lock_ = QIF_CreateMutex();
    if (lock_ == NULL)
	return 1;

    topLevel = XtVaAppInitialize(
		&app_context,		/* Application context */
		"Windowed Tetra",	/* Application class */
		NULL, 0,		/* command line option list */
		&argc, argv,		/* command line args */
		NULL,			/* for missing app-defaults file */
		NULL);			/* terminate varargs list */

    pane = XtVaCreateManagedWidget(
       		"pane",			/* widget name */
		panedWidgetClass,	/* widget class */
		topLevel,		/* parent widget*/
		NULL);			/* terminate varargs list */

    quit = XtVaCreateManagedWidget(
		"quit",			/* widget name */
		commandWidgetClass,	/* widget class */
		pane,			/* parent widget*/
		XtNx, 10,
		XtNy, 10,
		NULL);			/* terminate varargs list */

    qw = XtVaCreateManagedWidget(
		"qw",			/* widget name */
		qWidgetClass,		/* widget class */
		pane,			/* parent widget*/
		XtNwidth, RENDERER_WIDTH,
		XtNheight, RENDERER_HEIGHT,
		// Make it apparent that when we resize the pink background
		// belongs to the qw.
		XtNbackground, XtDefaultForeground,
		NULL);			/* terminate varargs list */

    XtAddCallback(quit, XtNcallback, quit_cb, 0);
    XtAddCallback(qw, XtNcallback, resize_cb, 0);

    XtRealizeWidget(topLevel);

    Draw::WindowInfo windowInfo;
    windowInfo.parent.window = XtWindow(qw);
    windowInfo.parent.display = XtDisplay(qw);
    windowInfo.parent.screenno = DefaultScreen(XtDisplay(qw));

    width_ = RENDERER_WIDTH;
    height_ = RENDERER_HEIGHT;

    draw3D(&windowInfo);

    return 0;
}


#ifdef STATIC
/* Make sure the drivers we want are linked in. */

extern int QDriver_OS_X11;
int* osref = &QDriver_OS_X11;

extern int QDriver_Display_OpenGL;
int* displayref = &QDriver_Display_OpenGL;
#endif
