/*==================================================================
  ==================================================================
  ==	XMusic -- An Interactive Interface to Csound		  ==
  ==       Copyright (C) 1988 by George D. Drapeau                ==
  ==                  All Rights Reserved                         ==
  ==================================================================
  ==================================================================
*/
/* $Log:	draw.c,v $
 * Revision 1.4  89/08/06  18:00:36  drapeau
 * Changed a call of XCopyArea to XCopyPlane to take in to account creation
 * of tool Pixmaps by using "XCreateBitmapFromData" instead of the older
 * method of using "XCreatePixmapFromBitmapData".
 * Also, removed comments about the DrawMusicWin() algorithm, which no
 * longer saves a copy of the window to save future drawing time.  Now the
 * function simply redraws each tool and connection in the diagram, no
 * matter how much (or little) was exposed.
 *  */

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <string.h>

#define USEBITMAPS
#include "defs.h"
#include "externs.h"


/*############################################################################
  FUNCTION	DrawMusicWin
  DESCRIPTION:
  	This function is responsible for drawing the Music window.  This
	function calls other functions to do the actual screen drawing.   
*/


void DrawMusicWin(w,clientData,callData)
     Widget	w;
     char	*clientData;
     XEvent	*callData;
{
  Tool		*tPtr;
  Window	rootReturn;
  int		wInfoX,wInfoY,wInfoWidth,wInfoHeight;
  int		rInfoX,rInfoY,rInfoWidth,rInfoHeight;
  
  XClearWindow(dpy,musicWin);					    /* Clear the Music window before drawing */
  for (tPtr = toolList; tPtr; tPtr = tPtr->next)		    /* Go through the list of tools so far and... */
    {
      DrawOneTool(tPtr);					    /* ...draw them, one by one, in the Music Window */
    }
  for (tPtr = toolList; tPtr; tPtr = tPtr->next)		    /* Go through the list of tools so far and... */
    {
      DrawToolConnection(tPtr);					    /* ...connect them, one by one, in the Music Window */
    }
}								    /* end function DrawMusicWin */




DrawOneTool(tool)
     Tool	*tool;
{
  if (tool->type == ParamTool)
    DrawParamTool(tool);
  else
    XCopyPlane(dpy,(Drawable)tool->map,(Drawable)musicWin,gc,
	      0,0,TOOLSIZE,TOOLSIZE,
	      tool->x - (TOOLSIZE/2), tool->y - (TOOLSIZE/2),1);
}								    /* end function DrawOneTool */

DrawParamTool(tPtr)
     Tool	*tPtr;
{
  XDrawImageString(dpy,(Drawable)musicWin,gc,
		   tPtr->x - (strlen(tPtr->fName) * FontWidth(musicFont) / 2),
		   tPtr->y + FontHeight(musicFont),
		   tPtr->fName, strlen(tPtr->fName));
}								    /* end function DrawParamTool */


DrawToolConnection(tool)					    /* This function draws a line between this tool... */
     Tool	*tool;						    /* ...and the tools that serve as input to this one. */
{
  if (tool->type == SineTool)
    {
      DrawSineTool(tool);
      return;
    }
  if (tool->input1 != NULL)					    /* Is the first input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input1  */
	      tool->input1->x, tool->input1->y + (TOOLSIZE/2));

  if (tool->input2 != NULL)					    /* Is the second input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input2  */
	      tool->input2->x, tool->input2->y + (TOOLSIZE/2));
  if (tool->input3 != NULL)					    /* Is the third input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input3  */
	      tool->input3->x, tool->input3->y + (TOOLSIZE/2));
  if (tool->input4 != NULL)					    /* Is the fourth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input4  */
	      tool->input4->x, tool->input4->y + (TOOLSIZE/2));
  if (tool->input5 != NULL)					    /* Is the fifth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input5  */
	      tool->input5->x, tool->input5->y + (TOOLSIZE/2));
  if (tool->input6 != NULL)					    /* Is the sixth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input6  */
	      tool->input6->x, tool->input6->y + (TOOLSIZE/2));
  if (tool->input7 != NULL)					    /* Is the seventh input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input7  */
	      tool->input7->x, tool->input7->y + (TOOLSIZE/2));
  if (tool->input8 != NULL)					    /* Is the eighth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input8  */
	      tool->input8->x, tool->input8->y + (TOOLSIZE/2));
}								    /* end function DrawToolConnection */


DrawSineTool(tool)
     Tool	*tool;
{
  if (tool->input1 != NULL)					    /* Is the Amplitude input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x - (TOOLSIZE/4), tool->y - (TOOLSIZE/2),	    /* ...tool pointed to by the Amplitude input.  */
	      tool->input1->x, tool->input1->y + (TOOLSIZE/2));
  if (tool->input2 != NULL)					    /* Is the Frequency input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x + (TOOLSIZE/4), tool->y - (TOOLSIZE/2),	    /* ...tool pointed to by the Frequency input.  */
	      tool->input2->x, tool->input2->y + (TOOLSIZE/2));
  if (tool->input3 != NULL)					    /* Is the third input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input3.  */
	      tool->input3->x, tool->input3->y + (TOOLSIZE/2));
  if (tool->input4 != NULL)					    /* Is the fourth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input4.  */
	      tool->input4->x, tool->input4->y + (TOOLSIZE/2));
  if (tool->input5 != NULL)					    /* Is the fifth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input5.  */
	      tool->input5->x, tool->input5->y + (TOOLSIZE/2));
  if (tool->input6 != NULL)					    /* Is the sixth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input6.  */
	      tool->input6->x, tool->input6->y + (TOOLSIZE/2));
  if (tool->input7 != NULL)					    /* Is the seventh input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input7.  */
	      tool->input7->x, tool->input7->y + (TOOLSIZE/2));
  if (tool->input8 != NULL)					    /* Is the eighth input being used? */
    XDrawLine(dpy,(Drawable)musicWin,gc,			    /* Yes, draw a connection between this tool and the... */
	      tool->x, tool->y - (TOOLSIZE/2),			    /* ...tool pointed to by input8.  */
	      tool->input8->x, tool->input8->y + (TOOLSIZE/2));
}								    /* end function DrawToolConnection */
