[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;
unsigned line_count;
string filename;
ListenerGroup *onEvent;
}
-initWithRect:(Rect) rect file:(string) filename;
-(ListenerGroup *)onEvent;
-(string)filename;
-scrollUp:(unsigned) count;
-scrollDown:(unsigned) count;

View file

@ -15,16 +15,11 @@
line_count = [buffer countLines: {0, [buffer textSize]}];
linebuffer = [DrawBuffer buffer: { xlen, 1 }];
growMode = gfGrowHi;
options = ofCanFocus;
options = ofCanFocus | ofRelativeEvents;
onEvent = [[ListenerGroup alloc] init];
return self;
}
-(ListenerGroup *)onEvent
{
return onEvent;
}
-(string)filename
{
return filename;
@ -90,19 +85,9 @@ static int handleEvent (Editor *self, qwaq_event_t *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)) {
event.what = qe_none;
}
if (event.what & qe_positional) {
event.mouse.x += xpos;
event.mouse.y += ypos;
}
return self;
}

View file

@ -245,6 +245,25 @@ find_mouse_view(Group *group, Point pos)
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
{
if (event.what & qe_focused) {
@ -253,7 +272,7 @@ find_mouse_view(Group *group, Point pos)
}
} else if (event.what & qe_positional) {
if (mouse_grabbed) {
[mouse_grabbed handleEvent: event];
handlePositionalEvent (event, mouse_grabbed);
} else {
Point pos = {event.mouse.x, event.mouse.y};
View *mouse_view = find_mouse_view (self, pos);
@ -263,7 +282,7 @@ find_mouse_view(Group *group, Point pos)
mouse_within = mouse_view;
}
if (mouse_within) {
[mouse_within handleEvent: event];
handlePositionalEvent (event, mouse_within);
}
}
} else {

View file

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

View file

@ -259,8 +259,15 @@ updateScreenCursor (View *view)
return self;
}
-(ListenerGroup *)onEvent
{
return onEvent;
}
-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)
&& options & ofCanFocus && !(state & (sfDisabled | sfInFocus))) {
[owner selectView: self];