home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************
- * applic.c
- *
- * THINK C 4.0 Application Shell for the Macintosh
- *
- * Written by Paco Xander Nathan
- * ⌐1990, Motorola Inc. Public domain source code.
- ********************************************************************************/
-
- #include "applic.h"
- #include "window.h"
- #include "dialog.h"
- #include "context.h"
- #include "edit.h"
- #include "noise.h"
- #include "setup.h"
- #include "print.h"
- #include "document.h"
- #include "ascii.h"
- #include "help.h"
-
-
- #define mouseMovedMsg 0xfa
- #define osEvent app4Evt
- #define resumeMask 1
- #define suspendResumeMsg 1
- #define CURS_DELAY 6L
-
-
- /* External Data Structures
- */
- acurHandle
- curList; /* rsrc for watch cursor */
-
- MachineConfig
- laMachine;
-
-
- #ifdef PROTOTYPES
- /* Local Function Prototypes
- */
- unsigned long ApplSleep (void);
- void ApplEventLoop (EventRecord *theEvent);
- short main (void);
- #endif
-
- /* Animate the watch cursor - call with a "TRUE" argument to reset to the initial
- * "nine o'clock" position then with a "FALSE argument to turn the hands...
- */
- void
- ApplSpinCurs (reset)
- register Boolean reset;
- {
- register CursHandle watchHdl;
- static long lastTick = 0L;
-
- if ((TickCount() - lastTick) > CURS_DELAY) {
- HLock(curList);
-
- /* Load the "nine o'clock" watch cursor if requested
- */
- if (reset)
- (*curList)->which = 0;
-
- /* Cycle to the next position watch cursor
- */
- SetCursor(*((*curList)->curs[((*curList)->which < (*curList)->frames) ? ((*curList)->which++) : ((*curList)->which = 0)]));
-
- HUnlock(curList);
- lastTick = TickCount();
- }
- }
-
-
- /* Special case of event handling; check for abort event (cmd-.)
- */
- Boolean
- ApplAbort ()
- {
- EventRecord theEvent;
- register Boolean aborted = FALSE;
-
- if (EventAvail(keyDownMask, &theEvent))
- if (theEvent.modifiers & cmdKey)
- if ((unsigned char) theEvent.message == '.')
- aborted = TRUE;
-
- return aborted;
- }
-
-
- /* This is the application's "idle time" processing routine... Null events,
- * background events, blinking cursors, etc.
- */
- void
- ApplTask ()
- {
- register WindowPtr theWindow = FrontWindow();
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- /* Keep the cursor blinking in text editor windows
- */
- if (WindIsApp(theWindow) && (infoPtr->kind == wkText) && infoPtr->active)
- TEIdle(infoPtr->item.text.teHdl);
-
- /* Adjust the window layout
- */
- if (windTrip)
- WindTileStack();
- }
-
-
- /* Exit the application with the utmost poise and grace
- */
- void
- ApplQuit ()
- {
- /* Application specific clean up
- */
- if (((WindowPeek) wPtrGnos)->visible)
- DocClose(wPtrGnos);
-
- /* Goodbye, my love!
- */
- ExitToShell();
- }
-
-
- /* Calculate a sleep value for WaitNextEvent. This takes into account the processing
- * time for which ApplTask() uses idle time, null events, etc.
- */
- static unsigned long
- ApplSleep ()
- {
- register long theSleep = 10L;
- register WindowPtr theWindow;
- register InfoPtr infoPtr;
-
- if (!laMachine.inBackground && WindIsApp(theWindow = FrontWindow())) {
- /* Get the window info
- */
- infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- if ((infoPtr->kind == wkText) && infoPtr->active)
- theSleep = GetCaretTime();
- }
-
- return theSleep;
- }
-
-
- /* Handle various event cases in a Multi-Tasking, Multi-Windowing MacOS environment
- */
- static void
- ApplEventLoop (theEvent)
- register EventRecord *theEvent;
- {
- WindowPtr theWindow;
- register InfoPtr infoPtr;
- register char theChar;
- register short thePart;
-
- switch(theEvent->what) {
- case nullEvent:
- ApplTask();
- break;
-
- case mouseDown:
- switch(thePart = FindWindow(theEvent->where, &theWindow)) {
- case inDesk:
- SysBeep(5);
- break;
-
- case inGoAway:
- if (WindIsApp(theWindow) && TrackGoAway(theWindow, theEvent->where))
- WindClose(theWindow);
-
- break;
-
- case inMenuBar:
- ContAdjMenu();
- ContDispatch(MenuSelect(theEvent->where));
- break;
-
- case inSysWindow:
- SystemClick(theEvent, theWindow);
- break;
-
- case inDrag:
- if (WindIsApp(theWindow))
- DragWindow(theWindow, theEvent->where, &dragArea);
-
- break;
-
- case inGrow:
- if (WindIsApp(theWindow))
- WindGrow(theWindow, theEvent);
- break;
-
- case inContent:
- if (theWindow != FrontWindow() && WindIsApp(theWindow))
- SelectWindow(theWindow);
- else
- WindContent(theWindow, theEvent);
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(theWindow, theEvent->where, thePart))
- WindZoom(theWindow, thePart);
- break;
- }
- break;
-
- case keyDown:
- case autoKey:
- theChar = (char) theEvent->message & charCodeMask;
-
- if ((theEvent->modifiers & btnState) && (theChar == asciiHelp)) {
- HelpAppl();
- }
- else if (theEvent->modifiers & cmdKey) {
- if (theEvent->what != autoKey) {
- ContAdjMenu();
- ContDispatch(MenuKey(theChar));
- }
- }
- else {
- theWindow = FrontWindow();
- infoPtr = (InfoPtr) GetWRefCon(theWindow);
-
- if (WindIsApp(theWindow) && (infoPtr->kind == wkText) && infoPtr->active)
- EditKeyStroke(theWindow, theChar);
- }
-
- break;
-
- case activateEvt:
- if (WindIsApp((WindowPtr) theEvent->message))
- WindActivate((WindowPtr) theEvent->message, theEvent->modifiers & activeFlag);
- break;
-
- case updateEvt:
- if (WindIsApp((WindowPtr) theEvent->message))
- WindUpdate((WindowPtr) theEvent->message);
- break;
-
- case osEvent:
- switch (theEvent->message >> 24) {
- case mouseMovedMsg:
- ApplTask();
- break;
-
- case suspendResumeMsg:
- if (theEvent->message & resumeMask) {
- laMachine.inBackground = FALSE;
-
- if (WindIsApp(FrontWindow()))
- WindActivate(FrontWindow(), TRUE);
- }
- else {
- laMachine.inBackground = TRUE;
-
- if (WindIsApp(FrontWindow()))
- WindActivate(FrontWindow(), FALSE);
- }
- break;
-
- }
- break;
-
- default:
- ;
- }
- }
-
-
- /* The main() routine
- */
- short
- main ()
- {
- register RgnHandle cursorRgn;
- register Boolean hasEvent;
- EventRecord theEvent;
-
- /* Initialization the application and unload the non-essential code segments
- */
- SetupAppl();
- UnloadSeg((Ptr) SetupAppl);
- UnloadSeg((Ptr) PrintDocument);
- cursorRgn = NewRgn();
-
- /* MultiFinder aware event processing loop
- */
- if (laMachine.hasWNE)
- while (TRUE) {
- ContAdjCurs(theEvent.where, cursorRgn);
- ApplTask();
-
- if (WaitNextEvent(everyEvent, &theEvent, ApplSleep(), cursorRgn)) {
- if (!((theEvent.what == keyDown) && (theEvent.modifiers & cmdKey)) && IsDialogEvent(&theEvent))
- DlogModelessEvent(&theEvent);
- else
- ApplEventLoop(&theEvent);
- }
- }
- else
- while (TRUE) {
- ContAdjCurs(theEvent.where, cursorRgn);
- SystemTask();
- ApplTask();
- hasEvent = GetNextEvent(everyEvent, &theEvent);
-
- if (!((theEvent.what == keyDown) && (theEvent.modifiers & cmdKey)) && IsDialogEvent(&theEvent))
- DlogModelessEvent(&theEvent);
- else if (hasEvent)
- ApplEventLoop(&theEvent);
- }
- }
-