Handle Apple events

Currently, only the 'open application' event is actually handled, but
handlers for the required core events are installed. The open
application event causes the program to create a new document when
opened.
This commit is contained in:
Dietrich Epp 2023-05-07 03:30:51 -04:00
parent 1f1407bb0e
commit 1396fb1cbf

View File

@ -261,8 +261,102 @@ static void HandleUpdate(EventRecord *event)
SetPort(savedPort); SetPort(savedPort);
} }
void main(void) static void HandleHighLevel(EventRecord *event)
{ {
OSErr err;
err = AEProcessAppleEvent(event);
if (err != 0) {
ShowError(kErrNone, kErrNone, err, NULL);
}
}
static OSErr CheckRequiredParams(const AppleEvent *event)
{
OSErr err;
DescType returnedType;
Size size;
err = AEGetAttributePtr(event, keyMissedKeywordAttr, typeWildCard,
&returnedType, NULL, 0, &size);
return err == errAEDescNotFound ? 0 : errAEParamMissed;
}
static pascal OSErr HandleOpenApplication(const AppleEvent *event,
AppleEvent *reply,
unsigned long refCon)
{
OSErr err;
(void)reply;
(void)refCon;
err = CheckRequiredParams(event);
if (err != 0) {
return err;
}
ProjectNew();
return 0;
}
static pascal OSErr HandleOpenDocuments(const AppleEvent *event,
AppleEvent *reply, unsigned long refCon)
{
(void)event;
(void)reply;
(void)refCon;
return 0;
}
static pascal OSErr HandlePrintDocuments(const AppleEvent *event,
AppleEvent *reply,
unsigned long refCon)
{
(void)event;
(void)reply;
(void)refCon;
return 0;
}
static pascal OSErr HandleQuitApplication(const AppleEvent *event,
AppleEvent *reply,
unsigned long refCon)
{
(void)event;
(void)reply;
(void)refCon;
return 0;
}
struct AppleEventHandler {
AEEventID id;
AEEventHandlerProcPtr function;
};
const struct AppleEventHandler kAppleEventHandlers[] = {
{kAEOpenApplication, HandleOpenApplication},
{kAEOpenDocuments, HandleOpenDocuments},
{kAEPrintDocuments, HandlePrintDocuments},
{kAEQuitApplication, HandleQuitApplication},
};
static void InitializeAppleEvents(void)
{
OSErr err;
int i;
for (i = 0; i < ARRAY_COUNT(kAppleEventHandlers); i++) {
err = AEInstallEventHandler(kCoreEventClass, kAppleEventHandlers[i].id,
kAppleEventHandlers[i].function, 0, false);
if (err != 0) {
ShowError(kErrNone, kErrNone, err, NULL);
return;
}
}
}
static void Initialize(void)
{
Handle menuBar;
MaxApplZone(); MaxApplZone();
InitGraf(&qd.thePort); InitGraf(&qd.thePort);
InitFonts(); InitFonts();
@ -273,8 +367,6 @@ void main(void)
InitCursor(); InitCursor();
// Set up menu bar. // Set up menu bar.
{
Handle menuBar;
menuBar = GetNewMBar(rMBAR_Main); menuBar = GetNewMBar(rMBAR_Main);
if (menuBar == NULL) { if (menuBar == NULL) {
EXIT_INTERNAL(); EXIT_INTERNAL();
@ -283,8 +375,14 @@ void main(void)
DisposeHandle(menuBar); DisposeHandle(menuBar);
AppendResMenu(GetMenuHandle(rMENU_Apple), 'DRVR'); AppendResMenu(GetMenuHandle(rMENU_Apple), 'DRVR');
DrawMenuBar(); DrawMenuBar();
InitializeAppleEvents();
} }
void main(void)
{
Initialize();
for (;;) { for (;;) {
EventRecord event; EventRecord event;
WaitNextEvent(everyEvent, &event, 1, NULL); WaitNextEvent(everyEvent, &event, 1, NULL);
@ -311,7 +409,10 @@ void main(void)
// case diskEvt: // case diskEvt:
// osEvt: // osEvt:
// case kHighLevelEvent:
case kHighLevelEvent:
HandleHighLevel(&event);
break;
} }
} }
} }