/* This file is part of `mogul'
 * Copyright (c) 1995 David Deaven
 *
 * In the following, "you" and "You" refer to any entity other than 
 * the Microsoft corporation and its subsidiaries.  All rights are
 * reserved by the author in connection with Microsoft and its
 * subsidiaries.  The Microsoft Network is explicity denied the right
 * to copy, reproduce, or transmit this software.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Author: deaven $
 * $Id: Mogul.c,v 2.0 1996/07/09 20:59:55 deaven Exp $
 */

#include <stdio.h>
#include <math.h>

#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>

#include "MogulP.h"

/* Resources:               Type:          Defaults:
 * ----------------------   ------------   ------------------------
 * XtNwidth               : Dimension    : 360
 * XtNheight              : Dimension    : 192
 * XtNfont                : XFontStruct  : XtDefaultFont
 */

#define offset(field) XtOffset(MogulWidget, mogul.field)
static XtResource MogulResources[] = 
{
  {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
    offset(X.font), XtRString, (XtPointer) XtDefaultFont},
};
#undef offset

#define MOGUL ((MogulWidget) w)->mogul
#define CORE  ((MogulWidget) w)->core

static void Resize(Widget w)
{
	MOGUL.X.width = CORE.width;
	MOGUL.X.height = CORE.height;
	ResizeXInstance(&(MOGUL.X));
}

static void Initialize(Widget request, Widget w, ArgList args, Cardinal *n)
{
	char deviceinfo[64];

	MogulDeviceInit();

	if (request->core.width == 0) CORE.width = DEFAULT_WIDTH;
	if (request->core.height == 0) CORE.height = DEFAULT_HEIGHT;

	/* Open a device instance and fill in the intrinsics structure.
	 * These are the parts of the instance that are required by dev_x11
	 * to init a window.
	 */
	MOGUL.X.display = XtDisplay(w);
	MOGUL.X.screen = XtScreen(w);
	MOGUL.X.window = XtWindow(w);

	MOGUL.X.width = CORE.width;
	MOGUL.X.height = CORE.height;

	sprintf(deviceinfo, "instance=%p", (void *) &(MOGUL.X));
	if((MOGUL.X.device = MogulGetDevice("x11", deviceinfo)) == NULL)
	  MogulPerror("could not set x11 device");
}

static void Realize(Widget w, XtValueMask *mask, XSetWindowAttributes *attr)
{
	XtCreateWindow(w, CopyFromParent, CopyFromParent, *mask, attr);
	MOGUL.X.display = XtDisplay(w);
	MOGUL.X.screen = XtScreen(w);
	MOGUL.X.window = XtWindow(w);
}

static void Destroy(Widget w)
{
	MogulFreeDevice(MOGUL.X.device);
	MOGUL.X.device = NULL;
}

static void Redisplay(Widget w, XEvent *event, Region region)
{
	if (CORE.visible)
	  RedisplayXInstance(&(MOGUL.X));
}

static Boolean SetValues(Widget wcurrent,
	Widget request, Widget w, ArgList args, Cardinal *n)
{
	Boolean redisplay = FALSE;
	MogulWidget current = (MogulWidget) wcurrent;

	if (MOGUL.X.font != current->mogul.X.font)
	  redisplay = TRUE;

	return redisplay;
}

void *MWGetDevice(Widget w)
{
	if(XtIsMogul(w)) return MOGUL.X.device;
	return NULL;
}

/* Complete class definition ---------------------------------- */

struct MogulClassRec_s mogulClassRec = 
{
  {        /* CoreClassPart */
    (WidgetClass)&widgetClassRec,  /* superclass */
    "Mogul",                       /* class_name */
    sizeof(MogulRec),              /* widget_size */
    NULL,                          /* class_initialize */
    NULL,                          /* class_part_initialize */
    FALSE,                         /* class_init */
    Initialize,                    /* initialize */
    NULL,                          /* initialize_hook */
    Realize,                       /* realize */
    NULL,	                   /* actions */
    0,				   /* num_actions */
    MogulResources,                /* resources */
    XtNumber(MogulResources),      /* num_resources */
    NULLQUARK,                     /* xrm_class */
    TRUE,                          /* compress_motion */
    TRUE,                          /* compress_exposure */
    TRUE,                          /* compress_enterleave */
    TRUE,                          /* visible_intress */
    Destroy,                       /* destroy */
    Resize,                        /* resize */
    Redisplay,                     /* expose */
    SetValues,                     /* set_values */
    NULL,                          /* set_values_hook */
    XtInheritSetValuesAlmost,      /* set_values_almost */
    NULL,                          /* get_values_hook */
    NULL,                          /* accept_focus */
    XtVersion,                     /* version */
    NULL,                          /* callback_private */
    NULL,		           /* tm_translations */
    NULL,
    NULL,
    NULL,
  },
  { 
    0 /* empty */
  }
};

WidgetClass mogulWidgetClass = (WidgetClass) &mogulClassRec;

