Commit d9ec645c authored by Sean McBride's avatar Sean McBride

Modernized Cocoa code to use NSTrackingArea

NSTrackingArea long ago replaced older APIs
used before this.  We still work back to 10.5
even after this change.  Code affects mouse cursor
shape changes.
parent 64b024ec
......@@ -18,18 +18,20 @@
// This class is a subclass of Cocoa's NSView; it uses Objective-C++.
// This class overrides several NSView methods.
// To provide the usual VTK keyboard user interface, it overrides the
// following methods from NSResponder: acceptsFirstResponder, keyDown:,
// following methods: acceptsFirstResponder, keyDown:,
// keyUp:, and flagsChanged:
// To provide the usual VTK mouse user interface, it overrides the
// following methods from NSResponder: mouseMoved:, mouseEntered:,
// following methods: mouseMoved:, mouseEntered:,
// mouseExited: scrollWheel:, mouseDown:, rightMouseDown:,
// otherMouseDown:, mouseDragged:, rightMouseDragged:, otherMouseDragged:,
// and resetCursorRects.
// To be able to render and draw onscreen, it overrides drawRect:
// from NSView.
// and updateTrackingAreas.
// To be able to render and draw onscreen, it overrides drawRect:.
// Note that this class was previously a subclass of NSOpenGLView,
// Compatibility notes:
// - this class was previously a subclass of NSOpenGLView,
// but starting with VTK 5.0 is now a subclass of NSView.
// - starting with VTK 6.3 this class overrides the more modern
// updateTrackingAreas instead of resetCursorRects.
//
// .SECTION See Also
// vtkCocoaRenderWindow vtkCocoaRenderWindowInteractor
......@@ -59,8 +61,7 @@
{
@private
vtkCocoaRenderWindowRef _myVTKRenderWindow;
NSTrackingRectTag _rolloverTrackingRectTag;
BOOL _rolloverTrackingRectSet;
NSTrackingArea* _rolloverTrackingArea;
}
- (vtkCocoaRenderWindowRef)getVTKRenderWindow;
......
......@@ -21,6 +21,11 @@
#import "vtkCocoaRenderWindowInteractor.h"
#import "vtkCommand.h"
//----------------------------------------------------------------------------
// Private
@interface vtkCocoaGLView()
@property(readwrite, retain, nonatomic) NSTrackingArea *rolloverTrackingArea;
@end
@implementation vtkCocoaGLView
......@@ -31,6 +36,9 @@
(void)sender;
}
//----------------------------------------------------------------------------
@synthesize rolloverTrackingArea = _rolloverTrackingArea;
//----------------------------------------------------------------------------
// Overridden (from NSView).
// designated initializer
......@@ -39,9 +47,6 @@
self = [super initWithFrame:frameRect];
if (self)
{
// The tracking rect is not set yet.
_rolloverTrackingRectSet = NO;
// Force Cocoa into "multi threaded mode" because VTK spawns pthreads.
// Apple's docs say: "If you intend to use Cocoa calls, you must force
// Cocoa into its multithreaded mode before detaching any POSIX threads.
......@@ -96,38 +101,32 @@
}
//----------------------------------------------------------------------------
// Private
- (void)clearTrackingRect
// Overridden (from NSView).
- (void)updateTrackingAreas
{
// remove any tracking rect we have
if (_rolloverTrackingRectSet)
//clear out the old tracking area
NSTrackingArea *trackingArea = [self rolloverTrackingArea];
if (trackingArea)
{
[self removeTrackingRect:_rolloverTrackingRectTag];
_rolloverTrackingRectSet = NO;
[self removeTrackingArea:trackingArea];
}
}
//----------------------------------------------------------------------------
// Private
- (void)resetTrackingRect
{
//clear out the old tracking rect
[self clearTrackingRect];
//create a new tracking rect
_rolloverTrackingRectTag = [self addTrackingRect:[self visibleRect]
owner:self
userData:NULL
assumeInside:NO];
_rolloverTrackingRectSet = YES;
}
//----------------------------------------------------------------------------
// Overridden (from NSView).
- (void)resetCursorRects
{
[super resetCursorRects];
[self resetTrackingRect];
//create a new tracking area
NSRect rect = [self visibleRect];
NSTrackingAreaOptions opts = (NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveAlways);
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
[self setRolloverTrackingArea:trackingArea];
#if !VTK_OBJC_IS_ARC
[trackingArea release];
#endif
[super updateTrackingAreas];
}
//----------------------------------------------------------------------------
......@@ -159,6 +158,7 @@ static const char *vtkMacCharCodeToKeySymTable[128] = {
"x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Delete",
};
//----------------------------------------------------------------------------
// For generating keysyms that are compatible with other VTK interactors
static const char *vtkMacKeyCodeToKeySymTable[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
......@@ -422,8 +422,7 @@ static const char *vtkMacKeyCodeToKeySymTable[128] = {
- (void)mouseEntered:(NSEvent *)theEvent
{
// Note: the mouseEntered/mouseExited events depend on the maintenance of
// the Tracking Rect, which is handled by the resetTrackingRect,
// clearTrackingRect and resetCursorRects methods above.
// the tracking area (updateTrackingAreas).
[self invokeVTKMoveEvent:vtkCommand::EnterEvent
cocoaEvent:theEvent];
}
......
......@@ -18,18 +18,20 @@
// This class is a subclass of Cocoa's NSView; it uses Objective-C++.
// This class overrides several NSView methods.
// To provide the usual VTK keyboard user interface, it overrides the
// following methods from NSResponder: acceptsFirstResponder, keyDown:,
// following methods: acceptsFirstResponder, keyDown:,
// keyUp:, and flagsChanged:
// To provide the usual VTK mouse user interface, it overrides the
// following methods from NSResponder: mouseMoved:, mouseEntered:,
// following methods: mouseMoved:, mouseEntered:,
// mouseExited: scrollWheel:, mouseDown:, rightMouseDown:,
// otherMouseDown:, mouseDragged:, rightMouseDragged:, otherMouseDragged:,
// and resetCursorRects.
// To be able to render and draw onscreen, it overrides drawRect:
// from NSView.
// and updateTrackingAreas.
// To be able to render and draw onscreen, it overrides drawRect:.
// Note that this class was previously a subclass of NSOpenGLView,
// Compatibility notes:
// - this class was previously a subclass of NSOpenGLView,
// but starting with VTK 5.0 is now a subclass of NSView.
// - starting with VTK 6.3 this class overrides the more modern
// updateTrackingAreas instead of resetCursorRects.
//
// .SECTION See Also
// vtkCocoaRenderWindow vtkCocoaRenderWindowInteractor
......@@ -59,8 +61,7 @@
{
@private
vtkCocoaRenderWindowRef _myVTKRenderWindow;
NSTrackingRectTag _rolloverTrackingRectTag;
BOOL _rolloverTrackingRectSet;
NSTrackingArea* _rolloverTrackingArea;
}
- (vtkCocoaRenderWindowRef)getVTKRenderWindow;
......
......@@ -22,6 +22,11 @@
#import "vtkCocoaRenderWindowInteractor.h"
#import "vtkCommand.h"
//----------------------------------------------------------------------------
// Private
@interface vtkCocoaGLView()
@property(readwrite, retain, nonatomic) NSTrackingArea *rolloverTrackingArea;
@end
@implementation vtkCocoaGLView
......@@ -32,6 +37,9 @@
(void)sender;
}
//----------------------------------------------------------------------------
@synthesize rolloverTrackingArea = _rolloverTrackingArea;
//----------------------------------------------------------------------------
// Overridden (from NSView).
// designated initializer
......@@ -40,9 +48,6 @@
self = [super initWithFrame:frameRect];
if (self)
{
// The tracking rect is not set yet.
_rolloverTrackingRectSet = NO;
// Force Cocoa into "multi threaded mode" because VTK spawns pthreads.
// Apple's docs say: "If you intend to use Cocoa calls, you must force
// Cocoa into its multithreaded mode before detaching any POSIX threads.
......@@ -97,38 +102,32 @@
}
//----------------------------------------------------------------------------
// Private
- (void)clearTrackingRect
// Overridden (from NSView).
- (void)updateTrackingAreas
{
// remove any tracking rect we have
if (_rolloverTrackingRectSet)
//clear out the old tracking area
NSTrackingArea *trackingArea = [self rolloverTrackingArea];
if (trackingArea)
{
[self removeTrackingRect:_rolloverTrackingRectTag];
_rolloverTrackingRectSet = NO;
[self removeTrackingArea:trackingArea];
}
}
//----------------------------------------------------------------------------
// Private
- (void)resetTrackingRect
{
//clear out the old tracking rect
[self clearTrackingRect];
//create a new tracking rect
_rolloverTrackingRectTag = [self addTrackingRect:[self visibleRect]
owner:self
userData:NULL
assumeInside:NO];
_rolloverTrackingRectSet = YES;
}
//----------------------------------------------------------------------------
// Overridden (from NSView).
- (void)resetCursorRects
{
[super resetCursorRects];
[self resetTrackingRect];
//create a new tracking area
NSRect rect = [self visibleRect];
NSTrackingAreaOptions opts = (NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveAlways);
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
[self setRolloverTrackingArea:trackingArea];
#if !VTK_OBJC_IS_ARC
[trackingArea release];
#endif
[super updateTrackingAreas];
}
//----------------------------------------------------------------------------
......@@ -160,6 +159,7 @@ static const char *vtkMacCharCodeToKeySymTable[128] = {
"x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Delete",
};
//----------------------------------------------------------------------------
// For generating keysyms that are compatible with other VTK interactors
static const char *vtkMacKeyCodeToKeySymTable[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
......@@ -423,8 +423,7 @@ static const char *vtkMacKeyCodeToKeySymTable[128] = {
- (void)mouseEntered:(NSEvent *)theEvent
{
// Note: the mouseEntered/mouseExited events depend on the maintenance of
// the Tracking Rect, which is handled by the resetTrackingRect,
// clearTrackingRect and resetCursorRects methods above.
// the tracking area (updateTrackingAreas).
[self invokeVTKMoveEvent:vtkCommand::EnterEvent
cocoaEvent:theEvent];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment