
#include <stdio.h>
#include <X10/Xlib.h>
#include "MenuMgr.h"

#define gray_width 16
#define gray_height 16
static unsigned short gray_bits[] = {
   0xaaaa, 0x5555, 0xaaaa, 0x5555,
   0xaaaa, 0x5555, 0xaaaa, 0x5555,
   0xaaaa, 0x5555, 0xaaaa, 0x5555,
   0xaaaa, 0x5555, 0xaaaa, 0x5555};

#include "menuicon.bitmap"

MenuBarPtr	menubar;
Window		test, icon;
FontInfo *	fi;

extern int	mm_debug;

#define max(a,b)	((a) > (b) ? (a) : (b))

main(argc,argv)

	int	argc;
	char *	argv[];
{
int	i;
char *	list;
char *	fontname = "helv12";
char	fn[64];
char *	iconname;
XEvent	event;
int	HandleName();
int	HandleMenu();
Bitmap	graybitmap;
Pixmap	graypixmap;
Bitmap	iconbitmap;
Pixmap	iconpixmap;
int	twidth, theight;
char *	display_name;
char *	getenv();

	mm_debug = 0;

	if( XOpenDisplay()==NULL )
	{
		if( display_name = getenv("DISPLAY") )
			fprintf(stderr,"%s: unable to open display < %s >.\n",
				argv[0],display_name);
		else
			fprintf(stderr,"%s: DISPLAY variable not set!\n",
				argv[0]);
		exit(1);			
	}

	graybitmap = XStoreBitmap(gray_width,gray_height,gray_bits );
	graypixmap = XMakePixmap( graybitmap,BlackPixel,WhitePixel );
	iconbitmap = XStoreBitmap(menuicon_width,menuicon_height,menuicon_bits );
	iconpixmap = XMakePixmap( iconbitmap,BlackPixel,WhitePixel );
	if( (fi = XOpenFont( fontname )) == NULL )
	{
		fprintf(stderr,"%s: couln't open font %s\n",argv[0],fontname);
		exit(-1);
	}

	test = XCreateWindow( RootWindow, 0,0,DisplayWidth(),DisplayHeight(),
		0,graypixmap,graypixmap );
	sprintf(fn,"%s: MENU",argv[0]);
	twidth = max( XStringWidth( fn, fi, 0, 0 ), menuicon_width ) + 10;
	theight = menuicon_height + fi->height + 15;
	icon = XCreateWindow( RootWindow, DisplayWidth()-(100+twidth),100,
		twidth, theight,
		2,BlackPixmap,WhitePixmap );
	XSetIconWindow( test, icon );
	XSelectInput( icon, ExposeWindow|ExposeRegion|ButtonPressed );
	XStoreName( icon, fn );

	mm_InitMenus(argv[0]);
	menubar = mm_GetMenuBar( argv[0], 1, "Visual Courseware Group" );
	if( !menubar )
		exit(1);

	mm_SetNameCallback( menubar, HandleName );
	mm_SetMenuCallback( menubar, HandleMenu );	
	mm_AttachMenuBar( menubar, test );

	XSelectInput( test, mm_MBarParentEvents|ExposeWindow|ExposeRegion );
	XMapWindow(test);
	XMapSubwindows(test);

	while(1)
	{
		XNextEvent(&event);
		mm_HandleEvent(&event);
		if( event.window == test && 
			(event.type == ExposeRegion 
			|| event.type == ExposeWindow) )
			XLowerWindow( test );
		else if( event.window == icon )
		{
			if( event.type == ExposeRegion
			|| event.type == ExposeWindow )
			{
				XPixmapPut( icon, 0,0,
				twidth/2 - menuicon_width/2,5,
				menuicon_width,menuicon_height,
				iconpixmap,GXcopy,AllPlanes);
				XFetchName( icon, &iconname );
				XTextMask( icon, 5,
					menuicon_height+10,
					fn,strlen(fn),
					fi->id,BlackPixel);
			}
			else if( event.type == ButtonPressed )
			{
				IconXlate( icon );
			}
		}
	}

}

int
HandleName( mbp )

	MenuBarPtr	mbp;
{
	printf("Hit NameBar of menubar <%s>\n",mbp->mbarName);
	IconXlate( test );
	return 0;
}

int
HandleMenu( mp, item )

	MenuPtr		mp;
	int		item;
{
	printf("Select in Menu <%s>: item is %d\n",
		mp->menuTitle,item);
	return 0;
}

int
IconXlate( w )

	Window	w;
{
WindowInfo	wi1, wi2;
int		steps = 20;
int		xs,ys,ws,hs,i,j;

	XQueryWindow( w, &wi1 );
	if( !wi1.assoc_wind )
	{
		fprintf(stderr,"IconXlate: window has no associate\n");
		return -1;
	}

	XQueryWindow( wi1.assoc_wind, &wi2 );
	XUnmapWindow( w );
	XFlush();

	for( j=0, i=0; i<steps; i++ )
	{
		xs = (wi2.x - wi1.x)/steps;
		ys = (wi2.y - wi1.y)/steps;
		ws = (wi2.width - wi1.width)/steps;
		hs = (wi2.height - wi1.height)/steps;

		DrawBox( RootWindow,
			wi1.x + (i*xs),
			wi1.y + (i*ys),
			wi1.width + (i*ws),
			wi1.height + (i*hs),
			2,
			BlackPixel,
			GXinvert,
			AllPlanes );

		if( i>2 )
		{
		DrawBox( RootWindow,
			wi1.x + (j*xs),
			wi1.y + (j*ys),
			wi1.width + (j*ws),
			wi1.height + (j*hs),
			2,
			BlackPixel,
			GXinvert,
			AllPlanes );
		j++;
		}

		XFlush();
		mpause(20);
	}

	for( ; j<steps; j++ )
		DrawBox( RootWindow,
			wi1.x + (j*xs),
			wi1.y + (j*ys),
			wi1.width + (j*ws),
			wi1.height + (j*hs),
			2,
			BlackPixel,
			GXinvert,
			AllPlanes );
	
	XMapWindow( wi1.assoc_wind );
	return 0;
}

int
DrawBox( win, x,y, w,h, thickness, pixel, func, planes )

	Window 	win;
	int	x,y,w,h,thickness,pixel,func,planes;
{
static Vertex	vlist[5];

	vlist[0].x = x;
	vlist[0].y = y;
	vlist[1].x = (x+w)-1;
	vlist[1].y = y;
	vlist[2].x = (x+w)-1;
	vlist[2].y = (y+h)-1;
	vlist[3].x = x;
	vlist[3].y = (y+h)-1;
	vlist[4].x = x;
	vlist[4].y = y;
	XDraw( win, vlist, 5, thickness, thickness, pixel, func, planes );
	return 0;
}
