home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
PRG
/
AESample.sit
/
AESample
/
sources
/
sample.AE.c
< prev
next >
Wrap
Text File
|
1996-06-22
|
10KB
|
340 lines
/*
*--------------------------------------------------------------
* sample.AE.c
*--------------------------------------------------------------
*/
#include <Gestalt.h>
#include <AppleEvents.h>
#include <AERegistry.h>
#include "sample.h"
#include "sample.AE.h"
#include "sample.send.h"
#include "sample.alrt.h"
enum AEResourceIDs {
dlgAEErrorID = 2002,
strAEErrorID = dlgAEErrorID
};
/* AppleEvent error kind messages */
enum AEErrKinds {
/* message kinds */
iAEOpenApplication = 1,
iAEOpenDocuments,
iAEPrintDocuments,
iAEQuitApplication,
iAESendOpenSelected,
iAESendPrintSelected,
iAEUnknownEvents,
/* error codes */
iAENotInstalled,
iAEInvalidConnect,
iAENotHandled,
iAEParameterErr,
iAEMemoryFull,
iAENotCoercion,
iAEWrongDataType,
iAEDecsNotFound,
iAEInvalDesc,
iAENoReplyComes,
iAETimeoutOccur,
iAEUnknownError,
errAENotInstalled = dlgAEErrorID
};
/* number of required AppleEvents */
enum {
kOpenApp = 0,
kOpenDoc,
kPrntDoc,
kQuitApp,
kNumOfAE
};
/* static functions only used in this source */
static pascal OSErr DoAEOpenApp(const AppleEvent *, AppleEvent *, long);
static pascal OSErr DoAEOpenDoc(const AppleEvent *, AppleEvent *, long);
static pascal OSErr DoAEPrintDoc(const AppleEvent *, AppleEvent *, long);
static pascal OSErr DoAEQuitApp(const AppleEvent *, AppleEvent *, long);
static OSErr GotRequiredParams(const AppleEvent *);
/* globals used for AppleEvent handling */
Boolean gAEDoubleClicked;
Boolean gAEDragAndDropped;
/* static global: AppleEvent Hander ProcPtr */
static AEEventHandlerUPP gMyAEUPP[kNumOfAE] = { nil, nil, nil, nil };
/*
*--------------------------------------------------------------
* SetUpMyAppleEvent
*--------------------------------------------------------------
* set up Apple Event procedure
* if AppleEvent is not supported, do nothing in here
*--------------------------------------------------------------
*/
Boolean SetUpMyAppleEvent(void)
{
AEEventID anAEID;
long answer;
OSErr result;
/* set startup flag */
gAEDoubleClicked = gAEDragAndDropped = false;
/* check gestalt whether AppleEvent is available */
result = Gestalt(gestaltAppleEventsAttr, &answer);
if (result != noErr || answer < gestaltAppleEventsPresent) {
anAEID = iAEUnknownEvents;
result = iAENotInstalled;
}
/* create new routine descriptors of my AppleEvent handlers */
if (result == noErr) {
gMyAEUPP[kOpenApp] = NewAEEventHandlerProc(DoAEOpenApp);
gMyAEUPP[kOpenDoc] = NewAEEventHandlerProc(DoAEOpenDoc);
gMyAEUPP[kPrntDoc] = NewAEEventHandlerProc(DoAEPrintDoc);
gMyAEUPP[kQuitApp] = NewAEEventHandlerProc(DoAEQuitApp);
}
/* istall AppleEvent handlers */
if (result == noErr) {
result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEOpenApplication,
gMyAEUPP[kOpenApp], 0, false);
}
if (result == noErr) {
result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEOpenDocuments,
gMyAEUPP[kOpenDoc], 0, false);
}
if (result == noErr) {
result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEPrintDocuments,
gMyAEUPP[kPrntDoc], 0, false);
}
if (result == noErr) {
result = AEInstallEventHandler(kCoreEventClass, anAEID = kAEQuitApplication,
gMyAEUPP[kQuitApp], 0, false);
}
if (result != noErr) {
/* if failed, show alert */
DoAEError(result, anAEID);
return (false);
}
return (true);
}
/*
*--------------------------------------------------------------
* RemoveMyAppleEvent
*--------------------------------------------------------------
* delete AppleEvent handlers
*--------------------------------------------------------------
*/
void RemoveMyAppleEvent(void)
{
int i; /* natural integer */
/* remove my AppleEvent handlers */
for (i = 0; i < kNumOfAE; ++i) {
if (gMyAEUPP[i] != nil) {
DisposeRoutineDescriptor(gMyAEUPP[i]);
}
}
}
/*
*--------------------------------------------------------------
* DoOpenApp
*--------------------------------------------------------------
* AppleEvent Open Application procedure: do nothing
*--------------------------------------------------------------
*/
pascal OSErr DoAEOpenApp(const AppleEvent *theAE, AppleEvent *reply, long refCon)
{
#pragma unused (theAE, reply, refCon)
gAEDoubleClicked = true;
return (noErr);
}
/*
*--------------------------------------------------------------
* DoOpenDoc
*--------------------------------------------------------------
* AppleEvent Open Document procedure
*--------------------------------------------------------------
*/
pascal OSErr DoAEOpenDoc(const AppleEvent *theAE, AppleEvent *reply, long refCon)
{
#pragma unused (theAE, reply, refCon)
AEDescList fileSpecList = { typeNull, nil };
FSSpec thisFileSpec;
DescType type;
AEKeyword keyword;
Size actual;
long count;
long index;
OSErr result;
gAEDragAndDropped = true;
/* pick up file list from the received AppleEvent */
result = AEGetParamDesc(theAE, keyDirectObject, typeAEList, &fileSpecList);
if (result == noErr) {
result = GotRequiredParams(theAE);
}
/* get number of files */
if (result == noErr) {
result = AECountItems(&fileSpecList, &count);
}
/* pick up all the files (FSSPec) in the received AppleEvent */
if (result == noErr) {
for (index = 1; index <= count; index++) {
result = AEGetNthPtr(&fileSpecList, index, typeFSS, &keyword, &type,
(Ptr)&thisFileSpec, sizeof(FSSpec), &actual);
if (result == noErr) {
/* This is the heart! */
OpenDroppedFile(&thisFileSpec);
} else {
break;
}
}
}
AEDisposeDesc(&fileSpecList);
if (result != noErr) {
DoAEError(result, kAEOpenDocuments);
}
return (result);
}
/*
*--------------------------------------------------------------
* DoPrintDoc
*--------------------------------------------------------------
* AppleEvent Quit procedure
*--------------------------------------------------------------
*/
pascal OSErr DoAEPrintDoc(const AppleEvent *theAE, AppleEvent *reply, long refCon)
{
#pragma unused (reply, refCon)
AEDescList fileSpecList = { typeNull, nil };
FSSpec thisFileSpec;
DescType type;
AEKeyword keyword;
Size actual;
long count;
long index;
OSErr result;
gAEDragAndDropped = true;
/* pick up file list from the received AppleEvent */
result = AEGetParamDesc(theAE, keyDirectObject, typeAEList, &fileSpecList);
if (result == noErr) {
result = GotRequiredParams(theAE);
}
/* get number of files */
if (result == noErr) {
result = AECountItems(&fileSpecList, &count);
}
/* pick up all the files (FSSPec) in the received AppleEvent */
if (result == noErr) {
for (index = 1; index <= count; index++) {
result = AEGetNthPtr(&fileSpecList, index, typeFSS, &keyword, &type,
(Ptr)&thisFileSpec, sizeof(FSSpec), &actual);
if (result == noErr) {
/* This is the heart! */
PrintDroppedFile(&thisFileSpec);
} else {
break;
}
}
}
AEDisposeDesc(&fileSpecList);
if (result != noErr) {
DoAEError(result, kAEPrintDocuments);
}
return (result);
}
/*
*--------------------------------------------------------------
* DoQuitApp
*--------------------------------------------------------------
* Apple Event Quit procedure: clear main event loop flag
*--------------------------------------------------------------
*/
pascal OSErr DoAEQuitApp(const AppleEvent *theAE, AppleEvent *reply, long refCon)
{
#pragma unused (theAE, reply, refCon)
gForever = false;
return (noErr);
}
/*
*--------------------------------------------------------------
* GotRequiredParams
*--------------------------------------------------------------
* Apple Event requred parameter parser
*--------------------------------------------------------------
*/
static OSErr GotRequiredParams(const AppleEvent *theAE)
{
DescType returnedType;
Size actualSize;
OSErr result;
result = AEGetAttributePtr(theAE, keyMissedKeywordAttr, typeWildCard,
&returnedType, nil, 0, &actualSize );
if (result == errAEDescNotFound) {
return (noErr);
} else if (result == noErr) {
return (errAEEventNotHandled);
}
return (result);
}
/*
*--------------------------------------------------------------
* DoAEError
*--------------------------------------------------------------
* Apple Event Error Messages
*--------------------------------------------------------------
*/
void DoAEError(const OSErr theError, const AEEventID theKind)
{
Str63 errKinds;
Str63 errCodes;
short kind;
short code;
switch (theKind) {
case kAEOpenApplication: kind = iAEOpenApplication; break;
case kAEOpenDocuments: kind = iAEOpenDocuments; break;
case kAEPrintDocuments: kind = iAEPrintDocuments; break;
case kAEQuitApplication: kind = iAEQuitApplication; break;
case kAEOpenSelection: kind = iAESendOpenSelected; break;
case kAEPrintSelection: kind = iAESendPrintSelected; break;
default: kind = iAEUnknownEvents; break;
}
switch (theError) {
case errAENotInstalled: code = iAENotInstalled; break;
case connectionInvalid: code = iAEInvalidConnect; break;
case errAEEventNotHandled: code = iAENotHandled; break;
case paramErr: code = iAEParameterErr; break;
case memFullErr: code = iAEMemoryFull; break;
case errAECoercionFail: code = iAENotCoercion; break;
case errAEWrongDataType: code = iAEWrongDataType; break;
case errAEDescNotFound: code = iAEDecsNotFound; break;
case errAENotAEDesc: code = iAEDecsNotFound; break;
case errAEReplyNotValid: code = iAENoReplyComes; break;
case errAETimeout: code = iAETimeoutOccur; break;
default: code = iAEUnknownError; break;
}
GetIndString(errKinds, strAEErrorID, kind);
GetIndString(errCodes, strAEErrorID, code);
ParamText(errKinds, errCodes, nil, nil);
MyAlert(dlgAEErrorID);
}