/*	XGMenuPop.cpp
 *
 *		This is the view which manages the popup menu lists
 */

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

  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>

#if OPT_XWIN

#include "XGMenuView.h"
#include <XDraw.h>
#include <XMenu.h>

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

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

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

/*	XGMenuPop::XGMenuPop
 *
 *		Create me. This creates a menu popup object used by X Windows.
 */

XGMenuPop::XGMenuPop(XGMenu *m) : XGView(NULL,MenuInitRecord)
{
	fMenu = m;
	fLocation = -1;
}

/*	XGMenuPop::~XGMenuPop
 *
 *		Go away
 */

XGMenuPop::~XGMenuPop()
{
}

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

/*	XGMenuPop::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 XGMenuPop::GetPopSize(XGDraw &draw)
{
	XPoint size;
	int i,len;
	char buffer[256];
	int width;
	int x;
	XGSMenuItem im;
	
	/*
	 *	Find height 
	 */
	
	len = fMenu->GetLength();
	size.v = (draw.GetFontHeight()+2) * len;

	/*
	 *	Find width 
	 */
	
	width = 20;
	for (i = 0; i < len; i++) {
		
		fMenu->fMenuData.GetString(i,buffer);
		fMenu->fMenuData.GetExtra(i,(void *)&im);
		x = draw.StringWidthA(buffer) + 30;
		if (im.fAccelerator) {
			sprintf(buffer,"Meta+%c",(char)im.fAccelerator);
			x += draw.StringWidth(buffer);
		}
		if (width < x) width = x;
	}
	size.h = width;
	
	/*
	 *	Now bump for the border
	 */
	
	size.h += 6;
	size.v += 6;
	return size;
}

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

/*	XGMenuPop::DoDrawView
 *
 *		Draw the contents of this view
 */

void XGMenuPop::DoDrawView(XRect)
{
	XRect r;
	int i;
	XGDraw draw(this);
	draw.SetFont(XGFont::LoadFont(0));			// system font

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

	for (i = 0; i < fMenu->GetLength(); i++) {
		DrawLine(draw,i,(i == fLocation));
	}
}

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

void XGMenuPop::DoMouseMove(XPoint where,short)
{
	short h;
	XGDraw draw(this);
	draw.SetFont(XGFont::LoadFont(0));
	
	if (where.v < 3) {
		h = -1;
	} else {
		h = draw.GetFontHeight() + 2;
		h = (where.v - 3) / h;
	
		if ((h < 0) || (h >= fMenu->GetLength())) h = -1;
	}
	if (fLocation == h) return;
	
	/*
	 *	Redraw the two items
	 */
	
	if (fLocation != -1) DrawLine(draw,fLocation,false);
	if (h != -1) DrawLine(draw,h,true);
	fLocation = h;
}


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

/*	XGMenuPop::DrawLine
 *
 *		Draw the text line
 */

void XGMenuPop::DrawLine(XGDraw &draw, short line, bool hilite)
{
	XRect r;
	int h;
	XGSMenuItem im;
	char buffer[32];
	char title[256];
	
	r = GetContentRect();
	::InsetRect(&r,3,3);
	h = draw.GetFontHeight() + 2;
	r.top = line * h + 3;
	r.bottom = r.top + h;
	
	h = r.bottom - 1 - draw.GetFontDescent();
	
	/*
	 *	Draw the dsabled text line (if the line is disabled)
	 */

	fMenu->fMenuData.GetExtra(line,(void *)&im);
	fMenu->fMenuData.GetString(line,title);
	
	if (title[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,KXGColorWhite);
		return;
	}
	
	if (!im.fEnabled) {
		draw.Draw3DRect(r,KXGEBackground);
		draw.SetForeColor(KXGColorBtnShadow);			// dim color
		
		if (im.fCheckmark) {
			char check;
			check = im.fCheckmark;
			
			if (check < ' ') {
				draw.MoveTo(5,h-3);
				draw.LineTo(7,h-1);
				draw.LineTo(11,h-9);
			} else {
				draw.MoveTo(5,h);
				draw.DrawString(&check,1);
			}
		}
	
		draw.MoveTo(20,h);
		draw.DrawStringA(title);
		
		if (im.fAccelerator) {
			sprintf(buffer,"Meta+%c",im.fAccelerator);
			draw.MoveTo(r.right - 5 - draw.StringWidth(buffer),h);
			draw.DrawString(buffer);
		}
		return;
	} else {
		draw.SetForeColor(KXGColorBlack);
	}
	
	/*
	 *	Draw the hilite
	 */
	
	if (hilite) {
		draw.FillRect(r,KXGColorBtnFace);
		draw.Draw3DRect(r,KXGEInset);
	} else {
		draw.Draw3DRect(r,KXGEBackground);
	}
	
	/*
	 *	Draw the line
	 */
	
		
	if (im.fCheckmark) {
		char check;
		check = im.fCheckmark;
		
		if (check < ' ') {
			draw.MoveTo(5,h-3);
			draw.LineTo(7,h-1);
			draw.LineTo(11,h-9);
		} else {
			draw.MoveTo(5,h);
			draw.DrawString(&check,1);
		}
	}
	
	draw.MoveTo(20,h);
	draw.DrawStringA(title);
	
	if (im.fAccelerator) {
		sprintf(buffer,"Meta+%c",(char)im.fAccelerator);
		draw.MoveTo(r.right - 5 - draw.StringWidth(buffer),h);
		draw.DrawString(buffer);
	}
}

#endif // OPT_XWIN
