/* WaveTableList.c */
/*****************************************************************************/
/*                                                                           */
/*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
/*    Copyright (C) 1994  Thomas R. Lawrence                                 */
/*                                                                           */
/*    This program is free software; you can redistribute it and/or modify   */
/*    it under the terms of the GNU General Public License as published by   */
/*    the Free Software Foundation; either version 2 of the License, or      */
/*    (at your option) any later version.                                    */
/*                                                                           */
/*    This program is distributed in the hope that it will be useful,        */
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/*    GNU General Public License for more details.                           */
/*                                                                           */
/*    You should have received a copy of the GNU General Public License      */
/*    along with this program; if not, write to the Free Software            */
/*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
/*                                                                           */
/*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
/*                                                                           */
/*****************************************************************************/

#include "MiscInfo.h"
#include "Audit.h"
#include "Debug.h"
#include "Definitions.h"

#include "WaveTableList.h"
#include "StringList.h"
#include "Array.h"
#include "Memory.h"
#include "Alert.h"
#include "DataMunging.h"
#include "PcodeSystem.h"
#include "WaveTableObject.h"
#include "BufferedFileInput.h"
#include "BufferedFileOutput.h"
#include "Files.h"
#include "Scrap.h"

struct WaveTableListRec
	{
		StringListRec*					List;
		struct CodeCenterRec*		CodeCenter;
		struct MainWindowRec*		MainWindow;
		ArrayRec*								WaveTableArray;
		MyBoolean								WaveTableListChanged;
	};


#define MAGICSCRAPSTRING ("\xff\x00\x1f\xfe WaveTableObjectScrap")


/* create a new wave table list */
WaveTableListRec*		NewWaveTableList(struct MainWindowRec* MainWindow,
											struct CodeCenterRec* CodeCenter, WinType* ScreenID,
											OrdType XLoc, OrdType YLoc, OrdType Width, OrdType Height)
	{
		WaveTableListRec*	WaveTableList;

		WaveTableList = (WaveTableListRec*)AllocPtrCanFail(sizeof(WaveTableListRec),
			"WaveTableListRec");
		if (WaveTableList == NIL)
			{
			 FailurePoint1:
				return NIL;
			}
		WaveTableList->List = NewStringList(ScreenID,XLoc,YLoc,Width,Height,
			GetScreenFont(),9,StringListDontAllowMultipleSelection,"Wave Tables");
		if (WaveTableList->List == NIL)
			{
			 FailurePoint2:
				ReleasePtr((char*)WaveTableList);
				goto FailurePoint1;
			}
		WaveTableList->WaveTableArray = NewArray();
		if (WaveTableList->WaveTableArray == NIL)
			{
			 FailurePoint3:
				DisposeStringList(WaveTableList->List);
				goto FailurePoint2;
			}
		WaveTableList->CodeCenter = CodeCenter;
		WaveTableList->MainWindow = MainWindow;
		WaveTableList->WaveTableListChanged = False;
		return WaveTableList;
	}


/* delete the wave table list and all of the wave tables it contains */
void								DisposeWaveTableList(WaveTableListRec* WaveTableList)
	{
		long							Scan;
		long							Limit;

		CheckPtrExistence(WaveTableList);
		Limit = ArrayGetLength(WaveTableList->WaveTableArray);
		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				WaveTableObjectRec*	WaveTableTemp;

				WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				DisposeWaveTableObject(WaveTableTemp);
			}
		DisposeArray(WaveTableList->WaveTableArray);
		DisposeStringList(WaveTableList->List);
		ReleasePtr((char*)WaveTableList);
	}


/* change the location of the wave table list in the window */
void								SetWaveTableListLocation(WaveTableListRec* WaveTableList,
											OrdType XLoc, OrdType YLoc, OrdType Width, OrdType Height)
	{
		CheckPtrExistence(WaveTableList);
		SetStringListLoc(WaveTableList->List,XLoc,YLoc,Width,Height);
	}


/* redraw the wave table list */
void								WaveTableListRedraw(WaveTableListRec* WaveTableList)
	{
		CheckPtrExistence(WaveTableList);
		RedrawStringList(WaveTableList->List);
	}


/* see if the specified coordinates falls inside the wave table list rectangle */
MyBoolean						WaveTableListHitTest(WaveTableListRec* WaveTableList,
											OrdType XLoc, OrdType YLoc)
	{
		CheckPtrExistence(WaveTableList);
		return StringListHitTest(WaveTableList->List,XLoc,YLoc);
	}


/* handle a mouse down event for the wave table list */
void								WaveTableListDoMouseDown(WaveTableListRec* WaveTableList,
											OrdType XLoc, OrdType YLoc, ModifierFlags Modifiers)
	{
		CheckPtrExistence(WaveTableList);
		if (StringListMouseDown(WaveTableList->List,XLoc,YLoc,Modifiers))
			{
				/* if it returns true, then it was a double click */
				WaveTableListOpenSelection(WaveTableList);
			}
	}


/* called when the window becomes active */
void								WaveTableListBecomeActive(WaveTableListRec* WaveTableList)
	{
		CheckPtrExistence(WaveTableList);
		EnableStringList(WaveTableList->List);
	}


/* called when the window becomes inactive */
void								WaveTableListBecomeInactive(WaveTableListRec* WaveTableList)
	{
		CheckPtrExistence(WaveTableList);
		DisableStringList(WaveTableList->List);
	}


/* called when a selection is made in another list, so that this list */
/* is deselected */
void								WaveTableListDeselect(WaveTableListRec* WaveTableList)
	{
		CheckPtrExistence(WaveTableList);
		DeselectAllStringListElements(WaveTableList->List);
	}


/* check to see if there is a selection in this list */
MyBoolean						WaveTableListIsThereSelection(WaveTableListRec* WaveTableList)
	{
		CheckPtrExistence(WaveTableList);
		return (GetStringListHowManySelectedItems(WaveTableList->List) > 0);
	}


/* check to see if any of the wave tables contained in this list need to be saved */
MyBoolean						DoesWaveTableListNeedToBeSaved(WaveTableListRec* WaveTableList)
	{
		long							Scan;
		long							Limit;
		MyBoolean					Flag;

		CheckPtrExistence(WaveTableList);
		Flag = WaveTableList->WaveTableListChanged;
		Limit = ArrayGetLength(WaveTableList->WaveTableArray);
		for (Scan = 0; (Scan < Limit) && !Flag; Scan += 1)
			{
				WaveTableObjectRec*	WaveTableTemp;

				WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				if (HasWaveTableObjectBeenModified(WaveTableTemp))
					{
						Flag = True;
					}
			}
		return Flag;
	}


/* open an edit window for the selected wave table */
void								WaveTableListOpenSelection(WaveTableListRec* WaveTableList)
	{
		ArrayRec*					ListOfSelections;

		CheckPtrExistence(WaveTableList);
		ListOfSelections = GetListOfSelectedItems(WaveTableList->List);
		if (ListOfSelections != NIL)
			{
				long							Scan;
				long							Limit;

				Limit = ArrayGetLength(ListOfSelections);
				for (Scan = 0; Scan < Limit; Scan += 1)
					{
						WaveTableObjectRec*		WaveTableTemp;

						WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(ListOfSelections,Scan);
						WaveTableObjectOpenWindow(WaveTableTemp);
					}
				DisposeArray(ListOfSelections);
			}
	}


/* create a new wave table and open a window for it */
void								WaveTableListNewWaveTable(WaveTableListRec* WaveTableList)
	{
		WaveTableObjectRec*	WaveTable;

		CheckPtrExistence(WaveTableList);
		/* create the object */
		WaveTable = NewWaveTableObject(WaveTableList->CodeCenter,
			WaveTableList->MainWindow,WaveTableList);
		if (WaveTable == NIL)
			{
			 FailurePoint1:
				AlertHalt("There is not enough memory available to "
					"create a new wave table.",NIL);
				return;
			}
		/* add it to the string list */
		if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTable,True))
			{
			 FailurePoint2:
				DisposeWaveTableObject(WaveTable);
				goto FailurePoint1;
			}
		MainWindowDeselectAllOtherStringLists(WaveTableList->MainWindow,WaveTableList);
		SelectStringListElement(WaveTableList->List,WaveTable);
		MakeStringListSelectionVisible(WaveTableList->List);
		/* add it to the array */
		if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTable))
			{
			 FailurePoint3:
				RemoveStringListElement(WaveTableList->List,WaveTable,True);
				goto FailurePoint2;
			}
		/* update our internal flags */
		WaveTableList->WaveTableListChanged = True;
		/* change the name in the list */
		WaveTableListWaveTableNameChanged(WaveTableList,WaveTable);
		/* show the window */
		WaveTableObjectOpenWindow(WaveTable);
	}


/* delete the selected wave table */
void								WaveTableListDeleteSelection(WaveTableListRec* WaveTableList)
	{
		ArrayRec*					ListOfSelections;

		CheckPtrExistence(WaveTableList);
		ListOfSelections = GetListOfSelectedItems(WaveTableList->List);
		if (ListOfSelections != NIL)
			{
				long								Scan;
				long								Limit;

				Limit = ArrayGetLength(ListOfSelections);
				for (Scan = 0; Scan < Limit; Scan += 1)
					{
						WaveTableObjectRec*	OneToZap;

						OneToZap = (WaveTableObjectRec*)ArrayGetElement(ListOfSelections,Scan);
						WaveTableListDeleteWaveTable(WaveTableList,OneToZap);
					}
				DisposeArray(ListOfSelections);
			}
	}


/* delete the explicitly specified wave table */
void								WaveTableListDeleteWaveTable(WaveTableListRec* WaveTableList,
											struct WaveTableObjectRec* TheWaveTable)
	{
		long								Scan;
		long								Limit;

		CheckPtrExistence(WaveTableList);
		MainWindowClearInstrObjects(WaveTableList->MainWindow);
		Limit = ArrayGetLength(WaveTableList->WaveTableArray);
		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				WaveTableObjectRec*		WaveTableTemp;

				WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				if (TheWaveTable == WaveTableTemp)
					{
						FileSpec*					BackupFileWhere;
						FileType*					BackupFile;
						MyBoolean					Success = False;

						BackupFileWhere = NewTempFileSpec(CODE4BYTES('?','?','?','?'),
							CODE4BYTES('?','?','?','?'));
						if (BackupFileWhere != NIL)
							{
								if (OpenFile(BackupFileWhere,&BackupFile,eReadAndWrite))
									{
										BufferedOutputRec*	Output;

										Output = NewBufferedOutput(BackupFile);
										if (Output != NIL)
											{
												if (WriteBufferedOutput(Output,sizeof(MAGICSCRAPSTRING),
													MAGICSCRAPSTRING))
													{
														if (WaveTableObjectWriteDataOut(TheWaveTable,Output)
															== eFileLoadNoError)
															{
																Success = True;
															}
													}
												if (!EndBufferedOutput(Output))
													{
														Success = False;
													}
											}
										 else
											{
												CloseFile(BackupFile);
											}
									}
								 else
									{
										DeleteFile(BackupFileWhere);
										DisposeFileSpec(BackupFileWhere);
									}
							}
						if (Success)
							{
								MainWindowNewDeleteUndoInfo(WaveTableList->MainWindow,BackupFileWhere,
									BackupFile);
								DisposeWaveTableObject(WaveTableTemp);
								RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
								ArrayDeleteElement(WaveTableList->WaveTableArray,Scan);
								WaveTableList->WaveTableListChanged = True;
							}
						 else
							{
								YesNoCancelType		Decision;

								Decision = AskYesNoCancel("Unable to save undo information for object.  "
									"Delete object anyway?",NIL,"Delete","Cancel",NIL/*nothirdbutton*/);
								if (Decision == eYes)
									{
										DisposeWaveTableObject(WaveTableTemp);
										RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
										ArrayDeleteElement(WaveTableList->WaveTableArray,Scan);
										WaveTableList->WaveTableListChanged = True;
									}
							}
						return;
					}
			}
		EXECUTE(PRERR(AllowResume,"WaveTableListDeleteWaveTable:  couldn't find object"));
	}


/* the name of a wave table has changed, so the name in the scrolling */
/* list must also be changed */
void								WaveTableListWaveTableNameChanged(WaveTableListRec* WaveTableList,
											struct WaveTableObjectRec* TheWaveTable)
	{
		char*							WaveTableName;

		CheckPtrExistence(WaveTableList);
		CheckPtrExistence(TheWaveTable);
		ERROR(ArrayFindElement(WaveTableList->WaveTableArray,TheWaveTable) < 0,
			PRERR(ForceAbort,"WaveTableListWaveTableNameChanged:  unknown wave table"));
		WaveTableName = WaveTableObjectGetNameCopy(TheWaveTable);
		if (WaveTableName != NIL)
			{
				char*							WaveTableNameNullTerminated;

				WaveTableNameNullTerminated = BlockToStringCopy(WaveTableName);
				if (WaveTableNameNullTerminated != NIL)
					{
						ChangeStringListElementName(WaveTableList->List,
							WaveTableNameNullTerminated,TheWaveTable);
						ReleasePtr(WaveTableNameNullTerminated);
					}
				ReleasePtr(WaveTableName);
			}
	}


/* look for a specified wave table.  returns NIL if not found.  the name is NOT null */
/* terminated */
struct WaveTableObjectRec*	WaveTableListLookupNamedWaveTable(
											WaveTableListRec* WaveTableList, char* Name)
	{
		long							Scan;
		long							Limit;

		CheckPtrExistence(WaveTableList);
		CheckPtrExistence(Name);
		Limit = ArrayGetLength(WaveTableList->WaveTableArray);
		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				WaveTableObjectRec*		WaveTable;
				char*									NameCopy;

				WaveTable = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				NameCopy = WaveTableObjectGetNameCopy(WaveTable);
				if (NameCopy != NIL)
					{
						if (PtrSize(Name) == PtrSize(NameCopy))
							{
								if (MemEqu(Name,NameCopy,PtrSize(Name)))
									{
										ReleasePtr(NameCopy);
										return WaveTable;
									}
							}
						ReleasePtr(NameCopy);
					}
			}
		return NIL;
	}


/* the document's name changed, so the windows need to be updated */
void								WaveTableListGlobalNameChange(WaveTableListRec* WaveTableList,
											char* NewFilename)
	{
		long							Scan;
		long							Limit;

		CheckPtrExistence(WaveTableList);
		Limit = ArrayGetLength(WaveTableList->WaveTableArray);
		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				WaveTableObjectRec*		WaveTable;

				WaveTable = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				WaveTableObjectGlobalNameChange(WaveTable,NewFilename);
			}
	}


/* use the provided data to open a new wave table with the specified attributes. */
/* this is used when opening an algorithmic wave table as a data wave table. */
/* RawData MUST be valid. */
WaveTableObjectRec*	WaveTableListCopyRawWaveTableAndOpen(WaveTableListRec* WaveTableList,
											char* RawData, NumBitsType NumBits, long NumTables,
											long FramesPerTable)
	{
		WaveTableObjectRec*	WaveTable;

		CheckPtrExistence(WaveTableList);
		CheckPtrExistence(RawData);
		/* create the object */
		WaveTable = NewWaveTableObjectFromData(WaveTableList->CodeCenter,
			WaveTableList->MainWindow,WaveTableList,RawData,NumBits,NumTables,FramesPerTable);
		if (WaveTable == NIL)
			{
			 FailurePoint1:
				AlertHalt("There is not enough memory available to create a new wave table.",NIL);
				return NIL;
			}
		/* add it to the string list */
		if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTable,True))
			{
			 FailurePoint2:
				DisposeWaveTableObject(WaveTable);
				goto FailurePoint1;
			}
		MainWindowDeselectAllOtherStringLists(WaveTableList->MainWindow,WaveTableList);
		SelectStringListElement(WaveTableList->List,WaveTable);
		MakeStringListSelectionVisible(WaveTableList->List);
		/* add it to the array */
		if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTable))
			{
			 FailurePoint3:
				RemoveStringListElement(WaveTableList->List,WaveTable,True);
				goto FailurePoint2;
			}
		/* update our internal flags */
		WaveTableList->WaveTableListChanged = True;
		/* change the name in the list */
		WaveTableListWaveTableNameChanged(WaveTableList,WaveTable);
		/* show the window */
		WaveTableObjectOpenWindow(WaveTable);
		return WaveTable;
	}


/* get the frame count for the named wave table */
SampleErrors				WaveTableListGetWaveTableFrameCount(WaveTableListRec* WaveTableList,
											char* WaveName, long* FrameCountOut)
	{
		WaveTableObjectRec*	WaveTable;

		CheckPtrExistence(WaveTableList);
		ERROR(FrameCountOut == NIL,PRERR(ForceAbort,
			"WaveTableListGetWaveTableFrameCount:  frame count out is NIL"));
		WaveTable = WaveTableListLookupNamedWaveTable(WaveTableList,WaveName);
		if (WaveTable == NIL)
			{
				return eEvalSampleUndefined;
			}
		*FrameCountOut = WaveTableObjectEntriesPerTable(WaveTable);
		return eEvalSampleNoError;
	}


/* get the table count for the named wave table */
SampleErrors				WaveTableListGetWaveTableTableCount(WaveTableListRec* WaveTableList,
											char* WaveName, long* TableCountOut)
	{
		WaveTableObjectRec*	WaveTable;

		CheckPtrExistence(WaveTableList);
		ERROR(TableCountOut == NIL,PRERR(ForceAbort,
			"WaveTableListGetWaveTableTableCount:  table count out is NIL"));
		WaveTable = WaveTableListLookupNamedWaveTable(WaveTableList,WaveName);
		if (WaveTable == NIL)
			{
				return eEvalSampleUndefined;
			}
		*TableCountOut = WaveTableObjectGetNumTables(WaveTable);
		return eEvalSampleNoError;
	}


/* get the table array for the named wave table */
SampleErrors				WaveTableListGetWaveTableArray(WaveTableListRec* WaveTableList,
											char* WaveName, largefixedsigned** TableOut)
	{
		WaveTableObjectRec*	WaveTable;

		CheckPtrExistence(WaveTableList);
		ERROR(TableOut == NIL,PRERR(ForceAbort,
			"WaveTableListGetWaveTableArray:  table out is NIL"));
		WaveTable = WaveTableListLookupNamedWaveTable(WaveTableList,WaveName);
		if (WaveTable == NIL)
			{
				return eEvalSampleUndefined;
			}
		*TableOut = WaveTableObjectGetFixed(WaveTable);
		return eEvalSampleNoError;
	}


/*   4-byte little endian number of wave table objects (positive 2's complement) */
/*   n-bytes data for the wave table objects */


/* read wave table objects from a file.  returns True if completely successful. */
FileLoadingErrors		WaveTableListReadData(WaveTableListRec* WaveTableList,
											struct BufferedInputRec* Input)
	{
		signed long				ObjectCount;
		long							Scan;

		CheckPtrExistence(WaveTableList);
		CheckPtrExistence(Input);

		/*   4-byte little endian number of objects (positive 2's complement) */
		if (!ReadBufferedSignedLongLittleEndian(Input,&ObjectCount))
			{
				return eFileLoadDiskError;
			}
		if (ObjectCount < 0)
			{
				return eFileLoadBadFormat;
			}

		/* load the objects */
		for (Scan = 0; Scan < ObjectCount; Scan += 1)
			{
				WaveTableObjectRec*	WaveTableTemp EXECUTE(= (WaveTableObjectRec*)0x81818181);
				FileLoadingErrors		Error;

				/* load the object */
				Error = WaveTableObjectNewFromFile(&WaveTableTemp,Input,
					WaveTableList->CodeCenter,WaveTableList->MainWindow,WaveTableList);
				if (Error != eFileLoadNoError)
					{
					 FailurePoint1:
						return Error;
					}
				/* add it to the string list */
				if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTableTemp,True))
					{
					 FailurePoint2:
						DisposeWaveTableObject(WaveTableTemp);
						Error = eFileLoadOutOfMemory;
						goto FailurePoint1;
					}
				/* add it to the array */
				if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTableTemp))
					{
					 FailurePoint3:
						RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
						goto FailurePoint2;
					}
				/* change the name in the list */
				WaveTableListWaveTableNameChanged(WaveTableList,WaveTableTemp);
			}

		return eFileLoadNoError;
	}


/* write wave table objects to a file.  returns True if completely successful. */
FileLoadingErrors		WaveTableListWriteData(WaveTableListRec* WaveTableList,
											struct BufferedOutputRec* Output)
	{
		long							NumberOfObjects;
		long							Scan;

		CheckPtrExistence(Output);
		CheckPtrExistence(WaveTableList);

		/*   4-byte little endian number of objects (positive 2's complement) */
		NumberOfObjects = ArrayGetLength(WaveTableList->WaveTableArray);
		if (!WriteBufferedSignedLongLittleEndian(Output,NumberOfObjects))
			{
				return eFileLoadDiskError;
			}

		/* write out the objects */
		for (Scan = 0; Scan < NumberOfObjects; Scan += 1)
			{
				WaveTableObjectRec*		WaveTable;
				FileLoadingErrors			Error;

				/* get the object */
				WaveTable = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				/* write it out */
				Error = WaveTableObjectWriteDataOut(WaveTable,Output);
				/* handle errors */
				if (Error != eFileLoadNoError)
					{
						return Error;
					}
			}

		return eFileLoadNoError;
	}


/* after a file has been saved, this is called to mark all objects as not modified. */
void								WaveTableListMarkAllObjectsSaved(WaveTableListRec* WaveTableList)
	{
		long							Scan;
		long							Limit;

		CheckPtrExistence(WaveTableList);
		Limit = ArrayGetLength(WaveTableList->WaveTableArray);
		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				WaveTableObjectRec*		WaveTable;

				WaveTable = (WaveTableObjectRec*)ArrayGetElement(
					WaveTableList->WaveTableArray,Scan);
				WaveTableObjectMarkAsSaved(WaveTable);
			}
		WaveTableList->WaveTableListChanged = False;
	}


/* copy the selected object in the list to the clipboard.  return False if failed. */
MyBoolean						WaveTableListCopyObject(WaveTableListRec* WaveTableList)
	{
		ArrayRec*							ListOfSelections;
		MyBoolean							TotalSuccessFlag = False;

		CheckPtrExistence(WaveTableList);
		ListOfSelections = GetListOfSelectedItems(WaveTableList->List);
		if (ListOfSelections != NIL)
			{
				if (ArrayGetLength(ListOfSelections) >= 1)
					{
						WaveTableObjectRec*	WaveTableTemp;
						FileSpec*						TempFileLocation;

						WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(ListOfSelections,0);
						/* open the temporary file */
						TempFileLocation = NewTempFileSpec(CODE4BYTES('\?','\?','\?','\?'),
							CODE4BYTES('\?','\?','\?','\?'));
						if (TempFileLocation != NIL)
							{
								FileType*							FileDescriptor;

								if (OpenFile(TempFileLocation,&FileDescriptor,eReadAndWrite))
									{
										BufferedOutputRec*		BufferedFile;

										BufferedFile = NewBufferedOutput(FileDescriptor);
										if (BufferedFile != NIL)
											{
												MyBoolean							WriteSucceeded = False;

												if (WriteBufferedOutput(BufferedFile,sizeof(MAGICSCRAPSTRING),
													MAGICSCRAPSTRING))
													{
														if (WaveTableObjectWriteDataOut(WaveTableTemp,BufferedFile)
															== eFileLoadNoError)
															{
																WriteSucceeded = True;
															}
													}
												if (EndBufferedOutput(BufferedFile) && WriteSucceeded)
													{
														char*							Buffer;
														long							NumberOfBytes;

														NumberOfBytes = GetFileLength(FileDescriptor);
														Buffer = AllocPtrCanFail(NumberOfBytes,
															"WaveTableListCopyObject:  scrap buffer");
														if (Buffer != NIL)
															{
																if (SetFilePosition(FileDescriptor,0))
																	{
																		if (0 == ReadFromFile(FileDescriptor,
																			Buffer,NumberOfBytes))
																			{
																				if (SetScrapToThis(Buffer))
																					{
																						TotalSuccessFlag = True;
																					}
																			}
																	}
																ReleasePtr(Buffer);
															}
													}
											}
										CloseFile(FileDescriptor);
									}
								DeleteFile(TempFileLocation);
								DisposeFileSpec(TempFileLocation);
							}
					}
				DisposeArray(ListOfSelections);
			}
		return TotalSuccessFlag;
	}


/* try to paste the clipboard in as a wave table object.  returns False if */
/* it failed or the clipboard did not contain a wave table object. */
MyBoolean						WaveTableListPasteObject(WaveTableListRec* WaveTableList)
	{
		MyBoolean					TotalSuccessFlag = False;
		char*							Scrap;

		CheckPtrExistence(WaveTableList);
		Scrap = GetCopyOfScrap();
		if (Scrap != NIL)
			{
				FileSpec*					TempFileLocation;

				TempFileLocation = NewTempFileSpec(CODE4BYTES('\?','\?','\?','\?'),
					CODE4BYTES('\?','\?','\?','\?'));
				if (TempFileLocation != NIL)
					{
						FileType*							FileDescriptor;

						if (OpenFile(TempFileLocation,&FileDescriptor,eReadAndWrite))
							{
								BufferedOutputRec*		BufferedFile;

								BufferedFile = NewBufferedOutput(FileDescriptor);
								if (BufferedFile != NIL)
									{
										MyBoolean							WriteSucceeded = False;

										if (WriteBufferedOutput(BufferedFile,PtrSize(Scrap),Scrap))
											{
												WriteSucceeded = True;
											}
										if (EndBufferedOutput(BufferedFile) && WriteSucceeded)
											{
												TotalSuccessFlag = WaveTableListPasteFromFile(
													WaveTableList,FileDescriptor);
											}
									}
								CloseFile(FileDescriptor);
							}
						DeleteFile(TempFileLocation);
						DisposeFileSpec(TempFileLocation);
					}
				ReleasePtr(Scrap);
			}
		return TotalSuccessFlag;
	}


/* try to paste the wave table object in from the file */
MyBoolean						WaveTableListPasteFromFile(WaveTableListRec* WaveTableList,
											struct FileType* File)
	{
		MyBoolean					TotalSuccessFlag = False;

		CheckPtrExistence(WaveTableList);
		if (SetFilePosition(File,0))
			{
				BufferedInputRec*	InputFile;

				InputFile = NewBufferedInput(File);
				if (InputFile != NIL)
					{
						char							HeaderTest[sizeof(MAGICSCRAPSTRING)];

						if (ReadBufferedInput(InputFile,sizeof(MAGICSCRAPSTRING),HeaderTest))
							{
								if (MemEqu(MAGICSCRAPSTRING,HeaderTest,sizeof(MAGICSCRAPSTRING)))
									{
										WaveTableObjectRec*		WaveTableTemp EXECUTE(= (WaveTableObjectRec*)0x81818181);

										if (eFileLoadNoError == WaveTableObjectNewFromFile(&WaveTableTemp,InputFile,
											WaveTableList->CodeCenter,WaveTableList->MainWindow,WaveTableList))
											{
												CheckPtrExistence(WaveTableTemp);
												/* add it to the scrolling object list */
												if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTableTemp,True))
													{
													 FailurePoint:
														DisposeWaveTableObject(WaveTableTemp);
													}
												 else
													{
														MainWindowDeselectAllOtherStringLists(WaveTableList->MainWindow,WaveTableList);
														SelectStringListElement(WaveTableList->List,WaveTableTemp);
														MakeStringListSelectionVisible(WaveTableList->List);
														/* add it to the array */
														if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTableTemp))
															{
																RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
																goto FailurePoint;
															}
														 else
															{
																/* change the name in the list */
																WaveTableListWaveTableNameChanged(WaveTableList,WaveTableTemp);
																TotalSuccessFlag = True;
																WaveTableList->WaveTableListChanged = True;
															}
													}
											}
									}
							}
						EndBufferedInput(InputFile);
					}
			}
		return TotalSuccessFlag;
	}


/* find out how many wave tables there are in this list */
long								WaveTableListHowMany(WaveTableListRec* WaveTableList)
	{
		CheckPtrExistence(WaveTableList);
		return ArrayGetLength(WaveTableList->WaveTableArray);
	}


/* get an indexed wave tables from the list */
struct WaveTableObjectRec*	WaveTableListGetIndexedWaveTable(
											WaveTableListRec* WaveTableList, long Index)
	{
		WaveTableObjectRec*	WaveTable;

		CheckPtrExistence(WaveTableList);
		ERROR((Index < 0) || (Index >= WaveTableListHowMany(WaveTableList)),PRERR(ForceAbort,
			"WaveTableListGetIndexedWaveTable:  index out of range"));
		WaveTable = (WaveTableObjectRec*)ArrayGetElement(WaveTableList->WaveTableArray,Index);
		CheckPtrExistence(WaveTable);
		return WaveTable;
	}
