[qwaq] Move onEvent into View

In the end, it did make sense since it seems to facilitate MVC nicely.
This commit is contained in:
Bill Currie 2020-03-26 17:28:38 +09:00
parent 91c5283af5
commit 90d89e8874
5 changed files with 37 additions and 27 deletions

View file

@ -21,10 +21,8 @@
Point cursor; Point cursor;
unsigned line_count; unsigned line_count;
string filename; string filename;
ListenerGroup *onEvent;
} }
-initWithRect:(Rect) rect file:(string) filename; -initWithRect:(Rect) rect file:(string) filename;
-(ListenerGroup *)onEvent;
-(string)filename; -(string)filename;
-scrollUp:(unsigned) count; -scrollUp:(unsigned) count;
-scrollDown:(unsigned) count; -scrollDown:(unsigned) count;

View file

@ -15,16 +15,11 @@
line_count = [buffer countLines: {0, [buffer textSize]}]; line_count = [buffer countLines: {0, [buffer textSize]}];
linebuffer = [DrawBuffer buffer: { xlen, 1 }]; linebuffer = [DrawBuffer buffer: { xlen, 1 }];
growMode = gfGrowHi; growMode = gfGrowHi;
options = ofCanFocus; options = ofCanFocus | ofRelativeEvents;
onEvent = [[ListenerGroup alloc] init]; onEvent = [[ListenerGroup alloc] init];
return self; return self;
} }
-(ListenerGroup *)onEvent
{
return onEvent;
}
-(string)filename -(string)filename
{ {
return filename; return filename;
@ -90,19 +85,9 @@ static int handleEvent (Editor *self, qwaq_event_t *event)
{ {
[super handleEvent: event]; [super handleEvent: event];
// give any listeners a chance to override or extend event handling
if (event.what & qe_positional) {
event.mouse.x -= xpos;
event.mouse.y -= ypos;
}
[onEvent respond:self withObject:event];
if (handleEvent (self, event)) { if (handleEvent (self, event)) {
event.what = qe_none; event.what = qe_none;
} }
if (event.what & qe_positional) {
event.mouse.x += xpos;
event.mouse.y += ypos;
}
return self; return self;
} }

View file

@ -245,6 +245,25 @@ find_mouse_view(Group *group, Point pos)
return nil; return nil;
} }
static void
handlePositionalEvent (qwaq_event_t *event, View *view)
{
Point pos = [view origin];
int options = [view options];
if (options & ofRelativeEvents) {
event.mouse.x -= pos.x;
event.mouse.y -= pos.y;
}
[view handleEvent: event];
if (options & ofRelativeEvents) {
event.mouse.x += pos.x;
event.mouse.y += pos.y;
}
}
-handleEvent: (qwaq_event_t *) event -handleEvent: (qwaq_event_t *) event
{ {
if (event.what & qe_focused) { if (event.what & qe_focused) {
@ -253,7 +272,7 @@ find_mouse_view(Group *group, Point pos)
} }
} else if (event.what & qe_positional) { } else if (event.what & qe_positional) {
if (mouse_grabbed) { if (mouse_grabbed) {
[mouse_grabbed handleEvent: event]; handlePositionalEvent (event, mouse_grabbed);
} else { } else {
Point pos = {event.mouse.x, event.mouse.y}; Point pos = {event.mouse.x, event.mouse.y};
View *mouse_view = find_mouse_view (self, pos); View *mouse_view = find_mouse_view (self, pos);
@ -263,7 +282,7 @@ find_mouse_view(Group *group, Point pos)
mouse_within = mouse_view; mouse_within = mouse_view;
} }
if (mouse_within) { if (mouse_within) {
[mouse_within handleEvent: event]; handlePositionalEvent (event, mouse_within);
} }
} }
} else { } else {

View file

@ -14,12 +14,12 @@
enum { enum {
ofCanFocus = 0x0001, ofCanFocus = 0x0001,
ofFirstClick = 0x0002, ofFirstClick = 0x0002,
ofDontDraw = 0x0004, ofMakeFirst = 0x0004,
ofPreProcess = 0x0008, ofDontDraw = 0x0008,
ofPostProcess = 0x0010, ofRelativeEvents= 0x0010,
ofMakeFirst = 0x0020,
ofTileable = 0x0040, ofTileable = 0x0020,
ofCentered = 0x0080, ofCentered = 0x0040,
}; };
enum { enum {
@ -68,6 +68,7 @@ enum {
-resize: (Extent) delta; -resize: (Extent) delta;
-move:(Point)dpos andResize:(Extent)dsize; -move:(Point)dpos andResize:(Extent)dsize;
-grow: (Extent) delta; -grow: (Extent) delta;
-(ListenerGroup *)onEvent;
-handleEvent: (qwaq_event_t *) event; -handleEvent: (qwaq_event_t *) event;
-takeFocus; -takeFocus;
-loseFocus; -loseFocus;
@ -100,7 +101,6 @@ enum {
}; };
}; };
Rect absRect; Rect absRect;
Point point; // can't be local :(
Group *owner; Group *owner;
id<TextContext> textContext; id<TextContext> textContext;
int state; int state;
@ -110,6 +110,7 @@ enum {
Point cursor; Point cursor;
ListenerGroup *onReceiveFocus; ListenerGroup *onReceiveFocus;
ListenerGroup *onReleaseFocus; ListenerGroup *onReleaseFocus;
ListenerGroup *onEvent;
} }
-initWithRect: (Rect) rect; -initWithRect: (Rect) rect;
@end @end

View file

@ -259,8 +259,15 @@ updateScreenCursor (View *view)
return self; return self;
} }
-(ListenerGroup *)onEvent
{
return onEvent;
}
-handleEvent: (qwaq_event_t *) event -handleEvent: (qwaq_event_t *) event
{ {
// give any listeners a chance to override or extend event handling
[onEvent respond:self withObject:event];
if (event.what & (qe_mousedown | qe_mouseclick) if (event.what & (qe_mousedown | qe_mouseclick)
&& options & ofCanFocus && !(state & (sfDisabled | sfInFocus))) { && options & ofCanFocus && !(state & (sfDisabled | sfInFocus))) {
[owner selectView: self]; [owner selectView: self];