// TSMorph - Amiga Morphing program
// Copyright (C) © 1993  Topicsave Limited

// 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
// 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.

// mpaddock@cix.compulink.co.uk

/*
 *  Source machine generated by GadToolsBox V2.0
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : M J P
 */

// Minor editing by MJP

#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 <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/alib_protos.h>
#include <string.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/utility_pragmas.h>

extern struct Library *SysBase,*GfxBase;
extern struct Library *UtilityBase;

#include "progress.h"

struct Screen         *Scr = NULL;
UBYTE                 *PubScreenName = NULL;
APTR                   VisualInfo = NULL;
struct Window         *ProgressWnd = NULL;
struct Gadget         *ProgressGList = NULL;
struct IntuiMessage    ProgressMsg;
UWORD                  ProgressZoom[4];
struct Gadget         *ProgressGadgets[4];
UWORD                  ProgressLeft = 215;
UWORD                  ProgressTop = 42;
UWORD                  ProgressWidth = 261;
UWORD                  ProgressHeight = 133;
UBYTE                 *ProgressWdt = (UBYTE *)"TSMorph Progress";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

UWORD ProgressGTypes[] = {
	SLIDER_KIND,
	SLIDER_KIND,
	BUTTON_KIND,
	LISTVIEW_KIND
};

struct NewGadget ProgressNGad[] = {
	52, 78, 176, 11, (UBYTE *)"Frame", NULL, GD_Frame, PLACETEXT_LEFT, NULL, (APTR)FrameClicked,
	52, 95, 176, 11, (UBYTE *)"Line", NULL, GD_Line, PLACETEXT_LEFT, NULL, (APTR)LineClicked,
	82, 111, 95, 19, (UBYTE *)"Stop", NULL, GD_Stop, PLACETEXT_IN, NULL, (APTR)StopClicked,
	6, 6, 251, 65, NULL, NULL, GD_Info, 0, NULL, NULL
};

extern struct List InfoList;

ULONG ProgressGTags[] = {
	(GTSL_Max), 999, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%3ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, GA_Disabled,TRUE,(TAG_DONE),
	(GTSL_Max), 999, (GTSL_MaxLevelLen), 4, (GTSL_LevelFormat), (ULONG)"%3ld", (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, GA_Disabled,TRUE,(TAG_DONE),
	(TAG_DONE),
	(GTLV_Labels), (ULONG)&InfoList, (GTLV_ReadOnly), TRUE, (TAG_DONE)
};

static UWORD ComputeX( UWORD value )
{
	return(( UWORD )((( FontX * value ) + 3 ) / 6 ));
}

static UWORD ComputeY( UWORD value )
{
	return(( UWORD )((( FontY * value ) + 5 ) / 10 ));
}

static void ComputeFont( UWORD width, UWORD height )
{
	Font = &Attr;
	Font->ta_Name = (STRPTR)Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
	Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
	FontX = Scr->RastPort.Font->tf_XSize;

	OffX = Scr->WBorLeft;
	OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;

	if ( width && height ) {
		if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
			goto UseTopaz;
		if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
			goto UseTopaz;
	}
	return;

UseTopaz:
	Font->ta_Name = (STRPTR)"topaz.font";
	FontX = FontY = Font->ta_YSize = 8;
}

int SetupScreen( void )
{
	if ( ! ( Scr = LockPubScreen( PubScreenName )))
		return( 1L );

	ComputeFont( 0, 0 );

	if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
		return( 2L );

	return( 0L );
}

void CloseDownScreen( void )
{
	if ( VisualInfo ) {
		FreeVisualInfo( VisualInfo );
		VisualInfo = NULL;
	}

	if ( Scr        ) {
		UnlockPubScreen( NULL, Scr );
		Scr = NULL;
	}
}
typedef int (FPTR(void));
int HandleProgressIDCMP( void )
{
	struct IntuiMessage	*m;
	int			(*func)(void);
	BOOL			running = TRUE;

	while( m = GT_GetIMsg( ProgressWnd->UserPort )) {

		CopyMem(( char * )m, ( char * )&ProgressMsg, (long)sizeof( struct IntuiMessage ));

		GT_ReplyIMsg( m );

		switch ( ProgressMsg.Class ) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh( ProgressWnd );
				GT_EndRefresh( ProgressWnd, TRUE );
				break;

			case	IDCMP_RAWKEY:
				running = ProgressRawKey();
				break;

			case	IDCMP_GADGETUP:
//			case	IDCMP_GADGETDOWN:
				func = (FPTR *)(( struct Gadget * )ProgressMsg.IAddress )->UserData;
				running = func();
				break;
		}
	}
	return( (int) running );
}

int OpenProgressWindow( void )
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		wleft = ProgressLeft, wtop = ProgressTop, ww, wh;

	NewList(&InfoList);

	ComputeFont( ProgressWidth, ProgressHeight );

	ww = ComputeX( ProgressWidth );
	wh = ComputeY( ProgressHeight );

	if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width ) wleft = Scr->Width - ww;
	if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height ) wtop = Scr->Height - wh;

	if ( ! ( g = CreateContext( &ProgressGList )))
		return( 1L );

	for( lc = 0, tc = 0; lc < Progress_CNT; lc++ ) {

		CopyMem((char * )&ProgressNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));

		ng.ng_VisualInfo = VisualInfo;
		ng.ng_TextAttr   = Font;
		ng.ng_LeftEdge   = OffX + ComputeX( ng.ng_LeftEdge );
		ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
		ng.ng_Width      = ComputeX( ng.ng_Width );
		ng.ng_Height     = ComputeY( ng.ng_Height);

		ProgressGadgets[ lc ] = g = CreateGadgetA((ULONG)ProgressGTypes[ lc ], g, &ng, ( struct TagItem * )&ProgressGTags[ tc ] );

		while( ProgressGTags[ tc ] ) tc += 2;
		tc++;

		if ( NOT g )
			return( 2L );
	}

		ProgressZoom[0] = ProgressZoom[1] = 0;
	if ( ProgressWdt )
		ProgressZoom[2] = TextLength( &Scr->RastPort, (UBYTE *)ProgressWdt, strlen((char *)ProgressWdt )) + 80;
	else
		ProgressZoom[2]  = 80L;
		ProgressZoom[3] = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

	if ( ! ( ProgressWnd = OpenWindowTags( NULL,
				WA_Left,	wleft,
				WA_Top,		wtop,
				WA_Width,	ww + OffX + Scr->WBorRight,
				WA_Height,	wh + OffY + Scr->WBorBottom,
				WA_IDCMP,	BUTTONIDCMP|LISTVIEWIDCMP|IDCMP_RAWKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH,
				WA_Gadgets,	ProgressGList,
				WA_Title,	ProgressWdt,
				WA_ScreenTitle,	"TSMorph ©1993/94 Topicsave Limited",
				WA_PubScreen,	Scr,
				WA_Zoom,	ProgressZoom,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( ProgressWnd, NULL );

	return( 0L );
}

void CloseProgressWindow( void )
{
	if ( ProgressWnd        ) {
		CloseWindow( ProgressWnd );
		ProgressWnd = NULL;
	}

	if ( ProgressGList      ) {
		FreeGadgets( ProgressGList );
		ProgressGList = NULL;
	}
}

