home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / SampleWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  106.4 KB  |  3,450 lines  |  [TEXT/KAHL]

  1. /* SampleWindow.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "SampleWindow.h"
  31. #include "Memory.h"
  32. #include "MainWindowStuff.h"
  33. #include "CodeCenter.h"
  34. #include "SampleObject.h"
  35. #include "SampleView.h"
  36. #include "TextEdit.h"
  37. #include "WindowDispatcher.h"
  38. #include "Alert.h"
  39. #include "GrowIcon.h"
  40. #include "SampleTestPlay.h"
  41. #include "DataMunging.h"
  42. #include "Main.h"
  43. #include "Numbers.h"
  44. #include "PcodeStack.h"
  45. #include "PcodeSystem.h"
  46. #include "Scroll.h"
  47. #include "SampleList.h"
  48. #include "EditImages.h"
  49. #include "IconButton.h"
  50. #include "SampleStorageActual.h"
  51. #include "SampleStorageDisplay.h"
  52. #include "FindDialog.h"
  53. #include "GlobalWindowMenuList.h"
  54. #include "CompilerRoot.h"
  55. #include "FunctionCode.h"
  56. #include "ExportWAVSample.h"
  57. #include "ExportRAWSample.h"
  58. #include "ExportAIFFSample.h"
  59.  
  60.  
  61. #define OVERLINEHEIGHT (13)
  62.  
  63. #define ZOOMFACTOR (2) /* zoom doubles / halves the resolution */
  64.  
  65. #define WINDOWXSIZE (512 - 4 - 4)
  66. #define WINDOWYSIZE (342 - 20 - 20 - 4 - 4)
  67.  
  68. #define EXPRESSIONEDITX (-1)
  69. #define EXPRESSIONEDITWIDTH(WindowWidth) ((WindowWidth) + 2)
  70. #define EXPRESSIONEDITHEIGHT (70)
  71. #define EXPRESSIONEDITY(WindowHeight) ((WindowHeight) - EXPRESSIONEDITHEIGHT + 1)
  72.  
  73. #define SAMPLEEDITX (-1)
  74. #define SAMPLEEDITY (41)
  75. #define SAMPLEEDITWIDTH(WindowWidth) ((WindowWidth) + 2)
  76. #define SAMPLEEDITHEIGHT(WindowHeight) ((WindowHeight) - SAMPLEEDITY - 70\
  77.                     - EXPRESSIONEDITHEIGHT)
  78.  
  79. #define NAMEEDITX (80)
  80. #define NAMEEDITY (1)
  81. #define NAMEEDITWIDTH (80)
  82. #define NAMEEDITHEIGHT (19)
  83.  
  84. #define SAMPLINGRATEEDITX (80)
  85. #define SAMPLINGRATEEDITY (21)
  86. #define SAMPLINGRATEEDITWIDTH (80)
  87. #define SAMPLINGRATEEDITHEIGHT (19)
  88.  
  89. #define ORIGINEDITX (240)
  90. #define ORIGINEDITY (1)
  91. #define ORIGINEDITWIDTH (80)
  92. #define ORIGINEDITHEIGHT (19)
  93.  
  94. #define BASEFREQEDITX (240)
  95. #define BASEFREQEDITY (21)
  96. #define BASEFREQEDITWIDTH (80)
  97. #define BASEFREQEDITHEIGHT (19)
  98.  
  99. #define LOOPSTARTEDITX (400)
  100. #define LOOPSTARTEDITY (1)
  101. #define LOOPSTARTEDITWIDTH (80)
  102. #define LOOPSTARTEDITHEIGHT (19)
  103.  
  104. #define LOOPENDEDITX (400)
  105. #define LOOPENDEDITY (21)
  106. #define LOOPENDEDITWIDTH (80)
  107. #define LOOPENDEDITHEIGHT (19)
  108.  
  109. #define SELECTSTARTEDITX (160)
  110. #define SELECTSTARTEDITY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3\
  111.                     - EXPRESSIONEDITHEIGHT)
  112. #define SELECTSTARTEDITWIDTH (80)
  113. #define SELECTSTARTEDITHEIGHT (19)
  114.  
  115. #define SELECTENDEDITX (160)
  116. #define SELECTENDEDITY(WinHeight) ((WinHeight) - 19 - 19 - 2 - EXPRESSIONEDITHEIGHT)
  117. #define SELECTENDEDITWIDTH (80)
  118. #define SELECTENDEDITHEIGHT (19)
  119.  
  120. #define SCALINGEDITX (330)
  121. #define SCALINGEDITY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3 - EXPRESSIONEDITHEIGHT)
  122. #define SCALINGEDITWIDTH (80)
  123. #define SCALINGEDITHEIGHT (19)
  124.  
  125. #define MOUSEPOSEDITX (160)
  126. #define MOUSEPOSEDITY(WindowHeight) ((WindowHeight) - 19 - 1 - EXPRESSIONEDITHEIGHT)
  127. #define MOUSEPOSEDITWIDTH (80)
  128. #define MOUSEPOSEDITHEIGHT (19)
  129.  
  130. #define BITS8X (1)
  131. #define BITS8Y(WinHeight) ((WinHeight) - 33 - EXPRESSIONEDITHEIGHT)
  132.  
  133. #define BITS16X (BITS8X + 33)
  134. #define BITS16Y(WinHeight) (BITS8Y(WinHeight))
  135.  
  136. #define MONOX (1)
  137. #define MONOY(WinHeight) ((WinHeight) - 33 - 33 - 3 - EXPRESSIONEDITHEIGHT)
  138.  
  139. #define STEREOX (MONOX + 33)
  140. #define STEREOY(WinHeight) (MONOY(WinHeight))
  141.  
  142. #define PLAYX (410 + 2)
  143. #define PLAYY(WinHeight) ((WinHeight) - 33 - EXPRESSIONEDITHEIGHT)
  144.  
  145. #define ZOOMINX (410 + 2)
  146. #define ZOOMINY(WinHeight) ((WinHeight) - 33 - 33 - 3 - EXPRESSIONEDITHEIGHT)
  147.  
  148. #define ZOOMOUTX (ZOOMINX + 33)
  149. #define ZOOMOUTY(WinHeight) (ZOOMINY(WinHeight))
  150.  
  151. #define SAMPLENAMEX (3)
  152. #define SAMPLENAMEY (4)
  153.  
  154. #define SAMPLERATEX (3)
  155. #define SAMPLERATEY (23)
  156.  
  157. #define ORIGINX (170)
  158. #define ORIGINY (4)
  159.  
  160. #define BASEFREQX (170)
  161. #define BASEFREQY (23)
  162.  
  163. #define LOOPSTARTX (330)
  164. #define LOOPSTARTY (4)
  165.  
  166. #define LOOPENDX (330)
  167. #define LOOPENDY (23)
  168.  
  169. #define SELECTSTARTX (76)
  170. #define SELECTSTARTY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3 + 3\
  171.                     - EXPRESSIONEDITHEIGHT)
  172.  
  173. #define SELECTENDX (76)
  174. #define SELECTENDY(WinHeight) ((WinHeight) - 19 - 19 - 2 + 3 - EXPRESSIONEDITHEIGHT)
  175.  
  176. #define MOUSELOCX (76)
  177. #define MOUSELOCY(WinHeight) ((WinHeight) - 19 - 1 + 3 - EXPRESSIONEDITHEIGHT)
  178.  
  179. #define SCALINGX (250)
  180. #define SCALINGY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3 + 3 - EXPRESSIONEDITHEIGHT)
  181.  
  182.  
  183. struct SampleWindowRec
  184.     {
  185.         MainWindowRec*            MainWindow;
  186.         CodeCenterRec*            CodeCenter;
  187.         SampleObjectRec*        SampleObject;
  188.         SampleListRec*            SampleList;
  189.  
  190.         WinType*                        ScreenID;
  191.         SampleViewRec*            SampleView;
  192.         TextEditRec*                NameEdit;
  193.         TextEditRec*                SampleExpressionEdit;
  194.         TextEditRec*                SamplingRateEdit;
  195.         TextEditRec*                OriginEdit;
  196.         TextEditRec*                NaturalFrequencyEdit;
  197.         TextEditRec*                LoopStartEdit;
  198.         TextEditRec*                LoopEndEdit;
  199.         SampleLoops                    EditingWhichLoop;
  200.         TextEditRec*                SelectionStartEdit;
  201.         TextEditRec*                SelectionEndEdit;
  202.         TextEditRec*                ScalingEdit;
  203.         TextEditRec*                ActiveTextEdit;
  204.         TextEditRec*                MousePositionEdit;
  205.         ScrollRec*                    Scrollbar;
  206.         IconButtonRec*            Bits8Button;
  207.         IconButtonRec*            Bits16Button;
  208.         IconButtonRec*            MonoButton;
  209.         IconButtonRec*            StereoButton;
  210.         IconButtonRec*            ZoomInButton;
  211.         IconButtonRec*            ZoomOutButton;
  212.         IconButtonRec*            PlayButton;
  213.         GenericWindowRec*        MyGenericWindow; /* how the window event dispatcher knows us */
  214.         MenuItemType*                MyMenuItem;
  215.         long                                UndoOrigin;
  216.         long                                UndoLoopStart1;
  217.         long                                UndoLoopStart2;
  218.         long                                UndoLoopStart3;
  219.         long                                UndoLoopEnd1;
  220.         long                                UndoLoopEnd2;
  221.         long                                UndoLoopEnd3;
  222.     };
  223.  
  224.  
  225. /* create a new sample editing window.  the function gets ownership of SampleStorage */
  226. SampleWindowRec*        NewSampleWindow(struct MainWindowRec* MainWindow,
  227.                                             struct SampleObjectRec* SampleObject,
  228.                                             struct CodeCenterRec* CodeCenter,
  229.                                             struct SampleStorageActualRec* SampleStorage,
  230.                                             struct SampleListRec* SampleList,
  231.                                             OrdType WinX, OrdType WinY, OrdType WinWidth, OrdType WinHeight)
  232.     {
  233.         SampleWindowRec*    Window;
  234.         char*                            StringTemp;
  235.         NumChannelsType        NumChannels;
  236.         SampleStorageDisplayRec*    SampleCopy;
  237.         long                            Limit;
  238.         long                            Scan;
  239.  
  240.         /* deal with window placement */
  241.         if ((WinWidth < 100) || (WinHeight < 100) || ((eOptionKey & CheckModifiers()) != 0))
  242.             {
  243.                 WinX = 1 + WindowOtherEdgeWidths(eDocumentWindow);
  244.                 WinY = 1 + WindowTitleBarHeight(eDocumentWindow);
  245.                 WinWidth = WINDOWXSIZE;
  246.                 WinHeight = WINDOWYSIZE;
  247.             }
  248.         MakeWindowFitOnScreen(&WinX,&WinY,&WinWidth,&WinHeight);
  249.  
  250.         Window = (SampleWindowRec*)AllocPtrCanFail(
  251.             sizeof(SampleWindowRec),"SampleWindowRec");
  252.         if (Window == NIL)
  253.             {
  254.              FailurePoint1:
  255.                 AlertHalt("There is not enough memory available to "
  256.                     "display the sample editor.",NIL);
  257.                 return NIL;
  258.             }
  259.         Window->MainWindow = MainWindow;
  260.         Window->SampleObject = SampleObject;
  261.         Window->CodeCenter = CodeCenter;
  262.         Window->SampleList = SampleList;
  263.  
  264.         Window->ScreenID = MakeNewWindow(eDocumentWindow,eWindowClosable,
  265.             eWindowZoomable,eWindowResizable,WinX,WinY,WinWidth,WinHeight,
  266.             (void (*)(void*))&SampleWindowUpdator,Window);
  267.         if (Window->ScreenID == 0)
  268.             {
  269.              FailurePoint2:
  270.                 ReleasePtr((char*)Window);
  271.                 goto FailurePoint1;
  272.             }
  273.  
  274.         /* create sample editor */
  275.         NumChannels = GetSampleStorageActualNumChannels(SampleStorage);
  276.         SampleCopy = NewSampleStorageDisplayEmpty(
  277.             GetSampleStorageActualNumBits(SampleStorage),NumChannels);
  278.         if (SampleCopy == NIL)
  279.             {
  280.              FailurePoint3:
  281.                 KillWindow(Window->ScreenID);
  282.                 goto FailurePoint2;
  283.             }
  284.         Limit = GetSampleStorageActualNumFrames(SampleStorage);
  285.         if (!InsertSampleStorageDisplayArea(SampleCopy,0,Limit))
  286.             {
  287.                 goto FailurePoint3;
  288.             }
  289.         switch (NumChannels)
  290.             {
  291.                 default:
  292.                     EXECUTE(PRERR(ForceAbort,"NewSampleWindow:  bad num bits"));
  293.                     break;
  294.                 case eSampleMono:
  295.                     for (Scan = 0; Scan < Limit; Scan += 1)
  296.                         {
  297.                             SetSampleStorageDisplayValue(SampleCopy,Scan,eMonoChannel,
  298.                                 GetSampleStorageActualValue(SampleStorage,Scan,eMonoChannel));
  299.                         }
  300.                     break;
  301.                 case eSampleStereo:
  302.                     for (Scan = 0; Scan < Limit; Scan += 1)
  303.                         {
  304.                             SetSampleStorageDisplayValue(SampleCopy,Scan,eLeftChannel,
  305.                                 GetSampleStorageActualValue(SampleStorage,Scan,eLeftChannel));
  306.                             SetSampleStorageDisplayValue(SampleCopy,Scan,eRightChannel,
  307.                                 GetSampleStorageActualValue(SampleStorage,Scan,eRightChannel));
  308.                         }
  309.                     break;
  310.             }
  311.         Window->SampleView = NewSampleView(Window->ScreenID,SAMPLEEDITX,
  312.             SAMPLEEDITY + (3 * OVERLINEHEIGHT),SAMPLEEDITWIDTH(WinWidth),
  313.             SAMPLEEDITHEIGHT(WinHeight) - 15 - (3 * OVERLINEHEIGHT),SampleCopy);
  314.         if (Window->SampleView == NIL)
  315.             {
  316.                 /* this is not a true exception point because SampleCopy is only */
  317.                 /* valid & needs to be dumped if this call fails, otherwise no. */
  318.                 DisposeSampleStorageDisplay(SampleCopy);
  319.                 goto FailurePoint3;
  320.             }
  321.         SampleViewHasBeenSaved(Window->SampleView);
  322.  
  323.         /* create name edit */
  324.         Window->NameEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  325.             GetScreenFont(),9,NAMEEDITX,NAMEEDITY,NAMEEDITWIDTH,NAMEEDITHEIGHT);
  326.         if (Window->NameEdit == NIL)
  327.             {
  328.              FailurePoint4:
  329.                 DisposeSampleView(Window->SampleView);
  330.                 goto FailurePoint3;
  331.             }
  332.         /* install new text in name edit */
  333.         StringTemp = SampleObjectGetNameCopy(SampleObject);
  334.         if (StringTemp == NIL)
  335.             {
  336.              FailurePoint5:
  337.                 DisposeTextEdit(Window->NameEdit);
  338.                 goto FailurePoint4;
  339.             }
  340.         TextEditNewRawData(Window->NameEdit,StringTemp,"\x0a");
  341.         ReleasePtr(StringTemp);
  342.         TextEditHasBeenSaved(Window->NameEdit);
  343.  
  344.         /* create sampling rate edit */
  345.         Window->SamplingRateEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  346.             GetScreenFont(),9,SAMPLINGRATEEDITX,SAMPLINGRATEEDITY,
  347.             SAMPLINGRATEEDITWIDTH,SAMPLINGRATEEDITHEIGHT);
  348.         if (Window->SamplingRateEdit == NIL)
  349.             {
  350.              FailurePoint6:
  351.                 goto FailurePoint5;
  352.             }
  353.  
  354.         /* create origin edit */
  355.         Window->OriginEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  356.             GetScreenFont(),9,ORIGINEDITX,ORIGINEDITY,ORIGINEDITWIDTH,ORIGINEDITHEIGHT);
  357.         if (Window->OriginEdit == NIL)
  358.             {
  359.              FailurePoint7:
  360.                 DisposeTextEdit(Window->SamplingRateEdit);
  361.                 goto FailurePoint6;
  362.             }
  363.  
  364.         /* create base frequency edit */
  365.         Window->NaturalFrequencyEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  366.             GetScreenFont(),9,BASEFREQEDITX,BASEFREQEDITY,BASEFREQEDITWIDTH,
  367.             BASEFREQEDITHEIGHT);
  368.         if (Window->NaturalFrequencyEdit == NIL)
  369.             {
  370.              FailurePoint8:
  371.                 DisposeTextEdit(Window->OriginEdit);
  372.                 goto FailurePoint7;
  373.             }
  374.  
  375.         /* create loop start edit */
  376.         Window->LoopStartEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  377.             GetScreenFont(),9,LOOPSTARTEDITX,LOOPSTARTEDITY,LOOPSTARTEDITWIDTH,
  378.             LOOPSTARTEDITHEIGHT);
  379.         if (Window->LoopStartEdit == NIL)
  380.             {
  381.              FailurePoint9:
  382.                 DisposeTextEdit(Window->NaturalFrequencyEdit);
  383.                 goto FailurePoint8;
  384.             }
  385.  
  386.         /* create loop end edit */
  387.         Window->LoopEndEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  388.             GetScreenFont(),9,LOOPENDEDITX,LOOPENDEDITY,LOOPENDEDITWIDTH,LOOPENDEDITHEIGHT);
  389.         if (Window->LoopEndEdit == NIL)
  390.             {
  391.              FailurePoint10:
  392.                 DisposeTextEdit(Window->LoopStartEdit);
  393.                 goto FailurePoint9;
  394.             }
  395.  
  396.         Window->EditingWhichLoop = eSampleLoop1;
  397.  
  398.         /* create selection start edit */
  399.         Window->SelectionStartEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  400.             GetScreenFont(),9,SELECTSTARTEDITX,SELECTSTARTEDITY(WinHeight),
  401.             SELECTSTARTEDITWIDTH,SELECTSTARTEDITHEIGHT);
  402.         if (Window->SelectionStartEdit == NIL)
  403.             {
  404.              FailurePoint11:
  405.                 DisposeTextEdit(Window->LoopEndEdit);
  406.                 goto FailurePoint10;
  407.             }
  408.  
  409.         /* create selection end edit */
  410.         Window->SelectionEndEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  411.             GetScreenFont(),9,SELECTENDEDITX,SELECTENDEDITY(WinHeight),
  412.             SELECTENDEDITWIDTH,SELECTENDEDITHEIGHT);
  413.         if (Window->SelectionEndEdit == NIL)
  414.             {
  415.              FailurePoint12:
  416.                 DisposeTextEdit(Window->SelectionStartEdit);
  417.                 goto FailurePoint11;
  418.             }
  419.  
  420.         /* create scaling edit */
  421.         Window->ScalingEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  422.             GetScreenFont(),9,SCALINGEDITX,SCALINGEDITY(WinHeight),
  423.             SCALINGEDITWIDTH,SCALINGEDITHEIGHT);
  424.         if (Window->ScalingEdit == NIL)
  425.             {
  426.              FailurePoint13:
  427.                 DisposeTextEdit(Window->SelectionEndEdit);
  428.                 goto FailurePoint12;
  429.             }
  430.  
  431.         /* create place to show mouse position */
  432.         Window->MousePositionEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  433.             GetScreenFont(),9,MOUSEPOSEDITX,MOUSEPOSEDITY(WinHeight),
  434.             MOUSEPOSEDITWIDTH,MOUSEPOSEDITHEIGHT);
  435.         if (Window->MousePositionEdit == NIL)
  436.             {
  437.              FailurePoint14:
  438.                 DisposeTextEdit(Window->ScalingEdit);
  439.                 goto FailurePoint13;
  440.             }
  441.  
  442.         /* expression editor */
  443.         Window->SampleExpressionEdit = NewTextEdit(Window->ScreenID,
  444.             eTEVScrollBar | eTEHScrollBar,GetMonospacedFont(),9,EXPRESSIONEDITX,
  445.             EXPRESSIONEDITY(WinHeight),EXPRESSIONEDITWIDTH(WinWidth),
  446.             EXPRESSIONEDITHEIGHT);
  447.         if (Window->SampleExpressionEdit == NIL)
  448.             {
  449.              FailurePoint15:
  450.                 DisposeTextEdit(Window->MousePositionEdit);
  451.                 goto FailurePoint14;
  452.             }
  453.         StringTemp = SampleObjectGetFormulaCopy(SampleObject);
  454.         if (StringTemp == NIL)
  455.             {
  456.              FailurePoint16:
  457.                 DisposeTextEdit(Window->SampleExpressionEdit);
  458.                 goto FailurePoint15;
  459.             }
  460.         SetTextEditAutoIndent(Window->SampleExpressionEdit,True);
  461.         SetTextEditTabSize(Window->SampleExpressionEdit,MainWindowGetTabSize(MainWindow));
  462.         TextEditNewRawData(Window->SampleExpressionEdit,StringTemp,"\x0a");
  463.         ReleasePtr(StringTemp);
  464.  
  465.         Window->Scrollbar = NewScrollBar(Window->ScreenID,eHScrollBar,SAMPLEEDITX,
  466.             SAMPLEEDITY + SAMPLEEDITHEIGHT(WinHeight) - 16,SAMPLEEDITWIDTH(WinWidth));
  467.         if (Window->Scrollbar == NIL)
  468.             {
  469.              FailurePoint17:
  470.                 goto FailurePoint16;
  471.             }
  472.  
  473.         Window->Bits8Button = NewIconButtonPreparedBitmaps(Window->ScreenID,BITS8X,
  474.             BITS8Y(WinHeight),32,32,Bits8Unselected,Bits8MouseDown,
  475.             Bits8Selected,Bits8Selected,eIconRadioMode);
  476.         if (Window->Bits8Button == NIL)
  477.             {
  478.              FailurePoint18:
  479.                 DisposeScrollBar(Window->Scrollbar);
  480.                 goto FailurePoint17;
  481.             }
  482.  
  483.         Window->Bits16Button = NewIconButtonPreparedBitmaps(Window->ScreenID,BITS16X,
  484.             BITS16Y(WinHeight),32,32,Bits16Unselected,Bits16MouseDown,
  485.             Bits16Selected,Bits16Selected,eIconRadioMode);
  486.         if (Window->Bits16Button == NIL)
  487.             {
  488.              FailurePoint19:
  489.                 DisposeIconButton(Window->Bits8Button);
  490.                 goto FailurePoint18;
  491.             }
  492.  
  493.         switch (SampleObjectGetNumBits(SampleObject))
  494.             {
  495.                 case eSample8bit:
  496.                     SetIconButtonState(Window->Bits8Button,True);
  497.                     break;
  498.                 case eSample16bit:
  499.                     SetIconButtonState(Window->Bits16Button,True);
  500.                     break;
  501.                 default:
  502.                     EXECUTE(PRERR(ForceAbort,"NewSampleWindow:  bad number of bits value"));
  503.                     break;
  504.             }
  505.  
  506.         Window->MonoButton = NewIconButtonPreparedBitmaps(Window->ScreenID,MONOX,
  507.             MONOY(WinHeight),32,32,MonoUnselected,MonoMouseDown,
  508.             MonoSelected,MonoSelected,eIconRadioMode);
  509.         if (Window->MonoButton == NIL)
  510.             {
  511.              FailurePoint20:
  512.                 DisposeIconButton(Window->Bits16Button);
  513.                 goto FailurePoint19;
  514.             }
  515.  
  516.         Window->StereoButton = NewIconButtonPreparedBitmaps(Window->ScreenID,STEREOX,
  517.             STEREOY(WinHeight),32,32,StereoUnselected,StereoMouseDown,
  518.             StereoSelected,StereoSelected,eIconRadioMode);
  519.         if (Window->StereoButton == NIL)
  520.             {
  521.              FailurePoint21:
  522.                 DisposeIconButton(Window->MonoButton);
  523.                 goto FailurePoint20;
  524.             }
  525.  
  526.         switch (SampleObjectGetNumChannels(SampleObject))
  527.             {
  528.                 case eSampleMono:
  529.                     SetIconButtonState(Window->MonoButton,True);
  530.                     break;
  531.                 case eSampleStereo:
  532.                     SetIconButtonState(Window->StereoButton,True);
  533.                     break;
  534.                 default:
  535.                     EXECUTE(PRERR(ForceAbort,"NewSampleWindow:  bad num channels value"));
  536.                     break;
  537.             }
  538.  
  539.         Window->ZoomInButton = NewIconButtonPreparedBitmaps(Window->ScreenID,ZOOMINX,
  540.             ZOOMINY(WinHeight),32,32,ZoomInMouseUp,ZoomInMouseDown,
  541.             ZoomInMouseUp,ZoomInMouseDown,eIconSimpleMode);
  542.         if (Window->ZoomInButton == NIL)
  543.             {
  544.              FailurePoint22:
  545.                 DisposeIconButton(Window->StereoButton);
  546.                 goto FailurePoint21;
  547.             }
  548.  
  549.         Window->ZoomOutButton = NewIconButtonPreparedBitmaps(Window->ScreenID,ZOOMOUTX,
  550.             ZOOMOUTY(WinHeight),32,32,ZoomOutMouseUp,ZoomOutMouseDown,
  551.             ZoomOutMouseUp,ZoomOutMouseDown,eIconSimpleMode);
  552.         if (Window->ZoomOutButton == NIL)
  553.             {
  554.              FailurePoint23:
  555.                 DisposeIconButton(Window->ZoomInButton);
  556.                 goto FailurePoint22;
  557.             }
  558.  
  559.         Window->PlayButton = NewIconButtonPreparedBitmaps(Window->ScreenID,PLAYX,
  560.             PLAYY(WinHeight),32,32,PlayMouseUp,PlayMouseDown,
  561.             PlayMouseUp,PlayMouseDown,eIconSimpleMode);
  562.         if (Window->PlayButton == NIL)
  563.             {
  564.              FailurePoint24:
  565.                 DisposeIconButton(Window->ZoomOutButton);
  566.                 goto FailurePoint23;
  567.             }
  568.  
  569.         /* notify window management */
  570.         Window->MyGenericWindow = CheckInNewWindow(Window->ScreenID,Window,
  571.             (void (*)(void*,MyBoolean,OrdType,OrdType,ModifierFlags))&SampleWindowDoIdle,
  572.             (void (*)(void*))&SampleWindowBecomeActive,
  573.             (void (*)(void*))&SampleWindowBecomeInactive,
  574.             (void (*)(void*))&SampleWindowJustResized,
  575.             (void (*)(OrdType,OrdType,ModifierFlags,void*))&SampleWindowDoMouseDown,
  576.             (void (*)(unsigned char,ModifierFlags,void*))&SampleWindowDoKeyDown,
  577.             (void (*)(void*))&SampleWindowClose,
  578.             (void (*)(void*))&SampleWindowMenuSetup,
  579.             (void (*)(void*,MenuItemType*))&SampleWindowDoMenuCommand);
  580.         if (Window->MyGenericWindow == NIL)
  581.             {
  582.              FailurePoint25:
  583.                 DisposeIconButton(Window->PlayButton);
  584.                 goto FailurePoint24;
  585.             }
  586.  
  587.         Window->MyMenuItem = MakeNewMenuItem(mmWindowMenu,"x",0);
  588.         if (Window->MyMenuItem == NIL)
  589.             {
  590.              FailurePoint26:
  591.                 CheckOutDyingWindow(Window->MyGenericWindow);
  592.                 goto FailurePoint25;
  593.             }
  594.         if (!RegisterWindowMenuItem(Window->MyMenuItem,(void (*)(void*))&ActivateThisWindow,
  595.             Window->ScreenID))
  596.             {
  597.              FailurePoint27:
  598.                 KillMenuItem(Window->MyMenuItem);
  599.                 goto FailurePoint26;
  600.             }
  601.  
  602.         Window->ActiveTextEdit = NIL;
  603.  
  604.         /* install new data into all the edits */
  605.         SampleWindowPutSelectStart(Window);
  606.         SampleWindowPutSelectEnd(Window);
  607.         SampleWindowPutScalingFactor(Window);
  608.         SampleWindowPutOrigin(Window,SampleObjectGetOrigin(SampleObject));
  609.         SampleWindowPutLoopStart(Window,SampleObjectGetLoopStart1(SampleObject));
  610.         SampleWindowPutLoopEnd(Window,SampleObjectGetLoopEnd1(SampleObject));
  611.         SampleWindowPutSamplingRate(Window,SampleObjectGetSamplingRate(SampleObject));
  612.         SampleWindowPutNaturalFrequency(Window,SampleObjectGetNaturalFrequency(SampleObject));
  613.         TextEditHasBeenSaved(Window->SelectionStartEdit);
  614.         TextEditHasBeenSaved(Window->SelectionEndEdit);
  615.         TextEditHasBeenSaved(Window->ScalingEdit);
  616.         TextEditHasBeenSaved(Window->OriginEdit);
  617.         TextEditHasBeenSaved(Window->LoopStartEdit);
  618.         TextEditHasBeenSaved(Window->LoopEndEdit);
  619.         TextEditHasBeenSaved(Window->SamplingRateEdit);
  620.         TextEditHasBeenSaved(Window->NaturalFrequencyEdit);
  621.         UpdateSampleWindowScrollbar(Window);
  622.  
  623.         SampleWindowResetTitlebar(Window);
  624.  
  625.         return Window;
  626.     }
  627.  
  628.  
  629. /* dispose of the window and write all modified data back */
  630. void                                DisposeSampleWindow(SampleWindowRec* Window)
  631.     {
  632.         CheckPtrExistence(Window);
  633.  
  634.         /* save the data */
  635.         if (!SampleWindowWritebackModifiedData(Window))
  636.             {
  637.                 /* failed -- now what? */
  638.             }
  639.  
  640.         /* now that we've recovered all the data, we can delete the things */
  641.         SampleObjectClosingWindowNotify(Window->SampleObject,
  642.             GetWindowXStart(Window->ScreenID),GetWindowYStart(Window->ScreenID),
  643.             GetWindowWidth(Window->ScreenID),GetWindowHeight(Window->ScreenID));
  644.         DeregisterWindowMenuItem(Window->MyMenuItem);
  645.         KillMenuItem(Window->MyMenuItem);
  646.         CheckOutDyingWindow(Window->MyGenericWindow);
  647.         DisposeSampleView(Window->SampleView);
  648.         DisposeTextEdit(Window->NameEdit);
  649.         DisposeTextEdit(Window->SamplingRateEdit);
  650.         DisposeTextEdit(Window->OriginEdit);
  651.         DisposeTextEdit(Window->NaturalFrequencyEdit);
  652.         DisposeTextEdit(Window->LoopStartEdit);
  653.         DisposeTextEdit(Window->LoopEndEdit);
  654.         DisposeTextEdit(Window->SelectionStartEdit);
  655.         DisposeTextEdit(Window->SelectionEndEdit);
  656.         DisposeTextEdit(Window->ScalingEdit);
  657.         DisposeTextEdit(Window->MousePositionEdit);
  658.         DisposeTextEdit(Window->SampleExpressionEdit);
  659.         DisposeScrollBar(Window->Scrollbar);
  660.         DisposeIconButton(Window->Bits8Button);
  661.         DisposeIconButton(Window->Bits16Button);
  662.         DisposeIconButton(Window->MonoButton);
  663.         DisposeIconButton(Window->StereoButton);
  664.         DisposeIconButton(Window->ZoomInButton);
  665.         DisposeIconButton(Window->ZoomOutButton);
  666.         DisposeIconButton(Window->PlayButton);
  667.         KillWindow(Window->ScreenID);
  668.         ReleasePtr((char*)Window);
  669.     }
  670.  
  671.  
  672. /* returns True if any data has been changed since the last time the file was saved */
  673. MyBoolean                        HasSampleWindowBeenModified(SampleWindowRec* Window)
  674.     {
  675.         CheckPtrExistence(Window);
  676.         return DoesSampleViewNeedToBeSaved(Window->SampleView)
  677.             || TextEditDoesItNeedToBeSaved(Window->NameEdit)
  678.             || TextEditDoesItNeedToBeSaved(Window->SamplingRateEdit)
  679.             || TextEditDoesItNeedToBeSaved(Window->OriginEdit)
  680.             || TextEditDoesItNeedToBeSaved(Window->NaturalFrequencyEdit)
  681.             || TextEditDoesItNeedToBeSaved(Window->LoopStartEdit)
  682.             || TextEditDoesItNeedToBeSaved(Window->LoopEndEdit)
  683.             || TextEditDoesItNeedToBeSaved(Window->SampleExpressionEdit);
  684.     }
  685.  
  686.  
  687. /* bring the window to the top and give it the focus */
  688. void                                SampleWindowBringToTop(SampleWindowRec* Window)
  689.     {
  690.         CheckPtrExistence(Window);
  691.         ActivateThisWindow(Window->ScreenID);
  692.     }
  693.  
  694.  
  695. void                                SampleWindowDoIdle(SampleWindowRec* Window,
  696.                                             MyBoolean CheckCursorFlag, OrdType XLoc, OrdType YLoc,
  697.                                             ModifierFlags Modifiers)
  698.     {
  699.         CheckPtrExistence(Window);
  700.         if (Window->ActiveTextEdit != NIL)
  701.             {
  702.                 TextEditUpdateCursor(Window->ActiveTextEdit);
  703.             }
  704.         SampleWindowPutMousePosition(Window);
  705.         if (CheckCursorFlag)
  706.             {
  707.                 if (TextEditIBeamTest(Window->NameEdit,XLoc,YLoc)
  708.                     || TextEditIBeamTest(Window->SamplingRateEdit,XLoc,YLoc)
  709.                     || TextEditIBeamTest(Window->OriginEdit,XLoc,YLoc)
  710.                     || TextEditIBeamTest(Window->NaturalFrequencyEdit,XLoc,YLoc)
  711.                     || TextEditIBeamTest(Window->LoopStartEdit,XLoc,YLoc)
  712.                     || TextEditIBeamTest(Window->LoopEndEdit,XLoc,YLoc)
  713.                     || TextEditIBeamTest(Window->SelectionStartEdit,XLoc,YLoc)
  714.                     || TextEditIBeamTest(Window->SelectionEndEdit,XLoc,YLoc)
  715.                     || TextEditIBeamTest(Window->ScalingEdit,XLoc,YLoc)
  716.                     || TextEditIBeamTest(Window->SampleExpressionEdit,XLoc,YLoc))
  717.                     {
  718.                         SetIBeamCursor();
  719.                     }
  720.                 else if ((XLoc >= SAMPLEEDITX) && (YLoc >= SAMPLEEDITY)
  721.                     && (XLoc < SAMPLEEDITX + SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  722.                     && (YLoc < SAMPLEEDITY
  723.                     + SAMPLEEDITHEIGHT(GetWindowHeight(Window->ScreenID)) - 15))
  724.                     {
  725.                         SetSampleInsertionCursor();
  726.                     }
  727.                 else
  728.                     {
  729.                         SetArrowCursor();
  730.                     }
  731.             }
  732.     }
  733.  
  734.  
  735. void                                SampleWindowBecomeActive(SampleWindowRec* Window)
  736.     {
  737.         OrdType                        XSize;
  738.         OrdType                        YSize;
  739.  
  740.         CheckPtrExistence(Window);
  741.         if (Window->ActiveTextEdit != NIL)
  742.             {
  743.                 EnableTextEditSelection(Window->ActiveTextEdit);
  744.             }
  745.         EnableScrollBar(Window->Scrollbar);
  746.         XSize = GetWindowWidth(Window->ScreenID);
  747.         YSize = GetWindowHeight(Window->ScreenID);
  748.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  749.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(True/*enablegrowicon*/));
  750.     }
  751.  
  752.  
  753. void                                SampleWindowBecomeInactive(SampleWindowRec* Window)
  754.     {
  755.         OrdType                        XSize;
  756.         OrdType                        YSize;
  757.  
  758.         CheckPtrExistence(Window);
  759.         if (Window->ActiveTextEdit != NIL)
  760.             {
  761.                 DisableTextEditSelection(Window->ActiveTextEdit);
  762.             }
  763.         DisableScrollBar(Window->Scrollbar);
  764.         XSize = GetWindowWidth(Window->ScreenID);
  765.         YSize = GetWindowHeight(Window->ScreenID);
  766.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  767.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(False/*disablegrowicon*/));
  768.     }
  769.  
  770.  
  771. void                                SampleWindowJustResized(SampleWindowRec* Window)
  772.     {
  773.         OrdType                        XSize;
  774.         OrdType                        YSize;
  775.  
  776.         CheckPtrExistence(Window);
  777.         XSize = GetWindowWidth(Window->ScreenID);
  778.         YSize = GetWindowHeight(Window->ScreenID);
  779.         SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  780.         DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  781.  
  782.         SetSampleViewLocation(Window->SampleView,SAMPLEEDITX,
  783.             SAMPLEEDITY + (3 * OVERLINEHEIGHT),
  784.             SAMPLEEDITWIDTH(XSize),SAMPLEEDITHEIGHT(YSize) - 15 - (3 * OVERLINEHEIGHT));
  785.         SetTextEditPosition(Window->SelectionStartEdit,SELECTSTARTEDITX,
  786.             SELECTSTARTEDITY(YSize),SELECTSTARTEDITWIDTH,SELECTSTARTEDITHEIGHT);
  787.         SetTextEditPosition(Window->SelectionEndEdit,SELECTENDEDITX,
  788.             SELECTENDEDITY(YSize),SELECTENDEDITWIDTH,SELECTENDEDITHEIGHT);
  789.         SetTextEditPosition(Window->ScalingEdit,SCALINGEDITX,SCALINGEDITY(YSize),
  790.             SCALINGEDITWIDTH,SCALINGEDITHEIGHT);
  791.         SetTextEditPosition(Window->MousePositionEdit,MOUSEPOSEDITX,
  792.             MOUSEPOSEDITY(YSize),MOUSEPOSEDITWIDTH,MOUSEPOSEDITHEIGHT);
  793.         SetTextEditPosition(Window->SampleExpressionEdit,EXPRESSIONEDITX,
  794.             EXPRESSIONEDITY(YSize),EXPRESSIONEDITWIDTH(XSize),EXPRESSIONEDITHEIGHT);
  795.         SetScrollLocation(Window->Scrollbar,SAMPLEEDITX,SAMPLEEDITY
  796.             + SAMPLEEDITHEIGHT(YSize) - 16,SAMPLEEDITWIDTH(XSize));
  797.         SetIconButtonLocation(Window->Bits8Button,BITS8X,BITS8Y(YSize));
  798.         SetIconButtonLocation(Window->Bits16Button,BITS16X,BITS16Y(YSize));
  799.         SetIconButtonLocation(Window->MonoButton,MONOX,MONOY(YSize));
  800.         SetIconButtonLocation(Window->StereoButton,STEREOX,STEREOY(YSize));
  801.         SetIconButtonLocation(Window->ZoomInButton,ZOOMINX,ZOOMINY(YSize));
  802.         SetIconButtonLocation(Window->ZoomOutButton,ZOOMOUTX,ZOOMOUTY(YSize));
  803.         SetIconButtonLocation(Window->PlayButton,PLAYX,PLAYY(YSize));
  804.     }
  805.  
  806.  
  807. /* helper function to set up the undo & present the user with a dialog */
  808. /* asking if he wants to continue if undo information couldn't be saved. */
  809. /* it returns True if the operation should continue. */
  810. static MyBoolean        SampleWindowUndoSetupReturnTrueIfGoAhead(SampleWindowRec* Window)
  811.     {
  812.         CheckPtrExistence(Window);
  813.         SampleWindowFinalizeCurrentEdit(Window);
  814.         Window->UndoOrigin = SampleObjectGetOrigin(Window->SampleObject);
  815.         Window->UndoLoopStart1 = SampleObjectGetLoopStart1(Window->SampleObject);
  816.         Window->UndoLoopStart2 = SampleObjectGetLoopStart2(Window->SampleObject);
  817.         Window->UndoLoopStart3 = SampleObjectGetLoopStart3(Window->SampleObject);
  818.         Window->UndoLoopEnd1 = SampleObjectGetLoopEnd1(Window->SampleObject);
  819.         Window->UndoLoopEnd2 = SampleObjectGetLoopEnd2(Window->SampleObject);
  820.         Window->UndoLoopEnd3 = SampleObjectGetLoopEnd3(Window->SampleObject);
  821.         if (!SampleViewSetupForUndo(Window->SampleView))
  822.             {
  823.                 switch (AskYesNoCancel("There is not enough memory available to save undo "
  824.                     "information.  Do you want to continue anyway?",NIL,"Continue","Cancel",NIL))
  825.                     {
  826.                         default:
  827.                             EXECUTE(PRERR(ForceAbort,"SampleWindowUndoSetupReturnTrueIfGoAhead:  "
  828.                                 "bad value from AskYesNoCancel"));
  829.                             break;
  830.                         case eYes:
  831.                             return True;
  832.                         case eNo:
  833.                             return False;
  834.                     }
  835.             }
  836.         return True;
  837.     }
  838.  
  839.  
  840. void                                SampleWindowDoMouseDown(OrdType XLoc, OrdType YLoc,
  841.                                             ModifierFlags Modifiers, SampleWindowRec* Window)
  842.     {
  843.         CheckPtrExistence(Window);
  844.         if ((XLoc >= GetWindowWidth(Window->ScreenID) - 15)
  845.             && (XLoc < GetWindowWidth(Window->ScreenID))
  846.             && (YLoc >= GetWindowHeight(Window->ScreenID) - 15)
  847.             && (YLoc < GetWindowHeight(Window->ScreenID)))
  848.             {
  849.                 UserGrowWindow(Window->ScreenID,XLoc,YLoc);
  850.                 SampleWindowJustResized(Window);
  851.             }
  852.         else if ((XLoc >= SAMPLEEDITX) && (YLoc >= SAMPLEEDITY)
  853.             && (XLoc < SAMPLEEDITX + SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  854.             && (YLoc < SAMPLEEDITY + SAMPLEEDITHEIGHT(GetWindowHeight(Window->ScreenID))))
  855.             {
  856.                 SampleWindowFinalizeCurrentEdit(Window);
  857.                 SampleWindowDoMouseDownInEdit(Window,XLoc,YLoc);
  858.             }
  859.         else if (TextEditHitTest(Window->SampleExpressionEdit,XLoc,YLoc))
  860.             {
  861.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  862.                     {
  863.                         SampleWindowFinalizeCurrentEdit(Window);
  864.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  865.                         EnableTextEditSelection(Window->ActiveTextEdit);
  866.                     }
  867.                 TextEditDoMouseDown(Window->SampleExpressionEdit,XLoc,YLoc,Modifiers);
  868.             }
  869.         else if (TextEditHitTest(Window->NameEdit,XLoc,YLoc))
  870.             {
  871.                 if (Window->ActiveTextEdit != Window->NameEdit)
  872.                     {
  873.                         SampleWindowFinalizeCurrentEdit(Window);
  874.                         Window->ActiveTextEdit = Window->NameEdit;
  875.                         EnableTextEditSelection(Window->ActiveTextEdit);
  876.                     }
  877.                 TextEditDoMouseDown(Window->NameEdit,XLoc,YLoc,Modifiers);
  878.             }
  879.         else if (TextEditHitTest(Window->SamplingRateEdit,XLoc,YLoc))
  880.             {
  881.                 if (Window->ActiveTextEdit != Window->SamplingRateEdit)
  882.                     {
  883.                         SampleWindowFinalizeCurrentEdit(Window);
  884.                         Window->ActiveTextEdit = Window->SamplingRateEdit;
  885.                         EnableTextEditSelection(Window->ActiveTextEdit);
  886.                     }
  887.                 TextEditDoMouseDown(Window->SamplingRateEdit,XLoc,YLoc,Modifiers);
  888.             }
  889.         else if (TextEditHitTest(Window->OriginEdit,XLoc,YLoc))
  890.             {
  891.                 if (Window->ActiveTextEdit != Window->OriginEdit)
  892.                     {
  893.                         SampleWindowFinalizeCurrentEdit(Window);
  894.                         Window->ActiveTextEdit = Window->OriginEdit;
  895.                         EnableTextEditSelection(Window->ActiveTextEdit);
  896.                     }
  897.                 TextEditDoMouseDown(Window->OriginEdit,XLoc,YLoc,Modifiers);
  898.             }
  899.         else if (TextEditHitTest(Window->NaturalFrequencyEdit,XLoc,YLoc))
  900.             {
  901.                 if (Window->ActiveTextEdit != Window->NaturalFrequencyEdit)
  902.                     {
  903.                         SampleWindowFinalizeCurrentEdit(Window);
  904.                         Window->ActiveTextEdit = Window->NaturalFrequencyEdit;
  905.                         EnableTextEditSelection(Window->ActiveTextEdit);
  906.                     }
  907.                 TextEditDoMouseDown(Window->NaturalFrequencyEdit,XLoc,YLoc,Modifiers);
  908.             }
  909.         else if (TextEditHitTest(Window->LoopStartEdit,XLoc,YLoc))
  910.             {
  911.                 if (Window->ActiveTextEdit != Window->LoopStartEdit)
  912.                     {
  913.                         SampleWindowFinalizeCurrentEdit(Window);
  914.                         Window->ActiveTextEdit = Window->LoopStartEdit;
  915.                         EnableTextEditSelection(Window->ActiveTextEdit);
  916.                     }
  917.                 TextEditDoMouseDown(Window->LoopStartEdit,XLoc,YLoc,Modifiers);
  918.             }
  919.         else if (TextEditHitTest(Window->LoopEndEdit,XLoc,YLoc))
  920.             {
  921.                 if (Window->ActiveTextEdit != Window->LoopEndEdit)
  922.                     {
  923.                         SampleWindowFinalizeCurrentEdit(Window);
  924.                         Window->ActiveTextEdit = Window->LoopEndEdit;
  925.                         EnableTextEditSelection(Window->ActiveTextEdit);
  926.                     }
  927.                 TextEditDoMouseDown(Window->LoopEndEdit,XLoc,YLoc,Modifiers);
  928.             }
  929.         else if (TextEditHitTest(Window->SelectionStartEdit,XLoc,YLoc))
  930.             {
  931.                 if (Window->ActiveTextEdit != Window->SelectionStartEdit)
  932.                     {
  933.                         SampleWindowFinalizeCurrentEdit(Window);
  934.                         Window->ActiveTextEdit = Window->SelectionStartEdit;
  935.                         EnableTextEditSelection(Window->ActiveTextEdit);
  936.                     }
  937.                 TextEditDoMouseDown(Window->SelectionStartEdit,XLoc,YLoc,Modifiers);
  938.             }
  939.         else if (TextEditHitTest(Window->SelectionEndEdit,XLoc,YLoc))
  940.             {
  941.                 if (Window->ActiveTextEdit != Window->SelectionEndEdit)
  942.                     {
  943.                         SampleWindowFinalizeCurrentEdit(Window);
  944.                         Window->ActiveTextEdit = Window->SelectionEndEdit;
  945.                         EnableTextEditSelection(Window->ActiveTextEdit);
  946.                     }
  947.                 TextEditDoMouseDown(Window->SelectionEndEdit,XLoc,YLoc,Modifiers);
  948.             }
  949.         else if (TextEditHitTest(Window->ScalingEdit,XLoc,YLoc))
  950.             {
  951.                 if (Window->ActiveTextEdit != Window->ScalingEdit)
  952.                     {
  953.                         SampleWindowFinalizeCurrentEdit(Window);
  954.                         Window->ActiveTextEdit = Window->ScalingEdit;
  955.                         EnableTextEditSelection(Window->ActiveTextEdit);
  956.                     }
  957.                 TextEditDoMouseDown(Window->ScalingEdit,XLoc,YLoc,Modifiers);
  958.             }
  959.         else if (IconButtonHitTest(Window->Bits8Button,XLoc,YLoc))
  960.             {
  961.                 if (IconButtonMouseDown(Window->Bits8Button,XLoc,YLoc,NIL,NIL))
  962.                     {
  963.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  964.                             {
  965.                                 if (!SetSampleViewNumBits(Window->SampleView,eSample8bit))
  966.                                     {
  967.                                         AlertHalt("There is not enough memory available to convert the "
  968.                                             "sample to 8-bit format.",NIL);
  969.                                     }
  970.                                  else
  971.                                     {
  972.                                         SetIconButtonState(Window->Bits16Button,False);
  973.                                     }
  974.                                 SampleWindowRedrawThangs(Window);
  975.                             }
  976.                     }
  977.             }
  978.         else if (IconButtonHitTest(Window->Bits16Button,XLoc,YLoc))
  979.             {
  980.                 if (IconButtonMouseDown(Window->Bits16Button,XLoc,YLoc,NIL,NIL))
  981.                     {
  982.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  983.                             {
  984.                                 if (!SetSampleViewNumBits(Window->SampleView,eSample16bit))
  985.                                     {
  986.                                         AlertHalt("There is not enough memory available to convert the "
  987.                                             "sample to 16-bit format.",NIL);
  988.                                     }
  989.                                  else
  990.                                     {
  991.                                         SetIconButtonState(Window->Bits8Button,False);
  992.                                     }
  993.                                 SampleWindowRedrawThangs(Window);
  994.                             }
  995.                     }
  996.             }
  997.         else if (IconButtonHitTest(Window->MonoButton,XLoc,YLoc))
  998.             {
  999.                 if (IconButtonMouseDown(Window->MonoButton,XLoc,YLoc,NIL,NIL))
  1000.                     {
  1001.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1002.                             {
  1003.                                 if (!SetSampleViewNumChannels(Window->SampleView,eSampleMono))
  1004.                                     {
  1005.                                         AlertHalt("There is not enough memory available to convert the "
  1006.                                             "sample to mono format.",NIL);
  1007.                                     }
  1008.                                  else
  1009.                                     {
  1010.                                         SetIconButtonState(Window->StereoButton,False);
  1011.                                     }
  1012.                                 SampleWindowRedrawThangs(Window);
  1013.                             }
  1014.                     }
  1015.             }
  1016.         else if (IconButtonHitTest(Window->StereoButton,XLoc,YLoc))
  1017.             {
  1018.                 if (IconButtonMouseDown(Window->StereoButton,XLoc,YLoc,NIL,NIL))
  1019.                     {
  1020.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1021.                             {
  1022.                                 if (!SetSampleViewNumChannels(Window->SampleView,eSampleStereo))
  1023.                                     {
  1024.                                         AlertHalt("There is not enough memory available to convert the "
  1025.                                             "sample to stereo format.",NIL);
  1026.                                     }
  1027.                                  else
  1028.                                     {
  1029.                                         SetIconButtonState(Window->MonoButton,False);
  1030.                                     }
  1031.                                 SampleWindowRedrawThangs(Window);
  1032.                             }
  1033.                     }
  1034.             }
  1035.         else if (IconButtonHitTest(Window->ZoomInButton,XLoc,YLoc))
  1036.             {
  1037.                 if (IconButtonMouseDown(Window->ZoomInButton,XLoc,YLoc,NIL,NIL))
  1038.                     {
  1039.                         double                    NewValue;
  1040.  
  1041.                         NewValue = GetSampleViewHorizontalScale(Window->SampleView) / ZOOMFACTOR;
  1042.                         if (NewValue < MINIMUMHORIZSCALE)
  1043.                             {
  1044.                                 NewValue = MINIMUMHORIZSCALE;
  1045.                             }
  1046.                         SetSampleViewHorizontalScale(Window->SampleView,NewValue);
  1047.                         SampleWindowRedrawThangs(Window);
  1048.                         SampleWindowPutScalingFactor(Window);
  1049.                         UpdateSampleWindowScrollbar(Window);
  1050.                     }
  1051.             }
  1052.         else if (IconButtonHitTest(Window->ZoomOutButton,XLoc,YLoc))
  1053.             {
  1054.                 if (IconButtonMouseDown(Window->ZoomOutButton,XLoc,YLoc,NIL,NIL))
  1055.                     {
  1056.                         double                    NewValue;
  1057.  
  1058.                         NewValue = GetSampleViewHorizontalScale(Window->SampleView) * ZOOMFACTOR;
  1059.                         SetSampleViewHorizontalScale(Window->SampleView,NewValue);
  1060.                         SampleWindowRedrawThangs(Window);
  1061.                         SampleWindowPutScalingFactor(Window);
  1062.                         UpdateSampleWindowScrollbar(Window);
  1063.                     }
  1064.             }
  1065.         else if (IconButtonHitTest(Window->PlayButton,XLoc,YLoc))
  1066.             {
  1067.                 if (IconButtonMouseDown(Window->PlayButton,XLoc,YLoc,
  1068.                     (void (*)(void*, MyBoolean))&SampleTestPlayCallback,Window))
  1069.                     {
  1070.                         if (SampleTestIsItLooping())
  1071.                             {
  1072.                                 SampleTestBreakLoop();
  1073.                             }
  1074.                         while (!SampleTestIsWaitingForCleanup())
  1075.                             {
  1076.                                 if (SampleTestDidErrorOccur() != eSampleTestNoError)
  1077.                                     {
  1078.                                         AlertHalt("An error occurred and the sample couldn't be played.",NIL);
  1079.                                         goto DonePlayingPoint;
  1080.                                     }
  1081.                                 SampleTestPlayCallback(Window,False);
  1082.                             }
  1083.                      DonePlayingPoint:
  1084.                         SampleTestPlayCleanup();
  1085.                         ERROR(SampleTestPlayInProgress(),PRERR(ForceAbort,
  1086.                             "SampleWindowDoMouseDown:  play test failure"));
  1087.                     }
  1088.             }
  1089.         else
  1090.             {
  1091.                 /* finalize edit for click outside any known area */
  1092.                 SampleWindowFinalizeCurrentEdit(Window);
  1093.             }
  1094.     }
  1095.  
  1096.  
  1097. void                                SampleWindowDoKeyDown(unsigned char KeyCode,
  1098.                                             ModifierFlags Modifiers,SampleWindowRec* Window)
  1099.     {
  1100.         CheckPtrExistence(Window);
  1101.         if ((KeyCode == 13) && (Window->ActiveTextEdit != Window->SampleExpressionEdit))
  1102.             {
  1103.                 SampleWindowFinalizeCurrentEdit(Window);
  1104.             }
  1105.         else if ((KeyCode == 9) && (Window->ActiveTextEdit != Window->SampleExpressionEdit))
  1106.             {
  1107.                 TextEditRec*            WasTheActiveTextEdit;
  1108.  
  1109.                 WasTheActiveTextEdit = Window->ActiveTextEdit;
  1110.                 SampleWindowFinalizeCurrentEdit(Window);
  1111.                 if ((Modifiers & eShiftKey) != 0)
  1112.                     {
  1113.                         if (WasTheActiveTextEdit == Window->ScalingEdit)
  1114.                             {
  1115.                                 Window->ActiveTextEdit = Window->SelectionEndEdit;
  1116.                             }
  1117.                         else if (WasTheActiveTextEdit == Window->SelectionEndEdit)
  1118.                             {
  1119.                                 Window->ActiveTextEdit = Window->SelectionStartEdit;
  1120.                             }
  1121.                         else if (WasTheActiveTextEdit == Window->SelectionEndEdit)
  1122.                             {
  1123.                                 Window->ActiveTextEdit = Window->SelectionStartEdit;
  1124.                             }
  1125.                         else if (WasTheActiveTextEdit == Window->SelectionStartEdit)
  1126.                             {
  1127.                                 Window->ActiveTextEdit = Window->LoopEndEdit;
  1128.                             }
  1129.                         else if (WasTheActiveTextEdit == Window->LoopEndEdit)
  1130.                             {
  1131.                                 Window->ActiveTextEdit = Window->LoopStartEdit;
  1132.                             }
  1133.                         else if (WasTheActiveTextEdit == Window->LoopStartEdit)
  1134.                             {
  1135.                                 Window->ActiveTextEdit = Window->NaturalFrequencyEdit;
  1136.                             }
  1137.                         else if (WasTheActiveTextEdit == Window->NaturalFrequencyEdit)
  1138.                             {
  1139.                                 Window->ActiveTextEdit = Window->OriginEdit;
  1140.                             }
  1141.                         else if (WasTheActiveTextEdit == Window->OriginEdit)
  1142.                             {
  1143.                                 Window->ActiveTextEdit = Window->SamplingRateEdit;
  1144.                             }
  1145.                         else if (WasTheActiveTextEdit == Window->SamplingRateEdit)
  1146.                             {
  1147.                                 Window->ActiveTextEdit = Window->NameEdit;
  1148.                             }
  1149.                         else
  1150.                             {
  1151.                                 Window->ActiveTextEdit = Window->ScalingEdit;
  1152.                             }
  1153.                     }
  1154.                  else
  1155.                     {
  1156.                         if (WasTheActiveTextEdit == Window->NameEdit)
  1157.                             {
  1158.                                 Window->ActiveTextEdit = Window->SamplingRateEdit;
  1159.                             }
  1160.                         else if (WasTheActiveTextEdit == Window->SamplingRateEdit)
  1161.                             {
  1162.                                 Window->ActiveTextEdit = Window->OriginEdit;
  1163.                             }
  1164.                         else if (WasTheActiveTextEdit == Window->OriginEdit)
  1165.                             {
  1166.                                 Window->ActiveTextEdit = Window->NaturalFrequencyEdit;
  1167.                             }
  1168.                         else if (WasTheActiveTextEdit == Window->NaturalFrequencyEdit)
  1169.                             {
  1170.                                 Window->ActiveTextEdit = Window->LoopStartEdit;
  1171.                             }
  1172.                         else if (WasTheActiveTextEdit == Window->LoopStartEdit)
  1173.                             {
  1174.                                 Window->ActiveTextEdit = Window->LoopEndEdit;
  1175.                             }
  1176.                         else if (WasTheActiveTextEdit == Window->LoopEndEdit)
  1177.                             {
  1178.                                 Window->ActiveTextEdit = Window->SelectionStartEdit;
  1179.                             }
  1180.                         else if (WasTheActiveTextEdit == Window->SelectionStartEdit)
  1181.                             {
  1182.                                 Window->ActiveTextEdit = Window->SelectionEndEdit;
  1183.                             }
  1184.                         else if (WasTheActiveTextEdit == Window->SelectionEndEdit)
  1185.                             {
  1186.                                 Window->ActiveTextEdit = Window->ScalingEdit;
  1187.                             }
  1188.                         else
  1189.                             {
  1190.                                 Window->ActiveTextEdit = Window->NameEdit;
  1191.                             }
  1192.                     }
  1193.                 EnableTextEditSelection(Window->ActiveTextEdit);
  1194.                 TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  1195.             }
  1196.         else if ((KeyCode == 8) && (Window->ActiveTextEdit == NIL))
  1197.             {
  1198.                 /* for delete key with no text box open, delete any selected range */
  1199.                 /* of the sample.  since ActiveTextEdit == NIL, this will be sent */
  1200.                 /* to the appropriate handler.  however, we must make sure there is */
  1201.                 /* a valid selection first, since SampleWindowMenuSetup usually does that. */
  1202.                 if (SampleViewIsThereValidSelection(Window->SampleView))
  1203.                     {
  1204.                         SampleWindowDoMenuCommand(Window,mClear);
  1205.                     }
  1206.             }
  1207.         else
  1208.             {
  1209.                 if (Window->ActiveTextEdit != NIL)
  1210.                     {
  1211.                         TextEditDoKeyPressed(Window->ActiveTextEdit,KeyCode,Modifiers);
  1212.                     }
  1213.             }
  1214.     }
  1215.  
  1216.  
  1217. void                                SampleWindowClose(SampleWindowRec* Window)
  1218.     {
  1219.         CheckPtrExistence(Window);
  1220.         DisposeSampleWindow(Window);
  1221.     }
  1222.  
  1223.  
  1224. void                                SampleWindowUpdator(SampleWindowRec* Window)
  1225.     {
  1226.         OrdType                        Height;
  1227.         OrdType                        Width;
  1228.         FontType                    ScreenFont;
  1229.  
  1230.         CheckPtrExistence(Window);
  1231.  
  1232.         Width = GetWindowWidth(Window->ScreenID);
  1233.         Height = GetWindowHeight(Window->ScreenID);
  1234.         ScreenFont = GetScreenFont();
  1235.  
  1236.         /* redraw edit boxes */
  1237.         RedrawSampleView(Window->SampleView);
  1238.         SampleWindowRedrawThangs(Window);
  1239.         TextEditFullRedraw(Window->NameEdit);
  1240.         TextEditFullRedraw(Window->SamplingRateEdit);
  1241.         TextEditFullRedraw(Window->OriginEdit);
  1242.         TextEditFullRedraw(Window->NaturalFrequencyEdit);
  1243.         TextEditFullRedraw(Window->LoopStartEdit);
  1244.         TextEditFullRedraw(Window->LoopEndEdit);
  1245.         TextEditFullRedraw(Window->SelectionStartEdit);
  1246.         TextEditFullRedraw(Window->SelectionEndEdit);
  1247.         TextEditFullRedraw(Window->ScalingEdit);
  1248.         TextEditFullRedraw(Window->MousePositionEdit);
  1249.         TextEditFullRedraw(Window->SampleExpressionEdit);
  1250.         RedrawScrollBar(Window->Scrollbar);
  1251.         RedrawIconButton(Window->Bits8Button);
  1252.         RedrawIconButton(Window->Bits16Button);
  1253.         RedrawIconButton(Window->MonoButton);
  1254.         RedrawIconButton(Window->StereoButton);
  1255.         RedrawIconButton(Window->ZoomInButton);
  1256.         RedrawIconButton(Window->ZoomOutButton);
  1257.         RedrawIconButton(Window->PlayButton);
  1258.  
  1259.         /* redraw static titles */
  1260.         SetClipRect(Window->ScreenID,0,0,Width,Height);
  1261.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Sample Name:",12,
  1262.             SAMPLENAMEX,SAMPLENAMEY,ePlain);
  1263.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Sampling Rate:",14,
  1264.             SAMPLERATEX,SAMPLERATEY,ePlain);
  1265.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Origin:",7,ORIGINX,ORIGINY,ePlain);
  1266.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Natural Freq:",13,
  1267.             BASEFREQX,BASEFREQY,ePlain);
  1268.         switch (Window->EditingWhichLoop)
  1269.             {
  1270.                 default:
  1271.                     EXECUTE(PRERR(ForceAbort,"SampleWindowUpdator:  bad value in EditingWhichLoop"));
  1272.                     break;
  1273.                 case eSampleLoop1:
  1274.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop Start 1:",13,
  1275.                         LOOPSTARTX,LOOPSTARTY,ePlain);
  1276.                     break;
  1277.                 case eSampleLoop2:
  1278.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop Start 2:",13,
  1279.                         LOOPSTARTX,LOOPSTARTY,ePlain);
  1280.                     break;
  1281.                 case eSampleLoop3:
  1282.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop Start 3:",13,
  1283.                         LOOPSTARTX,LOOPSTARTY,ePlain);
  1284.                     break;
  1285.             }
  1286.         switch (Window->EditingWhichLoop)
  1287.             {
  1288.                 default:
  1289.                     EXECUTE(PRERR(ForceAbort,"SampleWindowUpdator:  bad value in EditingWhichLoop"));
  1290.                     break;
  1291.                 case eSampleLoop1:
  1292.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop End 1:",11,
  1293.                         LOOPENDX,LOOPENDY,ePlain);
  1294.                     break;
  1295.                 case eSampleLoop2:
  1296.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop End 2:",11,
  1297.                         LOOPENDX,LOOPENDY,ePlain);
  1298.                     break;
  1299.                 case eSampleLoop3:
  1300.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop End 3:",11,
  1301.                         LOOPENDX,LOOPENDY,ePlain);
  1302.                     break;
  1303.             }
  1304.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Select Start:",13,
  1305.             SELECTSTARTX,SELECTSTARTY(Height),ePlain);
  1306.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Select End:",11,
  1307.             SELECTENDX,SELECTENDY(Height),ePlain);
  1308.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Mouse Location:",15,
  1309.             MOUSELOCX,MOUSELOCY(Height),ePlain);
  1310.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Scaling Factor:",15,
  1311.             SCALINGX,SCALINGY(Height),ePlain);
  1312.  
  1313.         /* grow icon */
  1314.         SetClipRect(Window->ScreenID,Width - 15,Height - 15,Width,Height);
  1315.         DrawBitmap(Window->ScreenID,Width - 15,Height - 15,
  1316.             GetGrowIcon(Window->MyGenericWindow == GetCurrentWindowID()));
  1317.     }
  1318.  
  1319.  
  1320. void                                SampleWindowMenuSetup(SampleWindowRec* Window)
  1321.     {
  1322.         CheckPtrExistence(Window);
  1323.         MainWindowEnableGlobalMenus(Window->MainWindow);
  1324.  
  1325.         if (Window->ActiveTextEdit != NIL)
  1326.             {
  1327.                 /* editing text, enable menu items for text entry */
  1328.                 EnableMenuItem(mPaste);
  1329.                 ChangeItemName(mPaste,"Paste Text");
  1330.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1331.                     {
  1332.                         EnableMenuItem(mCut);
  1333.                         ChangeItemName(mCut,"Cut Text");
  1334.                         EnableMenuItem(mCopy);
  1335.                         ChangeItemName(mCopy,"Copy Text");
  1336.                         EnableMenuItem(mClear);
  1337.                         ChangeItemName(mClear,"Clear Text");
  1338.                     }
  1339.                 EnableMenuItem(mSelectAll);
  1340.                 ChangeItemName(mSelectAll,"Select All Text");
  1341.                 if (TextEditCanWeUndo(Window->ActiveTextEdit))
  1342.                     {
  1343.                         EnableMenuItem(mUndo);
  1344.                         ChangeItemName(mUndo,"Undo Text Change");
  1345.                     }
  1346.                 EnableMenuItem(mShiftLeft);
  1347.                 EnableMenuItem(mShiftRight);
  1348.                 EnableMenuItem(mBalanceParens);
  1349.                 EnableMenuItem(mFind);
  1350.                 if (PtrSize(GlobalSearchString) != 0)
  1351.                     {
  1352.                         EnableMenuItem(mFindAgain);
  1353.                         if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1354.                             {
  1355.                                 EnableMenuItem(mReplace);
  1356.                                 EnableMenuItem(mReplaceAndFindAgain);
  1357.                             }
  1358.                     }
  1359.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1360.                     {
  1361.                         EnableMenuItem(mEnterSelection);
  1362.                     }
  1363.                 EnableMenuItem(mShowSelection);
  1364.             }
  1365.          else
  1366.             {
  1367.                 /* editing the sample */
  1368.                 EnableMenuItem(mPaste);
  1369.                 ChangeItemName(mPaste,"Paste Sample");
  1370.                 if (SampleViewIsThereValidSelection(Window->SampleView))
  1371.                     {
  1372.                         EnableMenuItem(mCut);
  1373.                         ChangeItemName(mCut,"Cut Sample");
  1374.                         EnableMenuItem(mCopy);
  1375.                         ChangeItemName(mCopy,"Copy Sample");
  1376.                         EnableMenuItem(mClear);
  1377.                         ChangeItemName(mClear,"Clear Sample");
  1378.                     }
  1379.                 EnableMenuItem(mSelectAll);
  1380.                 ChangeItemName(mSelectAll,"Select All Sample");
  1381.                 if (SampleViewUndoAvailable(Window->SampleView))
  1382.                     {
  1383.                         EnableMenuItem(mUndo);
  1384.                         ChangeItemName(mUndo,"Undo Sample Edit");
  1385.                     }
  1386.             }
  1387.  
  1388.         EnableMenuItem(mCloseFile);
  1389.         ChangeItemName(mCloseFile,"Close Sample Editor");
  1390.         EnableMenuItem(mEvaluateCalc);
  1391.         ChangeItemName(mDeleteObject,"Delete Sample");
  1392.         EnableMenuItem(mDeleteObject);
  1393.  
  1394.         EnableMenuItem(mSampleEditLoop1);
  1395.         EnableMenuItem(mSampleEditLoop2);
  1396.         EnableMenuItem(mSampleEditLoop3);
  1397.         switch (Window->EditingWhichLoop)
  1398.             {
  1399.                 default:
  1400.                     EXECUTE(PRERR(ForceAbort,
  1401.                         "SampleWindowMenuSetup:  bad value in EditingWhichLoop"));
  1402.                     break;
  1403.                 case eSampleLoop1:
  1404.                     SetItemCheckmark(mSampleEditLoop1);
  1405.                     break;
  1406.                 case eSampleLoop2:
  1407.                     SetItemCheckmark(mSampleEditLoop2);
  1408.                     break;
  1409.                 case eSampleLoop3:
  1410.                     SetItemCheckmark(mSampleEditLoop3);
  1411.                     break;
  1412.             }
  1413.  
  1414.         EnableMenuItem(mExportWAVFormat);
  1415.         EnableMenuItem(mExportRAWFormat);
  1416.         EnableMenuItem(mExportAIFFFormat);
  1417.  
  1418.         SetItemCheckmark(Window->MyMenuItem);
  1419.     }
  1420.  
  1421.  
  1422. void                                SampleWindowDoMenuCommand(SampleWindowRec* Window,
  1423.                                             MenuItemType* MenuItem)
  1424.     {
  1425.         CheckPtrExistence(Window);
  1426.         if (MainWindowDoGlobalMenuItem(Window->MainWindow,MenuItem))
  1427.             {
  1428.             }
  1429.         else if (MenuItem == mPaste)
  1430.             {
  1431.                 if (Window->ActiveTextEdit != NIL)
  1432.                     {
  1433.                         TextEditDoMenuPaste(Window->ActiveTextEdit);
  1434.                     }
  1435.                  else
  1436.                     {
  1437.                         long                                                SelectStart;
  1438.                         long                                                SelectEnd;
  1439.                         SampleStorageDisplayRec*        ClipboardData;
  1440.                         long                                                NetInsertedFrames;
  1441.  
  1442.                         /* paste sample */
  1443.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1444.                             {
  1445.                                 /* set up undo; ask user whether to continue if not saved ya? */
  1446.                                 return;
  1447.                             }
  1448.                         ClipboardData = GetSampleStorageDisplayFromClipboard();
  1449.                         if (ClipboardData == NIL)
  1450.                             {
  1451.                              PasteFailurePoint1:
  1452.                                 return;
  1453.                             }
  1454.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1455.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1456.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  1457.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  1458.                             SelectEnd - SelectStart))
  1459.                             {
  1460.                              PasteFailurePoint2:
  1461.                                 DisposeSampleStorageDisplay(ClipboardData);
  1462.                                 goto PasteFailurePoint1;
  1463.                             }
  1464.                         if (!InsertSampleViewSection(Window->SampleView,SelectStart,ClipboardData))
  1465.                             {
  1466.                              PasteFailurePoint3:
  1467.                                 goto PasteFailurePoint2;
  1468.                             }
  1469.                         NetInsertedFrames += GetSampleStorageDisplayNumFrames(ClipboardData);
  1470.                         DisposeSampleStorageDisplay(ClipboardData);
  1471.                         UpdateSampleWindowScrollbar(Window);
  1472.                         SampleWindowRedrawUnderThangs(Window);
  1473.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  1474.                         AdjustSampleWindowThangs(Window);
  1475.                         SampleWindowRedrawThangs(Window);
  1476.                         SampleWindowPutSelectStart(Window);
  1477.                         SampleWindowPutSelectEnd(Window);
  1478.                     }
  1479.             }
  1480.         else if (MenuItem == mCut)
  1481.             {
  1482.                 if (Window->ActiveTextEdit != NIL)
  1483.                     {
  1484.                         TextEditDoMenuCut(Window->ActiveTextEdit);
  1485.                     }
  1486.                  else
  1487.                     {
  1488.                         SampleStorageDisplayRec*        DeletedData;
  1489.                         long                                                SelectStart;
  1490.                         long                                                SelectEnd;
  1491.                         long                                                NetInsertedFrames;
  1492.  
  1493.                         /* sample cut */
  1494.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1495.                             {
  1496.                                 /* set up undo; ask user whether to continue if not saved ya? */
  1497.                                 return;
  1498.                             }
  1499.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1500.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1501.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  1502.                         DeletedData = ExtractSampleViewSection(Window->SampleView,SelectStart,
  1503.                             SelectEnd - SelectStart);
  1504.                         if (DeletedData == NIL)
  1505.                             {
  1506.                              CutSampleFailurePoint:
  1507.                                 AlertHalt("There is not enough memory available to "
  1508.                                     "complete the cut operation.",NIL);
  1509.                                 return;
  1510.                             }
  1511.                         PutSampleStorageDisplayOnClipboard(DeletedData); /* put it on the clipboard */
  1512.                         DisposeSampleStorageDisplay(DeletedData);
  1513.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  1514.                             SelectEnd - SelectStart))
  1515.                             {
  1516.                                 goto CutSampleFailurePoint;
  1517.                             }
  1518.                         UpdateSampleWindowScrollbar(Window);
  1519.                         SampleWindowRedrawUnderThangs(Window);
  1520.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  1521.                         AdjustSampleWindowThangs(Window);
  1522.                         SampleWindowRedrawThangs(Window);
  1523.                         SampleWindowPutSelectStart(Window);
  1524.                         SampleWindowPutSelectEnd(Window);
  1525.                     }
  1526.             }
  1527.         else if (MenuItem == mCopy)
  1528.             {
  1529.                 if (Window->ActiveTextEdit != NIL)
  1530.                     {
  1531.                         TextEditDoMenuCopy(Window->ActiveTextEdit);
  1532.                     }
  1533.                  else
  1534.                     {
  1535.                         SampleStorageDisplayRec*        CopiedData;
  1536.                         long                                                SelectStart;
  1537.                         long                                                SelectEnd;
  1538.  
  1539.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1540.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1541.                         CopiedData = ExtractSampleViewSection(Window->SampleView,SelectStart,
  1542.                             SelectEnd - SelectStart);
  1543.                         if (CopiedData == NIL)
  1544.                             {
  1545.                                 AlertHalt("There is not enough memory available to "
  1546.                                     "complete the copy operation.",NIL);
  1547.                                 return;
  1548.                             }
  1549.                         PutSampleStorageDisplayOnClipboard(CopiedData); /* put it on the clipboard */
  1550.                         DisposeSampleStorageDisplay(CopiedData);
  1551.                     }
  1552.             }
  1553.         else if (MenuItem == mClear)
  1554.             {
  1555.                 if (Window->ActiveTextEdit != NIL)
  1556.                     {
  1557.                         TextEditDoMenuClear(Window->ActiveTextEdit);
  1558.                     }
  1559.                  else
  1560.                     {
  1561.                         long                                SelectStart;
  1562.                         long                                SelectEnd;
  1563.                         long                                NetInsertedFrames;
  1564.  
  1565.                         /* sample delete */
  1566.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1567.                             {
  1568.                                 /* set up undo; ask user whether to continue if not saved ya? */
  1569.                                 return;
  1570.                             }
  1571.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1572.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1573.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  1574.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  1575.                             SelectEnd - SelectStart))
  1576.                             {
  1577.                                 AlertHalt("There is not enough memory available to "
  1578.                                     "complete the clear operation.",NIL);
  1579.                                 return;
  1580.                             }
  1581.                         UpdateSampleWindowScrollbar(Window);
  1582.                         SampleWindowRedrawUnderThangs(Window);
  1583.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  1584.                         AdjustSampleWindowThangs(Window);
  1585.                         SampleWindowRedrawThangs(Window);
  1586.                         SampleWindowPutSelectStart(Window);
  1587.                         SampleWindowPutSelectEnd(Window);
  1588.                     }
  1589.             }
  1590.         else if (MenuItem == mSelectAll)
  1591.             {
  1592.                 if (Window->ActiveTextEdit != NIL)
  1593.                     {
  1594.                         TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  1595.                     }
  1596.                  else
  1597.                     {
  1598.                         SetSampleViewSelection(Window->SampleView,0,
  1599.                             GetSampleViewNumFrames(Window->SampleView));
  1600.                         SampleWindowPutSelectStart(Window);
  1601.                         SampleWindowPutSelectEnd(Window);
  1602.                     }
  1603.             }
  1604.         else if (MenuItem == mShowSelection)
  1605.             {
  1606.                 if (Window->ActiveTextEdit != NIL)
  1607.                     {
  1608.                         TextEditShowSelection(Window->ActiveTextEdit);
  1609.                     }
  1610.                  else
  1611.                     {
  1612.                     }
  1613.             }
  1614.         else if (MenuItem == mUndo)
  1615.             {
  1616.                 if (Window->ActiveTextEdit != NIL)
  1617.                     {
  1618.                         /* this is the one for text editing */
  1619.                         TextEditDoMenuUndo(Window->ActiveTextEdit);
  1620.                     }
  1621.                  else
  1622.                     {
  1623.                         /* this is the one for samples */
  1624.                         SampleWindowFinalizeCurrentEdit(Window);
  1625.                         if (!SampleViewUndo(Window->SampleView))
  1626.                             {
  1627.                                 AlertHalt("There was not enough memory available to undo the sample "
  1628.                                     "edit.  Try closing some other windows to free up memory.",NIL);
  1629.                             }
  1630.                         switch (GetSampleViewNumChannels(Window->SampleView))
  1631.                             {
  1632.                                 case eSampleMono:
  1633.                                     SetIconButtonState(Window->MonoButton,True);
  1634.                                     SetIconButtonState(Window->StereoButton,False);
  1635.                                     break;
  1636.                                 case eSampleStereo:
  1637.                                     SetIconButtonState(Window->MonoButton,False);
  1638.                                     SetIconButtonState(Window->StereoButton,True);
  1639.                                     break;
  1640.                                 default:
  1641.                                     EXECUTE(PRERR(ForceAbort,
  1642.                                         "SampleWindowDoMenuCommand:  bad num channels value"));
  1643.                                     break;
  1644.                             }
  1645.                         switch (GetSampleViewNumBits(Window->SampleView))
  1646.                             {
  1647.                                 case eSample8bit:
  1648.                                     SetIconButtonState(Window->Bits8Button,True);
  1649.                                     SetIconButtonState(Window->Bits16Button,False);
  1650.                                     break;
  1651.                                 case eSample16bit:
  1652.                                     SetIconButtonState(Window->Bits8Button,False);
  1653.                                     SetIconButtonState(Window->Bits16Button,True);
  1654.                                     break;
  1655.                                 default:
  1656.                                     EXECUTE(PRERR(ForceAbort,
  1657.                                         "SampleWindowDoMenuCommand:  bad number of bits value"));
  1658.                                     break;
  1659.                             }
  1660.                         /* restore some crud */
  1661.                         SampleObjectPutOrigin(Window->SampleObject,Window->UndoOrigin);
  1662.                         SampleObjectPutLoopStart1(Window->SampleObject,Window->UndoLoopStart1);
  1663.                         SampleObjectPutLoopStart2(Window->SampleObject,Window->UndoLoopStart2);
  1664.                         SampleObjectPutLoopStart3(Window->SampleObject,Window->UndoLoopStart3);
  1665.                         SampleObjectPutLoopEnd1(Window->SampleObject,Window->UndoLoopEnd1);
  1666.                         SampleObjectPutLoopEnd2(Window->SampleObject,Window->UndoLoopEnd2);
  1667.                         SampleObjectPutLoopEnd3(Window->SampleObject,Window->UndoLoopEnd3);
  1668.                         /* fix up edit versions */
  1669.                         SampleWindowPutOrigin(Window,Window->UndoOrigin);
  1670.                         switch (Window->EditingWhichLoop)
  1671.                             {
  1672.                                 default:
  1673.                                     EXECUTE(PRERR(ForceAbort,
  1674.                                         "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1675.                                     break;
  1676.                                 case eSampleLoop1:
  1677.                                     SampleWindowPutLoopStart(Window,Window->UndoLoopStart1);
  1678.                                     SampleWindowPutLoopEnd(Window,Window->UndoLoopEnd1);
  1679.                                     break;
  1680.                                 case eSampleLoop2:
  1681.                                     SampleWindowPutLoopStart(Window,Window->UndoLoopStart2);
  1682.                                     SampleWindowPutLoopEnd(Window,Window->UndoLoopEnd2);
  1683.                                     break;
  1684.                                 case eSampleLoop3:
  1685.                                     SampleWindowPutLoopStart(Window,Window->UndoLoopStart3);
  1686.                                     SampleWindowPutLoopEnd(Window,Window->UndoLoopEnd3);
  1687.                                     break;
  1688.                             }
  1689.                     }
  1690.             }
  1691.         else if (MenuItem == mCloseFile)
  1692.             {
  1693.                 SampleWindowClose(Window);
  1694.             }
  1695.         else if (MenuItem == mEvaluateCalc)
  1696.             {
  1697.                 if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1698.                     {
  1699.                         /* set up undo; ask user whether to continue if not saved ya? */
  1700.                         return;
  1701.                     }
  1702.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1703.                     {
  1704.                         SampleWindowFinalizeCurrentEdit(Window);
  1705.                     }
  1706.                 SampleWindowPerformCalculation(Window);
  1707.             }
  1708.         else if (MenuItem == mShiftLeft)
  1709.             {
  1710.                 TextEditShiftSelectionLeftOneTab(Window->ActiveTextEdit);
  1711.             }
  1712.         else if (MenuItem == mShiftRight)
  1713.             {
  1714.                 TextEditShiftSelectionRightOneTab(Window->ActiveTextEdit);
  1715.             }
  1716.         else if (MenuItem == mBalanceParens)
  1717.             {
  1718.                 TextEditBalanceParens(Window->ActiveTextEdit);
  1719.             }
  1720.         else if (MenuItem == mDeleteObject)
  1721.             {
  1722.                 SampleListDeleteSample(Window->SampleList,Window->SampleObject);
  1723.             }
  1724.         else if (MenuItem == mSampleEditLoop1)
  1725.             {
  1726.                 OrdType                        XSize;
  1727.                 OrdType                        YSize;
  1728.  
  1729.                 SampleWindowFinalizeCurrentEdit(Window);
  1730.                 switch (Window->EditingWhichLoop)
  1731.                     {
  1732.                         default:
  1733.                             EXECUTE(PRERR(ForceAbort,
  1734.                                 "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1735.                             break;
  1736.                         case eSampleLoop1:
  1737.                             /* switch from 1 to 1 -- no change */
  1738.                             break;
  1739.                         case eSampleLoop2:
  1740.                             /* put away 2 */
  1741.                             SampleObjectPutLoopStart2(Window->SampleObject,
  1742.                                 SampleWindowGetLoopStart(Window));
  1743.                             SampleObjectPutLoopEnd2(Window->SampleObject,
  1744.                                 SampleWindowGetLoopEnd(Window));
  1745.                             /* load 1 */
  1746.                             SampleWindowPutLoopStart(Window,
  1747.                                 SampleObjectGetLoopStart1(Window->SampleObject));
  1748.                             SampleWindowPutLoopEnd(Window,
  1749.                                 SampleObjectGetLoopEnd1(Window->SampleObject));
  1750.                             break;
  1751.                         case eSampleLoop3:
  1752.                             /* put away 3 */
  1753.                             SampleObjectPutLoopStart3(Window->SampleObject,
  1754.                                 SampleWindowGetLoopStart(Window));
  1755.                             SampleObjectPutLoopEnd3(Window->SampleObject,
  1756.                                 SampleWindowGetLoopEnd(Window));
  1757.                             /* load 1 */
  1758.                             SampleWindowPutLoopStart(Window,
  1759.                                 SampleObjectGetLoopStart1(Window->SampleObject));
  1760.                             SampleWindowPutLoopEnd(Window,
  1761.                                 SampleObjectGetLoopEnd1(Window->SampleObject));
  1762.                             break;
  1763.                     }
  1764.                 Window->EditingWhichLoop = eSampleLoop1;
  1765.                 XSize = GetWindowWidth(Window->ScreenID);
  1766.                 YSize = GetWindowHeight(Window->ScreenID);
  1767.                 SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  1768.                 DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  1769.                 SampleWindowUpdator(Window);
  1770.             }
  1771.         else if (MenuItem == mSampleEditLoop2)
  1772.             {
  1773.                 OrdType                        XSize;
  1774.                 OrdType                        YSize;
  1775.  
  1776.                 SampleWindowFinalizeCurrentEdit(Window);
  1777.                 switch (Window->EditingWhichLoop)
  1778.                     {
  1779.                         default:
  1780.                             EXECUTE(PRERR(ForceAbort,
  1781.                                 "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1782.                             break;
  1783.                         case eSampleLoop1:
  1784.                             /* put away 1 */
  1785.                             SampleObjectPutLoopStart1(Window->SampleObject,
  1786.                                 SampleWindowGetLoopStart(Window));
  1787.                             SampleObjectPutLoopEnd1(Window->SampleObject,
  1788.                                 SampleWindowGetLoopEnd(Window));
  1789.                             /* load 2 */
  1790.                             SampleWindowPutLoopStart(Window,
  1791.                                 SampleObjectGetLoopStart2(Window->SampleObject));
  1792.                             SampleWindowPutLoopEnd(Window,
  1793.                                 SampleObjectGetLoopEnd2(Window->SampleObject));
  1794.                             break;
  1795.                         case eSampleLoop2:
  1796.                             /* switch from 2 to 2 -- no change */
  1797.                             break;
  1798.                         case eSampleLoop3:
  1799.                             /* put away 3 */
  1800.                             SampleObjectPutLoopStart3(Window->SampleObject,
  1801.                                 SampleWindowGetLoopStart(Window));
  1802.                             SampleObjectPutLoopEnd3(Window->SampleObject,
  1803.                                 SampleWindowGetLoopEnd(Window));
  1804.                             /* load 2 */
  1805.                             SampleWindowPutLoopStart(Window,
  1806.                                 SampleObjectGetLoopStart2(Window->SampleObject));
  1807.                             SampleWindowPutLoopEnd(Window,
  1808.                                 SampleObjectGetLoopEnd2(Window->SampleObject));
  1809.                             break;
  1810.                     }
  1811.                 Window->EditingWhichLoop = eSampleLoop2;
  1812.                 XSize = GetWindowWidth(Window->ScreenID);
  1813.                 YSize = GetWindowHeight(Window->ScreenID);
  1814.                 SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  1815.                 DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  1816.                 SampleWindowUpdator(Window);
  1817.             }
  1818.         else if (MenuItem == mSampleEditLoop3)
  1819.             {
  1820.                 OrdType                        XSize;
  1821.                 OrdType                        YSize;
  1822.  
  1823.                 SampleWindowFinalizeCurrentEdit(Window);
  1824.                 switch (Window->EditingWhichLoop)
  1825.                     {
  1826.                         default:
  1827.                             EXECUTE(PRERR(ForceAbort,
  1828.                                 "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1829.                             break;
  1830.                         case eSampleLoop1:
  1831.                             /* put away 1 */
  1832.                             SampleObjectPutLoopStart1(Window->SampleObject,
  1833.                                 SampleWindowGetLoopStart(Window));
  1834.                             SampleObjectPutLoopEnd1(Window->SampleObject,
  1835.                                 SampleWindowGetLoopEnd(Window));
  1836.                             /* load 3 */
  1837.                             SampleWindowPutLoopStart(Window,
  1838.                                 SampleObjectGetLoopStart3(Window->SampleObject));
  1839.                             SampleWindowPutLoopEnd(Window,
  1840.                                 SampleObjectGetLoopEnd3(Window->SampleObject));
  1841.                             break;
  1842.                         case eSampleLoop2:
  1843.                             /* put away 2 */
  1844.                             SampleObjectPutLoopStart2(Window->SampleObject,
  1845.                                 SampleWindowGetLoopStart(Window));
  1846.                             SampleObjectPutLoopEnd2(Window->SampleObject,
  1847.                                 SampleWindowGetLoopEnd(Window));
  1848.                             /* load 3 */
  1849.                             SampleWindowPutLoopStart(Window,
  1850.                                 SampleObjectGetLoopStart3(Window->SampleObject));
  1851.                             SampleWindowPutLoopEnd(Window,
  1852.                                 SampleObjectGetLoopEnd3(Window->SampleObject));
  1853.                             break;
  1854.                         case eSampleLoop3:
  1855.                             /* switch from 3 to 3 -- no change */
  1856.                             break;
  1857.                     }
  1858.                 Window->EditingWhichLoop = eSampleLoop3;
  1859.                 XSize = GetWindowWidth(Window->ScreenID);
  1860.                 YSize = GetWindowHeight(Window->ScreenID);
  1861.                 SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  1862.                 DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  1863.                 SampleWindowUpdator(Window);
  1864.             }
  1865.         else if (MenuItem == mFind)
  1866.             {
  1867.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1868.                     {
  1869.                         SampleWindowFinalizeCurrentEdit(Window);
  1870.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1871.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1872.                     }
  1873.                 switch (DoFindDialog(&GlobalSearchString,&GlobalReplaceString,
  1874.                     mCut,mPaste,mCopy,mUndo,mSelectAll,mClear))
  1875.                     {
  1876.                         default:
  1877.                             EXECUTE(PRERR(ForceAbort,
  1878.                                 "SampleWindowDoMenuCommand:  bad value from DoFindDialog"));
  1879.                             break;
  1880.                         case eFindCancel:
  1881.                         case eDontFind:
  1882.                             break;
  1883.                         case eFindFromStart:
  1884.                             SetTextEditInsertionPoint(Window->ActiveTextEdit,0,0);
  1885.                             TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1886.                             TextEditShowSelection(Window->ActiveTextEdit);
  1887.                             break;
  1888.                         case eFindAgain:
  1889.                             TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1890.                             TextEditShowSelection(Window->ActiveTextEdit);
  1891.                             break;
  1892.                     }
  1893.             }
  1894.         else if (MenuItem == mFindAgain)
  1895.             {
  1896.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1897.                     {
  1898.                         SampleWindowFinalizeCurrentEdit(Window);
  1899.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1900.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1901.                     }
  1902.                 TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1903.                 TextEditShowSelection(Window->ActiveTextEdit);
  1904.             }
  1905.         else if (MenuItem == mReplace)
  1906.             {
  1907.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1908.                     {
  1909.                         SampleWindowFinalizeCurrentEdit(Window);
  1910.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1911.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1912.                     }
  1913.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1914.                     {
  1915.                         TextEditInsertRawData(Window->ActiveTextEdit,GlobalReplaceString,
  1916.                             SYSTEMLINEFEED);
  1917.                     }
  1918.             }
  1919.         else if (MenuItem == mReplaceAndFindAgain)
  1920.             {
  1921.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1922.                     {
  1923.                         SampleWindowFinalizeCurrentEdit(Window);
  1924.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1925.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1926.                     }
  1927.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1928.                     {
  1929.                         TextEditInsertRawData(Window->ActiveTextEdit,GlobalReplaceString,
  1930.                             SYSTEMLINEFEED);
  1931.                         TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1932.                         TextEditShowSelection(Window->ActiveTextEdit);
  1933.                     }
  1934.             }
  1935.         else if (MenuItem == mEnterSelection)
  1936.             {
  1937.                 if (Window->ActiveTextEdit != NIL)
  1938.                     {
  1939.                         char*                        NewString;
  1940.  
  1941.                         NewString = TextEditGetSelection(Window->ActiveTextEdit);
  1942.                         if (NewString != NIL)
  1943.                             {
  1944.                                 ReleasePtr(GlobalSearchString);
  1945.                                 GlobalSearchString = NewString;
  1946.                             }
  1947.                     }
  1948.             }
  1949.         else if (MenuItem == mExportWAVFormat)
  1950.             {
  1951.                 SampleStorageActualRec*        TheStuff;
  1952.  
  1953.                 TheStuff = SampleWindowGetTheSampleActual(Window);
  1954.                 if (TheStuff != NIL)
  1955.                     {
  1956.                         ExportWAVSample(TheStuff,SampleWindowGetSamplingRate(Window));
  1957.                         DisposeSampleStorageActual(TheStuff);
  1958.                     }
  1959.                  else
  1960.                     {
  1961.                         AlertHalt("There is not enough memory available to export the sample.",NIL);
  1962.                     }
  1963.             }
  1964.         else if (MenuItem == mExportRAWFormat)
  1965.             {
  1966.                 SampleStorageActualRec*        TheStuff;
  1967.  
  1968.                 TheStuff = SampleWindowGetTheSampleActual(Window);
  1969.                 if (TheStuff != NIL)
  1970.                     {
  1971.                         ExportRAWSample(TheStuff);
  1972.                         DisposeSampleStorageActual(TheStuff);
  1973.                     }
  1974.                  else
  1975.                     {
  1976.                         AlertHalt("There is not enough memory available to export the sample.",NIL);
  1977.                     }
  1978.             }
  1979.         else if (MenuItem == mExportAIFFFormat)
  1980.             {
  1981.                 SampleStorageActualRec*        TheStuff;
  1982.  
  1983.                 TheStuff = SampleWindowGetTheSampleActual(Window);
  1984.                 if (TheStuff != NIL)
  1985.                     {
  1986.                         ExportAIFFSample(TheStuff,SampleWindowGetSamplingRate(Window));
  1987.                         DisposeSampleStorageActual(TheStuff);
  1988.                     }
  1989.                  else
  1990.                     {
  1991.                         AlertHalt("There is not enough memory available to export the sample.",NIL);
  1992.                     }
  1993.             }
  1994.         else
  1995.             {
  1996.                 EXECUTE(PRERR(AllowResume,"SampleWindowDoMenuCommand:  unknown menu command"));
  1997.             }
  1998.     }
  1999.  
  2000.  
  2001. /* copy data in the currently active text edit field to wherever it belongs */
  2002. void                                SampleWindowFinalizeCurrentEdit(SampleWindowRec* Window)
  2003.     {
  2004.         CheckPtrExistence(Window);
  2005.         if (Window->ActiveTextEdit != NIL)
  2006.             {
  2007.                 if (Window->ActiveTextEdit == Window->SampleExpressionEdit)
  2008.                     {
  2009.                     }
  2010.                 else if (Window->ActiveTextEdit == Window->NameEdit)
  2011.                     {
  2012.                     }
  2013.                 else if (Window->ActiveTextEdit == Window->SamplingRateEdit)
  2014.                     {
  2015.                         long                    Temp;
  2016.  
  2017.                         Temp = SampleWindowGetSamplingRate(Window);
  2018.                         if (Temp < MINSAMPLINGRATE)
  2019.                             {
  2020.                                 SampleWindowPutSamplingRate(Window,MINSAMPLINGRATE);
  2021.                             }
  2022.                         if (Temp > MAXSAMPLINGRATE)
  2023.                             {
  2024.                                 SampleWindowPutSamplingRate(Window,MAXSAMPLINGRATE);
  2025.                             }
  2026.                     }
  2027.                 else if (Window->ActiveTextEdit == Window->NaturalFrequencyEdit)
  2028.                     {
  2029.                         double                Temp;
  2030.  
  2031.                         Temp = SampleWindowGetNaturalFrequency(Window);
  2032.                         if (Temp < MINNATURALFREQ)
  2033.                             {
  2034.                                 SampleWindowPutNaturalFrequency(Window,MINNATURALFREQ);
  2035.                             }
  2036.                         if (Temp > MAXNATURALFREQ)
  2037.                             {
  2038.                                 SampleWindowPutNaturalFrequency(Window,MAXNATURALFREQ);
  2039.                             }
  2040.                     }
  2041.                 else if (Window->ActiveTextEdit == Window->OriginEdit)
  2042.                     {
  2043.                         AdjustSampleWindowThangs(Window);
  2044.                         SampleWindowUpdator(Window);
  2045.                     }
  2046.                 else if (Window->ActiveTextEdit == Window->LoopStartEdit)
  2047.                     {
  2048.                         AdjustSampleWindowThangs(Window);
  2049.                         SampleWindowUpdator(Window);
  2050.                     }
  2051.                 else if (Window->ActiveTextEdit == Window->LoopEndEdit)
  2052.                     {
  2053.                         AdjustSampleWindowThangs(Window);
  2054.                         SampleWindowUpdator(Window);
  2055.                     }
  2056.                 else if (Window->ActiveTextEdit == Window->SelectionStartEdit)
  2057.                     {
  2058.                         char*                            StringTemp;
  2059.  
  2060.                         StringTemp = TextEditGetRawData(Window->SelectionStartEdit,"\x0a");
  2061.                         if (StringTemp != NIL)
  2062.                             {
  2063.                                 SetSampleViewSelection(Window->SampleView,
  2064.                                     StringToInteger(StringTemp,PtrSize(StringTemp)),
  2065.                                     GetSampleViewSelectEnd(Window->SampleView));
  2066.                                 SampleWindowPutSelectStart(Window);
  2067.                                 SampleWindowPutSelectEnd(Window);
  2068.                                 SampleWindowRedrawThangs(Window);
  2069.                                 ReleasePtr(StringTemp);
  2070.                             }
  2071.                     }
  2072.                 else if (Window->ActiveTextEdit == Window->SelectionEndEdit)
  2073.                     {
  2074.                         char*                            StringTemp;
  2075.  
  2076.                         StringTemp = TextEditGetRawData(Window->SelectionEndEdit,"\x0a");
  2077.                         if (StringTemp != NIL)
  2078.                             {
  2079.                                 SetSampleViewSelection(Window->SampleView,
  2080.                                     GetSampleViewSelectStart(Window->SampleView),
  2081.                                     StringToInteger(StringTemp,PtrSize(StringTemp)));
  2082.                                 SampleWindowPutSelectStart(Window);
  2083.                                 SampleWindowPutSelectEnd(Window);
  2084.                                 SampleWindowRedrawThangs(Window);
  2085.                                 ReleasePtr(StringTemp);
  2086.                             }
  2087.                     }
  2088.                 else if (Window->ActiveTextEdit == Window->ScalingEdit)
  2089.                     {
  2090.                         char*                            StringTemp;
  2091.  
  2092.                         StringTemp = TextEditGetRawData(Window->ScalingEdit,"\x0a");
  2093.                         if (StringTemp != NIL)
  2094.                             {
  2095.                                 SetSampleViewHorizontalScale(Window->SampleView,
  2096.                                     StringToLongDouble(StringTemp,PtrSize(StringTemp)));
  2097.                                 SampleWindowPutScalingFactor(Window);
  2098.                                 SampleWindowRedrawThangs(Window);
  2099.                                 UpdateSampleWindowScrollbar(Window);
  2100.                                 ReleasePtr(StringTemp);
  2101.                             }
  2102.                     }
  2103.                 else
  2104.                     {
  2105.                         EXECUTE(PRERR(AllowResume,"SampleWindowFinalizeCurrentEdit:  "
  2106.                             "an unknown text edit is in the ActiveTextEdit field."));
  2107.                     }
  2108.                 DisableTextEditSelection(Window->ActiveTextEdit);
  2109.                 Window->ActiveTextEdit = NIL;
  2110.             }
  2111.     }
  2112.  
  2113.  
  2114. /* get the editing version of the origin point */
  2115. long                                SampleWindowGetOrigin(SampleWindowRec* Window)
  2116.     {
  2117.         long                            ReturnValue;
  2118.         char*                            StringTemp;
  2119.  
  2120.         CheckPtrExistence(Window);
  2121.         StringTemp = TextEditGetRawData(Window->OriginEdit,"\x0a");
  2122.         if (StringTemp != NIL)
  2123.             {
  2124.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2125.                 ReleasePtr(StringTemp);
  2126.             }
  2127.          else
  2128.             {
  2129.                 ReturnValue = 0;
  2130.             }
  2131.         return ReturnValue;
  2132.     }
  2133.  
  2134.  
  2135. /* get the current loop start point */
  2136. long                                SampleWindowGetLoopStart(SampleWindowRec* Window)
  2137.     {
  2138.         long                            ReturnValue;
  2139.         char*                            StringTemp;
  2140.  
  2141.         CheckPtrExistence(Window);
  2142.         StringTemp = TextEditGetRawData(Window->LoopStartEdit,"\x0a");
  2143.         if (StringTemp != NIL)
  2144.             {
  2145.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2146.                 ReleasePtr(StringTemp);
  2147.             }
  2148.          else
  2149.             {
  2150.                 ReturnValue = 0;
  2151.             }
  2152.         return ReturnValue;
  2153.     }
  2154.  
  2155.  
  2156. /* get the current loop end point */
  2157. long                                SampleWindowGetLoopEnd(SampleWindowRec* Window)
  2158.     {
  2159.         long                            ReturnValue;
  2160.         char*                            StringTemp;
  2161.  
  2162.         CheckPtrExistence(Window);
  2163.         StringTemp = TextEditGetRawData(Window->LoopEndEdit,"\x0a");
  2164.         if (StringTemp != NIL)
  2165.             {
  2166.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2167.                 ReleasePtr(StringTemp);
  2168.             }
  2169.          else
  2170.             {
  2171.                 ReturnValue = 0;
  2172.             }
  2173.         return ReturnValue;
  2174.     }
  2175.  
  2176.  
  2177. /* get the sampling rate */
  2178. long                                SampleWindowGetSamplingRate(SampleWindowRec* Window)
  2179.     {
  2180.         long                            ReturnValue;
  2181.         char*                            StringTemp;
  2182.  
  2183.         CheckPtrExistence(Window);
  2184.         StringTemp = TextEditGetRawData(Window->SamplingRateEdit,"\x0a");
  2185.         if (StringTemp != NIL)
  2186.             {
  2187.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2188.                 ReleasePtr(StringTemp);
  2189.             }
  2190.          else
  2191.             {
  2192.                 ReturnValue = 0;
  2193.             }
  2194.         return ReturnValue;
  2195.     }
  2196.  
  2197.  
  2198. /* get the natural recorded pitch */
  2199. double                            SampleWindowGetNaturalFrequency(SampleWindowRec* Window)
  2200.     {
  2201.         double                        ReturnValue;
  2202.         char*                            StringTemp;
  2203.  
  2204.         CheckPtrExistence(Window);
  2205.         StringTemp = TextEditGetRawData(Window->NaturalFrequencyEdit,"\x0a");
  2206.         if (StringTemp != NIL)
  2207.             {
  2208.                 ReturnValue = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  2209.                 ReleasePtr(StringTemp);
  2210.             }
  2211.          else
  2212.             {
  2213.                 ReturnValue = 0;
  2214.             }
  2215.         return ReturnValue;
  2216.     }
  2217.  
  2218.  
  2219. /* copy the select start point from the sample view to the edit box */
  2220. void                                SampleWindowPutSelectStart(SampleWindowRec* Window)
  2221.     {
  2222.         char*                            StringTemp;
  2223.  
  2224.         CheckPtrExistence(Window);
  2225.         StringTemp = IntegerToString(GetSampleViewSelectStart(Window->SampleView));
  2226.         if (StringTemp != NIL)
  2227.             {
  2228.                 TextEditNewRawData(Window->SelectionStartEdit,StringTemp,"\x0a");
  2229.                 ReleasePtr(StringTemp);
  2230.             }
  2231.     }
  2232.  
  2233.  
  2234. /* copy the select end point from the sample view to the edit box */
  2235. void                                SampleWindowPutSelectEnd(SampleWindowRec* Window)
  2236.     {
  2237.         char*                            StringTemp;
  2238.  
  2239.         CheckPtrExistence(Window);
  2240.         StringTemp = IntegerToString(GetSampleViewSelectEnd(Window->SampleView));
  2241.         if (StringTemp != NIL)
  2242.             {
  2243.                 TextEditNewRawData(Window->SelectionEndEdit,StringTemp,"\x0a");
  2244.                 ReleasePtr(StringTemp);
  2245.             }
  2246.     }
  2247.  
  2248.  
  2249. /* copy the scaling factor from the sample view to the edit box */
  2250. void                                SampleWindowPutScalingFactor(SampleWindowRec* Window)
  2251.     {
  2252.         char*                            StringTemp;
  2253.  
  2254.         CheckPtrExistence(Window);
  2255.         StringTemp = LongDoubleToString(
  2256.             GetSampleViewHorizontalScale(Window->SampleView),10,1e-4,1e6);
  2257.         if (StringTemp != NIL)
  2258.             {
  2259.                 TextEditNewRawData(Window->ScalingEdit,StringTemp,"\x0a");
  2260.                 ReleasePtr(StringTemp);
  2261.             }
  2262.     }
  2263.  
  2264.  
  2265. /* put a new origin point into the edit box */
  2266. void                                SampleWindowPutOrigin(SampleWindowRec* Window, long NewOrigin)
  2267.     {
  2268.         char*                            StringTemp;
  2269.  
  2270.         CheckPtrExistence(Window);
  2271.         SampleWindowRedrawUnderThangs(Window);
  2272.         StringTemp = IntegerToString(NewOrigin);
  2273.         if (StringTemp != NIL)
  2274.             {
  2275.                 TextEditNewRawData(Window->OriginEdit,StringTemp,"\x0a");
  2276.                 ReleasePtr(StringTemp);
  2277.                 AdjustSampleWindowThangs(Window);
  2278.                 SampleWindowRedrawThangs(Window);
  2279.             }
  2280.     }
  2281.  
  2282.  
  2283. /* put a new loop start into the current loop start edit box */
  2284. void                                SampleWindowPutLoopStart(SampleWindowRec* Window, long NewLoopStart)
  2285.     {
  2286.         char*                            StringTemp;
  2287.  
  2288.         CheckPtrExistence(Window);
  2289.         SampleWindowRedrawUnderThangs(Window);
  2290.         StringTemp = IntegerToString(NewLoopStart);
  2291.         if (StringTemp != NIL)
  2292.             {
  2293.                 TextEditNewRawData(Window->LoopStartEdit,StringTemp,"\x0a");
  2294.                 ReleasePtr(StringTemp);
  2295.                 AdjustSampleWindowThangs(Window);
  2296.                 SampleWindowRedrawThangs(Window);
  2297.             }
  2298.     }
  2299.  
  2300.  
  2301. /* put a new loop end into the current loop end edit box */
  2302. void                                SampleWindowPutLoopEnd(SampleWindowRec* Window, long NewLoopEnd)
  2303.     {
  2304.         char*                            StringTemp;
  2305.  
  2306.         CheckPtrExistence(Window);
  2307.         SampleWindowRedrawUnderThangs(Window);
  2308.         StringTemp = IntegerToString(NewLoopEnd);
  2309.         if (StringTemp != NIL)
  2310.             {
  2311.                 TextEditNewRawData(Window->LoopEndEdit,StringTemp,"\x0a");
  2312.                 ReleasePtr(StringTemp);
  2313.                 AdjustSampleWindowThangs(Window);
  2314.                 SampleWindowRedrawThangs(Window);
  2315.             }
  2316.     }
  2317.  
  2318.  
  2319. /* put a new sampling rate into the sampling rate edit box */
  2320. void                                SampleWindowPutSamplingRate(SampleWindowRec* Window,
  2321.                                             long NewSamplingRate)
  2322.     {
  2323.         char*                            StringTemp;
  2324.  
  2325.         CheckPtrExistence(Window);
  2326.         StringTemp = IntegerToString(NewSamplingRate);
  2327.         if (StringTemp != NIL)
  2328.             {
  2329.                 TextEditNewRawData(Window->SamplingRateEdit,StringTemp,"\x0a");
  2330.                 ReleasePtr(StringTemp);
  2331.             }
  2332.     }
  2333.  
  2334.  
  2335. /* put a new natural recorded pitch into the natural frequency edit box */
  2336. void                                SampleWindowPutNaturalFrequency(SampleWindowRec* Window,
  2337.                                             double NewNaturalFrequency)
  2338.     {
  2339.         char*                            StringTemp;
  2340.  
  2341.         CheckPtrExistence(Window);
  2342.         StringTemp = LongDoubleToString(NewNaturalFrequency,13,1e-4,1e6);
  2343.         if (StringTemp != NIL)
  2344.             {
  2345.                 TextEditNewRawData(Window->NaturalFrequencyEdit,StringTemp,"\x0a");
  2346.                 ReleasePtr(StringTemp);
  2347.             }
  2348.     }
  2349.  
  2350.  
  2351. /* find the current mouse position and put it in the box */
  2352. void                                SampleWindowPutMousePosition(SampleWindowRec* Window)
  2353.     {
  2354.         char*                            StringTemp;
  2355.         OrdType                        MouseXPosition;
  2356.         OrdType                        StupidYPosition;
  2357.         long                            CalculatedOffset;
  2358.  
  2359.         CheckPtrExistence(Window);
  2360.         GetMousePosition(&MouseXPosition,&StupidYPosition);
  2361.         CalculatedOffset = ((MouseXPosition - 1 - GetSampleViewXLoc(Window->SampleView))
  2362.             * GetSampleViewHorizontalScale(Window->SampleView))
  2363.             + GetSampleViewXOffset(Window->SampleView);
  2364.         StringTemp = IntegerToString(CalculatedOffset);
  2365.         if (StringTemp != NIL)
  2366.             {
  2367.                 TextEditNewRawData(Window->MousePositionEdit,StringTemp,"\x0a");
  2368.                 ReleasePtr(StringTemp);
  2369.             }
  2370.     }
  2371.  
  2372.  
  2373. /* get the number of bits used to store the sample */
  2374. NumBitsType                    SampleWindowGetNumBits(SampleWindowRec* Window)
  2375.     {
  2376.         CheckPtrExistence(Window);
  2377.         return GetSampleViewNumBits(Window->SampleView);
  2378.     }
  2379.  
  2380.  
  2381. /* get the number of channels the sample has */
  2382. NumChannelsType            SampleWindowGetNumChannels(SampleWindowRec* Window)
  2383.     {
  2384.         CheckPtrExistence(Window);
  2385.         return GetSampleViewNumChannels(Window->SampleView);
  2386.     }
  2387.  
  2388.  
  2389. /* get a copy of the name of the sample */
  2390. char*                                SampleWindowGetNameCopy(SampleWindowRec* Window)
  2391.     {
  2392.         char*                            ReturnValue;
  2393.  
  2394.         CheckPtrExistence(Window);
  2395.         ReturnValue = TextEditGetRawData(Window->NameEdit,"\x0a");
  2396.         if (ReturnValue != NIL)
  2397.             {
  2398.                 SetTag(ReturnValue,"SampWindowNameCopy");
  2399.             }
  2400.         return ReturnValue;
  2401.     }
  2402.  
  2403.  
  2404. /* get the number of frames in the current sample */
  2405. long                                SampleWindowGetNumFrames(SampleWindowRec* Window)
  2406.     {
  2407.         CheckPtrExistence(Window);
  2408.         return GetSampleViewNumFrames(Window->SampleView);
  2409.     }
  2410.  
  2411.  
  2412. /* get a copy of the formula text */
  2413. char*                                SampleWindowGetFormulaCopy(SampleWindowRec* Window)
  2414.     {
  2415.         char*                            TextCopy;
  2416.  
  2417.         CheckPtrExistence(Window);
  2418.         TextCopy = TextEditGetRawData(Window->SampleExpressionEdit,"\x0a");
  2419.         if (TextCopy != NIL)
  2420.             {
  2421.                 SetTag(TextCopy,"SampWinFormulaCopy");
  2422.             }
  2423.         return TextCopy;
  2424.     }
  2425.  
  2426.  
  2427. /* get a copy of the left channel for a stereo sample. */
  2428. largefixedsigned*        SampleWindowGetFixedArrayLeft(SampleWindowRec* Window)
  2429.     {
  2430.         CheckPtrExistence(Window);
  2431.         return SampleViewGetChannelFixed(Window->SampleView,eLeftChannel);
  2432.     }
  2433.  
  2434.  
  2435. /* get a copy of the right channel for a stereo sample. */
  2436. largefixedsigned*        SampleWindowGetFixedArrayRight(SampleWindowRec* Window)
  2437.     {
  2438.         CheckPtrExistence(Window);
  2439.         return SampleViewGetChannelFixed(Window->SampleView,eRightChannel);
  2440.     }
  2441.  
  2442.  
  2443. /* get a copy of a mono sample. */
  2444. largefixedsigned*        SampleWindowGetFixedArrayMono(SampleWindowRec* Window)
  2445.     {
  2446.         CheckPtrExistence(Window);
  2447.         return SampleViewGetChannelFixed(Window->SampleView,eMonoChannel);
  2448.     }
  2449.  
  2450.  
  2451. /* put a new name in.  the caller is responsible for deleting NewName */
  2452. void                                SampleWindowObjectNameChange(SampleWindowRec* Window, char* NewName)
  2453.     {
  2454.         CheckPtrExistence(Window);
  2455.         CheckPtrExistence(NewName);
  2456.         /* we only use the name, but we don't dispose it */
  2457.         TextEditNewRawData(Window->NameEdit,NewName,"\x0a");
  2458.         TextEditHasBeenSaved(Window->NameEdit);
  2459.     }
  2460.  
  2461.  
  2462. /* this is the argument list for stereo sample */
  2463. static FunctionParamRec        StereoArgList[] =
  2464.     {
  2465.         {"loopstart",eInteger},
  2466.         {"loopend",eInteger},
  2467.         {"origin",eInteger},
  2468.         {"samplingrate",eInteger},
  2469.         {"naturalfrequency",eDouble},
  2470.         {"selectstart",eInteger},
  2471.         {"selectend",eInteger},
  2472.         {"leftdata",eArrayOfFixed},
  2473.         {"rightdata",eArrayOfFixed}
  2474.     };
  2475. #define STEREOARGLISTLENGTH (sizeof(StereoArgList) / sizeof(StereoArgList[0]))
  2476.  
  2477.  
  2478. /* this is the argument list for mono sample */
  2479. static FunctionParamRec        MonoArgList[] =
  2480.     {
  2481.         {"loopstart",eInteger},
  2482.         {"loopend",eInteger},
  2483.         {"origin",eInteger},
  2484.         {"samplingrate",eInteger},
  2485.         {"naturalfrequency",eDouble},
  2486.         {"selectstart",eInteger},
  2487.         {"selectend",eInteger},
  2488.         {"data",eArrayOfFixed}
  2489.     };
  2490. #define MONOARGLISTLENGTH (sizeof(MonoArgList) / sizeof(MonoArgList[0]))
  2491.  
  2492.  
  2493. /* evaluate the formula and modify the sample */
  2494. void                                SampleWindowPerformCalculation(SampleWindowRec* Window)
  2495.     {
  2496.         char*                                Blob;
  2497.         PcodeRec*                        FuncCode;
  2498.         CompileErrors                Error;
  2499.         long                                LineNumber;
  2500.         ParamStackRec*            ParamList;
  2501.         EvalErrors                    OtherError;
  2502.         OpcodeRec*                    ErrorOpcode;
  2503.         long                                OffendingInstruction;
  2504.         DataTypes                        ReturnType;
  2505.  
  2506.         CheckPtrExistence(Window);
  2507.  
  2508.         /* bring the world up to date */
  2509.         if (!MainWindowMakeUpToDateFunctions(Window->MainWindow))
  2510.             {
  2511.                 return;
  2512.             }
  2513.  
  2514.         /* prepare the text blob to be evaluated */
  2515.         Blob = TextEditGetRawData(Window->SampleExpressionEdit,"\x0a");
  2516.         if (Blob == NIL)
  2517.             {
  2518.              FailurePoint1:
  2519.                 AlertHalt("There is not enough memory available to compile the expression.",NIL);
  2520.                 return;
  2521.             }
  2522.  
  2523.         /* perform compilation */
  2524.         Error = CompileSpecialFunction((GetSampleViewNumChannels(Window->SampleView)
  2525.             == eSampleStereo) ? StereoArgList : MonoArgList,(GetSampleViewNumChannels(
  2526.             Window->SampleView) == eSampleStereo) ? STEREOARGLISTLENGTH : MONOARGLISTLENGTH,
  2527.             &LineNumber,&ReturnType,Blob,&FuncCode);
  2528.         ReleasePtr(Blob);
  2529.         if (Error != eCompileNoError)
  2530.             {
  2531.                 SetTextEditSelection(Window->SampleExpressionEdit,LineNumber - 1,0,LineNumber,0);
  2532.                 TextEditShowSelection(Window->SampleExpressionEdit);
  2533.                 AlertHalt("A compile error occurred:  _",GetCompileErrorString(Error));
  2534.                 return;
  2535.             }
  2536.  
  2537.         /* try to evaluate the code */
  2538.         ParamList = NewParamStack();
  2539.         if (ParamList == NIL)
  2540.             {
  2541.              SecondFailurePoint1:
  2542.                 DisposePcode(FuncCode);
  2543.                 AlertHalt("There is not enough memory available to evaluate the expression.",NIL);
  2544.                 return;
  2545.             }
  2546.         /* add a space for the return value */
  2547.         if (!AddIntegerToStack(ParamList,0))
  2548.             {
  2549.              SecondFailurePoint2:
  2550.                 DisposeParamStack(ParamList);
  2551.                 goto SecondFailurePoint1;
  2552.             }
  2553.         /* add the special parameters (SymbolStack order MUST be the same */
  2554.         /* order as that used for the Parameterlist) */
  2555.         /* loopstart */
  2556.         if (!AddIntegerToStack(ParamList,SampleWindowGetLoopStart(Window)))
  2557.             {
  2558.                 goto SecondFailurePoint2;
  2559.             }
  2560.         /* loopend */
  2561.         if (!AddIntegerToStack(ParamList,SampleWindowGetLoopEnd(Window)))
  2562.             {
  2563.                 goto SecondFailurePoint2;
  2564.             }
  2565.         /* origin */
  2566.         if (!AddIntegerToStack(ParamList,SampleWindowGetOrigin(Window)))
  2567.             {
  2568.                 goto SecondFailurePoint2;
  2569.             }
  2570.         /* samplingrate */
  2571.         if (!AddIntegerToStack(ParamList,SampleWindowGetSamplingRate(Window)))
  2572.             {
  2573.                 goto SecondFailurePoint2;
  2574.             }
  2575.         /* basefrequency */
  2576.         if (!AddDoubleToStack(ParamList,SampleWindowGetNaturalFrequency(Window)))
  2577.             {
  2578.                 goto SecondFailurePoint2;
  2579.             }
  2580.         /* selectstart */
  2581.         if (!AddIntegerToStack(ParamList,GetSampleViewSelectStart(Window->SampleView)))
  2582.             {
  2583.                 goto SecondFailurePoint2;
  2584.             }
  2585.         /* selectend */
  2586.         if (!AddIntegerToStack(ParamList,GetSampleViewSelectEnd(Window->SampleView)))
  2587.             {
  2588.                 goto SecondFailurePoint2;
  2589.             }
  2590.         /* data (the actual thing is added, so we don't have to dispose it!) */
  2591.         if (GetSampleViewNumChannels(Window->SampleView) == eSampleStereo)
  2592.             {
  2593.                 largefixedsigned*        DataBlock;
  2594.  
  2595.                 /* left channel */
  2596.                 DataBlock = SampleViewGetChannelFixed(Window->SampleView,eLeftChannel);
  2597.                 if (DataBlock == NIL)
  2598.                     {
  2599.                         goto SecondFailurePoint2;
  2600.                     }
  2601.                 if (!AddArrayToStack(ParamList,DataBlock))
  2602.                     {
  2603.                         ReleasePtr((char*)DataBlock);
  2604.                         goto SecondFailurePoint2;
  2605.                     }
  2606.                 /* right channel */
  2607.                 DataBlock = SampleViewGetChannelFixed(Window->SampleView,eRightChannel);
  2608.                 if (DataBlock == NIL)
  2609.                     {
  2610.                         goto SecondFailurePoint2;
  2611.                     }
  2612.                 if (!AddArrayToStack(ParamList,DataBlock))
  2613.                     {
  2614.                         ReleasePtr((char*)DataBlock);
  2615.                         goto SecondFailurePoint2;
  2616.                     }
  2617.             }
  2618.          else
  2619.             {
  2620.                 largefixedsigned*        DataBlock;
  2621.  
  2622.                 DataBlock = SampleViewGetChannelFixed(Window->SampleView,eMonoChannel);
  2623.                 if (DataBlock == NIL)
  2624.                     {
  2625.                         goto SecondFailurePoint2;
  2626.                     }
  2627.                 if (!AddArrayToStack(ParamList,DataBlock))
  2628.                     {
  2629.                         ReleasePtr((char*)DataBlock);
  2630.                         goto SecondFailurePoint2;
  2631.                     }
  2632.             }
  2633.  
  2634.         /* executing the actual code */
  2635.         OtherError = EvaluatePcode(ParamList,FuncCode,
  2636.             Window->CodeCenter,&ErrorOpcode,&OffendingInstruction,Window->MainWindow,
  2637.             &MainWindowGetSampleLeftCopy,&MainWindowGetSampleRightCopy,
  2638.             &MainWindowGetSampleMonoCopy,&MainWindowGetWaveTableFrameCount,
  2639.             &MainWindowGetWaveTableTableCount,&MainWindowGetWaveTableArray);
  2640.         if (OtherError != eEvalNoError)
  2641.             {
  2642.                 char*                    FuncNameString;
  2643.                 FuncCodeRec*    ErrorFunction;
  2644.                 MyBoolean            SuccessFlag;
  2645.  
  2646.                 /* present error message */
  2647.                 SuccessFlag = False;
  2648.                 ErrorFunction = GetFunctionFromOpcode(Window->CodeCenter,ErrorOpcode);
  2649.                 if (ErrorFunction == NIL)
  2650.                     {
  2651.                         FuncNameString = StringToBlockCopy("<anonymous>");
  2652.                     }
  2653.                  else
  2654.                     {
  2655.                         FuncNameString = CopyPtr(GetFunctionName(ErrorFunction));
  2656.                     }
  2657.                 if (FuncNameString != NIL)
  2658.                     {
  2659.                         char*                    Key;
  2660.  
  2661.                         Key = StringToBlockCopy("_");
  2662.                         if (Key != NIL)
  2663.                             {
  2664.                                 char*                    BaseMessage;
  2665.  
  2666.                                 BaseMessage = StringFromRaw("Error in function _, instruction _:  _");
  2667.                                 if (BaseMessage != NIL)
  2668.                                     {
  2669.                                         char*                    FixedMessage1;
  2670.  
  2671.                                         FixedMessage1 = ReplaceBlockCopy(BaseMessage,Key,FuncNameString);
  2672.                                         if (FixedMessage1 != NIL)
  2673.                                             {
  2674.                                                 char*                    NumberStr;
  2675.  
  2676.                                                 NumberStr = IntegerToString(OffendingInstruction);
  2677.                                                 if (NumberStr != NIL)
  2678.                                                     {
  2679.                                                         char*                    FixedMessage2;
  2680.  
  2681.                                                         FixedMessage2 = ReplaceBlockCopy(FixedMessage1,Key,NumberStr);
  2682.                                                         if (FixedMessage2 != NIL)
  2683.                                                             {
  2684.                                                                 AlertHalt(FixedMessage2,GetPcodeErrorMessage(OtherError));
  2685.                                                                 SuccessFlag = True;
  2686.                                                                 ReleasePtr(FixedMessage2);
  2687.                                                             }
  2688.                                                         ReleasePtr(NumberStr);
  2689.                                                     }
  2690.                                                 ReleasePtr(FixedMessage1);
  2691.                                             }
  2692.                                         ReleasePtr(BaseMessage);
  2693.                                     }
  2694.                                 ReleasePtr(Key);
  2695.                             }
  2696.                         ReleasePtr(FuncNameString);
  2697.                     }
  2698.                 if (!SuccessFlag)
  2699.                     {
  2700.                         AlertHalt("There is not enough memory available to show "
  2701.                             "the compile error message.",NIL);
  2702.                     }
  2703.                 DisposeParamStack(ParamList);
  2704.                 DisposePcode(FuncCode);
  2705.                 return;
  2706.             }
  2707.  
  2708.         /* add the new data */
  2709.         if (GetSampleViewNumChannels(Window->SampleView) == eSampleStereo)
  2710.             {
  2711.                 largefixedsigned*        Left;
  2712.                 largefixedsigned*        Right;
  2713.  
  2714.                 Left = (largefixedsigned*)GetStackArray(ParamList,8);
  2715.                 Right = (largefixedsigned*)GetStackArray(ParamList,9);
  2716.                 if ((Left == NIL) || (Right == NIL))
  2717.                     {
  2718.                         AlertHalt("NIL array returned; data not changed.",NIL);
  2719.                     }
  2720.                 else if (PtrSize((char*)Left) != PtrSize((char*)Right))
  2721.                     {
  2722.                         AlertHalt("Left and Right arrays are not the same size.",NIL);
  2723.                     }
  2724.                 else
  2725.                     {
  2726.                         ERROR(PtrSize((char*)Left) % sizeof(largefixedsigned) != 0,
  2727.                             PRERR(ForceAbort,"SampleWindowPerformCalculation:  array alignment error"));
  2728.                         if (!SampleViewPutStereoFixed(Window->SampleView,Left,Right))
  2729.                             {
  2730.                                 AlertHalt("There is not enough memory available to "
  2731.                                     "write data back to editor.",NIL);
  2732.                             }
  2733.                     }
  2734.             }
  2735.          else
  2736.             {
  2737.                 largefixedsigned*        Middle;
  2738.  
  2739.                 Middle = (largefixedsigned*)GetStackArray(ParamList,8);
  2740.                 if (Middle == NIL)
  2741.                     {
  2742.                         AlertHalt("NIL array returned; data not changed.",NIL);
  2743.                     }
  2744.                 else
  2745.                     {
  2746.                         ERROR(PtrSize((char*)Middle) % sizeof(largefixedsigned) != 0,
  2747.                             PRERR(ForceAbort,"SampleWindowPerformCalculation:  array alignment error"));
  2748.                         if (!SampleViewPutMonoFixed(Window->SampleView,Middle))
  2749.                             {
  2750.                                 AlertHalt("There is not enough memory available to "
  2751.                                     "write data back to editor.",NIL);
  2752.                             }
  2753.                     }
  2754.             }
  2755.         DisposeParamStack(ParamList);
  2756.         DisposePcode(FuncCode);
  2757.         SampleWindowPutSelectStart(Window);
  2758.         SampleWindowPutSelectEnd(Window);
  2759.         AdjustSampleWindowThangs(Window);
  2760.         UpdateSampleWindowScrollbar(Window);
  2761.     }
  2762.  
  2763.  
  2764. /* redraw the origin and loop point bars */
  2765. void                                SampleWindowRedrawThangs(SampleWindowRec* Window)
  2766.     {
  2767.         long                            Location;
  2768.         OrdType                        XSize;
  2769.         OrdType                        YSize;
  2770.  
  2771.         CheckPtrExistence(Window);
  2772.         XSize = GetWindowWidth(Window->ScreenID);
  2773.         YSize = GetWindowHeight(Window->ScreenID);
  2774.  
  2775.         /* draw the cute horizontal bars */
  2776.         SetClipRect(Window->ScreenID,SAMPLEEDITX,SAMPLEEDITY,SAMPLEEDITWIDTH(XSize),
  2777.             SAMPLEEDITHEIGHT(YSize) - 15);
  2778.         DrawBoxFrame(Window->ScreenID,eBlack,SAMPLEEDITX,SAMPLEEDITY,
  2779.             SAMPLEEDITWIDTH(XSize),OVERLINEHEIGHT + 1);
  2780.         DrawBoxFrame(Window->ScreenID,eBlack,SAMPLEEDITX,SAMPLEEDITY + OVERLINEHEIGHT,
  2781.             SAMPLEEDITWIDTH(XSize),OVERLINEHEIGHT + 1);
  2782.         DrawBoxFrame(Window->ScreenID,eBlack,SAMPLEEDITX,SAMPLEEDITY + (2 * OVERLINEHEIGHT),
  2783.             SAMPLEEDITWIDTH(XSize),OVERLINEHEIGHT + 1);
  2784.         DrawBoxErase(Window->ScreenID,SAMPLEEDITX + 1,SAMPLEEDITY + 1,
  2785.             SAMPLEEDITWIDTH(XSize) - 2,OVERLINEHEIGHT + 1 - 2);
  2786.         DrawBoxErase(Window->ScreenID,SAMPLEEDITX + 1,SAMPLEEDITY + OVERLINEHEIGHT + 1,
  2787.             SAMPLEEDITWIDTH(XSize) - 2,OVERLINEHEIGHT + 1 - 2);
  2788.         DrawBoxErase(Window->ScreenID,SAMPLEEDITX + 1,SAMPLEEDITY + (2 * OVERLINEHEIGHT) + 1,
  2789.             SAMPLEEDITWIDTH(XSize) - 2,OVERLINEHEIGHT + 1 - 2);
  2790.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Origin",6,SAMPLEEDITX + 5,
  2791.             SAMPLEEDITY + 1,ePlain);
  2792.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Loop Start",10,SAMPLEEDITX + 5,
  2793.             SAMPLEEDITY + 1 + OVERLINEHEIGHT,ePlain);
  2794.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Loop End",8,SAMPLEEDITX + 5,
  2795.             SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),ePlain);
  2796.  
  2797.         /* drawing origin thang */
  2798.         Location = (SampleWindowGetOrigin(Window)
  2799.             - GetSampleViewXOffset(Window->SampleView))
  2800.             / GetSampleViewHorizontalScale(Window->SampleView);
  2801.         DrawTrianglePaint(Window->ScreenID,eBlack,
  2802.             SAMPLEEDITX - 4 + Location + 1,
  2803.             SAMPLEEDITY + 1,
  2804.             SAMPLEEDITX + 4 + Location + 1,
  2805.             SAMPLEEDITY + 1,
  2806.             SAMPLEEDITX + Location + 1,
  2807.             SAMPLEEDITY + 8);
  2808.         DrawLine(Window->ScreenID,eBlack,
  2809.             SAMPLEEDITX + Location + 1,
  2810.             SAMPLEEDITY + 1,
  2811.             0,SAMPLEEDITHEIGHT(YSize) - 15);
  2812.  
  2813.         /* drawing loop start thang */
  2814.         Location = (SampleWindowGetLoopStart(Window)
  2815.             - GetSampleViewXOffset(Window->SampleView))
  2816.             / GetSampleViewHorizontalScale(Window->SampleView);
  2817.         DrawTrianglePaint(Window->ScreenID,eBlack,
  2818.             SAMPLEEDITX - 4 + Location + 1,
  2819.             SAMPLEEDITY + 1 + OVERLINEHEIGHT,
  2820.             SAMPLEEDITX + 4 + Location + 1,
  2821.             SAMPLEEDITY + 1 + OVERLINEHEIGHT,
  2822.             SAMPLEEDITX + Location + 1,
  2823.             SAMPLEEDITY + 8 + OVERLINEHEIGHT);
  2824.         DrawLine(Window->ScreenID,eBlack,
  2825.             SAMPLEEDITX + Location + 1,
  2826.             SAMPLEEDITY + 1 + OVERLINEHEIGHT,
  2827.             0,SAMPLEEDITHEIGHT(YSize) - 15 - OVERLINEHEIGHT);
  2828.  
  2829.         /* drawing loop end thang */
  2830.         Location = (SampleWindowGetLoopEnd(Window)
  2831.             - GetSampleViewXOffset(Window->SampleView))
  2832.             / GetSampleViewHorizontalScale(Window->SampleView);
  2833.         DrawTrianglePaint(Window->ScreenID,eBlack,
  2834.             SAMPLEEDITX - 4 + Location + 1,
  2835.             SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),
  2836.             SAMPLEEDITX + 4 + Location + 1,
  2837.             SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),
  2838.             SAMPLEEDITX + Location + 1,
  2839.             SAMPLEEDITY + 8 + (2 * OVERLINEHEIGHT));
  2840.         DrawLine(Window->ScreenID,eBlack,
  2841.             SAMPLEEDITX + Location + 1,
  2842.             SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),
  2843.             0,SAMPLEEDITHEIGHT(YSize) - 15 - (2 * OVERLINEHEIGHT));
  2844.     }
  2845.  
  2846.  
  2847. /* redraw the sample points that occur under the origin and loop point bars */
  2848. void                                SampleWindowRedrawUnderThangs(SampleWindowRec* Window)
  2849.     {
  2850.         long                            Location;
  2851.  
  2852.         CheckPtrExistence(Window);
  2853.         Location = (SampleWindowGetOrigin(Window)
  2854.             - GetSampleViewXOffset(Window->SampleView))
  2855.             / GetSampleViewHorizontalScale(Window->SampleView);
  2856.         RedrawSampleViewOneLine(Window->SampleView,Location + 1);
  2857.         Location = (SampleWindowGetLoopStart(Window)
  2858.             - GetSampleViewXOffset(Window->SampleView))
  2859.             / GetSampleViewHorizontalScale(Window->SampleView);
  2860.         RedrawSampleViewOneLine(Window->SampleView,Location + 1);
  2861.         Location = (SampleWindowGetLoopEnd(Window)
  2862.             - GetSampleViewXOffset(Window->SampleView))
  2863.             / GetSampleViewHorizontalScale(Window->SampleView);
  2864.         RedrawSampleViewOneLine(Window->SampleView,Location + 1);
  2865.     }
  2866.  
  2867.  
  2868. /* make sure the loop start and end are within the proper ranges */
  2869. void                                AdjustSampleWindowThangs(SampleWindowRec* Window)
  2870.     {
  2871.         long                            NumFrames;
  2872.  
  2873.         CheckPtrExistence(Window);
  2874.         NumFrames = GetSampleViewNumFrames(Window->SampleView);
  2875.         /* loop start check */
  2876.         if (SampleWindowGetLoopStart(Window) > NumFrames)
  2877.             {
  2878.                 SampleWindowPutLoopStart(Window,NumFrames);
  2879.             }
  2880.         if (SampleWindowGetLoopStart(Window) < 0)
  2881.             {
  2882.                 SampleWindowPutLoopStart(Window,0);
  2883.             }
  2884.         /* loop end check */
  2885.         if (SampleWindowGetLoopEnd(Window) > NumFrames)
  2886.             {
  2887.                 SampleWindowPutLoopEnd(Window,NumFrames);
  2888.             }
  2889.         if (SampleWindowGetLoopEnd(Window) < SampleWindowGetLoopStart(Window))
  2890.             {
  2891.                 SampleWindowPutLoopEnd(Window,SampleWindowGetLoopStart(Window));
  2892.             }
  2893.     }
  2894.  
  2895.  
  2896. /* recalculate the scroll bar displacements and redraw it */
  2897. void                                UpdateSampleWindowScrollbar(SampleWindowRec* Window)
  2898.     {
  2899.         CheckPtrExistence(Window);
  2900.         SetMaxScrollIndex(Window->Scrollbar,GetSampleViewNumFrames(Window->SampleView)
  2901.             - GetSampleViewNumVisibleFrames(Window->SampleView));
  2902.         SetScrollIndex(Window->Scrollbar,GetSampleViewXOffset(Window->SampleView));
  2903.         RedrawScrollBar(Window->Scrollbar);
  2904.     }
  2905.  
  2906.  
  2907. static void                    SampleWindowScrollHook(long Parameter, ScrollType How,
  2908.                                             SampleWindowRec* Window)
  2909.     {
  2910.         CheckPtrExistence(Window);
  2911.         switch (How)
  2912.             {
  2913.                 case eScrollToPosition:
  2914.                     SetSampleViewXOffset(Window->SampleView,Parameter);
  2915.                     UpdateSampleWindowScrollbar(Window);
  2916.                     SampleWindowRedrawThangs(Window);
  2917.                     break;
  2918.                 case eScrollPageMinus:
  2919.                     SetSampleViewXOffset(Window->SampleView,
  2920.                         GetSampleViewXOffset(Window->SampleView)
  2921.                         - ((7 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2922.                     UpdateSampleWindowScrollbar(Window);
  2923.                     SampleWindowRedrawThangs(Window);
  2924.                     break;
  2925.                 case eScrollPagePlus:
  2926.                     SetSampleViewXOffset(Window->SampleView,
  2927.                         GetSampleViewXOffset(Window->SampleView)
  2928.                         + ((7 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2929.                     UpdateSampleWindowScrollbar(Window);
  2930.                     SampleWindowRedrawThangs(Window);
  2931.                     break;
  2932.                 case eScrollLineMinus:
  2933.                     SetSampleViewXOffset(Window->SampleView,
  2934.                         GetSampleViewXOffset(Window->SampleView)
  2935.                         - ((1 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2936.                     UpdateSampleWindowScrollbar(Window);
  2937.                     SampleWindowRedrawThangs(Window);
  2938.                     break;
  2939.                 case eScrollLinePlus:
  2940.                     SetSampleViewXOffset(Window->SampleView,
  2941.                         GetSampleViewXOffset(Window->SampleView)
  2942.                         + ((1 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2943.                     UpdateSampleWindowScrollbar(Window);
  2944.                     SampleWindowRedrawThangs(Window);
  2945.                     break;
  2946.                 default:
  2947.                     EXECUTE(PRERR(AllowResume,"SampleWindowScrollHook:  Unknown scroll opcode"));
  2948.                     break;
  2949.             }
  2950.     }
  2951.  
  2952.  
  2953. /* handle a mouse down in the sample edit area. */
  2954. void                                SampleWindowDoMouseDownInEdit(SampleWindowRec* Window, OrdType OrigX,
  2955.                                             OrdType OrigY)
  2956.     {
  2957.         long                            BaseSelectionStart;
  2958.         long                            BaseSelectionEnd;
  2959.         OrdType                        WhereX;
  2960.         OrdType                        WhereY;
  2961.  
  2962.         CheckPtrExistence(Window);
  2963.  
  2964.         /* first, check for a scrollbar operation */
  2965.         if (ScrollHitTest(Window->Scrollbar,OrigX,OrigY))
  2966.             {
  2967.                 ScrollHitProc(Window->Scrollbar,CheckModifiers(),OrigX,
  2968.                     OrigY,Window,(void (*)(long,ScrollType,void*))&SampleWindowScrollHook);
  2969.                 return;
  2970.             }
  2971.         /* now check for a parameter (origin, loop...) drag operation */
  2972.         if (OrigY < SAMPLEEDITY + (3 * OVERLINEHEIGHT))
  2973.             {
  2974.                 MyBoolean                DoingOrigin = False;
  2975.                 MyBoolean                DoingLoopStart = False;
  2976.                 MyBoolean                DoingLoopEnd = False;
  2977.  
  2978.                 if (OrigY < SAMPLEEDITY + OVERLINEHEIGHT)
  2979.                     {
  2980.                         DoingOrigin = True;
  2981.                     }
  2982.                 else if (OrigY < SAMPLEEDITY + (2 * OVERLINEHEIGHT))
  2983.                     {
  2984.                         DoingLoopStart = True;
  2985.                     }
  2986.                 else
  2987.                     {
  2988.                         DoingLoopEnd = True;
  2989.                     }
  2990.                 while (eMouseUp != GetAnEvent(&WhereX,&WhereY,NIL,NIL,NIL,NIL))
  2991.                     {
  2992.                         long                CurrentMouseX;
  2993.  
  2994.                         SampleWindowPutMousePosition(Window);
  2995.                         WhereX -= (GetSampleViewXLoc(Window->SampleView) + 1);
  2996.                         if (WhereX < 0)
  2997.                             {
  2998.                                 /* drag left */
  2999.                                 SampleWindowScrollHook(0,eScrollLineMinus,Window);
  3000.                             }
  3001.                         if (WhereX > SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  3002.                             {
  3003.                                 /* drag right */
  3004.                                 SampleWindowScrollHook(0,eScrollLinePlus,Window);
  3005.                             }
  3006.                         CurrentMouseX = WhereX * GetSampleViewHorizontalScale(Window->SampleView)
  3007.                             + GetSampleViewXOffset(Window->SampleView);
  3008.                         /* perform modification & redraw */
  3009.                         SampleWindowRedrawUnderThangs(Window);
  3010.                         if (DoingOrigin)
  3011.                             {
  3012.                                 SampleWindowPutOrigin(Window,CurrentMouseX);
  3013.                             }
  3014.                         else if (DoingLoopStart)
  3015.                             {
  3016.                                 SampleWindowPutLoopStart(Window,CurrentMouseX);
  3017.                             }
  3018.                         else /* if (DoingLoopEnd) */
  3019.                             {
  3020.                                 SampleWindowPutLoopEnd(Window,CurrentMouseX);
  3021.                             }
  3022.                         AdjustSampleWindowThangs(Window);
  3023.                         SampleWindowRedrawThangs(Window);
  3024.                     }
  3025.                 SampleWindowPutMousePosition(Window);
  3026.                 return;
  3027.             }
  3028.  
  3029.         /* convert mouse location into local coordinates */
  3030.         WhereX = OrigX - (GetSampleViewXLoc(Window->SampleView) + 1);
  3031.  
  3032.         /* set up base selection */
  3033.         if ((CheckModifiers() & eShiftKey) != 0)
  3034.             {
  3035.                 /* extending existing selection */
  3036.                 BaseSelectionStart = GetSampleViewSelectStart(Window->SampleView);
  3037.                 BaseSelectionEnd = GetSampleViewSelectEnd(Window->SampleView);
  3038.             }
  3039.          else
  3040.             {
  3041.                 /* replacing existing selection */
  3042.                 BaseSelectionStart = WhereX * GetSampleViewHorizontalScale(Window->SampleView)
  3043.                     + GetSampleViewXOffset(Window->SampleView);
  3044.                 BaseSelectionEnd = BaseSelectionStart;
  3045.                 SetSampleViewSelection(Window->SampleView,BaseSelectionStart,BaseSelectionEnd);
  3046.             }
  3047.  
  3048.         /* track mouse */
  3049.         while (eMouseUp != GetAnEvent(&WhereX,&WhereY,NIL,NIL,NIL,NIL))
  3050.             {
  3051.                 long                CurrentMouseX;
  3052.                 long                NewTempSelectStart;
  3053.                 long                NewTempSelectEnd;
  3054.  
  3055.                 SampleWindowPutMousePosition(Window);
  3056.                 SampleWindowPutSelectStart(Window);
  3057.                 SampleWindowPutSelectEnd(Window);
  3058.                 WhereX -= (GetSampleViewXLoc(Window->SampleView) + 1);
  3059.                 if (WhereX < 0)
  3060.                     {
  3061.                         /* drag left */
  3062.                         SampleWindowScrollHook(0,eScrollLineMinus,Window);
  3063.                     }
  3064.                 if (WhereX > SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  3065.                     {
  3066.                         /* drag right */
  3067.                         SampleWindowScrollHook(0,eScrollLinePlus,Window);
  3068.                     }
  3069.                 /* find union of selection areas */
  3070.                 CurrentMouseX = WhereX * GetSampleViewHorizontalScale(Window->SampleView)
  3071.                     + GetSampleViewXOffset(Window->SampleView);
  3072.                 NewTempSelectStart = BaseSelectionStart;
  3073.                 NewTempSelectEnd = BaseSelectionEnd;
  3074.                 if (CurrentMouseX < NewTempSelectStart)
  3075.                     {
  3076.                         NewTempSelectStart = CurrentMouseX;
  3077.                     }
  3078.                 if (CurrentMouseX > NewTempSelectEnd)
  3079.                     {
  3080.                         NewTempSelectEnd = CurrentMouseX;
  3081.                     }
  3082.                 /* set new selection region */
  3083.                 SetSampleViewSelection(Window->SampleView,NewTempSelectStart,NewTempSelectEnd);
  3084.             }
  3085.         /* now we leave, and the selection has been adjusted as desired */
  3086.         SampleWindowPutMousePosition(Window);
  3087.         SampleWindowPutSelectStart(Window);
  3088.         SampleWindowPutSelectEnd(Window);
  3089.         SampleWindowRedrawThangs(Window);
  3090.     }
  3091.  
  3092.  
  3093. /* find out which loop the sample is editing */
  3094. SampleLoops                    SampleWindowEditingWhichLoop(SampleWindowRec* Window)
  3095.     {
  3096.         CheckPtrExistence(Window);
  3097.         return Window->EditingWhichLoop;
  3098.     }
  3099.  
  3100.  
  3101. /* force the sample data to be written back to the object.  this does not write */
  3102. /* back any other data. */
  3103. MyBoolean                        SampleWindowForceUpdateSampleObjectData(SampleWindowRec* Window)
  3104.     {
  3105.         SampleStorageActualRec*        TheStuff;
  3106.  
  3107.         TheStuff = SampleWindowGetTheSampleActual(Window);
  3108.         if (TheStuff != NIL)
  3109.             {
  3110.                 /* this call disposes the sample for us */
  3111.                 SampleObjectPutNewSample(Window->SampleObject,TheStuff);
  3112.                 SampleViewHasBeenSaved(Window->SampleView);
  3113.                 return True;
  3114.             }
  3115.          else
  3116.             {
  3117.                 return False;
  3118.             }
  3119.     }
  3120.  
  3121.  
  3122. /* get the raw data for the sample, in char or short form.  the caller must NOT */
  3123. /* dispose of this data, and it will become invalid if any modifications are made */
  3124. /* to the sample.  This is basically SampleObjectGetRawData() with a call to */
  3125. /* SampleWindowForceUpdateSampleObjectData() to keep everything consistent. */
  3126. char*                                SampleWindowGetRawData(SampleWindowRec* Window)
  3127.     {
  3128.         /* this is a weird routine:  it forces the sample object to be up to date, then */
  3129.         /* returns the sample object's idea of what the sample is.  Be careful because */
  3130.         /* it can lead to recursive infinite loops */
  3131.         CheckPtrExistence(Window);
  3132.         if (!SampleWindowForceUpdateSampleObjectData(Window))
  3133.             {
  3134.                 /* failed */
  3135.             }
  3136.         return SampleObjectGetRawData(Window->SampleObject);
  3137.     }
  3138.  
  3139.  
  3140. /* get the raw fixedpoint data for the sample. */
  3141. largefixedsigned*        SampleWindowGetRawFixedPointData(SampleWindowRec* Window)
  3142.     {
  3143.         CheckPtrExistence(Window);
  3144.         return SampleViewGetActualRawData(Window->SampleView);
  3145.     }
  3146.  
  3147.  
  3148. /* the document title has changed, so change the window title bar.  caller is */
  3149. /* responsible for deleting the string, which should be non-null-terminated. */
  3150. void                                SampleWindowGlobalNameChange(SampleWindowRec* Window,
  3151.                                             char* NewFilename)
  3152.     {
  3153.         char*                            LocalNameCopy;
  3154.  
  3155.         CheckPtrExistence(Window);
  3156.         CheckPtrExistence(NewFilename);
  3157.         LocalNameCopy = TextEditGetRawData(Window->NameEdit,SYSTEMLINEFEED);
  3158.         if (LocalNameCopy != NIL)
  3159.             {
  3160.                 char*                            SeparatorString;
  3161.  
  3162.                 SeparatorString = StringToBlockCopy(":  ");
  3163.                 if (SeparatorString != NIL)
  3164.                     {
  3165.                         char*                            LeftHalfOfString;
  3166.  
  3167.                         LeftHalfOfString = ConcatBlockCopy(NewFilename,SeparatorString);
  3168.                         if (LeftHalfOfString != NIL)
  3169.                             {
  3170.                                 char*                            TotalString;
  3171.  
  3172.                                 TotalString = ConcatBlockCopy(LeftHalfOfString,LocalNameCopy);
  3173.                                 if (TotalString != NIL)
  3174.                                     {
  3175.                                         char*                            NullTerminatedString;
  3176.  
  3177.                                         NullTerminatedString = BlockToStringCopy(TotalString);
  3178.                                         if (NullTerminatedString != NIL)
  3179.                                             {
  3180.                                                 SetWindowName(Window->ScreenID,NullTerminatedString);
  3181.                                                 ChangeItemName(Window->MyMenuItem,NullTerminatedString);
  3182.                                                 ReleasePtr(NullTerminatedString);
  3183.                                             }
  3184.                                         ReleasePtr(TotalString);
  3185.                                     }
  3186.                                 ReleasePtr(LeftHalfOfString);
  3187.                             }
  3188.                         ReleasePtr(SeparatorString);
  3189.                     }
  3190.                 ReleasePtr(LocalNameCopy);
  3191.             }
  3192.     }
  3193.  
  3194.  
  3195. /* force the window title bar to be renamed even if the document hasn't been renamed */
  3196. void                                SampleWindowResetTitlebar(SampleWindowRec* Window)
  3197.     {
  3198.         char*                            DocumentName;
  3199.  
  3200.         CheckPtrExistence(Window);
  3201.         DocumentName = GetCopyOfDocumentName(Window->MainWindow);
  3202.         if (DocumentName != NIL)
  3203.             {
  3204.                 SampleWindowGlobalNameChange(Window,DocumentName);
  3205.                 ReleasePtr(DocumentName);
  3206.             }
  3207.     }
  3208.  
  3209.  
  3210. /* get an 'actual' sample object that contains the same data as the window.  the */
  3211. /* caller must delete the object. */
  3212. struct SampleStorageActualRec*    SampleWindowGetTheSampleActual(SampleWindowRec* Window)
  3213.     {
  3214.         SampleStorageActualRec*        TheStuff;
  3215.         NumChannelsType                        NumChannels;
  3216.         long                                            Limit;
  3217.         long                                            Scan;
  3218.  
  3219.         NumChannels = SampleWindowGetNumChannels(Window);
  3220.         Limit = GetSampleViewNumFrames(Window->SampleView);
  3221.         TheStuff = NewSampleStorageActual(SampleWindowGetNumBits(Window),
  3222.             NumChannels,Limit);
  3223.         if (TheStuff != NIL)
  3224.             {
  3225.                 switch (NumChannels)
  3226.                     {
  3227.                         default:
  3228.                             EXECUTE(PRERR(ForceAbort,"SampleWindowGetTheSampleActual:  bad num bits"));
  3229.                             break;
  3230.                         case eSampleMono:
  3231.                             for (Scan = 0; Scan < Limit; Scan += 1)
  3232.                                 {
  3233.                                     SetSampleStorageActualValue(TheStuff,Scan,eMonoChannel,
  3234.                                         GetSampleViewValue(Window->SampleView,Scan,eMonoChannel));
  3235.                                 }
  3236.                             break;
  3237.                         case eSampleStereo:
  3238.                             for (Scan = 0; Scan < Limit; Scan += 1)
  3239.                                 {
  3240.                                     SetSampleStorageActualValue(TheStuff,Scan,eLeftChannel,
  3241.                                         GetSampleViewValue(Window->SampleView,Scan,eLeftChannel));
  3242.                                     SetSampleStorageActualValue(TheStuff,Scan,eRightChannel,
  3243.                                         GetSampleViewValue(Window->SampleView,Scan,eRightChannel));
  3244.                                 }
  3245.                             break;
  3246.                     }
  3247.             }
  3248.         return TheStuff;
  3249.     }
  3250.  
  3251.  
  3252. /* force ALL modified data to be written back to the object.  this includes */
  3253. /* calling SampleWindowForceUpdateSampleObjectData() */
  3254. MyBoolean                        SampleWindowWritebackModifiedData(SampleWindowRec* Window)
  3255.     {
  3256.         MyBoolean                    SuccessFlag = True;
  3257.  
  3258.         CheckPtrExistence(Window);
  3259.  
  3260.         /* if an edit is being edited, then finalize it */
  3261.         SampleWindowFinalizeCurrentEdit(Window);
  3262.  
  3263.         if (TextEditDoesItNeedToBeSaved(Window->NameEdit))
  3264.             {
  3265.                 char*                            TempString;
  3266.  
  3267.                 TempString = SampleWindowGetNameCopy(Window);
  3268.                 if (TempString != NIL)
  3269.                     {
  3270.                         SampleObjectNewName(Window->SampleObject,TempString);
  3271.                         TextEditHasBeenSaved(Window->NameEdit);
  3272.                     }
  3273.                  else
  3274.                     {
  3275.                         SuccessFlag = False;
  3276.                     }
  3277.             }
  3278.  
  3279.         if (TextEditDoesItNeedToBeSaved(Window->OriginEdit))
  3280.             {
  3281.                 SampleObjectPutOrigin(Window->SampleObject,SampleWindowGetOrigin(Window));
  3282.                 TextEditHasBeenSaved(Window->OriginEdit);
  3283.             }
  3284.  
  3285.         if (TextEditDoesItNeedToBeSaved(Window->LoopStartEdit))
  3286.             {
  3287.                 switch (Window->EditingWhichLoop)
  3288.                     {
  3289.                         default:
  3290.                             EXECUTE(PRERR(ForceAbort,
  3291.                                 "DisposeSampleWindow:  bad value in EditingWhichLoop"));
  3292.                             break;
  3293.                         case eSampleLoop1:
  3294.                             SampleObjectPutLoopStart1(Window->SampleObject,
  3295.                                 SampleWindowGetLoopStart(Window));
  3296.                             break;
  3297.                         case eSampleLoop2:
  3298.                             SampleObjectPutLoopStart2(Window->SampleObject,
  3299.                                 SampleWindowGetLoopStart(Window));
  3300.                             break;
  3301.                         case eSampleLoop3:
  3302.                             SampleObjectPutLoopStart3(Window->SampleObject,
  3303.                                 SampleWindowGetLoopStart(Window));
  3304.                             break;
  3305.                     }
  3306.                 TextEditHasBeenSaved(Window->LoopStartEdit);
  3307.             }
  3308.  
  3309.         if (TextEditDoesItNeedToBeSaved(Window->LoopEndEdit))
  3310.             {
  3311.                 switch (Window->EditingWhichLoop)
  3312.                     {
  3313.                         default:
  3314.                             EXECUTE(PRERR(ForceAbort,
  3315.                                 "DisposeSampleWindow:  bad value in EditingWhichLoop"));
  3316.                             break;
  3317.                         case eSampleLoop1:
  3318.                             SampleObjectPutLoopEnd1(Window->SampleObject,
  3319.                                 SampleWindowGetLoopEnd(Window));
  3320.                             break;
  3321.                         case eSampleLoop2:
  3322.                             SampleObjectPutLoopEnd2(Window->SampleObject,
  3323.                                 SampleWindowGetLoopEnd(Window));
  3324.                             break;
  3325.                         case eSampleLoop3:
  3326.                             SampleObjectPutLoopEnd3(Window->SampleObject,
  3327.                                 SampleWindowGetLoopEnd(Window));
  3328.                             break;
  3329.                     }
  3330.                 TextEditHasBeenSaved(Window->LoopEndEdit);
  3331.             }
  3332.  
  3333.         if (TextEditDoesItNeedToBeSaved(Window->SamplingRateEdit))
  3334.             {
  3335.                 SampleObjectPutSamplingRate(Window->SampleObject,
  3336.                     SampleWindowGetSamplingRate(Window));
  3337.                 TextEditHasBeenSaved(Window->SamplingRateEdit);
  3338.             }
  3339.  
  3340.         if (TextEditDoesItNeedToBeSaved(Window->NaturalFrequencyEdit))
  3341.             {
  3342.                 SampleObjectPutNaturalFrequency(Window->SampleObject,
  3343.                     SampleWindowGetNaturalFrequency(Window));
  3344.                 TextEditHasBeenSaved(Window->NaturalFrequencyEdit);
  3345.             }
  3346.  
  3347.         if (TextEditDoesItNeedToBeSaved(Window->SampleExpressionEdit))
  3348.             {
  3349.                 char*                        TextTemp;
  3350.  
  3351.                 TextTemp = SampleWindowGetFormulaCopy(Window);
  3352.                 if (TextTemp != NIL)
  3353.                     {
  3354.                         SampleObjectNewFormula(Window->SampleObject,TextTemp);
  3355.                         TextEditHasBeenSaved(Window->SampleExpressionEdit);
  3356.                     }
  3357.                  else
  3358.                     {
  3359.                         SuccessFlag = False;
  3360.                     }
  3361.             }
  3362.  
  3363.         if (!SampleWindowForceUpdateSampleObjectData(Window))
  3364.             {
  3365.                 SuccessFlag = False;
  3366.             }
  3367.  
  3368.         return SuccessFlag;
  3369.     }
  3370.  
  3371.  
  3372. /* this routine updates the loop points when some number of sample frames are */
  3373. /* inserted or removed from the specified point.  (for removal, NumAddedFrames < 0) */
  3374. /* it does NOT do any redrawing of the points. */
  3375. void                                SampleWindowShiftPoints(SampleWindowRec* Window, long Position,
  3376.                                             long NumAddedFrames)
  3377.     {
  3378.         long                            Temp;
  3379.  
  3380.         CheckPtrExistence(Window);
  3381.         SampleWindowFinalizeCurrentEdit(Window);
  3382.  
  3383.         Temp = SampleObjectGetOrigin(Window->SampleObject);
  3384.         if (Temp >= Position)
  3385.             {
  3386.                 SampleObjectPutOrigin(Window->SampleObject,Temp + NumAddedFrames);
  3387.                 SampleWindowPutOrigin(Window,Temp + NumAddedFrames);
  3388.             }
  3389.  
  3390.         Temp = SampleObjectGetLoopStart1(Window->SampleObject);
  3391.         if (Temp >= Position)
  3392.             {
  3393.                 SampleObjectPutLoopStart1(Window->SampleObject,Temp + NumAddedFrames);
  3394.                 if (Window->EditingWhichLoop == eSampleLoop1)
  3395.                     {
  3396.                         SampleWindowPutLoopStart(Window,Temp + NumAddedFrames);
  3397.                     }
  3398.             }
  3399.  
  3400.         Temp = SampleObjectGetLoopStart2(Window->SampleObject);
  3401.         if (Temp >= Position)
  3402.             {
  3403.                 SampleObjectPutLoopStart2(Window->SampleObject,Temp + NumAddedFrames);
  3404.                 if (Window->EditingWhichLoop == eSampleLoop2)
  3405.                     {
  3406.                         SampleWindowPutLoopStart(Window,Temp + NumAddedFrames);
  3407.                     }
  3408.             }
  3409.  
  3410.         Temp = SampleObjectGetLoopStart3(Window->SampleObject);
  3411.         if (Temp >= Position)
  3412.             {
  3413.                 SampleObjectPutLoopStart3(Window->SampleObject,Temp + NumAddedFrames);
  3414.                 if (Window->EditingWhichLoop == eSampleLoop3)
  3415.                     {
  3416.                         SampleWindowPutLoopStart(Window,Temp + NumAddedFrames);
  3417.                     }
  3418.             }
  3419.  
  3420.         Temp = SampleObjectGetLoopEnd1(Window->SampleObject);
  3421.         if (Temp >= Position)
  3422.             {
  3423.                 SampleObjectPutLoopEnd1(Window->SampleObject,Temp + NumAddedFrames);
  3424.                 if (Window->EditingWhichLoop == eSampleLoop1)
  3425.                     {
  3426.                         SampleWindowPutLoopEnd(Window,Temp + NumAddedFrames);
  3427.                     }
  3428.             }
  3429.  
  3430.         Temp = SampleObjectGetLoopEnd2(Window->SampleObject);
  3431.         if (Temp >= Position)
  3432.             {
  3433.                 SampleObjectPutLoopEnd2(Window->SampleObject,Temp + NumAddedFrames);
  3434.                 if (Window->EditingWhichLoop == eSampleLoop2)
  3435.                     {
  3436.                         SampleWindowPutLoopEnd(Window,Temp + NumAddedFrames);
  3437.                     }
  3438.             }
  3439.  
  3440.         Temp = SampleObjectGetLoopEnd3(Window->SampleObject);
  3441.         if (Temp >= Position)
  3442.             {
  3443.                 SampleObjectPutLoopEnd3(Window->SampleObject,Temp + NumAddedFrames);
  3444.                 if (Window->EditingWhichLoop == eSampleLoop3)
  3445.                     {
  3446.                         SampleWindowPutLoopEnd(Window,Temp + NumAddedFrames);
  3447.                     }
  3448.             }
  3449.     }
  3450.