
#include "ZipGUI.h"
#include <stdio.h>



extern ZipGUI_Globals	g;



Boolean DoNewScrollBars( WindowRef myWindow )
{
	DocumentPtr	myDoc;

	myDoc = (DocumentPtr)myWindow;

	myDoc->vertScroll = GetNewControl( rScrollBar, myWindow );
	myDoc->horizScroll = GetNewControl( rScrollBar, myWindow );
		
	if ( myDoc->vertScroll && myDoc->horizScroll ) {
		DoSetScrollSizes( myWindow );
		return true;
	} else
		return false;
} /* DoNewScrollBars */



	
void DoAdjustScrollSizes( WindowRef myWindow )
{		
	DocumentPtr	myDoc;
	
	if (myWindow == NULL) return;
	
	myDoc = (DocumentPtr)myWindow;
	HideControl( myDoc->vertScroll );
	HideControl( myDoc->horizScroll );
	DoSetScrollSizes( myWindow );
	ShowControl( myDoc->vertScroll );
	ShowControl( myDoc->horizScroll );
} /* DoAdjustScrollSizes */



	
void DoSetScrollSizes( WindowRef myWindow )
{		
	DocumentPtr	myDoc;
	int 		newBarSize,
				newBarPos;	

  if (myWindow == NULL) return;

	myDoc = (DocumentPtr)myWindow;
	newBarSize = myWindow->portRect.bottom - kScrollBarAdapt;
	newBarPos = myWindow->portRect.right - kScrollBarAdjust;
	SizeControl( myDoc->vertScroll, kScrollBarWidth, newBarSize );
	MoveControl( myDoc->vertScroll, newBarPos, kScrollBarOffset );

	newBarSize = myWindow->portRect.right - kScrollBarAdapt;
	newBarPos = myWindow->portRect.bottom - kScrollBarAdjust;
	SizeControl( myDoc->horizScroll, newBarSize, kScrollBarWidth );
	MoveControl( myDoc->horizScroll, kScrollBarOffset, newBarPos );
} /* DoSetScrollSizes */




	
void DoAdjustScrollValues( WindowRef myWindow )
{
	DocumentPtr	myDoc;
	
	if (myWindow == NULL) return;
	
	myDoc = (DocumentPtr)myWindow;
	
	DoAdjustHorizVert( true, myDoc->vertScroll, myDoc->docTxt );
	DoAdjustHorizVert( false, myDoc->horizScroll, myDoc->docTxt );
} /* DoAdjustScrollValues */



	
void DoAdjustHorizVert( Boolean isVert, ControlRef myScroll, TEHandle myTxtH )
{
	int	myValue, myLines, myMax;
	
	if ( isVert ) {	
		myLines = (*myTxtH)->nLines;
		if ( *( *(**myTxtH).hText + (*myTxtH)->teLength - 1) == kCarReturn )
			myLines++;
		myMax = myLines - (((*myTxtH)->viewRect.bottom - (*myTxtH)->viewRect.top)
					/ (*myTxtH)->lineHeight);
		
	} else	
		myMax = g.DocWidth - 
			((*myTxtH)->viewRect.right - (*myTxtH)->viewRect.left);
	if ( myMax < 0 )
		myMax = 0;
	SetControlMaximum( myScroll, myMax);

	Delay( kDelayTime, &g.ignoreTicks );
	
	if ( isVert )	
		myValue = ((*myTxtH)->viewRect.top - (*myTxtH)->destRect.top) 
						/ (*myTxtH)->lineHeight;
	else		
		myValue = (*myTxtH)->viewRect.left - (*myTxtH)->destRect.left;
		
	if ( myValue < 0 )
		myValue = 0;
	else if ( myValue > myMax ) 
		myValue = myMax;

	SetControlValue( myScroll, myValue);
} /* DoAdjustHorizVert */




void DoAdjustText( WindowRef myWindow )
{
	TEPtr		myTxtP;
	DocumentPtr	myDoc;
	short		myHoriz, myVert;
	
	if (myWindow == NULL) return;
	
	myDoc = (DocumentPtr)myWindow;
	myTxtP = *myDoc->docTxt;
	myHoriz = ( myTxtP->viewRect.left - myTxtP->destRect.left ) - 
				GetControlValue( myDoc->horizScroll );
	myVert = ( myTxtP->viewRect.top - myTxtP->destRect.top ) - 
		( GetControlValue( myDoc->vertScroll ) * myTxtP->lineHeight );
	TEScroll( myHoriz, myVert, myDoc->docTxt );
} /* DoAdjustText */




void DoContentClick( EventRecord *myEvent, WindowRef myWindow )
{ 	
	Point 		mouseLoc;	
	short 		windowPart;
	ControlRef 	myScroll;
	
	if ( myWindow != FrontWindow() )
		SelectWindow( myWindow );
	else {
		mouseLoc = myEvent->where;
		GlobalToLocal( &mouseLoc );
		windowPart = FindControl( mouseLoc, myWindow, &myScroll );
		switch ( windowPart ) {					  

			case kControlNoPart:	
				DoTextClick( mouseLoc, myWindow, myEvent );
				break;
			
			case kControlIndicatorPart: 	
				DoScrollIndicator( mouseLoc, myWindow, myScroll );
				break;
				
			default:	
				DoScrollAction( mouseLoc, myScroll );
				break;
		}
	}
} /* DoContentClick */




void DoScrollIndicator( Point mouseLoc, WindowRef myWindow, ControlRef myScroll )
{
	short 		oldValue, scrollDist;
	TEHandle	myTxtH;

	myTxtH = ((DocumentPtr)myWindow)->docTxt;
	oldValue = GetControlValue( myScroll );
	TrackControl( myScroll, mouseLoc, nil );
	scrollDist = oldValue - GetControlValue( myScroll );
	if ( scrollDist )	
		if ( myScroll == ((DocumentPtr)myWindow)->vertScroll )
			TEScroll( 0, scrollDist * (*myTxtH)->lineHeight, myTxtH );
		else
			TEScroll( scrollDist, 0, myTxtH );
} /* DoScrollIndicator */




void DoScrollAction( Point mouseLoc, ControlRef myScroll )
{
	ControlActionUPP	myScrollProc = nil;

	myScrollProc = NewControlActionProc( DoScrollProc );
	TrackControl( myScroll, mouseLoc, myScrollProc );
	DisposeRoutineDescriptor( myScrollProc );
} /* DoScrollAction */





pascal void DoScrollProc( ControlRef myScroll, short scrollPart )
{
	short		myArrowScroll, myGrayScroll, myAmount, myMax,
				origValue, newValue, horizScrollDist, vertScrollDist;
	WindowRef	myWindow;
	TEHandle	myTxtH;
	
	myWindow = (*myScroll)->contrlOwner;
	myTxtH = ((DocumentPtr)myWindow)->docTxt;
	if ( myScroll == ((DocumentPtr)myWindow)->vertScroll ) {
		myGrayScroll = ( (*myTxtH)->viewRect.bottom - (*myTxtH)->viewRect.top ) 
				/ (*myTxtH)->lineHeight - 1;
		myArrowScroll = 1;
		
	} else {
		myGrayScroll = (*myTxtH)->viewRect.right - (*myTxtH)->viewRect.left;
		myArrowScroll = kHorizArrowScroll;
	}
			
	switch ( scrollPart ) {
	
		case kControlUpButtonPart:		
			myAmount = myArrowScroll;
			break;
				
		case kControlDownButtonPart:	
			myAmount = -myArrowScroll;
			break;
				
		case kControlPageUpPart:		
			myAmount = myGrayScroll;
			break;
				
		case kControlPageDownPart:		
			myAmount = -myGrayScroll;
			break;
	}

	origValue = GetControlValue( myScroll );
	myMax = GetControlMaximum( myScroll );
	newValue = origValue - myAmount;
	if ( newValue < 0 )
		newValue = 0;
	else if ( newValue > myMax )
		newValue = myMax;
	myAmount = origValue - newValue;
   	if ( myAmount == 0)
		return;
	SetControlValue( myScroll, newValue );
	if ( myScroll == ((DocumentPtr)myWindow)->vertScroll ) {
		horizScrollDist = 0;
		vertScrollDist = myAmount * (*myTxtH)->lineHeight;
	} else {
		horizScrollDist = myAmount;
		vertScrollDist = 0;
	}
	TEScroll( horizScrollDist, vertScrollDist, myTxtH );
} /* DoScrollProc */
