Here's some window-level event code, perhaps it will help. You'll also
need a main event loop where you first call
InstallApplicationEventHandler and then RunApplicationEventLoop.
void createWindow() {
WindowAttributes windowAttrs;
OSStatus err;
windowAttrs = kWindowCloseBoxAttribute |
kWindowCollapseBoxAttribute |
kWindowStandardHandlerAttribute;
err = CreateNewWindow (kDocumentWindowClass, windowAttrs, &border, &window);
assert(err == noErr, "Error creating window");
// Event handling
InstallStandardEventHandler(GetWindowEventTarget(window));
EventHandlerUPP handlerUPP;
EventTypeSpec eventTypes[] = {
{ kEventClassWindow,
kEventWindowDrawContent },
{ kEventClassWindow,
kEventWindowUpdate },
{ kEventClassWindow,
kEventWindowShown },
{ kEventClassWindow,
kEventWindowClose}
};
handlerUPP = NewEventHandlerUPP(windowHandleEventGlue);
InstallWindowEventHandler(window, handlerUPP, sizeof(eventTypes) /
sizeof(EventTypeSpec), eventTypes, (void *)this, null);
Quote:}
pascal OSStatus windowHandleEventGlue(EventHandlerCallRef nextHandler,
EventRef theEvent, void *userData) {
OSStatus status = eventNotHandledErr;
UInt32 eventClass = GetEventClass(theEvent);
UInt32 eventKind = GetEventKind(theEvent);
switch (eventClass) {
case kEventClassWindow:
switch (eventKind) {
case kEventWindowClose:
case kEventWindowDrawContent:
case kEventWindowUpdate:
case kEventWindowShown:
// Do something here
status = noErr;
break;
}
break;
default:
break;
}
return status;
Quote:}