
#include "ZipGUI.h"
#include "helpers.h"

extern ZipGUI_Globals	g;


Boolean DoNewText( WindowRef myWindow )
{
	DocumentPtr	myDoc;
	Rect		myViewRect,
				myDestRect;
	short		fontFamID;
	
	myDoc = (DocumentPtr)myWindow;
	
#ifdef JAPANESE
	GetFNum( "\pOsaca", &fontFamID );
#else
	GetFNum( "\pMonaco", &fontFamID );
#endif

	TextFont( fontFamID );
	TextSize( 9 );
	MyGetViewRect( myWindow, &myViewRect );
	myDestRect = myViewRect;
	myDestRect.right = myDestRect.left + g.DocWidth;
	myDoc->docTxt = TENew( &myDestRect, &myViewRect );

	if ( myDoc->docTxt ) {
		TEFeatureFlag( teFOutlineHilite, teBitSet, myDoc->docTxt );
		TEAutoView( true, myDoc->docTxt );
		DoAdjustViewRect( myDoc->docTxt );
		return true;
	} else
		return false;
} /* DoNewText */




void MyGetViewRect( WindowRef myWindow, Rect *myViewRect )
{
	*myViewRect = myWindow->portRect;
	myViewRect->right -= kScrollBarWidth;
	myViewRect->bottom -= kScrollBarWidth;
	myViewRect->top = myViewRect->top + 20;
	InsetRect( myViewRect, kMargin, kMargin);
} /* MyGetViewRect */



 
void DoAdjustViewRect( TEHandle myTxtH )
{
	TEPtr	myTxtP;
	
	myTxtP = *myTxtH;
	myTxtP->viewRect.bottom = 
	(((myTxtP->viewRect.bottom - myTxtP->viewRect.top)
	/ myTxtP->lineHeight) * myTxtP->lineHeight) + myTxtP->viewRect.top;
} /* DoAdjustViewRect */



int ClearScreen(void)
{	
	TEHandle	myTxtH;
	long		myLength;
	WindowRef	myWindow;

	myWindow = FrontWindow();
	if (myWindow == nil) return 0;
	
	myTxtH = ((DocumentPtr)myWindow)->docTxt;
	myLength = MyGetTEResultLength( myTxtH );
		
	TEDeactivate(myTxtH);
	TESetSelect( 0, myLength , myTxtH );
    TEDelete( myTxtH );
    TEActivate(myTxtH);
    TESelView(myTxtH);
    
return 0;
}




void DoHandleKeyDown( short myChar )
{	
	WindowRef	myWindow;
	TEHandle	myTxtH;
						 
	myWindow = FrontWindow();
	if ( myWindow ) {
		myTxtH = ((DocumentPtr)myWindow)->docTxt;
		if (((DocumentPtr)myWindow)->ReadOnly) return;
		
		
	switch ( myChar ) {
	
				case kBackSpace: 	 
				case kRightArrow: 	 
				case kLeftArrow:	 
				case kDownArrow:	 
				case kUpArrow:
				    TEKey( myChar, myTxtH );
				    return;
				    break;		 


				case kBlank: 		
					break;
	
				case kCarReturn:	
					break;
			
				default:			
					break;
	}
	TEKey( myChar, myTxtH );
	g.menuAdjust = true;
	}
	
	
} /* DoHandleKeyDown */





int PrintMessage(char *msg, short type)
{	
	DocumentPtr	myDoc;
	TEHandle	myTxtH;
	OSErr		myErr = noErr;
	long		msgLen;
	char		*tmpPtr;
	WindowRef	myWindow;
	long 		myLength;
	Rect        windowrect;
	static short Linecount = 0;

   myWindow = FrontWindow();
   
   
	if (myWindow == nil) /* no frontwindow present */
		{
		myWindow = DoNew(type);
		}

windowrect = myWindow->portRect;	
myDoc = (DocumentPtr)myWindow;
((DocumentPtr)myWindow)->modified = false;
myTxtH = ((DocumentPtr)myWindow)->docTxt;
myDoc = (DocumentPtr)myWindow;
myLength = MyGetTEResultLength( myTxtH );

if ((msgLen = strlen(msg)) == 0) return 0;

if (g.BeSilent) return msgLen;

if ((g.PauseOnScreen) && (Linecount > ((windowrect.bottom - windowrect.top) / 15))) {
	Linecount = 0;
	rightStatusString("*** Pause; Press Enter ***");
	waitChar(); 
	rightStatusString("");
	}	

if (g.SaveMessages) fwrite( msg, msgLen, 1L, g.SaveMessages_fp);

  for (tmpPtr = msg; *tmpPtr; tmpPtr++)  /* exchange '\n' in '\r' */
    if (*tmpPtr == '\n') {
        *tmpPtr = '\r';
        Linecount++;
     }
      			
	if ((myLength + msgLen + 10) >= kMaxTELength ) /* check the text length of the current win */
		{							          /* the textwindow "textedit" can hold only 32kbyte */
		if (g.ContOnNextWin)
			{
			myWindow = DoNew(type);
			myTxtH = ((DocumentPtr)myWindow)->docTxt;
			}
		else
			{
			FreeTextEditBuffer(myTxtH, myDoc);
        	}
		}
		
TESelView(myTxtH);  
TEInsert(msg,msgLen, myTxtH );  /* insert the text in the Window */

	
/* normaly I should call DoAdjustScrollValues() after each call of PrintMessage()
 * but this consumes too much time. So I decided to call the func once every run 
 * of ZipMain */
/*	DoAdjustScrollValues( myWindow );   */
		
return msgLen;
} /* PrintMessage */



void FreeTextEditBuffer(TEHandle myTxtH, DocumentPtr myDoc)
{
char        buffer[2000];
long myLength = MyGetTEResultLength( myTxtH );
char *myTxtPtr; 

if (myLength < (sizeof(buffer) * 2)) return;

myTxtPtr =  *(**myDoc->docTxt).hText + (myLength - 2000);

memcpy(buffer,myTxtPtr,2000);         /*  32 Kbyte. See my small solution:        */
									/* I have to take care this limit:          */
TEDeactivate(myTxtH);			      /*    a: save the last 2000 Bytes of the text */
TESetSelect( 0, myLength , myTxtH );  /*    b: delete the hole text */
TEDelete( myTxtH ); 				  /*    c: insert the last 2000 Byte into the  */
TEActivate(myTxtH);					  /*       textwin and continue ... */
TEInsert("\r ... [cut]\r\r" , 13, myTxtH ); /* let the user know that the text is cutted */
TEInsert(buffer , 2000, myTxtH );

g.InputTxtPtr_start = *(**myDoc->docTxt).hText + MyGetTEResultLength( myTxtH );
g.InputTxtPtr_start = g.InputTxtPtr_start - g.InputLength;
}




long MyGetTEResultLength( TEHandle myTxtH )
{
	return (*myTxtH)->teLength - ((*myTxtH)->selEnd - (*myTxtH)->selStart);
} /* MyGetTEResultLength */




	
void DoTextClick( Point mouseLoc, WindowRef myWindow, EventRecord *myEvent )
{
	Boolean 	shiftDown;
	
	shiftDown = (myEvent->modifiers & shiftKey) != 0;
	TEClick( mouseLoc, shiftDown, ((DocumentPtr)myWindow)->docTxt );
	DoAdjustScrollValues( myWindow );
	g.menuAdjust = true;
} /* DoTextClick */




void DoCut( WindowRef myWindow )
{
	DocumentPtr	myDoc;

	myDoc = (DocumentPtr)myWindow;
	TECut( myDoc->docTxt );
	myDoc->modified = true;
} /* DoCut */




void DoCopy( WindowRef myWindow )
{
	DocumentPtr	myDoc;

	myDoc = (DocumentPtr)myWindow;
	TECopy( myDoc->docTxt );
} /* DoCopy */



void DoPaste( WindowRef myWindow )
{
	DocumentPtr	myDoc;
	TEHandle	myTxtH;

	myDoc = (DocumentPtr)myWindow;
	myTxtH = myDoc->docTxt;
	if ( ( MyGetTEResultLength( myTxtH ) + TEGetScrapLength() ) <= kMaxTELength ) {
		TEPaste( myTxtH );
		myDoc->modified = true;
	} else
		DoAlertUser( eExceedPaste, false );
} /* DoPaste */




void DoDelete( WindowRef myWindow )
{
	DocumentPtr	myDoc;

	myDoc = (DocumentPtr)myWindow;
	TEDelete( myDoc->docTxt );
	myDoc->modified = true;
} /* DoDelete */




void DoSelectAll( WindowRef myWindow )
{
	DocumentPtr	myDoc;
	TEHandle	myTxtH;

	myDoc = (DocumentPtr)myWindow;
	myTxtH = myDoc->docTxt;
	TESetSelect( 0, (*myTxtH)->teLength, myTxtH );
} /* DoSelectAll */




void DoConvertScrap( Boolean resume )
{
	if ( resume )	
		TEFromScrap();
	else {			/* Suspend-Event */
		ZeroScrap();
		TEToScrap();
	}
} /* DoConvertScrap */





void DoIdle( void )
{
	WindowRef	myWindow;
	
	myWindow = FrontWindow();
	if ( myWindow && (GetWindowKind( myWindow ) == userKind) )
		TEIdle( ((DocumentPtr)myWindow)->docTxt );
} /* DoIdle */





void DoAdjustCursor( void )
{
	WindowRef	myWindow;
	Point 		mousePt, localMouse;

	GetMouse( &mousePt );
	localMouse = mousePt;
	LocalToGlobal( &mousePt );
	FindWindow( mousePt, &myWindow );
	if ( myWindow == FrontWindow() ) {
		if ( PtInRect( localMouse, &(*((DocumentPtr)myWindow)->docTxt)->viewRect ) )
			SetCursor( *(GetCursor( iBeamCursor )) );
		else
			SetCursor( &qd.arrow );
	} else	
		SetCursor( &qd.arrow );
} /* DoAdjustCursor */





void leftStatusString(char *status)
{
static unsigned char pstatus[100];
Rect	rectToErase, windowrect;
WindowPtr	windowPtr;

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

if (!windowPtr) return;
	
if (status != NULL) 
    {
    CToPCpy(pstatus,status);
    SetRect(&rectToErase,0,0,200,20);
    EraseRect(&rectToErase);
    }

MoveTo(10,15);
DrawString(pstatus);
}





void rightStatusString(char *status)
{
static unsigned char pstatus[100];
Rect	rectToErase, windowrect;
WindowPtr	windowPtr;

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

UserStop();

if (!windowPtr) return;

if (status != NULL) 
    {
    CToPCpy(pstatus,status);
    SetRect(&rectToErase,200,0,400,20);
    EraseRect(&rectToErase);
    }

MoveTo(210,15);
DrawString(pstatus);
}
