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

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



SelectTool(event)
     XButtonPressedEvent	*event;
{
  int	whichTool;

  whichTool = (event->y+2) /64;
  return(whichTool);
}								    /* end function SelectTool */




void MoveTool()
{
  Window	rootReturn,childReturn,subW;
  XEvent	event;
  Tool		*tool, *GetToolFromCoords();
  int		x,y;
  int		rootX,rootY;
  unsigned int	maskReturn;
  Bool		returnVal;

  returnVal = XQueryPointer(dpy,musicWin,
			    &rootReturn,&childReturn,
			    &rootX,&rootY,
			    &x,&y,
			    &maskReturn);
  if ((tool = GetToolFromCoords(x,y)) == NULL)
      {
	printf("Not pointing to any tool.\n");
	return;
      }
  XGrabPointer(dpy,musicWin,False,ButtonPressMask,
	       GrabModeAsync,GrabModeAsync,
	       None,tCursor,CurrentTime);
  XNextEvent(dpy,&event);
  returnVal = XQueryPointer(dpy,musicWin,
			    &rootReturn,&childReturn,
			    &rootX,&rootY,
			    &x,&y,
			    &maskReturn);
  tool->x = x;
  tool->y = y;
  XUngrabPointer(dpy,CurrentTime);
  DrawMusicWin();
}								    /* end function MoveTool */



void RemoveTool(widget,clientData,callData)
     Widget	widget;
     char	*clientData,*callData;
{
  Window	rootReturn,childReturn;
  XEvent	event;
  Tool		*tool, *GetToolFromCoords();
  int		x,y;
  int		rootX,rootY;
  unsigned int	maskReturn;
  Bool		returnVal;

  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,			    /* Determine where the mouse was clicked */
			    &rootReturn,&childReturn,
			    &rootX,&rootY,
			    &x,&y,
			    &maskReturn);
  XUngrabPointer(dpy,CurrentTime);				    /* Release exclusive hold of the mouse */
  if ((tool = GetToolFromCoords(x,y)) == NULL)			    /* Was a tool pointed to? */
      {
	printf("Not pointing to any tool.\n");			    /* Nope, print an error message and exit this function */
	return;
      }
  DeleteToolFromList(tool);					    /* Yep, delete the tool pointed to */
  DrawMusicWin();						    /* Refresh the diagram to reflect the deleted tool */
}								    /* end function RemoveTool */



Tool	*GetToolFromCoords(x,y)
     int	x,y;
{
  Tool	*tPtr;

  for (tPtr = toolList;  tPtr;  tPtr = tPtr->next)		    /* Examine every tool in the list */
    {
      if ((x > (tPtr->x - (TOOLSIZE/2))) &&			    /* Are the coordinates passed in inside the... */
	  (x < (tPtr->x + (TOOLSIZE/2))) &&			    /* ...area that would be occupied by this... */
	  (y > (tPtr->y - (TOOLSIZE/2))) &&			    /* ...tool? */
	  (y < (tPtr->y + (TOOLSIZE/2))))			    /* If so, return a pointer to the tool in which... */
	return(tPtr);						    /* ...the coordinates were found. */
    }
  return(NULL);							    /* No tool is close to the given coordinates */
}								    /* end function GetToolFromCoords */



Tool	*MakeParamTool(x,y)
     int	x,y;
{
  Tool		*tPtr, *AddToolToList();
  char		*malloc(), tmpName[32];
  int		sLen;
  
  sLen = strlen(inputStr);
  if (sLen > 0)							    /* Did the user actually enter a string name? */
    {
      tPtr = AddToolToList();					    /* Yes, make a new tool and return a pointer to it */
      sprintf(tmpName, "paramtool%d",tPtr->toolNum);		    /* Generate a new name for this tool */
      tPtr->name = malloc(strlen(tmpName));			    /* Allocate space to hold the tool name */
      strcpy(tPtr->name, tmpName);				    /* Assign the new name to this tool */
      tPtr->fName = malloc(sLen+1);				    /* Allocate space to hold the Csound function name */
      strncpy(tPtr->fName, inputStr, sLen);			    /* Assign the function name to this tool */
      tPtr->fName[sLen] = '\0';					    /* Append null byte to string. */
      tPtr->x = x;
      tPtr->y = y;
      tPtr->type = ParamTool;
      return(tPtr);
    }								    /* end if */
  else
    return(NULL);						    /* No, return a null pointer */
}								    /* end function MakeParamTool */



Tool	*MakeEQTool(x,y)
     int	x,y;
{
  Tool		*tPtr, *AddToolToList();
  char		*malloc(), tmpName[32];
  int		sLen;
  Arg		arg;
  
  sLen = strlen(inputStr);
  if (sLen > 0)							    /* Did the user actually enter a string name? */
    {
      tPtr = AddToolToList();					    /* Yes, make a new tool and return a pointer to it */
      sprintf(tmpName, "eqtool%d",tPtr->toolNum);		    /* Generate a new name for this tool */
      tPtr->name = malloc(strlen(tmpName));			    /* Allocate space to hold the tool name */
      strcpy(tPtr->name, tmpName);				    /* Assign the new name to this tool */
      tPtr->fName = malloc(sLen+1);				    /* Allocate space to hold the Csound function name */
      strncpy(tPtr->fName, inputStr, sLen);			    /* Assign the function name to this tool */
      tPtr->fName[sLen] = '\0';					    /* Append null byte to string. */
      tPtr->x = x;
      tPtr->y = y;
      tPtr->type = EQTool;
      tPtr->map = eqMap;
      return(tPtr);
    }								    /* end if */
  else
    return(NULL);						    /* No, return a null pointer */
}								    /* end function MakeEQTool */
