#include "ScrollbarPane.h"

// if you don't want to support Smart Scroll (aw, c'mon!), comment out this line:
#define USESMARTSCROLL 1

static pascal void 	ScrollBarPaneProc(ControlHandle theControl, short partCode);
static pascal void	ScrollBarPaneThumbProc();

static ControlActionUPP		gScrollbarProc = NewControlActionProc(ScrollBarPaneProc);

#ifdef USESMARTSCROLL
extern pascal void SetSmartScrollInfo (ControlRef theScrollBar, long amountVisible , long amountTotal);
#endif

ScrollbarPane::ScrollbarPane(WindowPtr pWindow): mWindowPtr(pWindow),
mArrowRate(1), mPageRate(20)
{
	// create the scroll bar control
	mControl = NewControl(pWindow, &mFrame, "\p", FALSE, 0, 0, 0, scrollBarProc, (long) this);
}

ScrollbarPane::~ScrollbarPane()
{
	// delete the scroll bar control
	DisposeControl( mControl );
}

void ScrollbarPane::FitFrame()
{
	// force the scroll bar to match the frame
	HideControl(mControl);
	SizeControl(mControl, mFrame.right - mFrame.left, mFrame.bottom - mFrame.top );
	MoveControl(mControl, mFrame.left, mFrame.top );

	#ifdef USESMARTSCROLL
		long max = GetControlMaximum( mControl ) + mPageRate;
		SetSmartScrollInfo (mControl, mPageRate , max );
	#endif
	ShowControl(mControl);
}

void ScrollbarPane::HandleScroll( const short partCode )
{
	// This function actually handles the scrolling (except when thumb is dragged)

	// get current value... adjust for when we're within the top or bottom page
	short curval = GetControlValue(mControl);
	short max = GetControlMaximum(mControl);
	switch (partCode)
	{
		case kControlIndicatorPart:
			break;
		case kControlUpButtonPart:
			SetControlValue(mControl, curval - mArrowRate);
			break;
		case kControlDownButtonPart:
			SetControlValue(mControl, curval + mArrowRate);
			break;
		case kControlPageUpPart:
			SetControlValue(mControl, curval - mPageRate);
			break;
		case kControlPageDownPart:
			SetControlValue(mControl, curval + mPageRate);
			break;
	}
}

void ScrollbarPane::Draw()
{
	Draw1Control( mControl );// or should it be UpdateControl()?
}

unsigned char ScrollbarPane::Click( const Point p, const long modifiers )
{
	if (!PtInRect(p, &mFrame)) return false;
	
	short partCode = FindControl( p, mWindowPtr, &mControl );
	
	if (partCode == kControlNoPart)
	{
		// that's odd... it didn't hit a control part, but here we are nonetheless?!?
		return false;
	}
	// scrollbar was hit, so track the control
	short curValue = GetControlValue(mControl);
	
	if (partCode == kControlIndicatorPart)
		partCode = TrackControl( mControl, p, NULL );
	else
		partCode = TrackControl( mControl, p, gScrollbarProc );
	
	// if the thumb was dragged, figure out where it was dragged to and scroll
	// the contents appropriately

	// LATER!		
/*		if (partCode == kControlIndicatorPart)
	{
		curValue -= GetControlValue(mControl);
		
		if (mControl == theHBar)
			Scroll( curValue, 0 );
		else
			Scroll( 0, curValue );

		PostScroll( mControl );
	}
*/
	return true;
}

#pragma mark -

static pascal void	ScrollBarPaneProc( ControlHandle theControl, short partCode )
{
	// action proc for passing scrolling callbacks back to the object.
	
	ScrollbarPane*	aScrollbar = (ScrollbarPane*) GetControlReference(theControl);
	
	if (aScrollbar)
		aScrollbar->HandleScroll( partCode );
}



static pascal void	ScrollBarPaneThumbProc()
{
}

