/************************************************************************
*																								*
*		action																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Module containing the actions, you can do ...							*
*																								*
************************************************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/reqbase.h>
#include <private/help.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/reqproto.h>

#include	"settings.h"

/***************************************

					Definitions

***************************************/

#define	RP			( FirstWindow->RPort )
#define	TOSIZE	(( SHORT )( 10 * sizeof( struct Settings )))

/***************************************

				External References

***************************************/
/***************************************
					Functions
***************************************/

IMPORT	VOID			RefreshTSize( VOID );
IMPORT	VOID			RefreshRMarg( VOID );
IMPORT	VOID			RefreshLMarg( VOID );
IMPORT	VOID			RefreshPLength( VOID );
IMPORT	VOID			RefreshPWidth( VOID );
IMPORT	VOID			DoRefresh( VOID );

IMPORT	VOID			ShowText( SHORT );

/***************************************
					Variables
***************************************/

IMPORT	struct Window				  *FirstWindow;

IMPORT	SHORT								CurLay;
IMPORT	USHORT							OTSize;
IMPORT	SHORT								Topline;
IMPORT	UBYTE								LoadString[];
IMPORT	struct FileInfoBlock		  *FIB;
IMPORT	UBYTE								Dir[],
												File[],
												Path[];
IMPORT	struct FileRequester			FReq;

IMPORT	LONG								Memsize;
IMPORT	UBYTE							  *ActFile;

IMPORT	struct Settings				Sets[ 10 ];
IMPORT	SHORT								ActSet;
IMPORT	UBYTE								ScTitle[];

IMPORT	struct Gadget		TSize,
									RMarg,
									LMarg,
									PLength,
									PWidth;

/***************************************

					Declarations

***************************************/

VOID			About( VOID );
VOID			Load( VOID );
VOID			AskTSize( VOID );
VOID			AskRMarg( VOID );
VOID			AskLMarg( VOID );
VOID			AskPLength( VOID );
VOID			AskPWidth( VOID );
VOID			LoadDefSets( VOID );
VOID			SaveSettings( VOID );
VOID			LoadSettings( VOID );

/************************************************************************
*																								*
*		About																					*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Well, just try ...																*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		About( VOID )
{
	SimpleRequest(
	"                       PPrint V1.10\n\n"
	"             © 1991 by Marc Jackisch\n"
	"                       Im Schaufsfeld 17\n"
	"                       4018 Langenfeld\n"
	"                       GERMANY\n\n"
	"This is just a printing tool, capable  of printing a text\n"
	"just as I like it, i. e. tabs  are converted to any size,\n"
	"the pages can be numbered, the printing style can be set,\n"
	"and at the end of each page it  sends a form feed ( which\n"
	"probably is the most important point,  as I never found a\n"
	"tool which can do this ).\n"
	"  Please refer to the doc-file for more information about\n"
	"copyright and distribution.\n"
	"Hope you like it !\n"
	"                                           Marc"
	);
}

/************************************************************************
*																								*
*		Load																					*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Load a text file.	( incudes memory allocation, etc. )					*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		Load( VOID )
{
	BPTR							lock;
	BPTR							datei;

	/***/

	if( ActFile )
	{
		free( ActFile );
		ActFile	=	NULL;
	}

	FReq.Flags	&=	~FRQSAVINGM;
	FReq.Flags	|=	FRQLOADINGM;

	if( !FileRequester( &FReq ))
	{
		strcpy( &ScTitle[20], "< none >" );
		SetWindowTitles( FirstWindow, NULL, ScTitle );
		Topline	=	1;
		InitText();
		ShowText( 0 );
		return;
	}
	lock	=	Lock( Path, ACCESS_READ );
	if( lock == NULL )
	{
		SimpleRequest( "Could not open file\nobject in use" );
		return;
	}
	if( !Examine( lock, FIB ))
	{
		SimpleRequest( "Could not get information on file" );
		UnLock( lock );
		return;
	}
	Memsize	=	FIB->fib_Size;
	UnLock( lock );
	ActFile	=	( UBYTE * )malloc( Memsize );
	if( ActFile == NULL )
	{
		SimpleRequest( "Could not allocate memory\nfor the file" );
		return;
	}
	datei	=	Open( Path, MODE_OLDFILE );
	if( datei == NULL )
	{
		SimpleRequest( "Could not open file" );
		return;
	}
	if( Read( datei, ActFile, Memsize ) != Memsize )
	{
		Close( datei );
		SimpleRequest( "Error while reading !" );
		return;
	}
	Close( datei );
	stccpy( &ScTitle[20], File, 50 );
	SetWindowTitles( FirstWindow, NULL, ScTitle );
	Topline	=	1;
	InitText();
	ShowText( 0 );
	return;
}

/************************************************************************
*																								*
*		AskTSize																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Opens a requester asking for a new tab - size.							*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		AskTSize( VOID )
{
	static struct GetLongStruct	GL	=
	{
		"Tab size",
		0L,0L,12L,0L,NULL,0,0L,0L
	};

	/***/

	if( CurLay == 1 )
		ActivateGadget( &TSize, FirstWindow, NULL );
	else
	{
		GL.defaultval	=	Sets[ ActSet ].Tabsize;
		if( GetLong( &GL ))
		{
			Sets[ ActSet ].Tabsize	=	GL.result;
			RefreshTSize();
			if( OTSize != Sets[ ActSet ].Tabsize )
			{
				ShowText( Topline );
				OTSize	=	Sets[ ActSet ].Tabsize;
			}
		}
	}
}

/************************************************************************
*																								*
*		AskRMarg																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Opens a requester asking for a new right margin.						*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		AskRMarg( VOID )
{
	static struct GetLongStruct	GL	=
	{
		"Right margin",
		0L,0L,100000L,0L,NULL,0,0L,0L
	};

	/***/

	if( CurLay == 1 )
		ActivateGadget( &RMarg, FirstWindow, NULL );
	else
	{
		GL.defaultval	=	Sets[ ActSet ].RMarg;
		if( GetLong( &GL ))
		{
			Sets[ ActSet ].RMarg	=	GL.result;
			RefreshRMarg();
		}
	}
}

/************************************************************************
*																								*
*		AskLMarg																				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Opens a requester asking for a new left margin.							*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		AskLMarg( VOID )
{
	static struct GetLongStruct	GL	=
	{
		"Left margin",
		0L,0L,100000L,0L,NULL,0,0L,0L
	};

	/***/

	if( CurLay == 1 )
		ActivateGadget( &LMarg, FirstWindow, NULL );
	else
	{
		GL.defaultval	=	Sets[ ActSet ].LMarg;
		if( GetLong( &GL ))
		{
			Sets[ ActSet ].LMarg	=	GL.result;
			RefreshLMarg();
		}
	}
}

/************************************************************************
*																								*
*		AskPLength																			*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Opens a requester asking for a new page length.							*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		AskPLength( VOID )
{
	static struct GetLongStruct	GL	=
	{
		"Page length",
		0L,0L,100000L,0L,NULL,0,0L,0L
	};

	/***/

	if( CurLay == 2 )
		ActivateGadget( &PLength, FirstWindow, NULL );
	else
	{
		GL.defaultval	=	Sets[ ActSet ].PageLen;
		if( GetLong( &GL ))
		{
			Sets[ ActSet ].PageLen	=	GL.result;
			RefreshPLength();
		}
	}
}

/************************************************************************
*																								*
*		AskPWidth																			*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Opens a requester asking for a new page width.							*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		AskPWidth( VOID )
{
	static struct GetLongStruct	GL	=
	{
		"Page width",
		0L,0L,100000L,0L,NULL,0,0L,0L
	};

	/***/

	if( CurLay == 2 )
		ActivateGadget( &PWidth, FirstWindow, NULL );
	else
	{
		GL.defaultval	=	Sets[ ActSet ].PageWidth;
		if( GetLong( &GL ))
		{
			Sets[ ActSet ].PageWidth	=	GL.result;
			RefreshPWidth();
		}
	}
}

/************************************************************************
*																								*
*		LoadDefSets																			*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Loads the default settings or, if they do not exist, fills the		*
*		"Sets" vector with the values dedicated by preferences.				*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		LoadDefSets( VOID )
{
	BPTR							lock,
									datei;
	struct Preferences	  *Prefs,
									PrefBuf;
	SHORT							i;
	UBYTE							Name[128]	=	"S:";

	/***/

	strcpy( &Name[2], DSETNAME );
	lock	=	Lock( Name, ACCESS_READ );
	if( lock == NULL )
	{
		SimpleRequest( "Could not find the default settings.\n"
							"I will use your preferences" );
		Prefs	=	GetPrefs( &PrefBuf, sizeof( struct Preferences ));
		for( i = 0; i < 10; i++ )
		{
			Sets[ i ].LMarg		=	Prefs->PrintLeftMargin;
			Sets[ i ].RMarg		=	Prefs->PrintRightMargin;
			Sets[ i ].Tabsize		=	8;
			Sets[ i ].PageLen		=	Prefs->PaperLength;
			Sets[ i ].PageWidth	=	Sets[ i ].RMarg - Sets[ i ].LMarg;
			Sets[ i ].Style		=	( Prefs->PrintPitch != 0 ) ? STYLE_ELITE : STYLE_PICA;
			Sets[ i ].Numb			=	0;
			Sets[ i ].Cond			=	FALSE;
			Sets[ i ].LetterQ		=	( Prefs->PrintQuality == 0 ) ? FALSE : TRUE;
			Sets[ i ].LSpace6		=	( Prefs->PrintSpacing == 0 ) ? TRUE : FALSE;
		}
	}
	else
	{
		UnLock( lock );
		datei	=	Open( Name, MODE_OLDFILE );
		if( datei == NULL )
		{
			SimpleRequest(	"Could not open the default settings.\n"
								"Sorry, only zeros for you ..." );
			return;
		}
		if( Read( datei, ( UBYTE * )&Sets[0], TOSIZE ) != TOSIZE )
		{
			SimpleRequest(	"Error while reading the default settings !\n"
								"The results may be invaluable; please\n"
								"check all values before using them !" );
		}
		Close( datei );
	}
}

/************************************************************************
*																								*
*		SaveSettings																		*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Saves the settings to a file.													*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		SaveSettings( VOID )
{
	BPTR				lock,
						datei;
	UBYTE				TSD[ DSIZE + 1 ],
						TSF[ FCHARS + 1 ];

	/***/

	FReq.Flags	&=	~FRQLOADINGM;
	FReq.Flags	|=	FRQSAVINGM;
	strcpy( TSD, Dir );
	strcpy( TSF, File );
	strcpy( Dir, "s:" );
	strcpy( File, DSETNAME );

	if( FileRequester( &FReq ))
	{
		lock	=	Lock( Path, ACCESS_READ );
		if( lock != NULL )
		{
			if( !TwoGadRequest(	"File %s exists !\n"
										"Want to overwrite it ?", File ))
			{
				UnLock( lock );
				strcpy( Dir, TSD );
				strcpy( File, TSF );
				return;
			}
			UnLock( lock );
		}
		datei	=	Open( Path, MODE_NEWFILE );
		if( datei == NULL )
		{
			SimpleRequest(	"Could not open file\n"
								"%s\n"
								"for writing !!!", File );
			strcpy( Dir, TSD );
			strcpy( File, TSF );
			return;
		}
		if( Write( datei, ( UBYTE * )&Sets[0], TOSIZE ) != TOSIZE )
		{
			SimpleRequest(	"Error while writing file\n"
								"%s.\n"
								"Hope, you did not loose your data", File );
		}
		strcpy( Dir, TSD );
		strcpy( File, TSF );
		Close( datei );
	}
	else
	{
		strcpy( Dir, TSD );
		strcpy( File, TSF );
	}
}

/************************************************************************
*																								*
*		LoadSettings																		*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Loads alternate settings														*
*																								*
*-----------------------------------------------------------------------*
*																								*
*		Parameters		:																	*
*			none																				*
*																								*
*		Returns			:																	*
*			none																				*
*																								*
************************************************************************/

VOID		LoadSettings( VOID )
{
	BPTR		lock,
				datei;
	UBYTE		TSD[ DSIZE + 1 ],
				TSF[ FCHARS + 1 ];

	/***/

	FReq.Flags	&=	~FRQSAVINGM;
	FReq.Flags	|=	FRQLOADINGM;
	strcpy( TSD, Dir );
	strcpy( TSF, File );
	strcpy( Dir, "s:" );
	strcpy( File, DSETNAME );

	if( FileRequester( &FReq ))
	{
		lock	=	Lock( Path, ACCESS_READ );
		if( lock == NULL )
		{
			SimpleRequest( "Could not open file\nobject in use" );
			strcpy( Dir, TSD );
			strcpy( File, TSF );
			return;
		}
		if( !Examine( lock, FIB ))
		{
			SimpleRequest( "Could not get information on file" );
			UnLock( lock );
			strcpy( Dir, TSD );
			strcpy( File, TSF );
			return;
		}
		if( FIB->fib_Size != TOSIZE )
		{
			SimpleRequest(	"A file of a size %ld\n"
								"can never be a settings - file", FIB->fib_Size );
			UnLock( lock );
			strcpy( Dir, TSD );
			strcpy( File, TSF );
			return;
		}
		UnLock( lock );
		datei	=	Open( Path, MODE_OLDFILE );
		if( datei == NULL )
		{
			SimpleRequest(	"Could not open file" );
			strcpy( Dir, TSD );
			strcpy( File, TSF );
			return;
		}
		if( Read( datei, ( UBYTE * )&Sets[0], TOSIZE ) != TOSIZE )
		{
			SimpleRequest(	"Error while reading the settings file !\n"
								"The results may be invaluable; please\n"
								"check all values before using them !" );
		}
		Close( datei );
		DoRefresh();
		strcpy( Dir, TSD );
		strcpy( File, TSF );
	}
	else
	{
		strcpy( Dir, TSD );
		strcpy( File, TSF );
	}
}
