// MPImage - Amiga Image Conversion
// Copyright (C) © 1996 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.

// mark@topic.demon.co.uk
// 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 <proto/exec.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <proto/utility.h>

#include <string.h>

#include <stdlib.h>

#include "progress.h"

BOOL						  ShowProgress = FALSE;
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[2];
UWORD                  ProgressLeft = 215;
UWORD                  ProgressTop = 42;
UWORD                  ProgressWidth = 261;
UWORD                  ProgressHeight = 108;
UBYTE                 *ProgressWdt = (UBYTE *)"MPImage Progress";
struct TextAttr       *Font, Attr;
UWORD                  FontX, FontY;
UWORD                  OffX, OffY;

UWORD ProgressGTypes[] = {
	SLIDER_KIND,
	LISTVIEW_KIND
};

struct NewGadget ProgressNGad[] = {
	6, 78, 251, 11, NULL, NULL, GD_Progress, PLACETEXT_LEFT, NULL, NULL,
	6, 6, 251, 65, NULL, NULL, GD_Info, 0, NULL, NULL
};

extern struct List InfoList = {0};

ULONG ProgressGTags[] = {
	(GTSL_Max), 0, (GTSL_MaxLevelLen), 6, (GTSL_LevelFormat), (ULONG)"%4ld", (GTSL_LevelPlace), (PLACETEXT_BELOW), GTSL_Justification, GTJ_CENTER, (PGA_Freedom), LORIENT_HORIZ, (GA_RelVerify), TRUE, GA_Disabled,TRUE,(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 (!ShowProgress) {
		return (3L);
	}
	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;
	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;
		}
	}
	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] = (USHORT)~0;
	ProgressZoom[2] = TextLength( &Scr->RastPort, (UBYTE *)ProgressWdt, strlen((char *)ProgressWdt )) + 80;
	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,	SLIDERIDCMP|LISTVIEWIDCMP|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH,
				WA_Gadgets,	ProgressGList,
				WA_Title,	ProgressWdt,
				WA_ScreenTitle,	"MPImage ©1996 Mark John Paddock",
				WA_PubScreen,	Scr,
				WA_Zoom,	ProgressZoom,
				TAG_DONE )))
	return( 4L );

	GT_RefreshWindow( ProgressWnd, NULL );

	return( 0L );
}

void CloseProgressWindow( void )
{
	struct Node *node;
	if ( ProgressWnd        ) {
		CloseWindow( ProgressWnd );
		ProgressWnd = NULL;
	}

	if ( ProgressGList      ) {
		FreeGadgets( ProgressGList );
		ProgressGList = NULL;
	}
	while ((node = InfoList.lh_Head)->ln_Succ) {
		Remove(node);
		free(node->ln_Name);
		free(node);
	}
}
