From e58d97f480f8215ff0b68b775edfaa5696ebb6da Mon Sep 17 00:00:00 2001 From: jmiltner Date: Wed, 23 Mar 2005 14:53:31 +0000 Subject: [PATCH] added AppleEventHandler for "open document" events (Mac OS) to allow dropping of documents onto application to open them --- sqlitebrowser/sqlitebrowser/browsermain.cpp | 72 ++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/sqlitebrowser/sqlitebrowser/browsermain.cpp b/sqlitebrowser/sqlitebrowser/browsermain.cpp index cac801bb..8ea6dca8 100755 --- a/sqlitebrowser/sqlitebrowser/browsermain.cpp +++ b/sqlitebrowser/sqlitebrowser/browsermain.cpp @@ -1,14 +1,84 @@ #include #include "form1.h" +#if defined(Q_WS_MAC) +#include +static OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent) +{ + DescType returnedType; + Size actualSize; + OSErr err; + + switch (err = AEGetAttributePtr(&theAppleEvent, + keyMissedKeywordAttr, typeWildCard, &returnedType, nil, 0, + &actualSize)) { + case errAEDescNotFound: // If we couldnŐt find the error attribute + return noErr; // everything is ok, return noErr + case noErr: // We found an error attribute, so + return errAEEventNotHandled; // tell the client we ignored the event + default: + return err; // Something else happened, return it + } +} + +static pascal OSErr odocHandler(const AppleEvent* inEvent, AppleEvent* + /*reply*/, long refCon) +{ + AEDescList documentList; + OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList, + &documentList); + if (err == noErr) { + err = checkAppleEventForMissingParams(*inEvent); + + if (err == noErr) { + long documentCount; + err = AECountItems(&documentList, &documentCount); + + for (long documentIndex = 1; err == noErr && documentIndex <= + documentCount; documentIndex++) { + // What kind of document is it? + DescType returnedType; + Size actualSize; + err = AESizeOfNthItem(&documentList, documentIndex, &returnedType, + &actualSize); + if (err == noErr) { + + // It's just a normal document file + AEKeyword keyword; + FSRef ref; + err = AEGetNthPtr(&documentList, documentIndex, typeFSRef, + &keyword, &returnedType, (Ptr)&ref, sizeof(FSRef), &actualSize); + + if (err == noErr) { + char buf[1024]; + err = FSRefMakePath(&ref, reinterpret_cast(buf), 1024); + if ( err == noErr ) + reinterpret_cast(refCon)->fileOpen(QString::fromUtf8(buf)); + } + } + } + } + AEDisposeDesc(&documentList); + } + return err; +} + +#endif // Q_WS_MAC + + + int main( int argc, char ** argv ) { QApplication a( argc, argv ); mainForm w; +#if defined(Q_WS_MAC) + AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, + NewAEEventHandlerUPP(odocHandler),reinterpret_cast(&w),false); +#endif // Q_WS_MAC w.show(); if (argc>1) { //first and only argument we accept is the name of the database to open - w.fileOpen(QString(argv[1])); + w.fileOpen(QString(argv[1])); } a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) ); return a.exec();