/* MPMorph - Amiga Morphing program */
/* Copyright (C) © 1993-97 Mark John Paddock */

/* 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 <exec/memory.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 <graphics/scale.h>
#include <string.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>

#ifdef __GNUC__
#include <inline/exec.h>
#include <inline/intuition.h>
#include <inline/gadtools.h>
#include <inline/graphics.h>
#include <inline/utility.h>
#else
#include <pragmas/exec_sysbase_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/utility_pragmas.h>
#endif

extern struct ExecBase *SysBase;
extern struct GfxBase *GfxBase;
extern struct UtilityBase *UtilityBase;

#include <dos/dos.h>
#include "progress.h"

extern const char Version[];

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 *)"MPRender Progress";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

extern BOOL NoProgress;

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
};

struct NewGadget StopGadget = {
	0, 0, 95, 19, (UBYTE *)"Stop", NULL, GD_Stop, PLACETEXT_IN, NULL, (APTR)StopClicked
};

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;

if (SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
	return StopClicked();
}

if (ProgressWnd) {
	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 );
}
else {
	return 1;
}
}

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 );

	if (NoProgress) {
		StopGadget.ng_LeftEdge		= OffX;
		StopGadget.ng_TopEdge		= OffY;
		StopGadget.ng_VisualInfo 	= VisualInfo;
		StopGadget.ng_TextAttr		= Font;
		StopGadget.ng_Width			= ComputeX(95);
		StopGadget.ng_Height			= ComputeY(19);
		ProgressGadgets[GD_Stop] = g = CreateGadget(BUTTON_KIND,g,&StopGadget,TAG_END);
	}
	else {
	 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] = (USHORT)~0;
	ProgressZoom[2] = TextLength( &Scr->RastPort, &(Version[6]), strlen(&(Version[6]))) + 80;
	ProgressZoom[3] = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

	if (NoProgress) {
		if (!(ProgressWnd = OpenWindowTags( NULL,
				WA_Left,		0,
				WA_Top,		0,
				WA_Width,	ComputeX(95+2*OffX),
				WA_Height,	ComputeY(19+OffY+Scr->WBorBottom),
				WA_IDCMP,	BUTTONIDCMP|IDCMP_RAWKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH,
				WA_Title,	(ULONG)(&(Version[6])),
				WA_ScreenTitle,	(ULONG)"MPRender ©1997 Mark John Paddock",
				WA_PubScreen,	(ULONG)Scr,
				TAG_DONE )))
		return( 4L );
	}
	else {
	 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,	(ULONG)ProgressGList,
				WA_Title,	(ULONG)(&(Version[6])),
				WA_ScreenTitle,	(ULONG)"MPRender ©1997 Mark John Paddock",
				WA_PubScreen,	(ULONG)Scr,
				WA_Zoom,	(ULONG)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;
	}
}

