/***************************************************************************
 *		  Copyright (C) 1994  Charles P. Peterson                  *
 *	     4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
 *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
 *                                                                         *
 *		  This is free software with NO WARRANTY.                  *
 *	      See gfft.c, or run program itself, for details.              *
 *		      Support is available for a fee.                      *
 ***************************************************************************
 *
 * Program:     gfft--General FFT analysis
 * File:        wbenchm.c
 * Purpose:     workbench GUI main functions
 * Author:      Charles Peterson (CPP)
 * History:     19-Nov-1993 CPP; Created.
 * Comments:    Workbench GUI.  Amiga Dependent!
 */

#ifdef AMIGA

#include <stdlib.h>    /* exit()     */
#include <stdio.h>     /* sprintf() */
#include <string.h>
/*
 * Amiga includes
 */
#include <exec/types.h>
#include <exec/exec.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>
#include <intuition/intuition.h>
#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
#include <proto/icon.h>
#include <proto/dos.h>
#include <dos/dos.h>

/*
 * GFFT includes
 */
#include "gfft.h"
#include "settings.h"  /* for testing */
#include "wbench.h"

/*
 * Default console (Many thanks to SAS for making this possible.)
 */
char __stdiowin[] = "CON:0/160/640/40/GfftConsole";


extern struct WBStartup *WBenchMsg;

/*
 * Semi-static variables (used also by wbdialog)
 */
LONG num_files = 0;
LONG file_index = 0;

/*
 * Statics
 */
static int within_workbench_loop = FALSE;
static void workbench_dialog_loop (void);


void workbench_main (void)
{
    LONG olddir;

    open_libraries ();

    if (WBenchMsg && (num_files = WBenchMsg->sm_NumArgs - 1))
    {
    /*
     * Loop through selected project icon(s)
     */
	for (file_index = 0; file_index < num_files; file_index++)
	{
        /*
         * Change to directory containing selected object (file)
         * then, open it
         * then, change back
         * then, run dialog window
         * (if error, try next file)
         */
	    LONG i = file_index;
	    CATCH_ERROR
	    {
		olddir = CurrentDir (WBenchMsg->sm_ArgList[i+1].wa_Lock);
		set_read (WBenchMsg->sm_ArgList[i+1].wa_Name);
		CurrentDir (olddir);
		workbench_dialog_loop ();
	    }
	    END_CATCH_ERROR;
	}
    }
    else
    {
    /* 
     * No project files selected
     */
	CATCH_ERROR
	{
	    workbench_dialog_loop ();
	}
	END_CATCH_ERROR;
    }
}


/*
 * The following functions permit the CLI interactive mode to be activated
 * from the workbench, and the CLI to be started from the workbench, and
 * to return the the original mode.
 */

static void workbench_dialog_loop (void)
{
    int next_action = WORKBENCH_MODE;
    BOOLEAN help_displayed = FALSE;

    within_workbench_loop = TRUE;
    Plot = ANY_PLOT;
    while (next_action == WORKBENCH_MODE ||
	   next_action == INTERACTIVE_MODE)
    {
	switch (next_action)
	{
	case WORKBENCH_MODE:
	    next_action = workbench_dialog ();
	    break;
	case INTERACTIVE_MODE:
	    if (!help_displayed)
	    {
		printf ("Enter HELP for help...\n");
		help_displayed = TRUE;
	    }
	    cli_interactive_loop (FALSE);
	    next_action = WORKBENCH_MODE;
	    break;
	}
    }
    within_workbench_loop = FALSE;
}
	      
char *workbench_command (char *arguments)
{
    int previous_command_mode = CommandMode;

    if (within_workbench_loop)
    {
	CommandMode = WORKBENCH_MODE;
	return arguments;
    /*
     * return to workbench through cli_interactive_loop
     */
    }
    else
    {
	workbench_dialog_loop ();
	CommandMode = previous_command_mode;
    }
    return arguments;
}

#endif  /* ifdef AMIGA */

