/*==================================================================
  ==================================================================
  ==	XMusic -- An Interactive Interface to Csound		  ==
  ==       Copyright (C) 1988 by George D. Drapeau                ==
  ==                  All Rights Reserved                         ==
  ==================================================================
  ==================================================================
*/
/* $Log:	xresources.c,v $
 * Revision 1.4  89/08/06  18:17:56  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>
#define USEBITMAPS
#include "defs.h"
#include "externs.h"


CleanUp()
{
  XFreePixmap(dpy,musicPixmap);					    /* Free up the storage used by Music window's Pixmap */
  XFreePixmap(dpy,sineMap);
  XFreePixmap(dpy,envMap);
  XFreePixmap(dpy,sumMap);
  XFreePixmap(dpy,ampMap);
  XFreePixmap(dpy,iconMap);
  XFreePixmap(dpy,paramMap);
  XFreePixmap(dpy,eqMap);
  if (changesMade)
    SaveDiagram();
}


GetDiagramSize(x,y)						    /* This function determines the best window size to... */
     Dimension	*x,*y;						    /* ...contain the diagram the user is looking at */
{
  Tool	*tPtr;
  int	minX = TOOLSIZE, minY = TOOLSIZE, maxX = DEFWIDTH, maxY = DEFHEIGHT;
  
  if (!toolList)						    /* Is the tool list empty? */
    {
      *x = (Dimension)DEFWIDTH;					    /* Set default width and height for Music window */
      *y = (Dimension)DEFHEIGHT;
      return;
    }
  for (tPtr = toolList;  tPtr;  tPtr = tPtr->next)		    /* Go through the list of tools and look at each... */
    {								    /* ...of the tools' coordinates.  Determine the... */
      minX = min(minX, tPtr->x);				    /* ...upper leftmost tool and the lower rightmost. */
      minY = min(minY, tPtr->y);
      maxX = max(maxX, tPtr->x);
      maxY = max(maxY, tPtr->y);
    }
  if (minX < (TOOLSIZE/2))					    /* Are any tools above the top of the window? */
    {
      maxX += ((TOOLSIZE) - minX);				    /* Adjust window size to accomodate moving the tools */
      for (tPtr = toolList;  tPtr;  tPtr = tPtr->next)		    /* Yes, push all tools down so all tools are... */
	tPtr->x += ((TOOLSIZE) - minX);				    /* ...shown completely */
    }
  if (minY < (TOOLSIZE/2))					    /* Are any tools to the left of the window? */
    {
      maxY += ((TOOLSIZE) - minY);				    /* Adjust window size to accomodate moving the tools */
      for (tPtr = toolList;  tPtr;  tPtr = tPtr->next)		    /* Yes, push all tools to the right so all... */
	tPtr->y += ((TOOLSIZE) - minY);				    /* ...tools are shown completely */
     }
  if (maxX > 16383)						    /* Is the diagram too big for X to make a window for? */
    {								    /* Yes, report the error and correct for it */
      printf("Sorry, your diagram is too large to be drawn completely.\n");
      maxX = 16383;
    }
  if (maxY > 16383)
    {
      printf("Sorry, your diagram is too large to be drawn completely.\n");
      maxY = 16383;
    }
  *x = (Dimension)(maxX + (TOOLSIZE));				    /* Set width and height hints appropriately... */
  *y = (Dimension)(maxY + (TOOLSIZE));				    /* ...large to hold the diagram */
}								    /* end function GetDiagramSize */
