diff options
Diffstat (limited to 'macosx')
-rw-r--r-- | macosx/tkMacOSXHLEvents.c | 291 | ||||
-rw-r--r-- | macosx/tkMacOSXWindowEvent.c | 26 |
2 files changed, 213 insertions, 104 deletions
diff --git a/macosx/tkMacOSXHLEvents.c b/macosx/tkMacOSXHLEvents.c index e1789f8..4bbffa6 100644 --- a/macosx/tkMacOSXHLEvents.c +++ b/macosx/tkMacOSXHLEvents.c @@ -10,20 +10,20 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkMacOSXHLEvents.c,v 1.5.2.7 2006/03/28 02:44:13 das Exp $ + * RCS: @(#) $Id: tkMacOSXHLEvents.c,v 1.5.2.8 2006/04/11 10:21:53 das Exp $ */ #include "tkMacOSXInt.h" /* * This is a Tcl_Event structure that the Quit AppleEvent handler - * uses to schedule the tkReallyKillMe function. + * uses to schedule the ReallyKillMe function. */ typedef struct KillEvent { - Tcl_Event header; /* Information that is standard for + Tcl_Event header; /* Information that is standard for * all events. */ - Tcl_Interp *interp; /* Interp that was passed to the + Tcl_Interp *interp; /* Interp that was passed to the * Quit AppleEvent */ } KillEvent; @@ -31,17 +31,24 @@ typedef struct KillEvent { * Static functions used only in this file. */ -static OSErr QuitHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); -static OSErr OappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); -static OSErr RappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); -static OSErr OdocHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); -static OSErr PrintHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); -static OSErr ScriptHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); -static OSErr PrefsHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon); - -static int MissedAnyParameters _ANSI_ARGS_((const AppleEvent *theEvent)); -static int ReallyKillMe _ANSI_ARGS_((Tcl_Event *eventPtr, int flags)); -static OSErr FSRefToDString _ANSI_ARGS_((const FSRef *fsref, Tcl_DString *ds)); +static OSErr QuitHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); +static OSErr OappHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); +static OSErr RappHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); +static OSErr OdocHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); +static OSErr PrintHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); +static OSErr ScriptHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); +static OSErr PrefsHandler(const AppleEvent * event, AppleEvent * reply, + long handlerRefcon); + +static int MissedAnyParameters(const AppleEvent *theEvent); +static int ReallyKillMe(Tcl_Event *eventPtr, int flags); +static OSErr FSRefToDString(const FSRef *fsref, Tcl_DString *ds); /* *---------------------------------------------------------------------- @@ -68,38 +75,44 @@ TkMacOSXInitAppleEvents( AEEventHandlerUPP OappHandlerUPP, RappHandlerUPP, OdocHandlerUPP, PrintHandlerUPP, QuitHandlerUPP, ScriptHandlerUPP, PrefsHandlerUPP; + static Boolean initialized = FALSE; + + if (!initialized) { + initialized = TRUE; + + /* + * Install event handlers for the core apple events. + */ + QuitHandlerUPP = NewAEEventHandlerUPP(QuitHandler); + err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, + QuitHandlerUPP, (long) interp, false); + + OappHandlerUPP = NewAEEventHandlerUPP(OappHandler); + err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, + OappHandlerUPP, (long) interp, false); + + RappHandlerUPP = NewAEEventHandlerUPP(RappHandler); + err = AEInstallEventHandler(kCoreEventClass, kAEReopenApplication, + RappHandlerUPP, (long) interp, false); + + OdocHandlerUPP = NewAEEventHandlerUPP(OdocHandler); + err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, + OdocHandlerUPP, (long) interp, false); + + PrintHandlerUPP = NewAEEventHandlerUPP(PrintHandler); + err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, + PrintHandlerUPP, (long) interp, false); + + PrefsHandlerUPP = NewAEEventHandlerUPP(PrefsHandler); + err = AEInstallEventHandler(kCoreEventClass, kAEShowPreferences, + PrefsHandlerUPP, (long) interp, false); + + if (interp) { + ScriptHandlerUPP = NewAEEventHandlerUPP(ScriptHandler); + err = AEInstallEventHandler(kAEMiscStandards, kAEDoScript, + ScriptHandlerUPP, (long) interp, false); + } - /* - * Install event handlers for the core apple events. - */ - QuitHandlerUPP = NewAEEventHandlerUPP(QuitHandler); - err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, - QuitHandlerUPP, (long) interp, false); - - OappHandlerUPP = NewAEEventHandlerUPP(OappHandler); - err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, - OappHandlerUPP, (long) interp, false); - - RappHandlerUPP = NewAEEventHandlerUPP(RappHandler); - err = AEInstallEventHandler(kCoreEventClass, kAEReopenApplication, - RappHandlerUPP, (long) interp, false); - - OdocHandlerUPP = NewAEEventHandlerUPP(OdocHandler); - err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, - OdocHandlerUPP, (long) interp, false); - - PrintHandlerUPP = NewAEEventHandlerUPP(PrintHandler); - err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, - PrintHandlerUPP, (long) interp, false); - - PrefsHandlerUPP = NewAEEventHandlerUPP(PrefsHandler); - err = AEInstallEventHandler(kCoreEventClass, kAEShowPreferences, - PrefsHandlerUPP, (long) interp, false); - - if (interp != NULL) { - ScriptHandlerUPP = NewAEEventHandlerUPP(ScriptHandler); - err = AEInstallEventHandler(kAEMiscStandards, kAEDoScript, - ScriptHandlerUPP, (long) interp, false); } } @@ -120,7 +133,8 @@ TkMacOSXInitAppleEvents( */ int -TkMacOSXDoHLEvent(EventRecord *theEvent) +TkMacOSXDoHLEvent( + EventRecord *theEvent) { return AEProcessAppleEvent(theEvent); } @@ -130,8 +144,7 @@ TkMacOSXDoHLEvent(EventRecord *theEvent) * * QuitHandler, OappHandler, etc. -- * - * These are the core Apple event handlers. Only the Quit event does - * anything interesting. + * These are the core Apple event handlers. * * Results: * None. @@ -141,61 +154,88 @@ TkMacOSXDoHLEvent(EventRecord *theEvent) * *---------------------------------------------------------------------- */ -OSErr QuitHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +static OSErr +QuitHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; KillEvent *eventPtr; - /* - * Call the exit command from the event loop, since you are not supposed - * to call ExitToShell in an Apple Event Handler. We put this at the head - * of Tcl's event queue because this message usually comes when the Mac is - * shutting down, and we want to kill the shell as quickly as possible. - */ - - eventPtr = (KillEvent *) ckalloc(sizeof(KillEvent)); - eventPtr->header.proc = ReallyKillMe; - eventPtr->interp = interp; - - Tcl_QueueEvent((Tcl_Event *) eventPtr, TCL_QUEUE_HEAD); - + if (interp) { + /* + * Call the exit command from the event loop, since you are not supposed + * to call ExitToShell in an Apple Event Handler. We put this at the head + * of Tcl's event queue because this message usually comes when the Mac is + * shutting down, and we want to kill the shell as quickly as possible. + */ + + eventPtr = (KillEvent *) ckalloc(sizeof(KillEvent)); + eventPtr->header.proc = ReallyKillMe; + eventPtr->interp = interp; + + Tcl_QueueEvent((Tcl_Event *) eventPtr, TCL_QUEUE_HEAD); + } return noErr; } - + static OSErr -OappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +OappHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { + Tcl_CmdInfo dummy; + Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; + + if (interp && + Tcl_GetCommandInfo(interp, "::tk::mac::OpenApplication", &dummy)) { + Tcl_GlobalEval(interp, "::tk::mac::OpenApplication"); + } return noErr; } - + static OSErr -RappHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +RappHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { + Tcl_CmdInfo dummy; + Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; ProcessSerialNumber thePSN = {0, kCurrentProcess}; - return SetFrontProcess(&thePSN); + OSStatus status = SetFrontProcess(&thePSN); + + if (interp && + Tcl_GetCommandInfo(interp, "::tk::mac::ReopenApplication", &dummy)) { + Tcl_GlobalEval(interp, "::tk::mac::ReopenApplication"); + } + return status; } - + /* Called when the user selects 'Preferences...' in MacOS X */ static OSErr -PrefsHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +PrefsHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { Tcl_CmdInfo dummy; Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; - /* - * Don't bother if we don't have an interp or - * the show preferences procedure doesn't exist. - */ - if ((interp == NULL) || - (Tcl_GetCommandInfo(interp, "::tk::mac::ShowPreferences", &dummy)) == 0) { - return noErr; + if (interp && + Tcl_GetCommandInfo(interp, "::tk::mac::ShowPreferences", &dummy)) { + Tcl_GlobalEval(interp, "::tk::mac::ShowPreferences"); } - Tcl_GlobalEval(interp, "::tk::mac::ShowPreferences"); return noErr; } - + static OSErr -OdocHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +OdocHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; AEDescList fileSpecList; @@ -262,10 +302,76 @@ OdocHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) Tcl_DStringFree(&command); return noErr; } - + static OSErr -PrintHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +PrintHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { + Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; + AEDescList fileSpecList; + FSRef file; + OSErr err; + DescType type; + Size actual; + long count; + AEKeyword keyword; + long index; + Tcl_DString command; + Tcl_DString pathName; + Tcl_CmdInfo dummy; + + /* + * Don't bother if we don't have an interp or + * the print document procedure doesn't exist. + */ + + if ((interp == NULL) || + (Tcl_GetCommandInfo(interp, "::tk::mac::PrintDocument", &dummy)) == 0) { + return noErr; + } + + /* + * If we get any errors wil retrieving our parameters + * we just return with no error. + */ + + err = AEGetParamDesc(event, keyDirectObject, + typeAEList, &fileSpecList); + if (err != noErr) { + return noErr; + } + + err = MissedAnyParameters(event); + if (err != noErr) { + return noErr; + } + + err = AECountItems(&fileSpecList, &count); + if (err != noErr) { + return noErr; + } + + Tcl_DStringInit(&command); + Tcl_DStringAppend(&command, "::tk::mac::PrintDocument", -1); + for (index = 1; index <= count; index++) { + err = AEGetNthPtr(&fileSpecList, index, typeFSRef, + &keyword, &type, (Ptr) &file, sizeof(FSRef), &actual); + if ( err != noErr ) { + continue; + } + + err = FSRefToDString(&file, &pathName); + if (err == noErr) { + Tcl_DStringAppendElement(&command, Tcl_DStringValue(&pathName)); + Tcl_DStringFree(&pathName); + } + } + + Tcl_GlobalEval(interp, Tcl_DStringValue(&command)); + + Tcl_DStringFree(&command); return noErr; } @@ -286,7 +392,10 @@ PrintHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) */ static OSErr -ScriptHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) +ScriptHandler ( + const AppleEvent * event, + AppleEvent * reply, + long handlerRefcon) { OSErr theErr; AEDescList theDesc; @@ -415,13 +524,17 @@ ScriptHandler (const AppleEvent * event, AppleEvent * reply, long handlerRefcon) */ static int -ReallyKillMe(Tcl_Event *eventPtr, int flags) +ReallyKillMe( + Tcl_Event *eventPtr, + int flags) { Tcl_Interp *interp = ((KillEvent *) eventPtr)->interp; - if (interp != NULL) { - Tcl_GlobalEval(interp, "exit"); + Tcl_CmdInfo dummy; + if (Tcl_GetCommandInfo(interp, "::tk::mac::Quit", &dummy)) { + Tcl_GlobalEval(interp, "::tk::mac::Quit"); + } else { + Tcl_GlobalEval(interp, "exit"); } - return 1; } @@ -472,7 +585,9 @@ MissedAnyParameters( */ static OSErr -FSRefToDString(const FSRef *fsref, Tcl_DString *ds) +FSRefToDString( + const FSRef *fsref, + Tcl_DString *ds) { UInt8 fileName[PATH_MAX+1]; OSErr err; diff --git a/macosx/tkMacOSXWindowEvent.c b/macosx/tkMacOSXWindowEvent.c index 42f1041..d34bcf1 100644 --- a/macosx/tkMacOSXWindowEvent.c +++ b/macosx/tkMacOSXWindowEvent.c @@ -54,7 +54,7 @@ * software in accordance with the terms specified in this * license. * - * RCS: @(#) $Id: tkMacOSXWindowEvent.c,v 1.3.2.10 2006/04/11 07:36:40 das Exp $ + * RCS: @(#) $Id: tkMacOSXWindowEvent.c,v 1.3.2.11 2006/04/11 10:21:53 das Exp $ */ #include "tkMacOSXInt.h" @@ -128,28 +128,22 @@ TkMacOSXProcessApplicationEvent( statusPtr->stopProcessing = 1; break; case kEventAppHidden: - /* - * Don't bother if we don't have an interp or - * the show preferences procedure doesn't exist. - */ - toggleHide = 1; - if ((eventPtr->interp == NULL) || - (Tcl_GetCommandInfo(eventPtr->interp, - "::tk::mac::OnHide", &dummy)) == 0) { - break; + if (toggleHide == 0) { + toggleHide = 1; + if (eventPtr->interp && Tcl_GetCommandInfo(eventPtr->interp, + "::tk::mac::OnHide", &dummy)) { + Tcl_GlobalEval(eventPtr->interp, "::tk::mac::OnHide"); + } } - Tcl_GlobalEval(eventPtr->interp, "::tk::mac::OnHide"); statusPtr->stopProcessing = 1; break; case kEventAppShown: if (toggleHide == 1) { toggleHide = 0; - if ((eventPtr->interp == NULL) || - (Tcl_GetCommandInfo(eventPtr->interp, - "::tk::mac::OnShow", &dummy)) == 0) { - break; + if (eventPtr->interp && Tcl_GetCommandInfo(eventPtr->interp, + "::tk::mac::OnShow", &dummy)) { + Tcl_GlobalEval(eventPtr->interp, "::tk::mac::OnShow"); } - Tcl_GlobalEval(eventPtr->interp, "::tk::mac::OnShow"); } statusPtr->stopProcessing = 1; break; |