File: vtkCocoaGLView.mm

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (792 lines) | stat: -rw-r--r-- 26,616 bytes parent folder | download | duplicates (4)
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*=========================================================================

  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"


@implementation vtkCocoaGLView

//----------------------------------------------------------------------------
- (void)emptyMethod:(id)sender
{
  (void)sender;
}

//----------------------------------------------------------------------------
// Overridden (from NSView).
// designated initializer
- (id)initWithFrame:(NSRect)frameRect
{
  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.
    // 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();
    }
}

//----------------------------------------------------------------------------
- (void)clearTrackingRect
{
  // remove any tracking rect we have
  if (_rolloverTrackingRectSet)
    {
    [self removeTrackingRect:_rolloverTrackingRectTag];
    _rolloverTrackingRectSet = NO;
    }
}

//----------------------------------------------------------------------------
- (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];
}

//----------------------------------------------------------------------------
// 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,
};

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)keyDown:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];

  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!renWin)
    {
    return;
    }

  // Get the location of the mouse event relative to this NSView's bottom
  // left corner.  Since this is a NOT mouseevent, 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];

  int shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;
  // 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.
  unsigned char 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];
  const char *keySym = 0;
  if (macKeyCode < 128)
    {
    keySym = vtkMacKeyCodeToKeySymTable[macKeyCode];
    }
  if (keySym == 0 && charCode < 128)
    {
    keySym = vtkMacCharCodeToKeySymTable[charCode];
    }
  if (keySym == 0)
    {
    keySym = "None";
    }

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown,
                                  charCode, 1, keySym);
  interactor->SetAltKey(altDown);

  interactor->InvokeEvent(vtkCommand::KeyPressEvent, NULL);
  if (charCode != '\0')
    {
    interactor->InvokeEvent(vtkCommand::CharEvent, NULL);
    }
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)keyUp:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];

  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!renWin)
    {
    return;
    }

  // Get the location of the mouse event relative to this NSView's bottom
  // left corner.  Since this is a NOT mouseevent, 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];

  int shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;
  // 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.
  unsigned char 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];
  const char *keySym = 0;
  if (macKeyCode < 128)
    {
    keySym = vtkMacKeyCodeToKeySymTable[macKeyCode];
    }
  if (keySym == 0 && charCode < 128)
    {
    keySym = vtkMacCharCodeToKeySymTable[charCode];
    }
  if (keySym == 0)
    {
    keySym = "None";
    }

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown,
                                  charCode, 1, keySym);
  interactor->SetAltKey(altDown);

  interactor->InvokeEvent(vtkCommand::KeyReleaseEvent, NULL);
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)flagsChanged:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];

  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!renWin)
    {
    return;
    }

  // Get the location of the mouse event relative to this NSView's bottom
  // left corner.  Since this is a NOT mouseevent, 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];

  int shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;

  int oldControlDown = interactor->GetControlKey();
  int oldShiftDown = interactor->GetShiftKey();
  int oldAltDown = interactor->GetAltKey();

  int keyPress = 0;
  char charCode = '\0';
  const char *keySym = 0;
  if (controlDown != oldControlDown)
    {
    keySym = "Control_L";
    keyPress = oldControlDown = controlDown;
    }
  else if (shiftDown != oldShiftDown)
    {
    keySym = "Shift_L";
    keyPress = oldShiftDown = shiftDown;
    }
  else if (altDown != oldAltDown)
    {
    keySym = "Alt_L";
    keyPress = oldAltDown = altDown;
    }
  else
    {
    return;
    }

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  oldControlDown, oldShiftDown,
                                  charCode, 1, keySym);
  interactor->SetAltKey(oldAltDown);

  if (keyPress)
    {
    interactor->InvokeEvent(vtkCommand::KeyPressEvent, NULL);
    }
  else
    {
    interactor->InvokeEvent(vtkCommand::KeyReleaseEvent, NULL);
    }
}

//----------------------------------------------------------------------------
// 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.

  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!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];

  // Ignore motion outside the view in order to mimic other interactors
  if (!NSPointInRect(mouseLoc, [self visibleRect]))
    {
    return;
    }

  int shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown);
  interactor->SetAltKey(altDown);
  interactor->InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (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.

  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!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 shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                 (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown);
  interactor->SetAltKey(altDown);
  interactor->InvokeEvent(vtkCommand::EnterEvent, NULL);
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseExited:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!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 shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                 (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown);
  interactor->SetAltKey(altDown);
  interactor->InvokeEvent(vtkCommand::LeaveEvent, NULL);
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)scrollWheel:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!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 shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown);
  interactor->SetAltKey(altDown);

  if( [theEvent deltaY] > 0)
    {
    interactor->InvokeEvent(vtkCommand::MouseWheelForwardEvent, NULL);
    }
  else if( [theEvent deltaY] < 0)
    {
    interactor->InvokeEvent(vtkCommand::MouseWheelBackwardEvent, NULL);
    }
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)mouseDown:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!renWin)
    {
    return;
    }

  BOOL keepOn = YES;

  // 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 shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;
  int clickCount = static_cast<int>([theEvent clickCount]);
  int repeatCount = clickCount > 1 ? clickCount - 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown,
                                  0, repeatCount);
  interactor->SetAltKey(altDown);

  interactor->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);

  NSApplication* application = [NSApplication sharedApplication];
  NSDate* infinity = [NSDate distantFuture];
  do
    {
    theEvent = [application nextEventMatchingMask:NSLeftMouseUpMask |
                                                  NSLeftMouseDraggedMask
                                        untilDate:infinity
                                           inMode:NSEventTrackingRunLoopMode
                                          dequeue:YES];
    if (theEvent)
      {
      mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];

      interactor->SetEventInformation((int)round(mouseLoc.x),
                                      (int)round(mouseLoc.y),
                                      controlDown, shiftDown);
      interactor->SetAltKey(altDown);

      switch ([theEvent type])
        {
      case NSLeftMouseDragged:
        interactor->InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
        break;
      case NSLeftMouseUp:
        interactor->InvokeEvent(vtkCommand::LeftButtonReleaseEvent, NULL);
        keepOn = NO;
      default:
        break;
        }
      }
    else
      {
      keepOn = NO;
      }
    }
  while (keepOn);
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)rightMouseDown:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!renWin)
    {
    return;
    }

  BOOL keepOn = YES;

  // 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 shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;
  int clickCount = [theEvent clickCount];
  int repeatCount = clickCount > 1 ? clickCount - 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown,
                                  0, repeatCount);
  interactor->SetAltKey(altDown);

  interactor->InvokeEvent(vtkCommand::RightButtonPressEvent,NULL);

  NSApplication* application = [NSApplication sharedApplication];
  NSDate* infinity = [NSDate distantFuture];
  do
    {
    theEvent = [application nextEventMatchingMask:NSRightMouseUpMask |
                                                  NSRightMouseDraggedMask
                                        untilDate:infinity
                                           inMode:NSEventTrackingRunLoopMode
                                          dequeue:YES];
    if (theEvent)
      {
      mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];

      interactor->SetEventInformation((int)round(mouseLoc.x),
                                      (int)round(mouseLoc.y),
                                      controlDown, shiftDown);
      interactor->SetAltKey(altDown);

      switch ([theEvent type])
        {
      case NSRightMouseDragged:
        interactor->InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
        break;
      case NSRightMouseUp:
        interactor->InvokeEvent(vtkCommand::RightButtonReleaseEvent, NULL);
        keepOn = NO;
      default:
        break;
        }
      }
    else
      {
      keepOn = NO;
      }
    }
  while (keepOn);
}

//----------------------------------------------------------------------------
// Overridden (from NSResponder).
- (void)otherMouseDown:(NSEvent *)theEvent
{
  vtkCocoaRenderWindowInteractor *interactor = [self getInteractor];
  if (!interactor)
    {
    return;
    }

  vtkCocoaRenderWindow* renWin =
    vtkCocoaRenderWindow::SafeDownCast([self getVTKRenderWindow]);

  if (!renWin)
    {
    return;
    }

  BOOL keepOn = YES;

  // 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 shiftDown = ([theEvent modifierFlags] & NSShiftKeyMask) ? 1 : 0;
  int controlDown = ([theEvent modifierFlags] & NSControlKeyMask) ? 1 : 0;
  int altDown = ([theEvent modifierFlags] &
                  (NSCommandKeyMask | NSAlternateKeyMask)) ? 1 : 0;
  int clickCount = [theEvent clickCount];
  int repeatCount = clickCount > 1 ? clickCount - 1 : 0;

  interactor->SetEventInformation((int)round(mouseLoc.x),
                                  (int)round(mouseLoc.y),
                                  controlDown, shiftDown,
                                  0, repeatCount);
  interactor->SetAltKey(altDown);

  interactor->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);

  NSApplication* application = [NSApplication sharedApplication];
  NSDate* infinity = [NSDate distantFuture];
  do
    {
    theEvent = [application nextEventMatchingMask:NSOtherMouseUpMask |
                                                  NSOtherMouseDraggedMask
                                        untilDate:infinity
                                           inMode:NSEventTrackingRunLoopMode
                                          dequeue:YES];
    if (theEvent)
      {
      mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];

      interactor->SetEventInformation((int)round(mouseLoc.x),
                                      (int)round(mouseLoc.y),
                                      controlDown, shiftDown);
      interactor->SetAltKey(altDown);

      switch ([theEvent type])
        {
      case NSOtherMouseDragged:
        interactor->InvokeEvent(vtkCommand::MouseMoveEvent, NULL);
        break;
      case NSOtherMouseUp:
        interactor->InvokeEvent(vtkCommand::MiddleButtonReleaseEvent, NULL);
        keepOn = NO;
      default:
        break;
        }
      }
    else
      {
      keepOn = NO;
      }
    }
  while (keepOn);
}

@end