/********************************************************************************/
/*							ZipGUI - Header-Files								*/
/********************************************************************************/

#define OLDROUTINENAMES     0			/* use new function names only */
#define OLDROUTINELOCATIONS 0			/* use new headerlocations only */
#define SystemSevenOrLater 	1	        /* Runs only on System 7.0 or later	*/

/********************************************************************************/
/*	Header-Files																*/
/********************************************************************************/

#include <AEObjects.h>				/* Apple-Event-Support */
#include <Devices.h>				/* Device-Manager */
#include <Fonts.h>					/* Font-Manager */
#include <Gestalt.h>				/* Gestalt-Manager */
#include <Processes.h>				/* Process-Manager */
#include <Resources.h>				/* Resource-Manager */
#include <Scrap.h>					/* Scrap-Manager */
#include <Sound.h>					/* Sound-Manager */
#include <StandardFile.h>			/* Standard-File-Package */
#include <ToolUtils.h>				/* Tool-Utilities */
#include <Traps.h>					/* Trap-Const */
#include <stdio.h>
#include <string.h>



/********************************************************************************/
/*	IDs of the Recources														*/
/********************************************************************************/

/****** ID of the Documentwindow ************************************************/
#define rDocWind			    128				/* 'WIND'-Ressource 			*/


/********************************************************************************/
/*	Konstanten																	*/
/********************************************************************************/

#define kDiscardItemIndex	3


#define kSleep				15		
#define kDelayTime			8		
									
#define kTimeLens			100		
									  
									   
/****** Size of the Documentwindow ***************************************/
#define kMinWidth			130					
#define kMinHeight			80					
#define kMaxWidth			32767				
#define kMaxHeight			32767				
		
#define	kInFront 		(WindowRef) -1	
#define kMargin				4			

#define kHorizArrowScroll	6			
										 
#define kMaxTELength		32767	

#define rScrollBar			128					/* ID of 'CNTL'-Ressource 		*/

#define kScrollBarWidth		16			
#define	kScrollBarAdjust	15			
#define kScrollBarAdapt		13			
#define kScrollBarOffset	-1			
#define kControlInvisible	0
#define kControlVisible		255


#define kEnter				3					
#define kBackSpace			8					
#define kTab  				9					
#define kCarReturn			13					
#define kEscape				27					
#define kLeftArrow			28					
#define kRightArrow			29					
#define kUpArrow			30					
#define kDownArrow			31					
#define kBlank				32					
#define kPeriod				46		

#define kDel				0x7F
#define kInsert				0x05
#define kPageDown			0x0C
#define kPageUp				0x0B			


/****** IDs of 'STR#'-Ressource-Lists *****************************************/
#define	rStandStrings		128					
#define	rErrStrings			129					
#define	rNoiseStrings		130					


enum  {
 	eWrongSystem	=	1,					
 	eNoMenuBar		,						
 	eNoAppleEvents	,					
 	eNoWindError	,						
 	eFileTooLarge	,						
 	eOpenError		,						
 	eSaveError		,						
 	eExceedPaste	,	 					
 	eExceedChar		,	 					
    eNoMoreWindows  ,
    eOutOfMemory    ,
    ePathAreNotOk   ,
    eTooManyItemsDropped,
    CorruptCryptEntries
    };  


#define	kTooBig				5000				

#define	kCreator			'R*ch'     /* mark message-files as BBedit file */
#define	kFileType			'TEXT'

#define sProgName			1					
#define sSaveAs				2					
#define sUntitled			3					

#define rCurrentAppInfo	    135


#define		rWindowTitles	131

enum  {
	MessageWindow = 1,  /*  all messages except internal-messages goes */
	WarningWindow,		/* to the std-window */
	ErrorWindow,
	InternalWindow,
	DuplicVolumeName,
	CreditsTextTitle,
	kMaxDocNum	= 40
	};
	

typedef struct {
	Boolean			doneFlag,			
	    			Passwort7BitOnly,
	    			ShowCommandParam,
					menuAdjust,		
					AppIsRunning,	
					inForeground,
					RunTestArchiveAfterwards,
					PathAreOK;
	short			numDocuments;		
	ModalFilterUPP	modalFilterUPP; 	
										   
	unsigned long	ignoreTicks;			
		
	Boolean			ContOnNextWin;
	Boolean			AllMsgOneWin;
	Boolean			PauseOnScreen;
	Boolean			SaveMessages;
	FSSpec			SaveMessagesFSSpec;
	FILE			*SaveMessages_fp;
	Boolean			Noisy;
	
	long            DocWidth;     	    
	short			CurrentApp;
	short			LastApp;
	char 			CurrentAppStr[50];	    /* name of the currend app */
	char       		*InputTxtPtr_start;
	unsigned long   InputLength;
	Boolean         BeSilent;
	int             ExitResult;
	Boolean			MaczipIsRunning;
	Boolean			NoWarningBoxes;
	Boolean 		is_EnvForExit_valid;
	ProcessSerialNumber myPsn;
	Boolean         gHasSyslog;
	char 			*LineBreak;
} ZipGUI_Globals;


enum  {				/* const for all Apps  */
	 kZip		= 1,
	 kUnzip,
	 kUnknown
	 };

																			
typedef struct {
	WindowRecord	docWindow;								
	ControlRef		vertScroll,								
					horizScroll;
	TEHandle		docTxt;									
	FSSpec			fileSpec;								
	short			fileRefNum;
	Boolean			modified;		
	Boolean			ReadOnly;
} Document, *DocumentPtr;



/********************************************************************************/
/*	Functions-Prototypes														*/
/********************************************************************************/

int 	zipmain(int argc,char **argvalue);

int 	main( void );
int 	StartApp(char *cmd, Boolean callFree, char *ZipPath);
void    PrepareAppStartup(int argc,char **argv);
int 	UzpMain(int argc,char **argvalue);

void 	DoInitToolbox( void );			
void 	DoCheckSysConfig( void );
void 	DoInitGlobals();


/****** Event **************************************************************/
void 	DoEventLoop( void );
void 	DoEventDispatch( EventRecord *myEvent );
void 	DoMouseDown( EventRecord *myEvent );
void 	DoKeyDown( EventRecord *myEvent );
void 	DoOpSysEvent( EventRecord *myEvent );
char    waitChar(void);
void    UserStop(void);
int     macgetch(void);

/****** Menues *******************************************************************/
void 	DoInstallMenus( void );
void 	DoAdjustMenus( void );
void	DoAdjustFileMenu(void);
void	DoAdjustEditMenu( WindowRef myWindow );
void	DoAdjustOptionMenu( void );
void 	DoMenuCommand( long menuChoice );
void 	DoAppleMenu( short menuItem );
void 	DoFileMenu( short menuItem );
void 	DoEditMenu( short menuItem );
void 	DoOptionMenu( short menuItem );
void    DoCurrentAppMenu( short menuItem );
void 	DoAdjustAppMenu( void );
OSErr   ChooseActionStartCommand(char *CMDString);

/****** Documentwindow *********************************************************/
WindowRef 	DoNew(short type );
WindowRef 	DoNewDocWindow( void );
void 	DoSetWindUntitled( WindowRef myWindow, short type);
void 	DoAdjustWindow( WindowRef myWindow );

void 	DoActivate( WindowRef myWindow, Boolean activate );
void 	DoUpdate( WindowRef myWindow );

Boolean DoClose( WindowRef myWindow );
void 	DoCloseWindow( WindowRef myWindow );
void 	DoQuit( void );
void 	DoCloseAllWin(void);
void    drawStatusLine(void);

/****** Text **********************************************************/
Boolean DoNewText( WindowRef myWindow );
int 	ClearScreen(void);
int 	PrintMessage(char *msg, short type);
void	DoTextClick( Point mouseLoc, WindowRef myWindow, EventRecord *myEvent );
void 	DoAdjustText( WindowRef	myWindow );
void 	DoAdjustViewRect( TEHandle	myTxtH );
void 	DoAdjustCursor( void );
void 	MyGetViewRect( WindowRef myWindow, Rect *myViewRect );
long 	MyGetTEResultLength( TEHandle myTxtH );
void	DoConvertScrap( Boolean resume );
void 	DoIdle( void );
void 	DoHandleKeyDown( short myChar );

void	DoCut( WindowRef myWindow );
void	DoCopy( WindowRef myWindow );
void	DoPaste( WindowRef myWindow );
void	DoDelete( WindowRef myWindow );
void	DoSelectAll( WindowRef myWindow );

void FreeTextEditBuffer(TEHandle myTxtH,DocumentPtr myDoc);
void rightStatusString(char *status);
void leftStatusString(char *status);

/****** Mouse-Actions ***********************************************************/
void 	DoDragWindow( EventRecord *myEvent, WindowRef myWindow );
void 	DoZoomWindow( EventRecord *myEvent, WindowRef myWindow, short myPart );
void 	DoGrowWindow( EventRecord *myEvent, WindowRef myWindow );
void 	DoGoAway( EventRecord *myEvent, WindowRef myWindow );


/****** Scrollbars **************************************************************/
Boolean DoNewScrollBars( WindowRef myWindow );
void 	DoAdjustScrollSizes( WindowRef myWindow );
void	DoSetScrollSizes( WindowRef myWindow );
void 	DoAdjustScrollValues( WindowRef myWindow );
void 	DoAdjustHorizVert( Boolean isVert, ControlRef myScroll, TEHandle myTxtH );
void 	DoContentClick( EventRecord *myEvent, WindowRef myWindow );
void	DoScrollIndicator( Point mouseLoc, WindowRef myWindow, ControlRef myScroll );
void	DoScrollAction( Point mouseLoc, ControlRef myScroll );
pascal void DoScrollProc( ControlRef myScroll, short windowPart);


/****** Files *****************************************************************/
Boolean DoSave( WindowRef myWindow);
OSErr 	DoSaveDoc(WindowRef myWindow);
OSErr 	DoSaveAs( WindowRef myWindow);
OSErr   DoSaveMessages(void);

void 	DoOpen( void );
OSErr 	DoOpenFile( FSSpec mySpec );
OSErr 	DoReadFile ( short refNum, TEHandle myText );


/****** Dialogwindow ***********************************************************/
void 	DoAboutBox( void );
void 	DoCmdBox( void );
short 	DoAskUser( WindowRef myWindow );
Boolean DoWaringForSwitch( void );
void 	DoWarnUserDupVol( char *path );
void 	DoAlertUser( short myErr, Boolean quitFlag );
void 	FatalError(char *myMessage, short myErrCode, Boolean quitFlag );
void 	DoZipDialogBox( void );
void 	DoUnZipDialogBox( void );
char   *DoCommentEntryBox(char *string,int number,char *item);

char *GetZipVersionsInfo(void);
char *GetUnZipInfoVersions(void);
char *GetZipVersionLocal(void);
char *GetUnZipLocalVersion(void);
char *GetZipCopyright(void);

pascal Boolean DoModalFilter( DialogRef myDialog, EventRecord *myEvent, short *itemHit);
void 	DoSetupDefaultButton( DialogRef myDialog, short defaultItem );
void 	DoDrawDefaultButton( DialogRef myDialog, short myButton );
Boolean DoFlashButton( DialogRef myDialog, short itemHit );


/****** AppleEvents *************************************************************/
void	DoInstallAEHandlers( void );
void	DoHighLevelEvent( EventRecord *myEvent );
pascal OSErr DoOAppAE( AppleEvent *myAEvent, AppleEvent *myReply, long refCon );
pascal OSErr DoODocAE( AppleEvent *myAppleEvent, AppleEvent *myReply, long refCon );
pascal OSErr DoPrintAE( AppleEvent *myAEvent, AppleEvent *myReply, long refCon );
pascal OSErr DoQuitAE( AppleEvent *myAEvent, AppleEvent *myReply, long refCon );
OSErr	MyGotRequiredParams( AppleEvent *myAppleEvent );


/****** Utilities ***************************************************************/
OSErr 	DoPlaySound( short mySndRes );
void 	DoAddString( Str255 myOrigString, Str255 myAddString );


/*Macintosh version of syslog. The priority param is one
  of the priorites in syslog.h. The format and ... params are
  like the ones from printf. This calls openlog(), formats your messages,
  sets the priority, gets the time, then packs it all into
  an AppleEvent that is then sent to the Syslogd program which
  actually logs the message and does cleanup, if necessary with 
  Closelog(). Errors returned will be AppleEvent
  errors.  */
int Print2Syslog(UInt8 priority, const char *format, ...);


#define CMDLineSize 2048

