home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / RadarScope 1.0.2 / RadarScope.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  7.4 KB  |  389 lines  |  [TEXT/CWIE]

  1. // RadarScope
  2. // version 1.0.2
  3. // ported to CodeWarrior by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. #include <math.h>
  7.  
  8. #define over qd.screenBits.bounds.right
  9. #define down qd.screenBits.bounds.bottom
  10.  
  11. #define drawWindow    2
  12. #define OK            1
  13. #define Cancel        2
  14.  
  15. typedef struct Info
  16. {
  17.     RgnHandle clip;
  18.     Rect rect;
  19.     short h;
  20.     short v;
  21. } Info;
  22.  
  23. Info stuff[20];
  24.  
  25. typedef struct
  26. {
  27.     WindowRecord    windRec;
  28.     short             windtype;
  29.     Boolean         drawnIn;
  30. } MyWindRecord, *MyWindPeek;
  31.  
  32. MenuHandle appleMenu, fileMenu, editMenu, widthMenu;
  33.  
  34. enum {
  35.     appleID = 1,
  36.     fileID,
  37.     editID,
  38.     widthID
  39. };
  40. enum {
  41.     openItem = 1,
  42.     closeItem,
  43.     quitItem = 4
  44. };
  45. Point centre, circle[360], deltaPt, lastPt, whereLast;
  46.  
  47. short     bottom, lasti, radius, width, height, right, left, bottom, top;
  48.  
  49. Rect globWind, ourRect;
  50. EventRecord theEvent;                
  51. RgnHandle tempRgnHandle;
  52. Str255 strangs[20];
  53. WindowPtr shell_window;
  54. short        windowType;             
  55.  
  56. void InitRadar (Rect *drawArea, short ID);
  57. void DrawRadar (short ID);
  58. void Action (void);
  59. void SetUp (short ID);
  60. void TakeDown (short ID);
  61. void StowInfo (Rect *tempRect, short ID);
  62. void SetUpWindow (void);
  63. Boolean DetermineDrawnInFlag (WindowPtr theWindow);
  64. void UpdateDrawWindow (WindowPtr theWindow);
  65. void DoSomeUpdateStuff (void);
  66. void HandleUpdate (void);
  67. short DetermineWindowType (WindowPtr theWindow);
  68. void InitManagers (short numMasters);
  69. short MainEvent (void);
  70. void DoAbout (void);
  71. void SetUpMenus (void);
  72. void DoKeyDownStuff (void);
  73. short HandleMenu (long mSelect);
  74. void main (void);
  75.  
  76. void InitRadar (Rect *drawArea, short ID)
  77. {
  78.     short i;
  79.     double sinner, cosinner, thetaStep, theta;
  80.     
  81.     //• Our rectangle is at the drawArea, which is tempRect, which
  82.     //• is the one 4 pixels in, in the window.
  83.     ourRect = *drawArea;
  84.     
  85.     //• So, width is 192 pixels, and so is height.
  86.     width = ourRect.right;
  87.     height = ourRect.bottom;
  88.     
  89.     //• This makes radius half that, or 96
  90.     radius = width / 2;
  91.     centre.h = radius;
  92.     centre.v = radius;
  93.     thetaStep = (2 * 3.14159) / 360;
  94.     theta = 0;
  95.     for (i = 0; i < 360; i++)
  96.     {
  97.         circle[i].h = radius * cos (theta) + radius;
  98.         circle[i].v = radius * sin (theta) + radius;
  99.         theta += thetaStep;
  100.     }
  101.     lasti = 0;
  102. }
  103.  
  104. void DrawRadar (short ID)
  105. {
  106.     FillOval (&ourRect, &qd.black);
  107.     MoveTo (centre.h, centre.v);
  108.     PenMode (srcXor);
  109.     LineTo (circle[lasti].h, circle[lasti].v);
  110.     lasti = (++lasti % 360);
  111.     PenPat (&qd.black);
  112. }
  113.  
  114. void Action (void)
  115. {
  116.     SetUp (1);
  117.     DrawRadar (1);
  118.     TakeDown (1);
  119. }
  120.  
  121. void SetUp (short ID)
  122. {
  123.     SetClip (stuff[ID].clip);
  124.     SetOrigin (stuff[ID].h, stuff[ID].v);
  125. }
  126.  
  127. void TakeDown (short ID)
  128. {
  129.     SetOrigin (0, 0);
  130. }
  131.  
  132. void StowInfo (Rect *tempRect, short ID)
  133. {
  134.     RgnHandle tempRgn;
  135.     
  136.     stuff[ID].rect = *tempRect;
  137.     stuff[ID].h = - tempRect->left;
  138.     stuff[ID].v = - tempRect->top;
  139.     
  140.     InsetRect (&stuff[ID].rect, -1, -1);
  141.     tempRgn = NewRgn ();
  142.     OffsetRect (tempRect, stuff[ID].h, stuff[ID].v);
  143.     RectRgn (tempRgn, tempRect);
  144.     stuff[ID].clip = tempRgn;
  145. }
  146.  
  147. void SetUpWindow (void)
  148. {
  149.     Rect tempRect, frameRect, ovalRect;
  150.     RgnHandle tempRgn;
  151.     
  152.     //• Since QuickDraw has the port set globally (default),
  153.     //• we get the screen size off right and bottom, and
  154.     //• backtrack off it. The below results in a 200x200
  155.     //• window centered on the screen.
  156.     SetRect (&globWind, over / 2 - 100,
  157.                         down / 2 - 100,
  158.                         over / 2 + 100,
  159.                         down / 2 + 100);
  160.                         
  161.     shell_window = NewWindow (0L, &globWind, "\pKen's Shell", true, 
  162.                             plainDBox, (WindowPtr) -1L, true, 0);
  163.  
  164.     //• Now we've said the WindowPtr points to a window in the
  165.     //• rectangle we set off the screen size. So we localize our
  166.     //• coordinates to that window by saying IT is the port that
  167.     //• QuickDraw will draw in.
  168.     SetPort (shell_window);
  169.     
  170.     //• Now we set a rectangle inside that local port. Since it's
  171.     //• a 200 pixel square window, it's 2 pixels in on all sides.
  172.     SetRect (&frameRect, 2, 2, 198, 198);
  173.     
  174.     //• Now we frame it.
  175.     FrameRect (&frameRect);
  176.  
  177.     //• Now we set another one 4 pixels in, to show our stuff in.
  178.     SetRect (&tempRect, 4, 4, 196, 196);
  179.     
  180.     StowInfo (&tempRect, 1);
  181.     InitRadar (&tempRect, 1);
  182.     
  183.     stuff[0].clip = NewRgn ();
  184.     GetClip (stuff[0].clip);
  185. }
  186.  
  187. void DoSomeUpdateStuff (void)
  188. {
  189.     short i = 0;
  190.     SetClip (stuff[0].clip);
  191.     FrameRect (&stuff[i].rect);
  192. }
  193.  
  194. void InitManagers (short numMasters)
  195. {
  196.     short i;
  197.     InitGraf (&qd.thePort);
  198.     InitFonts ();
  199.     InitWindows ();
  200.     InitMenus ();
  201.     TEInit ();
  202.     InitDialogs (((void *) 0));
  203.     for (i = 0; i < 5; i++)
  204.     {
  205.         MoreMasters ();
  206.     }
  207.     FlushEvents (everyEvent, 0);
  208.     InitCursor ();
  209. }
  210.  
  211. short MainEvent ()
  212. {
  213.     WindowPtr whichWindow;
  214.     Rect r;
  215.     SystemTask ();
  216.     if (GetNextEvent (everyEvent, &theEvent))
  217.     {
  218.         switch (theEvent.what)
  219.         {
  220.             case mouseDown:
  221.             switch (FindWindow (theEvent.where, &whichWindow))
  222.             {
  223.                 case inDesk:
  224.                 break;
  225.                 
  226.                 case inMenuBar:
  227.                     return (HandleMenu (MenuSelect (theEvent.where)));
  228.                 break;
  229.                 
  230.                 case inGoAway:
  231.                 break;
  232.                 
  233.                 case inSysWindow:
  234.                 break;
  235.                 
  236.                 case inContent:
  237.                 break;
  238.                 
  239.                 case inDrag:
  240.                     SetRect (&r, 0, 0, 5000, 5000);
  241.                     DragWindow (whichWindow, theEvent.where, &r);
  242.                 break;
  243.             }
  244.             break;
  245.             
  246.             case keyDown:
  247.             case autoKey:
  248.                 DoKeyDownStuff ();
  249.             break;
  250.             
  251.             case activateEvt:
  252.             break;
  253.             
  254.             case updateEvt:
  255.             BeginUpdate (shell_window);
  256. //            HandleUpdate ();                     
  257.             DoSomeUpdateStuff ();
  258.             EndUpdate (shell_window);
  259.             break;
  260.             
  261.             default:;
  262.         }
  263.     }
  264.     Action ();
  265.     HiliteMenu (0);
  266.     return (1);
  267. }
  268.  
  269. void DoAbout ()
  270. {
  271.     Boolean done;
  272.     short theItem;
  273.     DialogPtr AboutPtr;
  274.     short itemType;
  275.     Handle item;
  276.     Rect box;
  277.     done = 0;
  278.     AboutPtr = GetNewDialog (128,0L, (WindowPtr)-1);
  279.     while (!done)
  280.     {
  281.         ModalDialog (0L,&theItem);
  282.         switch (theItem)
  283.         {
  284.             case OK:
  285.             done = 1;
  286.             break;
  287.             
  288.             case Cancel:
  289.             done = 1;
  290.             break;
  291.         }
  292.     }
  293.     DisposDialog (AboutPtr);
  294. }
  295.  
  296. void SetUpMenus (void)
  297. {
  298.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  299.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  300.     InsertMenu (editMenu = NewMenu (editID, "\pEdit"), 0);
  301.     InsertMenu (widthMenu = NewMenu (widthID, "\pBlips"), 0);
  302.     DrawMenuBar ();
  303.     AddResMenu (appleMenu, 'DRVR');
  304.     AppendMenu (fileMenu, "\pOpen/O;Close/W; (-;Quit/Q");
  305.     AppendMenu (editMenu, "\pUndo/Z; (-;Cut/X;Copy/C;Paste/V;Clear");
  306.     AppendMenu (widthMenu, "\p0/0;1/1;2/2;3/3;4/4;5/5;6/6;7/7;8/8;9/9");
  307. }
  308.  
  309. void DoKeyDownStuff ()
  310. {
  311.     short chr; 
  312.     long  menuChoice;
  313.   
  314.     chr = theEvent.message & charCodeMask;
  315.   
  316.     if ((theEvent.modifiers & cmdKey) != 0)
  317.     {
  318.         if (theEvent.what != autoKey) 
  319.         {
  320.             menuChoice = MenuKey (chr);
  321.             HandleMenu (menuChoice);
  322.         }
  323.     }
  324. }
  325.  
  326. short HandleMenu (long mSelect)
  327. {
  328.     short menuID = HiWord (mSelect);
  329.     short menuItem = LoWord (mSelect);
  330.     Str255 name;
  331.     GrafPtr savePort;
  332.     WindowPeek frontWindow;
  333.     switch (menuID)
  334.     {
  335.         case appleID:
  336.             GetPort (&savePort);
  337.             GetItem (appleMenu, menuItem, name);
  338.             OpenDeskAcc (name);
  339.             SetPort (savePort);
  340.         break;
  341.         
  342.         case fileID:
  343.             switch (menuItem)
  344.             {
  345.                 case openItem:
  346.                     ShowWindow (shell_window);
  347.                     SelectWindow (shell_window);
  348.                 break;
  349.                 
  350.                 case closeItem:
  351.                     if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
  352.                         break;
  353.                 
  354.                     if (frontWindow->windowKind < 0)
  355.                         CloseDeskAcc (frontWindow->windowKind);
  356.                     else 
  357.                         if (frontWindow = (WindowPeek) shell_window)
  358.                             HideWindow (shell_window);
  359.                 break;
  360.                 
  361.                 case quitItem:
  362.                     ExitToShell ();
  363.                 break;
  364.             }
  365.         break;
  366.         
  367.         case editID:
  368.             if (!SystemEdit (menuItem-1))
  369.                 SysBeep (5);
  370.         break;
  371.         
  372.         case widthID:
  373.             CheckItem (widthMenu, width, false);
  374.             width = menuItem;
  375.             InvalRect (&shell_window->portRect);
  376.         break;
  377.     }
  378. }
  379.  
  380. void main ()
  381. {
  382.     InitManagers (5);
  383.     SetUpMenus ();
  384.     SetUpWindow ();
  385.     for (;;)
  386.         MainEvent ();
  387. }
  388.  
  389.