home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Utilities / MandelNet / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  22.5 KB  |  1,308 lines  |  [TEXT/KAHL]

  1. #include "mandel.h"
  2. #include <math.h>
  3.  
  4.  
  5.  
  6. pascal void HiliteDefaultButton(theDPtr, whichItem)
  7. DialogPtr theDPtr; int whichItem;
  8. {
  9. int            type;
  10. Handle        theItem;
  11. Rect        theRect;
  12.  
  13.     GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
  14.     PenNormal();
  15.     PenSize(3,3);
  16.     InsetRect(&theRect,-4,-4);
  17.     FrameRoundRect(&theRect,16,16);
  18.     PenSize(1,1);
  19. }
  20.  
  21.  
  22.  
  23. CenterWindow(wPtr)
  24. WindowPtr    wPtr;
  25. {
  26. int        screenWidth,screenHeight,windowWidth,windowHeight;
  27.  
  28.     screenWidth = screenBits.bounds.right - screenBits.bounds.left;
  29.     screenHeight = screenBits.bounds.bottom - screenBits.bounds.top - 20;
  30.     windowWidth = wPtr->portRect.right - wPtr->portRect.left;
  31.     windowHeight = wPtr->portRect.bottom - wPtr->portRect.top;
  32.     
  33.     if (wPtr && windowWidth<screenWidth && windowHeight<screenHeight)
  34.         MoveWindow(wPtr,screenBits.bounds.left + (screenWidth - windowWidth)/2,screenBits.bounds.top + 20 + (screenHeight - windowHeight)/2,FALSE);
  35. }
  36.  
  37.  
  38.  
  39. FatalError(p0)
  40. char    *p0;
  41. {
  42.     ErrorAlert(p0);
  43.     ExitToShell();
  44. }
  45.  
  46.  
  47.  
  48. OptionKey()
  49. {
  50. char    theKeyMap[16];
  51.  
  52.     GetKeys(theKeyMap);
  53.     
  54.     if (theKeyMap[7] & 0x04)
  55.         return(TRUE);
  56.     else
  57.         return(FALSE);
  58. }
  59.  
  60.  
  61.  
  62. ErrorAlert(p0)
  63. char    *p0;
  64. {
  65. GrafPtr            oldPort;
  66. DialogPtr        theDPtr,tempDPtr;
  67. int                itemHit, type;
  68. Handle            theItem;
  69. Rect            theRect;
  70. EventRecord        theEvent;
  71.  
  72.     GetPort(&oldPort);
  73.     
  74.     InitCursor();
  75.     
  76.     if (!(theDPtr = GetNewDialog(errorDLOG, NULL,(WindowPtr) -1L)))
  77.     {
  78.         SysBeep(1);
  79.         ExitToShell();
  80.     }
  81.     
  82.     SetPort(theDPtr);
  83.     
  84.     CenterWindow(theDPtr);
  85.     ShowWindow(theDPtr);
  86.     
  87.     ((DialogPeek) theDPtr)->aDefItem = 1;
  88.     
  89.     GetDItem(theDPtr, 3, &type, &theItem, &theRect);
  90.     SetDItem(theDPtr, 3, type, HiliteDefaultButton, &theRect);
  91.     
  92.     GetDItem(theDPtr, 2, &type, &theItem, &theRect);
  93.     SetIText(theItem, p0);
  94.     
  95.     if (GetNextEvent(updateMask,&theEvent) && theEvent.message == (long) theDPtr)
  96.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  97.     
  98.     SysBeep(1);
  99.     
  100.     do 
  101.     {
  102.         itemHit = 0;
  103.         
  104.         while (!GetNextEvent(everyEvent,&theEvent));
  105.         
  106.         if (theEvent.what==keyDown || theEvent.what==autoKey)
  107.         {
  108.             if ((theEvent.message & charCodeMask)=='\r' || (theEvent.message & charCodeMask)==0x03)
  109.                 itemHit = ((DialogPeek) theDPtr)->aDefItem;
  110.             else
  111.                 SysBeep(1);
  112.         }
  113.         
  114.         else
  115.         {
  116.             tempDPtr = (DialogPtr) 0L;
  117.             
  118.             if (!IsDialogEvent(&theEvent) || !DialogSelect(&theEvent,&tempDPtr,&itemHit) || tempDPtr!=theDPtr)
  119.                 itemHit = 0;
  120.         }
  121.         
  122.     } while (itemHit!=1);
  123.     
  124.     DisposDialog(theDPtr);
  125.     SetPort(oldPort);
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. GetInput(thePrompt, theText)
  133. char    *thePrompt,*theText;
  134. {
  135. DialogPtr    theDPtr;
  136. int            itemHit, type;
  137. Handle        theItem;
  138. Rect        theRect;
  139. GrafPtr        oldPort;
  140.  
  141.     GetPort(&oldPort);
  142.  
  143.     ParamText(thePrompt,"\p","\p","\p");
  144.     
  145.     theDPtr = GetNewDialog(inputDLOG, NULL,(WindowPtr) -1L);
  146.     
  147.     if (theDPtr == 0L)
  148.         FatalError("\p Can't allocate memory for window");
  149.     
  150.     SetPort(theDPtr);
  151.     
  152.     CenterWindow(theDPtr);
  153.     ShowWindow(theDPtr);
  154.     
  155.     ((DialogPeek) theDPtr)->aDefItem = 1;
  156.     
  157.     GetDItem(theDPtr, 5, &type, &theItem, &theRect);
  158.     SetDItem(theDPtr, 5, type, HiliteDefaultButton, &theRect);
  159.     
  160.     GetDItem(theDPtr, 4, &type, &theItem, &theRect);
  161.     CtoPstr(theText);
  162.     SetIText(theItem, theText);
  163.     SelIText(theDPtr, 4, 0, 256);
  164.     PtoCstr(theText);
  165.     
  166.     do 
  167.     {
  168.         ModalDialog(0L,&itemHit);
  169.     }    
  170.     while ((itemHit!=1)&&(itemHit!=2));
  171.     
  172.     
  173.     GetDItem(theDPtr, 4, &type, &theItem, &theRect);
  174.     
  175.     if (itemHit==1)
  176.     {
  177.         GetIText(theItem,  theText);
  178.         PtoCstr(theText);
  179.         
  180.         if (theText[0] != '\000')
  181.             itemHit = 0;
  182.         
  183.         else
  184.             itemHit = -1;
  185.     }
  186.     else
  187.     {
  188.         theText[0] = '\000';
  189.         itemHit = -1;
  190.     }
  191.     
  192.     DisposDialog(theDPtr);
  193.     SetPort(oldPort);
  194.     
  195.     return(itemHit);
  196. }
  197.  
  198.  
  199.  
  200.  
  201. SetUpMenus()
  202. {    
  203.     myMenus[appleMenu] = GetMenu(appleID);
  204.     AddResMenu(myMenus[appleMenu],'DRVR');
  205.     myMenus[fileMenu] = GetMenu(fileID);
  206.     myMenus[editMenu] = GetMenu(editID);
  207.     myMenus[modeMenu] = GetMenu(modeID);
  208.     myMenus[graphMenu] = GetMenu(graphID);
  209.     myMenus[controlMenu] = GetMenu(controlID);
  210.     
  211.     InsertMenu(myMenus[appleMenu],0);
  212.     InsertMenu(myMenus[fileMenu],0);
  213.     InsertMenu(myMenus[editMenu],0);
  214.     InsertMenu(myMenus[modeMenu],0);
  215.     InsertMenu(myMenus[graphMenu],0);
  216.     
  217.     DrawMenuBar();
  218. }
  219.  
  220.  
  221.  
  222. int DoCommand(mResult)
  223. long mResult;
  224. {
  225. int            theItem;
  226. char        tempString[256];
  227. WindowPeek     wPtr;
  228.     
  229.     theItem = LoWord(mResult);
  230.     
  231.     switch (HiWord(mResult)) 
  232.     {
  233.         case appleID:
  234.             if (theItem == aboutMe)
  235.                 DoAboutBox(0);
  236.             
  237.             else if (theItem == helpCommand)
  238.                 DoHelpBox(helpWIND,helpTEXT,helpSTYL);
  239.                 
  240.             else
  241.             {
  242.                 GetItem(myMenus[appleMenu], theItem, &tempString);
  243.                 OpenDeskAcc(&tempString);
  244.                 SetPort(theWPtr);
  245.             }
  246.             
  247.             break;
  248.             
  249.         case fileID:
  250.             alive = FALSE;
  251.             break;
  252.         
  253.         case editID:
  254.             SysBeep(1);
  255.             break;
  256.             
  257.             
  258.         case modeID:
  259.             
  260.             switch (theItem)
  261.             {
  262.                 case slaveCommand:
  263.                     if (slaveMode == FALSE)
  264.                     {
  265.                         slaveMode = TRUE;
  266.                         
  267.                         if (ShutDownAppleTalk() != noErr)
  268.                         {
  269.                             ErrorAlert("\pError in attempt to revert to node mode (ShutDownAppleTalk failed)");
  270.                             
  271.                             slaveMode = FALSE;
  272.                         }
  273.                         
  274.                         else if (SetupAppleTalk() != noErr)
  275.                         {
  276.                             ErrorAlert("\pError in attempt to revert to node mode (SetupAppleTalk failed)");
  277.                             
  278.                             slaveMode = FALSE;
  279.                         }
  280.                         
  281.                         else if (AwaitRequest(receiveRecord,&theMessage) != noErr)
  282.                         {
  283.                             ErrorAlert("\pError in attempt to revert to node mode (AwaitRequest failed)");
  284.                             
  285.                             slaveMode = FALSE;
  286.                         }
  287.                         
  288.                         else
  289.                         {
  290.                             slaveMode = TRUE;
  291.                             
  292.                             DeleteMenu(controlID);
  293.                             DrawMenuBar();
  294.                         }
  295.                         
  296.                         DrawInfo();
  297.                     }
  298.                     
  299.                     break;
  300.                 
  301.                 case masterCommand:
  302.                     if (slaveMode == TRUE)
  303.                     {
  304.                         slaveMode = FALSE;
  305.                         
  306.                         if (KillRequest() != noErr)
  307.                         {
  308.                             ErrorAlert("\pError in attempt to enter master mode (can't kill outstanding ATP request)");
  309.                             
  310.                             slaveMode = TRUE;
  311.                         }
  312.                         
  313.                         else if (SetupAppleTalk() != noErr)
  314.                         {
  315.                             ErrorAlert("\pError in attempt to enter master mode (can't set up AppleTalk)");
  316.                                     
  317.                             slaveMode = TRUE;
  318.                         }
  319.                         
  320.                         else
  321.                         {
  322.                             slaveMode = FALSE;
  323.                             
  324.                             LookupSlaves();
  325.                             
  326.                             InsertMenu(myMenus[controlMenu],0);
  327.                             DrawMenuBar();
  328.                         }
  329.                         
  330.                         DrawInfo();
  331.                     }
  332.                     
  333.                     break;
  334.             }
  335.             
  336.             break;
  337.             
  338.         case graphID:
  339.             
  340.             switch (theItem)
  341.             {    
  342.                 case setLimsCommand:
  343.                     if (SetGraphLimits() == 1)
  344.                         CopyBits(&(theWPtr->portBits),&graphBits,&graphRect,&graphRect,srcCopy,0L);
  345.                     break;
  346.                     
  347.                 case setIterCommand:
  348.                     SetMaxIterations();
  349.                     break;
  350.                 
  351.                 case setPixSizeCommand:
  352.                     SetPixelSize();
  353.                     break;
  354.                 
  355.                 case shadingCommand:
  356.                     shading = (shading) ? FALSE : TRUE;
  357.                     DrawInfo();
  358.                     break;
  359.                 
  360.                 case defaultLimsCommand:
  361.                     lims[xmin] = -2.0;
  362.                     lims[xmax] = 1.0;
  363.                     lims[ymin] = -1.5;
  364.                     lims[ymax] = 1.5;
  365.                     
  366.                     ClearGraph();
  367.                     DrawInfo();
  368.                     
  369.                     break;
  370.                 
  371.                 case clearGraphCommand:
  372.                     ClearGraph();
  373.                     CopyBits(&(theWPtr->portBits),&graphBits,&graphRect,&graphRect,srcCopy,0L);
  374.                     break;
  375.                 
  376.                 case mandelCommand:
  377.                     AssignMandelbrot();
  378.                     CopyBits(&(theWPtr->portBits),&graphBits,&graphRect,&graphRect,srcCopy,0L);
  379.                     break;
  380.                 
  381.             }
  382.             
  383.             break;
  384.         
  385.         
  386.         case controlID:
  387.             
  388.             switch (theItem)
  389.             {    
  390.                 case checkSlavesCommand:
  391.                     LookupSlaves();
  392.                     DrawInfo();
  393.                     break;
  394.                 
  395.                 case stopSlavesCommand:
  396.                     StopSlaves();
  397.                     break;
  398.                 
  399.                 case slaveStatusCommand:
  400.                     CheckSlaves();
  401.                     break;
  402.                 
  403.                 case setDelayCommand:
  404.                     SetDelay();
  405.                     break;
  406.                 
  407.                 case meTooCommand:
  408.                     meToo = (meToo) ? FALSE : TRUE;
  409.                     DrawInfo();
  410.                     break;
  411.             }
  412.             
  413.             break;
  414.     }
  415.     
  416.     HiliteMenu(0);
  417. }
  418.  
  419.  
  420.  
  421. DoContent(theEvent)
  422. EventRecord    *theEvent;
  423. {
  424. GrafPtr            oldPort;
  425.  
  426.  
  427.     GetPort(&oldPort);
  428.     SetPort(theWPtr);
  429.     GlobalToLocal(&theEvent->where);
  430.     
  431.     if (PtInRect(theEvent->where, &graphRect))
  432.     {
  433.         ZoomGraph(theEvent->where);
  434.         
  435.         DrawInfo();
  436.     }
  437.         
  438.     else
  439.         SysBeep(1);
  440.     
  441.     SetPort(oldPort);
  442. }
  443.  
  444.  
  445.  
  446.  
  447. InitWindow()
  448. {
  449. FontInfo    theFontInfo;
  450.  
  451.  
  452.     theWPtr = GetNewWindow(mainWIND,&wRecord,-1L);
  453.     
  454.     if (theWPtr == 0L)
  455.         FatalError("\p Can't allocate memory for window");
  456.     
  457.     SetPort(theWPtr);
  458.     
  459.     CenterWindow(theWPtr);
  460.     ShowWindow(theWPtr);
  461.     
  462.     SetRect(&nullClipRect, -32768, -32768, 32767, 32767);
  463.     
  464.     infoRect = theWPtr->portRect;
  465.     infoRect.right = infoRect.right - 256 - 2;
  466.     infoRect.bottom = infoRect.top +256;
  467.     
  468.     messageRect = theWPtr->portRect;
  469.     messageRect.top = messageRect.top + 256 + 2;
  470.     
  471.     graphRect = theWPtr->portRect;
  472.     graphRect.left = graphRect.right - 256;
  473.     graphRect.bottom = graphRect.top + 256;
  474.     
  475.     graphBits.rowBytes = (graphRect.right - graphRect.left + 1)/8;
  476.     if (graphBits.rowBytes & 1) graphBits.rowBytes++;
  477.     graphBits.bounds = graphRect;
  478.     graphBits.baseAddr = NewPtr(graphBits.rowBytes * (graphRect.bottom - graphRect.top + 1));
  479.     
  480.     if (graphBits.baseAddr==0L)
  481.         FatalError("\p Can't allocate memory for BitMap");
  482.     
  483.     
  484.     ClearGraph();
  485.     
  486.     CopyBits(&(theWPtr->portBits),&graphBits,&graphRect,&graphRect,srcCopy,0L);
  487.     
  488.     TextFont(3);
  489.     TextMode(srcCopy);
  490.     TextSize(9);
  491.     TextFace(0);
  492.     GetFontInfo(&theFontInfo);
  493.     
  494.     coordRect.top = graphRect.top;
  495.     coordRect.right = graphRect.right;
  496.     coordRect.left = coordRect.right - StringWidth("\p (-0000.000,-0000.000) ");
  497.     coordRect.bottom = coordRect.top + theFontInfo.ascent + theFontInfo.descent + 2;
  498.     
  499.     UpdateWindow();
  500. }
  501.  
  502.  
  503.  
  504.  
  505. UpdateWindow()
  506. {
  507. GrafPtr        oldPort;
  508.  
  509.     GetPort(&oldPort);
  510.     SetPort(theWPtr);
  511.     
  512.     BeginUpdate(theWPtr);
  513.     
  514.     DrawInfo();
  515.     
  516.     ClearGraph();
  517.     CopyBits(&graphBits,&(theWPtr->portBits),&graphRect,&graphRect,srcCopy,0L);
  518.     
  519.     DrawDisplayMessages();
  520.     
  521.     EndUpdate(theWPtr);
  522.     
  523.     SetPort(oldPort);
  524. }
  525.  
  526.  
  527.  
  528.  
  529. DrawInfo()
  530. {
  531. GrafPtr        oldPort;
  532. char        tempString[256];
  533. int            x,y;
  534.  
  535.     GetPort(&oldPort);
  536.     SetPort(theWPtr);
  537.     
  538.     PenNormal();
  539.     TextFont(3);
  540.     TextFace(0);
  541.     TextMode(srcCopy);
  542.     TextSize(10);
  543.     
  544.     ClipRect(&infoRect);
  545.     EraseRect(&infoRect);
  546.     FrameRect(&infoRect);
  547.     
  548.     x = infoRect.left + 5;
  549.     y = infoRect.top + 15;
  550.     
  551.     MoveTo(x,y);
  552.     TextFace(bold);
  553.     
  554.     if (slaveMode==TRUE)
  555.         DrawString("\pNode Mode");
  556.     else
  557.         DrawString("\pMaster Mode");
  558.     
  559.     sprintf(tempString,"Node #%d",myNode);
  560.     CtoPstr(tempString);
  561.     
  562.     y += 12;
  563.     MoveTo(x,y);
  564.     TextFace(0);
  565.     DrawString(tempString);
  566.     
  567.     sprintf(tempString,"Socket #%d",mySocket);
  568.     CtoPstr(tempString);
  569.     
  570.     y += 12;
  571.     MoveTo(x,y);
  572.     DrawString(tempString);
  573.     
  574.     if (!slaveMode)
  575.     {
  576.         sprintf(tempString,"Node Count: %d",numSlaves);
  577.         CtoPstr(tempString);
  578.         y += 12;
  579.         MoveTo(x,y);
  580.         DrawString(tempString);
  581.     }
  582.     
  583.     sprintf(tempString,"xmin: %lf",lims[xmin]);
  584.     CtoPstr(tempString);
  585.     y += 20;
  586.     MoveTo(x,y);
  587.     DrawString(tempString);
  588.     
  589.     sprintf(tempString,"xmax: %lf",lims[xmax]);
  590.     CtoPstr(tempString);
  591.     y += 12;
  592.     MoveTo(x,y);
  593.     DrawString(tempString);
  594.     
  595.     sprintf(tempString,"ymin: %lf",lims[ymin]);
  596.     CtoPstr(tempString);
  597.     y += 12;
  598.     MoveTo(x,y);
  599.     DrawString(tempString);
  600.     
  601.     sprintf(tempString,"ymax: %lf",lims[ymax]);
  602.     CtoPstr(tempString);
  603.     y += 12;
  604.     MoveTo(x,y);
  605.     DrawString(tempString);
  606.     
  607.     sprintf(tempString,"maximum iterations: %d",maxIterations);
  608.     CtoPstr(tempString);
  609.     y += 20;
  610.     MoveTo(x,y);
  611.     DrawString(tempString);
  612.     
  613.     sprintf(tempString,"pixel size: %d",pixelSize);
  614.     CtoPstr(tempString);
  615.     y += 12;
  616.     MoveTo(x,y);
  617.     DrawString(tempString);
  618.     
  619.     if (shading)
  620.         sprintf(tempString,"shading: ENABLED");
  621.     else
  622.         sprintf(tempString,"shading: DISABLED");
  623.     
  624.     CtoPstr(tempString);
  625.     y += 12;
  626.     MoveTo(x,y);
  627.     DrawString(tempString);
  628.     
  629.     if (slaveMode == FALSE)
  630.     {
  631.         sprintf(tempString,"polling delay: %.2lf",timeDelay);
  632.         CtoPstr(tempString);
  633.         y += 12;
  634.         MoveTo(x,y);
  635.         DrawString(tempString);
  636.         
  637.         if (meToo)
  638.             sprintf(tempString,"master participation: ENABLED");
  639.         else
  640.             sprintf(tempString,"master participation: DISABLED");
  641.         
  642.         CtoPstr(tempString);
  643.         y += 12;
  644.         MoveTo(x,y);
  645.         DrawString(tempString);
  646.     }
  647.     
  648.     ClipRect(&nullClipRect);
  649.     
  650.     SetPort(oldPort);
  651. }
  652.  
  653.  
  654.  
  655. ClearGraph()
  656. {
  657. GrafPtr        oldPort;
  658.  
  659.     GetPort(&oldPort);
  660.     SetPort(theWPtr);
  661.     
  662.     PenNormal();
  663.     
  664.     ClipRect(&graphRect);
  665.     EraseRect(&graphRect);
  666.     FrameRect(&graphRect);
  667.     
  668.     ClipRect(&nullClipRect);
  669.     
  670.     SetPort(oldPort);
  671. }
  672.  
  673.  
  674.  
  675.  
  676. DisplayMessage(theString)
  677. char    *theString;
  678. {
  679.     PtoCstr(theDisplayMessages[1]);
  680.     PtoCstr(theDisplayMessages[2]);
  681.     
  682.     strcpy(theDisplayMessages[0],theDisplayMessages[1]);
  683.     strcpy(theDisplayMessages[1],theDisplayMessages[2]);
  684.     strcpy(theDisplayMessages[2],theString);
  685.     
  686.     CtoPstr(theDisplayMessages[0]);
  687.     CtoPstr(theDisplayMessages[1]);
  688.     CtoPstr(theDisplayMessages[2]);
  689.     
  690.     DrawDisplayMessages();
  691. }
  692.  
  693.  
  694.  
  695. DrawDisplayMessages()
  696. {
  697. GrafPtr        oldPort;
  698.  
  699.     GetPort(&oldPort);
  700.     SetPort(theWPtr);
  701.     
  702.     PenNormal();
  703.     TextFont(3);
  704.     TextFace(0);
  705.     TextMode(srcCopy);
  706.     TextSize(10);
  707.     
  708.     ClipRect(&messageRect);
  709.     EraseRect(&messageRect);
  710.     FrameRect(&messageRect);
  711.     
  712.     MoveTo(messageRect.left+5,messageRect.top + 12);
  713.     DrawString(theDisplayMessages[0]);
  714.     
  715.     MoveTo(messageRect.left+5,messageRect.top + 24);
  716.     DrawString(theDisplayMessages[1]);
  717.     
  718.     TextFace(bold);
  719.     
  720.     MoveTo(messageRect.left+5,messageRect.top + 36);
  721.     DrawString(theDisplayMessages[2]);
  722.     
  723.     ClipRect(&nullClipRect);
  724.     
  725.     SetPort(oldPort);
  726. }
  727.  
  728.  
  729.  
  730.  
  731. ErrorMessage(routineName,errCode)
  732. char    *routineName;int    errCode;
  733. {
  734. char    tempString[256];
  735.  
  736.     sprintf(tempString,"%s: error #%d",routineName,errCode);
  737.     DisplayMessage(tempString);
  738. }
  739.  
  740.  
  741.  
  742.  
  743. ClearMessage()
  744. {
  745.     DisplayMessage("");
  746. }
  747.  
  748.  
  749.  
  750. MaintainMenus()
  751. {
  752.     if (FrontWindow() != theWPtr) 
  753.     {
  754.         EnableItem(myMenus[editMenu],undoCommand);
  755.         EnableItem(myMenus[editMenu],cutCommand);
  756.         EnableItem(myMenus[editMenu],copyCommand);
  757.         EnableItem(myMenus[editMenu],clearCommand);
  758.         EnableItem(myMenus[editMenu],pasteCommand);
  759.     }
  760.     
  761.     else 
  762.     {
  763.         DisableItem(myMenus[editMenu],undoCommand);
  764.         DisableItem(myMenus[editMenu],cutCommand);
  765.         DisableItem(myMenus[editMenu],copyCommand);
  766.         DisableItem(myMenus[editMenu],clearCommand);
  767.         DisableItem(myMenus[editMenu],pasteCommand);
  768.         
  769.         if (slaveMode)
  770.         {
  771.             CheckItem(myMenus[modeMenu],slaveCommand,TRUE);
  772.             CheckItem(myMenus[modeMenu],masterCommand,FALSE);
  773.         }
  774.         else
  775.         {
  776.             CheckItem(myMenus[modeMenu],slaveCommand,FALSE);
  777.             CheckItem(myMenus[modeMenu],masterCommand,TRUE);
  778.             
  779.             if (meToo)
  780.                 CheckItem(myMenus[controlMenu],meTooCommand,TRUE);
  781.             else
  782.                 CheckItem(myMenus[controlMenu],meTooCommand,FALSE);
  783.         }
  784.         
  785.         if (shading)
  786.             CheckItem(myMenus[graphMenu],shadingCommand,TRUE);
  787.         else
  788.             CheckItem(myMenus[graphMenu],shadingCommand,FALSE);
  789.     }
  790. }
  791.  
  792.  
  793.  
  794. SetUpCursors()
  795. {
  796. CursHandle    hCurs;
  797.     
  798.     hCurs = GetCursor(watchCursor);
  799.     waitCursor = **hCurs;
  800.     hCurs = GetCursor(crossCursor);
  801.     xCursor = **hCurs;
  802. }
  803.  
  804.  
  805.  
  806. MaintainCursor()
  807. {
  808. Point        tempPoint;
  809. GrafPtr        oldPort;
  810.     
  811.     if (FrontWindow() == theWPtr) 
  812.     {
  813.         GetPort(&oldPort);
  814.         SetPort(theWPtr);
  815.         
  816.         GetMouse(&tempPoint);
  817.         
  818.         if (PtInRect(tempPoint,&graphRect))
  819.         {
  820.             SetCursor(&xCursor);
  821.             
  822.             ShowXY(tempPoint);
  823.         }
  824.         
  825.         else
  826.         {
  827.             SetCursor(&arrow);
  828.             
  829.             if (coords)
  830.             {
  831.                 InvalRect(&coordRect);
  832.                 
  833.                 UpdateWindow();
  834.                 
  835.                 coords = FALSE;
  836.             }
  837.         }
  838.             
  839.         SetPort(oldPort);
  840.     }
  841. }
  842.  
  843.  
  844.  
  845.  
  846. SetGraphLimits()
  847. {
  848. DialogPtr    theDPtr;
  849. int            itemHit, type, i,syntax;
  850. Handle        theItem;
  851. Rect        theRect;
  852. GrafPtr        oldPort;
  853. char        tempString[256];
  854. double        tlims[4],delta;
  855.  
  856.     GetPort(&oldPort);
  857.     
  858.     theDPtr = GetNewDialog(limsDLOG, NULL,(WindowPtr) -1L);
  859.     
  860.     CenterWindow(theDPtr);
  861.     ShowWindow(theDPtr);
  862.     
  863.     SetPort(theDPtr);
  864.     InitCursor();
  865.     
  866.     GetDItem(theDPtr, 1, &type, &theItem, &theRect);
  867.     PenNormal();
  868.     PenSize(3,3);
  869.     InsetRect(&theRect,-4,-4);
  870.     FrameRoundRect(&theRect,16,16);
  871.     PenSize(1,1);
  872.     
  873.     for (i=0;i<4;i++)
  874.     {
  875.         tlims[i] = lims[i];
  876.         
  877.         sprintf(tempString,"%lf",lims[i]);
  878.         CtoPstr(tempString);
  879.         GetDItem(theDPtr, 4+2*i, &type, &theItem, &theRect);
  880.         SetIText(theItem,  tempString);
  881.     }
  882.     
  883.     SelIText(theDPtr, 4, 0, 256);
  884.     
  885.     syntax = 0;
  886.     
  887.     do
  888.     {
  889.         do
  890.         {
  891.             ModalDialog(0L,&itemHit);
  892.         
  893.         } while (itemHit!=1 && itemHit!=2 && itemHit!=11);
  894.         
  895.         if (itemHit==1)
  896.         {
  897.             syntax = 1;
  898.             
  899.             for (i=0;i<4  &&  syntax==1; i++)
  900.             {
  901.                 GetDItem(theDPtr, 4+2*i, &type, &theItem, &theRect);
  902.                 GetIText(theItem,  tempString);
  903.                 PtoCstr(tempString);
  904.                 syntax = sscanf(tempString,"%lf",&(tlims[i]));
  905.                 
  906.                 if (syntax != 1)
  907.                 {
  908.                     ErrorAlert("\pBad numeric input");
  909.                     SelIText(theDPtr, 4+2*i, 0, 256);
  910.                 }
  911.             }
  912.             
  913.             
  914.             if (tlims[xmin]>=tlims[xmax] || tlims[ymin]>=tlims[ymax])
  915.             {
  916.                 ErrorAlert("\pMinimum limits must be less than maximum limits");
  917.                 syntax = 0;
  918.             }
  919.             
  920.             else
  921.             {
  922.                 delta = 0.0;
  923.                 
  924.                 for (i=0;i<4;i++)
  925.                 {
  926.                     if (lims[i] > tlims[i])
  927.                         delta += lims[i] - tlims[i];
  928.                     else
  929.                         delta += tlims[i] - lims[i];
  930.                     
  931.                     lims[i] = tlims[i];
  932.                 }
  933.             }
  934.         }
  935.         
  936.         else if (itemHit==2)
  937.             syntax = 1;
  938.         
  939.         else if (itemHit==11)
  940.         {
  941.             tlims[xmin] = -2.0;
  942.             tlims[xmax] = 1.0;
  943.             tlims[ymin] = -1.5;
  944.             tlims[ymax] = 1.5;
  945.             
  946.             for (i=0;i<4;i++)
  947.             {
  948.                 sprintf(tempString,"%lf",tlims[i]);
  949.                 CtoPstr(tempString);
  950.                 GetDItem(theDPtr, 4+2*i, &type, &theItem, &theRect);
  951.                 SetIText(theItem,  tempString);
  952.             }
  953.             
  954.             SelIText(theDPtr, 4, 0, 256);
  955.         }
  956.     
  957.     } while (syntax != 1);
  958.     
  959.     
  960.     DisposDialog(theDPtr);
  961.     SetPort(oldPort);
  962.     
  963.     
  964.     if (itemHit==1 && delta!=0.0) 
  965.     {
  966.         ClearGraph();
  967.         DrawInfo();
  968.     }
  969.     else
  970.         itemHit = 2;
  971.     
  972.     
  973.     return(itemHit);
  974. }
  975.  
  976.  
  977. SetMaxIterations()
  978. {
  979. int        errCode;
  980. char    tempString[256];
  981.  
  982.     sprintf(tempString,"%d",maxIterations);
  983.     
  984.     errCode = GetInput("\pEnter maximum number of iterations:", tempString);
  985.     
  986.     if (errCode == noErr)
  987.     {
  988.         if (sscanf(tempString,"%d",&maxIterations)!=1 || maxIterations<1)
  989.         {
  990.             ErrorAlert("\pBad numeric input for number of iterations");
  991.         }
  992.         else
  993.             DrawInfo();
  994.     }
  995. }
  996.  
  997.  
  998.  
  999.  
  1000. SetPixelSize()
  1001. {
  1002. int        errCode;
  1003. char    tempString[256];
  1004.  
  1005.     sprintf(tempString,"%d",pixelSize);
  1006.     
  1007.     errCode = GetInput("\pEnter pixel size (1-254):", tempString);
  1008.     
  1009.     if (errCode == noErr)
  1010.     {
  1011.         if (sscanf(tempString,"%d",&pixelSize)!=1 || pixelSize<1 || pixelSize>254)
  1012.         {
  1013.             ErrorAlert("\ppixel size must be from 1 to 254");
  1014.         }
  1015.         else
  1016.             DrawInfo();
  1017.     }
  1018. }
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024. SetDelay()
  1025. {
  1026. int        errCode;
  1027. char    tempString[256];
  1028.  
  1029.     sprintf(tempString,"%.2lf",timeDelay);
  1030.     
  1031.     errCode = GetInput("\pEnter minimum delay between node polls (seconds):", tempString);
  1032.     
  1033.     if (errCode == noErr)
  1034.     {
  1035.         if (sscanf(tempString,"%lf",&timeDelay)!=1 || timeDelay<=0)
  1036.         {
  1037.             ErrorAlert("\pdelay must be greater than zero seconds");
  1038.         }
  1039.         else
  1040.             DrawInfo();
  1041.     }
  1042. }
  1043.  
  1044.  
  1045.  
  1046.  
  1047. double XToD(x)
  1048. int x;
  1049. {
  1050. double    ratio;
  1051.  
  1052.     ratio = (double) (x - graphRect.left)/ (double) (graphRect.right - graphRect.left);
  1053.     ratio = ratio * (lims[xmax] - lims[xmin]) + lims[xmin];
  1054.     
  1055.     return(ratio);
  1056. }
  1057.  
  1058. double YToD(y)
  1059. int y;
  1060. {
  1061. double    ratio;
  1062.  
  1063.     ratio = (double) (y - graphRect.top)/ (double) (graphRect.bottom - graphRect.top);
  1064.     ratio = lims[ymax] - ratio * (lims[ymax] - lims[ymin]);
  1065.     
  1066.     return(ratio);
  1067. }
  1068.  
  1069.  
  1070. DToX(x)
  1071. double x;
  1072. {
  1073. double    ratio;
  1074.  
  1075.     ratio = (x - lims[xmin])/(lims[xmax] - lims[xmin]);
  1076.     ratio = ratio * (graphRect.right - graphRect.left) + graphRect.left;
  1077.     
  1078.     if (ratio < -32000.0)
  1079.         ratio = -32000.0;
  1080.     else if (ratio > 32000.0)
  1081.         ratio = 32000.0;
  1082.     
  1083.     return((int) ratio);
  1084. }
  1085.  
  1086.  
  1087. DToY(y)
  1088. double y;
  1089. {
  1090. double    ratio;
  1091.  
  1092.     ratio = (y - lims[ymin])/(lims[ymax] - lims[ymin]);
  1093.     ratio = graphRect.bottom - ratio * (graphRect.bottom - graphRect.top);
  1094.     
  1095.     if (ratio < -32000.0)
  1096.         ratio = -32000.0;
  1097.     else if (ratio > 32000.0)
  1098.         ratio = 32000.0;
  1099.     
  1100.     return((int) ratio);
  1101. }
  1102.  
  1103.  
  1104.  
  1105. ZoomGraph(thePoint)
  1106. Point    thePoint;
  1107. {
  1108. GrafPtr        oldPort;
  1109. Point        mousePoint,oldPoint;
  1110. Rect        theRect,tempGraphRect;
  1111. double        tlims[4],r;
  1112. int            delta;
  1113.  
  1114.     GetPort(&oldPort);
  1115.     SetPort(theWPtr);
  1116.     
  1117.     theRect.left  = thePoint.h;
  1118.     theRect.right = thePoint.h;
  1119.     theRect.top = thePoint.v;
  1120.     theRect.bottom = thePoint.v;
  1121.     
  1122.     GetMouse(&oldPoint);
  1123.     ShowXY(oldPoint);
  1124.     
  1125.     PenNormal();
  1126.     PenMode(patXor);
  1127.     PenPat(gray);
  1128.     FrameRect(&theRect);
  1129.     
  1130.     while (StillDown())
  1131.     {
  1132.         GetMouse(&mousePoint);
  1133.         
  1134.         if ((mousePoint.v!=oldPoint.v)||(mousePoint.h!=oldPoint.h))
  1135.         {
  1136.             FrameRect(&theRect);
  1137.             
  1138.             if (mousePoint.h < graphRect.left)
  1139.                 mousePoint.h = graphRect.left;
  1140.             if (mousePoint.h > graphRect.right)
  1141.                 mousePoint.h = graphRect.right;
  1142.             
  1143.             if (mousePoint.v < graphRect.top)
  1144.                 mousePoint.v = graphRect.top;
  1145.             if (mousePoint.v > graphRect.bottom)
  1146.                 mousePoint.v = graphRect.bottom;
  1147.             
  1148.             ShowXY(mousePoint);
  1149.             
  1150.             r = ((double) mousePoint.h - (double) thePoint.h)*((double) mousePoint.h - (double) thePoint.h) + ((double) mousePoint.v - (double) thePoint.v)*((double) mousePoint.v - (double) thePoint.v);
  1151.             delta = sqrt(r)/1.4142135624;
  1152.             
  1153.             if (mousePoint.v < thePoint.v)
  1154.             {
  1155.                 theRect.top = thePoint.v - delta;
  1156.                 theRect.bottom = thePoint.v;
  1157.             }
  1158.             else
  1159.             {
  1160.                 theRect.bottom = thePoint.v + delta;
  1161.                 theRect.top = thePoint.v;
  1162.             }
  1163.             
  1164.             if (mousePoint.h < thePoint.h)
  1165.             {
  1166.                 theRect.left = thePoint.h - delta;
  1167.                 theRect.right = thePoint.h;
  1168.             }
  1169.             else
  1170.             {
  1171.                 theRect.right = thePoint.h + delta;
  1172.                 theRect.left = thePoint.h;
  1173.             }
  1174.             
  1175.             FrameRect(&theRect);
  1176.         }
  1177.         
  1178.         oldPoint = mousePoint;
  1179.     }
  1180.     
  1181.     FrameRect(&theRect);
  1182.     
  1183.     if ((theRect.right!=theRect.left)&&(theRect.top!=theRect.bottom))
  1184.     {
  1185.         tlims[xmin] = XToD(theRect.left);
  1186.         tlims[xmax] = XToD(theRect.right);
  1187.         tlims[ymin] = YToD(theRect.bottom);
  1188.         tlims[ymax] = YToD(theRect.top);
  1189.         
  1190.         lims[xmin] = tlims[xmin];
  1191.         lims[xmax] = tlims[xmax];
  1192.         lims[ymin] = tlims[ymin];
  1193.         lims[ymax] = tlims[ymax];
  1194.         
  1195.         tempGraphRect = graphRect;
  1196.         InsetRect(&tempGraphRect,1,1);
  1197.         
  1198.         CopyBits(&(theWPtr->portBits),&graphBits,&theRect,&tempGraphRect,srcCopy,0L);
  1199.         CopyBits(&graphBits,&(theWPtr->portBits),&graphRect,&graphRect,srcCopy,0L);
  1200.     }
  1201.     
  1202.     SetPort(oldPort);
  1203. }
  1204.  
  1205.  
  1206.  
  1207. ShowXY(thePoint)
  1208. Point    thePoint;
  1209. {
  1210. double        x,y;
  1211. char        tempString[256];
  1212. FontInfo    theFontInfo;
  1213. GrafPtr        oldPort;
  1214. Rect        theRect;
  1215. RgnHandle    saveClip;
  1216. int            oldFont, oldMode, oldSize, oldFace;
  1217. PenState    oldPenState;
  1218.  
  1219.     if ((thePoint.h != oldPoint.h)||(thePoint.v != oldPoint.v))
  1220.     {
  1221.         GetPort(&oldPort);
  1222.         SetPort(theWPtr);
  1223.         saveClip = NewRgn();
  1224.         GetClip(saveClip);
  1225.         
  1226.         x = XToD(thePoint.h);
  1227.         y = YToD(thePoint.v);
  1228.         
  1229.         sprintf(tempString,"(%.5lf,%.5lf)",x,y);
  1230.         CtoPstr(tempString);
  1231.         
  1232.         oldFont = ((GrafPtr) theWPtr)->txFont;
  1233.         oldMode = ((GrafPtr) theWPtr)->txMode;
  1234.         oldSize = ((GrafPtr) theWPtr)->txSize;
  1235.         oldFace = ((GrafPtr) theWPtr)->txFace;
  1236.         
  1237.         TextFont(3);
  1238.         TextMode(srcOr);
  1239.         TextSize(9);
  1240.         TextFace(0);
  1241.         GetFontInfo(&theFontInfo);
  1242.         
  1243.         GetPenState(&oldPenState);
  1244.         
  1245.         PenNormal();
  1246.         
  1247.         FrameRect(&coordRect);
  1248.         theRect = coordRect;
  1249.         InsetRect(&theRect,1,1);
  1250.         EraseRect(&theRect);
  1251.         ClipRect(&theRect);
  1252.         
  1253.         MoveTo(coordRect.left + 3,coordRect.top + theFontInfo.ascent);
  1254.         DrawString(tempString);
  1255.         
  1256.         SetClip(saveClip);
  1257.         DisposeRgn(saveClip);
  1258.         
  1259.         SetPenState(&oldPenState);
  1260.         TextFont(oldFont);
  1261.         TextMode(oldMode);
  1262.         TextSize(oldSize);
  1263.         TextFace(oldFace);
  1264.         
  1265.         coords = TRUE;
  1266.         
  1267.         SetPort(oldPort);
  1268.     }
  1269.     
  1270.     oldPoint.h = thePoint.h;
  1271.     oldPoint.v = thePoint.v;
  1272. }
  1273.  
  1274.  
  1275.  
  1276.  
  1277. BreakKey()
  1278. {
  1279. EventRecord        theEvent;
  1280. char            theChar;
  1281. int                theResult;
  1282.  
  1283.     theResult = FALSE;
  1284.     
  1285.     if (GetNextEvent(keyDownMask+autoKeyMask,&theEvent))
  1286.     {
  1287.         theChar = theEvent.message & charCodeMask;
  1288.         
  1289.         if (theEvent.modifiers&cmdKey && theChar=='.')
  1290.             theResult = TRUE;
  1291.     }
  1292.         
  1293.     return(theResult);
  1294. }
  1295.  
  1296.  
  1297.  
  1298. DoDrag(whichWindow,theEvent)
  1299. WindowPtr    whichWindow;EventRecord    *theEvent;
  1300. {
  1301. Rect    tempRect;
  1302.     
  1303.     if (whichWindow == theWPtr)
  1304.     {
  1305.         SetRect(&tempRect,screenBits.bounds.left+10,screenBits.bounds.top+25,screenBits.bounds.right-10,screenBits.bounds.bottom-25);
  1306.         DragWindow(whichWindow,theEvent->where,&tempRect);
  1307.     }
  1308. }