// Graphics Importer and Exporter Samples
// Originally written by Sam Bushell for QuickTime "Live" '99
// WWDC 2000 Introduction to QuickTime

#include "MacShell.h"

#ifndef __APPLE_CC__
    #include <SIOUX.h>
#endif

// globals
// ------------------------
BitMap screenBits;
Boolean gDone = false;

//
//	doAEQuit
//
static pascal OSErr doAEQuit(const AppleEvent *message, AppleEvent *reply, long refcon)
{
#pragma unused(message, reply, refcon)
	gDone = true;
	return noErr;
}

static void doMenu( long menuSelection )
{
	short whichMenu = HiWord(menuSelection);
	short whichMenuItem = LoWord(menuSelection);
	
	switch (whichMenu) {
	case kAppleMenuID:
		switch (whichMenuItem) {
		case kAppleMenuAbout:
			Alert(128, NULL);
			break;

		default:
			break;
		}
		break;

	case kFileMenuID:
		switch (whichMenuItem) {
		case kFileMenuQuit:
			gDone = true;
			break;
		}
		break;
	
	case kDemoMenuID:
		switch (whichMenuItem) {
	#ifdef SAMPLE1
		case kDemoMenuDraw: DrawImage(); break;
	#endif
	#ifdef SAMPLE2
		case kDemoMenuScaleRotate: ScaleAndRotate(); break;
	#endif
	#ifdef SAMPLE3
		case kDemoMenuAlpha: AlphaComposite(); break;
	#endif
	#ifdef SAMPLE4
		case kDemoMenuMoreInfo: GetMoreInfo(); break;
	#endif
	#ifdef SAMPLE5
		case kDemoMenuMultipleImage: MultipleImage(); break;
	#endif
	#ifdef SAMPLE6
		case kDemoMenuURLImage: ImageFromURL(); break;
	#endif
	#ifdef SAMPLE7
		case kDemoMenuFiltersExport: FilterExport(); break;
	#endif
	#ifdef SAMPLE8
		case kDemoMenuMovieImage: MovieToImage(); break;
	#endif
		default:
			break;
		} // switch
	}
	
	HiliteMenu(0);
}

void pause( void )
{
	EventRecord event;
	Str255 saveTitle;
	
	if( FrontWindow() ) {
		GetWTitle( FrontWindow(), saveTitle );
		SetWTitle( FrontWindow(), "\p(paused)" );
	}
	
	do {
		// wait
	} while( false == WaitNextEvent( mDownMask + keyDownMask, &event, -1, NULL ) );
	
	if( FrontWindow() )
		SetWTitle( FrontWindow(), saveTitle );
}

Boolean IsQuickTimeInstalled(void) 
{
	OSErr	err;
	long	lResult;

	err = Gestalt(gestaltQuickTime, &lResult);
	return (err == noErr);
}

int main( void )
{
	long	result = 0;
	
	// initialize for carbon & QuickTime
	InitCursor();
	if ( IsQuickTimeInstalled() )
		EnterMovies();
	else
		goto bail;
		
	GetQDGlobalsScreenBits( &screenBits );
	
	Gestalt(gestaltMenuMgrAttr, &result);
	if (result & gestaltMenuMgrAquaLayoutMask) {
		// Mmmmmm...X
		SetMenuBar(GetNewMBar(129));
		if (AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(doAEQuit), NULL, false))
			goto bail;
	} else {
		SetMenuBar(GetNewMBar(128));
	}
	DrawMenuBar();
	
	while (gDone == false) {
		EventRecord theEvent;
		WindowPtr pWhichWindow;		
		short windowPart;
        Boolean SIOUXHandledEvent = false;
		
		WaitNextEvent(everyEvent, &theEvent, -1, NULL);
		
        #ifndef __APPLE_CC__
            SIOUXHandledEvent = SIOUXHandleOneEvent(&theEvent);
        #endif
        
		if ( !SIOUXHandledEvent ) {
		
			switch (theEvent.what) {
				case updateEvt:
					pWhichWindow = (WindowPtr)theEvent.message;
					
					// we don't do anything for this simple sample
					BeginUpdate(pWhichWindow);
					EndUpdate(pWhichWindow);
					break;
				
				case keyDown:
					if (theEvent.modifiers & cmdKey) {
						doMenu(MenuKey(theEvent.message & charCodeMask));
					}
					break;
				
				case mouseDown:
					windowPart = FindWindow(theEvent.where, &pWhichWindow);

					switch (windowPart) {
						case inDrag:
							DragWindow(pWhichWindow, theEvent.where, &screenBits.bounds);
							break;

						case inGoAway:
							if (TrackGoAway(pWhichWindow, theEvent.where))
								DisposeWindow( pWhichWindow );
								//gDone = true;
							break;

						case inContent:
							SelectWindow(pWhichWindow);
							break;

						case inMenuBar:
							doMenu(MenuSelect(theEvent.where));
							break;
					}
					break;
			}
		}
	}
	
bail:

	return 0;	
}
