/*	XGPopMenuView.cpp
 *
 *		The code which handles the popup menu view
 */

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

  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 <XDraw.h>
#include <XPopup.h>
#include <XMisc.h>
#include <ctype.h>
#include <string.h>

/************************************************************************/
/*																		*/
/*	Globals																*/
/*																		*/
/************************************************************************/

static XGSViewInitRecord MenuInitRecord = {
	'****', 1, 0,
	0, 0, 0, 0,
	true, true, true, true,
	true, true, false
};

/************************************************************************/
/*																		*/
/*	String support														*/
/*																		*/
/************************************************************************/

/*	CompareStrings
 *
 *		Compare a C string against a length specified string. Return
 *	1 if str bigger than ptr, 0 if equal, and -1 if less.
 */

static int CompareStrings(short len, char *ptr, char *str)
{
	short i;
	char a,b;
	
	for (i = 0; i < len; i++) {
		if (toupper(str[i]) != toupper(ptr[i])) break;
	}
	if (i >= len) {
		if (str[i] != 0) return 1;
		return 0;
	}
	
	a = toupper(str[i]);
	b = toupper(ptr[i]);
	if (a < b) return -1;
	if (a > b) return 1;
	return 0;
}

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

/*$	XGPopMenuView::XGPopMenuView
 *
 *		Create me
 */

XGPopMenuView::XGPopMenuView(XGStringList *l, short f, short d) : 
		XGView(NULL,MenuInitRecord)
{
	if ((d < 0) || (d >= l->GetNumStrings())) d = 0;

	fDefault = d;
	fList = l;
	fFont = f;
	fLocation = fDefault;
	
	fHeight = 0;
	fScroll = 0;
	
	fTime = 0;
	fScrollFlag = false;
}

/*$	XGPopMenuView::~XGPopMenuView
 *
 *		Go away
 */

XGPopMenuView::~XGPopMenuView()
{
}

/************************************************************************/
/*																		*/
/*	Size preflight														*/
/*																		*/
/************************************************************************/

/*$	XGPopMenuView::GetPopSize
 *
 *		The drawing object I'm handed is already initialized. All I
 *	do is figure out how big my menu should be.
 *
 *		Dimensions:
 *
 *			| <-20-> MENUNAME <-5-> ACCEL <-5-> |
 */

XPoint XGPopMenuView::GetPopSize(XGDraw &draw)
{
	XPoint size;
	int i,len;
	int width;
	int x;
	char item[256];
	
	/*
	 *	Find height 
	 */
	
	len = fList->GetNumStrings();
	size.v = (draw.GetFontHeight()) * len;

	/*
	 *	Find width 
	 */
	
	width = 20;
	for (i = 0; i < len; i++) {
		fList->GetString(i,item);
		x = draw.StringWidth(item) + 30;
		if (width < x) width = x;
	}
	size.h = width;
	
	/*
	 *	Now bump for the border
	 */
	
	size.h += 6;
	size.v += 6;
	return size;
}

/************************************************************************/
/*																		*/
/*	Event Processing													*/
/*																		*/
/************************************************************************/

/*$	XGPopMenuView::DoDrawView
 *
 *		Draw the contents of this view
 */

void XGPopMenuView::DoDrawView(XRect)
{
	XRect r;
	int i;
	XGDraw draw(this);
	draw.SetFont(XGFont::LoadFont(fFont));
	short t,b;

	r = GetContentRect();	
	draw.Draw3DRect(r,KXGEBackground);
	draw.Draw3DRect(r,KXGEFrame);
	::InsetRect(&r,1,1);
	draw.Draw3DRect(r,KXGEOutset);
	::InsetRect(&r,1,1);

	if (fScroll > 0) {
		DrawUDArrow(draw,true);
	}
	if (fScroll < fMaxScroll) {
		DrawUDArrow(draw,false);
	}
	
	if (fScroll == 0) {
		t = 0;
	} else {
		t = fScroll+1;
	}
	
	if (fScroll < fMaxScroll) {
		b = fHeight + fScroll - 1;
	} else {
		b = fList->GetNumStrings();
	}
	
	for (i = t; i < b; i++) {
		DrawLine(draw,i,(i == fLocation));
	}
}

/*$	XGPopMenuView::DoSizeView
 *
 *		Handle the initial resize as I'm created
 */

void XGPopMenuView::DoSizeView()
{
	RecalcScroll();
	ScrollInRange();
}

/*$	XGPopMenuView::DoMouseMove
 *
 *		Handle mouse move. I assume this is down
 */

void XGPopMenuView::DoMouseMove(XPoint where, short)
{
	short h;
	XGDraw draw(this);
	draw.SetFont(XGFont::LoadFont(fFont));
	
	/*
	 *	Find selection
	 */
	
	fLastPos = where;
	
	if (where.v < 0) {
		/*
		 *	Don't autoscroll--this is done to prevent losing
		 *	the scroll position, yet reselecting the default
		 */
		
		h = fDefault;
		if (fLocation != h) {
			if (fLocation != -1) DrawLine(draw,fLocation,false);
			if (h != -1) DrawLine(draw,h,true);
			fLocation = h;
			fScrollFlag = false;
		}		
		return;
	} else {
		h = draw.GetFontHeight();
		h = (where.v - 3) / h;
		h += fScroll;
	
		if ((h < 0) || (h >= fList->GetNumStrings())) h = fDefault;
	}
	
	/*
	 *	Set selection (and scroll in range if not in range)
	 */
	
	if (fLocation != h) {
		if (fLocation != -1) DrawLine(draw,fLocation,false);
		if (h != -1) DrawLine(draw,h,true);
		fLocation = h;
	}
	
	/*
	 *	Autoscroll (but only up to 10 times a second)
	 */
	
	if ((fTime == 0) || (fTime < XGGetTime())) {
		fScrollFlag = ScrollInRange();
		fTime = XGGetTime() + (XGGetSecond() / 10);
	}
}

/*$	XGPopMenuView::DoPrePeriodic
 *
 *		Figure out where the mouse was the last time the user moved
 *	the mouse, and tickle the mouse move event again. This allows
 *	me to repeat scrolling
 */

void XGPopMenuView::DoPrePeriodic()
{
	XRect r;
	
	if (fScrollFlag && (fTime < XGGetTime())) {
		r = GetContentRect();
		if (PtInRect(fLastPos,&r)) {
			DoMouseMove(fLastPos,0);
		}
	}
}

/*$	XGPopMenuView::MoveUp
 *
 *		Move default selection up one
 */

void XGPopMenuView::MoveUp()
{
	fDefault = fLocation;
	if (fDefault > 0) fDefault--;

	fScrollFlag = false;
	SetSelect(fDefault);
}

/*$	XGPopMenuView::MoveDown
 *
 *		Move default selection down one
 */

void XGPopMenuView::MoveDown()
{
	short len = fList->GetNumStrings() - 1;
	fDefault = fLocation;
	fDefault++;
	if (fDefault > len) fDefault = len;

	fScrollFlag = false;
	SetSelect(fDefault);
}

/*$	XGPopMenuView::MoveTo
 *
 *		This searches for the string that is the closest fit to the
 *	characters provided, and sets that as the current selection.
 */

void XGPopMenuView::MoveTo(short len, char *ptr)
{
	char buffer[256];
	char cptr[256];
	short i,num;
	short cur = -1;
	
	if (len == 0) return;		// Huh?
	
	/*
	 *	Find the smallest bigger than this ptr
	 */
	
	num = fList->GetNumStrings();
	for (i = 0; i < num; i++) {
		fList->GetString(i,buffer);
		if (-1 < CompareStrings(len,ptr,buffer)) {
			if (cur == -1) {
				cur = i;
				strcpy(cptr,buffer);
			} else if (-1 < CompareStrings(strlen(buffer),buffer,cptr)) {
				cur = i;
				strcpy(cptr,buffer);
			}
		}
	}
	
	if (cur == -1) {
		/*
		 *	None were bigger, so find the last in alphabetical order
		 */
		
		for (i = 0; i < num; i++) {
			fList->GetString(i,buffer);
			if (cur == -1) {
				cur = i;
				strcpy(cptr,buffer);
			} else if (-1 < CompareStrings(strlen(cptr),cptr,buffer)) {
				cur = i;
				strcpy(ptr,buffer);
			}
		}
	}
	
	/*
	 *	Now set this as the selection
	 */
	
	fDefault = cur;
	fScrollFlag = false;
	SetSelect(fDefault);
}

/*$	XGPopMenuView::SetSelect
 *
 *		Change the selection to the one specified
 */

void XGPopMenuView::SetSelect(short h)
{
	XGDraw draw(this);
	draw.SetFont(XGFont::LoadFont(fFont));
	
	if (fLocation != h) {
		if (fLocation != -1) DrawLine(draw,fLocation,false);
		if (h != -1) DrawLine(draw,h,true);
		fLocation = h;
	}
	
	ScrollInRange();
}

/************************************************************************/
/*																		*/
/*	Low level															*/
/*																		*/
/************************************************************************/

/*$	XGPopMenuView::ScrollInRange
 *
 *		Scroll this thing into range if it's not.
 */

bool XGPopMenuView::ScrollInRange()
{
	short nscroll;
	short b;
	
	if (fLocation == -1) return false;	// nothing selected.
	
	nscroll = fScroll;
	if (fLocation <= fScroll) {
		if (fLocation == 0) nscroll = 0;
		else nscroll = fLocation - 1;
		if (nscroll < 0) nscroll = 0;
	} else {
		if (fScroll < fMaxScroll) {
			b = fHeight + fScroll - 1;
		} else {
			b = fList->GetNumStrings();
		}
		if (fLocation >= b) {
			nscroll = fLocation + 2 - fHeight;
			if (nscroll > fMaxScroll) nscroll = fMaxScroll;
		}
	}
	
	if (nscroll != fScroll) {
		fScroll = nscroll;
		DoDrawView(GetContentRect());
		return true;
	} else {
		return false;
	}
}

/*$	XGPopMenuView::RecalcScroll
 *
 *		Recalculate the scrolling limits of this. This determines the
 *	height of the area where I actually fit, and calculates the
 *	various scroll parameters
 */

void XGPopMenuView::RecalcScroll()
{
	XRect r;
	short h;
	
	XGDraw draw(this);
	draw.SetFont(XGFont::LoadFont(fFont));
	
	r = GetContentRect();
	fScroll = 0;
	fHeight = (r.bottom - r.top - 6) / draw.GetFontHeight();
	h = fList->GetNumStrings();
	if (h > fHeight) {
		fMaxScroll = h - fHeight;
	} else {
		fMaxScroll = 0;
	}
}

/*$	XGPopMenuView::DrawUDArrow
 *
 *		Draw up/down arrow
 */

void XGPopMenuView::DrawUDArrow(XGDraw &draw, bool upflag)
{
	XRect r;
	int h;
	int x;
	XRect s;
	int m;
	
	r = GetContentRect();
	::InsetRect(&r,3,3);
	h = draw.GetFontHeight();
	
	if (upflag) {
		r.bottom = r.top + h;
	} else {
		r.top = r.bottom - h;
	}
	
	draw.Draw3DRect(r,KXGEBackground);
	
	s.left = 20;
	s.right = s.left + h;
	h /= 2;
	s.top = (r.top + r.bottom - h)/2;
	s.bottom = s.top + h;

	::InsetRect(&s,1,1);
	m = (s.left + s.right)/2;
	
	for (x = 0; x < h; x++) {
		if (upflag) {
			draw.MoveTo(m-x,s.top+x);
			draw.LineTo(m+x+1,s.top+x);
		} else {
			draw.MoveTo(m-x,s.bottom-x);
			draw.LineTo(m+x+1,s.bottom-x);
		}
	}
}

/*$	XGPopMenuView::DrawLine
 *
 *		Draw the text line
 */

void XGPopMenuView::DrawLine(XGDraw &draw, short line, bool hilite)
{
	XRect r;
	int h;
	char item[256];
	char *iptr;
	bool enable;
	short t,b;
	
	/*
	 *	Verify that I'm drawing in range
	 */
	
	if (fScroll == 0) {
		t = 0;
	} else {
		t = fScroll+1;
	}
	
	if (fScroll < fMaxScroll) {
		b = fHeight + fScroll - 1;
	} else {
		b = fList->GetNumStrings();
	}
	
	if ((line < t) || (line >= b)) return;
	
	/*
	 *	Find where I'm drawing
	 */

	r = GetContentRect();
	::InsetRect(&r,3,3);
	h = draw.GetFontHeight();
	r.top = (line - fScroll) * h + 3;
	r.bottom = r.top + h;
	
	h = r.bottom - 1 - draw.GetFontDescent();
	
	/*
	 *	Draw the dsabled text line (if the line is disabled)
	 */

	fList->GetString(line,item);
	if (item[0] == 0) {
		XRect s;
		
		/*
		 *	Separator
		 */
		
		draw.Draw3DRect(r,KXGEBackground);
		s = r;
		s.top = (r.top + r.bottom) / 2;
		s.bottom = s.top + 1;
		draw.FillRect(s,KXGColorBtnShadow);
		s.top++;
		s.bottom++;
		draw.FillRect(s,KXGColorBtnHilite);
		return;
	}
		
	/*
	 *	Draw the hilite
	 */
	
	iptr = item;
	if (*iptr == 0x02) {
		enable = false;
		iptr++;
	} else {
		enable = true;
	}
	if (enable) {
		if (hilite) {
			draw.FillRect(r,KXGColorMHilite);
			draw.SetForeColor(KXGColorMHiliteText);
		} else {
			draw.SetForeColor(KXGColorBlack);
			draw.Draw3DRect(r,KXGEBackground);
		}
	} else {
		draw.SetForeColor(KXGColorBtnShadow);		// disabled text
		draw.Draw3DRect(r,KXGEBackground);
	}
		
	/*
	 *	Draw the line
	 */
	
	if (*iptr == 0x01) {
		XRect s;
		
		iptr++;
		s.top = r.top + 2;
		s.left = 5;
		s.bottom = h;
		s.right = s.left + (s.bottom - s.top);
		draw.PaintOval(s);
	}
	draw.MoveTo(20,h);
	draw.DrawString(iptr);
}
