[qwaq] Add Listener and ListenerGroup delegates

Simple single parameter (caller) direct messages.
This commit is contained in:
Bill Currie 2020-03-19 13:49:27 +09:00
parent 01835cbd94
commit 8c3cf83f02
3 changed files with 96 additions and 0 deletions

View file

@ -28,6 +28,7 @@ qwaq_app_dat_src= \
qwaq-draw.r \
qwaq-garray.r \
qwaq-group.r \
qwaq-listener.r \
qwaq-rect.r \
qwaq-screen.r \
qwaq-textcontext.r \

View file

@ -0,0 +1,29 @@
#ifndef __qwaq_listener_h
#define __qwaq_listener_h
#include <Object.h>
@class Array;
@interface Listener : Object
{
id responder;
SEL message;
IMP imp;
}
-initWithResponder: (id) responder message: (SEL) message;
-(void) respond: (id) caller;
-(BOOL) matchResponder: (id) responder message: (SEL) message;
@end
@interface ListenerGroup : Object
{
Array *listeners;
}
-init;
-addListener: (id) responder message: (SEL) message;
-removeListener: (id) responder message: (SEL) message;
-(void) respond: (id) caller;
@end
#endif//__qwaq_listener_h

View file

@ -0,0 +1,66 @@
#include <Array.h>
#include "qwaq-listener.h"
@class Array;
@implementation Listener
-initWithResponder: (id) responder message: (SEL) message
{
if (!(self = [super init])) {
return nil;
}
self.responder = responder;
self.message = message;
imp = [responder instanceMethodForSelector: message];
return self;
}
-(void)respond: (id) caller
{
imp (responder, message, caller);
}
-(BOOL) matchResponder: (id) responder message: (SEL) message
{
return self.responder == responder && self.message == message;
}
@end
@implementation ListenerGroup : Object
-init
{
if (!(self = [super init])) {
return nil;
}
listeners = [[Array alloc] retain];
return self;
}
-addListener: (id) responder message: (SEL) message
{
Listener *listener = [[Listener alloc] initWithResponder: responder
message: message];
if (listener) {
[listeners addObject: listener];
}
return self;
}
-removeListener: (id) responder message: (SEL) message
{
for (int i = [listeners count]; i-- > 0; ) {
Listener *l = [listeners objectAtIndex: i];
if ([l matchResponder: responder message: message]) {
[listeners removeObjectAtIndex: i];
}
}
return self;
}
-(void)respond: (id) caller
{
[listeners makeObjectsPerformSelector: @selector (respond:)
withObject: caller];
}
@end