/********************************************************************************/
/*								MODULE DIALOGWINDOW							*/
/********************************************************************************/

#include <stdlib.h>
#include <unistd.h>
#include "ZipGUI.h"
#include "helpers.h"
#include "pathname.h"
#include "macglob.h"
#include "GetFold.h"
#include "VolWarn.h"
#include "Credits.h"


#define	rSaveChanges		128
#define	rErrorAlert			129					
#define	rDuplicVolume		130		
#define	rWaringForSwitch	131		
			

#define	rAboutBox			128					
#define	rCmdLineBox			129					

#define kEntryBoxCmdLine    3
#define kItemName           7
#define kCurrendPath		8

enum	{
    kZipLastCommand = 128,
    kZipArchivePath,
    kZipSearchPath,
    kUnzipArchive,
    kUnzipRestorePath,
    kZipAddSwitches,
    kUnzipAddSwitches
    };

#define	kButtonFrameInset	-4
#define kButtonFrameSize	3
#define kFillSize			16



/***** Elements of the Zip-Dialog *****************************************/

#define kZipDialogBox		130
enum	{
	kZf_Switch	= 3,
	kZu_Switch,
	kZm_Switch,
	kZr_Switch,
	kZj_Switch,
	kZoutput_1,
	kZoutput_2,
	kZZipArchive_But,
	kZModifiedAfter,
	kZEntryBoxFileDate,
	kZStartPath_But,
	kZdf_Switch,
	kZadd_switches = 17,
	kZRunTestArchiveAfterwards,
	kZIgnoreEnvironment
	};
	


/***** Elements of the UnZip-Dialog *****************************************/

#define kUnZipDialogBox		131
enum	{
	kUZZl_Switch	= 3,
	kUZt_Switch,
	kUZa_Switch,
	kUZf_Switch,
	kUZu_Switch,
	kUZoutput_1,
	kUZoutput_2,
	kUZZipArchive_But,
	kUZExtract_But,
	kUZdf_Switch,
	kUZadd_switches = 14,
	kUZZv_Switch,
	kUZIgnoreEnvironment
	};
	

char *DoUnZipMoreDialogBox( void );
char *DoStdFileDialog(char *ReturnedFileName, unsigned char *thePrompt,
					  Boolean ShowAny, Boolean ShowFiles);
char *ProcessFileButton(DialogRef	myDialog,
						char *ReturnedFileName,unsigned char *msg, 
						short kOutPut, Boolean ShowAny, Boolean ShowFiles);
void SwitchCheckBox(DialogRef myDialog,short kCheckBox,Boolean *Switch);
void SetCheckBox(DialogRef myDialog,short kCheckBox,Boolean Switch);
void DisplayPath(char *buffer, short ResourceID, DialogRef	myDialog);
void exit(int status);

extern ZipGUI_Globals	g;
extern MacZipGlobals	MacZip;
extern char FileNamePattern[257];
extern Boolean IgnoreEnvironment;




/********************************************************************************/
/*	Zip-Dialogfunc														*/
/********************************************************************************/

void DoZipDialogBox( void )
{
	DialogRef	myDialog;
	Handle		myItem;
	short		itemHit;
	short 		ignoreType;
	Rect		ignoreBox;
	
	/* elements of the command-line */
	char	switches[50];
	char 	datestr[50];
	char    *CommandLine, *envptr;
	unsigned long CmdLength;
	
	unsigned char *disp_path = NULL;	
	char *ReturnedFileName = NULL;
	char *add_switches = NULL;
	char *returned_path2 = NULL;
	char *ZipPath        = NULL;

	unsigned char pdatestr[50];
	unsigned char padd_switches[50];
	
 Boolean	f_Switch = false;
 Boolean	u_Switch = false;
 Boolean	m_Switch = false;
 Boolean	r_Switch = false;
 Boolean	j_Switch = false;
 Boolean	df_Switch = false;
 Boolean	ModifiedAfter = false;

switches[0]	= 0;
datestr[0]	= 0;

myDialog = GetNewDialog( kZipDialogBox, nil, kInFront );
Assert_it(myDialog,"GetNewDialog myDialog == NULL","")

if (myDialog == NULL)
	{
    SysBeep(30);
	return;
	}
	
SetCheckBox(myDialog,kZf_Switch,false);
SetCheckBox(myDialog,kZu_Switch,false);
SetCheckBox(myDialog,kZm_Switch,false);
SetCheckBox(myDialog,kZr_Switch,false);
SetCheckBox(myDialog,kZj_Switch,false);
SetCheckBox(myDialog,kZdf_Switch,false);
SetCheckBox(myDialog,kZModifiedAfter,false);
SetCheckBox(myDialog,kZRunTestArchiveAfterwards,false);
SetCheckBox(myDialog,kZIgnoreEnvironment,false);

	if ( myDialog ) {

		ShowWindow( (WindowRef)myDialog );
		do {
			ModalDialog( g.modalFilterUPP, &itemHit );
			Assert_it(g.modalFilterUPP,"g.modalFilterUPP == NULL","")
			switch ( itemHit ) {
					
			 	case kZIgnoreEnvironment:		
			 		SwitchCheckBox(myDialog, kZIgnoreEnvironment, &IgnoreEnvironment);
					break;

			 	case kZf_Switch:		
			 		SwitchCheckBox(myDialog, kZf_Switch, &f_Switch);
					break;
			
			 	case kZu_Switch:
			 		SwitchCheckBox(myDialog, kZu_Switch, &u_Switch);
					break;

			 	case kZm_Switch:	
			 		SwitchCheckBox(myDialog, kZm_Switch, &m_Switch);
					break;

			 	case kZr_Switch:	
			 		SwitchCheckBox(myDialog, kZr_Switch, &r_Switch);
					break;

			 	case kZj_Switch:
			 		SwitchCheckBox(myDialog, kZj_Switch, &j_Switch);
					break;

			 	case kZdf_Switch:	
			 		SwitchCheckBox(myDialog, kZdf_Switch, &df_Switch);
					break;

			 	case kZModifiedAfter:	
			 		SwitchCheckBox(myDialog, kZModifiedAfter, &ModifiedAfter);
					break;
					
			 	case kZRunTestArchiveAfterwards:
			 		SwitchCheckBox(myDialog, kZRunTestArchiveAfterwards, 
			 		                         &g.RunTestArchiveAfterwards);
					break;
			 	
				case kZZipArchive_But:
					ReturnedFileName = StrCalloc(NAME_MAX);	
					Assert_it(ReturnedFileName,"ReturnedFileName == NULL","")
					strcpy(ReturnedFileName,"Archive");
					ZipPath = ProcessFileButton(myDialog, ReturnedFileName,
													"\p Select a Archive:", kZoutput_1, 
													false, true);
					Assert_it(g.archive_path,"g.archive_path == NULL","") 
					ReturnedFileName = StrFree(ReturnedFileName);							
					break;
					
										
				case kZStartPath_But:
					ReturnedFileName = StrCalloc(NAME_MAX);	
					strcpy(ReturnedFileName,"*");
					returned_path2 = ProcessFileButton(myDialog, ReturnedFileName,
											"\p Select a Folder or File:", kZoutput_2, 
											true, true);
					ReturnedFileName = StrFree(ReturnedFileName);							
					break;
					
					
			default:
				break;		
			
			}
		} while ( itemHit > kStdCancelItemIndex );    /* kStdCancelItemIndex = 2 */
													   /* kStdOkItemIndex     = 1 */
		switch ( itemHit ) {
					
		 	case kStdOkItemIndex:		/* ok-button */
		 
		        g.PathAreOK = false;
		        
		        /* both pathnames must be allocated and valid */
		        if ((ZipPath == NULL) && (returned_path2 == NULL))
		        	{
		        	DisposeDialog( myDialog );	
		        	return;
		        	}
		        
		 		strcpy(switches," ");
		 		if (f_Switch) strcat(switches, "-f ");
		 		if (u_Switch) strcat(switches, "-u ");
		 		if (m_Switch) strcat(switches, "-m ");
		 		if (r_Switch) strcat(switches, "-r ");
		 		if (j_Switch) strcat(switches, "-j ");	
		 		if (df_Switch) strcat(switches, "-df ");
		 		 		
			    GetDialogItem( myDialog, kZEntryBoxFileDate, &ignoreType, &myItem, &ignoreBox );
				GetDialogItemText(myItem, pdatestr);
				Assert_it(returned_path2,"returned_path2 == NULL","") 
				
			    GetDialogItem( myDialog, kZadd_switches, &ignoreType, &myItem, &ignoreBox );
				GetDialogItemText(myItem, padd_switches);
				Assert_it(padd_switches,"GetDialogItemText padd_switches == 0","") 
				
				add_switches = P2CStr(padd_switches);
				
				pdatestr[pdatestr[0]+1] = 0x00;   /* convert to c-string */
				memset(datestr, 0, sizeof(datestr));
				if (ModifiedAfter) 
					{
					strcat(datestr, " -t ");	
					strcat(datestr, (char *)&pdatestr[1]);
					}

					/* calculate length of command-line */					
				CmdLength = strlen(switches);
				CmdLength = strlen(add_switches) 	+ CmdLength;
				CmdLength = strlen(datestr) 		+ CmdLength;
				
				Assert_it(ZipPath,"ZipPath == NULL","") 
				if (ZipPath != NULL)
					{
					CmdLength = strlen(ZipPath) 	+ CmdLength;
					}
					
				if (returned_path2 != NULL)
					{
					CmdLength = strlen(returned_path2) 	+ CmdLength;
					}
				
				CommandLine = StrCalloc(CmdLength + 100);	
				
				strcat(CommandLine, switches);
				strcat(CommandLine, add_switches);

				strcat(CommandLine, datestr);
				
				Assert_it(ZipPath,"g.archive_path == NULL","") 
				if (ZipPath != NULL)
					{
					strcat(CommandLine, " \"");	
					strcat(CommandLine, ZipPath);
					strcat(CommandLine, "\" ");	
					}

				if (returned_path2 != NULL)
					{					
					strcat(CommandLine, " \"");	
					strcat(CommandLine, returned_path2);
					strcat(CommandLine, "\" ");	
					}
				
				if ((strlen(ZipPath) > 5) &&
				    (strlen(returned_path2) > 5)) g.PathAreOK = true;
				    
				break;

			case kStdCancelItemIndex: 	/* cancel-button */
				ZipPath = StrFree(ZipPath);
				returned_path2 = StrFree(returned_path2);
				DisposeDialog( myDialog );	
				return;			
				break;      /* do nothing special */
		}
		returned_path2 = StrFree(returned_path2);

		DisposeDialog( myDialog );
	} else
	{
		Assert_it(0,"DoZipDialogBox","reached wrong point") 
		SysBeep( 30 );
		return;
	}
		
envptr = getenv("TestArchiveAfterwards");
if ((!(envptr == (char *)NULL || *envptr == '\0')) && g.PathAreOK)
	{
	if ((stricmp(envptr,"yes") == 0) && (!IgnoreEnvironment))
		g.RunTestArchiveAfterwards = true;
	else 
		g.RunTestArchiveAfterwards = false;
	}

Assert_it(ZipPath,"ZipPath == NULL","") 
if (g.CurrentApp != g.LastApp) DoCloseAllWin();		
StartApp(CommandLine, true, ZipPath);
} /* DoZipDialogBox */






/********************************************************************************/
/*	UnZip-Dialogfunc														*/
/********************************************************************************/

void DoUnZipDialogBox( void )
{
	DialogRef	myDialog;
	Handle		myItem;
	short		itemHit;
	short 		ignoreType;
	Rect		ignoreBox;
	
	/* elements of the command-line */
	char	switches[50];
	char    *CommandLine;
	unsigned long CmdLength;
	
	unsigned char *disp_path = NULL;	
	char *ReturnedFileName = NULL;
	char *ExtractFolder   = NULL;
	char *add_switches    = NULL;
	char *more_switches   = NULL;
	char *returned_path1  = NULL;
	char *returned_path2  = NULL;

	unsigned char padd_switches[50];

 Boolean	Zl_Switch = false;
 Boolean	t_Switch = false;
 Boolean	a_Switch = false;
 Boolean	f_Switch = false;
 Boolean	u_Switch = false;
 Boolean	Zv_Switch = false;

switches[0]	= 0;

myDialog = GetNewDialog( kUnZipDialogBox, nil, kInFront );
Assert_it(myDialog,"GetNewDialog myDialog == NULL","")

if (myDialog == NULL)
	{
    SysBeep(30);
	return;
	}

SetCheckBox(myDialog,kUZZl_Switch,false);
SetCheckBox(myDialog,kUZt_Switch,false);
SetCheckBox(myDialog,kUZa_Switch,false);
SetCheckBox(myDialog,kUZf_Switch,false);
SetCheckBox(myDialog,kUZu_Switch,false);
SetCheckBox(myDialog,kUZZv_Switch,false);
SetCheckBox(myDialog,kUZIgnoreEnvironment,false);

ExtractFolder = StrCalloc(NAME_MAX);	
strcpy(ExtractFolder,"Extract-Folder");

	if ( myDialog ) {

		ShowWindow( (WindowRef)myDialog );
		do {
			ModalDialog( g.modalFilterUPP, &itemHit );
			Assert_it(g.modalFilterUPP,"g.modalFilterUPP == NULL","")
			switch ( itemHit ) {

			 	case kUZIgnoreEnvironment:		
			 		SwitchCheckBox(myDialog, kUZIgnoreEnvironment, &IgnoreEnvironment);
					break;
					
			 	case kUZZl_Switch:		
			 		SwitchCheckBox(myDialog, kUZZl_Switch, &Zl_Switch);
					break;
			
			 	case kUZt_Switch:		
			 		SwitchCheckBox(myDialog, kUZt_Switch, &t_Switch);
					break;

			 	case kUZa_Switch:	
			 		SwitchCheckBox(myDialog, kUZa_Switch, &a_Switch);
					break;

			 	case kUZf_Switch:
			 		SwitchCheckBox(myDialog, kUZf_Switch, &f_Switch);
					break;

			 	case kUZu_Switch:	
			 		SwitchCheckBox(myDialog, kUZu_Switch, &u_Switch);
					break;

				case kUZZv_Switch:
			 		SwitchCheckBox(myDialog, kUZZv_Switch, &Zv_Switch);
					break;				
				
				case kUZZipArchive_But:
					ReturnedFileName = StrCalloc(NAME_MAX);	
					strcpy(ReturnedFileName,"Archive.zip");
					returned_path1 = ProcessFileButton(myDialog, ReturnedFileName,
													"\p Select a Archive:", kUZoutput_1, 
													false, true);				
					Assert_it(ExtractFolder,"ExtractFolder == NULL","")
					strcpy(ExtractFolder,returned_path1);
					FindNewExtractFolder(ExtractFolder);	
					strcpy(ReturnedFileName,ExtractFolder);
					ReturnedFileName[strlen(ReturnedFileName) - 1] = 0;
					GetFilename(ExtractFolder, ReturnedFileName);
					strcat(ExtractFolder,":");
					ReturnedFileName = StrFree(ReturnedFileName);
					break;
									
				case kUZExtract_But:
					returned_path2 = ProcessFileButton(myDialog, ExtractFolder,
											"\p Select a Folder:", kUZoutput_2, 
											false, false);			
					break;
					
										
			default:
				break;		
			
			}
		/* Schleife bis zum Klick in OK abarbeiten */
		} while ( itemHit > kStdCancelItemIndex );    /* kStdCancelItemIndex = 2 */
													   /* kStdOkItemIndex     = 1 */
		switch ( itemHit ) {
					
		 	case kStdOkItemIndex:		/* ok-button */
		 	    g.PathAreOK = false;
		        /* both pathnames must be allocated and valid */
		        if ((returned_path1 == NULL) && (returned_path2 == NULL))
		        	{
		        	DisposeDialog( myDialog );	
		        	return;
		        	}

		 	    strcpy(switches," ");
		 		if (Zl_Switch) strcat(switches, "-Zl ");
		 		if (t_Switch) strcat(switches, "-t ");
		 		if (a_Switch) strcat(switches, "-a ");
		 		if (f_Switch) strcat(switches, "-f ");
		 		if (u_Switch) strcat(switches, "-u ");	
		 		if (Zv_Switch) strcat(switches, "-Zv ");
		 		 		
			    GetDialogItem( myDialog, kUZadd_switches, &ignoreType, &myItem, &ignoreBox );
				GetDialogItemText(myItem, padd_switches);
				Assert_it(padd_switches,"GetDialogItem padd_switches == 0","") 
		
				add_switches = P2CStr(padd_switches);
				
					/* calculate length of command-line */					
				CmdLength = strlen(switches);
				CmdLength = strlen(add_switches) 	+ CmdLength;
				CmdLength = strlen(returned_path1) 	+ CmdLength;
				
				if (returned_path1 != NULL)
					{
					CmdLength = strlen(returned_path1) 	+ CmdLength;
					}
				if (returned_path2 != NULL)
					{
					CmdLength = strlen(returned_path2) 	+ CmdLength;
					}					
				CommandLine = StrCalloc(CmdLength + 100);	
				
				strcat(CommandLine, switches);
				strcat(CommandLine, add_switches);
				
				if (returned_path2 != NULL)
					{
					strcat(CommandLine, " -d \"");	
					strcat(CommandLine, returned_path2);
					strcat(CommandLine, "\" ");	
					}

				if (returned_path1 != NULL)
					{				
					strcat(CommandLine, " \"");	
					strcat(CommandLine, returned_path1);
					strcat(CommandLine, "\" ");	
					}
     			if (strlen(returned_path1) > 5) g.PathAreOK = true;

				break;

			case kStdCancelItemIndex: 	/* cancel-button */
				returned_path1 = StrFree(returned_path1);
				returned_path2 = StrFree(returned_path2);
                ExtractFolder = StrFree(ExtractFolder);	
				DisposeDialog( myDialog );	
				return;			
				break;      /* do nothing special */
		}

		returned_path1 = StrFree(returned_path1);
		returned_path2 = StrFree(returned_path2);
        ExtractFolder = StrFree(ExtractFolder);	
		DisposeDialog( myDialog );
	} else
	{

		Assert_it(0,"DoUnZipDialogBox","reached wrong point") 
		SysBeep( 30 );
		return;
	}
		
if (g.CurrentApp != g.LastApp) DoCloseAllWin();		
StartApp(CommandLine, true, NULL);
} /* DoUnZipDialogBox */






/********************************************************************************/
/*	CmdLine Dialogfunc   														*/
/********************************************************************************/

void DoCmdBox( void )
{
	DialogRef	myDialog;
	Handle		myItem;
	short		itemHit;
	short 		ignoreType;
	Rect		ignoreBox;
	static unsigned char buffer[257];
	StringHandle	textHandle;

	/* get last used command line from string resource */
	textHandle = GetString( kZipLastCommand );
 
	myDialog = GetNewDialog( rCmdLineBox, nil, kInFront );
	Assert_it(myDialog,"GetNewDialog myDialog == NULL","")

	if (myDialog == NULL)
		{
    	SysBeep(30);
		return;
		}

	GetDialogItem( myDialog, kItemName, &ignoreType, &myItem, &ignoreBox );
	SetDialogItemText( myItem, "\pCurrent Path:" ); 
	
	GetDialogItem( myDialog, kEntryBoxCmdLine, &ignoreType, &myItem, &ignoreBox );

	HLock( (Handle)textHandle );  /* fill entry box with last used command line */
	SetDialogItemText( myItem, *textHandle );
	HUnlock( (Handle)textHandle );
	
	Assert_it(buffer,"DoCmdBox buffer == NULL","")
	getcwd((char *)buffer, sizeof(buffer));
	
	DisplayPath((char *)buffer, kCurrendPath, myDialog);
	
	if ( myDialog ) {

		ShowWindow( (WindowRef)myDialog );
		do {
			ModalDialog( g.modalFilterUPP, &itemHit );
			switch ( itemHit ) {
			
			}
		} while ( itemHit > kStdCancelItemIndex );    /* kStdCancelItemIndex = 2 */
													   /* kStdOkItemIndex     = 1 */
		switch ( itemHit ) {
					
		 	case kStdOkItemIndex:		/* ok-button */
			    GetDialogItem( myDialog, kEntryBoxCmdLine, &ignoreType, &myItem, &ignoreBox );
				GetDialogItemText(myItem, buffer);
				HLock( (Handle)textHandle );
				GetDialogItemText( myItem, *textHandle );
				HUnlock( (Handle)textHandle );         /* write last used command line  */
				ChangedResource( (Handle)textHandle ); /* to string resource */
				WriteResource( (Handle)textHandle );

				buffer[buffer[0]+1] = 0x00;   /* convert to c-string */
				g.PathAreOK = true;
				break;
		
			case kStdCancelItemIndex: 	/* cancel-button */
				DisposeDialog( myDialog );	
				return;			
				break;      /* do nothing special */
		}
	
		DisposeDialog( myDialog );
	} else
		SysBeep( 30 );

ChooseActionStartCommand((char *)&buffer[1]);
} /* DoCmdBox */




/********************************************************************************/
/*	About-Dialog anzeigen														*/
/********************************************************************************/

#define koutput_1	3

#define koutput_2	4
#define koutput_3	5

#define koutput_4	6
#define koutput_5	7

#define kCreditsButton	2

void DoAboutBox( void )
{
	Str255		myTitle;
	DialogRef	myDialog;
	WindowRef	myWindow = nil;
	DocumentPtr	myDoc;
	
	Handle		myItem;
	short		itemHit;
	short 		ignoreType;
	Rect		ignoreBox;
	char unsigned *ZipCopyright;
	void *ZipVersionInfo;    /* it's declared to void to avoid multible conversions  */
	void *UnZipInfoVersion;
	char unsigned *ZipVersionLocal;
	char unsigned *UnZipLocalVersion;
	char *tmpPtr;
	
	myDialog = GetNewDialog( rAboutBox, nil, kInFront );
	Assert_it(myDialog,"GetNewDialog myDialog == NULL","")
	
	if (myDialog == NULL)
		{
    	SysBeep(30);
		return;
		}

	ZipCopyright		=  C2PStr(GetZipCopyright());
	GetDialogItem( myDialog, koutput_1, &ignoreType, &myItem, &ignoreBox );
	SetDialogItemText( myItem, ZipCopyright ); 

	ZipVersionInfo		=  GetZipVersionsInfo();
  for (tmpPtr = ZipVersionInfo; *tmpPtr; tmpPtr++)  /* exchange '\n' in '\r' */
    if (*tmpPtr == '\n')
      *tmpPtr = '\r';
	C2PStr(ZipVersionInfo);
	
	GetDialogItem( myDialog, koutput_2, &ignoreType, &myItem, &ignoreBox );
	SetDialogItemText( myItem, ZipVersionInfo ); 

	ZipVersionLocal	=  C2PStr(GetZipVersionLocal());
	GetDialogItem( myDialog, koutput_4, &ignoreType, &myItem, &ignoreBox );
	SetDialogItemText( myItem, ZipVersionLocal ); 




	UnZipInfoVersion	=  GetUnZipInfoVersions();		
  for (tmpPtr = UnZipInfoVersion; *tmpPtr; tmpPtr++)  /* exchange '\n' in '\r' */
    if (*tmpPtr == '\n')
      *tmpPtr = '\r';
	C2PStr(UnZipInfoVersion);

	GetDialogItem( myDialog, koutput_3, &ignoreType, &myItem, &ignoreBox );
	SetDialogItemText( myItem, UnZipInfoVersion ); 

	UnZipLocalVersion	=  C2PStr(GetUnZipLocalVersion());
	GetDialogItem( myDialog, koutput_5, &ignoreType, &myItem, &ignoreBox );
	SetDialogItemText( myItem, UnZipLocalVersion ); 



	if ( myDialog ) {

		ShowWindow( (WindowRef)myDialog );
		do {
			ModalDialog( g.modalFilterUPP, &itemHit );
			switch ( itemHit ) {
					
			}
		} while ( itemHit > kCreditsButton );	
		switch ( itemHit ) {
					
		 	case kStdOkItemIndex:		/* ok-button */ 	
				DisposeDialog( myDialog );
				break;
				
		 	case kCreditsButton:		 
		 		DisposeDialog( myDialog );	
		 		
		 		myWindow = DoNewDocWindow();
				myDoc = (DocumentPtr)myWindow;

				TEInsert( CreditsText , sizeof(CreditsText), myDoc->docTxt );

				DoAdjustScrollValues( myWindow );
				GetIndString( myTitle, rWindowTitles, CreditsTextTitle );
				SetWTitle( myWindow, myTitle );
				ShowWindow( myWindow );
			break;
			
			}

	} else
		SysBeep( 30 );
} /* DoAboutBox */





short DoAskUser( WindowRef myWindow )
{
	Str255	myTitle;
	Assert_it(myWindow,"DoAskUser myWindow == NULL","")
	
	SetCursor( &qd.arrow );
	GetWTitle( myWindow, myTitle );
	Assert_it(myTitle,"DoAskUser myTitle == NULL","")
	ParamText( myTitle, "\p", "\p", "\p");
	return CautionAlert( rSaveChanges, g.modalFilterUPP );
} /* DoAskUser */





/*
Since Macintosh users are used to be guided by their
software in order not to do something stupid, I will
post an extra warning if the options -m and data fork 
only are both cecked.
*/

Boolean DoWaringForSwitch( void )
{
short 			result;
WindowRef		myWindow = FrontWindow();

DoCloseWindow(myWindow);
myWindow = nil;

result = CautionAlert( rWaringForSwitch, g.modalFilterUPP );

if (result == 1) {
    DoInitGlobals();
	return true;   /* stop was pressed */
}

return false;
}


/*

void DoWarnUserDupVol( char *FullPath )
displays a warning message about volume-names. This func must be called
*after* setjump() because it uses exit() which uses longjmp().

*/
void DoWarnUserDupVol( char *FullPath )
{
char 			volume[257];
Str255			myTitle;
short 			result;
WindowRef		myWindow = FrontWindow();
DocumentPtr		myDoc;
OSErr			myErr = noErr;
short    DoNotWarnAnyMore = false;

DoCloseWindow(myWindow);
myWindow = nil;

if (DoNotWarnAnyMore) return;
else
	{
	GetVolumeFromPath(FullPath,volume);

	ParamText( C2PStr(volume), "\p", "\p", "\p");
	result = CautionAlert( rDuplicVolume, g.modalFilterUPP );
	DoNotWarnAnyMore = true;
	}

if (result == 1) exit(0);   /* stop was pressed */

if (result == 3) /* user wants a descriptions */
{

	myWindow = DoNewDocWindow();
	
	if ( myWindow ) 
		{
		myDoc = (DocumentPtr)myWindow;

		TEInsert( DuplicVolumeNote , sizeof(DuplicVolumeNote), myDoc->docTxt );

		DoAdjustScrollValues( myWindow );
		GetIndString( myTitle, rWindowTitles, DuplicVolumeName );
		Assert_it(myTitle,"DoWarnUserDupVol myTitle == NULL","")
		SetWTitle( myWindow, myTitle );
		ShowWindow( myWindow );
		exit(0);
		} 
		else 
		{
		DoCloseWindow( myWindow );
		DoAlertUser( eOpenError, false );
		}
}

return;
} /* DoWarnUserDupVol */


/********************************************************************************/
/*	Warndialog 								*/
/********************************************************************************/

void DoAlertUser( short myErrCode, Boolean quitFlag )
{
	Str255		myMessage;
	WindowRef	myWindow = FrontWindow();
	
	DoCloseWindow(myWindow);
	
	SetCursor( &qd.arrow );
	GetIndString( myMessage, rErrStrings, myErrCode );
	Assert_it(myMessage,"DoAlertUser myMessage == NULL","")
	ParamText( myMessage, "\p", "\p", "\p" );
	StopAlert( rErrorAlert, g.modalFilterUPP );
	
	if ( quitFlag )									 
		ExitToShell();
} /* DoAlertUser */






pascal Boolean DoModalFilter( DialogRef myDialog, EventRecord *myEvent, 
															short *itemHit )
{
	Boolean		result = false;
	unsigned long myKey, myPart;
	WindowRef	myWindow;
	
	Assert_it(myDialog,"DoModalFilter myDialog == NULL","")
	if ( ((DialogRef)myEvent->message != myDialog) ) {
	
		switch ( myEvent->what ) {

			case keyDown:
			case autoKey:
				myKey = myEvent->message & charCodeMask;
				if ( myEvent->modifiers & cmdKey ) {
					if ( myKey == kPeriod ) {
						result = DoFlashButton( myDialog, kStdCancelItemIndex );
						*itemHit = kStdCancelItemIndex;
					}
				} else {
					if ( myKey == kCarReturn || myKey == kEnter ) {
						result = DoFlashButton( myDialog, kStdOkItemIndex );
						*itemHit = kStdOkItemIndex;
					} else if ( myKey == kEscape ) {
						result = DoFlashButton( myDialog, kStdCancelItemIndex );
						*itemHit = kStdCancelItemIndex;
					}
				}
				break;
			
			case mouseDown:
				if ( myEvent->modifiers & cmdKey ) {
					myPart = FindWindow( myEvent->where, &myWindow);	
					if ( (myPart == inDrag) )
						DoDragWindow( myEvent, myWindow );
				}
				break;

			default:
				DoEventDispatch( myEvent );
				break;
		}
	}
	return result;
} /* DoModalFilter */




Boolean DoFlashButton( DialogRef myDialog, short itemHit )
{
	short	itemType;
	Handle	itemHandle;
	Rect	ignoreBox;

    Assert_it(myDialog,"DoFlashButton myDialog == NULL","")
	GetDialogItem( myDialog, itemHit, &itemType, &itemHandle, &ignoreBox );
	if ( itemType != kButtonDialogItem )
		return false;
	HiliteControl( (ControlRef)itemHandle, 1 );
	Delay( kDelayTime, &g.ignoreTicks );
	HiliteControl( (ControlRef)itemHandle, 0 );
	return true;
} /* DoFlashButton */





void FatalError(char *cString, short myErrCode, Boolean quitFlag )
{
unsigned char pString[255];
char buffer[255];
	
	SetCursor( &qd.arrow );
	
	Assert_it(cString,"FatalError cString == NULL","")
	sprintf(buffer, "%s rc: %d",cString,myErrCode);
	ParamText( CToPCpy(pString,buffer) , "\p", "\p", "\p" );
	
	StopAlert( rErrorAlert, NULL );
	if ( quitFlag )									 
		ExitToShell();
} /* DoAlertUser */




char *DoStdFileDialog(char *ReturnedFileName1, unsigned char *thePrompt, 
					  Boolean ShowAny,Boolean ShowFiles)
{
	StandardFileReply	mySFR;
	OSErr				err;
	char 				*returned_path;
	const  short 		numTypes = 1;
	OSType 				typeList[numTypes] = { 'ZIP ' };
	unsigned char      *ReturnedFileName2;
	
	Assert_it(ReturnedFileName1,"DoStdFileDialog ReturnedFileName1 == NULL","")
	Assert_it(thePrompt,"DoStdFileDialog thePrompt == NULL","")
	ReturnedFileName2 = C2PStr(ReturnedFileName1);

	myCustomGetFile( &mySFR, typeList , numTypes, 
					thePrompt, ReturnedFileName2, ShowAny,ShowFiles);
					
	ReturnedFileName1 = P2CStr(ReturnedFileName2);
	returned_path = StrCalloc(1024);	

	if( mySFR.sfGood )
		{
		GetFullPathFromSpec(returned_path, &mySFR.sfFile , &err);
		return returned_path;
		} 
		
return NULL;
} /* DoStdFileDialog */




char *ProcessFileButton(DialogRef	myDialog,
						char *ReturnedFileName,
						unsigned char *msg, 
						short kOutPut,
						Boolean ShowAny,
						Boolean ShowFiles)
{
unsigned char *disp_path = NULL;	
char *returned_path = NULL;

disp_path = (unsigned char *) StrCalloc(1024);		
									
Assert_it(myDialog,"ProcessFileButton myDialog == NULL","")
Assert_it(msg,"ProcessFileButton msg == NULL","")
Assert_it(ReturnedFileName,"ProcessFileButton ReturnedFileName == NULL","")
returned_path = DoStdFileDialog(ReturnedFileName, msg, ShowAny, ShowFiles); 
											
if( (returned_path != NULL) & (disp_path != NULL) )
	{ 
	if (returned_path[strlen(returned_path)-2] == ':')     /* eliminate double ':' */
	     returned_path[strlen(returned_path)-1] = '\0';
	strcat(returned_path, ReturnedFileName); 
	DisplayPath(returned_path, kOutPut, myDialog);
	}	
			
disp_path = (unsigned char *) StrFree( (char *) disp_path);

return returned_path;
}




void SwitchCheckBox(DialogRef myDialog,short kCheckBox,Boolean *Switch)
{
Handle		myItem;
short 		ignoreType;
Rect		ignoreBox;

Assert_it(myDialog,"SwitchCheckBox myDialog == NULL","")
*Switch = !*Switch;
GetDialogItem( myDialog, kCheckBox, &ignoreType, &myItem, &ignoreBox);
SetControlValue( (ControlRef)myItem, *Switch);
}




void SetCheckBox(DialogRef myDialog,short kCheckBox,Boolean Switch)
{
Handle		myItem;
short 		ignoreType;
Rect		ignoreBox;

Assert_it(myDialog,"SetCheckBox myDialog == NULL","")
GetDialogItem( myDialog, kCheckBox, &ignoreType, &myItem, &ignoreBox);
SetControlValue( (ControlRef)myItem, Switch);
}



void DisplayPath(char *buffer, short ResourceID, DialogRef	myDialog)
{
Handle		myItem;
short 		ignoreType;
Rect		ignoreBox;
unsigned char disp_path[257];

Assert_it(buffer,"DisplayPath buffer == NULL","")
Assert_it(myDialog,"DisplayPath myDialog == NULL","")
CToPCpy(disp_path, buffer); 
TruncString(380, disp_path, truncMiddle);						
GetDialogItem( myDialog, ResourceID, &ignoreType, &myItem, &ignoreBox );
SetDialogItemText( myItem, disp_path ); 
}