home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Demos / Tools / QC™ 1.1.3 / QCAPI / BadAPPL / BadAPPL src / BadAPPL.c next >
Encoding:
C/C++ Source or Header  |  1995-04-21  |  9.3 KB  |  426 lines  |  [TEXT/R*ch]

  1. /*________________________________________________________________________________
  2.  
  3.     BadAPPL.c
  4.  
  5.     Copyright © 1993-1995 Onyx Technology - All rights reserved
  6.  
  7. ________________________________________________________________________________*/
  8.  
  9. #define    _DEFINE_GLOBS_            // so our apps globals are declared (see BadGlobs.h)
  10. #ifndef    _H_BadAPPL
  11. #include "BadAPPL.h"
  12. #endif
  13. #ifndef    _H_BadWindows
  14. #include "BadWindows.h"
  15. #endif
  16. #ifndef    _H_BadUtils
  17. #include "BadUtils.h"
  18. #endif
  19. #ifndef    _H_BadGestalt
  20. #include "BadGestalt.h"
  21. #endif
  22. #ifndef    _H_BadErrors
  23. #include "BadErrors.h"
  24. #endif
  25. #ifndef    _H_BadGlobs
  26. #include "BadGlobs.h"
  27. #endif
  28. #ifndef    _H_BadMacros
  29. #include "BadMacros.h"
  30. #endif
  31. #ifndef    _H_QCAPI
  32. #include "QCAPI.h"
  33. #endif
  34.  
  35. // static functions
  36. static void        InitGlobals(void);
  37. static short    MainEvent(void);
  38. static short    DoCommand(long mResult);
  39. static void        DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent);
  40. static void        TogglePower(void);
  41.  
  42. /*________________________________________________________________________________
  43.  
  44.     main()
  45.     
  46.     info:    this is main for this bad little app.
  47. ________________________________________________________________________________*/
  48.  
  49. void main(void) 
  50. {
  51.     QCCallBackUPP    handlerProc;
  52.     QCErr            result;
  53.  
  54.     InitGraf((Ptr) &qd.thePort);            // works for MPW and Think
  55.  
  56.     InitFonts();
  57.     FlushEvents(everyEvent, 0);
  58.     InitWindows();
  59.     InitMenus();
  60.     TEInit();
  61.     InitDialogs(0L);
  62.     InitCursor();
  63.     MaxApplZone();
  64.  
  65.     gAppHeap =  GetZone();                    // save the zone we are running in right now
  66.     result = QCInstalled();                    // is QC extension installed?
  67.     if (result)
  68.         {
  69.         if (result == kQCAPIMismatch)
  70.             BadAlert("\pOoops! There is a mismatch between the QCAPI library in use,",
  71.                     "\pand the version of QC™ extension code in use.", 0L);
  72.         gQCInstalled = false;
  73.         }
  74.     else
  75.         gQCInstalled = true;
  76.  
  77.     InitGlobals();
  78.     SetUpMenus();
  79.  
  80.     if (!gQCInstalled)
  81.         {
  82.         BadAlert("\pSorry, the QC INIT is not installed. Some testing functions will be disabled.",
  83.                     "\pYou can cause Errors, but QC isn’t available to detect them.", 0L);
  84.         }
  85.  
  86.     SetUpWindows();
  87.  
  88.     if (gTEH)
  89.         {
  90.         #ifdef    powerc
  91.             OutputString( kStringsID, kRunningPPC, 0L );
  92.         #else
  93.             OutputString( kStringsID, kRunning68K, 0L );
  94.         #endif
  95.  
  96.         if (!gQCInstalled)
  97.             OutputString( kStringsID, kInitNotLoaded, 0L );
  98.         else
  99.             OutputString( kStringsID, kInitLoaded, 0L );
  100.         }
  101.  
  102.     handlerProc = InstallHandler();        // install our QC error handler
  103.     while (MainEvent());                // start a eternal loop until the user quits
  104.     RemoveHandler(handlerProc);            // remove out QC error handler
  105. }
  106.  
  107. /*________________________________________________________________________________
  108.  
  109.     DoBeforeQuit()
  110.  
  111.     info:     Do any house cleanup needed before we ExitToShell.
  112.  
  113. ________________________________________________________________________________*/
  114. void    DoBeforeQuit(void)
  115. {
  116.     if (QCIsActive())                            // make sure we turn QC off if it is on
  117.         QCDeActivate();
  118.  
  119.     if (gTestListH)
  120.         DisposeHandle((Handle)gTestListH);
  121.  
  122.     ExitToShell();                                // time to say goodnight, Gracie…
  123. }
  124.  
  125. #pragma mark -
  126.  
  127. /*________________________________________________________________________________
  128.  
  129.     MainEvent()
  130.     
  131.     info:    Handle a mouse down event
  132.  
  133. ________________________________________________________________________________*/
  134. static short MainEvent(void)
  135. {
  136.     EventRecord        myEvent;
  137.     WindowPtr        whichWindow;
  138.     short            windowPart;
  139.  
  140.     MaintainCursor();
  141.     SystemTask();
  142.  
  143.     if (GetNextEvent(everyEvent, &myEvent))
  144.         {
  145.         switch (myEvent.what)
  146.             {
  147.             case mouseDown:
  148.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  149.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  150.                 break;
  151.     
  152.             case keyDown:
  153.             case autoKey: 
  154.                 {
  155.                 register char    theChar;
  156.                 
  157.                 theChar = myEvent.message & charCodeMask;
  158.                 if ((myEvent.modifiers & cmdKey) != 0) 
  159.                     return(DoCommand(MenuKey( theChar)));
  160.                     }
  161.                 break;
  162.     
  163.             case activateEvt:
  164.                 if (ourWindow((WindowPtr)myEvent.message))
  165.                     {
  166.                     if (myEvent.modifiers & activeFlag)
  167.                         {
  168.                         TEActivate(gTEH);
  169.                         ShowControl(gVScroll);
  170.                         DisableItem(gMenus[editM], undoCommand);
  171.                         }
  172.                     else
  173.                         {
  174.                         TEDeactivate(gTEH);
  175.                         HideControl(gVScroll);
  176.                         }
  177.                     }
  178.                 break;
  179.     
  180.             case updateEvt: 
  181.                 if (ourWindow((WindowPtr) myEvent.message))
  182.                         UpdateWindow(gStatusWindow);
  183.                 break;
  184.             } // end of case myEvent.what
  185.         }
  186.  
  187.     return(1);
  188. }
  189.  
  190. /*________________________________________________________________________________
  191.  
  192.     DoCommand()
  193.  
  194.     info:     Do a menu command
  195.     
  196.     entry:    long result from MenuSelect
  197.  
  198.     return:    at present, always returns true
  199. ________________________________________________________________________________*/
  200. static short DoCommand(long mResult)
  201. {
  202.     short        theItem, err;
  203.     Str255        name;
  204.     
  205.     theItem = LoWord(mResult);
  206.     switch (HiWord(mResult))
  207.         {
  208.         case kAppleID:
  209.             switch(theItem)
  210.                 {
  211.                 case    aboutCommand:
  212.                     DoDialog(aboutDLOG);
  213.                     break;
  214.                 default:
  215.                     GetMenuItemText(gMenus[appleM], theItem, name);
  216.                     OpenDeskAcc(name);
  217.                     SetPort(gStatusWindow);
  218.                     break;
  219.                 }
  220.             break;
  221.  
  222.         case kFileID:
  223.             switch(theItem)
  224.                 {
  225.                 case    onCommand:
  226.                     TogglePower();
  227.                     break;
  228.                 case    quitCommand:
  229.                     DoBeforeQuit(); 
  230.                     break;
  231.                 }
  232.             break;
  233.  
  234.         case kEditID: 
  235.             if (SystemEdit(theItem-1) == 0)
  236.                 ShowSelect();
  237.             break;
  238.  
  239.         case kControlID:
  240.             MaintainControlMenu(theItem, kDoState);
  241.             break;
  242.  
  243.         case kErrorsID:
  244.             switch(theItem)
  245.                 {
  246.                 case    bkOverwriteID:
  247.                     DoBlockOverwrite();
  248.                 }
  249.             break;
  250.  
  251.         case kStateID:
  252.             switch(theItem)
  253.                 {
  254.                 case    saveCommand:
  255.                     gQCSavedStateH = QCGetState();
  256.                     if (gQCSavedStateH)
  257.                         OutputString( kStringsID, kStateSaved, 0L);
  258.                     else
  259.                         OutputString( kErrStringsID, kStateRestored, 0L);
  260.                     break;
  261.                 case    restoreCommand:
  262.                     err = QCSetState(gQCSavedStateH);
  263.                     if (!err)
  264.                         OutputString( kStringsID, kStateRestored, 0L);
  265.                     else
  266.                         OutputString( kErrStringsID, kStateRestored, (long) err);
  267.  
  268.                     QCDisposeState(gQCSavedStateH);
  269.                     gQCSavedStateH = 0L;                // make sure this is cleared out
  270.  
  271.                     break;
  272.                 }
  273.             break;
  274.  
  275.         case handleID:
  276.             switch(theItem)
  277.                 {
  278.                 case    createDisposHndCommand:
  279.                     DoHandlePtrExistance(true);            // true indicates test Handles
  280.                     break;
  281.                 case    resizeHndCommand:
  282.                     DoHandlePtrResize(true);            // true indicates test Handles
  283.                     break;
  284.                 case    verifyHndCommand:
  285.                     DoHandlePtrVerify(true);
  286.                     break;
  287.                 }
  288.             break;
  289.  
  290.         case ptrID:
  291.             switch(theItem)
  292.                 {
  293.                 case    createDisposPtrCommand:
  294.                     DoHandlePtrExistance(false);        // false indicates test Pointers
  295.                     break;
  296.                 case    resizePtrCommand:
  297.                     DoHandlePtrResize(false);            // false indicates test Pointers
  298.                     break;
  299.                 case    verifyPtrCommand:
  300.                     DoHandlePtrVerify(false);
  301.                     break;
  302.                 }
  303.             break;
  304.  
  305.         case addrZeroID:
  306.             switch(theItem)
  307.                 {
  308.                 case    write2ZeroCommand:
  309.                     WriteToZeroTest();
  310.                     break;
  311.                 case    derefZeroCommand:
  312.                     DerefZeroTest();
  313.                     break;
  314.                 }
  315.             break;
  316.  
  317.         case purgeID:
  318.             // at this time, (09/10/91) there is only one purge menu item.  Sometime in the
  319.             // future there may be more so…keep an eye out for a switch later on…
  320.             DoPurgeTest();
  321.             break;
  322.         }
  323.  
  324.     HiliteMenu(0);
  325.     return(1);
  326. }
  327.  
  328. /*________________________________________________________________________________
  329.  
  330.     DoMouseDown()
  331.     
  332.     info:    Handle a mouse down event
  333.  
  334. ________________________________________________________________________________*/
  335. static void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  336. {
  337.     Rect    dragRect = { 0, 0, 1024, 1024 };
  338.  
  339.     switch (windowPart)
  340.         {
  341.         case inGoAway:
  342.             if (ourWindow(whichWindow))
  343.                 if (TrackGoAway(gStatusWindow, myEvent->where))
  344.                     CloseMyWindow();
  345.             break;
  346.  
  347.         case inMenuBar:
  348.             MaintainMenus();
  349.             DoCommand(MenuSelect(myEvent->where));
  350.  
  351.         case inSysWindow:
  352.             SystemClick(myEvent, whichWindow);
  353.             break;
  354.  
  355.         case inDrag:
  356.             if (ourWindow(whichWindow))
  357.                 DragWindow(whichWindow, myEvent->where, &dragRect);
  358.             break;
  359.  
  360.         case inGrow:
  361.             if (ourWindow(whichWindow))
  362.                 MyGrowWindow(whichWindow, myEvent->where);
  363.             break;
  364.  
  365.         case inContent:
  366.             if (whichWindow != FrontWindow())
  367.                 SelectWindow(whichWindow);
  368.             else if (ourWindow(whichWindow))
  369.                 DoContent(whichWindow, myEvent);
  370.             break;
  371.         }
  372. }
  373.  
  374. /*________________________________________________________________________________
  375.  
  376.     InitGlobals()
  377.  
  378.     info:     Initialize any necessary globals.
  379.  
  380. ________________________________________________________________________________*/
  381. static void InitGlobals(void)
  382. {
  383.     if (GetSystemVers() >= System7)
  384.         gAtleastSys7    =    true;
  385.     else
  386.         gAtleastSys7    =    false;
  387.  
  388.     gQCSavedStateH = 0L;
  389. }
  390.  
  391. /*________________________________________________________________________________
  392.  
  393.     TogglePower()
  394.  
  395.     info:     Toggle the activating and deactivating of QC.
  396. ________________________________________________________________________________*/
  397. static void    TogglePower(void)
  398. {
  399.     short    err;
  400.  
  401.     if (gQCInstalled)                            // is the INIT available?
  402.         {
  403.         if (QCIsActive())                        // are we currently on?
  404.             {
  405.             err = QCDeActivate();                // then turn us off…
  406.             if (!err)
  407.                 OutputString(kStringsID, kOffIndx, 0L);
  408.             else
  409.                 OutputString(kErrStringsID, kQCDeActivateIndx, (long) err);
  410.             }
  411.         else
  412.             {
  413.             // Note that we are specifically giving QCActivate() a heap to activate on.
  414.             //    If we passed 0L, QCActivate() would activate on the current heap zone.
  415.  
  416.             err = QCActivate(gAppHeap);            // check this specific zone for errors
  417.             if (!err)
  418.                 OutputString(kStringsID, kOnIndx, 0L);
  419.             else
  420.                 OutputString(kErrStringsID, kQCActivateIndx, (long) err);
  421.             }
  422.  
  423.         ShowSelect();                            // update our status text edit window
  424.         }
  425. }
  426.