#include "graphics dispatch.h"
#include "drag utilities.h"
#include "window layer.h"
#include "program globals.h"
#include "edit functions.h"
#include "text twiddling.h"
#include "generic window handlers.h"
#include "about.h"
#include "about MSG.h"
#include "other MSG window.h"
#include "help.h"
#include "kant main window.h"
#include "kant build window.h"

enum DispatchError SetupWindowDispatch(short index, WindowPtr theWindow)
{
	Boolean			success=FALSE;
	RgnHandle		hiliteRgn;
	
	SetWindowIndex(theWindow, index);
	SetWindowTE(theWindow, 0L);
	SetWindowVScrollBar(theWindow, 0L);
	SetWindowHScrollBar(theWindow, 0L);
	SetWindowIsDraggable(theWindow, FALSE);
	SetWindowHiliteRgn(theWindow, 0L);
	SetWindowOldClickLoopProc(theWindow, 0L);
	
	switch (index)
	{
		case kAboutWindow:
			SetupTheAboutWindow(theWindow);
			success=TRUE;
			break;
		case kAboutMSGWindow:
			SetupTheAboutMSGWindow(theWindow);
			success=TRUE;
			break;
		case kOtherMSGWindow:
			SetupTheOtherMSGWindow(theWindow);
			success=TRUE;
			break;
		case kHelpWindow:
			SetupTheHelpWindow(theWindow);
			success=TRUE;
			break;
		case kMainWindow:
			SetupTheMainWindow(theWindow);
			success=TRUE;
			break;
		case kBuildWindow:
			SetupTheBuildWindow(theWindow);
			success=TRUE;
			break;
	}
	
	if ((DragManagerAvailableQQ()) && (success) && (WindowIsDraggableQQ(theWindow)))
	{
		hiliteRgn=NewRgn();
		SetWindowHiliteRgn(theWindow, hiliteRgn);
		InstallTrackingHandler((DragTrackingHandler)MyTrackingHandler, theWindow, (void *)theWindow);
		InstallReceiveHandler((DragReceiveHandler)MyReceiveDropHandler, theWindow, (void *)theWindow);
	}
	
	return success ? kSuccess : kFailure;
}

enum DispatchError ShutdownWindowDispatch(short index)
{
	switch (index)
	{
		case kAboutWindow:
			ShutDownTheAboutWindow();
			return kSuccess;
		case kAboutMSGWindow:
			ShutDownTheAboutMSGWindow();
			return kSuccess;
		case kOtherMSGWindow:
			ShutDownTheOtherMSGWindow();
			return kSuccess;
		case kHelpWindow:
			ShutDownTheHelpWindow();
			return kSuccess;
		case kMainWindow:
			ShutDownTheMainWindow();
			return kSuccess;
		case kBuildWindow:
			ShutDownTheBuildWindow();
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError OpenWindowDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kAboutWindow:
			OpenTheAboutWindow(theWindow);
			return kSuccess;
		case kAboutMSGWindow:
			OpenTheMSGWindow(theWindow);
			return kSuccess;
		case kOtherMSGWindow:
			OpenTheOtherMSGWindow(theWindow);
			return kSuccess;
		case kHelpWindow:
			OpenTheHelpWindow(theWindow);
			return kSuccess;
		case kMainWindow:
			OpenTheMainWindow(theWindow);
			return kSuccess;
		case kBuildWindow:
			OpenTheBuildWindow(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError CloseWindowDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			if (CloseTheMainWindow(theWindow))
				return kSuccess;
			else
				return kCancel;
	}
	
	return kFailure;
}

enum DispatchError DisposeWindowDispatch(short index)
{
	WindowPtr		theWindow;
	Boolean			success=FALSE;
	RgnHandle		hiliteRgn;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kAboutWindow:
			DisposeTheAboutWindow(theWindow);
			success=TRUE;
			break;
		case kOtherMSGWindow:
			DisposeTheOtherMSGWindow(theWindow);
			success=TRUE;
			break;
		case kHelpWindow:
			DisposeTheHelpWindow(theWindow);
			success=TRUE;
			break;
		case kMainWindow:
			DisposeTheMainWindow(theWindow);
			success=TRUE;
			break;
		case kBuildWindow:
			DisposeTheBuildWindow(theWindow);
			success=TRUE;
			break;
	}
	
	if ((DragManagerAvailableQQ()) && (success) && (WindowIsDraggableQQ(theWindow)))
	{
		RemoveTrackingHandler((DragTrackingHandler)MyTrackingHandler, theWindow);
		RemoveReceiveHandler((DragReceiveHandler)MyReceiveDropHandler, theWindow);
		hiliteRgn=GetWindowHiliteRgn(theWindow);
		DisposeRgn(hiliteRgn);
	}
	
	return success ? kSuccess : kFailure;
}

enum DispatchError DrawWindowDispatch(short index, short theDepth)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kAboutMSGWindow:
			DrawTheAboutMSGWindow(theWindow, theDepth);
			return kSuccess;
		case kHelpWindow:
			DrawTheHelpWindow(theWindow, theDepth);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError ChangeDepthDispatch(short index, short newDepth)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kAboutMSGWindow:
			ChangeDepthTheAboutMSGWindow(theWindow, newDepth);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError CopybitsDispatch(short index, WindowPtr offscreenWindow)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kOtherMSGWindow:
		case kMainWindow:
		case kBuildWindow:
			GenericCopybits(theWindow, offscreenWindow, WindowIsActiveQQ(theWindow));
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError IdleWindowDispatch(short index, Point mouseLoc)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			GenericIdleInWindow(theWindow, mouseLoc);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError ActivateWindowDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kOtherMSGWindow:
		case kMainWindow:
		case kBuildWindow:
			GenericActivate(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError DeactivateWindowDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kOtherMSGWindow:
		case kMainWindow:
		case kBuildWindow:
			GenericDeactivate(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError GrowWindowDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kOtherMSGWindow:
		case kMainWindow:
			GenericResizeControls(theWindow, 0);
			return kSuccess;
		case kBuildWindow:
			GenericResizeControls(theWindow, 1000);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError ZoomWindowDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kOtherMSGWindow:
		case kMainWindow:
			GenericResizeControls(theWindow, 0);
			return kSuccess;
		case kBuildWindow:
			GenericResizeControls(theWindow, 1000);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError GetGrowSizeDispatch(short index, Rect *sizeRect)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kOtherMSGWindow:
		case kMainWindow:
		case kBuildWindow:
			GenericGetGrowSize(theWindow, sizeRect);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError KeyDownDispatch(short index, unsigned char theChar)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kAboutWindow:
			KeyDownInAboutWindow(theWindow, theChar);
			return kSuccess;
		case kOtherMSGWindow:
			GenericKeyPressedInWindow(theWindow, theChar);
			return kSuccess;
		case kHelpWindow:
			KeyPressedInHelpWindow(theWindow, theChar);
			return kSuccess;
		case kMainWindow:
			if (!GenericKeyPressedInWindow(theWindow, theChar))
				KeyPressedInMainWindow(theWindow, theChar);
			return kSuccess;
		case kBuildWindow:
			if (!GenericKeyPressedInWindow(theWindow, theChar))
				KeyPressedInBuildWindow(theWindow, theChar);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError MouseUpDispatch(short index, Point thePoint)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kBuildWindow:
			MouseUpInBuildWindow(theWindow, thePoint);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError MouseDownDispatch(short index, Point thePoint)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kAboutWindow:
			MouseDownInAboutWindow(theWindow, thePoint);
			return kSuccess;
		case kOtherMSGWindow:
			GenericMouseClickedInWindow(theWindow, thePoint, FALSE);
			return kSuccess;
		case kHelpWindow:
			MouseClickedInHelpWindow(theWindow, thePoint);
			return kSuccess;
		case kMainWindow:
			GenericMouseClickedInWindow(theWindow, thePoint, TRUE);
			return kSuccess;
		case kBuildWindow:
			MouseClickedInBuildWindow(theWindow, thePoint);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError UndoDispatch(short index)
{
	switch (index)
	{
	}
	
	return kFailure;
}

enum DispatchError CutDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			GenericCut(theWindow);
			return kSuccess;
			break;
	}
	
	return kFailure;
}

enum DispatchError CopyDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			GenericCopy(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError PasteDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			PasteInMainWindow(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError ClearDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			GenericClear(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}

enum DispatchError SelectAllDispatch(short index)
{
	WindowPtr		theWindow;
	
	theWindow=GetIndWindowPtr(index);
	
	switch (index)
	{
		case kMainWindow:
			GenericSelectAll(theWindow);
			return kSuccess;
	}
	
	return kFailure;
}
