/* 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: top.c,v 2.0 1996/07/09 20:48:28 deaven Exp deaven $
 */

#include <stdio.h> 
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

#include <X11/Xaw/Form.h>
#include <X11/keysym.h>
#include "dmd/memory.h"
#include "xmogul.h"

static void key(Widget w, XtPointer client_data, XEvent *event)
{
	MogulPanel mp = (MogulPanel) client_data;
	KeySym keysym;
	int index;

	if (event->type == KeyPress) {
	  for(index = 0;
	      index < 4 &&
	      (keysym = XLookupKeysym((XKeyEvent *) event, index)) != NoSymbol;
	      ++index)
	  {
	    switch(keysym) {
	      case XK_q:
	      case XK_Q:
		exit(0);

	      case XK_r:
	      case XK_R:
		MPReopenFile(mp);
		break;
	    }
	  }
	}
}

void MPWarning(MogulPanel mp, String msg)
{
	MPInformationDialog(mp->app, mp->frame, msg);
}

void MPError(MogulPanel mp, String msg)
{
	XtAppErrorMsg(
	  mp->app,
	  "XMogulError",
	  "XMogulRuntimeError",
	  "XMogul",
	  msg,
	  NULL, 0);
	exit(1);
}

static MogulPanel last_panel = NULL; /* XXX bad way to do this, I think */
static void xterror_handler(String msg)
{
	if(last_panel)
	  MPError(last_panel, msg);
	else {
	  fprintf(stderr, "xmogul error: %s\n", msg);
	  exit(1);
	}
}

static void xtwarning_handler(String msg)
{
	if(last_panel)
	  MPWarning(last_panel, msg);
	else {
	  fprintf(stderr, "xmogul warning: %s\n", msg);
	  exit(1);
	}
}

MogulPanel
MPCreatePanel(XtAppContext app_context, Widget parent)
{
	Arg args[10];
	Cardinal n;
	MogulPanel mp = NewPointer(mp);

	mp->app = app_context;
	mp->toplevel = parent;

	mp->plot = NewMogulPlot("xmogul");

	/* Main panel is a form to hold everything
	 */
	n = 0;
	mp->frame =
	  XtCreateManagedWidget("MogulPanel", formWidgetClass,
	  parent, args, n);

	MPCreateMenus(mp);

	n = 0;
	mp->mogul = 
	  XtCreateManagedWidget("Mogul", mogulWidgetClass,
	  mp->frame, args, n);

	mp->bondRange = 1.e10;

	XtAddEventHandler(mp->mogul, KeyPressMask, FALSE, key, mp);

	MPCreateControls(mp);

	MPCreateZoom(mp);

	MogulSetMsgHandler(xterror_handler, xtwarning_handler);

	last_panel = mp; /* XXX static copy */

	return mp;
}

void MPDeletePanel(MogulPanel mp)
{
	DeleteMogulPlot(mp->plot);
	mp->plot = NULL;
	XtDestroyWidget(mp->toplevel);
}

