/*
 *	(c) 1998-99 Sassafras Software Inc.
 *
 *	Simple bottom-of-window 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 <XScroll.h>
#include <XDraw.h>
#include <XWindow.h>
#include <XCursorUtil.h>
#include <XStringUtil.h>
#include <XFooterBar.h>

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

/*$	XGFooterBar::XGFooterBar
 *
 *		Build me
 */

XGFooterBar::XGFooterBar (XGView *owner, XGArgStream &stream) : XGView(owner,stream)
{
	fTrays = stream.ReadInteger();
	fResID = stream.ReadInteger();
	fLeaveRoom = stream.ReadBoolean();
	fClickable = stream.ReadBoolean();
	/*fRemovable = */stream.ReadBoolean();

	IInitView();
}

XGFooterBar::XGFooterBar (XGView *parent, XGFooterBarInitRecord &init) : XGView(parent, init.v)
{
	fTrays = init.trays;
	fResID = init.resID;
	fLeaveRoom = init.leaveroom;
	fClickable = init.clickable;
	/*fRemovable = init.removable;*/

	IInitView();
}

XGFooterBar::~XGFooterBar()
{

}

void XGFooterBar::SetTrayLabel (long ix, char *name)
{
	if (ix >= 0 && ix < fTrays) {
		try {
			XRect r;
			XGDraw draw(this);
			while (ix >= fLabels.GetNumStrings())
				fLabels.Insert(fLabels.GetNumStrings(), "");
			fLabels.SetString(ix, name);
			GetTrayBox(ix, &r);
			InvalView(&r);
		} catch (...) { }
	}
}

long XGFooterBar::SetTrayWidth (long ix, long width, bool shift)
{
	width -= GetTrayWidth(ix);
	return(GrowTrayWidth(ix, width, shift));
}

long XGFooterBar::GrowTrayWidth (long ix, long delta, bool shift)
{
	XRect r, box;
	int width, cix;

	if (ix < 0 || ix >= fTrays - 1)		/* the last tray has infinite width */
		return(0);

	width = fPositions[ix + 1].left - fPositions[ix].left;
	if (delta) {
		cix = ix + 1;
		if (shift) {
			while (cix < fTrays)
				fPositions[cix++].left += delta;
		} else {
			if (cix < fTrays - 1)
				width = fPositions[cix + 1].left - fPositions[cix].left;
			fPositions[cix].left += delta;
		}
		if (delta) {
			r = GetContentRect();
			box = r;
			box.left = fPositions[ix + 1].left;
			if (delta > 0)
				box.left -= delta;
			if (!shift && (ix < fTrays - 2))
				box.right = fPositions[ix + 2].left;
			box.right--;
			ScrollView(delta, 0, &box, KXGColorBtnFace);

			r.right = fPositions[ix + 1].left;
			r.left = r.right - 5;
			if (delta > 0)
				r.left -= delta;
			InvalView(&r);
		}
	}
	return(delta);
}

long XGFooterBar::GetTrayWidth (long ix)
{
	XRect r;

	GetTrayBox(ix, &r);
	return(r.right - r.left);
}

void XGFooterBar::GetTrayBox (long ix, XRect *box)
{
	XRect r = GetContentRect();

	box->top = 0;
	box->bottom = r.bottom - r.top;
	if (ix < 0 || ix >= fTrays) {
		box->left = 0;
		box->right = 0;
	} else {
		box->left = fPositions[ix].left;
		ix += 1;
		if (ix == fTrays)
			box->right = r.right - r.left;
		else
			box->right = fPositions[ix].left;
	}
}

short XGFooterBar::GetTrayHilite (long ix)
{
	if (ix >= 0 && ix < fTrays)
		return(fPositions[ix].hilited);
	return(false);
}

void XGFooterBar::SetTrayHilite (long ix, short hilite)
{
	XRect r;

	if (ix >= 0 && ix < fTrays) {
		fPositions[ix].hilited = hilite;
		GetTrayBox(ix, &r);
		InvalView(&r);
		UpdateView();
	}
}

long XGFooterBar::InsertTray (long before)
{
	XGFooterBarPositionRecord posn;

	if (before >= 0 && before < fTrays) {
		posn.left = fPositions[before].left;
	} else {
		before = fTrays;
		posn.left = 10000;
	}
	posn.hilited = false;
	try {
		fPositions.Insert(before, posn);
		fTrays++;
		InvalView();
	} catch (...) {
		before = -1;
	}
	return(before);
}

void XGFooterBar::DeleteTray (long ix)
{
	if (ix >= 0 && ix < fTrays) {
		SetTrayWidth(ix, 0);
		fPositions.Delete(ix);
		fTrays--;
	}
}
		
void XGFooterBar::DoDrawView(XRect)
{
	long i, vin;
	XRect r, vr, sr;
	XGDraw draw(this);

	vr = GetContentRect();
	vin = (vr.bottom - vr.top - draw.GetFontHeight()) >> 1;
	if (vin <= 0)
		vin = 1;

	draw.Draw3DRect(vr, KXGEBackground);
	for (i = 0; i < fTrays; i++) {
		GetTrayBox(i, &r);
		if (::SectRect(&vr, &r, &sr)) {
			draw.ClipRect(&r);

			::InsetRect(&r, 2, vin);
			r.right += 1;
			if (fLeaveRoom & (i == fTrays - 1))
				r.right += 2;
#if OPT_MACOS != 1
			draw.Draw3DRect(r, KXGEInset);
#endif

			DrawTray(draw, r, i, fPositions[i].hilited);

			draw.ClipRect();
		}
	}
}

void
XGFooterBar::DrawTray (XGDraw &draw, XRect &r, long ix, short hilite)
{
	char s[256];
	long descent = draw.GetFontDescent();
	long h = draw.GetFontAscent() + descent;

	try {
		fLabels.GetString(ix, s);
		draw.MoveTo(r.left + 8, r.bottom - ((r.bottom - r.top - h + 1) >> 1) - descent);
		if (hilite >= 0)
			draw.SetForeColor(KXGColorBlack);
		else
			draw.SetForeColor(KXGColorBtnShadow);
		draw.DrawString(s, -1);
	} catch (...) { }
}

long
XGFooterBar::InTray (XPoint where)
{
	XRect r;
	int i;

	r = GetContentRect();
	if (where.v < r.top || where.v > r.bottom)
		return(-1);

	for (i = fTrays; --i >= 0; ) {
		if (where.h >= fPositions[i].left)
			break;
	}
	return(i);
}

bool
XGFooterBar::DoMouseDown (XPoint where, short)
{
	int tray;

	//SetFocus();
	if (fClickable && ((tray = InTray(where)) >= 0)) {
		fLastSlop = (GetTrayHilite(tray) == 0);
		fLastState = 3;
		if (fLastSlop)
			fLastState = 2;
		fLastCol = tray;
		SetTrayHilite(fLastCol, -fLastSlop);
		return true;
	}
	return false;
}

void
XGFooterBar::DoMouseMove (XPoint where, short flags)
{
	if (!(flags & KCapturedMouse)) {
		XGView::DoMouseMove(where,flags);
	} else {
		if (fClickable) {
			bool inside = (fLastCol == InTray(where));
			if (fLastState == 2) {
				if (fLastSlop != inside) {
					fLastSlop = inside;
					SetTrayHilite(fLastCol, -fLastSlop);
				}
			} else {
				if (fLastSlop != inside) {
					fLastSlop = inside;
					SetTrayHilite(fLastCol, 1 - (fLastSlop << 1));
				}
			}
		}
	}
}

void
XGFooterBar::DoMouseUp (XPoint where, short flags)
{
	long pargs[2];

	if (!(flags & KCapturedMouse)) {
		XGView::DoMouseUp(where,flags);
	} else {
		if (fClickable) {
			if (GetTrayHilite(fLastCol)) {
				SetTrayHilite(fLastCol, 1);
				for (int i = 0; i < fTrays; i++) {
					if ((i != fLastCol) && GetTrayHilite(i))
						SetTrayHilite(i, 0);
				}
				pargs[0] = fLastCol;
				pargs[1] = flags;
				DoDispatch(KFooterBarSelect, GetViewID(), pargs);
			}
		}
		fLastState = -1;
		fLastCol = -1;
	}
}

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

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

long
XGFooterBar::GetHeight (void)
{
	return(XGScrollBar::GetDefaultSize().v - 2);
}

void
XGFooterBar::IInitView (void)
{
	int i;
	char s[64];

	fLastState = -1;
	fLastCol = -1;

	fPositions.SetLength(fTrays);
	for (i = 0; i < fTrays; i++) {
		fPositions[i].left = 0;
		fPositions[i].hilited = 0;
		XGGetIndResString(fResID, i, s, sizeof(s));
		SetTrayLabel(i, s);
	}
}