home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / AESample.sit / AESample / sources / sample.main.c < prev    next >
C/C++ Source or Header  |  1996-06-22  |  6KB  |  239 lines

  1. /*
  2.  *--------------------------------------------------------------
  3.  * sample.main.c
  4.  *--------------------------------------------------------------
  5.  */
  6. #include "sample.h"
  7. #include "sample.AE.h"
  8. #include "sample.subs.h"
  9. #include "sample.menu.h"
  10. #include "sample.dlog.h"
  11. #include "sample.alrt.h"
  12. #include "sample.drag.h"
  13. #include "EmbossDraw.h"
  14.  
  15. /* static functions only used in this source */
  16. static void InitToolBox(void);
  17. static Boolean SetupMyGoodies(void);
  18. static Boolean DoStartUp(void);
  19. static void MainEventLoop(void);
  20. static void DoMouseDown(EventRecord *);
  21. static void DoKeyDown(EventRecord *);
  22. static void DoRepeatKey(EventRecord *);
  23. static void DoIdlelTask(void);
  24. static void GoodByeKiss(void);
  25.  
  26. /* global variables */
  27. Boolean gForever;
  28. Boolean    gNowBackGround;
  29.  
  30. /*
  31.  *--------------------------------------------------------------
  32.  * main
  33.  *--------------------------------------------------------------
  34.  */
  35. void main(void)
  36. {
  37.     InitToolBox();
  38.  
  39.     if (GestaltChecks()
  40.      && SetUpPreferences()
  41.      && SetupMyGoodies()) {
  42.          gForever = DoStartUp();
  43.         MainEventLoop();
  44.     }
  45.     GoodByeKiss();
  46. }
  47. /*
  48.  *--------------------------------------------------------------
  49.  * InitToolBox
  50.  *--------------------------------------------------------------
  51.  *    standard intialization
  52.  *--------------------------------------------------------------
  53.  */
  54. static void InitToolBox(void)
  55. {
  56.     int    i;
  57.     /* heap setup */
  58.     MaxApplZone();
  59.     for (i = 0; i < 20; ++i) MoreMasters();
  60.  
  61.     /* Toolbox intialization */
  62.     InitGraf(&qd.thePort);
  63.     InitFonts();
  64.     InitWindows();
  65.     InitMenus();
  66.     TEInit();
  67.     InitDialogs(0); 
  68.     InitCursor();
  69.     FlushEvents(everyEvent, 0);
  70. }
  71. /*
  72.  *--------------------------------------------------------------
  73.  * SetupMyGoodies
  74.  *--------------------------------------------------------------
  75.  *    set my equipments
  76.  *--------------------------------------------------------------
  77.  */
  78. static Boolean SetupMyGoodies(void)
  79. {
  80.     if (InitEmbossDraw() == false
  81.      || SetUpMyCursors() == false
  82.      || SetUpMyMenus() == false
  83.      || SetUpMyWindows() == false
  84.      || SetUpMyDialogs() == false
  85.      || SetUpMyAppleEvent() == false
  86.     ) {
  87.          FatalErrorAlert();
  88.         return (false);
  89.     }
  90.     return (true);
  91. }
  92. /*
  93.  *--------------------------------------------------------------
  94.  * DoStartUp
  95.  *--------------------------------------------------------------
  96.  *    handle the first apple events on startup
  97.  *--------------------------------------------------------------
  98.  */
  99. static Boolean DoStartUp(void)
  100. {
  101.     EventRecord    myEvent;
  102.  
  103.     while (!gAEDoubleClicked && !gAEDragAndDropped) {
  104.         if (WaitNextEvent(highLevelEventMask, &myEvent, -1, nil)
  105.          && myEvent.what == kHighLevelEvent)
  106.             AEProcessAppleEvent(&myEvent);
  107.     }
  108.     if (gAEDragAndDropped) {
  109.         return (false);
  110.     }
  111.     ShowWindow(gMainWindow);
  112.     return (true);
  113. }
  114. /*
  115.  *--------------------------------------------------------------
  116.  * MainEventLoop
  117.  *--------------------------------------------------------------
  118.  *    infinite event loop
  119.  *--------------------------------------------------------------
  120.  */
  121. static void MainEventLoop(void)
  122. {
  123.     EventRecord    myEvent;
  124.  
  125.     do {
  126.         long    sleepTicks = (gNowBackGround) ? -1 : 60;
  127.  
  128.         if (WaitNextEvent(everyEvent, &myEvent, sleepTicks, nil)) {
  129.             if (IsDialogEvent(&myEvent)) {
  130.                 DoDialogs(&myEvent);
  131.             }
  132.             else switch (myEvent.what) {
  133.             case   mouseDown:    DoMouseDown(&myEvent);    break;
  134.             case     keyDown:    DoKeyDown(&myEvent);    break;
  135.             case     autoKey:    DoRepeatKey(&myEvent);    break;
  136.             case   updateEvt:    DoUpdate(&myEvent);     break;
  137.             case activateEvt:    DoActivate(&myEvent);    break;
  138.             case       osEvt:    DoOSEvent(&myEvent);    break;
  139.             case     diskEvt:    DoMountDisk(&myEvent);    break;
  140.             case kHighLevelEvent:    AEProcessAppleEvent(&myEvent);    break;
  141.             }
  142.         } else {
  143.             DoIdlelTask();
  144.         }
  145.     } while (gForever);
  146. }
  147. /*
  148.  *--------------------------------------------------------------
  149.  * DoMouseDown
  150.  *--------------------------------------------------------------
  151.  *    mouse event handler
  152.  *--------------------------------------------------------------
  153.  */
  154. static void DoMouseDown(EventRecord * theEvent)
  155. {
  156.     WindowRef    aWindow;
  157.     short        windowPart = FindWindow(theEvent->where, &aWindow);
  158.  
  159.     switch (windowPart) {
  160.     case   inMenuBar:    DoMenus(MenuSelect(theEvent->where));    break;
  161.     case inSysWindow:    SystemClick(theEvent, aWindow);            break;
  162.     case   inContent:    DoContent(aWindow, theEvent->where);    break;
  163.     case      inGrow:    DoGrow(aWindow, theEvent->where);        break;
  164.     case      inDrag:    DoDrag(aWindow, theEvent->where);        break;
  165.     case    inGoAway:    DoGoAway(aWindow, theEvent->where);        break;
  166.     case    inZoomIn:
  167.     case   inZoomOut:    DoZoom(aWindow, theEvent->where, windowPart);    break;
  168.     }
  169. }
  170. /*
  171.  *--------------------------------------------------------------
  172.  *    DoKeyDown
  173.  *--------------------------------------------------------------
  174.  *    handle keyboard event
  175.  *--------------------------------------------------------------
  176.  */
  177. static void DoKeyDown(EventRecord *theEvent)
  178. {
  179.     char    charCode;
  180.  
  181.     if (theEvent->modifiers & cmdKey) {
  182.         charCode = theEvent->message & charCodeMask;
  183.         DoMenus(MenuKey(charCode));
  184.     }
  185.     else
  186.     {
  187.     /*
  188.         handle TextEdit or something
  189.     */
  190.     }
  191. }
  192. /*
  193.  *--------------------------------------------------------------
  194.  *    DoRepeatKey
  195.  *--------------------------------------------------------------
  196.  *    handle auto-repeat keyboard event
  197.  *--------------------------------------------------------------
  198.  */
  199. static void DoRepeatKey(EventRecord * theEvent)
  200. {
  201.     char    charCode = theEvent->message & charCodeMask;
  202.     /*
  203.         handle repeat key
  204.     */
  205. }
  206. /*
  207.  *--------------------------------------------------------------
  208.  * DoIdlelTask
  209.  *--------------------------------------------------------------
  210.  *    idle task procedures
  211.  *--------------------------------------------------------------
  212.  */
  213. void DoIdlelTask(void)
  214. {
  215.     if (gNowBackGround == false) {
  216.  
  217.         /* regular house keepings */
  218.         AdjustMyMenus();
  219.         AdjustMyCursor();
  220.  
  221.         /* check if some error has occurred in background */
  222.         CheckMyErrors();
  223.     }
  224.     /*
  225.         other idle tasks
  226.     */
  227. }
  228. /*
  229.  *--------------------------------------------------------------
  230.  * GoodByeKiss
  231.  *--------------------------------------------------------------
  232.  */
  233. static void GoodByeKiss(void)
  234. {
  235.     RemoveMyAppleEvent();
  236.     RemoveMyDragAndDrop();
  237.     SaveMyPreferences();
  238. }
  239.