diff --git a/ruamoko/qwaq/Makefile.am b/ruamoko/qwaq/Makefile.am index 76b4af136..f0dbe4167 100644 --- a/ruamoko/qwaq/Makefile.am +++ b/ruamoko/qwaq/Makefile.am @@ -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 \ diff --git a/ruamoko/qwaq/qwaq-listener.h b/ruamoko/qwaq/qwaq-listener.h new file mode 100644 index 000000000..bb02d71f1 --- /dev/null +++ b/ruamoko/qwaq/qwaq-listener.h @@ -0,0 +1,29 @@ +#ifndef __qwaq_listener_h +#define __qwaq_listener_h + +#include + +@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 diff --git a/ruamoko/qwaq/qwaq-listener.r b/ruamoko/qwaq/qwaq-listener.r new file mode 100644 index 000000000..5184668fd --- /dev/null +++ b/ruamoko/qwaq/qwaq-listener.r @@ -0,0 +1,66 @@ +#include + +#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