/*	XStatusBar.cpp
 *
 *		This is the code which actually displays my status bar.
 */

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

  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 <XStatusBar.h>
#include <XDraw.h>

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

/*$	XGStatusBar::XGStatusBar
 *
 *		Construct me
 */

XGStatusBar::XGStatusBar(XGView *parent, XGArgStream &s) : XGSlider(parent,s)
{
	fCurValue = s.ReadInteger();
	fMinValue = s.ReadInteger();
	fMaxValue = s.ReadInteger();
	fUpdate = false;
}

/*$	XGStatusBar::~XGStatusBar
 *
 *		Go the way of the dodo
 */

XGStatusBar::~XGStatusBar()
{
}

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

/*$	XGStatusBar::DoDrawView
 *
 *		Draw the view immediately
 */

void XGStatusBar::DoDrawView(XRect)
{
	DrawStatusBar();
}

/************************************************************************/
/*																		*/
/*	Bar drawing															*/
/*																		*/
/************************************************************************/

/*$	XGStatusBar::DrawStatusBar
 *
 *		Draw the status bar immediately
 */

void XGStatusBar::DrawStatusBar(void)
{
	long min,max,val;
	long offset;
	XRect r;
	XRect s;
	
	XGDraw draw(this);
	r = GetContentRect();
	fUpdate = false;

	/* Reduce the min,max values to prevent overflow */
	min = fMinValue;
	max = fMaxValue;
	val = fCurValue;
	if (min >= max) {
		/* Huh? Don't display a thing */
		
//		draw.Draw3DRect(r,KXGEInset);
//		::InsetRect(&r,1,1);
		draw.Draw3DRect(r,KXGEBackground);
		return;
	}
	
	/* Frame the slider */	
	draw.Draw3DRect(r,KXGEFrame);
	::InsetRect(&r,1,1);
	
	while ((max - min) > 65536L) {
		max >>= 1;
		min >>= 1;
		val >>= 1;
	}
	
	/* Figure out if this is wider or taller and draw it */
	if (r.right - r.left > r.bottom - r.top) {
		/* Wide */
		offset = ((r.right - r.left) * (val - min)) / (max - min) + r.left;
		if (offset < r.left) offset = r.left;
		if (offset > r.right) offset = r.right;
		
		/* Draw the outset part of this thing */
		s = r;
		s.right = offset;
		if (s.right > s.left) {
			draw.Draw3DRect(s,KXGEOutset);
			::InsetRect(&s,1,1);
			draw.Draw3DRect(s,KXGEOutset);
			::InsetRect(&s,1,1);
			draw.Draw3DRect(s,KXGEBackground);
		}
		
		/* Draw the inset part of this thing */
		s = r;
		s.left = offset;
		if (s.left < s.right) {
			draw.Draw3DRect(s,KXGEInset);
			::InsetRect(&s,1,1);
			draw.Draw3DRect(s,KXGEBackground);
		}
	} else {
		/* Tall */
		offset = ((r.top - r.bottom) * (val - min)) / (max - min) + r.bottom;
		if (offset < r.top) offset = r.top;
		if (offset > r.bottom) offset = r.bottom;
		
		
		/* Draw the outset part of this thing */
		s = r;
		s.top = offset;
		if (s.bottom > s.top) {
			draw.Draw3DRect(s,KXGEOutset);
			::InsetRect(&s,1,1);
			draw.Draw3DRect(s,KXGEOutset);
			::InsetRect(&s,1,1);
			draw.Draw3DRect(s,KXGEBackground);
		}
		
		/* Draw the inset part of this thing */
		s = r;
		s.bottom = offset;
		if (s.top < s.bottom) {
			draw.Draw3DRect(s,KXGEInset);
			::InsetRect(&s,1,1);
			draw.Draw3DRect(s,KXGEBackground);
		}
	}
}



/************************************************************************/
/*																		*/
/*	Value management													*/
/*																		*/
/************************************************************************/

/*$	XGStatusBar::GetMinValue
 *
 *		Get my minimum value
 */

long XGStatusBar::GetMinValue()
{
	return fMinValue;
}

/*$	XGStatusBar::GetMaxValue
 *
 *		Get my maximum value
 */

long XGStatusBar::GetMaxValue()
{
	return fMaxValue;
}

/*$	XGStatusBar::GetValue
 8
 *		Get my current value
 */

long XGStatusBar::GetValue()
{
	return fCurValue;
}

/*$	XGStatusBar::SetMinValue
 *
 *		Set my minimum value.
 */

void XGStatusBar::SetMinValue(long value)
{
	if (fMinValue == value) return;
	fMinValue = value;
	fUpdate = true;
	InvalView();
}

/*$	XGStatusBar::SetMaxValue
 *
 *		Set my maximum value
 */

void XGStatusBar::SetMaxValue(long value)
{
	if (fMaxValue == value) return;
	fMaxValue = value;
	fUpdate = true;
	InvalView();
}

/*$	XGStatusBar::SetValue
 *
 *		Set my value. This draws the status bar immediately. The
 *	'fUpdate' flag is used to determine if I should update when the
 *	value is the same; that's set when the min or max values are changed.
 */

void XGStatusBar::SetValue(long value)
{
	if ((fCurValue == value) && !fUpdate) return;
	fCurValue = value;
	DrawStatusBar();
}
