#include "kant main window.h"
#include "kant load-save.h"
#include "drag utilities.h"
#include "environment.h"
#include "util.h"
#include "menus.h"
#include "main.h"
#include "dialogs.h"
#include "text twiddling.h"
#include "generic window handlers.h"
#include "window layer.h"
#include "program globals.h"

#define kGrowBoxSize		15

static	void PutDataIntoTEFields(WindowPtr theWindow);

static	short			gOldForegroundTime;		/* stored foreground wait time */
#if powerc
static	TEClickLoopUPP	gClickLoopUPP;
#endif
static	Boolean			gSetupDone=FALSE;
static	Boolean			gIsActive=FALSE;

void SetupTheMainWindow(WindowPtr theWindow)
{
	unsigned char	*titleStr="\puntitled";
	Point			topLeft;
	FSSpec			fs;
	
	SetWindowHeight(theWindow, qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28);
	SetWindowWidth(theWindow, qd.screenBits.bounds.right-qd.screenBits.bounds.left-70);
	SetWindowType(theWindow, zoomDocProc);
	topLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
	topLeft.h=qd.screenBits.bounds.left+10;
	SetWindowTopLeft(theWindow, topLeft);
	SetWindowHasCloseBox(theWindow, TRUE);
	SetWindowMaxDepth(theWindow, 1);
	SetWindowDepth(theWindow, 1);
	SetWindowIsFloat(theWindow, FALSE);
	SetWindowTitle(theWindow, titleStr);
	SetWindowAutoCenter(theWindow, FALSE);
	fs.name[0]=0x00;
	fs.vRefNum=0;
	fs.parID=0;
	SetWindowFS(theWindow, fs);
	SetWindowIsModified(theWindow, FALSE);
	SetWindowIsDraggable(theWindow, TRUE);
	
	if (gSetupDone)
		return;
	
	gSetupDone=TRUE;
#if powerc
	gClickLoopUPP=NewTEClickLoopProc(ClickLoopProc);
#endif
}

void ShutDownTheMainWindow(void)
{
#if powerc
	if (gClickLoopUPP!=0L)
		DisposeRoutineDescriptor(gClickLoopUPP);
#endif
}

void OpenTheMainWindow(WindowPtr theWindow)
{
	TEHandle		hTE;
	FontInfo		theFontInfo;
	Rect			vScrollBarRect, hScrollBarRect;
	Rect			destRect, viewRect;
	
	hTE=GetWindowTE(theWindow);
	if (hTE==0L)
	{
		SetRect(&vScrollBarRect, GetWindowWidth(theWindow)-kGrowBoxSize, -1,
			GetWindowWidth(theWindow)+1, GetWindowHeight(theWindow)+1-kGrowBoxSize);
		SetRect(&hScrollBarRect, -1, GetWindowHeight(theWindow)-kGrowBoxSize,
			GetWindowWidth(theWindow)-kGrowBoxSize+1, GetWindowHeight(theWindow)+1);
		SetWindowVScrollBar(theWindow,
			NewControl(theWindow, &vScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
		SetWindowHScrollBar(theWindow,
			NewControl(theWindow, &hScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
		
		GetTERect(theWindow, &destRect, TRUE);
		viewRect=destRect;
		hTE=TENew(&destRect, &viewRect);
		SetWindowTE(theWindow, hTE);
		TextFont((**hTE).txFont=monaco);
		TextSize((**hTE).txSize=9);
		TextFace((**hTE).txFace=0);
		GetFontInfo(&theFontInfo);
		(**hTE).fontAscent=theFontInfo.ascent;
		(**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
		AdjustViewRect(hTE);
		TEAutoView(TRUE, hTE);
		SetWindowOldClickLoopProc(theWindow, (**hTE).clickLoop);
#if powerc
		TESetClickLoop(gClickLoopUPP, hTE);
#else
		(**hTE).clickLoop=(TEClickLoopUPP)MyClikLoop;
#endif
		PutDataIntoTEFields(theWindow);
	}
	
	gIsActive=TRUE;
	AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
	AdjustMenus();
}

void KeyPressedInMainWindow(WindowPtr theWindow, unsigned char theChar)
{
	TEHandle		hTE;
	ControlHandle	vScrollBar;
	
	hTE=GetWindowTE(theWindow);
	vScrollBar=GetWindowVScrollBar(theWindow);
	
	switch (theChar)
	{
		case 0x09:
			TESetSelect(0, 32767, hTE);
			break;
		default:
			TEKey(theChar, hTE);
			SetWindowIsModified(theWindow, TRUE);
			break;
	}
	
	AdjustVScrollBar(vScrollBar, hTE);
}

void DisposeTheMainWindow(WindowPtr theWindow)
{
	TEHandle		hTE;
	
	hTE=GetWindowTE(theWindow);
	if (hTE!=0L)
	{
		TEDispose(hTE);
		SetWindowTE(theWindow, 0L);
	}
}

Boolean CloseTheMainWindow(WindowPtr theWindow)
{
	ModalFilterUPP	procFilter = NewModalFilterProc(ThreeButtonFilter);
	short			result;
	
	if (WindowIsModifiedQQ(theWindow))
	{
		SetCursor(&qd.arrow);
		PositionDialog('ALRT', saveAlert);
		ParamText(GetWindowTitle(theWindow), "\p", "\p", "\p");
		result=Alert(saveAlert, procFilter);
		DisposeRoutineDescriptor(procFilter);
		switch (result)
		{
			case 1:	/* save */
				LoadSaveDispatch(FALSE, TRUE);
				if (WindowIsModifiedQQ(theWindow))	/* save didn't work for some reason */
					return FALSE;
				break;
			case 2:	 /* cancel */
				return FALSE;
				break;
			case 3:	/* don't save */
				break;
		}
	}
	
	gCustomCursor=FALSE;
	
	return TRUE;
}

void PasteInMainWindow(WindowPtr theWindow)
{
	TEHandle		hTE;
	ControlHandle	vScrollBar;
	Handle			scrapHandle;
	long			offset;
	unsigned long	scrapLength;
	unsigned char	*theSource="\pMBDF-A";
	unsigned char	*theTarget="\pProgramming is not a crime.";
	
	hTE=GetWindowTE(theWindow);
	vScrollBar=GetWindowVScrollBar(theWindow);
	
	scrapHandle=NewHandle(0L);
	if (GetScrap(scrapHandle, 'TEXT', &offset)!=noTypeErr)
	{
		scrapLength=GetHandleSize(scrapHandle);
		if (scrapLength==theSource[0])
		{
			HLock(scrapHandle);
			if (Mymemcompare((Ptr)*scrapHandle, (Ptr)&theSource[1], theSource[0]))
			{
				ZeroScrap();
				PutScrap(theTarget[0], 'TEXT', (char*)&theTarget[1]);
			}
		}
		
		TEFromScrap();
		TEPaste(hTE);
		TESelView(hTE);
		if (vScrollBar!=0L)
			AdjustVScrollBar(vScrollBar, hTE);
	}
	DisposeHandle(scrapHandle);
}

static	void PutDataIntoTEFields(WindowPtr theWindow)
{
	TEHandle			hTE;
	
	hTE=GetWindowTE(theWindow);
	TESetSelect(0, 0, hTE);
	TEKey(0x00, hTE);
	TEKey(0x08, hTE);
}
