/*==================================================================
  ==================================================================
  ==	XMusic -- An Interactive Interface to Csound		  ==
  ==       Copyright (C) 1988 by George D. Drapeau                ==
  ==                  All Rights Reserved                         ==
  ==================================================================
  ==================================================================
*/
/* $Log:	eventhandler.c,v $
 * Revision 1.4  89/08/06  18:04:45  drapeau
 * Cleaned up the code considerably for the first X11 release.
 * Also, added an iconPixmap for the topLevelShell so a nice
 * icon shows up when the Music Window is iconified.
 * Made X Toolkit argument retrieval more consistent with the rest of the
 * code in this release.
 * Worked around a bug in (XToolkit?  My code?) in retrieving background &
 * foreground resources.  This only occurs in monochrome mode:  the workaround
 * is that if the foreground and background colors are seen as equal, then
 * XMusic will flip the foreground color to become the opposite of the
 * background color.
 *  */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <stdio.h>
#include <ctype.h>
#define USEBITMAPS
#include "defs.h"
#include "externs.h"



/*############################################################################
  FUNCTION	DoXEventLoop
  DESCRIPTION:
  	This function is the heart of the X part of XMusic.  It initializes
	the X Toolkit, creates all the X resources needed by XMusic, and
	then calls "XtMainLoop" to let the toolkit handle event processing.
	This function will never exit normally; the ExitPgm function is
	responsible for exiting Music.
*/


DoXEventLoop()
{
  XGCValues		gcValues;
  Arg			topLevelArgs[2],argList[2];
  Window		topLevelWin,xmusicWin;
  static XtActionsRec	actionTable[] =
    {
      {"AcceptInput",AcceptInput}
    };

  topLevelShell = XtInitialize(Argv[0],"XMusic",NULL,0,&Argc,Argv); /* Initialize the X Toolkit */
  if (Argc >1)							    /* After all options have been removed, is there still... */
    {								    /* ...a filename for XMusic to work on? */
      strcpy(fileName,Argv[1]);					    /* Yes, get that filename */
      printf("File name is %s\n",fileName);
      ReadDiagram();						    /* Read in a pre-made diagram */
    }
  else								    /* Nope, no filename specified, create a default filename. */
    sprintf(fileName,"orch.xm");				    /* Create a new file name for this diagram */
  sprintf(orchFileName,"xmusic.orc");				    /* Create a new file name for the resulting Csound file */
  XtAppAddActions(XtWidgetToApplicationContext(topLevelShell),	    /* Add the 'AcceptInput()' function to the list of... */
		  actionTable,					    /* ...actions recognized by this application */
		  XtNumber(actionTable));
  CreateXMusicWorkSpace();					    /* Create the main workspace for XMusic diagrams. */
  XtRealizeWidget(topLevelShell);
  dpy		=	XtDisplay(topLevelShell);		    /* Get display and window of top level shell for... */
  topLevelWin	=	XtWindow(topLevelShell);		    /* ...later use in creating Pixmaps */
  musicWin	=	XtWindow(workSpaceWidget);		    /* Get the Window id of the workSpace widget */
  musicPixmap	=	XCreateBitmapFromData(dpy,topLevelWin,	    /* Create the Pixmap for XMusic's icon */
					      mIcon_bits,
					      mIcon_width,mIcon_height);
  XtSetArg(topLevelArgs[0],XtNiconPixmap,musicPixmap);		    /* Give this application an icon picture (Pixmap) */
  XtSetArg(topLevelArgs[1],XtNtitle,"Welcome To XMusic");	    /* Give this application a title */
  XtSetValues(topLevelShell,topLevelArgs,XtNumber(topLevelArgs));

  XtSetArg(argList[0],XtNforeground,&fg);			    /* Set up arguments to retrieve pixel values for... */
  XtSetArg(argList[1],XtNbackground,&bg);			    /* ...background and foreground */
  XtGetValues(workSpaceWidget,argList,XtNumber(argList));	    /* Get the current bg and fg settings */
  if (fg == bg)							    /* Take care of a bug I must have in getting fg & ... */
    fg = ~bg;							    /* ...bg resources (they come out equal on mono displays) */
  PrintToolList();						    /* Print list of tools read in from file */
  changesMade = 0;						    /* Set flag saying nothing has been done yet */
  CreateHelpWindow();						    /* Create the widgets to support the help window */
  CreateExitWindow();						    /* Create the Exit window. */
  CreateInfoWindow();						    /* Create the Info window. */
  CreateToolPalette();						    /* Create all widgets in the Tool Palette */
  XtPopup(toolPalette,XtGrabNone);				    /* Allow the Tool Palette to be seen */
  CreateToolPixmaps();						    /* Attach pictures of tools to the toolPalette Commands */
  GetToolPixmaps();						    /* Fill in the tools' Pixmap id's now that they're ready */
  
  if (!(musicFont=XLoadQueryFont(dpy, MUSICFONTNAME)))		    /* Open the MusicFont */
    printf("Unable to open font %s!\n", MUSICFONTNAME);
  gcValues.function = GXcopy;					    /* Set this drawing function for drawing Pixmaps */
  gcValues.foreground = fg;
  gcValues.background = bg;
  gcValues.font = musicFont->fid;
  gc = XtGetGC(topLevelShell,					    /* Get a GC for drawing Pixmaps later on */
	       (GCFunction|GCForeground|GCBackground|GCFont),
	       &gcValues);
  XtAddEventHandler(workSpaceWidget,(ExposureMask),False,	    /* Tell the workSpaceWidget how to redraw itself */
		    DrawMusicWin,NULL);
  XtAddEventHandler(workSpaceWidget,(ButtonPressMask),False,	    /* Tell the workspace widget about button presses */
		    HandleButtonEvent,NULL);
  DrawMusicWin();						    /* Draw the Music Window, in case a diagram was loaded in */
  XtMainLoop();							    /* Let the X Toolkit dispatch events */
}								    /* end function DoXEventLoop */
