diff --git a/dgl/src/pugl/pugl_osx.m b/dgl/src/pugl/pugl_osx.m index 1c00106d..4e5ba89c 100644 --- a/dgl/src/pugl/pugl_osx.m +++ b/dgl/src/pugl/pugl_osx.m @@ -99,8 +99,10 @@ puglDisplay(PuglView* view) @protocol PuglGenericView @required -- (PuglView *) puglView; +- (PuglView *) puglview; - (void) setPuglview:(PuglView *)pv; +- (NSTrackingArea *) puglTrackingArea; +- (void) setPuglTrackingArea:(NSTrackingArea *)area; @end static unsigned @@ -129,6 +131,164 @@ getFixedAppKitButton(NSInteger button) } } +static void +cursorUpdate(NSView *self, NSEvent* event) +{ + [[NSCursor arrowCursor] set]; + (void)self; + (void)event; +} + +static void +updateTrackingAreas(NSView *self) +{ + static const int opts = NSTrackingMouseEnteredAndExited + | NSTrackingMouseMoved + | NSTrackingEnabledDuringMouseDrag + | NSTrackingInVisibleRect + | NSTrackingActiveAlways + | NSTrackingCursorUpdate; + + NSTrackingArea *trackingArea = [self puglTrackingArea]; + if (trackingArea != nil) { + [self removeTrackingArea:trackingArea]; + [trackingArea release]; + } + + trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] + options:opts + owner:self + userInfo:nil]; + [self setPuglTrackingArea:trackingArea]; + [self addTrackingArea:trackingArea]; +} + +static void +viewWillMoveToWindow(NSView *self, NSWindow* newWindow) +{ + if (newWindow != nil) { + [newWindow setAcceptsMouseMovedEvents:YES]; + [newWindow makeFirstResponder:self]; + } +} + +static void +reshape(NSView *self) +{ + PuglView* puglview = [self puglview]; + + NSRect bounds = [self bounds]; + int width = bounds.size.width; + int height = bounds.size.height; + + puglEnterContext(puglview); + + if (puglview->reshapeFunc) { + puglview->reshapeFunc(puglview, width, height); + } else { + puglDefaultReshape(width, height); + } + + puglLeaveContext(puglview, false); + + puglview->width = width; + puglview->height = height; +} + +static void +mouseMoved(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->motionFunc) { + NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; + puglview->mods = getModifiers(puglview, event); + puglview->motionFunc(puglview, loc.x, loc.y); + } +} + +static void +mouseDown(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->mouseFunc) { + NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; + puglview->mods = getModifiers(puglview, event); + puglview->mouseFunc(puglview, getFixedAppKitButton([event buttonNumber]), true, loc.x, loc.y); + } +} + +static void +mouseUp(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->mouseFunc) { + NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; + puglview->mods = getModifiers(puglview, event); + puglview->mouseFunc(puglview, getFixedAppKitButton([event buttonNumber]), false, loc.x, loc.y); + } +} + +static void +scrollWheel(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->scrollFunc) { + NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; + puglview->mods = getModifiers(puglview, event); + puglview->scrollFunc(puglview, + loc.x, loc.y, + [event deltaX], [event deltaY]); + } +} + +static void +keyDown(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->keyboardFunc && !(puglview->ignoreKeyRepeat && [event isARepeat])) { + NSString* chars = [event characters]; + puglview->mods = getModifiers(puglview, event); + puglview->keyboardFunc(puglview, true, [chars characterAtIndex:0]); + } +} + +static void +keyUp(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->keyboardFunc) { + NSString* chars = [event characters]; + puglview->mods = getModifiers(puglview, event); + puglview->keyboardFunc(puglview, false, [chars characterAtIndex:0]); + } +} + +static void +flagsChanged(NSView *self, NSEvent *event) +{ + PuglView* puglview = [self puglview]; + + if (puglview->specialFunc) { + const unsigned mods = getModifiers(puglview, event); + if ((mods & PUGL_MOD_SHIFT) != (puglview->mods & PUGL_MOD_SHIFT)) { + puglview->specialFunc(puglview, mods & PUGL_MOD_SHIFT, PUGL_KEY_SHIFT); + } else if ((mods & PUGL_MOD_CTRL) != (puglview->mods & PUGL_MOD_CTRL)) { + puglview->specialFunc(puglview, mods & PUGL_MOD_CTRL, PUGL_KEY_CTRL); + } else if ((mods & PUGL_MOD_ALT) != (puglview->mods & PUGL_MOD_ALT)) { + puglview->specialFunc(puglview, mods & PUGL_MOD_ALT, PUGL_KEY_ALT); + } else if ((mods & PUGL_MOD_SUPER) != (puglview->mods & PUGL_MOD_SUPER)) { + puglview->specialFunc(puglview, mods & PUGL_MOD_SUPER, PUGL_KEY_SUPER); + } + puglview->mods = mods; + } +} + #ifdef PUGL_HAVE_GL @interface PuglOpenGLView : NSOpenGLView { @@ -138,8 +298,10 @@ getFixedAppKitButton(NSInteger button) bool doubleBuffered; } -- (PuglView *) puglView; +- (PuglView *) puglview; - (void) setPuglview:(PuglView *)pv; +- (NSTrackingArea *) puglTrackingArea; +- (void) setPuglTrackingArea:(NSTrackingArea *)area; - (BOOL) acceptsFirstMouse:(NSEvent*)e; - (BOOL) acceptsFirstResponder; @@ -170,7 +332,7 @@ getFixedAppKitButton(NSInteger button) @end @implementation PuglOpenGLView -- (PuglView *) puglView { +- (PuglView *) puglview { return self->puglview; } @@ -178,6 +340,14 @@ getFixedAppKitButton(NSInteger button) self->puglview = pv; } +- (NSTrackingArea *) puglTrackingArea { + return self->trackingArea; +} + +- (void) setPuglTrackingArea:(NSTrackingArea *)area { + self->trackingArea = area; +} + - (BOOL) acceptsFirstMouse:(NSEvent*)e { return YES; @@ -256,22 +426,7 @@ getFixedAppKitButton(NSInteger button) [[self openGLContext] update]; - NSRect bounds = [self bounds]; - int width = bounds.size.width; - int height = bounds.size.height; - - puglEnterContext(puglview); - - if (puglview->reshapeFunc) { - puglview->reshapeFunc(puglview, width, height); - } else { - puglDefaultReshape(width, height); - } - - puglLeaveContext(puglview, false); - - puglview->width = width; - puglview->height = height; + reshape(self); } - (void) drawRect:(NSRect)r @@ -286,150 +441,89 @@ getFixedAppKitButton(NSInteger button) - (void) cursorUpdate:(NSEvent*)e { - [[NSCursor arrowCursor] set]; - - // unused - return; (void)e; + cursorUpdate(self, e); } - (void) updateTrackingAreas { - static const int opts = NSTrackingMouseEnteredAndExited - | NSTrackingMouseMoved - | NSTrackingEnabledDuringMouseDrag - | NSTrackingInVisibleRect - | NSTrackingActiveAlways - | NSTrackingCursorUpdate; - - if (trackingArea != nil) { - [self removeTrackingArea:trackingArea]; - [trackingArea release]; - } - - trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] - options:opts - owner:self - userInfo:nil]; - [self addTrackingArea:trackingArea]; + updateTrackingAreas(self); [super updateTrackingAreas]; } - (void) viewWillMoveToWindow:(NSWindow*)newWindow { - if (newWindow != nil) { - [newWindow setAcceptsMouseMovedEvents:YES]; - [newWindow makeFirstResponder:self]; - } - + viewWillMoveToWindow(self, newWindow); [super viewWillMoveToWindow:newWindow]; } - (void) mouseMoved:(NSEvent*)event { - if (puglview->motionFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->motionFunc(puglview, loc.x, loc.y); - } + mouseMoved(self, event); } - (void) mouseDragged:(NSEvent*)event { - [self mouseMoved:event]; + mouseMoved(self, event); } - (void) rightMouseDragged:(NSEvent*)event { - [self mouseDragged:event]; + mouseMoved(self, event); } - (void) otherMouseDragged:(NSEvent*)event { - [self mouseDragged:event]; + mouseMoved(self, event); } - (void) mouseDown:(NSEvent*)event { - if (puglview->mouseFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->mouseFunc(puglview, getFixedAppKitButton([event buttonNumber]), true, loc.x, loc.y); - } + mouseDown(self, event); } - (void) rightMouseDown:(NSEvent*)event { - [self mouseDown:event]; + mouseDown(self, event); } - (void) otherMouseDown:(NSEvent*)event { - [self mouseDown:event]; + mouseDown(self, event); } - (void) mouseUp:(NSEvent*)event { - if (puglview->mouseFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->mouseFunc(puglview, getFixedAppKitButton([event buttonNumber]), false, loc.x, loc.y); - } + mouseUp(self, event); } - (void) rightMouseUp:(NSEvent*)event { - [self mouseUp:event]; + mouseUp(self, event); } - (void) otherMouseUp:(NSEvent*)event { - [self mouseUp:event]; + mouseUp(self, event); } - (void) scrollWheel:(NSEvent*)event { - if (puglview->scrollFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->scrollFunc(puglview, - loc.x, loc.y, - [event deltaX], [event deltaY]); - } + scrollWheel(self, event); } - (void) keyDown:(NSEvent*)event { - if (puglview->keyboardFunc && !(puglview->ignoreKeyRepeat && [event isARepeat])) { - NSString* chars = [event characters]; - puglview->mods = getModifiers(puglview, event); - puglview->keyboardFunc(puglview, true, [chars characterAtIndex:0]); - } + keyDown(self, event); } - (void) keyUp:(NSEvent*)event { - if (puglview->keyboardFunc) { - NSString* chars = [event characters]; - puglview->mods = getModifiers(puglview, event); - puglview->keyboardFunc(puglview, false, [chars characterAtIndex:0]); - } + keyUp(self, event); } - (void) flagsChanged:(NSEvent*)event { - if (puglview->specialFunc) { - const unsigned mods = getModifiers(puglview, event); - if ((mods & PUGL_MOD_SHIFT) != (puglview->mods & PUGL_MOD_SHIFT)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_SHIFT, PUGL_KEY_SHIFT); - } else if ((mods & PUGL_MOD_CTRL) != (puglview->mods & PUGL_MOD_CTRL)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_CTRL, PUGL_KEY_CTRL); - } else if ((mods & PUGL_MOD_ALT) != (puglview->mods & PUGL_MOD_ALT)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_ALT, PUGL_KEY_ALT); - } else if ((mods & PUGL_MOD_SUPER) != (puglview->mods & PUGL_MOD_SUPER)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_SUPER, PUGL_KEY_SUPER); - } - puglview->mods = mods; - } + flagsChanged(self, event); } @end @@ -443,8 +537,10 @@ getFixedAppKitButton(NSInteger button) NSTrackingArea* trackingArea; } -- (PuglView *) puglView; +- (PuglView *) puglview; - (void) setPuglview:(PuglView *)pv; +- (NSTrackingArea *) puglTrackingArea; +- (void) setPuglTrackingArea:(NSTrackingArea *)area; - (cairo_t *) cairoContext; @@ -456,7 +552,6 @@ getFixedAppKitButton(NSInteger button) - (id) initWithFrame:(NSRect)frame; - (void) reshape; - (void) drawRect:(NSRect)r; -/* TODO: duplication of code from PuglOpenGLView */ - (void) cursorUpdate:(NSEvent*)e; - (void) updateTrackingAreas; - (void) viewWillMoveToWindow:(NSWindow*)newWindow; @@ -477,7 +572,7 @@ getFixedAppKitButton(NSInteger button) @end @implementation PuglCairoView -- (PuglView *) puglView { +- (PuglView *) puglview { return self->puglview; } @@ -485,6 +580,14 @@ getFixedAppKitButton(NSInteger button) self->puglview = pv; } +- (NSTrackingArea *) puglTrackingArea { + return self->trackingArea; +} + +- (void) setPuglTrackingArea:(NSTrackingArea *)area { + self->trackingArea = area; +} + - (cairo_t *) cairoContext { return cr; } @@ -535,22 +638,7 @@ getFixedAppKitButton(NSInteger button) return; } - NSRect bounds = [self bounds]; - int width = bounds.size.width; - int height = bounds.size.height; - - puglEnterContext(puglview); - - if (puglview->reshapeFunc) { - puglview->reshapeFunc(puglview, width, height); - } else { - puglDefaultReshape(width, height); - } - - puglLeaveContext(puglview, false); - - puglview->width = width; - puglview->height = height; + reshape(self); } - (void) drawRect:(NSRect)r { @@ -576,150 +664,89 @@ getFixedAppKitButton(NSInteger button) - (void) cursorUpdate:(NSEvent*)e { - [[NSCursor arrowCursor] set]; - - // unused - return; (void)e; + cursorUpdate(self, e); } - (void) updateTrackingAreas { - static const int opts = NSTrackingMouseEnteredAndExited - | NSTrackingMouseMoved - | NSTrackingEnabledDuringMouseDrag - | NSTrackingInVisibleRect - | NSTrackingActiveAlways - | NSTrackingCursorUpdate; - - if (trackingArea != nil) { - [self removeTrackingArea:trackingArea]; - [trackingArea release]; - } - - trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] - options:opts - owner:self - userInfo:nil]; - [self addTrackingArea:trackingArea]; + updateTrackingAreas(self); [super updateTrackingAreas]; } - (void) viewWillMoveToWindow:(NSWindow*)newWindow { - if (newWindow != nil) { - [newWindow setAcceptsMouseMovedEvents:YES]; - [newWindow makeFirstResponder:self]; - } - + viewWillMoveToWindow(self, newWindow); [super viewWillMoveToWindow:newWindow]; } - (void) mouseMoved:(NSEvent*)event { - if (puglview->motionFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->motionFunc(puglview, loc.x, loc.y); - } + mouseMoved(self, event); } - (void) mouseDragged:(NSEvent*)event { - [self mouseMoved:event]; + mouseMoved(self, event); } - (void) rightMouseDragged:(NSEvent*)event { - [self mouseDragged:event]; + mouseMoved(self, event); } - (void) otherMouseDragged:(NSEvent*)event { - [self mouseDragged:event]; + mouseMoved(self, event); } - (void) mouseDown:(NSEvent*)event { - if (puglview->mouseFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->mouseFunc(puglview, getFixedAppKitButton([event buttonNumber]), true, loc.x, loc.y); - } + mouseDown(self, event); } - (void) rightMouseDown:(NSEvent*)event { - [self mouseDown:event]; + mouseDown(self, event); } - (void) otherMouseDown:(NSEvent*)event { - [self mouseDown:event]; + mouseDown(self, event); } - (void) mouseUp:(NSEvent*)event { - if (puglview->mouseFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->mouseFunc(puglview, getFixedAppKitButton([event buttonNumber]), false, loc.x, loc.y); - } + mouseUp(self, event); } - (void) rightMouseUp:(NSEvent*)event { - [self mouseUp:event]; + mouseUp(self, event); } - (void) otherMouseUp:(NSEvent*)event { - [self mouseUp:event]; + mouseUp(self, event); } - (void) scrollWheel:(NSEvent*)event { - if (puglview->scrollFunc) { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - puglview->mods = getModifiers(puglview, event); - puglview->scrollFunc(puglview, - loc.x, loc.y, - [event deltaX], [event deltaY]); - } + scrollWheel(self, event); } - (void) keyDown:(NSEvent*)event { - if (puglview->keyboardFunc && !(puglview->ignoreKeyRepeat && [event isARepeat])) { - NSString* chars = [event characters]; - puglview->mods = getModifiers(puglview, event); - puglview->keyboardFunc(puglview, true, [chars characterAtIndex:0]); - } + keyDown(self, event); } - (void) keyUp:(NSEvent*)event { - if (puglview->keyboardFunc) { - NSString* chars = [event characters]; - puglview->mods = getModifiers(puglview, event); - puglview->keyboardFunc(puglview, false, [chars characterAtIndex:0]); - } + keyUp(self, event); } - (void) flagsChanged:(NSEvent*)event { - if (puglview->specialFunc) { - const unsigned mods = getModifiers(puglview, event); - if ((mods & PUGL_MOD_SHIFT) != (puglview->mods & PUGL_MOD_SHIFT)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_SHIFT, PUGL_KEY_SHIFT); - } else if ((mods & PUGL_MOD_CTRL) != (puglview->mods & PUGL_MOD_CTRL)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_CTRL, PUGL_KEY_CTRL); - } else if ((mods & PUGL_MOD_ALT) != (puglview->mods & PUGL_MOD_ALT)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_ALT, PUGL_KEY_ALT); - } else if ((mods & PUGL_MOD_SUPER) != (puglview->mods & PUGL_MOD_SUPER)) { - puglview->specialFunc(puglview, mods & PUGL_MOD_SUPER, PUGL_KEY_SUPER); - } - puglview->mods = mods; - } + flagsChanged(self, event); } @end #endif