
#include "ZipGUI.h"
#include <stdlib.h>

#define ZE_ABORT          80   /* user hit [command]+[.] to terminate */

extern ZipGUI_Globals	g;

int macgetch(void);

#define IS_8BIT_CHAR(byte)	((byte & 0xC0) != 0x80) 


void DoEventLoop( void )
{
	EventRecord	myEvent;

	while ( !g.doneFlag ) {
		if ( g.inForeground && FrontWindow() )
		DoAdjustCursor();
		WaitNextEvent( everyEvent, &myEvent, kSleep, nil );
		DoEventDispatch( &myEvent );
		if ( g.menuAdjust )
			DoAdjustMenus();
	}
} /* DoEventLoop */




void DoEventDispatch( EventRecord *myEvent )
{
	switch ( myEvent->what ) {
	
		case activateEvt:	
			DoActivate( (WindowRef)myEvent->message, (myEvent->modifiers & activeFlag) );
			break;

		case mouseDown:		
			DoMouseDown( myEvent );
			break;

		case keyDown:		
		case autoKey:		
			DoKeyDown( myEvent );
			break;
			
		case updateEvt:		
			DoUpdate( (WindowRef)myEvent->message );
			break;
			
		case osEvt:			
			DoOpSysEvent( myEvent );
			break;

		case kHighLevelEvent:	
			DoHighLevelEvent( myEvent );
			break;

		case nullEvent:		
			DoIdle();
			break;
	}
} /* DoEventDispatch */





void DoMouseDown( EventRecord *myEvent )
{
	WindowRef	myWindow;
	short		myPart;

	myPart = FindWindow( myEvent->where, &myWindow );
	switch ( myPart ) {
			
		case inMenuBar:		
			DoMenuCommand( MenuSelect( myEvent->where) );
			break;
	
    	case inDrag:		
			DoDragWindow( myEvent, myWindow );
    			break;
			
		case inZoomIn:		
		case inZoomOut:
			DoZoomWindow( myEvent, myWindow, myPart );
			break;
		
		case inGrow:		
			DoGrowWindow( myEvent, myWindow );	
			break;
		
		case inGoAway:		
			DoGoAway( myEvent, myWindow );
			break;
		
		case inContent:		
			if (!g.AppIsRunning) DoContentClick( myEvent, myWindow );
			break; 
	}
} /* DoMouseDown */





void DoKeyDown( EventRecord *myEvent )
{
	short	myKey;


	myKey = myEvent->message & charCodeMask;
	if ( myEvent->modifiers & cmdKey )
	
		DoMenuCommand( MenuKey( myKey ) );
	else
		DoHandleKeyDown( myKey );

} /* DoKeyDown */



	
void DoOpSysEvent( EventRecord *myEvent )
{	
	WindowRef	myWindow;
	Boolean	resume;

	resume = myEvent->message & resumeFlag;

	DoConvertScrap( resume );
	myWindow = FrontWindow();
	if ( myWindow )
		DoActivate( myWindow, resume );
	if ( resume )
		g.inForeground = true;
	else
		g.inForeground = false;		
} /* DoOpSysEvent */





char waitChar(void) 
{
    EventRecord theEvent;

    for ( ; ; ) {
	if ( WaitNextEvent( everyEvent, &theEvent, kSleep, nil )) {

            switch (theEvent.what) {
            
            case nullEvent:
            	{
            	DoAdjustCursor();
               	break;
            	}
            
            case autoKey:
            case keyDown:
            	{
            	DoKeyDown(&theEvent );
                if ((theEvent.modifiers & cmdKey) &&
                    ((theEvent.message & charCodeMask) == '.'))
                    exit(1);  /* setjmp() must be already called  */
                return(theEvent.message & charCodeMask);
                }
                
            case mouseDown:
            	{
                DoMouseDown( &theEvent );
               	break;
            	}
                
            case updateEvt:
            	{
                break;
                }
            } /*   switch (theEvent.what)   */
        }  /*   if ( WaitNextEvent(...  */
    } /*   for ( ; ; )    */
}






void UserStop(void) 
{
    EventRecord theEvent;

	if ( WaitNextEvent( everyEvent, &theEvent, 0, nil )) {

            switch (theEvent.what) {

        	case mouseDown:
               	DoMouseDown( &theEvent );
               	break;
            
            case autoKey:
            case keyDown:
            	{
                if ((theEvent.modifiers & cmdKey) &&
                    ((theEvent.message & charCodeMask) == '.'))
                    {
                    printf("\n\n <- User Canceled -> \n");
                    DoQuit();
                    }
                return;
                }
                
            } /*   switch (theEvent.what)   */
        }  /*   if ( WaitNextEvent(...  */
}




int macgetch(void)
{
    EventRecord theEvent;
    char c;                     /* one-byte buffer for read() to use */

    do {
        SystemTask();
        if (!GetNextEvent(everyEvent, &theEvent))
            theEvent.what = nullEvent;
        else {
        
        c = theEvent.message & charCodeMask;
        
        if (g.Passwort7BitOnly && 
            !IS_8BIT_CHAR(c) &&
            theEvent.what == keyDown )
        	{
        	SysBeep( 30 );
        	theEvent.what = nullEvent; /* do nothing */
        	}
        else
        	{
	            switch (theEvent.what) {
	
    	        case keyDown:
            	    switch(c)
                		{
						case kBackSpace: 	 
						case kRightArrow: 	 
						case kLeftArrow:	 
						case kDownArrow:	 
						case kUpArrow:
						case kDel:
						case kInsert:
						case kPageDown:
						case kPageUp:			
						    SysBeep( 30 );
						    theEvent.what = nullEvent; /* do nothing */
						    break;		        
						    
						case kCarReturn:
						     PrintMessage("\r", MessageWindow);  
						     return (int)c;
						     break;       	
        	        	} 
            	    if ((theEvent.modifiers & cmdKey) &&
                	    ((theEvent.message & charCodeMask) == '.'))
           		         exit(1);  /* setjmp() must be already called  */
                	break;

  	        	case mouseDown:
                	DoMouseDown( &theEvent );
                	break;
            	case updateEvt:
                	break;
            	}
            } /* if (g.Passwort7BitOnly   */
        }
    } while (theEvent.what != keyDown);


PrintMessage("*", MessageWindow);
return (int)c;
}
