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

  1. {$U-}
  2. {$R-}
  3. unit General(60);
  4.  
  5. interface
  6. uses
  7.     MemTypes,Quickdraw,OSIntf,ToolIntf,MacPrint,PackIntf,FixMath,Graf3D,
  8.     Globals,utilities,Routines,ThreeD,Interf,Init;
  9.  
  10. procedure InitThings;
  11. procedure SetUpThings;
  12. procedure MainEventLoop;
  13. procedure CloseThings;
  14.  
  15. implementation
  16.  
  17. procedure DealwthUpdates(Event: EventRecord); forward;
  18.  
  19. procedure InitData; forward;
  20.  
  21. procedure ReSizeWindow (theWindow : WindowPtr;
  22.                         MouseLoc : Point);
  23. var
  24.     NewSize : Point;
  25.     oldWidth,oldHeight,
  26.     oldBottom,oldRight,
  27.     Width : Integer;
  28.     Height : Integer;
  29.     R : Rect;
  30. begin
  31.     SetPort(theWindow);
  32.     NewSize.v := HiWord(GrowWindow(theWindow, MouseLoc, GrowArea));
  33.     NewSize.h := LoWord(GrowWindow(theWindow, MouseLoc, GrowArea));
  34.  
  35.     if longint(NewSize) <> 0 then begin
  36.         Height := NewSize.v;
  37.         Width := NewSize.h;
  38.     
  39.           {invalidate size box}
  40.         with theWindow^.portRect do
  41.             SetRect(R,right-ScrollBarWidth,bottom-ScrollBarWidth,right,bottom);
  42.         InvalRect(R);
  43.     
  44.         if Height < currentGridSize then
  45.             Height := currentGridSize; {don't let the window close on itself}
  46.         if Width < currentGridSize then
  47.             Width := currentGridSize;
  48.  
  49.       {now set the new size}
  50.         SizeWindow(theWindow, Width, Height, true);   {resize this Window}
  51.  
  52.         fixPlotRect;
  53.         fixScrollBars;
  54.         MoveScrollBars;
  55.     end;
  56. end;
  57.  
  58. procedure Zoom(theWindow: WindowPtr;
  59.                            thePt : Point; partCode: integer);
  60.                 
  61. begin
  62.     if TrackBox(theWindow,thePt,partCode) then begin
  63.         SetPort(theWindow);
  64.         ZoomWindow(theWindow,partCode,false);
  65.         fixPlotRect;
  66.         fixScrollBars;
  67.         MoveScrollBars;
  68.     end;
  69. end;
  70.  
  71. {-------------------------------------------------------------------}
  72.  
  73. procedure ProcessMenu_in (CodeWord : longint);
  74. var
  75.     Menu_No : integer;        {menu number that was selected}
  76.     Item_No : integer;        {item in menu that was selected}
  77.     NameHolder : Str255;         {name holder for desk accessory or font}
  78.     i,
  79.     DNA : integer;        {OpenDA will never return 0, so don't care}
  80. begin
  81.     if CodeWord <> 0 then begin {go ahead and process the command}
  82.         Menu_No := HiWord(CodeWord);   {get the Hi word of...}
  83.         Item_no := LoWord(CodeWord);   {get the Lo word of...}
  84.  
  85.         case Menu_No of
  86.         AppleMenuID : 
  87.             if Item_NO = 1 then 
  88.                 DoAbout
  89.             else begin
  90.                GetItem(GetMHandle(AppleMenuID), Item_No, NameHolder);
  91.                DNA := OpenDeskAcc(NameHolder);
  92.             end;
  93.          FileMenuID :
  94.              case Item_No of
  95.                    1 : 
  96.                       InitData;
  97.                    2 : DoSave(FrontWindow);
  98.              3 : if PrStlDialog(prRecHdl) then;
  99.              4 : DoPrint;
  100.                    6 : 
  101.                        Finished := True;    {quit}
  102.                    otherwise;
  103.             end;
  104.          EditMenuID : begin
  105.              if not SystemEdit(Item_No - 1) then {if not for a desk accessory}
  106.                  case Item_No of
  107.                  4 : 
  108.                     if FrontWindow <> nil then
  109.                         DoScrap(FrontWindow);
  110.                  end;
  111.          end;
  112.          otherwise 
  113.             ProcessContourMenu(Menu_No, Item_No);
  114.          end; { case }
  115.         HiliteMenu(0);               {unhilite after processing menu}
  116.    end;
  117.  
  118. end; {of W procedure}
  119.  
  120.  
  121. {-------------------------------------------------------------------}
  122. {----- These are procedures called from the main event loop  -------}
  123.  
  124. procedure DealwthMouseDowns (Event : EventRecord);
  125. var
  126.     Location : integer;
  127.     WindowPointedTo : WindowPtr;
  128.     MouseLoc : Point;
  129.     WindoLoc : integer;
  130.     
  131.     
  132. begin
  133.     MouseLoc := Event.Where;
  134.     WindoLoc := FindWindow(MouseLoc, WindowPointedTo);
  135.     case WindoLoc of
  136.  
  137.     inMenuBar : 
  138.         ProcessMenu_in(MenuSelect(MouseLoc));
  139.  
  140.     inSysWindow : 
  141.         SystemClick(Event, WindowPointedTo);
  142.  
  143.     inContent : 
  144.         if WindowPointedTo <> FrontWindow then
  145.             ChangeWindow(WindowPointedTo)
  146.         else
  147.             DoContent(Event, WindowPointedTo);
  148.  
  149.     inGrow : 
  150.         if WindowPointedTo <> FrontWindow then
  151.             ChangeWindow(WindowPointedTo)
  152.         else
  153.             ReSizeWindow(WindowPointedTo, MouseLoc);
  154.  
  155.     inDrag : 
  156.         if (WindowPointedTo = FrontWindow) or
  157.            (BitAnd(event.modifiers,CmdKey) <>  0) then
  158.             DragWindow(WindowPointedTo, MouseLoc, DragArea)
  159.         else
  160.             changeWindow(windowPointedTo);
  161.     
  162.     inZoomIn,
  163.     inZoomOut :
  164.         if WindowPointedTo <> FrontWindow then
  165.                      ChangeWindow(WindowPointedTo)
  166.         else
  167.             Zoom(WindowPointedTo,MouseLoc,WindoLoc);
  168.             
  169.     inGoAway : 
  170.         if TrackGoAway(WindowPointedTo, MouseLoc) then
  171.             closeWindow(WindowPointedTo);
  172.     otherwise;
  173.     end;{ of case}
  174. end;
  175.  
  176. {-----------------------------------------------------------------------------}
  177.  
  178. procedure DealwthKeyDowns (Event : EventRecord);
  179. var
  180.     CharCode : char;
  181. begin
  182.     CharCode := char(BitAnd(Event.message, $FF));
  183.  
  184.     if BitAnd(Event.modifiers, CmdKey) = CmdKey then
  185.       {key board command - probably a menu command}
  186.         ProcessMenu_in(MenuKey(CharCode));
  187. end;
  188.  
  189. {-----------------------------------------------------------------------------}
  190.  
  191. procedure DealwthActivates (Event : EventRecord);
  192. var
  193.     theWindow : WindowPtr;
  194.     savePort : WindowPtr;
  195.     i : integer;
  196. begin
  197.     theWindow := WindowPtr(Event.message);
  198.  
  199.     if Odd(Event.modifiers) then begin
  200.       {then the window is becoming active}
  201.       {and activate whatever else you need}
  202.         setPort(theWindow);
  203.         if theWindow = ContourWindow[windowIndex] then begin
  204.         {the scroll bars}
  205.             fixScrollBars;
  206.             MoveScrollBars;
  207.             DisableItem(PlotMenu,5);
  208.             EnableItem(PlotMenu,6);
  209.             EnableItem(GoodiesMenu,3);
  210.             DrawGrowIcon(theWindow);
  211.         end
  212.         else begin
  213.              EnableItem(PlotMenu,5);
  214.              DisableItem(GoodiesMenu,3);
  215.              DisableItem(PlotMenu,6);
  216.         end;
  217.         DrawControls(theWindow);
  218.     end
  219.     else begin
  220.         getPort(savePort);
  221.         setPort(theWindow);
  222.         {deactivate whatever you need}
  223.         {deactivate the scroll bars}
  224.         i := 1;
  225.         while (theWindow <> ContourWindow[i]) and
  226.             (theWindow <> threeDWindow[i]) do
  227.             i := i + 1;
  228.         if theWindow = ContourWindow[i] then begin
  229.             HideControl(HorizontalScrollBar[i]);
  230.             HideControl(VerticalScrollBar[i]);
  231.             DrawGrowIcon(theWindow);
  232.         end
  233.         else if theWindow = threeDWindow[i] then begin
  234.             Hidecontrol(TDHBar[i]);
  235.             HideControl(TDVBar[i]);
  236.         end;
  237.         setPort(savePort);
  238.  
  239.     end;
  240.     
  241.  
  242. end;
  243.  
  244. {-----------------------------------------------------------------------------}
  245.  
  246. procedure DealwthUpdates {(Event : EventRecord)};
  247. var
  248.     UpDateWindow, savePort : WindowPtr;
  249.     updateRect : Rect;
  250.     updateRgn : RgnHandle;
  251.     clipRgn : RgnHandle;
  252.     twoDWindow : boolean;
  253.     i : integer;
  254.     oldGridSize : longint;
  255.  
  256. begin
  257.     SetCursor(ClockCursor^^);
  258.     UpDateWindow := WindowPtr(Event.message);
  259.     GetPort(savePort);                {Save the current port}
  260.     SetPort(UpDateWindow);      {set the port to one in Evt.msg}
  261.     twoDWindow := false;
  262.     i := 1;
  263.     while not twoDWindow and (i <= maxWindows) do begin
  264.         if UpDateWindow = ContourWindow[i] then
  265.             twoDWindow := true;
  266.         i := i + 1;
  267.     end;
  268.     if not twoDWindow then begin
  269.         i := 1; twoDWindow := true;
  270.         while (i <= maxWindows) and twoDWindow do begin
  271.             if UpDateWindow = threeDWindow[i] then
  272.                 twoDWindow := false;
  273.             i := i + 1;
  274.         end;
  275.     end;
  276.     oldWindowIndex := windowIndex;
  277.     windowIndex := i-1;
  278.     oldGridSize := currentGridSize;
  279.     if oldWindowIndex <> windowIndex then 
  280.         currentGridSize := gridSize[windowIndex];
  281.     BeginUpDate(UpDateWindow);
  282.     begin
  283.         if twoDWindow then begin
  284.             updateRgn := NewRgn;
  285.             RectRgn(updateRgn, PlotRect[windowIndex]);
  286.             SectRgn(updateRgn, UpDateWindow^.VisRgn, updateRgn);
  287.             updateRect := updateRgn^^.rgnBBox;
  288.             {showRgn(UpdateWindow^.cliprgn);}
  289.             DisposeRgn(updateRgn);
  290.  
  291.             EraseRect(updateRect);
  292.             clipRgn := newRgn;
  293.             GetClip(clipRgn);
  294.             ClipRect(updateRect);
  295.             ContourPlot(updateRect);
  296.             SetClip(clipRgn);
  297.             disposeRgn(clipRgn);
  298.             DrawGrowIcon(UpdateWindow);
  299.         end
  300.         else begin
  301.             eraseRect(myRect[windowIndex]);
  302.             Refresh3D;
  303.             Prepare3DPlot;
  304.         end;
  305.     end;
  306.     EndUpDate(UpDateWindow);
  307.     if FrontWindow = UpDateWindow then
  308.         DrawControls(UpDateWindow);
  309.     InitCursor;
  310.     SetPort(savePort);
  311.     windowIndex := oldWindowIndex;
  312.     currentGridSize := oldGridSize;
  313. end;
  314.  
  315. {-----------------------------------------------------------------------------}
  316.  
  317. procedure MainEventStep;
  318. var
  319.     ProcessIt : Boolean;
  320. begin
  321.     SystemTask;             {so we can support Desk Accessories}
  322.     ProcessIt := GetNextEvent(EveryEvent-keyUpMask, theEvent);
  323.  
  324.     if ProcessIt then begin
  325.            {we'll ProcessIt}
  326.         case theEvent.what of
  327.         mouseDown : 
  328.             DealwthMouseDowns(theEvent);
  329.         KeyDown : 
  330.             DealwthKeyDowns(theEvent);
  331.         ActivateEvt : 
  332.             DealwthActivates(theEvent);
  333.         UpDateEvt : 
  334.             DealwthUpdates(theEvent);
  335.         otherwise; 
  336.         end; {of Case}
  337.            if (windowIndex <> 0) and zooming[windowIndex] then 
  338.                   if not zoomed[windowIndex] then
  339.                          zoomed[windowIndex] := true
  340.                   else
  341.                 UnZoom;
  342.     end;
  343. end;
  344.  
  345.  
  346.  procedure MainEventLoop;
  347.  begin
  348.   repeat
  349.    MainEventStep;
  350.   until Finished;
  351.  end;
  352.  
  353. {-----------------------------------------------------------------------------}
  354.  
  355. procedure InitThings;
  356.  
  357.     procedure InitLocalStuff;
  358.     var 
  359.         y : integer;
  360.     begin
  361.         windowIndex := 0;
  362.         NoOfWindows := 0;
  363.         for y := 1 to maxWindows do begin
  364.             zooming[y] := false;
  365.             zoomed[y] := false;
  366.             ThreeDWindow[y] := nil;
  367.             ContourWindow[y] := nil;
  368.             theFile[y] := -1;
  369.         end;
  370.         prRecHdl := THPrint(NewHandle(SizeOf(TPrint)));
  371.         PrOpen;
  372.         PrintDefault(PrRecHdl);
  373.         upHill := boolPtr(NewPtr(SizeOf(boolean)*maxWidth));
  374.             
  375.         for y := 0 to maxRows do begin
  376.             data[y] := ArrayPtr(NewPtr(maxWidth));
  377.             if data[y] = nil then begin
  378.                 Sysbeep(1); sysbeep(1); sysbeep(1);
  379.             end;
  380.               end;
  381.         Finished := False; {set program terminator to false}
  382.     end;
  383.  
  384. begin
  385.    {UnLoadSeg (&_DATAINIT);}
  386.     MoreMasters;                 {extra pointer blocks at the bottom of the heap}
  387.     MoreMasters;                 {this is 5 X 64 master pointers}
  388.     MoreMasters;
  389.     MoreMasters;
  390.     MoreMasters;
  391.     MoreMasters;
  392.     
  393.     InitGraf(@thePort);          {create a grafport for the screen}
  394.     InitGrf3D(@thePort3D);
  395.  
  396.      {get the cursors we use and lock them down - no clutter}
  397.     ClockCursor := GetCursor(watchCursor);
  398.     HLock(Handle(clockCursor));
  399.     {show the watch while we wait for inits & setups to finish}
  400.     SetCursor(ClockCursor^^);
  401.  
  402.      {init everything in case the app is the Startup App}
  403.  
  404.     InitFonts;                     {startup the fonts manager}
  405.     InitWindows;                 {startup the window manager}
  406.  
  407.     InitMenus;                     {startup the menu manager}
  408.     TEInit;                        {startup the text edit manager}
  409.     InitDialogs(nil);              {startup the dialog manager}
  410.     InitLocalStuff;
  411.     FlushEvents(everyEvent, 0);     {clear events from previous program}
  412. end;
  413.  
  414. {-----------------------------------------------------------------------------}
  415.  
  416. procedure SetupWindows;
  417. begin
  418.    { Create the window from information stored on the Resource file }
  419.  
  420.     ContourWindow[windowIndex] := GetNewWindow(WindResID, nil, POINTER(-1));      {Resource ID where Window info is }
  421.             {W Mgr allocates space for W. Record}
  422.    {set the window to be in front    }
  423.     if ContourWindow[windowIndex] = nil then SysBeep(1);
  424.     SetPort(ContourWindow[windowIndex]);
  425.     fixOrigin(xOrigin[windowIndex]*currentGridSize,yOrigin[windowIndex]*currentGridSize);
  426.     HorizontalScrollBar[windowIndex] := GetNewControl(HSBarID,ContourWindow[windowIndex]);
  427.     VerticalScrollBar[windowIndex] := GetNewControl(VSBarID,ContourWindow[windowIndex]);
  428.     fixPlotRect;
  429.     fixScrollBars;
  430.     MoveScrollBars;
  431.   
  432. end;
  433.  
  434.  
  435.  procedure InitData;
  436.  var
  437.     reply : SFReply;
  438.     dlgOrigin : Point;
  439.     theTypeList : SFTypeList;
  440.  resFileNum : integer;
  441.      
  442.  begin
  443.     SetPt(dlgOrigin,100,100);
  444.     theTypeList[0] := 'TEXT';
  445.     SFGetFile(dlgOrigin,'',nil,1,theTypeList,nil,reply);
  446.         if reply.good then begin
  447.        if windowIndex <> 0 then begin
  448.            gridSize[windowIndex] := currentGridSize;
  449.            oldWindowIndex := windowIndex;
  450.        end;
  451.        windowIndex := windowIndex + 1;
  452.        if windowIndex > maxWindows then
  453.             windowIndex := 1;
  454.        NoOfWindows := NoOfWindows + 1;
  455.              with reply do begin
  456.                      resultCode := FSOpen(fname,vRefNum,theFile[windowIndex]);
  457.                      if resultCode <> 0 then begin sysbeep(1); repeat until button end;
  458.                      resultCode := SetFPos(theFile[windowIndex],FSFromStart,0);
  459.             resName[windowIndex] := fname;
  460.             resFileNum := OpenResFile(fname);
  461.             dimen[windowIndex] := dimenhandle(GetResource('crac',resID));
  462.             if (dimen[windowIndex] = nil) or (resError = resNotFound) or button then begin
  463.                 newFile[windowIndex] := true;
  464.                 if dimen[windowIndex] <> nil then begin
  465.                     sysbeep(1);
  466.                     RmveResource(handle(dimen[windowIndex]));
  467.                 end;
  468.                 maxData[windowIndex] := -1;
  469.             end  
  470.             else begin
  471.                 NewFile[windowIndex] := false;
  472.                 maxX[windowIndex] := dimen[windowIndex]^^.maxX;
  473.                 maxY[windowIndex] := dimen[windowIndex]^^.maxY;
  474.                 currentGridSize := dimen[windowIndex]^^.gridSize;
  475.                 NoOfLevels[windowIndex] := dimen[windowIndex]^^.NoOfLevels;
  476.                 xOrigin[windowIndex] := dimen[windowIndex]^^.xOrigin;
  477.                 yOrigin[windowIndex] := dimen[windowIndex]^^.yOrigin;
  478.                 minData[windowIndex] := dimen[windowIndex]^^.minData;
  479.                 maxData[windowIndex] := dimen[windowIndex]^^.maxData;
  480.             end;
  481.               end;
  482.         if getITDims(maxX[windowIndex], maxY[windowIndex],currentGridSize,
  483.                      NoOfLevels[windowIndex],xOrigin[windowIndex],yOrigin[windowIndex],
  484.                      minData[windowIndex],maxData[windowIndex]) then begin
  485.              if NoOfWindows = 1 then begin
  486.                 SetUpContourMenus;
  487.                 SetUpContour;
  488.              end;
  489.                    SetUpWindows;
  490.                    InitTables;
  491.         end
  492.         else begin
  493.             resultCode := FSClose(theFile[windowIndex]);
  494.             windowIndex := windowIndex - 1;
  495.             currentGridSize := gridSize[windowIndex];
  496.             NoOfWindows := NoOfWindows - 1;
  497.             
  498.         end;
  499.         CloseResFile(resFileNum);
  500.         if NoOfWindows = maxWindows then
  501.             DisableItem(FileMenu,1);
  502.     end;
  503. end;
  504.     
  505.  
  506. {-----------------------------------------------------------------------------}
  507.  
  508.  procedure SetupLimits;
  509.  begin
  510.   Screen := ScreenBits.Bounds;   {set the size of the screen}
  511.   SetRect(DragArea, Screen.left + 4, Screen.top + 24, Screen.right - 4, Screen.bottom - 4);
  512.   SetRect(GrowArea, Screen.left, Screen.top + 24, Screen.right, Screen.bottom);
  513.  end;
  514.  
  515. {-----------------------------------------------------------------------------}
  516.  
  517.  procedure SetupMenus;
  518.  begin
  519.   FileMenu := GetMenu(AppleMenuID);  {get the apple desk accessories menu}
  520.   AddResMenu(FileMenu, 'DRVR');     {adds all names into item list}
  521.   InsertMenu(FileMenu, 0);          {put in list held by menu manager}
  522.  
  523.   FileMenu := GetMenu(FileMenuID);   {always need this for Quiting}
  524.   InsertMenu(FileMenu, 0);
  525.  
  526.   EditMenu := GetMenu(EditMenuID);   {always need for editing Desk Accessories}
  527.   InsertMenu(EditMenu, 0);
  528.  
  529.   DrawMenuBar;           {all done so show the menu bar}
  530.  end;
  531.  
  532. {-----------------------------------------------------------------------------}
  533.  
  534.  procedure SetUpThings;
  535.  begin
  536.   SetupMenus;
  537.   SetupLimits;
  538.   SetUpCursors;
  539.   doAbout;
  540.  end;
  541.  
  542. {-------------------------------------------------------------------------}
  543.  
  544.  procedure CloseThings;
  545.  var 
  546.     i : integer;
  547.  begin
  548.     for i := 1 to maxWindows do begin
  549.         windowIndex := i;
  550.         if contourWindow[windowIndex] <> nil then
  551.             CloseWindow(contourWindow[windowIndex]);
  552.     end;
  553.     DisposPtr(ptr(uphill));
  554.     for i := 0 to maxRows do
  555.         DisposPtr(ptr(data[i]));
  556. end;
  557.  
  558. end.
  559.