home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc archive / bull.c next >
Encoding:
C/C++ Source or Header  |  1994-02-05  |  6.8 KB  |  382 lines  |  [TEXT/JRRK]

  1. #include <Quickdraw.h>
  2. #include <Menus.h>
  3. #include <Windows.h>
  4. #include <ToolUtils.h>
  5. #include <Desk.h>
  6. #include <SegLoad.h>
  7. #include <OSUtils.h>
  8. #include <Memory.h>
  9. #include <Fonts.h>
  10. #include <Events.h>
  11. #include <OSEvents.h>
  12. #include <TextEdit.h>
  13. #include <Dialogs.h>
  14.  
  15. void AdjustMenus(void); 
  16. void HandleMenu (long mSelect);
  17. void SetUpMenus(void);
  18. void InitMacintosh(void);
  19. void HandleMouseDown (EventRecord *theEvent);
  20. void HandleEvent(void);
  21. void DrawBullseye (short active);
  22. void SetUpWindow(void);
  23.  
  24. void
  25. main( void)
  26.  
  27. {
  28.     InitMacintosh();
  29.     SetUpMenus();
  30.     SetUpWindow();
  31.     
  32.     for (;;)
  33.         HandleEvent();
  34. }
  35. /* end main */
  36.  
  37. extern    WindowPtr bullseyeWindow;
  38. extern    int    width;
  39.  
  40.  
  41. MenuHandle    appleMenu, fileMenu, editMenu, widthMenu;
  42.  
  43. enum    {
  44.     appleID = 1,
  45.     fileID,
  46.     editID,
  47.     widthID
  48.     };
  49.  
  50. enum    {
  51.     openItem = 1,
  52.     closeItem,
  53.     quitItem = 4
  54.     };
  55.  
  56.  
  57. /****
  58.  * SetUpMenus()
  59.  *
  60.  *    Set up the menus. Normally, we’d use a resource file, but
  61.  *    for this example we’ll supply “hardwired” strings.
  62.  *
  63.  ****/
  64.  
  65. void SetUpMenus(void)
  66.  
  67. {
  68.     InsertMenu(appleMenu = NewMenu(appleID, "\p\024"), 0);
  69.     InsertMenu(fileMenu = NewMenu(fileID, "\pFile"), 0);
  70.     InsertMenu(editMenu = NewMenu(editID, "\pEdit"), 0);
  71.     InsertMenu(widthMenu = NewMenu(widthID, "\pWidth"), 0);
  72.     DrawMenuBar();
  73.     AddResMenu(appleMenu, 'DRVR');
  74.     AppendMenu(fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
  75.     AppendMenu(editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  76.     AppendMenu(widthMenu, "\p1/1;2/2;3/3;4/4;5/5;6/6;7/7;8/8;9/9;10/0;11;12");
  77. }
  78. /* end SetUpMenus */
  79.  
  80.  
  81. /****
  82.  *  AdjustMenus()
  83.  *
  84.  *    Enable or disable the items in the Edit menu if a DA window
  85.  *    comes up or goes away. Our application doesn't do anything with
  86.  *    the Edit menu.
  87.  *
  88.  ****/
  89.  
  90. static void enable (MenuHandle menu, short item, short ok);
  91.  
  92. void AdjustMenus(void)
  93. {
  94.     register WindowPeek wp = (WindowPeek) FrontWindow();
  95.     short kind = wp ? wp->windowKind : 0;
  96.     Boolean DA = kind < 0;
  97.     
  98.     enable(editMenu, 1, DA);
  99.     enable(editMenu, 3, DA);
  100.     enable(editMenu, 4, DA);
  101.     enable(editMenu, 5, DA);
  102.     enable(editMenu, 6, DA);
  103.     
  104.     enable(fileMenu, openItem, !((WindowPeek) bullseyeWindow)->visible);
  105.     enable(fileMenu, closeItem, DA || ((WindowPeek) bullseyeWindow)->visible);
  106.  
  107.     CheckItem(widthMenu, width, true);
  108. }
  109.  
  110.  
  111. static
  112. void enable(MenuHandle menu, short item, short ok)
  113. {
  114.     if (ok)
  115.         EnableItem(menu, item);
  116.     else
  117.         DisableItem(menu, item);
  118. }
  119.  
  120.  
  121. /*****
  122.  * HandleMenu(mSelect)
  123.  *
  124.  *    Handle the menu selection. mSelect is what MenuSelect() and
  125.  *    MenuKey() return: the high word is the menu ID, the low word
  126.  *    is the menu item
  127.  *
  128.  *****/
  129.  
  130. void HandleMenu (long mSelect)
  131.  
  132. {
  133.     int            menuID = HiWord(mSelect);
  134.     int            menuItem = LoWord(mSelect);
  135.     Str255        name;
  136.     GrafPtr        savePort;
  137.     WindowPeek    frontWindow;
  138.     
  139.     switch (menuID)
  140.       {
  141.       case    appleID:
  142.         GetPort(&savePort);
  143.         GetItem(appleMenu, menuItem, name);
  144.         OpenDeskAcc(name);
  145.         SetPort(savePort);
  146.         break;
  147.     
  148.       case    fileID:
  149.         switch (menuItem)
  150.           {
  151.           case    openItem:
  152.             ShowWindow(bullseyeWindow);
  153.             SelectWindow(bullseyeWindow);
  154.             break;
  155.                                 
  156.           case    closeItem:
  157.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  158.               break;
  159.               
  160.             if (frontWindow->windowKind < 0)
  161.               CloseDeskAcc(frontWindow->windowKind);
  162.             else if ((frontWindow = (WindowPeek) bullseyeWindow) != NULL)
  163.               HideWindow(bullseyeWindow);
  164.                         break;
  165.                         
  166.           case    quitItem:
  167.             ExitToShell();
  168.             break;
  169.           }
  170.         break;
  171.                   
  172.       case    editID:
  173.         if (!SystemEdit(menuItem-1))
  174.           SysBeep(5);
  175.         break;
  176.         
  177.       case    widthID:
  178.         CheckItem(widthMenu, width, false);
  179.         width = menuItem;
  180.         InvalRect(&bullseyeWindow->portRect);
  181.         break;
  182.       }
  183. }
  184. /* end HandleMenu */
  185.  
  186. /*****
  187.  * bullWindow.c
  188.  *
  189.  *        The window routines for the Bullseye demo
  190.  *
  191.  *****/
  192.  
  193. /*****
  194.  * bullWindow.h
  195.  *
  196.  *        Public interfaces for bullWindow.c
  197.  *
  198.  *****/
  199.  
  200. WindowPtr    bullseyeWindow;
  201. Rect        dragRect;
  202. Rect        windowBounds = { 40, 40, 150, 150 };
  203. Rect        circleStart = {10, 10, 100, 100};
  204. int            width = 10;
  205.  
  206. /****
  207.  * SetUpWindow()
  208.  *
  209.  *    Create the Bullseye window, and open it.
  210.  *
  211.  ****/
  212.  
  213. void SetUpWindow(void)
  214.  
  215. {
  216.     dragRect = qd.screenBits.bounds;
  217.     
  218.     bullseyeWindow = NewWindow(0L, &windowBounds, "\pBullseye", true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  219.     SetPort(bullseyeWindow);
  220. }
  221. /* end SetUpWindow */
  222.  
  223.  
  224. /*****
  225.  * DrawBullseye()
  226.  *
  227.  *    Draws the bullseye.
  228.  *
  229.  *****/
  230.  
  231. void DrawBullseye(short active)
  232.  
  233. {
  234.     Rect    myRect;
  235.     int        color = true;
  236.     
  237.     SetPort(bullseyeWindow);
  238.     EraseRect(&circleStart);
  239.     myRect = circleStart;
  240.     
  241.     while(myRect.left < myRect.right)
  242.       {
  243.       ConstPatternParam pat = color ? (active ? &qd.black : &qd.gray) : &qd.white;
  244.       FillOval(&myRect, pat);
  245.       InsetRect(&myRect, width, width);
  246.       color = !color;
  247.       } 
  248. }
  249. /* end DrawBullseye */
  250. /*****
  251.  * bullseye.c
  252.  *
  253.  *    A simple demonstration program to play with the debugger
  254.  *
  255.  *
  256.  *****/
  257.  
  258. extern    WindowPtr    bullseyeWindow;
  259. extern    Rect        dragRect;
  260.  
  261. /****
  262.  * InitMacintosh()
  263.  *
  264.  * Initialize all the managers & memory
  265.  *
  266.  ****/
  267.  
  268. void InitMacintosh(void)
  269.  
  270. {
  271.     MaxApplZone();
  272.     
  273.     InitGraf(&qd.thePort);
  274.     InitFonts();
  275.     FlushEvents(everyEvent, 0);
  276.     InitWindows();
  277.     InitMenus();
  278.     TEInit();
  279.     InitDialogs(0L);
  280.     InitCursor();
  281.  
  282. }
  283. /* end InitMacintosh */
  284.  
  285.  
  286. /****
  287.  * HandleMouseDown (theEvent)
  288.  *
  289.  *    Take care of mouseDown events.
  290.  *
  291.  ****/
  292.  
  293. void HandleMouseDown (EventRecord    *theEvent)
  294.  
  295. {
  296.     WindowPtr    theWindow;
  297.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  298.     
  299.     switch (windowCode)
  300.       {
  301.       case inSysWindow: 
  302.         SystemClick (theEvent, theWindow);
  303.         break;
  304.         
  305.       case inMenuBar:
  306.           AdjustMenus();
  307.         HandleMenu(MenuSelect(theEvent->where));
  308.         break;
  309.         
  310.       case inDrag:
  311.           if (theWindow == bullseyeWindow)
  312.             DragWindow(bullseyeWindow, theEvent->where, &dragRect);
  313.             break;
  314.             
  315.       case inContent:
  316.           if (theWindow == bullseyeWindow)
  317.             {
  318.             if (theWindow != FrontWindow())
  319.               SelectWindow(bullseyeWindow);
  320.             else
  321.               InvalRect(&bullseyeWindow->portRect);
  322.             }
  323.           break;
  324.           
  325.       case inGoAway:
  326.           if (theWindow == bullseyeWindow && 
  327.               TrackGoAway(bullseyeWindow, theEvent->where))
  328.           HideWindow(bullseyeWindow);
  329.             break;
  330.       }
  331. }
  332. /* end HandleMouseDown */
  333.  
  334.  
  335. /****
  336.  * HandleEvent()
  337.  *
  338.  *        The main event dispatcher. This routine should be called
  339.  *        repeatedly (it  handles only one event).
  340.  *
  341.  *****/
  342.  
  343. void HandleEvent(void)
  344.  
  345. {
  346.     int            ok;
  347.     EventRecord    theEvent;
  348.  
  349.     HiliteMenu(0);
  350.     SystemTask ();        /* Handle desk accessories */
  351.     
  352.     ok = GetNextEvent (everyEvent, &theEvent);
  353.     if (ok)
  354.       switch (theEvent.what)
  355.         {
  356.         case mouseDown:
  357.             HandleMouseDown(&theEvent);
  358.             break;
  359.             
  360.         case keyDown: 
  361.         case autoKey:
  362.             if ((theEvent.modifiers & cmdKey) != 0)
  363.               {
  364.               AdjustMenus();
  365.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  366.               }
  367.             break;
  368.             
  369.         case updateEvt:
  370.             BeginUpdate(bullseyeWindow);
  371.             DrawBullseye(((WindowPeek) bullseyeWindow)->hilited);
  372.             EndUpdate(bullseyeWindow);
  373.             break;
  374.             
  375.         case activateEvt:
  376.             InvalRect(&bullseyeWindow->portRect);
  377.             break;
  378.         }
  379. }
  380. /* end HandleEvent */
  381.  
  382.