home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncsa.uiuc.edu / ftp.ncsa.uiuc.edu.zip / ftp.ncsa.uiuc.edu / Contour / interf.p < prev    next >
Text File  |  2017-03-03  |  27KB  |  851 lines

  1. {$U-}
  2. unit Interf(40);
  3.  
  4. interface
  5. uses
  6.   
  7.     MemTypes, QuickDraw, OSIntf, ToolIntf,MacPrint,PackIntf,FixMath,Graf3D,
  8.     Globals, Utilities,Routines,ThreeD;
  9.  
  10. procedure MoveScrollBars;
  11. procedure fixPlotRect;
  12. procedure fixScrollBars;
  13.  
  14. procedure ProcessContourMenu (menuid, menuitem : integer);
  15. procedure DoContent (myEvent : EventRecord;
  16.                       theWindow : WindowPtr);
  17.  
  18. procedure ContourContent;
  19. procedure ContourScroll (dh, dv : integer);
  20.  
  21. function GetITDims(var Horiz, Vert:integer;var mag:longint;var level, xOrigin, yOrigin,
  22.                    minData, maxData : integer) : boolean;
  23.                     
  24. procedure closeWindow(myWindow : WindowPtr);
  25. procedure fixOrigin(x,y : integer);
  26. procedure DoAbout;
  27. procedure DoPrint;
  28. procedure DoScrap(myWindow : windowPtr);
  29. procedure DoSave(myWindow : windowPtr);
  30.  
  31. implementation
  32. var
  33.        newX,newY : integer;
  34.     myPict : PicHandle;
  35.     clipRgn : RgnHandle;
  36.  
  37.  
  38.  
  39. procedure MoveScrollBars;
  40. begin
  41.     with ContourWindow[windowIndex]^.portRect do begin
  42.         HideControl(VerticalScrollBar[windowIndex]);
  43.         HideControl(HorizontalScrollBar[windowIndex]);
  44.  
  45.         MoveControl(VerticalScrollBar[windowIndex], right - (ScrollBarWidth - 1), top - 1);
  46.         SizeControl(VerticalScrollBar[windowIndex], ScrollBarWidth, bottom + 1 - (top - 1) - (ScrollBarWidth - 1));
  47.  
  48.         MoveControl(HorizontalScrollBar[windowIndex], left - 1, bottom - (ScrollBarWidth - 1));
  49.         SizeControl(HorizontalScrollBar[windowIndex], right + 1 - (left - 1) - (ScrollBarWidth - 1), ScrollBarWidth);
  50.  
  51.         ShowControl(HorizontalScrollBar[windowIndex]);
  52.         ShowControl(VerticalScrollBar[windowIndex]);
  53.    end
  54.  end;
  55.  
  56.  
  57. procedure OffsetScrollBars (dh, dv : integer);
  58. { Offset the Scroll Bars by dh and dv - to avoid flashing them with MoveScrollBars }
  59. var
  60.     R : Rect;
  61. begin
  62.     R := VerticalScrollBar[windowIndex]^^.ContrlRect;
  63.     OffsetRect(R, dh, dv);
  64.     VerticalScrollBar[windowIndex]^^.ContrlRect := R;
  65.  
  66.     R := HorizontalScrollBar[windowIndex]^^.ContrlRect;
  67.     OffsetRect(R, dh, dv);
  68.     HorizontalScrollBar[windowIndex]^^.ContrlRect := R;
  69. end;
  70.  
  71.  
  72. procedure fixPlotRect;
  73. { Set plotRect to be the contents without scrollbars }
  74. begin
  75.     plotRect[windowIndex] := ContourWindow[windowIndex]^.portRect;
  76.     plotRect[windowIndex].bottom := plotRect[windowIndex].bottom - ScrollBarWidth + 1;
  77.     plotRect[windowIndex].right := plotRect[windowIndex].right - ScrollBarWidth + 1;
  78. end;
  79.  
  80.  
  81. procedure fixScrollBars;
  82.     { fix the maximum scroll bar value to allow scrolling to end of data, but not much further }
  83.     { Or set min and max to -90 and 90 if scrollAngle }
  84.     { It uses the plotRect so fixPlotRect first }
  85.  
  86. begin
  87.  
  88.     with plotRect[windowIndex] do begin
  89.  
  90.         SetCtlMin(HorizontalScrollBar[windowIndex], MinDataX*currentGridSize);
  91.         SetCtlMin(VerticalScrollBar[windowIndex], MinDataY*currentGridSize);
  92.         SetCtlMax(HorizontalScrollBar[windowIndex], max(minDataX*currentGridSize,
  93.                   maxX[windowIndex] * currentGridSize - (right - left)));
  94.         SetCtlMax(VerticalScrollBar[windowIndex], max(minDataY* currentGridSize,
  95.                   maxY[windowIndex] * currentGridSize - (bottom - top)));
  96.         SetCtlValue(HorizontalScrollBar[windowIndex], max(left, minDataX* currentGridSize));
  97.         SetCtlValue(VerticalScrollBar[windowIndex], max(top, minDataY* currentGridSize));
  98.  
  99.    end;
  100. end;
  101.  
  102.  
  103. procedure ContourContent;
  104. var
  105.     newBounds : Rect;
  106.     P : Point;
  107.     Xgrid, Ygrid : longint;
  108.     alrslt : integer;
  109.  
  110. begin
  111.  
  112.     GetMouse(P);
  113.     SelectRectangle(P, newBounds);
  114.     zooming[windowIndex] := true;
  115.  
  116.  
  117.     with newBounds do
  118.         if ((right - left) <= 5) and ((bottom - top) <= 5) then begin
  119.     { Zoom out centered on P }
  120.             if (currentGridSize <> 1) then begin
  121.                 setCursor(Zorro^^);
  122.                 enableItem(GoodiesMenu,2);
  123.                 NewGridSize := currentGridSize div 2;
  124.                 if NewGridSize = 0 then
  125.                     NewGridSize := 1;
  126.                 with P, plotRect[windowIndex] do begin
  127.                     newX := (h-(right-left)) div 2;
  128.                     newY := (v-(bottom-top)) div 2;
  129.                 end;
  130.             end
  131.             else begin
  132.                 newGridSize := 1;
  133.                 alrslt := NoteAlert(ZBalertID, nil);
  134.             end
  135.         end
  136.         else
  137.             if ((right - left) >= currentGridSize) and
  138.                ((bottom - top) >= currentGridSize) then begin
  139.             { Zoom in on newBounds }
  140.                 setCursor(Zorro^^);
  141.                 enableItem(GoodiesMenu,1);
  142.                 enableItem(PlotMenu,3);
  143.                 setItem(FileMenu,4,'Print Selection ...');
  144.                 setItem(EditMenu,4,'Copy Selection');
  145.                 selection[windowIndex] := true;
  146.  
  147.                 with ContourWindow[windowIndex]^.portRect do begin
  148.                     Xgrid := trunc(currentGridSize * longint(right - left) /
  149.                                    (newBounds.right - newBounds.left));
  150.                     Ygrid := trunc(currentGridSize * longint(bottom - top) / 
  151.                                    (newBounds.bottom - newBounds.top));
  152.                 end;
  153.                 NewGridSize := min(Xgrid, Ygrid);
  154.  
  155.                 { Set the origin so that points may be specified with index * currentGridSize }
  156.                 with newBounds do begin
  157.                     newX := (left*newGridSize) div currentGridSize;
  158.                     newY := (top*newGridSize) div currentGridSize;
  159.                 end;
  160.             end
  161.             else begin
  162.                 alrslt := NoteAlert(IEalertID, nil);
  163.                 newGridSize := currentGridSize
  164.             end;
  165.         flushEvents(everyEvent,0);
  166. end;
  167.  
  168. procedure zoomer(level:integer);
  169. begin
  170.  
  171.     HideControl(VerticalScrollBar[windowIndex]);
  172.     HideControl(HorizontalScrollBar[windowIndex]);
  173.  
  174.     currentGridSize := newGridSize;
  175.     fixOrigin(newX,newY);
  176.  
  177.     fixPlotRect;
  178.  
  179.     { Force complete replot }
  180.     InvalRect(ContourWindow[windowIndex]^.portRect);
  181.  
  182.     { Adjust scroll bar value and position }
  183.     fixScrollBars;
  184.     MoveScrollBars;
  185.  
  186.  
  187. end;
  188.  
  189.  
  190. procedure ContourScroll;  {(dh, dv : integer)}
  191.     { Determine the minimum amount of scrolling required to update the window. }
  192.     { Only scroll the plotRect, without the previous update region, }
  193.     { and inside the visible region.  ScrollRect then returns the correct update region. }
  194. var
  195.     updateRgn : RgnHandle;
  196.     plotRgn : RgnHandle;
  197.     copyUpdate : RgnHandle;
  198.     peek : WindowPeek;
  199.     ScrollR : Rect;
  200. begin
  201.     SetPort(ContourWindow[windowIndex]);
  202.     peek := WindowPeek(ContourWindow[windowIndex]);
  203.     fixPlotRect;
  204.  
  205.         { first optain a copy of the current update region }
  206.     copyUpdate := NewRgn;
  207.     CopyRgn(peek^.updateRgn, copyUpdate);
  208.  
  209.         { give the region local coordinates }
  210.     with ContourWindow[windowIndex]^.portBits.bounds do
  211.         OffsetRgn(copyUpdate, left, top);
  212.  
  213.         { make a region out of the whole plotting rectangle }
  214.     plotRgn := NewRgn;
  215.     RectRgn(plotRgn, plotRect[windowIndex]);
  216.  
  217.         { remove the current update region }
  218.     DiffRgn(plotRgn, copyUpdate, plotRgn);
  219.         { intersect with the current visible region }
  220.     SectRgn(plotRgn, ContourWindow[windowIndex]^.VisRgn, plotRgn);
  221.  
  222.         { Get the scrolling rectangle from the resulting regions bounding box }
  223.     scrollR := plotRgn^^.rgnBBox;
  224.  
  225.     DisposeRgn(copyUpdate);
  226.     DisposeRgn(plotRgn);
  227.  
  228.         { Scroll the rectangle and invalidate the returned updateRgn }
  229.     updateRgn := NewRgn;
  230.     ScrollRect(scrollR, dh, dv, updateRgn);
  231.     InvalRgn(updateRgn);
  232.  
  233.     DisposeRgn(updateRgn);
  234.  
  235.  
  236.     with ContourWindow[windowIndex]^.portRect do
  237.         setOrigin(left - dh, top - dv);
  238.  
  239.     fixPlotRect;
  240.     OffsetScrollBars(-dh, -dv);
  241.  
  242. end;
  243.  
  244.  
  245. procedure ScrollBits;
  246. var
  247.     newOrigin : point;
  248.     dh, dv : INTEGER;
  249. begin
  250.     with ContourWindow[windowIndex]^ do begin
  251.         newOrigin.h := GetCtlValue(HorizontalScrollBar[windowIndex]);
  252.         newOrigin.v := GetCtlValue(VerticalScrollBar[windowIndex]);
  253.         dh := portRect.left - newOrigin.h;
  254.         dv := portRect.top - newOrigin.v;
  255.         ContourScroll(dh, dv);
  256.     end
  257. end;
  258.  
  259.  
  260. procedure ScrollUp (whichControl : ControlHandle;
  261.                     theCode : INTEGER);
  262. begin
  263.     if theCode = inUpButton then begin
  264.         SetCtlValue(whichControl, GetCtlValue(whichControl) -
  265.                     trunc(5 * sqrt(currentGridSize)));
  266.         ScrollBits
  267.    end
  268. end;
  269.  
  270.  
  271. procedure ScrollDown (whichControl : ControlHandle;
  272.                       theCode : INTEGER);
  273. begin
  274.     if theCode = inDownButton then begin
  275.         SetCtlValue(whichControl, GetCtlValue(whichControl) +
  276.                      trunc(5 * sqrt(currentGridSize)));
  277.         ScrollBits
  278.     end
  279. end;
  280.  
  281.  
  282. procedure PageScroll (whichControl : ControlHandle;
  283.                       code, direction : INTEGER);
  284. var
  285.     myPt : point;
  286.     amount : integer;
  287. begin
  288.     amount := PlotRect[windowIndex].bottom - PlotRect[windowIndex].top;
  289.  
  290.     repeat
  291.         GetMouse(myPt);
  292.         if TestControl(whichControl, myPt) = code then begin
  293.             SetCtlValue(whichControl, GetCtlValue(whichControl) + amount * direction);
  294.             ScrollBits
  295.         end
  296.     until not StillDown;
  297. end;
  298.  
  299. procedure Scroll(myControl : ControlHandle; code : integer);
  300. var
  301.     currentValue : integer;
  302.     myPt : point;
  303.     delta : integer;
  304.  begin
  305.     currentValue := GetCtlValue(myControl);
  306.     case code of
  307.     inUpButton :
  308.         delta := -10;
  309.     inDownButton : 
  310.         delta := 10;
  311.     inPageUp :
  312.         delta := -30;
  313.     inPageDown :
  314.         delta := 30;
  315.     end;
  316.     
  317.     if (code = inPageUp) or (code = inPageDown) then
  318.         repeat
  319.             GetMouse(myPt);
  320.             if TestControl(myControl,myPt) = code then begin
  321.                 currentValue := currentValue + delta;
  322.                 SetCtlValue(myControl,currentValue);
  323.             end;
  324.         until not StillDown
  325.     else
  326.         SetCtlValue(myControl,CurrentValue+delta);
  327.  end;
  328.  
  329.  procedure DoContent;  {(myEvent : EventRecord; theWindow : WindowPtr) }
  330.   var
  331.    thePoint : Point;
  332.    theControlPart : integer;
  333.    theControl : ControlHandle;
  334.    t : integer;
  335.    currentValue : integer;
  336. begin
  337.     thePoint := myEvent.where;
  338.     GlobalToLocal(thePoint);
  339.     theControlPart := FindControl(thePoint, theWindow, theControl);
  340.  
  341.     if theControlPart <> 0  then
  342.         if theWindow = ContourWindow[windowIndex] then
  343.              case theControlPart of
  344.              inUpButton : 
  345.              t := TrackControl(theControl, thePoint, @ScrollUp);
  346.              inDownButton : 
  347.              t := TrackControl(theControl, thePoint, @ScrollDown);
  348.              inPageUP : 
  349.              PageScroll(theControl, theControlPart, -1);
  350.              inPageDown : 
  351.              PageScroll(theControl, theControlPart, 1);
  352.              inThumb : 
  353.              begin
  354.              t := TrackControl(theControl, thePoint, nil);
  355.              ScrollBits
  356.              end;
  357.              otherwise
  358.              end { case }
  359.         else begin
  360.             currentValue := GetCtlValue(theControl);
  361.             if theControlPart = inThumb then
  362.                 t := TrackControl(theControl,thePoint,nil)
  363.             else
  364.                 t := TrackControl(theControl,thePoint,@Scroll);
  365.             if currentValue <> GetCtlValue(theControl) then begin
  366.                 InvalRect(myRect[windowIndex]);
  367.                 xTops[windowIndex] := GetCtlValue(TDHBar[windowIndex]);
  368.                 yTops[windowIndex] := GetCtlValue(TDVBar[windowIndex]);
  369.                 CreateData(xTops[windowIndex],yTops[windowIndex]);
  370.             end;
  371.         end
  372.         else
  373.             if theWindow = ContourWindow[windowIndex] then
  374.                 ContourContent
  375.             else
  376.                 redrawBox;
  377.                 
  378.  end; { DoContent }
  379.  
  380.     function StrtoInt (var value : integer;  strng : str255): boolean;
  381.     var
  382.        i: integer;
  383.          ok : boolean;
  384.     begin
  385.     
  386.       value := 0;
  387.         ok := true;
  388.         
  389.       for i := 1 to length (strng) do
  390.            if ((strng [i] >= '0') and (strng [i] <= '9')) then
  391.           value := (value * 10) + (ord (strng[i]) - $30)
  392.              else
  393.           ok := false;
  394.                     
  395.           StrtoInt := ok and (length(strng) <> 0);
  396.   end;
  397.  
  398. function getITdims;{(var Horiz, Vert: integer; mag:longint; level, xOrigin, yOrigin,
  399.                      minData, maxData : integer) : boolean;}
  400. var
  401.     aboutDITL : DialogPtr;
  402.     ItemNo, rslt : integer;
  403.     okHandle,thndl : handle;
  404.     box : rect;
  405.     numb : str255;
  406.     cont, allNumbers : boolean;
  407.     savePort : WindowPtr;
  408.     oldmag : longint;
  409.     oldLevel,oldx,oldy,oldmin,oldmax : integer;
  410.     
  411.     procedure restoreData;
  412.     begin
  413.         oldmag := mag; oldLevel := level; oldx := xOrigin;
  414.         oldy := yOrigin; oldmin := minData; oldmax := maxData;
  415.         GetDItem(aboutDITL,hditem,rslt,thndl,box);
  416.         SetDItem(aboutDITL,hditem,statText,thndl,box);
  417.         NumToString(longint(Horiz),numb);
  418.         SetIText(thndl,numb);
  419.         GetDItem(aboutDITL,vditem,rslt,thndl,box);
  420.         SetDItem(aboutDITL,vditem,statText,thndl,box);
  421.         NumToString(longint(Vert),numb);
  422.         SetIText(thndl,numb);
  423.         GetDItem(aboutDITL,magItem,rslt,thndl,box);
  424.         NumToString(mag,numb);
  425.         SetIText(thndl,numb);
  426.         GetDItem(aboutDITL,levelItem,rslt,thndl,box);
  427.         NumToString(longint(level),numb);
  428.         SetIText(thndl,numb);
  429.         GetDItem(aboutDITL,xAxisItem,rslt,thndl,box);
  430.         NumToString(longint(xOrigin),numb);
  431.         SetIText(thndl,numb);
  432.         GetDItem(aboutDITL,yAxisItem,rslt,thndl,box);
  433.         NumToString(longint(yOrigin),numb);
  434.         SetIText(thndl,numb);
  435.         GetDItem(aboutDITL,minItem,rslt,thndl,box);
  436.         NumToString(longint(minData),numb);
  437.         SetIText(thndl,numb);
  438.         GetDItem(aboutDITL,maxItem,rslt,thndl,box);
  439.         NumToString(longint(maxData),numb);
  440.         SetIText(thndl,numb);
  441.         SelIText(aboutDITL,magItem,0,4);
  442.     end;
  443. begin
  444.     GetPort(savePort);
  445.     aboutDITL := getnewDialog(dimenID,nil,pointer(-1));
  446.     if maxData <> -1 then
  447.         restoreData
  448.     else
  449.         SelIText(aboutDITL,hditem,0,3);
  450.     setPort(aboutDITL);
  451.     GetDItem(aboutDITL,okItem,rslt,okHandle,box);
  452.     PenSize(3,3);
  453.     InsetRect(box,-4,-4);
  454.     FrameRoundRect(box,16,16);
  455.     PenSize(1,1);
  456.     cont := true;
  457.     while cont do begin
  458.         ModalDialog(nil,ItemNo);
  459.         if ItemNo = cancelItem then 
  460.             cont := false
  461.         else begin
  462.             allNumbers := false;
  463.             GetDItem(aboutDITL,hditem,rslt,thndl,box);
  464.             GetIText(thndl,numb);
  465.             if StrToInt(Horiz,numb) and (Horiz > 0) then begin
  466.                 GetDItem(aboutDITL,vditem,rslt,thndl,box);
  467.                 GetIText(thndl,numb);
  468.                 if StrToInt(Vert,numb) and (Vert > 0)  then begin
  469.                 GetDItem(aboutDITL,magItem,rslt,thndl,box);
  470.                 GetIText(thndl,numb);
  471.                 if StrToInt(level,numb) and (level > 0) then begin
  472.                 mag := longint(level);
  473.                 GetDItem(aboutDITL,levelItem,rslt,thndl,box);
  474.                 GetIText(thndl,numb);
  475.                 if StrToInt(level,numb) and (level> 0) and
  476.                            (level <= maxLevels) then begin
  477.                 GetDItem(aboutDITL,xAxisItem,rslt,thndl,box);
  478.                 GetIText(thndl,numb);
  479.                 if StrToInt(xOrigin,numb) and (xOrigin <= Horiz) then begin
  480.                 GetDItem(aboutDITL,yAxisItem,rslt,thndl,box);
  481.                 GetIText(thndl,numb);
  482.                 if StrToInt(yOrigin,numb) and (yOrigin <= Vert) then begin
  483.                 GetDItem(aboutDITL,minItem,rslt,thndl,box);
  484.                 GetIText(thndl,numb);
  485.                 if StrToInt(minData,numb) and (minData >= 0) then begin
  486.                 GetDItem(aboutDITL,maxItem,rslt,thndl,box);
  487.                 GetIText(thndl,numb);
  488.                 if StrToInt(maxData,numb) and (maxData > minData) and
  489.                            (maxData <= 255) then begin
  490.                     if ItemNo = ok then
  491.                         cont := false;
  492.                     allNumbers := true;
  493.                 end; end; end; end; end; end; end;
  494.             end;
  495.             if allNumbers then
  496.                 HiliteControl(ControlHandle(okHandle),0)
  497.             else
  498.                 HiliteControl(ControlHandle(okHandle),255);
  499.         end;
  500.     end;
  501.     if ItemNo = CancelItem then begin
  502.         GetITDims := false;
  503.         mag := oldMag; level := oldLevel; xOrigin := oldx;
  504.         yOrigin := oldy; minData := oldmin; maxData := oldmax;
  505.     end
  506.     else
  507.         GetITDims := true;
  508.     setPort(savePort);
  509.     DisposDialog(aboutDITL);
  510. end;
  511.  
  512.  procedure redraw;
  513.  var
  514.     xlow,xhigh,ylow,yhigh : integer;
  515.  begin
  516.     
  517.     getMatrixBounds(plotRect[windowIndex],xlow,xhigh,ylow,yhigh);
  518.     if getITDims(maxX[windowIndex],maxY[windowIndex], currentGridSize, NoOfLevels[windowIndex],
  519.                  xlow,ylow, minData[windowIndex], maxData[windowIndex]) then begin
  520.         fixOrigin(xlow*currentGridSize,ylow*currentGridSize);
  521.         fixPlotRect;
  522.         fixScrollBars;
  523.         MoveScrollBars;
  524.         CalculateLevels;
  525.         InvalRect(PlotRect[windowIndex]);
  526.     end;
  527. end;
  528.                  
  529.  
  530.  procedure ProcessContourMenu;
  531.  begin
  532.  
  533.   case menuid of
  534.    PlotMenuID : 
  535.      case menuitem of
  536.      1: begin
  537.        if ThreeDWindow[windowIndex] <> nil then
  538.             closeWindow(ThreeDWindow[windowIndex]);
  539.        if shadeSurface[windowIndex] then begin
  540.             shadeSurface[windowIndex] := false;
  541.             InvalRect(plotRect[windowIndex]);
  542.             CheckItem(PlotMenu,menuitem,true);
  543.             CheckItem(PlotMenu,2,false);
  544.             CheckItem(PlotMenu,3,false);
  545.        end;
  546.      end;
  547.            2 : (* Shade Surface *) begin
  548.        if ThreeDWindow[windowIndex] <> nil then
  549.             closeWindow(ThreeDWindow[windowIndex]);
  550.        if not shadeSurface[windowIndex] then begin   
  551.             shadeSurface[windowIndex] := true;
  552.             InvalRect(plotRect[windowIndex]);
  553.                   CheckItem(PlotMenu, menuitem, true);
  554.             CheckItem(plotMenu,1,false);
  555.             CheckItem(plotMenu,3,false);
  556.        end;
  557.      end;
  558.            
  559.            3 : (* 3D *) begin
  560.          CheckItem(PlotMenu,menuItem,true);
  561.          CheckItem(PlotMenu,1,false);
  562.          CheckItem(PlotMenu,2,false);
  563.                ThreeDPlot(marqueeRect[windowIndex]);
  564.      end;
  565.  
  566.      5 : begin
  567.          toggle[windowIndex] := not toggle[windowIndex];
  568.          InvalRect(myRect[windowIndex]);
  569.          end;
  570.      
  571.      6 : begin
  572.          gridOn[windowIndex] := not gridOn[windowIndex];
  573.          CheckItem(PlotMenu,menuItem,gridOn[windowIndex]);
  574.          if gridOn[windowIndex] then begin
  575.             getMatrixBounds(plotRect[windowIndex],Xlow,Xhigh,Ylow,Yhigh);
  576.             DrawGrid(xlow,xhigh,ylow,yhigh);
  577.          end
  578.          else
  579.             InvalRect(plotRect[windowIndex]);
  580.      end;
  581.          
  582.          otherwise;
  583.      end; (* case *)
  584.  
  585.    GoodiesMenuID : 
  586.     case menuItem of 
  587.     1,2 : 
  588.         zoomer(menuItem);
  589.     3 : 
  590.         redraw;
  591.     otherwise;
  592.     end;
  593.  
  594.  
  595.   end; (* case *)
  596. end;
  597.  
  598. procedure closeWindow; {myWindow: WindowPtr}
  599.  
  600. var
  601.     i : integer;
  602.     resFileNum : integer;
  603.     
  604.     procedure FixMenuBar;
  605.     begin
  606.         if NoOfWindows = maxWindows then
  607.             EnableItem(FileMenu,1);
  608.         NoOfWindows := NoOfWindows -1;
  609.         if NoOfWindows = 0 then begin
  610.             DisableItem(FileMenu,2);
  611.             DisableItem(FileMenu,3);
  612.             DisableItem(FileMenu,4);
  613.             DeleteMenu(PlotMenuID);
  614.             DeleteMenu(goodiesMenuID);
  615.             DisposeMenu(PlotMenu);
  616.             DisposeMenu(GoodiesMenu);
  617.         end;
  618.         DrawMenuBar;
  619.     end;
  620.  
  621.     procedure ReInit;
  622.     begin
  623.         DisposeWindow(ContourWindow[windowIndex]);
  624.         ContourWindow[windowIndex] := nil;
  625.         if ThreeDWindow[windowIndex] <> nil then begin
  626.             DisposeWindow(ThreeDWindow[windowIndex]);
  627.             ThreeDWindow[windowIndex] := nil;
  628.         end;
  629.         secondTime[windowIndex] := false;
  630.         shadeSurface[windowIndex] := False;
  631.         gridOn[windowIndex] := false;
  632.         selection[windowIndex] := false;
  633.         zooming[windowIndex] := false;
  634.         zoomed[windowIndex] := false;
  635.         ThreeDWindow[windowIndex] := nil;
  636.         ContourWindow[windowIndex] := nil;
  637.         theFile[windowIndex] := -1;
  638.     end;
  639.  
  640. begin
  641.     if myWindow = ThreeDWindow[windowIndex] then begin
  642.         setPort(ContourWindow[windowIndex]);
  643.         CheckItem(PlotMenu,3,false);
  644.         if shadeSurface[windowIndex] then
  645.             CheckItem(PlotMenu,2,true)
  646.         else
  647.             CheckItem(PlotMenu,1,true);
  648.         HideWindow(myWindow);
  649.     end
  650.     else if myWindow = ContourWindow[windowIndex] then begin
  651.         FixMenuBar;
  652.         resultCode := FSClose(theFile[windowIndex]);
  653.         ReInit;
  654.         if NewFile[windowIndex] then begin
  655.             createResFile(resName[windowIndex]);
  656.             resFileNum := OpenResFile(resName[windowIndex]);
  657.             dimen[windowIndex] := dimenHandle(NewHandle(sizeOf(dimensions)));
  658.             dimen[windowIndex]^^.maxX := maxX[windowIndex];
  659.             dimen[windowIndex]^^.maxY := maxY[windowIndex];
  660.             dimen[windowIndex]^^.gridSize := currentGridSize;
  661.             dimen[windowIndex]^^.NoOfLevels := NoOfLevels[windowIndex];
  662.             dimen[windowIndex]^^.xOrigin := xOrigin[windowIndex];
  663.             dimen[windowIndex]^^.yOrigin := yOrigin[windowIndex];
  664.             dimen[windowIndex]^^.minData := minData[windowIndex];
  665.             dimen[windowIndex]^^.maxData := maxData[windowIndex];
  666.             AddResource(handle(dimen[windowIndex]),'crac',resID,'');
  667.             CloseResFile(resFileNum);
  668.         end;
  669.     end;
  670.     if NoOfWindows <> 0 then
  671.         ChangeWindow(FrontWindow)
  672.     else
  673.         windowIndex := 0;
  674.  end;
  675.  
  676. procedure DoAbout;
  677. const
  678.     dlgID = 8863;
  679. var
  680.     contourDialog : DialogPtr;
  681.     tempRect : Rect;
  682. begin
  683.     contourDialog := GetNewDialog(dlgID,nil,pointer(-1));
  684.     tempRect := contourDialog^.PortRect;
  685.     with ScreenBits.bounds do
  686.         MoveWindow(contourDialog,(right-(tempRect.right-tempRect.left)) div 2,
  687.                    (bottom-(tempRect.bottom-tempRect.top)) div 2,true);
  688.     ShowWindow(contourDialog);
  689.     DrawDialog(contourDialog);
  690.     while button do;
  691.     repeat until button;
  692.     FlushEvents(everyEvent,0);
  693.     DisposDialog(contourDialog);
  694. end;
  695. procedure fixOrigin; {(x,y : integer)}
  696. var
  697.     xLength,
  698.     yLength : longint;
  699. begin
  700.     with plotRect[windowIndex] do begin
  701.         xLength := right - left;
  702.         yLength := bottom - top;
  703.     end;
  704.     if (x+xLength) > currentGridSize*maxX[windowIndex] then 
  705.         x := maxX[windowIndex]*currentGridSize-xLength;
  706.     if x < 0 then
  707.         x := 0;
  708.     if (y+yLength) > currentGridSize*maxY[windowIndex] then
  709.         y := maxY[windowIndex]*currentGridSize-yLength;
  710.     if y < 0 then
  711.         y := 0;
  712.     setOrigin(x,y);
  713. end;
  714.  
  715. procedure GetPicture(picFrame : rect;NewGrid : longint;TwoD : boolean);
  716. var
  717.     temp : longint;
  718. begin
  719.     setCursor(ClockCursor^^);
  720.     clipRgn := newRgn;
  721.     GetClip(clipRgn);
  722.     ClipRect(picFrame);
  723.     myPict := OpenPicture(picFrame);
  724.     if TwoD then begin
  725.         temp := currentGridSize;
  726.         currentGridSize := NewGrid;
  727.         contourPlot(picFrame);
  728.         currentGridSize := temp;
  729.     end
  730.     else
  731.         Prepare3DPlot;
  732.     closePicture;
  733.     SetClip(clipRgn);
  734.     DisposeRgn(clipRgn);
  735.     InitCursor;
  736. end;
  737.  
  738. procedure DoPrint;
  739. var
  740.     mark : char;
  741.     PlotArea : rect;
  742.     temp : longint;
  743.     p : windowPtr;
  744.     xlow,xhigh,
  745.     ylow,yhigh : integer;
  746. begin
  747.     if PrJobDialog(prRecHdl) then begin
  748.         GetPort(p);
  749.         myPrPort := PrOpenDoc(prRecHdl,nil,nil);
  750.         if PrError =  noErr then begin
  751.             with prRecHdl^^.prInfo.rPage do
  752.                if selection[windowIndex] then begin
  753.                     GetMatrixBounds(marqueeRect[windowIndex],xlow,xhigh,ylow,yhigh);
  754.                     temp := currentGridSize;
  755.                     currentGridSize := min(right div (xhigh-xlow),bottom div (yhigh-ylow));
  756.                     setRect(plotArea,xlow*currentGridSize,ylow*currentGridSize,
  757.                             xhigh*currentGridSize,(yhigh-1)*currentGridSize);
  758.                     {writeln(xlow:5,xhigh:5,ylow:5,yhigh:5,currentGridSize:10);
  759.                     with plotArea do writeln(left:5,top:5,right:5,bottom:5);}
  760.                     setOrigin(xlow*currentGridSize,ylow*currentGridSize);
  761.                  end
  762.                 else begin
  763.                     temp := currentGridSize;
  764.                     currentGridSize := min(right div maxX[windowIndex],bottom div maxY[windowIndex]);
  765.                     if currentGridSize = 0 then currentGridSize := 1;
  766.                     setRect(plotArea,0,0,currentGridSize*maxX[windowIndex],currentGridSize*maxY[windowIndex]);
  767.                 end;
  768.             PrOpenPage(myPrPort,nil);
  769.             if PrError = noErr then begin
  770.                 clipRect(plotArea);
  771.                 ContourPlot(plotArea);
  772.                 currentGridSize := temp;
  773.             end;
  774.             PRClosePage(myPrPort);
  775.         end;
  776.         PrCloseDoc(myPrPort);
  777.         if (prRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) then begin
  778.             sysbeep(1); sysbeep(1);
  779.             PrPicFile(prRecHdl,nil,nil,nil,PrStRec);
  780.         end;
  781.         if PrError <> noErr then
  782.             sysbeep(9);
  783.         SetPort(p);
  784.     end;
  785. end;
  786.  
  787. procedure DoScrap;
  788. var
  789.     picFrame : rect;
  790.     NewGrid : longint;
  791. begin
  792.     resultCode := ZeroScrap;
  793.     if resultCode <> 0 then begin sysbeep(1);sysbeep(3);end;
  794.     if myWindow = ContourWindow[windowIndex] then begin
  795.         if selection[windowIndex] then begin
  796.             NewGrid := currentGridSize;
  797.             picFrame := marqueeRect[windowIndex];
  798.         end
  799.         else begin
  800.             NewGrid := 1;
  801.             setRect(picFrame,0,0,maxX[windowIndex],maxY[windowIndex]);
  802.         end;
  803.         GetPicture(picFrame,NewGrid,true);
  804.     end
  805.     else begin
  806.         setRect(picFrame,100,0,460,284);
  807.         GetPicture(picFrame,NewGrid,false);
  808.     end;
  809.     HLock(handle(myPict));
  810.     resultCode := PutScrap(GetHandleSize(handle(myPict)),'PICT',ptr(myPict^));
  811.     if resultCode <>  0 then sysbeep(9);
  812.     KillPicture(myPict);
  813. end;
  814.  
  815. procedure DoSave;
  816. var
  817.     picFrame : rect;
  818.     where : point;
  819.     reply : SFReply;
  820.     buffer : packed array[0..511] of char;
  821.     i : integer;
  822.     theFile : integer;
  823.     count : longint;
  824. begin
  825.     SetPt(where,100,100);
  826.     SFPutFile(where,'','contours',nil,reply);
  827.     if reply.good then 
  828.         with reply do begin
  829.             resultCode := Create(fname,vRefNum,'crac','PICT');
  830.             resultCode := FSOpen(fname,vRefNum,theFile);
  831.             for i := 0 to 511 do
  832.                 buffer[i] := chr(0);
  833.             count := 512;
  834.             resultCode := FSWrite(theFile,count,@buffer);
  835.             if myWindow = ContourWindow[windowIndex] then begin
  836.                 setRect(picFrame,0,0,maxX[windowIndex],maxY[windowIndex]);
  837.                 GetPicture(picFrame,1,true);
  838.             end
  839.             else 
  840.             if myWindow = ThreeDWindow[windowIndex] then begin
  841.                 setRect(picFrame,100,0,460,284);
  842.                 GetPicture(picFrame,1,false);
  843.             end;
  844.             Hlock(handle(myPict));
  845.             count := GetHandleSize(handle(myPict));
  846.             resultCode := FSWrite(theFile,count,ptr(myPict^));
  847.             resultCode := FSClose(theFile);          
  848.         end;
  849. end; 
  850. end.
  851.