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

/* This file contains empty template routines that
 * the IDCMP handler will call uppon. Fill out these
 * routines with your code or use them as a reference
 * to create your program.
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <libraries/AmigaGuide.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <libraries/asl.h>
#include <workbench/startup.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/asl_protos.h>
#include <clib/amigaguide_protos.h>
#include <string.h>
#include "stdlib.h"
#include "stdio.h"

#include "GUI.h"

void FixString( char *from, char *to, int length);

extern struct Library *AmigaGuideBase;
extern struct FileRequester *OurFReq;
extern struct List *SelFiles;
extern LONG TotalK, Rate, Rates[];
extern char ptrn[];

int selected=0;
char Entry[100];
char FName[22];

struct Node *AddANodeAlpha( struct List *list, char *Name );
void ClearList( struct List *Start );
BOOL FindNodeA( struct Node *Start, char *String, struct Node **Insert );
struct Node *FindOrd( struct List *list, UWORD ordinal );
WORD OrdNode( struct Node *node );
void RemoveANode( struct Node *Bye );
APTR Allocvec( ULONG size, LONG type );
void Freevec( APTR addr );

BOOL TFReq( char *title);

void UpdateDL( void );

int Gadget00Clicked( void )
{
	/* routine when gadget ListView"" is clicked. */

selected = MarksMsg.Code;	/* Save ID of selected entry */

return 1;
}

int Gadget10Clicked( void )
{

/* routine when gadget MX"" is clicked. */

Rate = Rates[ MarksMsg.Code ];

/* Update the CPS gadget */
GT_SetGadgetAttrs( MarksGadgets[8], MarksWnd, NULL, GTNM_Number, Rate, TAG_END );

/* Update d'l size and time */
UpdateDL();

return 1;
}

int Gadget30Clicked( void )
{
	/* routine when gadget "_Add" is clicked. */

struct FileLock *lock;
struct FileHandle *handle;
struct WBArg *files;

LONG size;
int temp;

/* Display the ASL requester .. */
if( AslRequestTags( OurFReq,ASLFR_InitialPattern, &ptrn[0],
						ASLFR_DoMultiSelect,
						ASLFR_DoPatterns,
						ASLFR_Flags1, FRF_DOPATTERNS,
						ASLFR_Flags1, FRF_DOMULTISELECT,
						TAG_DONE ) )
	{
	files = OurFReq->fr_ArgList;

	if( lock=(struct FileLock *)Lock( OurFReq->fr_Drawer, ACCESS_READ ) )
		{
		/* CD to selected drawer */
		lock = (struct FileLock *)CurrentDir( (BPTR)lock );

		/* Step through the list of selected files */
		for(temp=0; temp<OurFReq->fr_NumArgs ; temp++)
			{
			/* Determine the length of the file */
			if ( handle = ( struct FileHandle *) Open( files->wa_Name, MODE_OLDFILE) )
				{
				Seek( (BPTR)handle, 0, OFFSET_END );
				size=Seek((BPTR)handle, 0, OFFSET_BEGINNING );
				TotalK += size;
				Close( (BPTR)handle );
				}

			/* Make text for entry */
			FixString( files->wa_Name, FName, 20);
			sprintf( Entry, "%20s %7d", FName, size );

			/* Freeze List View */
			GT_SetGadgetAttrs(MarksGadgets[0],MarksWnd,NULL,GTLV_Labels,-1,TAG_END);

			/* Add text to list */
			AddANodeAlpha( SelFiles, Entry );

			/* Update List View */
			GT_SetGadgetAttrs( MarksGadgets[0], MarksWnd, NULL, GTLV_Labels, SelFiles, TAG_END );

			/* Update d'l size and time */
			UpdateDL();

			/* Step to next selected file */
			files++;
			}
		
		/* CD back to our original drawer and free the lock */
		lock=(struct FileLock *)CurrentDir( (BPTR)lock );
		UnLock( (BPTR)lock );
		}

	}

return 1;
}

int Gadget40Clicked( void )
{
	/* routine when gadget "_Delete" is clicked. */
int l;

struct Node *node;

/* Freeze List View */
GT_SetGadgetAttrs( MarksGadgets[0], MarksWnd, NULL, GTLV_Labels, -1, TAG_END );

/* Remove Node */

if( node= FindOrd( SelFiles, selected ) )
	{
	l=atoi( (node->ln_Name)+20 );
	TotalK -= l;
	RemoveANode( node );
	
	/* Update d'l size and time */
	UpdateDL();
	
	if(!FindOrd( SelFiles, selected ) )
		if( selected ) selected--;
	}

/* Update List View */
GT_SetGadgetAttrs( MarksGadgets[0], MarksWnd, NULL, GTLV_Labels, SelFiles, TAG_END );

return 1;
}

int Gadget50Clicked( void )
{
	/* routine when gadget "_Clear" is clicked. */

/* Freeze List View */
GT_SetGadgetAttrs( MarksGadgets[0], MarksWnd, NULL, GTLV_Labels, -1, TAG_END );

/* Clear the list */
ClearList( SelFiles );

TotalK=0;

sprintf( Entry, " %02d:%02d:%02d", 0, 0, 0 );
GT_SetGadgetAttrs( MarksGadgets[2], MarksWnd, NULL, GTTX_Text, Entry, TAG_END );

sprintf( Entry, " %d bytes", TotalK );
GT_SetGadgetAttrs( MarksGadgets[7], MarksWnd, NULL, GTTX_Text, Entry, TAG_END );

/* Update List View */
GT_SetGadgetAttrs( MarksGadgets[0], MarksWnd, NULL, GTLV_Labels, SelFiles, TAG_END );

return 1;
}

int Gadget60Clicked( void )
{
	/* routine when gadget "About" is clicked. */
int m;

if ( !OpenProject1Window() )
	{
	m=1;
	while (m) m=HandleProject1IDCMP();
	CloseProject1Window();
	}

return 1;
}

int Gadget90Clicked( void )
{
	/* routine when gadget "_Help" is clicked. */

struct NewAmigaGuide nag = {NULL};
AMIGAGUIDECONTEXT handle;
LONG retval = 0L;

if( AmigaGuideBase )
	{
	/* Fill in the NewAmigaGuide structure */
	nag.nag_Name = "TimeUL.Guide";
	nag.nag_Node = NULL;
	nag.nag_Line = 0;

	/* Open the AmigaGuide client */
	if ( handle = OpenAmigaGuide(&nag, NULL))
		CloseAmigaGuide(handle);
	else
		OkReq( "Error opening 'TimeUL.Guide'!" );
	}
else
	OkReq( "Error: No 'amigaguide.library'!" );

return 1;
}

int Gadget01Clicked( void )
{
	/* routine when gadget "That's Just Dandy!" is clicked. */
return 0;
}

int MarksCloseWindow( void )
{
	/* routine for "IDCMP_CLOSEWINDOW". */

return TFReq( "Really Quit :-(" );
/*return 0;*/
}

int MarksVanillaKey( void )
{
	/* routine for "IDCMP_VANILLAKEY". */

switch( (char)MarksMsg.Code )
	{
	case 'A':
	case 'a':
		Gadget30Clicked();
		break;
	
	case 'D':
	case 'd':
		Gadget40Clicked();
		break;
	
	case 'C':
	case 'c':
		Gadget50Clicked();
		break;

	case 'H':
	case 'h':
		Gadget90Clicked();
		break;

	}

return 1;
}

int Project1CloseWindow( void )
{
	/* routine for "IDCMP_CLOSEWINDOW". */
return 0;
}

/* Copies a string, either clipping it or expanding it to a defined width

Entry	*from	source string
	*to	dest string
	length	desired length of destination string

*/

void FixString( char *from, char *to, int length)
{
int loop, plus;
register int i;

loop=strlen( from );

if( loop > length )
	{
	loop=length;
	plus=0;
	}
else
	plus=length-loop;

for( i=0; i<loop; i++ )	*to++ = *from++;

if( plus )
	for( i=0; i<plus; i++ ) *to++ = ' ';

*to = '\0';

}

void UpdateDL( void )
{
LONG TotalHr, TotalMin, TotalSec;

/* Determine time in Hr:Min:Sec  */
TotalSec = TotalK/Rate;
TotalHr = TotalSec/3600;
TotalSec = TotalSec - TotalHr*3600;
TotalMin = TotalSec/60;
TotalSec = TotalSec - TotalMin*60;

sprintf( Entry, " %02d:%02d:%02d", TotalHr, TotalMin, TotalSec );
GT_SetGadgetAttrs( MarksGadgets[2], MarksWnd, NULL, GTTX_Text, Entry, TAG_END );

sprintf( Entry, " %d bytes", TotalK );
GT_SetGadgetAttrs( MarksGadgets[7], MarksWnd, NULL, GTTX_Text, Entry, TAG_END );

}