1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCocoaGLView.mm
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#import <Cocoa/Cocoa.h>
#import "vtkCocoaMacOSXSDKCompatibility.h" // Needed to support old SDKs
#import "vtkCocoaGLView.h"
#import "vtkCocoaRenderWindow.h"
#import "vtkCocoaRenderWindowInteractor.h"
#import "vtkCommand.h"
//----------------------------------------------------------------------------
// Private
@interface vtkCocoaGLView()
@property(readwrite, retain, nonatomic) NSTrackingArea *rolloverTrackingArea;
@end
@implementation vtkCocoaGLView
//----------------------------------------------------------------------------
// Private
- (void)emptyMethod:(id)sender
{
(void)sender;
}
//----------------------------------------------------------------------------
@synthesize rolloverTrackingArea = _rolloverTrackingArea;
//----------------------------------------------------------------------------
// Overridden (from NSView).
// designated initializer
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
// 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.
// To do this, simply detach an NSThread and have it promptly exit.
// This is enough to ensure that the locks needed by the Cocoa
// frameworks are put in place"
if ([NSThread isMultiThreaded] == NO)
{
[NSThread detachNewThreadSelector:@selector(emptyMethod:)
toTarget:self
withObject:nil];
}
}
return self;
}
//----------------------------------------------------------------------------
- (vtkCocoaRenderWindow *)getVTKRenderWindow
{
return _myVTKRenderWindow;
}
//----------------------------------------------------------------------------
- (void)setVTKRenderWindow:(vtkCocoaRenderWindow *)theVTKRenderWindow
{
_myVTKRenderWindow = theVTKRenderWindow;
}
//----------------------------------------------------------------------------
- (vtkCocoaRenderWindowInteractor *)getInteractor
{
if (_myVTKRenderWindow)
{
return (vtkCocoaRenderWindowInteractor *)_myVTKRenderWindow->GetInteractor();
}
else
{
return NULL;
}
}
//----------------------------------------------------------------------------
// Overridden (from NSView).
- (void)drawRect:(NSRect)theRect
{
(void)theRect;
if (_myVTKRenderWindow && _myVTKRenderWindow->GetMapped())
{
_myVTKRenderWindow->Render();
}
}
//----------------------------------------------------------------------------
// Overridden (from NSView).
- (void)updateTrackingAreas
{
//clear out the old tracking area
NSTrackingArea *trackingArea = [self rolloverTrackingArea];
if (trackingArea)
{
[self removeTrackingArea:trackingArea];
}
//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];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (BOOL)acceptsFirstResponder
{
return YES;
}
//----------------------------------------------------------------------------
// For generating keysyms that are compatible with other VTK interactors
static const char *vtkMacCharCodeToKeySymTable[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"space", "exclam", "quotedbl", "numbersign",
"dollar", "percent", "ampersand", "quoteright",
"parenleft", "parenright", "asterisk", "plus",
"comma", "minus", "period", "slash",
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "colon", "semicolon", "less", "equal", "greater", "question",
"at", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "bracketleft",
"backslash", "bracketright", "asciicircum", "underscore",
"quoteleft", "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w",
"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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, "Return", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"Tab", 0, 0, "Backspace", 0, "Escape", 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, "period", 0, "asterisk", 0, "plus", 0, "Clear",
0, 0, 0, "slash", "KP_Enter", 0, "minus", 0,
0, 0, "KP_0", "KP_1", "KP_2", "KP_3", "KP_4", "KP_5",
"KP_6", "KP_7", 0, "KP_8", "KP_9", 0, 0, 0,
"F5", "F6", "F7", "F3", "F8", 0, 0, 0,
0, "Snapshot", 0, 0, 0, 0, 0, 0,
0, 0, "Help", "Home", "Prior", "Delete", "F4", "End",
"F2", "Next", "F1", "Left", "Right", "Down", "Up", 0,
};
//----------------------------------------------------------------------------
// Convert a Cocoa key event into a VTK key event
- (void)invokeVTKKeyEvent:(unsigned long)theEventId
cocoaEvent:(NSEvent *)theEvent
{
vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
vtkCocoaRenderWindow *renWin =
vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);
if (!interactor || !renWin)
{
return;
}
// Get the location of the mouse event relative to this NSView's bottom
// left corner. Since this is NOT a mouse event, we can not use
// locationInWindow. Instead we get the mouse location at this instant,
// which may not be the exact location of the mouse at the time of the
// keypress, but should be quite close. There seems to be no better way.
// And, yes, vtk does sometimes need the mouse location even for key
// events, example: pressing 'p' to pick the actor under the mouse.
// Also note that 'mouseLoc' may have nonsense values if a key is pressed
// while the mouse in not actually in the vtk view but the view is
// first responder.
NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
mouseLoc = [self convertPoint:mouseLoc fromView:nil];
NSUInteger flags = [theEvent modifierFlags];
int shiftDown = ((flags & NSShiftKeyMask) != 0);
int controlDown = ((flags & (NSControlKeyMask | NSCommandKeyMask)) != 0);
int altDown = ((flags & NSAlternateKeyMask) != 0);
unsigned char charCode = '\0';
const char *keySym = 0;
NSEventType type = [theEvent type];
BOOL isPress = (type == NSKeyDown);
if (type == NSKeyUp || type == NSKeyDown)
{
// Get the characters associated with the key event as a utf8 string.
// This pointer is only valid for the duration of the current autorelease
// context!
const char* keyedChars = [[theEvent characters] UTF8String];
// Since vtk only supports ASCII, we just blindly use the first element
// of the above string, hoping it's ASCII.
charCode = (unsigned char)keyedChars[0];
// Get the virtual key code and convert it to a keysym as best we can.
unsigned short macKeyCode = [theEvent keyCode];
if (macKeyCode < 128)
{
keySym = vtkMacKeyCodeToKeySymTable[macKeyCode];
}
if (keySym == 0 && charCode < 128)
{
keySym = vtkMacCharCodeToKeySymTable[charCode];
}
}
else if (type == NSFlagsChanged)
{
// Check to see what modifier flag changed.
if (controlDown != interactor->GetControlKey())
{
keySym = "Control_L";
isPress = (controlDown != 0);
}
else if (shiftDown != interactor->GetShiftKey())
{
keySym = "Shift_L";
isPress = (shiftDown != 0);
}
else if (altDown != interactor->GetAltKey())
{
keySym = "Alt_L";
isPress = (altDown != 0);
}
else
{
return;
}
theEventId = (isPress ?
vtkCommand::KeyPressEvent :
vtkCommand::KeyReleaseEvent);
}
else // No info from which to generate a VTK key event!
{
return;
}
if (keySym == 0)
{
keySym = "None";
}
interactor->SetEventInformation(static_cast<int>(round(mouseLoc.x)),
static_cast<int>(round(mouseLoc.y)),
controlDown, shiftDown,
charCode, 1, keySym);
interactor->SetAltKey(altDown);
interactor->InvokeEvent(theEventId, NULL);
if (isPress && charCode != '\0')
{
interactor->InvokeEvent(vtkCommand::CharEvent, NULL);
}
}
//----------------------------------------------------------------------------
// Convert a Cocoa motion event into a VTK button event
- (void)invokeVTKMoveEvent:(unsigned long)theEventId
cocoaEvent:(NSEvent *)theEvent
{
vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
vtkCocoaRenderWindow *renWin =
vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);
if (!interactor || !renWin)
{
return;
}
// Get the location of the mouse event relative to this NSView's bottom
// left corner. Since this is a mouse event, we can use locationInWindow.
NSPoint mouseLoc =
[self convertPoint:[theEvent locationInWindow] fromView:nil];
NSUInteger flags = [theEvent modifierFlags];
int shiftDown = ((flags & NSShiftKeyMask) != 0);
int controlDown = ((flags & (NSControlKeyMask | NSCommandKeyMask)) != 0);
int altDown = ((flags & NSAlternateKeyMask) != 0);
interactor->SetEventInformation(static_cast<int>(round(mouseLoc.x)),
static_cast<int>(round(mouseLoc.y)),
controlDown, shiftDown);
interactor->SetAltKey(altDown);
interactor->InvokeEvent(theEventId, NULL);
}
//----------------------------------------------------------------------------
// Convert a Cocoa motion event into a VTK button event
- (void)invokeVTKButtonEvent:(unsigned long)theEventId
cocoaEvent:(NSEvent *)theEvent
{
vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
vtkCocoaRenderWindow *renWin =
vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);
if (!interactor || !renWin)
{
return;
}
// Get the location of the mouse event relative to this NSView's bottom
// left corner. Since this is a mouseevent, we can use locationInWindow.
NSPoint mouseLoc =
[self convertPoint:[theEvent locationInWindow] fromView:nil];
int clickCount = static_cast<int>([theEvent clickCount]);
int repeatCount = ((clickCount > 1) ? clickCount - 1 : 0);
NSUInteger flags = [theEvent modifierFlags];
int shiftDown = ((flags & NSShiftKeyMask) != 0);
int controlDown = ((flags & (NSControlKeyMask | NSCommandKeyMask)) != 0);
int altDown = ((flags & NSAlternateKeyMask) != 0);
interactor->SetEventInformation(static_cast<int>(round(mouseLoc.x)),
static_cast<int>(round(mouseLoc.y)),
controlDown, shiftDown,
0, repeatCount);
interactor->SetAltKey(altDown);
interactor->InvokeEvent(theEventId, NULL);
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)keyDown:(NSEvent *)theEvent
{
[self invokeVTKKeyEvent:vtkCommand::KeyPressEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)keyUp:(NSEvent *)theEvent
{
[self invokeVTKKeyEvent:vtkCommand::KeyReleaseEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)flagsChanged:(NSEvent *)theEvent
{
// what kind of event it is will be decided by invokeVTKKeyEvent
[self invokeVTKKeyEvent:vtkCommand::AnyEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseMoved:(NSEvent *)theEvent
{
// Note: this method will only be called if this view's NSWindow
// is set to receive mouse moved events. See setAcceptsMouseMovedEvents:
// An NSWindow created by vtk automatically does accept such events.
// Ignore motion outside the view in order to mimic other interactors
NSPoint mouseLoc =
[self convertPoint:[theEvent locationInWindow] fromView:nil];
if (NSPointInRect(mouseLoc, [self visibleRect]))
{
[self invokeVTKMoveEvent:vtkCommand::MouseMoveEvent
cocoaEvent:theEvent];
}
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseDragged:(NSEvent *)theEvent
{
[self invokeVTKMoveEvent:vtkCommand::MouseMoveEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)rightMouseDragged:(NSEvent *)theEvent
{
[self invokeVTKMoveEvent:vtkCommand::MouseMoveEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)otherMouseDragged:(NSEvent *)theEvent
{
[self invokeVTKMoveEvent:vtkCommand::MouseMoveEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseEntered:(NSEvent *)theEvent
{
// Note: the mouseEntered/mouseExited events depend on the maintenance of
// the tracking area (updateTrackingAreas).
[self invokeVTKMoveEvent:vtkCommand::EnterEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseExited:(NSEvent *)theEvent
{
[self invokeVTKMoveEvent:vtkCommand::LeaveEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)scrollWheel:(NSEvent *)theEvent
{
CGFloat dy = [theEvent deltaY];
unsigned long eventId = 0;
if (dy > 0)
{
eventId = vtkCommand::MouseWheelForwardEvent;
}
else if (dy < 0)
{
eventId = vtkCommand::MouseWheelBackwardEvent;
}
if (eventId != 0)
{
[self invokeVTKMoveEvent:eventId
cocoaEvent:theEvent];
}
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseDown:(NSEvent *)theEvent
{
[self invokeVTKButtonEvent:vtkCommand::LeftButtonPressEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)rightMouseDown:(NSEvent *)theEvent
{
[self invokeVTKButtonEvent:vtkCommand::RightButtonPressEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)otherMouseDown:(NSEvent *)theEvent
{
[self invokeVTKButtonEvent:vtkCommand::MiddleButtonPressEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseUp:(NSEvent *)theEvent
{
[self invokeVTKButtonEvent:vtkCommand::LeftButtonReleaseEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)rightMouseUp:(NSEvent *)theEvent
{
[self invokeVTKButtonEvent:vtkCommand::RightButtonReleaseEvent
cocoaEvent:theEvent];
}
//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)otherMouseUp:(NSEvent *)theEvent
{
[self invokeVTKButtonEvent:vtkCommand::MiddleButtonReleaseEvent
cocoaEvent:theEvent];
}
@end
|