home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / appkill.cpt / appkiller.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-15  |  4.8 KB  |  238 lines

  1. /*************
  2.   appkiller.c
  3.   
  4.   This file contains main() which in turn initializes all the different
  5.   parts of the program and Operating System.  The main event loop
  6.   is in main().
  7. *************/
  8.  
  9. #ifndef THINK_C
  10.  #include <AppleEvents.h>
  11.  #include <Desk.h>
  12.  #include <Dialogs.h>
  13.  #include <Events.h>
  14.  #include <Fonts.h>
  15.  #include <GestaltEqu.h>
  16.  #include <Menus.h>
  17.  #include <OSEvents.h>
  18.  #include <QuickDraw.h>
  19.  #include <ToolUtils.h>
  20.  #include <Types.h>
  21.  #include <Windows.h>
  22. #else
  23.  #include "ak_headers"
  24. #endif
  25. #include "aevent.h"
  26. #include "appkiller.h"
  27. #include "ak_ppc.h"
  28. #include "trapavail.h"
  29.  
  30. static void Init(void);
  31. static void Check4Sys7(void);
  32. static void SetupMenus(void);
  33. static void DoEvent(void);
  34. static void DoMouseDown(void);
  35. static void DoKeyDown(void);
  36. static void DoCommand(long);
  37. static void DoHLEvent(EventRecord *);
  38.        void DoAlert(Str255, Str255, Str255, Str255);
  39. static void DoAbout(void);
  40.  
  41. DialogPtr        gAboutDlog;                    /* About box...        */
  42. WindowPtr        gWhichWindow;                /* window of event     */
  43. MenuHandle        gAppleM;                    /* handles to menus    */
  44. EventRecord        gTheEvent;                    /* the current event   */
  45. int                gHaveEvent;                    /* do we have an event */
  46. long            gSleepTime = 60;            /* time to sleep/WNE   */
  47. int                gEventMask = everyEvent;    /* current event mask  */
  48. RgnHandle        gMouseRgn = NULL;            /* current mouse rgn   */
  49.  
  50.  
  51. void main()
  52. {
  53.     Init();
  54.     Check4Sys7();    /* this exits if Sys 7 isn't running */
  55.     InitAEs();        /* initialize my Apple Event handlers */
  56.     SetupMenus();
  57.     MyPPCInit();
  58.  
  59.     PostWithPPCBrowser();
  60.     while (1) {
  61.         gHaveEvent = WaitNextEvent( gEventMask, &gTheEvent, 
  62.             gSleepTime, gMouseRgn);
  63.         if (gHaveEvent)
  64.             DoEvent();
  65.     } /* while */
  66. } /* main() */
  67.  
  68.  
  69. static void Init()
  70. {
  71.     register int i;
  72.     InitGraf(&thePort);
  73.     InitFonts();
  74.     InitWindows();
  75.     InitMenus();
  76.     TEInit();
  77.     InitDialogs(NULL);
  78.     InitCursor();
  79.     for (i=0; i<7; i++) (void)MoreMasters();    
  80.     FlushEvents(everyEvent, 0);
  81.     SetEventMask(everyEvent);
  82. } /* Init() */
  83.  
  84.  
  85. static void Check4Sys7(void)
  86. {
  87. #define _Gestalt 0xa1ad   /* Gestalt trap number (from IM) */
  88.     if (TrapAvailable(_Gestalt)) {
  89.         long result, mask=0x0700; /* mask = system 7.0 */
  90.  
  91.         if ( (Gestalt( gestaltSystemVersion, &result) == noErr) &&
  92.             ( mask == (result & mask)) )
  93.             return;
  94.     }
  95.     /* Let user know we need sys 7, */
  96.     /* if Gestalt isn't on the system, don't even check the others */
  97.     DoAlert("\pThe Application Killer requires",
  98.             "\pSystem 7 in order to work","\pproperly.","\p");
  99.     ExitToShell();
  100.  
  101. } /* Check4Sys7() */
  102.  
  103.  
  104. static void SetupMenus(void)
  105. {
  106.     Handle mbarH;
  107.     
  108.     mbarH = (Handle)GetNewMBar(MENU_BAR);
  109.     if (mbarH == NULL) {
  110.         SysBeep(1); SysBeep(1);
  111.         ExitToShell();
  112.     }
  113.     SetMenuBar( mbarH);
  114.     gAppleM= GetMenu(APPLE_MENU);
  115.     AddResMenu( gAppleM, 'DRVR');
  116.     DrawMenuBar();
  117. } /* SetupMenus() */
  118.  
  119.  
  120. void Cleanup(void)
  121. {
  122.     ExitToShell();
  123. } /* Cleanup() */
  124.  
  125.  
  126. static void DoEvent(void)
  127. {
  128.     switch (gTheEvent.what) {
  129.         case mouseUp:
  130.         case mouseDown:
  131.             DoMouseDown();
  132.             break;
  133.         case keyDown:
  134.         case autoKey:
  135.             DoKeyDown();
  136.             break;
  137.         case activateEvt:
  138.         case updateEvt:
  139.         case osEvt:
  140.         case kHighLevelEvent:
  141.             DoHLEvent(&gTheEvent);
  142.             break;
  143.     }
  144. } /* DoEvent() */
  145.  
  146.  
  147. static void DoMouseDown(void)
  148. {
  149.     switch (FindWindow( gTheEvent.where, &gWhichWindow)) {
  150.         case inSysWindow:
  151.             SystemClick( &gTheEvent, gWhichWindow);
  152.             break;
  153.         case inMenuBar:
  154.             DoCommand( MenuSelect(gTheEvent.where) );
  155.             break;
  156.         case inDrag:
  157.         case inContent:
  158.         case inGoAway:
  159.             break;
  160.     }
  161. } /* DoMouseDown() */
  162.  
  163.  
  164. static void DoKeyDown(void)
  165. {
  166.     char theChar = (char)(gTheEvent.message & charCodeMask);
  167.     if (gTheEvent.modifiers & cmdKey)
  168.         DoCommand( MenuKey(theChar));
  169. } /* DoKeyDown() */
  170.  
  171.  
  172. static void DoCommand(long mResult)
  173. {
  174.     int theItem = LoWord(mResult);
  175.     int theMenu = HiWord(mResult);
  176.     Str255 name;
  177.     int temp;
  178.     
  179.     switch (theMenu) {
  180.         case APPLE_MENU:
  181.             if (theItem == ABOUT_ITEM) {
  182.                 DoAbout();
  183.             } else {
  184.                 GetItem( gAppleM, theItem, name);
  185.                 temp = OpenDeskAcc(name);
  186.             }
  187.             break;
  188.             
  189.         case FILE_MENU:
  190.             switch (theItem) {
  191.                 case POST_ITEM:
  192.                     PostWithPPCBrowser();
  193.                     break;
  194.                 case LAUNCH_ITEM:
  195.                     DoLaunch();
  196.                     break;
  197.                 case QUIT_ITEM:
  198.                     Cleanup();
  199.                     break;
  200.             } /* switch theItem */
  201.             break;
  202.             
  203.     } /* switch theMenu */
  204.     HiliteMenu(0);        /* unhilite the selected menu */
  205. } /* DoCommand() */
  206.  
  207.  
  208. static void DoHLEvent(EventRecord *theEvent)
  209. {
  210.     OSErr err;
  211.  
  212.     if (theEvent->message != (long)    AK_EventClass)
  213.         DoAppleEvent(theEvent);
  214. /*  then perhaps it is one of my events... */
  215.  
  216. } /* DoHLEvent() */
  217.  
  218.  
  219. void DoAlert(Str255 s0,Str255 s1,Str255 s2,Str255 s3)
  220. {
  221.     ParamText(s0,s1,s2,s3);
  222.     Alert(AlertID, NULL);
  223. } /* DoAlert() */
  224.  
  225.  
  226. static void DoAbout(void)
  227. {
  228.     short itemHit = -9;
  229.     if ((gAboutDlog = GetNewDialog(AboutDlogID, NULL, (void*)(-1))) != NULL)
  230.     {
  231.         SelectWindow((WindowPtr)gAboutDlog);
  232.         while (itemHit != AboutOKButt)
  233.             ModalDialog( NULL, &itemHit);
  234.         DisposDialog(gAboutDlog);
  235.         gAboutDlog = NULL;
  236.     } else
  237.         SysBeep(5);
  238. } /* DoAbout() */