/*	XGHeaderIcon.cpp
 *
 *		Small header icon, typically used for sort direction
 *	of list views.
 */

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

  XGHeaderIcon.cpp is Copyright 1998-1999 Sassafras Software Inc.
  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 <XScroll.h>
#include <XDraw.h>
#include <XCursorUtil.h>
#include <XHeaderIcon.h>

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

/*$	XGHeaderIcon::XGHeaderIcon
 *
 *		Build me
 */

XGHeaderIcon::XGHeaderIcon (XGView *owner, XGArgStream &stream) : XGControl(owner,stream)
{
	fNumStates = stream.ReadInteger();
	fResID = stream.ReadInteger();
	fClickable = stream.ReadBoolean();

	IInitView();
}

XGHeaderIcon::XGHeaderIcon (XGView *parent, XGHeaderIconInitRecord &init) : XGControl(parent, init.v)
{
	fNumStates = init.states;
	fResID = init.resID;
	fClickable = init.clickable;

	IInitView();
}

XGHeaderIcon::~XGHeaderIcon (void)
{

}

long XGHeaderIcon::GetValue (void)
{
	return(fState);
}

void XGHeaderIcon::SetValue (long state)
{
	fState = state;
	InvalView();
	UpdateView();
}

void XGHeaderIcon::DoDrawView(XRect)
{
	XRect r;
	long x, y;
	XGDraw draw(this);

	r = GetContentRect();
	x = r.left + ((r.right - r.left - 16) >> 1);
	y = r.top + ((r.bottom - r.top - 16) >> 1);

	draw.Draw3DRect(r, KXGEBackground);
	draw.ClipRect(&r);

	::InsetRect(&r, 0, -1);
	r.right += 1;
	if (fWithin)
		draw.Draw3DRect(r, KXGEInButton);
	else
		draw.Draw3DRect(r, KXGEButton);

	draw.DrawIcon(fResID + fState, x, y);

	draw.ClipRect();
}

bool
XGHeaderIcon::DoMouseDown (XPoint, short)
{
	//SetFocus();
	if (fClickable) {
		fWithin = true;
		InvalView();
		UpdateView();
		return true;
	}
	return false;
}

void
XGHeaderIcon::DoMouseMove (XPoint where, short flags)
{
	if (!(flags & KCapturedMouse)) {
		XGView::DoMouseMove(where,flags);
	} else {
		if (fClickable) {
			XRect r = GetContentRect();
			bool inside = PtInRect(where, &r);
			if (fWithin != inside) {
				fWithin = inside;
				InvalView();
				UpdateView();
			}
		}
	}
}

void
XGHeaderIcon::DoMouseUp (XPoint where, short flags)
{	
	if (!(flags & KCapturedMouse)) {
		XGView::DoMouseUp(where,flags);
	} else {
		if (fClickable) {
			if (fWithin) {
				fWithin = false;
				SetValue((GetValue() + 1) % fNumStates);
				DoDispatch(KHeaderIconChanged, GetViewID(), (void *)NULL);
			}
		}
	}
}

void
XGHeaderIcon::RecalcAutoLocation (XGView *below, XGView *beside, XRect *r)
{
	XRect tr;

	if (!r) {
		r = &tr;
		tr.top = 0;
		tr.left = -XGScrollBar::GetDefaultSize().h + 1;
		tr.bottom = tr.top + GetHeight();
		tr.right = 0x8000;
	}
	XGView::RecalcAutoLocation(below, beside, r);
}

long
XGHeaderIcon::GetHeight (void)
{
	XGDraw draw(this);
	return(draw.GetFontHeight() + 4);
}

void
XGHeaderIcon::IInitView (void)
{
	fState = 0;
	fWithin = false;
}