/*****************************************************************************

			      PilotForth

      Copyright (C) 1997, Ivan A. Curtis.  All rights reserved.

			       based on
C-FORTH: a portable, C-coded figFORTH interpreter by Allan Pratt April 1985.

This program may not be redistibuted in part or in whole without this Copyright
Message being included at the top of each file.

   This is free software, licensed under the GNU Public License V2.
		  See the file COPYING for details.

*******************************************************************************
*******************************************************************************

Filename:	Forth/Forth.c

Description:	Main module for PilotForth

Update History:   (most recent first)
   I. Curtis  15-May-97 20:59 -- v0.1 Released.
   I. Curtis  27-Apr-97 13:54 -- Created.

******************************************************************************/
#pragma pack(2)
#include "stdio.h"
#include <Common.h>
#include <System/SysAll.h>
#include <UI/UIAll.h>

#include "common.h"
#include "exec.h"
#include "memory.h"

/********************
 * The Main form... *
 ********************/
#define FormMain 1000
#define MenuMain 1000
#define MenuMainAbout 1001

/**********************
 * The Error alert... *
 **********************/
#define AlertError 2000

/**********************
 * The About alert... *
 **********************/
#define AlertAbout 3000

/*************************************
 * Handle events for the application *
 *************************************/
Boolean running;
short keypressed = -1;
short waitkey = false;
Boolean fth_event_main(EventPtr e)
{
  FormPtr frm;
  Boolean handled = false;

  switch (e->eType) {
  case frmOpenEvent:
    frm = FrmGetActiveForm();
    FrmDrawForm(frm);

    if (e->data.frmOpen.formID == FormMain) {
      if (fth_db_open() != 0) {
	FrmAlert(AlertError);
	handled = true;
	break;
      }
      if (fth_db_map() != 0) {
	fth_db_close();
	FrmAlert(AlertError);
	handled = true;
	break;
      };
      forceip = 0;
      rsp = INITR0;		/* initialize return stack */
      csp = INITS0;		/* and calculation stack */
      if (fth_mfetch(SAVEDIP) == 0) {
	printf("New Image ");
	ip = fth_mfetch(COLDIP);	/* this is the CFA of COLD */
	fth_mstore(SAVEDIP, 1);	        /* mark image as warm */
      } else {
	printf("Existing Image ");
	ip = fth_mfetch(ABORTIP);	/* this is the CFA of ABORT */
      }
      printf("Running..\n");
      running = true;
    }
    handled = true;
    break;

  case appStopEvent:
  case frmCloseEvent:
    handled = true;
    break;

  case keyDownEvent:
    if (waitkey) {
      keypressed = e->data.keyDown.chr;
      /* printf("<%d>", keypressed); */
    }
    handled = true;
    break;

  case menuEvent:
    switch (e->data.menu.itemID) {
    case MenuMainAbout:
      FrmAlert(AlertAbout);
      handled = true;
      break;
    default:
      handled = false;
      break;
    }
    break;

  default:
    handled = false;
  }
  return handled;
}

/*****************************************************************************
 *                                                                           *
 * 		     Main - Execution begins here                            *
 *                                                                           *
 *****************************************************************************/
DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
{
  short err;
  EventType e;

  if (cmd == sysAppLaunchCmdNormalLaunch) {
    FrmInitForm(FormMain);
    FrmGotoForm(FormMain);
    do {
      if (running) {
	next();
	while (!waitkey) {
	  next();
	}
      }
      EvtGetEvent(&e, evtWaitForever);
      if (SysHandleEvent(&e)) {
	continue;
      }
      if (MenuHandleEvent((void *)0, &e, &err)) {
	continue;
      }
      if (e.eType == frmLoadEvent) {
	FormPtr frm = FrmGetFormPtr(e.data.frmLoad.formID);
	FrmSetActiveForm(frm);
	FrmSetEventHandler(frm, fth_event_main);
	FrmDrawForm(frm);
	continue;
      }

      FrmDispatchEvent(&e);
    }
    while (e.eType != appStopEvent);
    fth_db_unmap();
    fth_db_close();
  }
  return(0);
}
