
#include "ZipGUI.h"
#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>					
#include <Balloons.h>
#include "helpers.h"
#include "pathname.h"


void ShowAllKeyValues(void);
void DoAddHelpMenu(void);
void DoHelpMenu( short menuItem );


#define mApple				128					
#define iAbout				1					

#define rMenuBar			128				    	
#define mFile				129					
enum {
	iCmdLine   = 1,									
	izip,		
	iunzip,						
	iEnviroment,
	iseperator1,
	iQuit
	};


#define mEdit				130					/* "Edit"-MenŸ			*/
enum {
	iUndo 		= 1,
	iseperator11, 
	iCut,
	iCopy, 
 	iPaste, 
 	iClear, 
	iseperator22, 
 	iSelectAll 
 };




#define mOption				131					/* "Options"-MenŸ				*/		
enum {
	iPauseOnScreen_onoff	= 1,
	iSaveMessages_onoff,
	iPasswort7BitOnly,
	iShowCommandParam
	};
					
enum {
	iZipHelp	= 5,
	iUnzipHelp	= 6,
	iLicense    = 7
	};

#define rPauseOnScreen			133
#define sPauseOnScreen_on		1
#define sPauseOnScreen_off		2

#define rSaveMessages			134
#define sSaveMessages_on		2
#define sSaveMessages_off		1



#if GENERATINGCFM || USESROUTINEDESCRIPTORS
#  define CreateRoutineDescriptor(info, proc)									\
		RoutineDescriptor g##proc##RD = BUILD_ROUTINE_DESCRIPTOR(info, proc)
#  define GetRoutineAddress(proc)	(&g##proc##RD)
#else
#  define GetRoutineAddress(proc)	proc
#endif



extern ZipGUI_Globals	g;




void DoInstallMenus( void )
{
	Handle		menuBar;
	MenuHandle	myMenu;

	menuBar = GetNewMBar( rMenuBar );
	if ( !menuBar )
		DoAlertUser( eNoMenuBar, true );
	SetMenuBar( menuBar );
	DisposeHandle( menuBar );
	myMenu = GetMenuHandle( mApple );
	AppendResMenu( myMenu, 'DRVR' );
	DrawMenuBar();
	DoAddHelpMenu();
} /* DoInstallMenus */




void DoAddHelpMenu(void)
{ 			
	MenuHandle  myMenu;
	OSErr       myErr;
	
myErr = HMGetHelpMenuHandle(&myMenu);

if (myErr == noErr) 
	{
	if (myMenu != NULL)
		{
		AppendMenu(myMenu, "\p Zip Help");
		AppendMenu(myMenu, "\p Unzip Help");
		AppendMenu(myMenu, "\p License");
		}
	}
} /* DoAdjustMenus */





void DoAdjustMenus( void )
{ 			
	WindowRef	myWindow;
	
	myWindow = FrontWindow();
	DoAdjustFileMenu();
	DoAdjustEditMenu( myWindow );
	DoAdjustOptionMenu();
	
	g.menuAdjust = false;
} /* DoAdjustMenus */




void DoAdjustFileMenu(void)
{
	MenuHandle		myMenu;
	
	myMenu = GetMenuHandle( mFile );

	if ( g.MaczipIsRunning ) {
		DisableItem( myMenu, iCmdLine );
		DisableItem( myMenu, izip );
		DisableItem( myMenu, iunzip );
		}
	else {
		EnableItem( myMenu, iCmdLine );
		EnableItem( myMenu, izip );
		EnableItem( myMenu, iunzip );
	}
	
} /* DoAdjustFileMenu */




void DoAdjustEditMenu( WindowRef myWindow )
{
	MenuHandle		myMenu;
	TEHandle		myTxtH;
	Boolean			editUsed = false;
	static Boolean	origFlag = true;
	
	myMenu = GetMenuHandle( mEdit );
	DisableItem( myMenu, iUndo); 
	if ( myWindow ) {
		myTxtH = ((DocumentPtr)myWindow)->docTxt;
		if ( (*myTxtH)->teLength ) {
			EnableItem( myMenu, iSelectAll );
			editUsed = true;
		} else
			DisableItem( myMenu, iSelectAll );
			
		if ( (*myTxtH)->selStart == (*myTxtH)->selEnd ) {
			DisableItem( myMenu, iCut);
			DisableItem( myMenu, iCopy);
		} else {
			EnableItem( myMenu, iCopy );
			editUsed = true;
		}

		DisableItem( myMenu, iPaste );
	}
	if ( origFlag != editUsed ) {
		if ( editUsed )
			EnableItem( myMenu, 0 );
		else
			DisableItem( myMenu, 0 );
		InvalMenuBar();
	}
	origFlag = editUsed;
		
} /* DoAdjustEditMenu */





void DoAdjustOptionMenu( void )
{
	MenuHandle	myMenu;
	Str255		itemString;
	
	myMenu = GetMenuHandle( mOption );

	if (g.PauseOnScreen  == true)
		{
		GetIndString( itemString, rPauseOnScreen, sPauseOnScreen_on);
		}
	else
		{
		GetIndString( itemString, rPauseOnScreen, sPauseOnScreen_off);
		}
	SetMenuItemText(myMenu, iPauseOnScreen_onoff, itemString);


	if (g.SaveMessages  == true)
		GetIndString( itemString, rSaveMessages, sSaveMessages_on);
	else
		GetIndString( itemString, rSaveMessages, sSaveMessages_off);
	
	SetMenuItemText(myMenu, iSaveMessages_onoff, itemString);

	CheckItem(myMenu, iPasswort7BitOnly, g.Passwort7BitOnly);
	CheckItem(myMenu, iShowCommandParam, g.ShowCommandParam);
 
} /* DoAdjustOptionMenu */




void DoMenuCommand( long menuChoice )
{
	short	menuID,
			menuItem;
			
	menuID = HiWord( menuChoice );
	menuItem = LoWord( menuChoice ); 	
	
	switch ( menuID ) {
	
		case mApple:	
			DoAppleMenu( menuItem);
			break;
			
		case mFile:		
			DoFileMenu( menuItem );
			break;
			
		case mEdit:		
			DoEditMenu( menuItem );
			break;

		case mOption:	
			DoOptionMenu( menuItem );
			break;
			
		case kHMHelpMenuID:
			DoHelpMenu( menuItem );
			break;		
	}
	HiliteMenu( 0 );
if ( menuID && !g.doneFlag )
		g.menuAdjust = true;
} /* DoMenuCommand */





void DoAppleMenu( short menuItem )
{
	Str255		itemName;
	MenuHandle	myMenu;

	switch ( menuItem ) {
	
		case iAbout:			
			DoAboutBox();
			break;
		
		default:	  			
			myMenu = GetMenuHandle( mApple );
			GetMenuItemText( myMenu, menuItem, itemName );
			OpenDeskAcc( itemName );
			break;
	}
} /* DoAppleMenu */





void DoFileMenu( short menuItem )
{
	WindowRef	myWindow;

	myWindow = FrontWindow();

	switch ( menuItem ) {
	
		case iCmdLine:			
			 DoCmdBox(); 
			break;

		case izip: 		
			{
			g.LastApp = g.CurrentApp;
			g.CurrentApp	= kZip;
			DoZipDialogBox();					
			}
			break;		
									    	
		case iunzip: 		
			{
			g.LastApp = g.CurrentApp;
			g.CurrentApp	= kUnzip;
			DoUnZipDialogBox();					
			}
			break;								    	

		case iEnviroment:
			ClearScreen();
			ShowAllKeyValues();
			break;
			
		case iQuit:			
			DoQuit();
			break;
	}
} /* DoFileMenu */




void DoEditMenu( short menuItem )
{
	WindowRef	myWindow;
	
	myWindow = FrontWindow();
		
	switch( menuItem ) {
		
		case iCut:				
			DoCut( myWindow );
			break;

		case iCopy:				
			DoCopy( myWindow );				
			break;

		case iPaste:			
			DoPaste( myWindow );
			break;

		case iClear:			
			ClearScreen();
			break;

		case iSelectAll:		
			DoSelectAll( myWindow );
			break;
	}
	DoAdjustScrollValues( myWindow );
	DoAdjustText( myWindow );
} /* DoEditMenu */




void DoOptionMenu( short menuItem )
{
    MenuHandle	myMenu = GetMenuHandle( mOption );
    
	switch ( menuItem ) {
		
		case iPauseOnScreen_onoff:
			g.PauseOnScreen = !g.PauseOnScreen;
			break;
			
		case iSaveMessages_onoff:
			g.SaveMessages = !g.SaveMessages;
			if (g.SaveMessages) DoSaveMessages();
			break;
			
		case iPasswort7BitOnly:
			g.Passwort7BitOnly = !g.Passwort7BitOnly;
			CheckItem(myMenu, iPasswort7BitOnly, g.Passwort7BitOnly);
			break;

		case iShowCommandParam:
			g.ShowCommandParam = !g.ShowCommandParam;
			CheckItem(myMenu, iShowCommandParam, g.ShowCommandParam);
			break;

	}
	
} /* DoOptionMenu */



void DoHelpMenu( short menuItem )
{ 

	switch ( menuItem ) {
		

		case iZipHelp:
			g.Noisy = false;
			g.LastApp = g.CurrentApp;
			g.CurrentApp = kZip;
			g.PathAreOK = true;
			if (g.CurrentApp != g.LastApp) DoCloseAllWin();		
			StartApp("-h", false, NULL);
			break;

			
		case iUnzipHelp:
			g.Noisy = false;
			g.LastApp = g.CurrentApp;
			g.CurrentApp = kUnzip;
			g.PathAreOK = true;
			if (g.CurrentApp != g.LastApp) DoCloseAllWin();		
			StartApp("-h", false, NULL);            
			break;	
			
		case iLicense:
			g.Noisy = false;
			g.LastApp = g.CurrentApp;
			g.CurrentApp = kZip;
			g.PathAreOK = true;
			g.LineBreak = "\n";
			if (g.CurrentApp != g.LastApp) DoCloseAllWin();		
			StartApp("-L", false, NULL);
			break;
				
	}
	
} /* DoHelpMenu */


