home *** CD-ROM | disk | FTP | other *** search
- /*
-
- FILE: Originally AppDialogs.c++
- Renamed as MovableModalDialogs.cpp
-
- CREATED: MAY 29, 1994
-
- BY: Hiep Dam
- Copyright© 1994 by Hiep Dam, All Rights Reserved
-
- INFO: MovableModalDialogs.cpp contains routines that deal with both
- modeless and movable modal dialog boxes. But mainly
- movable modal. Just don't let the name fool you.
-
- This file serves as a "dispatching center"; it collects info
- concerning events sent to it by the main event loop (mousedowns,
- updates, etc.), determines which dialog the event pertains to,
- and calls the appropriate handler.
-
- VERSION HISTORY:
- 2.0 Nov 8 95
- Completely updated code so it can be plugged into any
- application seamlessly. And doing it required very few
- changes, which tells me this is well written.
- I'm so proud...
- */
-
- #include "MovableModalDialogs.h"
- #include "DialogUtils.h"
- #include "QDUtils.h"
- #include "Class_DynamoArray.cpp"
-
- // ---------------------------------------------------------------------------
-
- typedef struct DialogHandlers {
- DialogPtr theDialog; // Pointer to the dialog
- long dialogID; // Optional id in refcon of dialog
- DialogUpdateHandler updateHandler;
- DialogHitHandler hitHandler;
- DialogKeyHandler keyHandler;
- DialogActivateHandler actHandler;
- DialogIdleHandler idleHandler;
- } *DialogHandlersPtr, **DialogHandlersHdl;
-
- static DynamoArray<DialogHandlersPtr> gDialogHandlers;
- static AdjustMenusProc sAdjustMenusProc = NULL;
-
- // ---------------------------------------------------------------------------
-
- void InitMovableModalDialogs(AdjustMenusProc adjustMenus) {
- sAdjustMenusProc = adjustMenus;
- } // END InitMovableModalDialogs
-
- // ---------------------------------------------------------------------------
-
- void RegisterMovableModalDialog(
- DialogPtr theDialog,
- DialogHitHandler hitProc,
- DialogKeyHandler keyProc,
- DialogUpdateHandler updateProc,
- DialogActivateHandler activateProc,
- DialogIdleHandler idleProc) {
-
- DialogHandlersPtr theHandler;
- theHandler = (DialogHandlersPtr)NewPtr(sizeof(DialogHandlers));
-
- theHandler->theDialog = theDialog;
- theHandler->dialogID = GetWRefCon(theDialog);
- theHandler->updateHandler = updateProc;
- theHandler->hitHandler = hitProc;
- theHandler->keyHandler = keyProc;
- theHandler->actHandler = activateProc;
- theHandler->idleHandler = idleProc;
-
- gDialogHandlers.Append(theHandler);
- } // END RegisterMovableModalDialog
-
- // ---------------------------------------------------------------------------
-
- void UnregisterMovableModalDialog(DialogPtr theDialog) {
- short i;
- DialogHandlersPtr oneHandler;
-
- for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
- oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
-
- if (oneHandler->theDialog == theDialog) {
- gDialogHandlers.Delete(i);
- DisposePtr((Ptr)oneHandler);
- break;
- }
- }
-
- // By unregistering, we're assuming (pretty safely) that
- // the caller is closing the dialog box. So now we'll
- // call the update menu procedure.
- sAdjustMenusProc();
- } // END UnregisterMovableModalDialog
-
- // ---------------------------------------------------------------------------
-
- void HandleUpdateDialog(DialogPtr theDialog, EventRecord *theEvt, Boolean inBackground) {
- GrafPtr savePort;
- short i;
- Boolean frontMost;
- DialogHandlersPtr oneHandler;
-
- GetPort(&savePort);
-
- for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
- oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
-
- if (oneHandler->theDialog == theDialog) {
- if (inBackground || FrontWindow() != theDialog)
- frontMost = false;
- else
- frontMost = true;
-
- SetPort(theDialog);
- if (oneHandler->updateHandler != NULL)
- oneHandler->updateHandler(theDialog, theEvt, frontMost);
- break;
- }
- }
-
- SetPort(savePort);
- } // END HandleUpdateDialog
-
- // ---------------------------------------------------------------------------
-
- void HandleContentClickModeless(EventRecord *theEvt, DialogPtr theDialog, short itemHit) {
- DialogHandlersPtr oneHandler;
- short i;
-
- for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
- oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
-
- if (oneHandler->theDialog == theDialog) {
- if (oneHandler->hitHandler != NULL)
- oneHandler->hitHandler(theDialog, theEvt, itemHit);
- return;
- }
- }
- } // END HandleContentClickModeless
-
- // ---------------------------------------------------------------------------
-
- Boolean HandleKeyDownModeless(DialogPtr theDialog, EventRecord *theEvt) {
- short i;
- DialogHandlersPtr oneHandler;
- char theKey = theEvt->message & charCodeMask;
-
- for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
- oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
-
- if (oneHandler->theDialog == theDialog) {
- if (oneHandler->keyHandler != NULL)
- return(oneHandler->keyHandler(theDialog, theEvt));
- break;
- }
- }
-
- return(false);
- } // END HandleKeyDownInModeless
-
- // ---------------------------------------------------------------------------
-
- void HandleActivateModeless(DialogPtr theDialog, Boolean activate) {
- short i;
- GrafPtr savePort;
- DialogHandlersPtr oneHandler;
-
- GetPort(&savePort);
-
- for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
- oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
-
- if (oneHandler->theDialog == theDialog) {
- SetPort(theDialog);
- if (oneHandler->actHandler != NULL)
- oneHandler->actHandler(theDialog, activate);
- break;
- }
- }
- SetPort(savePort);
- } // END HandleActivateModeless
-
- // ---------------------------------------------------------------------------
-
- void HandleIdleModeless(DialogPtr theDialog) {
- short i;
- GrafPtr savePort;
- DialogHandlersPtr oneHandler;
-
- GetPort(&savePort);
- for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
- oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
-
- if (oneHandler->theDialog == theDialog) {
- SetPort(theDialog);
- if (oneHandler->idleHandler != NULL)
- oneHandler->idleHandler(theDialog);
- break;
- }
- }
- SetPort(savePort);
- } // END HandleIdleModeless
-
- // ---------------------------------------------------------------------------
-
- Boolean IsMovableModal(WindowPtr thisWindow) {
- if (thisWindow == NULL)
- return(false);
- else
- return(GetWVariant(thisWindow) == movableDBoxProc);
- } // END IsMovableModal
-
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- // END MovableModalDialogs.cpp