;/* GadToolsStart - Execute me to compile me with Lattice.
LC -b0 -cfistq -v -y GadToolsStart.c
Blink with Make
quit
*/


	/* ****************************************************** */
	/* 					Include Commodore files				  */
	/* ****************************************************** */

#include "stdio.h"

#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/gadgetclass.h>
#include <graphics/displayinfo.h>
#include <libraries/dos.h>
#include <libraries/asl.h>
#include <libraries/gadtools.h>
#include <utility/tagitem.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/asl.h>
#include <proto/gadtools.h>
#include <proto/utility.h>
#include <proto/icon.h>

#ifdef LATTICE
int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
int chkabort(void) { return(0); }  /* really */
#endif

	/* ****************************************************** */
	/* 					Include other files					  */
	/* ****************************************************** */


	/* ****************************************************** */
	/* 	    	    		Declarations					  */
	/* ****************************************************** */


	/* ****************************************************** */
	/* 	    			Function Prototypes					  */
	/* ****************************************************** */

BOOL open_libs( void );
void close_libs( void );

/* The following routines are made global so all code can access them */

extern void OkReq( char *title);
extern BOOL TFReq( char *title);
void ClearList( struct List *);
void DoTools( char *name );

	/* ****************************************************** */
	/* 	    		External Function Prototypes			  */
	/* ****************************************************** */

#include "gui.h"

	/* ****************************************************** */
	/* 				Global variable declerations			  */
	/* ****************************************************** */

struct IntuitionBase *IntuitionBase=NULL;
struct GfxBase *GfxBase=NULL;
extern struct DOSBase *DOSBase;
struct AslBase *AslBase=NULL;
struct GadToolsBase *GadToolsBase=NULL;
struct UtilityBase *UtilityBase=NULL;
struct Library *AmigaGuideBase=NULL;

struct Window *win;
struct IntuiMessage *msg;
USHORT msg_code;
struct Gadget *gadg;

struct List *SelFiles=NULL;
struct FileRequester *OurFReq;		/* For File Selection */
LONG TotalK=0, Rate=30;
LONG Rates[] = { 30, 120, 240, 480, 960, 1440, 1680, 1920, 2160, 2880 };

UWORD index=0;
char ptrn[50]="(*.lha|*.lzx|*.dms)";

	/* ****************************************************** */
	/* 						Main							  */
	/* ****************************************************** */

void _main( int argc, char *argv[] )
{

int m;
struct WBStartup *WBmsg;
struct WBArg *WBarg;
BPTR parent_dir=NULL;

if( open_libs() )
	{

	/* CD to our directory if from Workbench */

	if (argc == 0)							/* Only 0 when from Workbench */
		{
		WBmsg=(struct WBStartup *)argv;		/* argv -> WBStartup message */
		WBarg=WBmsg->sm_ArgList;			/* 1st WB Argumeent */
		if ( WBarg->wa_Lock ) parent_dir=(BPTR)CurrentDir(WBarg->wa_Lock );
		DoTools( WBarg->wa_Name );			/* Examine icons ToolTypes */
		}
	else
		DoTools( "TimeUL" );

	/* Get a file requester */
	if( OurFReq=(struct FileRequester *)
		AllocAslRequestTags( ASL_FileRequest,
							ASLFR_TitleText, "Select Files .... ", TAG_DONE ) )
		{

		if( !SetupScreen() )
			{
			if ( !OpenMarksWindow() )
				{

				/* Set default rate & CPS */
				Rate = Rates[index];
				GT_SetGadgetAttrs( MarksGadgets[1], MarksWnd, NULL, GTMX_Active, index, TAG_END );
				GT_SetGadgetAttrs( MarksGadgets[8], MarksWnd, NULL, GTNM_Number, Rate, TAG_END );

				m=1;
				while (m) m=HandleMarksIDCMP();
				CloseMarksWindow();
				}
			CloseDownScreen();
			}
		/* Free the file requester structure */
		FreeAslRequest( OurFReq );
		}
	if ( parent_dir ) CurrentDir( parent_dir );
    }
close_libs();

}

	/* ****************************************************** */
	/* 			Attempt to open required libraries 			  */
	/* ****************************************************** */

/* ASL library is attempted, but if not there the program will continue */

BOOL open_libs( void )
{

BOOL ok=FALSE;

if ( IntuitionBase = (struct IntuitionBase *)
	     OpenLibrary ( "intuition.library",0) )
	
    if ( GfxBase = (struct GfxBase *)
	       OpenLibrary( "graphics.library",0) )

    if ( GadToolsBase = (struct GadToolsBase *)
	       OpenLibrary( "gadtools.library", 0L ) )

    if ( UtilityBase = (struct UtilityBase *)
	       OpenLibrary( "utility.library", 0L ) )

	if ( AslBase = (struct AslBase *)
		   OpenLibrary( AslName, 0 ) )

	if ( SelFiles=(struct List *)AllocMem( sizeof(struct List),MEMF_CLEAR) )
			{
			NewList( SelFiles );
			ok=TRUE;
			}

AmigaGuideBase = (struct Library *) OpenLibrary( "amigaguide.library", 0 );

return ok;
}

	/* ****************************************************** */
	/* 					Close Any Open Libraries			  */
	/* ****************************************************** */

void close_libs( void )
{
if ( IntuitionBase ) CloseLibrary( (struct Library *)IntuitionBase );
if ( GfxBase ) CloseLibrary( (struct Library *)GfxBase );
if ( AslBase ) CloseLibrary( (struct Library *)AslBase );
if ( GadToolsBase ) CloseLibrary( (struct Library *)GadToolsBase );
if ( UtilityBase ) CloseLibrary( (struct Library *)UtilityBase );
if ( AmigaGuideBase ) CloseLibrary( AmigaGuideBase );

if ( SelFiles )
	{
	ClearList( SelFiles );
	FreeMem( SelFiles, sizeof(struct List) );
	}
}

	/* ****************************************************** */
	/* 					A Simple OK Requester				  */
	/* ****************************************************** */

void OkReq( char *title )
{

struct EasyStruct easy =
	{
	sizeof( struct EasyStruct ),
	NULL,
	NULL,
	NULL,
	"Ok"
	};

easy.es_TextFormat = title;
EasyRequest( MarksWnd, &easy, NULL, 0L );

}

	/* ****************************************************** */
	/* 					An Ok--Cancel Requester				  */
	/* ****************************************************** */

BOOL TFReq( char *title )
{

BOOL RetVal;
struct EasyStruct easy =
	{
	sizeof( struct EasyStruct ),
	NULL,
	NULL,
	NULL,
	"Cancel|Ok"
	};

easy.es_TextFormat = title;
RetVal=(BOOL)EasyRequest( MarksWnd, &easy, NULL, 0L );

return RetVal;

}

	/* ****************************************************** */
	/* 					Examine Tool Types					  */
	/* ****************************************************** */

/* given name of program, examine it's icons ToolTypes. Assumes we are in */
/* the same directory as the program, cd to it if from WBench.			  */

void DoTools( char *name )
{

struct Library *IconBase;
struct DiskObject *object;
char **toolsarray,*s;

if( IconBase=(struct Library *)OpenLibrary( "icon.library", 0L ) )
	{
		
	/* Can now load in our icon */
	if ( object=GetDiskObject( name ) )
		{
		toolsarray=(char **)object->do_ToolTypes;
				
		if( s=(char *)FindToolType( toolsarray, "300" ))
			Rates[0]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "1200" ))
			Rates[1]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "2400" ))
			Rates[2]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "4800" ))
			Rates[3]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "9600" ))
			Rates[4]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "14400" ))
			Rates[5]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "16800" ))
			Rates[6]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "19200" ))
			Rates[7]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "21600" ))
			Rates[8]=atol( s );
		
		if( s=(char *)FindToolType( toolsarray, "28800" ))
			Rates[9]=atol( s );

		if( s=(char *)FindToolType( toolsarray, "DEFRATE" ))
			{
			index=atol( s );
			if( index<0 || index>9 )index=0;
			}

		if( s=(char *)FindToolType( toolsarray, "PATTERN" ))
			strcpy( ptrn, s );

		FreeDiskObject( object );
		}
	CloseLibrary( IconBase );
	}
}
