/*	XGScrollView.cpp
 *
 *		Scrollbar positioning and scrolling view management
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XMessages.h>
#include <XScroll.h>
#include <XDraw.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGScrollView::XGScrollView
 *
 *		Construct me
 */

XGScrollView::XGScrollView(XGView *parent, XGArgStream &stream) : XGView(parent,stream)
{
	XGSScrollViewInit init;
	
	init.fHScroll = stream.ReadBoolean();
	init.fVScroll = stream.ReadBoolean();
	init.fCorner  = stream.ReadBoolean();
	InitScrollView(init);
}

XGScrollView::XGScrollView(XGView *parent, XGSScrollViewInit &init) : XGView(parent,init.view)
{
	InitScrollView(init);
}

/*$	XGScrollView::~XGScrollView
 *
 *		Delete these
 */

XGScrollView::~XGScrollView()
{
	if (fHoriz) delete fHoriz;
	if (fVert)  delete fVert;
}

/************************************************************************/
/*																		*/
/*	Events																*/
/*																		*/
/************************************************************************/

/*$	XGScrollView::DoDrawView
 *
 *		Draw the corner if it's visible
 */

void XGScrollView::DoDrawView(XRect)
{
	XGDraw draw(this);
	XRect r;

	r = GetContentRect();
	draw.Draw3DRect(r,KXGEFrame);
	
	if (!fCorner) return;
	r = CalcCornerRect();
	draw.Draw3DRect(r,KXGEBackground);
}


void XGScrollView::DoSizeView()
{
	XRect r = GetContentRect();
	int step = fPixel;
	if (step < 1) step = 1;
	long xval = (r.right - r.left - (step >> 1)) / step;
	if (xval < 1) xval = 1;
	long yval = (r.bottom - r.top - (step >> 1)) / step;
	if (yval < 1) yval = 1;
	SetPageSize(xval,yval);
	
	XGView::DoSizeView();
}

/*$	XGScrollView::CalcChildPos
 *
 *		Set the position of the children. This
 */

XRect XGScrollView::CalcChildPos(XGView *view)
{
	if (view == fHoriz) {
		return CalcHScroll();
	} else if (view == fVert) {
		return CalcVScroll();
	} else {
		return view->CalcAutoLocate(CalcContentRect());
	}
}

/************************************************************************/
/*																		*/
/*	Scrollbar Management												*/
/*																		*/
/************************************************************************/

/*$	XGScrollView::SetScrollValue
 *
 *		Set the scrollbar values
 */

void XGScrollView::SetScrollValue(long x, long y)
{
	if (fHoriz) fHoriz->SetValue(x);
	if (fVert) fVert->SetValue(y);
}

/*$	XGScrollView::SetMinValue
 *
 *		Set the scrollbar values
 */

void XGScrollView::SetMinValue(long x, long y)
{
	if (fHoriz) fHoriz->SetMinValue(x);
	if (fVert) fVert->SetMinValue(y);
}

/*$	XGScrollView::SetMaxValue
 *
 *		Set the scrollbar values
 */

void XGScrollView::SetMaxValue(long x, long y)
{
	if (fHoriz) fHoriz->SetMaxValue(x);
	if (fVert) fVert->SetMaxValue(y);
}

/*$	XGScrollView::SetStepSize
 *
 *		Set the scrollbar values
 */

void XGScrollView::SetStepSize(long x)
{
	if (fHoriz) fHoriz->SetStepSize(x);
	if (fVert) fVert->SetStepSize(x);
}

/*$	XGScrollView::SetPageSize
 *
 *		Set the scrollbar values
 */

void XGScrollView::SetPageSize(long x, long y)
{
	if (fHoriz) fHoriz->SetPageSize(x);
	if (fVert) fVert->SetPageSize(y);
}

/*$	XGScrollView::GetScrollValue
 *
 *		Get the scrollbar values
 */

void XGScrollView::GetScrollValue(long *x, long *y)
{
	*x = fHoriz ? fHoriz->GetValue() : 0;
	*y = fVert  ? fVert->GetValue()  : 0;
}

/*$	XGScrollView::GetMinValue
 *
 *		Get the scrollbar values
 */

void XGScrollView::GetMinValue(long *x, long *y)
{
	*x = fHoriz ? fHoriz->GetMinValue() : 0;
	*y = fVert  ? fVert->GetMinValue()  : 0;
}

/*$	XGScrollView::GetMaxValue
 *
 *		Get the scrollbar values
 */

void XGScrollView::GetMaxValue(long *x, long *y)
{
	*x = fHoriz ? fHoriz->GetMaxValue() : 0;
	*y = fVert  ? fVert->GetMaxValue()  : 0;
}

/*$	XGScrollView::GetStepSize
 *
 *		Get the scrollbar values
 */

long XGScrollView::GetStepSize()
{
	if (fHoriz) return fHoriz->GetStepSize();
	if (fVert) return fVert->GetStepSize();
	return 1;
}

/*$	XGScrollView::GetPageSize
 *
 *		Get the scrollbar values
 */

void XGScrollView::GetPageSize(long *x, long *y)
{
	*x = fHoriz ? fHoriz->GetPageSize() : 0;
	*y = fVert  ? fVert->GetPageSize()  : 0;
}

/************************************************************************/
/*																		*/
/*	Events																*/
/*																		*/
/************************************************************************/

BEGINDISPATCH(XGScrollView,XGView)
	DISPATCHTABLE(KXGSliderStart,XGScrollView::DoSliderStart)
	DISPATCHTABLE(KXGSliderValue,XGScrollView::DoSliderValue)
ENDDISPATCH

/*	XGScrollView::DoSliderStart
 *
 *		Start the slider by updating the content rectangles. This makes
 *	sure the update regions are filled in before we start moving bits
 *	around;  that way the bits are correct...
 */

long XGScrollView::DoSliderStart(long, void *)
{
	XGView *view;
	for (view = GetChild(); view != NULL; view = view->GetSibling()) {
		if (view == fHoriz) continue;
		if (view == fVert) continue;
		
		view->UpdateView();
	}
	return 0;
}

/*$	XGScrollView::DoSliderValue
 *
 *		Update the value from this control
 */

long XGScrollView::DoSliderValue(long, void *ptr)
{
	long dx,dy;
	XGSValue *value = (XGSValue *)ptr;
	XGView *view;
	
	if (value->slider == fHoriz) {
		dy = 0;
		dx = value->oldVal - value->newVal;
	} else {
		dx = 0;
		dy = value->oldVal - value->newVal;
	}
	
	for (view = GetChild(); view != NULL; view = view->GetSibling()) {
		if ((view != fVert) && (view != fHoriz)) {
			view->Scroll(dx*fPixel,dy*fPixel);			// scroll and draw
			view->UpdateView();
		}
	}
	return 0;
}

/************************************************************************/
/*																		*/
/*	Internals															*/
/*																		*/
/************************************************************************/

/*$	XGScrollView::InitScrollView
 *
 *		Initialize the scroll view
 */

void XGScrollView::InitScrollView(XGSScrollViewInit &s)
{
	XGSScrollInitRecord sinit;
	XRect r;
	XRect c;

	fPixel = 1;
	
	fCorner = s.fCorner;
	if (s.fHScroll && s.fVScroll) fCorner = true;
	
	c = GetContentRect();
	if (s.fHScroll) {
		r = CalcHScroll();
		sinit.view.fViewType = XGScrollBar::kClassID;
		sinit.view.fViewID = 0;
		sinit.view.fRefNum = 0;
		
		sinit.view.fLockLeft = true;
		sinit.view.fLockTop = true;
		sinit.view.fLockRight = true;
		sinit.view.fLockBottom = true;
		
		sinit.view.fAutoLeft = r.left;
		sinit.view.fAutoTop = r.top;
		sinit.view.fAutoRight = c.right - r.right;
		sinit.view.fAutoBottom = c.bottom - r.bottom;
		
		sinit.view.fVisible = true;
		sinit.view.fEnabled = true;
		sinit.view.fTabStop = false;
		
		sinit.fValue = 0;
		sinit.fMinValue = 0;
		sinit.fMaxValue = 0;
		sinit.fStepValue = 1;
		sinit.fPageValue = 1;
		
		fHoriz = new XGScrollBar(this,sinit);
		fHoriz->SetLocation(CalcChildPos(fHoriz));
	} else {
		fHoriz = NULL;
	}
	
	if (s.fVScroll) {
		r = CalcVScroll();
		sinit.view.fViewType = XGScrollBar::kClassID;
		sinit.view.fViewID = 0;
		sinit.view.fRefNum = 0;
		
		sinit.view.fLockLeft = true;
		sinit.view.fLockTop = true;
		sinit.view.fLockRight = true;
		sinit.view.fLockBottom = true;
		
		sinit.view.fAutoLeft = r.left;
		sinit.view.fAutoTop = r.top;
		sinit.view.fAutoRight = c.right - r.right;
		sinit.view.fAutoBottom = c.bottom - r.bottom;
		
		sinit.view.fVisible = true;
		sinit.view.fEnabled = true;
		sinit.view.fTabStop = false;
		
		sinit.fValue = 0;
		sinit.fMinValue = 0;
		sinit.fMaxValue = 0;
		sinit.fStepValue = 1;
		sinit.fPageValue = 1;
		
		fVert = new XGScrollBar(this,sinit);
		fVert->SetLocation(CalcChildPos(fVert));
	} else {
		fVert = NULL;
	}
}

/************************************************************************/
/*																		*/
/*	Locations															*/
/*																		*/
/************************************************************************/

/*$	XGScrollView::CalcHScroll
 *
 *		Calculate the horizontal view
 */

XRect XGScrollView::CalcHScroll()
{
	XRect r;
	XPoint pt;
	
	pt = XGScrollBar::GetDefaultSize();
	r = GetContentRect();
	::InsetRect(&r,1,1);

#if OPT_MACOS || OPT_XWIN
	r.left--;
	r.right++;
	r.bottom++;
	r.top = r.bottom - pt.v;
	if (fCorner) r.right -= pt.h - 1;
#endif

#if OPT_WINOS
	r.top = r.bottom - pt.v;
	if (fCorner) r.right -= pt.h;
#endif

	return r;
}

/*$	XGScrollView::CalcVScroll
 *
 *		Calculate the horizontal view
 */

XRect XGScrollView::CalcVScroll()
{
	XRect r;
	XPoint pt;
	
	pt = XGScrollBar::GetDefaultSize();
	r = GetContentRect();
	::InsetRect(&r,1,1);

#if OPT_MACOS || OPT_XWIN
	r.top--;
	r.right++;
	r.bottom++;
	r.left = r.right - pt.h;
	if (fCorner) r.bottom -= pt.v - 1;
#endif

#if OPT_WINOS
	r.left = r.right - pt.h;
	if (fCorner) r.bottom -= pt.v;
#endif

	return r;
}

/*$	XGScrollView::CalcCornerRect
 *
 *		Calculate the size of the corner
 */

XRect XGScrollView::CalcCornerRect()
{
	XRect r;
	XPoint pt;
	
	pt = XGScrollBar::GetDefaultSize();
	r = GetContentRect();
	::InsetRect(&r,1,1);

#if OPT_MACOS || OPT_XWIN
	r.left = r.right - pt.h + 2;
	r.top = r.bottom - pt.v + 2;
#endif

#if OPT_WINOS
	r.left = r.right - pt.h;
	r.top = r.bottom - pt.v;
#endif

	return r;
}

/*$	XGScrollView::CalcContentRect
 *
 *		Calculate the size of the corner
 */

XRect XGScrollView::CalcContentRect()
{
	XRect r;
	XPoint pt;
	
	pt = XGScrollBar::GetDefaultSize();
	r = GetContentRect();
	::InsetRect(&r,1,1);

#if OPT_MACOS || OPT_XWIN
	if (fVert)  r.right -= pt.h - 1;
	if (fHoriz) r.bottom -= pt.v - 1;
#endif

#if OPT_WINOS
	if (fVert)  r.right -= pt.h;
	if (fHoriz) r.bottom -= pt.v;
#endif

	return r;
}
