/*==================================================================
  ==================================================================
  ==	XMusic -- An Interactive Interface to Csound		  ==
  ==       Copyright (C) 1988 by George D. Drapeau                ==
  ==                  All Rights Reserved                         ==
  ==================================================================
  ==================================================================
*/
/* $Log:	placetool.c,v $
 * Revision 1.4  89/08/06  18:14:23  drapeau
 * Cleaned up the code for the first X11 release.
 *  */

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <string.h>
#include "defs.h"
#include "externs.h"


PlaceTool(whichTool)
     int	whichTool;
{
  XEvent	event;
  Window	rootReturn,childReturn;
  int		rootX,rootY,x,y;
  unsigned int	maskReturn;
  Bool		returnVal;
  int		*xyArray;					    /* Holds the xy position of the tool being created */
  Tool		*tPtr, *AddToolToList(), *MakeParamTool(), *MakeEQTool();
  char		*malloc();
  Arg		arg;
  
  XGrabPointer(dpy,musicWin,False,ButtonPressMask,		    /* Allow input only on the music window now */
	       GrabModeAsync,GrabModeAsync,
	       None,mCursor,CurrentTime);
  XWindowEvent(dpy,musicWin,ButtonPressMask,&event);		    /* Wait until the user clicks in the music window */
  returnVal = XQueryPointer(dpy,musicWin,			    /* Find out where the user clicked the mouse */
			    &rootReturn,&childReturn,
			    &rootX,&rootY,
			    &x,&y,
			    &maskReturn);
  XUngrabPointer(dpy,CurrentTime);				    /* Release exclusive hold of the mouse */
  xyArray = (int *)malloc(2*sizeof(int));			    /* Allocate space for the xy coords of the new tool */
  switch (whichTool)
    {
    case ParamTool:
      xyArray[0] = x;
      xyArray[1] = y;
      CreateInputWindow("What Would You Like To Call This Parameter?",
			inputStr,
			ParamToolInput,(char *)xyArray);
      tPtr = NULL;
      break;
    case EQTool:
      xyArray[0] = x;
      xyArray[1] = y;
      CreateInputWindow("Enter an Equation, Please:",
			inputStr,
			EQToolInput,(char *)xyArray);
      tPtr = NULL;
      break;
    default:
      tPtr = AddToolToList();
      tPtr->x = x;
      tPtr->y = y;
      tPtr->type = whichTool;
      switch(whichTool)
	{
	case SineTool:
	  MakeSineTool(tPtr);					    /* Initialize default values for a SineTool */
	  break;
	case EnvTool:
	  MakeEnvTool(tPtr);					    /* Initialize default values for an EnvTool */
	  break;
	case AmpTool:
	  MakeAmpTool(tPtr);					    /* Initialize default values for an AmpTool */
	  break;
	case SumTool:
	  MakeSumTool(tPtr);					    /* Initialize default values for a SumTool */
	  break;
	case NoiseTool:
	  MakeNoiseTool(tPtr);					    /* Initialize default values for a NoiseTool */
	  break;
	}							    /* end inner switch whichTool... */
    }								    /* end outer switch whichTool.... */
  if (tPtr)
    DrawMusicWin();						    /* Refresh the Music Window */
}								    /* end function PlaceTool */



MakeSineTool(tPtr)
     Tool	*tPtr;
{
  char	*malloc(), tmpName[MAXSTRLEN];

  tPtr->map = sineMap;
  sprintf(tmpName, "sinetool%d",tPtr->toolNum);			    /* Generate a new name for this tool */
  tPtr->name = malloc(MAXSTRLEN);				    /* Allocate space to hold the tool name */
  strcpy(tPtr->name, tmpName);					    /* Assign the new name to this tool */
  sprintf(tmpName,"oscil");					    /* Setup default name for this tool type */
  tPtr->fName = malloc(MAXSTRLEN);				    /* Allocate space to hold the Csound function name */
  strcpy(tPtr->fName, tmpName);					    /* Assign the function name to this tool */
  tPtr->arg1 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg1,"1000");
  tPtr->arg2 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg2,"440");
  tPtr->arg3 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg3,"1");
  tPtr->arg4 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg4,"0");
}								    /* end function MakeSineTool */



MakeEnvTool(tPtr)
     Tool	*tPtr;
{
  char	*malloc(), tmpName[MAXSTRLEN];

  tPtr->map = envMap;
  sprintf(tmpName, "envtool%d",tPtr->toolNum);			    /* Generate a new name for this tool */
  tPtr->name = malloc(MAXSTRLEN);				    /* Allocate space to hold the tool name */
  strcpy(tPtr->name, tmpName);					    /* Assign the new name to this tool */
  sprintf(tmpName,"linen");					    /* Setup default name for this tool type */
  tPtr->fName = malloc(MAXSTRLEN);				    /* Allocate space to hold the Csound function name */
  strcpy(tPtr->fName, tmpName);					    /* Assign the function name to this tool */
  tPtr->arg1 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg1,"1000");
  tPtr->arg2 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg2,".1");
  tPtr->arg3 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg3,"1");
  tPtr->arg4 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg4,".5");
}								    /* end function MakeEnvTool */



MakeAmpTool(tPtr)
     Tool	*tPtr;
{
  char	*malloc(), tmpName[MAXSTRLEN];

  tPtr->map = ampMap;
  sprintf(tmpName, "amptool%d",tPtr->toolNum);			    /* Generate a new name for this tool */
  tPtr->name = malloc(MAXSTRLEN);				    /* Allocate space to hold the tool name */
  strcpy(tPtr->name, tmpName);					    /* Assign the new name to this tool */
  sprintf(tmpName,"gain");					    /* Setup default name for this tool type */
  tPtr->fName = malloc(MAXSTRLEN);				    /* Allocate space to hold the Csound function name */
  strcpy(tPtr->fName, tmpName);					    /* Assign the function name to this tool */
  tPtr->arg1 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg1,"100");
  tPtr->arg2 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg2,"100");
  tPtr->arg3 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg3,"10");
  tPtr->arg4 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg4,"0");
}								    /* end function MakeAmpTool */



MakeSumTool(tPtr)
     Tool	*tPtr;
{
  char	*malloc(), tmpName[MAXSTRLEN];

  tPtr->map = sumMap;
  sprintf(tmpName, "sumtool%d",tPtr->toolNum);			    /* Generate a new name for this tool */
  tPtr->name = malloc(MAXSTRLEN);				    /* Allocate space to hold the tool name */
  strcpy(tPtr->name, tmpName);					    /* Assign the new name to this tool */
  sprintf(tmpName,"+");						    /* Setup default name for this tool type */
  tPtr->fName = malloc(MAXSTRLEN);				    /* Allocate space to hold the Csound function name */
  strcpy(tPtr->fName, tmpName);					    /* Assign the function name to this tool */
}								    /* end function MakeSumTool */



MakeNoiseTool(tPtr)
     Tool	*tPtr;
{
  char	*malloc(), tmpName[MAXSTRLEN];

  tPtr->map = noiseMap;
  sprintf(tmpName, "noisetool%d",tPtr->toolNum);		    /* Generate a new name for this tool */
  tPtr->name = malloc(MAXSTRLEN);				    /* Allocate space to hold the tool name */
  strcpy(tPtr->name, tmpName);					    /* Assign the new name to this tool */
  sprintf(tmpName,"rand");					    /* Setup default name for this tool type */
  tPtr->fName = malloc(MAXSTRLEN);				    /* Allocate space to hold the Csound function name */
  strcpy(tPtr->fName, tmpName);					    /* Assign the function name to this tool */
  tPtr->arg1 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg1,"1000");
  tPtr->arg2 = malloc(MAXSTRLEN);
  strcpy(tPtr->arg2,".5");
}								    /* end function MakeNoiseTool */
