home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / luschsrc.sit / applic.c < prev    next >
C/C++ Source or Header  |  1990-05-23  |  7KB  |  321 lines

  1. /********************************************************************************
  2.  *    applic.c
  3.  *
  4.  *    THINK C 4.0 Application Shell for the Macintosh
  5.  *
  6.  *    Written by Paco Xander Nathan
  7.  *    ⌐1990, Motorola Inc.  Public domain source code.
  8.  ********************************************************************************/
  9.  
  10. #include "applic.h"
  11. #include "window.h"
  12. #include "dialog.h"
  13. #include "context.h"
  14. #include "edit.h"
  15. #include "noise.h"
  16. #include "setup.h"
  17. #include "print.h"
  18. #include "document.h"
  19. #include "ascii.h"
  20. #include "help.h"
  21.  
  22.  
  23. #define mouseMovedMsg        0xfa
  24. #define osEvent                app4Evt
  25. #define resumeMask            1
  26. #define suspendResumeMsg    1
  27. #define CURS_DELAY            6L
  28.  
  29.  
  30. /* External Data Structures
  31.  */
  32. acurHandle
  33.     curList;                /* rsrc for watch cursor */
  34.  
  35. MachineConfig
  36.     laMachine;
  37.  
  38.  
  39. #ifdef PROTOTYPES
  40. /* Local Function Prototypes
  41.  */
  42. unsigned long ApplSleep (void);
  43. void ApplEventLoop (EventRecord *theEvent);
  44. short main (void);
  45. #endif
  46.  
  47. /* Animate the watch cursor - call with a "TRUE" argument to reset to the initial
  48.  * "nine o'clock" position then with a "FALSE argument to turn the hands...
  49.  */
  50. void
  51. ApplSpinCurs (reset)
  52.     register Boolean reset;
  53. {
  54.     register CursHandle watchHdl;
  55.     static long lastTick = 0L;
  56.     
  57.     if ((TickCount() - lastTick) > CURS_DELAY) {
  58.         HLock(curList);
  59.     
  60.         /* Load the "nine o'clock" watch cursor if requested
  61.          */
  62.         if (reset)
  63.             (*curList)->which = 0;
  64.  
  65.         /* Cycle to the next position watch cursor
  66.          */
  67.         SetCursor(*((*curList)->curs[((*curList)->which < (*curList)->frames) ? ((*curList)->which++) : ((*curList)->which = 0)]));
  68.     
  69.         HUnlock(curList);
  70.         lastTick = TickCount();
  71.     }
  72. }
  73.  
  74.  
  75. /* Special case of event handling; check for abort event (cmd-.)
  76.  */
  77. Boolean
  78. ApplAbort ()
  79. {
  80.     EventRecord theEvent;
  81.     register Boolean aborted = FALSE;
  82.  
  83.     if (EventAvail(keyDownMask, &theEvent))
  84.         if (theEvent.modifiers & cmdKey) 
  85.             if ((unsigned char) theEvent.message == '.')
  86.                 aborted = TRUE;
  87.  
  88.     return aborted;
  89. }
  90.  
  91.  
  92. /* This is the application's "idle time" processing routine...  Null events,
  93.  * background events, blinking cursors, etc.
  94.  */
  95. void
  96. ApplTask ()
  97. {
  98.     register WindowPtr theWindow = FrontWindow();
  99.     register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
  100.  
  101.     /* Keep the cursor blinking in text editor windows
  102.      */
  103.      if (WindIsApp(theWindow) && (infoPtr->kind == wkText) && infoPtr->active)
  104.         TEIdle(infoPtr->item.text.teHdl);
  105.  
  106.     /* Adjust the window layout
  107.      */
  108.     if (windTrip)
  109.         WindTileStack();
  110. }
  111.  
  112.  
  113. /* Exit the application with the utmost poise and grace
  114.  */
  115. void
  116. ApplQuit ()
  117. {
  118.     /* Application specific clean up
  119.      */
  120.     if (((WindowPeek) wPtrGnos)->visible)
  121.         DocClose(wPtrGnos);
  122.  
  123.     /* Goodbye, my love!
  124.      */
  125.     ExitToShell();
  126. }
  127.  
  128.  
  129. /* Calculate a sleep value for WaitNextEvent.  This takes into account the processing
  130.  * time for which ApplTask() uses idle time, null events, etc.
  131.  */
  132. static unsigned long
  133. ApplSleep ()
  134. {
  135.     register long theSleep = 10L;
  136.     register WindowPtr theWindow;
  137.     register InfoPtr infoPtr;
  138.     
  139.     if (!laMachine.inBackground && WindIsApp(theWindow = FrontWindow())) {
  140.          /* Get the window info
  141.          */
  142.         infoPtr = (InfoPtr) GetWRefCon(theWindow);
  143.  
  144.          if ((infoPtr->kind == wkText) && infoPtr->active)
  145.             theSleep = GetCaretTime();
  146.     }
  147.     
  148.     return theSleep;
  149. }
  150.  
  151.  
  152. /* Handle various event cases in a Multi-Tasking, Multi-Windowing MacOS environment
  153.  */
  154. static void
  155. ApplEventLoop (theEvent) 
  156.     register EventRecord *theEvent;
  157. {
  158.     WindowPtr theWindow;
  159.     register InfoPtr infoPtr;
  160.     register char theChar;
  161.     register short thePart;
  162.     
  163.     switch(theEvent->what) {
  164.     case nullEvent:
  165.         ApplTask();
  166.         break;
  167.  
  168.     case mouseDown:
  169.         switch(thePart = FindWindow(theEvent->where, &theWindow)) {
  170.         case inDesk: 
  171.             SysBeep(5);
  172.             break;
  173.     
  174.         case inGoAway:
  175.             if (WindIsApp(theWindow) && TrackGoAway(theWindow, theEvent->where))
  176.                 WindClose(theWindow);
  177.  
  178.             break;
  179.  
  180.         case inMenuBar:
  181.             ContAdjMenu();
  182.             ContDispatch(MenuSelect(theEvent->where));
  183.             break;
  184.                 
  185.         case inSysWindow:
  186.             SystemClick(theEvent, theWindow);
  187.             break;
  188.         
  189.         case inDrag:
  190.             if (WindIsApp(theWindow))
  191.                 DragWindow(theWindow, theEvent->where, &dragArea);
  192.  
  193.             break;
  194.  
  195.         case inGrow:
  196.             if (WindIsApp(theWindow))
  197.                 WindGrow(theWindow, theEvent);
  198.             break;
  199.  
  200.         case inContent:
  201.             if (theWindow != FrontWindow() && WindIsApp(theWindow))
  202.                 SelectWindow(theWindow);
  203.             else 
  204.                 WindContent(theWindow, theEvent);
  205.             break;
  206.  
  207.         case inZoomIn:
  208.         case inZoomOut:
  209.             if (TrackBox(theWindow, theEvent->where, thePart))
  210.                 WindZoom(theWindow, thePart);
  211.             break;
  212.         }
  213.         break;
  214.  
  215.     case keyDown:
  216.     case autoKey: 
  217.         theChar = (char) theEvent->message & charCodeMask;
  218.  
  219.         if ((theEvent->modifiers & btnState) && (theChar == asciiHelp)) {
  220.             HelpAppl();
  221.         }
  222.         else if (theEvent->modifiers & cmdKey) {
  223.             if (theEvent->what != autoKey) {
  224.                 ContAdjMenu();
  225.                 ContDispatch(MenuKey(theChar));
  226.             }
  227.         }
  228.         else {
  229.             theWindow = FrontWindow();
  230.             infoPtr = (InfoPtr) GetWRefCon(theWindow);
  231.  
  232.             if (WindIsApp(theWindow) && (infoPtr->kind == wkText) && infoPtr->active)
  233.                 EditKeyStroke(theWindow, theChar);
  234.         }
  235.  
  236.         break;
  237.  
  238.     case activateEvt:
  239.         if (WindIsApp((WindowPtr) theEvent->message))
  240.             WindActivate((WindowPtr) theEvent->message, theEvent->modifiers & activeFlag);
  241.         break;
  242.  
  243.     case updateEvt: 
  244.         if (WindIsApp((WindowPtr) theEvent->message))
  245.             WindUpdate((WindowPtr) theEvent->message);
  246.         break;
  247.  
  248.     case osEvent:
  249.         switch (theEvent->message >> 24) {
  250.         case mouseMovedMsg:
  251.             ApplTask();
  252.             break;
  253.         
  254.         case suspendResumeMsg:
  255.             if (theEvent->message & resumeMask) {
  256.                 laMachine.inBackground = FALSE;
  257.  
  258.                 if (WindIsApp(FrontWindow()))
  259.                     WindActivate(FrontWindow(), TRUE);
  260.             }
  261.             else {
  262.                 laMachine.inBackground = TRUE;
  263.  
  264.                 if (WindIsApp(FrontWindow()))
  265.                     WindActivate(FrontWindow(), FALSE);
  266.             }
  267.             break;
  268.             
  269.         }
  270.         break;
  271.  
  272.     default:
  273.         ;
  274.     }
  275. }
  276.  
  277.  
  278. /* The main() routine
  279.  */
  280. short
  281. main () 
  282. {
  283.     register RgnHandle cursorRgn;
  284.     register Boolean hasEvent;
  285.     EventRecord theEvent;
  286.  
  287.     /* Initialization the application and unload the non-essential code segments
  288.      */
  289.     SetupAppl();
  290.     UnloadSeg((Ptr) SetupAppl);
  291.     UnloadSeg((Ptr) PrintDocument);
  292.     cursorRgn = NewRgn();
  293.     
  294.     /* MultiFinder aware event processing loop
  295.      */
  296.     if (laMachine.hasWNE)
  297.         while (TRUE) {
  298.             ContAdjCurs(theEvent.where, cursorRgn);
  299.             ApplTask();
  300.  
  301.             if (WaitNextEvent(everyEvent, &theEvent, ApplSleep(), cursorRgn)) {
  302.                 if (!((theEvent.what == keyDown) && (theEvent.modifiers & cmdKey)) && IsDialogEvent(&theEvent))
  303.                     DlogModelessEvent(&theEvent);
  304.                 else
  305.                     ApplEventLoop(&theEvent);
  306.             }
  307.         }
  308.     else
  309.         while (TRUE) {
  310.             ContAdjCurs(theEvent.where, cursorRgn);
  311.             SystemTask();
  312.             ApplTask();
  313.             hasEvent = GetNextEvent(everyEvent, &theEvent);
  314.  
  315.             if (!((theEvent.what == keyDown) && (theEvent.modifiers & cmdKey)) && IsDialogEvent(&theEvent))
  316.                 DlogModelessEvent(&theEvent);
  317.             else if (hasEvent)
  318.                 ApplEventLoop(&theEvent);
  319.         }
  320. }
  321.