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



extern ZipGUI_Globals	g;



WindowRef DoNew(short type )
{	
	WindowRef myWindow;
	Boolean		closed = true;
	short countWin = 0;
	
	if (kMaxDocNum <= g.numDocuments) 
		{
		DoAlertUser( eNoMoreWindows, false );
		DoCloseAllWin();
		}
	
	myWindow = DoNewDocWindow();		

	if ( myWindow ) {
		DoSetWindUntitled( myWindow, type );
		((DocumentPtr)myWindow)->fileRefNum = 0;
		((DocumentPtr)myWindow)->modified = false;
		((DocumentPtr)myWindow)->ReadOnly = true;
		ShowWindow( myWindow );
		g.numDocuments++;
	}
	
return myWindow;
} /* DoNew */



WindowRef DoNewDocWindow( void )
{	
	Ptr			myDocStorage = nil;
	WindowRef	myWindow = nil;
	DocumentPtr	myDoc = nil;
	Boolean		success = false;

	myDocStorage = NewPtrClear( sizeof(Document) );
	if ( myDocStorage )
		myWindow = GetNewWindow( rDocWind, myDocStorage, kInFront );
	if ( myWindow ) {
		SetPort( myWindow ); 
		success = DoNewText( myWindow );
	}
	if ( success )
		success = DoNewScrollBars( myWindow );
	if ( success )
		return myWindow;

	myDoc = (DocumentPtr)myWindow; 
	if ( myDoc && myDoc->docTxt )
		TEDispose( myDoc->docTxt );	
	if ( myWindow )
		CloseWindow( myWindow );	
	if ( myDocStorage )
		DisposePtr( myDocStorage );	
	DoAlertUser( eNoWindError, false );
	return nil;
} /* DoNewDocWindow */




void DoSetWindUntitled( WindowRef myWindow, short type )
{
	Str255			myTitle;
	Str32			myApp;

	GetIndString( myTitle, rWindowTitles, type );
	GetIndString( myApp, rCurrentAppInfo, g.CurrentApp);
	
	DoAddString( myTitle, "\p " );
	DoAddString( myTitle, myApp);

	SetWTitle( myWindow, myTitle );
} 



void DoAddString( Str255 myOrigStr, Str255 myAddStr )
{
	short addLength = myAddStr[0];
	int totalLength = myOrigStr[0] + addLength;

	if ( totalLength > 255 ) {
		totalLength = 255;
		addLength = totalLength - myOrigStr[0];
	}

	BlockMoveData( &myAddStr[1], &myOrigStr[totalLength-addLength + 1], addLength);
	myOrigStr[0] = totalLength;
} /* DoAddString */




Boolean DoClose( WindowRef myWindow )
{ 		
	Boolean		closeDoc = true;
	OSErr		myErr = noErr;
	
	if (myWindow == nil) return true;
	
	if ( ((DocumentPtr)myWindow)->modified )
		switch ( DoAskUser( myWindow ) ) {

			case kStdOkItemIndex:		
				closeDoc = DoSave( myWindow );
				break;
			
			case kStdCancelItemIndex:	
				closeDoc = false;
				break;
			
			case kDiscardItemIndex:		
				closeDoc = true;
				break;
		}

	if ( closeDoc )
	
		DoCloseWindow( myWindow );

	return closeDoc;
} /* DoClose */




void DoCloseWindow( WindowRef myWindow )
{
short countWin = 0;

if (myWindow == NULL) return;

	TEDispose( ((DocumentPtr)myWindow)->docTxt );
	CloseWindow( myWindow );
	DisposePtr( (Ptr)myWindow );
	SetCursor( &qd.arrow );
	g.numDocuments--;
	
} /* DoCloseWindow */




void DoQuit( void )
{
	WindowRef	myWindow;
	Boolean		closed = true;

	while ( g.numDocuments > 0 && closed ) {

		myWindow = FrontWindow();
		closed = DoClose( myWindow );
	}

	if ( closed ) {
		g.doneFlag = true;

		DisposeRoutineDescriptor( g.modalFilterUPP );
	}
ExitToShell();
} /* DoQuit */




void DoActivate( WindowRef myWindow, Boolean activate )
{
	DocumentPtr myDoc;

    if (myWindow == NULL) return;

	myDoc = (DocumentPtr)myWindow;
		
	if ( activate ) {
		SetPort( myWindow );
		ShowControl( myDoc->vertScroll );
		ShowControl( myDoc->horizScroll );
		DrawGrowIcon( myWindow );
		TEActivate( ((DocumentPtr)myWindow)->docTxt );
		SetCursor( &qd.arrow );
		g.menuAdjust = true;
	} else {
		HideControl( myDoc->vertScroll );
		HideControl( myDoc->horizScroll );
		DrawGrowIcon( myWindow );
		TEDeactivate( myDoc->docTxt );
	}
} /* DoActivate */




void DoUpdate( WindowRef myWindow )
{
	GrafPtr	origPort;
	Rect		myContRect;
	TEHandle	myTxtH;
						 
if (myWindow == NULL) return;
						 
	GetPort( &origPort );
	SetPort( myWindow );
	BeginUpdate ( myWindow );
	myContRect = myWindow->portRect; 
	myContRect.right -= kScrollBarAdjust;
	myContRect.bottom -= kScrollBarAdjust;
	EraseRect( &myContRect );
	UpdateControls( myWindow, myWindow->visRgn );
	DrawGrowIcon( myWindow );
	myTxtH = ((DocumentPtr)myWindow)->docTxt;
	TEUpdate( &(*myTxtH)->viewRect, myTxtH );
	EndUpdate( myWindow );
	SetPort( origPort );
	
    drawStatusLine();
	
	rightStatusString(NULL);
    leftStatusString(NULL);

} /* DoUpdate */




void DoCloseAllWin(void)
{
	WindowRef	myWindow;
	Boolean		closed = true;

	while ( g.numDocuments > 0 && closed ) {
		myWindow = FrontWindow();
		closed = DoClose( myWindow );
	}
}
	
	
	
void drawStatusLine(void)
{
static unsigned char pstatus[100];
Rect	windowrect;
WindowPtr	windowPtr;

windowPtr = FrontWindow();
windowrect = windowPtr->portRect;	

if (windowPtr)
	{
	MoveTo(1,20);
	LineTo(windowrect.right - kScrollBarAdjust,20);
	}

}
