

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

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

#include "bitmaps/menuicon.bitmap" /* pattern for mentor icon	M*/
#include "bitmaps/gray.bitmap"	/* pattern for screen background  M*/

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

extern int	mm_debug;

int pid[MAXPROCS];
int pcount;
int tmppid=0;
int xproc;


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;
	system("/mit/visual/tools/mentor/install_defs");
	system("attach vsStaffTools");

	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,"MENTOR");
	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, "MENTOR" );
	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);

	DesktopInit();

	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
DesktopInit()
{

  for (pcount=0; pcount<MAXPROCS; pcount++)
    pid[pcount]=0;
  pcount=0;

	if ((tmppid = fork()) == 0) {
		execlp("/usr/bin/X10/xclock","xclock","-digital","=-0+0",(char *) 0);
		_exit();
	} 
	pid[pcount++] = tmppid;
  printf("Value of tmppid is %d\n",tmppid);
	printf("Process xclock #%d created\n", pid[pcount-1]);

	if ((tmppid = fork()) == 0) {
		execlp("/usr/bin/X10/xload","xload","-scale","3","=350x75-0-0",(char *) 0);
		_exit();
	}
  printf("Value of tmppid is %d\n",tmppid);
	pid[pcount++] = tmppid;
	printf("Process xload #%d created\n", pid[pcount-1]);

	if ((tmppid = fork()) == 0) {
		execlp("/usr/bin/X10/emacs","emacs","-i","-w","=80x25-0+50",(char *) 0);
		_exit();
	}
  printf("Value of tmppid is %d\n",tmppid);
	pid[pcount++] = tmppid;
	printf("Process gnuemacs #%d created\n", pid[pcount-1]);

	printf("Default desktop now in place.\n");
}

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;
{
  int i;
  extern int pid[MAXPROCS];

  printf("Select in Menu <%s>: item is %d\n",
	mp->menuTitle,item);

  switch(mp->menuID)	{    /* which menu was selected? 	M*/
     case M_LOGOUT:  
	XFlush();
    /*  Call_Dialog_Box("This selection will kill all your windows and
	 log you out.\nDo you really want to do this?","Cancel","Logout"); M*/
	for (i=pcount-1;i>=0;i--) {
		kill(pid[i],SIGKILL); 
		printf("Killing process %d\n",pid[i]);
	}
	printf("Attempting to kill extra process -> %d\n", tmppid+1);
	kill(tmppid+1,SIGKILL);
	XFlush();
	exit(0);
	break;

     case M_HELP: 
       switch (item) {  /*  Choice in programs menu */
       case 1:
/*
	       if ((tmppid = fork()) == 0) {
		        execlp("/mit/StaffTools/bin/xmore","xmore","=80x25+50+150", "/site/mit/mrose/mm/mentor.txt",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
*/
 	        break;

       case 2:
		printf("Sorry, no Desktop Help available yet.\n");
		break;

       case 3:
		if ((tmppid = fork()) == 0) {
			execlp("/mit/visual/tools/bin/vcman","vcman",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
		break;

       case 4:
		if ((tmppid = fork()) == 0) {
			execlp("/mit/StaffTools/bin/xman","xman",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
		break;
        default:
	       printf("No valid choice taken.\n");
	       break;
	}  /* switch item (M_HELP)	M*/    
	break;
        
      case M_SYSTEM:
       switch (item) {  /*  Choice in programs menu */
       case 1:
	       if ((tmppid = fork()) == 0) {
		        execlp("/usr/bin/X10/xterm","xterm","=80x25+0+50",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
		break;

       case 2:
	       if ((tmppid = fork()) == 0) {
		 execlp("/usr/bin/X10/emacs","emacs","-i","-w","=80x25-0+50",(char *) 0);
		 _exit();
	}
	       pid[pcount++] = tmppid;
	       printf("Process gnuemacs #%d created\n", pid[pcount-1]);
	       break;

       case 3:
	       if ((tmppid = fork()) == 0) {
		 execlp("/bin/csh","csh","/mit/visual/tools/mentor/browse",(char *) 0);
		 _exit();
	}
	       pid[pcount++] = tmppid;
	       printf("Process Broswer #%d created\n", pid[pcount-1]);
	       break;

       case 4:
	       if ((tmppid = fork()) == 0) {
		 execlp("/mit/visual/tools/toolbox/toolbox","toolbox",(char *) 0);
		 _exit();
	}
	       pid[pcount++] = tmppid;
	       printf("Process ToolBox #%d created\n", pid[pcount-1]);
	       break;

       case 5:
	       /* Appl_SubMenu();  */
		printf("Sorry, there is no Applications Submenu yet.\n");
		break;

        default:
	       printf("No valid choice taken.\n");
	       break;
	}  /* switch item (M_HELP)	M*/    
	break;
        
     case M_USER: 
       switch (item) {  /*  Choice in programs menu */
       case 1:
/*		if ((tmppid = fork()) == 0) {
			execlp("/site/mrose/edd","edd",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
*/
		break;

       case 2:
/*
		if ((tmppid = fork()) == 0) {
			execlp("/site/mrose/jas","jas",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
*/
		break;

       case 3:
/*
		if ((tmppid = fork()) == 0) {
			execlp("/usr/bin/X10/xterm","xterm","=50x25-0+0","-ft","oldeng","-n","ToDo_List","-e","/usr/new/jove","/site/mrose/projX/todo.txt",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
*/
		break;

       case 4:
/*
		if ((tmppid = fork()) == 0) {
			execlp("/usr/bin/X10/xterm","xterm","=50x25+0-0","-n","What's Happening?","-e","/usr/ucb/vi","/site/mrose/MyCal",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
*/
		break;

       case 5:
/*
		if ((tmppid = fork()) == 0) {
			execlp("/site/mrose/bin/squeek","squeek",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
*/
		break;

        default:
	       printf("No valid choice taken.\n");
	       break;
	}  /* switch item (M_HELP)	M*/    
	break;
        
      case M_COURSEW:
       switch (item) {  /*  Choice in programs menu */
       case 1:
		printf("Selection is %s\n",mp->menuTitle);
		break;

       case 2:
		printf("Selection is %s\n",mp->menuTitle);
		break;

       case 3:
		printf("Selection is %s\n",mp->menuTitle);
		break;

       case 4:
		printf("Selection is %s\n",mp->menuTitle);
		system("attach neuro");
		if ((tmppid = fork()) == 0) {
			system("cd /mit/neuro/X; neuro");
			_exit();
		}
		printf("Invoking Neuroanatomy courseware; it will take a moment to load...\n");
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
		break;

       case 5:
		printf("Selection is %s\n",mp->menuTitle);
		break;

       case 6:
		printf("Selection is %s\n",mp->menuTitle);
		system("attach 1.38");
		if ((tmppid = fork()) == 0) {
			execlp("/mit/visual/tools/mentor/gepse/gepse","gepse",(char *) 0);
			_exit();
		}
		pid[pcount++] = tmppid;
		printf("Process #%d created\n", pid[pcount-1]);
		break;

        default:
	       printf("No valid choice taken.\n");
	       break;
	}  /* switch item (M_COURSEW)	M*/    
	break;

  case M_DEMO:
        switch (item) {  /*  Choice in programs menu */
        case 1:
	     system("/mit/visual/amuse/muse /mit/visual/courses/musedemo.dat &");

        case 2:
	     system("/mit/visual/amuse/muse /mit/visual/courses/musedemo.dat &");

        case 3:
	     system("/mit/visual/amuse/muse /mit/visual/courses/logdisc.dat &");
        default:
	       printf("No valid choice taken.\n");
	       break;
	}  /* switch item (M_DEMO)	M*/    
	break;

        
     default:  XFeep(1);
	       printf("Not implemented, be patient.\n");
	       break;
     }      /* switch choice[0] */

  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;
}
