home *** CD-ROM | disk | FTP | other *** search
- /* MPW C Application skeleton by Dean Yu */
- /* Event handling routines */
-
- #include <Types.h>
- #include <AppleEvents.h>
- #include <Desk.h>
- #include <Dialogs.h>
- #include <Editions.h>
- #include <EPPC.h>
- #include <Events.h>
- #include <Menus.h>
- #include <SegLoad.h>
- #include <TextUtils.h>
- #include <ToolUtils.h>
- #include <Windows.h>
-
- #include "globals.h"
- #include "eventHandler.h"
- #include "menuDispatch.h"
- #include "WindowExtensions.h"
-
- /* Routine prototypes */
-
- void DoMouseEvent (EventRecord *event);
- void DoKeyEvent (EventRecord *event);
- void DoActivateEvent(EventRecord *event);
- void DoUpdateEvent (EventRecord *event);
- void DrawWindowContent(Boolean doFill);
- void DoOSEvent(EventRecord *event);
- void DoHighLevelEvent(EventRecord *event);
- void HandleGenericHighLevelEvent(EventRecord *event);
-
- void FatalErrorAlert(short errID)
-
- /*
- Fatal error alert routine.
- If an error occurs, this routine is called with the error message number
- that message is pulled from the error STR# resource, and displayed in an
- alert. The application then quits when the user clicks the “OK” button.
- */
-
- {
- short alrtItem;
- Str255 errString;
-
- GetIndString(&errString, rFatalErrorStringsID, errID);
- ParamText(errString, nil, nil, nil);
- alrtItem = Alert(rFatalErrorAlertID, nil);
- ExitToShell();
- }
-
- void EventLoop()
- {
- EventRecord event;
- DialogPtr theDialog;
- short itemHit;
- Boolean gotEvent;
-
- gDone = false;
- gInBackground = false;
-
- do {
- gotEvent = WaitNextEvent(everyEvent, &event, 15, nil);
- if (gotEvent) {
- if (!IsDialogEvent(&event))
- HandleEvent(&event);
- else {
- DialogSelect(&event, &theDialog, &itemHit);
- if ((itemHit == ok) || (itemHit == cancel))
- DisposeFindDialog();
-
- if (event.what == osEvt)
- DoOSEvent(&event);
- }
- }
-
- // If we’re in the background, and the find dialog is up, close it after
- // a time to demonstrate what should happen to floating windows in the background.
-
- if ((gInBackground != false) && (gTimeToDialogDismissal > 0)) {
- --gTimeToDialogDismissal;
- if (gTimeToDialogDismissal == 0)
- DisposeFindDialog();
- }
- } while (!gDone);
- ExitToShell();
- }
-
- void HandleEvent(EventRecord *event)
- {
- switch (event->what) {
- case mouseDown: DoMouseEvent(event);
- break;
- case keyDown:
- case autoKey: DoKeyEvent(event);
- break;
- case activateEvt: DoActivateEvent(event);
- break;
- case updateEvt: DoUpdateEvent(event);
- break;
- case osEvt: DoOSEvent(event);
- break;
- case kHighLevelEvent: DoHighLevelEvent(event);
- break;
-
- }
- }
-
- void DoMouseEvent(EventRecord *event)
- {
- short part;
- long growResult;
- WindowRef whichWindow;
- WindowRef frontWindow;
-
- part = FindWindow(event->where, &((WindowPtr) whichWindow));
- switch (part) {
- case inDesk: break;
- case inMenuBar: DoMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow: SystemClick(event, (WindowPtr) whichWindow);
- break;
- case inContent: frontWindow = (WindowRef) FrontWindow();
- if ((GetWindowKind(frontWindow) == dialogKind) &&
- (whichWindow != frontWindow))
- SysBeep(1);
- else
- if (whichWindow != frontWindow)
- SelectReferencedWindow(whichWindow);
- break;
- case inDrag: frontWindow = (WindowRef) FrontWindow();
- if ((GetWindowKind(frontWindow) == dialogKind) &&
- (whichWindow != frontWindow))
- SysBeep(1);
- else
- DragReferencedWindow(whichWindow, event->where, &gDragArea);
- break;
- case inGrow: growResult = GrowWindow((WindowPtr) whichWindow, event->where, &gGrowBounds);
- SizeWindow((WindowPtr) whichWindow, growResult & 0x0000FFFF, (growResult & 0xFFFF0000) >> 16, true);
- break;
- case inGoAway: if (TrackGoAway((WindowPtr) whichWindow, event->where)) {
- DisposeWindowReference(whichWindow);
- if (FrontNonFloatingWindow() == nil)
- DisableItem(GetMHandle(rFileMenuID), kCloseItem);
- }
- break;
- case inZoomIn:
- case inZoomOut: break;
- }
- }
-
- void DoKeyEvent(EventRecord *event)
- {
- long theKey;
-
- theKey = event->message & charCodeMask;
- if ((event->modifiers & cmdKey) && (event->what == keyDown))
- DoMenuCommand(MenuKey((char) theKey));
- }
-
- void DoActivateEvent(EventRecord *event)
- {
- Boolean activating = false;
-
- if (GetWindowKind((WindowRef) event->message) != dialogKind) {
- if (event->modifiers & activeFlag)
- activating = true;
-
- DebugStr("\pGot activate from event loop.");
-
- ActivateEventHandler((WindowRef) event->message, activating);
- }
- }
-
- pascal void ActivateEventHandler(WindowRef theWindow, Boolean activateWindow)
- {
- GrafPtr currentPort;
-
- GetPort(¤tPort);
- SetPort((GrafPtr) theWindow);
-
- DrawWindowContent(activateWindow);
-
- if (activateWindow == false)
- SetPort(currentPort);
- }
-
- pascal void RecordingFloaterActivateHandler(WindowRef theWindow, Boolean activateWindow)
- {
- GrafPtr currentPort;
- PicHandle recordingControlsPicture;
- short whichPicture;
-
- GetPort(¤tPort);
- SetPort((GrafPtr) theWindow);
-
- if (activateWindow == true)
- whichPicture = 128;
- else
- whichPicture = 129;
- recordingControlsPicture = GetPicture(whichPicture);
-
- DrawPicture(recordingControlsPicture, &((**recordingControlsPicture).picFrame));
- SetWindowPic((WindowPtr) theWindow, recordingControlsPicture);
-
- if (activateWindow == false)
- SetPort(currentPort);
- }
-
- pascal void ToolsFloaterActivateHandler(WindowRef theWindow, Boolean activateWindow)
- {
- GrafPtr currentPort;
- PicHandle toolsPicture;
- short whichPicture;
-
- GetPort(¤tPort);
- SetPort((GrafPtr) theWindow);
-
- if (activateWindow == true)
- whichPicture = 130;
- else
- whichPicture = 131;
- toolsPicture = GetPicture(whichPicture);
-
- DrawPicture(toolsPicture, &((**toolsPicture).picFrame));
- SetWindowPic((WindowPtr) theWindow, toolsPicture);
-
- if (activateWindow == false)
- SetPort(currentPort);
- }
-
- void DoUpdateEvent(EventRecord *event)
- {
- WindowPtr theWindow = (WindowPtr) event->message;
-
- SetPort(theWindow);
- BeginUpdate(theWindow);
- EraseRect(&(theWindow->portRect));
- DrawWindowContent(theWindow == FrontNonFloatingWindow());
- EndUpdate(theWindow);
- }
-
- void DrawWindowContent(Boolean doFill)
- {
- Rect tempRect;
-
- SetRect(&tempRect, 5, 5, 50, 50);
-
- if (doFill == true)
- FillRect(&tempRect, &(qd.black));
- else {
- FrameRect(&tempRect);
- InsetRect(&tempRect, 1, 1);
- EraseRect(&tempRect);
- }
- }
-
- void DoOSEvent(EventRecord *event)
- {
- switch ((event->message >> 24) & 0xFF) {
- case mouseMovedMessage: break;
- case suspendResumeMessage: if (event->message & resumeFlag) {
- gInBackground = false;
- gTimeToDialogDismissal = 0;
- DisableItem(GetMHandle(rEditMenuID), 0);
- DrawMenuBar();
- ResumeFloatingWindows();
- SetCursor(&qd.arrow);
- }
- else {
- gInBackground = true;
- if (GetWindowKind((WindowRef) FrontWindow()) == dialogKind)
- gTimeToDialogDismissal = 5;
- SuspendFloatingWindows();
- }
- break;
- }
- }
-
-
- /* Handle high level events */
-
- void DoHighLevelEvent(EventRecord *event)
- {
- OSErr AEProcessResult;
-
- switch (event->message) {
- case kCoreEventClass: AEProcessResult = AEProcessAppleEvent(event); /* Handle core AppleEvents */
- break;
- case rSectionType: break; /* Handle Edition Manager AppleEvents */
- default: HandleGenericHighLevelEvent(event); /* Other high level events */
- break;
- }
- }
-
- void HandleGenericHighLevelEvent(EventRecord *event)
- {
- TargetID eventSenderInfo;
- unsigned long eventIdentifier;
- Ptr dataBuffer;
- unsigned long dataSize;
- OSErr HLEventError;
-
- dataSize = 0;
- dataBuffer = nil;
- HLEventError = AcceptHighLevelEvent(&eventSenderInfo, &eventIdentifier, dataBuffer, &dataSize);
-
- /*
- Since we don’t know how much data is coming in, call AcceptHighLevelEvent once to
- find out how much data was sent, then use create a new buffer large enough to hold
- all the data, then call AcceptHighLevelEvent again to get the data.
- */
-
- if (HLEventError == bufferIsSmall) {
- dataBuffer = NewPtr(dataSize);
- HLEventError = AcceptHighLevelEvent(&eventSenderInfo, &eventIdentifier, dataBuffer, &dataSize);
- }
-
- /* Dispatch the high level event to the proper routine depending on the message class */
-
- switch (event->message) {
- }
-
- if (dataBuffer)
- DisposPtr(dataBuffer);
- }
-