home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / map / plot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-09  |  4.8 KB  |  259 lines  |  [TEXT/KAHL]

  1. #include <Windows.h>
  2. #include <Quickdraw.h>
  3. #include <Menus.h>
  4. #include <Resources.h>
  5. #include <Memory.h>
  6. #include <Fonts.h>
  7. #include <OSEvents.h>
  8. #include <TextEdit.h>
  9. #include <Dialogs.h>
  10. #include <Desk.h>
  11. #include <ToolUtils.h>
  12. #include "trigtab.h"
  13.  
  14. trigtab *trig;
  15. int xmax,ymax;
  16. WindowPtr     myWindow;
  17. Rect        dragRect = { 0, 0, 1024, 1024 };
  18. GrafPtr     offscreen;/* our off-screen bitmap */
  19. MenuHandle        myMenus[3];
  20.  
  21. /* resource IDs of menus */
  22. #define appleID            128
  23. #define fileID            129
  24.  
  25. /* Menu indices */
  26. #define appleM            0
  27. #define fileM            1
  28.  
  29. #define fmNew        1
  30. #define fmClose        2
  31. #define fmPageSetUp    4
  32. #define fmPrint        5
  33. #define fmQuit        7
  34.  
  35. static int UpdateWindow (void)
  36. {
  37.     GrafPtr    savePort;
  38.     
  39.     GetPort(&savePort);
  40.     SetPort(myWindow);
  41.  
  42.     BeginUpdate(myWindow);
  43.     CopyBits(&offscreen->portBits, &(*myWindow).portBits,
  44.              &offscreen->portRect, &(*myWindow).portRect, srcCopy, 0L);
  45. /*
  46.     DrawControls(myWindow);
  47.     DrawGrowIcon(myWindow);
  48. */
  49.     EndUpdate(myWindow);
  50.  
  51.     SetPort(savePort);
  52. }
  53.  
  54. static int SetUpMenus(void)
  55. {
  56.     int        i;
  57.     
  58.     myMenus[appleM] = NewMenu(appleID, "\p\024");
  59.     AddResMenu(myMenus[appleM], 'DRVR');
  60.     myMenus[fileM] = GetMenu(fileID);
  61.     for ((i=appleM); (i<=fileM); i++)
  62.         InsertMenu(myMenus[i], 0) ;
  63.     DrawMenuBar();
  64. }
  65.  
  66. #define    ours(w)        ((myWindow != NULL) && (w == myWindow))
  67.  
  68. static int DoCommand(long mResult)
  69. {
  70.     int        theItem;
  71.     Str255    name;
  72.     
  73.     theItem = LoWord(mResult);
  74.     switch (HiWord(mResult)) {
  75.         case appleID:
  76.             GetItem(myMenus[appleM], theItem, name);
  77.             OpenDeskAcc(name);
  78.             SetPort(myWindow);
  79.             break;
  80.  
  81.         case fileID: 
  82.             return(0);
  83.             break;
  84.     }
  85.     HiliteMenu(0);
  86.     return(1);
  87. }
  88.  
  89. #define SBarWidth    15
  90.  
  91. static int MyGrowWindow(WindowPtr w, Point p)
  92.  
  93. {
  94.     GrafPtr    savePort;
  95.     long    theResult;
  96.     Rect    oldHorizBar;
  97.     Rect     r;
  98.     
  99.     GetPort(&savePort);
  100.     SetPort(w);
  101.     
  102.     oldHorizBar = w->portRect;
  103.     oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
  104.  
  105.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  106.     theResult = GrowWindow(w, p, &r);
  107.     if (theResult == 0)
  108.       return;
  109.     SizeWindow( w, LoWord(theResult), HiWord(theResult), false);
  110.  
  111.     InvalRect(&w->portRect);
  112.     
  113.     EraseRect(&oldHorizBar);
  114.     
  115.     SetPort(savePort);
  116. }
  117.  
  118. static int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  119. {
  120.     switch (windowPart) {
  121.         case inGoAway:
  122.             if (ours(whichWindow))
  123.                 if (TrackGoAway(myWindow, myEvent->where))
  124.                     ExitToShell();
  125.             break;
  126.  
  127.         case inMenuBar:
  128.             return(DoCommand(MenuSelect(myEvent->where)));
  129.  
  130.         case inSysWindow:
  131.             SystemClick(myEvent, whichWindow);
  132.             break;
  133.  
  134.         case inDrag:
  135.             if (ours(whichWindow))
  136.                 DragWindow(whichWindow, myEvent->where, &dragRect);
  137.             break;
  138.  
  139.         case inGrow:
  140.             if (ours(whichWindow))
  141.                 MyGrowWindow(whichWindow, myEvent->where);
  142.             break;
  143.  
  144.         case inContent:
  145.             if (whichWindow != FrontWindow())
  146.                 SelectWindow(whichWindow);
  147.             else if (ours(whichWindow))
  148.                 {
  149.                 /* DoContent(whichWindow, myEvent); */
  150.                 }
  151.             break;
  152.     }
  153. }
  154.  
  155. static int MainEvent(void) 
  156. {
  157.     EventRecord        myEvent;
  158.     WindowPtr        whichWindow;
  159.     short            windowPart;
  160.     Rect            r;
  161.     
  162.     SystemTask();
  163.     if (GetNextEvent(everyEvent, &myEvent)) {
  164.         switch (myEvent.what) {
  165.         case mouseDown:
  166.             windowPart = FindWindow(myEvent.where, &whichWindow);
  167.             return DoMouseDown(windowPart, whichWindow, &myEvent);
  168.             break;
  169.  
  170.         case activateEvt:
  171.             if (ours((WindowPtr)myEvent.message)) {
  172.                 if (myEvent.modifiers & activeFlag) {
  173.                 }
  174.                 else {
  175.                 }
  176.             }
  177.             break;
  178.  
  179.         case updateEvt: 
  180.             if (ours((WindowPtr) myEvent.message))
  181.                     UpdateWindow();
  182.             break;
  183.         } /* end of case myEvent.what */
  184.     } /* if */
  185.     return(1);
  186. }
  187.  
  188. main(void)
  189. {
  190.     double ref_lat = 0.0;
  191.     double ref_long = 0.0;
  192.     double plot_size = 90.0;
  193.  
  194.     Rect rBounds = {50,50,450,550};
  195.     
  196.     struct {
  197.         short col;
  198.         long a,b,c,d;
  199.         } *rsrc;
  200.     int i;
  201.     trigtab *tr;
  202.     Handle themap = GetResource('TRIG', 128);
  203.     int siz = MaxSizeRsrc(themap);
  204.     xmax = rBounds.right-rBounds.left-4;
  205.     ymax = rBounds.bottom-rBounds.top-4;
  206.     
  207.     trig = (trigtab *)NewPtr(siz);
  208.     HLock(themap);
  209.     rsrc = (void *)*themap;
  210.     tr = trig;
  211.     do
  212.         {
  213.         tr->color = rsrc->col;
  214.         tr->cosnewlng = rsrc->a/1000000.0;
  215.         tr->sinnewlng = rsrc->b/1000000.0;
  216.         tr->cosnewlat = rsrc->c/1000000.0;
  217.         tr->sinnewlat = rsrc->d/1000000.0;
  218.         rsrc++;
  219.         tr++;
  220.         }
  221.     while (tr-trig < siz/sizeof(trigtab));
  222.  
  223.     InitGraf(&qd.thePort);
  224.     InitFonts();
  225.     FlushEvents(everyEvent, 0);
  226.     InitWindows();
  227.     InitMenus();
  228.     TEInit();
  229.     InitDialogs(0L);
  230.     InitCursor();
  231.     MaxApplZone();
  232.  
  233.     SetUpMenus();
  234.  
  235.     myWindow = NewWindow(0,&rBounds,"\pMap",1,noGrowDocProc,(WindowPtr)-1,0,0);
  236.     
  237.     if(!CreateOffscreenBitMap(&offscreen, &myWindow->portRect))
  238.         {
  239.         SysBeep(1);
  240.         ExitToShell();
  241.         }
  242.     
  243.     while (MainEvent())
  244.         {
  245.         GrafPtr    savePort;
  246.     
  247.         GetPort(&savePort);
  248.         SetPort(offscreen);
  249.         EraseRect(&(offscreen->portRect));
  250.         plot(ref_lat,ref_long,plot_size);
  251.         SetPort(myWindow);
  252.         InvalRect(&(myWindow->portRect));
  253.         SetPort(savePort);
  254.         UpdateWindow();
  255.         ref_long += 5;
  256.         }
  257.     DestroyOffscreenBitMap(offscreen);    /* dump the off-screen bitmap */
  258.     }
  259.