-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGLUTView.m
2113 lines (1819 loc) · 65.3 KB
/
GLUTView.m
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) Dietmar Planitzer, 1998, 2002 - 2003 */
/* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */
#import "GLUTView.h"
#import "GLUTWindow.h"
#import "GLUTMenu.h"
// HiDPI support
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
@interface NSView (HiDPI)
- (void)setWantsBestResolutionOpenGLSurface:(BOOL)aBool;
- (BOOL)wantsBestResolutionOpenGLSurface;
@end
#endif
@interface GLUTView(GLUTPrivate)
- (void)_commonReshape;
- (void)_updateTrackingRects: (NSNotification *)notification;
- (NSCursor *)_inheritedNativeCursor;
- (void)_recursiveInvalidateCursorRectsWithWindow: (GLUTWindow *)aWindow;
- (void)_recursiveCopyPixelsTo: (NSBitmapImageRep *)bitmap sourceRect: (NSRect)srcRect baseView: (NSView *)bView;
- (void)_recursiveMarkHidden;
- (void)_updateComputedVisibility;
- (void)evaluateVisibility;
- (NSArray *)_orderedSiblings;
@end
static GLUTView * __glutVisibilityUpdateList = NULL;
static GLUTView * __glutVisibilityUpdateTail = NULL;
#pragma mark -
@implementation GLUTView
/* Designated initializer */
- (id)initWithFrame: (NSRect)frameRect pixelFormat: (NSOpenGLPixelFormat *)pixelFormat
windowID: (int)winid treatAsSingle: (BOOL)treatAsSingle isSubwindow: (BOOL)isSub
fullscreenStereo: (BOOL)pfStereo isVBLSynced: (BOOL)isVBLSync
{
if((self = [super initWithFrame: frameRect]) != nil) {
if ([self respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) {
if(__glutDisplayString) {
[self setWantsBestResolutionOpenGLSurface:(strstr(__glutDisplayString, "hidpi") != NULL)];
}
}
_openGLContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
if(!_openGLContext) {
[self release];
return nil;
}
[self setAutoresizingMask: (NSViewHeightSizable | NSViewWidthSizable)];
[self setAutoresizesSubviews: NO];
[self setPostsBoundsChangedNotifications: NO];
[self setPostsFrameChangedNotifications: NO];
[self allocateGState]; // make -lockFocus 2x faster...
/* This list contains ALL subviews of a GLUTView including
hidden views - which are NOT part of -subviews. This is
necessary so that we can re-insert a hidden view at the
correct place in the sibling list. I.e. the view just
below us might have been killed while we were hidden.
We track such changes via this list and are thus able to
re-display the hidden view in the correct position once
glutShowWindow is called on the hidden view again. */
__glutInitList(&_allChildrens);
_siblings.obj = self;
_cursorID = GLUT_CURSOR_INHERIT;
_visState = GLUT_UNKNOWN_VISIBILITY;
_flags.forceReshape = YES;
_flags.isVisibilityUpdateAllowed = [NSApp isRunning];
_flags.isSubwindow = isSub;
_flags.isShown = NO;
_flags.treatAsSingle = treatAsSingle;
_winid = winid;
_displayFunc = __glutDefaultDisplay;
_reshapeFunc = __glutDefaultReshape;
_wmCloseFunc = __glutDefaultWMClose;
_quadObj = NULL;
_newVisState = GLUT_UNKNOWN_VISIBILITY;
_eventMask = 0;
_curEventMask = 0;
_isFullscreenStereo = pfStereo;
if (isVBLSync)
_isVBLSync = 1;
else
_isVBLSync = 0;
[[self openGLContext] makeCurrentContext];
if(_flags.treatAsSingle) {
/* We do this because either the window really is single
buffered (in which case this is redundant, but harmless,
because this is the initial single-buffered context
state); or we are treating a double buffered window as a
single-buffered window because the system does not appear
to export any suitable single- buffered visuals (in which
the following are necessary). */
glDrawBuffer(GL_FRONT);
glReadBuffer(GL_FRONT);
}
[[self openGLContext] setValues:&_isVBLSync forParameter:NSOpenGLCPSwapInterval];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_surfaceNeedsUpdate:) name:NSViewGlobalFrameDidChangeNotification object:self];
return self;
}
return nil;
}
- (void)dealloc
{
int i, winid = [self windowID];
GLUTNode * curChild = _allChildrens.head.succ;
/* Recursively destroy any children. */
while(curChild) {
GLUTNode * tmp = curChild->succ;
[(id)(curChild->obj) release];
curChild = tmp;
}
/* Unbind if bound to this window. */
if(self == __glutCurrentView) {
UNMAKE_CURRENT();
__glutCurrentView = nil;
}
for(i = 0; i < GLUT_MAX_MENUS; i++)
[_menu[i] release];
[self releaseGState];
[_nativeCursor release];
if(_trackingRectTag)
[self removeTrackingRect: _trackingRectTag];
if(_quadObj)
gluDeleteQuadric(_quadObj);
/* NULLing the __glutWindowList helps detect is a window
instance has been destroyed, given a window number. */
__glutViewList[winid - 1] = NULL;
/* Cleanup data structures that might contain window. */
__glutPurgeWorkEvents(winid);
if(self == __glutGameModeWindow) {
/* Destroying the game mode window should implicitly
have GLUT leave game mode. */
__glutGameModeWindow = nil;
__glutDestoryingGameMode = false;
[[self openGLContext] clearDrawable];
__glutCloseDownGameMode();
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewGlobalFrameDidChangeNotification object:self];
// If our context is current, clear it so no one can do anything bad any more.
if([NSOpenGLContext currentContext] == _openGLContext)
[NSOpenGLContext clearCurrentContext];
[_openGLContext release];
[super dealloc];
}
- (void)finalize
{
int i, winid = [self windowID];
GLUTNode * curChild = _allChildrens.head.succ;
/* Recursively destroy any children. */
while(curChild) {
GLUTNode * tmp = curChild->succ;
[(id)(curChild->obj) release];
curChild = tmp;
}
/* Unbind if bound to this window. */
if(self == __glutCurrentView) {
UNMAKE_CURRENT();
__glutCurrentView = nil;
}
for(i = 0; i < GLUT_MAX_MENUS; i++)
[_menu[i] release];
[self releaseGState];
if(_trackingRectTag)
[self removeTrackingRect: _trackingRectTag];
if(_quadObj)
gluDeleteQuadric(_quadObj);
/* NULLing the __glutWindowList helps detect is a window
instance has been destroyed, given a window number. */
__glutViewList[winid - 1] = NULL;
/* Cleanup data structures that might contain window. */
__glutPurgeWorkEvents(winid);
if(self == __glutGameModeWindow) {
/* Destroying the game mode window should implicitly
have GLUT leave game mode. */
__glutGameModeWindow = nil;
__glutDestoryingGameMode = false;
[[self openGLContext] clearDrawable];
__glutCloseDownGameMode();
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewGlobalFrameDidChangeNotification object:self];
if([NSOpenGLContext currentContext] == _openGLContext)
[NSOpenGLContext clearCurrentContext];
[super finalize];
}
/////////////////////////////////////////////
#pragma mark -
#pragma mark NSView overrides
#pragma mark -
- (BOOL)isOpaque
{
return YES;
}
- (void)lockFocus
{
NSOpenGLContext* context = _openGLContext;
// make sure we are ready to draw
//
[super lockFocus];
// when we are about to draw, make sure we are linked to the view and
//
if (!_isFullscreenStereo && ([context view] != self)) {
[context setView:self];
}
// make us the current OpenGL context
//
[context makeCurrentContext];
}
- (void)makeCurrentGLUTView
{
NSOpenGLContext* context = _openGLContext;
if(_isFullscreenStereo && (__glutGameModeWindow == self) && !_inFullScreen)
{
[context setFullScreen];
_inFullScreen = YES;
}
[context makeCurrentContext];
}
- (void)resignCurrentGLUTView
{
NSOpenGLContext* context = _openGLContext;
if(_isFullscreenStereo && (__glutGameModeWindow != self) && _inFullScreen)
{
[context clearDrawable];
_inFullScreen = NO;
}
[NSOpenGLContext clearCurrentContext];
}
- (NSOpenGLContext *)openGLContext
{
return _openGLContext;
}
- (void)update
{
if ([_openGLContext view] == self) {
[_openGLContext update];
}
}
- (void) _surfaceNeedsUpdate:(NSNotification*)notification
{
[self update];
}
/////////////////////////////////////////////
#pragma mark -
#pragma mark Accessors
#pragma mark -
- (NSPoint)windowPosition
{
NSPoint pt;
GLUTWindow * window = (GLUTWindow *) [self window];
if(!_flags.isSubwindow) {
unsigned int mask = [window styleMask];
NSRect rect = [NSWindow contentRectForFrameRect: [window frame] styleMask: mask];
pt = rect.origin;
pt.y = __glutScreenHeight - (pt.y + rect.size.height);
} else {
pt = [self convertPoint: [self bounds].origin toView: nil];
pt = [window convertBaseToScreen: pt];
}
return pt;
}
- (BOOL)isSubwindow
{
return _flags.isSubwindow;
}
/* Return window size in local coordinates. */
- (NSSize)windowSize
{
return [self convertSizeToBacking: [self bounds].size];
}
- (int)visibilityState
{
return _visState;
}
- (BOOL)isDamaged
{
return _flags.isDamaged;
}
- (BOOL)isShown
{
return _flags.isShown;
}
- (void)setShown: (BOOL)flag
{
_flags.isShown = flag;
}
- (BOOL)isTreatAsSingle
{
return _flags.treatAsSingle;
}
- (int)windowID
{
return _winid;
}
- (int)parentWindowID
{
if(_flags.isSubwindow) {
GLUTView * parentWindow = (GLUTView *)[self superview];
if(parentWindow == nil)
parentWindow = _savedSuperview;
return [parentWindow windowID];
}
return 0;
}
/* Return the real number of childrens including hidden ones */
- (unsigned)numberOfChildrens
{
GLUTNode * tail = &_allChildrens.tail;
GLUTNode * node = _allChildrens.head.succ;
unsigned i = 0;
while(node != tail) {
i++;
node = node->succ;
}
return i;
}
- (BOOL)ignoreKeyRepeats
{
return _flags.ignoreKeyRepeats;
}
- (void)setIgnoreKeyRepeats: (BOOL)yesno
{
_flags.ignoreKeyRepeats = yesno;
}
- (NSTimeInterval)joystickPollInterval
{
return _pollInterval;
}
- (int)eventMask
{
return _eventMask;
}
- (void)setEventMask: (int)mask
{
_eventMask = mask;
}
- (BOOL)isFullscreenStereo
{
return _isFullscreenStereo;
}
- (BOOL)isVBLSync
{
return _isVBLSync;
}
- (int)windowScale
{
return [self windowSize].width / [self bounds].size.width;
}
/////////////////////////////////////////////
#pragma mark -
#pragma mark Callbacks
#pragma mark -
- (void)setPassiveMotionCallback: (GLUTmotionCB)func { _passiveMotionFunc = func; }
- (void)setEntryCallback: (GLUTentryCB)func { _entryFunc = func; }
- (void)setKeyDownCallback: (GLUTkeyboardCB)func { _keyDownFunc = func; }
- (void)setKeyUpCallback: (GLUTkeyboardCB)func { _keyUpFunc = func; }
- (void)setMouseCallback: (GLUTmouseCB)func { _mouseFunc = func; }
- (void)setMotionCallback: (GLUTpassiveCB)func { _motionFunc = func; }
- (void)setSpecialDownCallback: (GLUTspecialCB)func { _specialFunc = func; }
- (void)setSpecialUpCallback: (GLUTspecialCB)func { _specialUpFunc = func; }
- (void)setDisplayCallback: (GLUTdisplayCB)func
{
if(!func) {
__glutFatalError("NULL display callback not allowed in GLUT 3.0; update your code.");
}
_displayFunc = func;
}
- (void)setReshapeCallback: (GLUTreshapeCB)func
{
_reshapeFunc = (func) ? func : __glutDefaultReshape;
}
- (void)setWindowStatusCallback: (GLUTwindowStatusCB)func
{
_windowStatusFunc = func;
if(!_windowStatusFunc) {
/* Make state invalid. */
_visState = GLUT_UNKNOWN_VISIBILITY;
}
}
- (void)setSpaceballMotionCallback: (GLUTspaceMotionCB)func
{
_spaceballMotionFunc = func;
if (_spaceballMotionFunc)
if (!_spaceballTimer)
_spaceballTimer = [NSTimer scheduledTimerWithTimeInterval: 0.01
target: self
selector: @selector(processSpaceball:)
userInfo: 0
repeats: YES];
else
[_spaceballTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow:0.01]];
else if (!_spaceballMotionFunc && !_spaceballRotateFunc && !_spaceballButtonFunc) { // if no spaceball functions turn off timer
[_spaceballTimer invalidate];
_spaceballTimer = nil;
}
}
- (void)setSpaceballRotateCallback: (GLUTspaceRotateCB)func
{
_spaceballRotateFunc = func;
if (_spaceballRotateFunc)
if (!_spaceballTimer)
_spaceballTimer = [NSTimer scheduledTimerWithTimeInterval: 0.01
target: self
selector: @selector(processSpaceball:)
userInfo: 0
repeats: YES];
else
[_spaceballTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow:0.01]];
else if (!_spaceballMotionFunc && !_spaceballRotateFunc && !_spaceballButtonFunc) { // if no spaceball functions turn off timer
[_spaceballTimer invalidate];
_spaceballTimer = nil;
}
}
- (void)setSpaceballButtonCallback: (GLUTspaceButtonCB)func
{
_spaceballButtonFunc = func;
if (_spaceballButtonFunc)
if (!_spaceballTimer)
_spaceballTimer = [NSTimer scheduledTimerWithTimeInterval: 0.01
target: self
selector: @selector(processSpaceball:)
userInfo: 0
repeats: YES];
else
[_joyTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow:0.01]];
else if (!_spaceballMotionFunc && !_spaceballRotateFunc && !_spaceballButtonFunc) { // if no spaceball functions turn off timer
[_spaceballTimer invalidate];
_spaceballTimer = nil;
}
}
- (void)setButtonBoxCallback: (GLUTbuttonBoxCB)func { _buttonBoxFunc = func; }
- (void)setDialCallback: (GLUTdialsCB)func { _dialFunc = func; }
- (void)setTabletMotionCallback: (GLUTtabletMotionCB)func { _tabletMotionFunc = func; }
- (void)setTabletButtonCallback: (GLUTtabletButtonCB)func { _tabletButtonFunc = func; }
- (void)setJoystickCallback: (GLUTjoystickCB)func pollInterval: (NSTimeInterval)delay
{
_joystickFunc = func; // set joystick function always
if ((delay > 0) && _joystickFunc)
_pollInterval = delay;
else
_pollInterval = 0;
if (_pollInterval) {
if (!_joyTimer)
_joyTimer = [NSTimer scheduledTimerWithTimeInterval: delay
target: self
selector: @selector(processJoystick:)
userInfo: 0
repeats: YES];
else
[_joyTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow:_pollInterval]];
} else {
[_joyTimer invalidate];
_joyTimer = nil;
}
}
- (void)setVisibilityCallback: (GLUTvisibilityCB)func { _visibilityFunc = func; }
- (GLUTvisibilityCB)visibilityCallback { return _visibilityFunc; }
- (void)setWMCloseCallback: (GLUTwmcloseCB)func
{
/* WM close func is only relevant for top-level windows on MacOS X */
if(!_flags.isSubwindow) {
_wmCloseFunc = func;
/* Enabled/disable window close button */
if(_wmCloseFunc == __glutDefaultWMClose)
[[[self window] standardWindowButton: NSWindowCloseButton] setEnabled: NO];
else
[[[self window] standardWindowButton: NSWindowCloseButton] setEnabled: YES];
}
}
- (GLUTwmcloseCB)wmCloseCallback { return _wmCloseFunc; }
- (void)setFortranCallback: (int)which callback: (void *)func
{
switch (which) {
case GLUT_FCB_DISPLAY:
_fdisplayFunc = (GLUTdisplayFCB) func;
break;
case GLUT_FCB_WMCLOSE:
_fwmcloseFunc = (GLUTwmcloseFCB) func;
break;
case GLUT_FCB_RESHAPE:
_freshapeFunc = (GLUTreshapeFCB) func;
break;
case GLUT_FCB_MOUSE:
_fmouseFunc = (GLUTmouseFCB) func;
break;
case GLUT_FCB_MOTION:
_fmotionFunc = (GLUTmotionFCB) func;
break;
case GLUT_FCB_PASSIVE:
_fpassiveMotionFunc = (GLUTpassiveFCB) func;
break;
case GLUT_FCB_ENTRY:
_fentryFunc = (GLUTentryFCB) func;
break;
case GLUT_FCB_KEYBOARD:
_fkeyDownFunc = (GLUTkeyboardFCB) func;
break;
case GLUT_FCB_KEYBOARD_UP:
_fkeyUpFunc = (GLUTkeyboardFCB) func;
break;
case GLUT_FCB_WINDOW_STATUS:
_fwindowStatusFunc = (GLUTwindowStatusFCB) func;
break;
case GLUT_FCB_VISIBILITY:
_fvisibilityFunc = (GLUTvisibilityFCB) func;
break;
case GLUT_FCB_SPECIAL:
_fspecialFunc = (GLUTspecialFCB) func;
break;
case GLUT_FCB_SPECIAL_UP:
_fspecialUpFunc = (GLUTspecialFCB) func;
break;
case GLUT_FCB_BUTTON_BOX:
_fbuttonBoxFunc = (GLUTbuttonBoxFCB) func;
break;
case GLUT_FCB_DIALS:
_fdialFunc = (GLUTdialsFCB) func;
break;
case GLUT_FCB_SPACE_MOTION:
_fspaceballMotionFunc = (GLUTspaceMotionFCB) func;
break;
case GLUT_FCB_SPACE_ROTATE:
_fspaceballRotateFunc = (GLUTspaceRotateFCB) func;
break;
case GLUT_FCB_SPACE_BUTTON:
_fspaceballButtonFunc = (GLUTspaceButtonFCB) func;
break;
case GLUT_FCB_TABLET_MOTION:
_ftabletMotionFunc = (GLUTtabletMotionFCB) func;
break;
case GLUT_FCB_TABLET_BUTTON:
_ftabletButtonFunc = (GLUTtabletButtonFCB) func;
break;
case GLUT_FCB_JOYSTICK:
_fjoystickFunc = (GLUTjoystickFCB) func;
break;
}
}
- (void *)getFortranCallback: (int)which;
{
switch (which) {
case GLUT_FCB_DISPLAY:
return (void*) _fdisplayFunc;
break;
case GLUT_FCB_WMCLOSE:
return (void*) _fwmcloseFunc;
break;
case GLUT_FCB_RESHAPE:
return (void*) _freshapeFunc;
break;
case GLUT_FCB_MOUSE:
return (void*) _fmouseFunc;
break;
case GLUT_FCB_MOTION:
return (void*) _fmotionFunc;
break;
case GLUT_FCB_PASSIVE:
return (void*) _fpassiveMotionFunc;
break;
case GLUT_FCB_ENTRY:
return (void*) _fentryFunc;
break;
case GLUT_FCB_KEYBOARD:
return (void*) _fkeyDownFunc;
break;
case GLUT_FCB_KEYBOARD_UP:
return (void*) _fkeyUpFunc;
break;
case GLUT_FCB_WINDOW_STATUS:
return (void*) _fwindowStatusFunc;
break;
case GLUT_FCB_VISIBILITY:
return (void*) _fvisibilityFunc;
break;
case GLUT_FCB_SPECIAL:
return (void*) _fspecialFunc;
break;
case GLUT_FCB_SPECIAL_UP:
return (void*) _fspecialUpFunc;
break;
case GLUT_FCB_BUTTON_BOX:
return (void*) _fbuttonBoxFunc;
break;
case GLUT_FCB_DIALS:
return (void*) _fdialFunc;
break;
case GLUT_FCB_SPACE_MOTION:
return (void*) _fspaceballMotionFunc;
break;
case GLUT_FCB_SPACE_ROTATE:
return (void*) _fspaceballRotateFunc;
break;
case GLUT_FCB_SPACE_BUTTON:
return (void*) _fspaceballButtonFunc;
break;
case GLUT_FCB_TABLET_MOTION:
return (void*) _ftabletMotionFunc;
break;
case GLUT_FCB_TABLET_BUTTON:
return (void*) _ftabletButtonFunc;
break;
case GLUT_FCB_JOYSTICK:
return (void*) _fjoystickFunc;
break;
default:
return nil;
break;
}
}
/////////////////////////////////////////////
#pragma mark -
#pragma mark Cursor
#pragma mark -
- (int)cursor
{
return _cursorID;
}
- (NSCursor *)_inheritedNativeCursor
{
if(!_nativeCursor) {
if(_flags.isSubwindow)
return [(GLUTView *)[self superview] _inheritedNativeCursor];
else
return [NSCursor arrowCursor];
}
return _nativeCursor;
}
- (void)resetCursorRects
{
if(!_nativeCursor) {
if(_cursorID == GLUT_CURSOR_INHERIT) {
_nativeCursor = [[self _inheritedNativeCursor] retain];
} else {
_nativeCursor = [__glutGetNativeCursor(_cursorID) retain];
}
}
[self addCursorRect: [self visibleRect] cursor: _nativeCursor];
}
- (void)_recursiveInvalidateCursorRectsWithWindow: (GLUTWindow *)aWindow
{
NSArray * childrens = [self subviews];
unsigned int i, count = [childrens count];
for(i = 0; i < count; i++) {
GLUTView * view = (GLUTView *)[childrens objectAtIndex: i];
if(view->_cursorID == GLUT_CURSOR_INHERIT) {
[view->_nativeCursor release];
view->_nativeCursor = nil;
[aWindow invalidateCursorRectsForView: view];
[view _recursiveInvalidateCursorRectsWithWindow: aWindow];
}
}
}
- (void)setCursor: (int)cursor
{
if(_cursorID != cursor) {
GLUTWindow * window = (GLUTWindow *) [self window];
[_nativeCursor release];
_nativeCursor = nil;
_cursorID = cursor;
[window invalidateCursorRectsForView: self];
[self _recursiveInvalidateCursorRectsWithWindow: window];
[window performSelector:@selector(resetCursorRects) withObject:nil afterDelay:0.0];
// need the above to work around some AppKit issue with setting cursors while inside cursor rects
// [[self window] resetCursorRects];
}
}
/////////////////////////////////////////////
#pragma mark -
#pragma mark Work Events
#pragma mark -
/**
* --- Temp fix for current sub-windowing scheme, full logic change for post Panther ---
*
* We need to clear and re-establish the surface of each (sub-)view which acts
* as a GLUT sub-window, because sub-windows (ONLY those) which are moved from
* one top-level window to another one fail to draw once they are re-inserted
* into the new window and told to draw themselves.
*/
- (void)_recursiveKickSurface
{
NSArray * subviews = [self subviews];
int i, count = [subviews count];
[[self openGLContext] clearDrawable];
[[self openGLContext] setView: self];
for(i = 0; i < count; i++)
[(GLUTView *)[subviews objectAtIndex: i] _recursiveKickSurface];
}
static GLUTWindow *__glutSwitchWindowFullscreenMode(GLUTWindow *window, NSRect frame, BOOL mode)
{
GLUTWindow * othWindow = nil;
GLUTView * view = (GLUTView *)[window contentView];
NSDictionary * args = nil;
int op = kGLUTMorphOperationFullscreen;
if(!mode) {
args = [NSDictionary dictionaryWithObject: [NSValue valueWithRect: frame]
forKey: GLUTWindowFrame];
op = kGLUTMorphOperationRegular;
}
UNMAKE_CURRENT();
othWindow = [[GLUTWindow windowByMorphingWindow: window
operation: op
arguments: args] retain];
[view _recursiveKickSurface];
if([window isVisible])
[othWindow makeKeyAndOrderFront: nil];
MAKE_CURRENT_WINDOW(view);
[window setReleasedWhenClosed: YES];
[window close];
return othWindow;
}
- (void)_updateTrackingRects: (NSNotification *)notification
{
NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
NSRect bounds = [self bounds];
[self removeTrackingRect: _trackingRectTag];
_flags.wasMouseInside = NSMouseInRect([self convertPoint: mouseLoc fromView: nil], bounds, YES);
_trackingRectTag = [self addTrackingRect: bounds owner: self userData: nil assumeInside: _flags.wasMouseInside];
}
/**
* Updates the current event mask so that it becomes equivalent to
* the event mask stored in _eventMask.
*/
- (void)_updateCurrentEventMask
{
BOOL forceEntry = NO;
// kPassiveMotionEvents
if((_curEventMask & kPassiveMotionEvents) ^ (_eventMask & kPassiveMotionEvents)) {
if((_eventMask & kPassiveMotionEvents) == kPassiveMotionEvents) {
// turn 'em on
[(GLUTWindow *) [self window] enableMouseMovedEvents];
} else {
// turn 'em off
[(GLUTWindow *) [self window] disableMouseMovedEvents];
/* Force recreation of any necessary tracking rectangle because
the AppKit forgets about them as soon as you enable the
generation of mouse moved events... */
if((_curEventMask & kEntryEvents) == kEntryEvents) {
[self removeTrackingRect: _trackingRectTag];
[self setPostsFrameChangedNotifications: NO];
[[NSNotificationCenter defaultCenter] removeObserver: self];
_trackingRectTag = 0;
forceEntry = YES;
}
}
}
// kEntryEvents
if(forceEntry || (_curEventMask & kEntryEvents) ^ (_eventMask & kEntryEvents)) {
if((_eventMask & kEntryEvents) == kEntryEvents) {
/* setup our tracking rect */
NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
NSRect bounds = [self bounds];
_flags.wasMouseInside = NSMouseInRect([self convertPoint: mouseLoc fromView: nil], bounds, YES);
_trackingRectTag = [self addTrackingRect: bounds
owner: self
userData: nil
assumeInside: _flags.wasMouseInside];
[self setPostsFrameChangedNotifications: YES];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(_updateTrackingRects:)
name: NSViewFrameDidChangeNotification
object: self];
}
} else {
if(_trackingRectTag) {
[self removeTrackingRect: _trackingRectTag];
[self setPostsFrameChangedNotifications: NO];
[[NSNotificationCenter defaultCenter] removeObserver: self];
_trackingRectTag = 0;
}
}
_curEventMask = _eventMask;
}
- (void)handleWorkEvent: (GLUTWorkEvent *)event
{
int workMask;
BOOL isSub = _flags.isSubwindow;
#if __GLUT_LOG_WORK_EVENTS
__glutPrintWorkMask(event, _winid, _eventMask); // dump the events to process
#endif
/* Capture work mask for work that needs to be done to this
window, then clear the window's work mask (excepting the
dummy work bit, see below). Then, process the captured
work mask. This allows callbacks in the processing the
captured work mask to set the window's work mask for
subsequent processing. */
workMask = event->workMask;
assert((workMask & GLUT_DUMMY_WORK) == 0);
/* Set the dummy work bit, clearing all other bits, to
indicate that the window is currently on the window work
list _and_ that the window's work mask is currently being
processed. This convinces __glutPostWorkEvent that this
window is on the work list still. */
event->workMask = GLUT_DUMMY_WORK;
/* Optimization: most of the time, the work to do is a
redisplay and not these other types of work. Check for
the following cases as a group to before checking each one
individually one by one. */
if(workMask & (GLUT_EVENT_MASK_WORK | GLUT_DEVICE_MASK_WORK | GLUT_CONFIGURE_WORK |
GLUT_COLORMAP_WORK | GLUT_MAP_WORK)) {
if(workMask & GLUT_MAP_WORK) {
/* Show / hide window */
if(isSub) {
NSMutableSet * views = [NSMutableSet set];
if(event->desiredMapState == kWithdrawnState) {
/* hide */
[[self superview] setNeedsDisplay: YES];
/* Remove us from the superview. Doing this is save because
the _glutViewList still retains us. */
_savedSuperview = (GLUTView *)[self superview];
[views unionSet: [self coveredViews]];
[views addObject: self];
[self removeFromSuperview];
[GLUTView evaluateVisibilityOfViews: views];
[self setShown: NO];
} else {
/* show */
if(_savedSuperview) {
GLUTList * list = &_savedSuperview->_allChildrens;
if(_siblings.pred == &list->head) {
/* We're the bottom most of all siblings */
[_savedSuperview addSubview: self positioned: NSWindowBelow relativeTo: nil];
} else {
/* We're somewhere in the middle of the stack or the top-most */
GLUTView * refView = _siblings.pred->obj;
[_savedSuperview addSubview: self positioned: NSWindowAbove relativeTo: refView];
}
_savedSuperview = nil;
[views unionSet: [self coveredViews]];
[self recursiveCollectViewsIntoSet: views];
[GLUTView evaluateVisibilityOfViews: views];
[self setShown: YES];
[self setNeedsDisplay: YES];
// ggs: fix for not initially accepting keyboard events.
// set subview to be first responder
[[self window] makeFirstResponder: self];
}
}
} else {
/* Use the persistent window here because [self window] will return
nil if we're currently miniaturized, but [self persistentWindow]
always returns our true GLUTWindow miniaturized or not. */
GLUTWindow * window = (GLUTWindow *) [self window];
switch(event->desiredMapState) {
case kWithdrawnState:
[window orderOut: nil];
break;
case kNormalState:
if([window isAffectedByFullscreenWindow])
[window setLevel: GLUT_FULLSCREEN_LEVEL];
if([window isMiniaturized])
[window deminiaturize: nil];
[window makeKeyAndOrderFront: nil];
break;
case kGameModeState:
[window setLevel: GLUT_GAMEMODE_LEVEL];
if([window isMiniaturized])
[window deminiaturize: nil];
[window makeKeyAndOrderFront: nil];
break;
case kIconicState:
/* Give our GLUTViews a chance to draw themselves so that
the miniaturization code is able to pick up a meaningful
OGL graphics and not just some randomly set pixels... */
[window display];
[window miniaturize: nil];
break;
}
}
}
if(workMask & GLUT_CONFIGURE_WORK) {
if(event->desiredConfMask & (CWWidth | CWHeight)) {
/* resize window */
NSSize Xsize = NSMakeSize(event->desiredWidth, event->desiredHeight);
Xsize = [self convertSizeFromBacking: Xsize];
NSMutableSet * views = [NSMutableSet set];
[views unionSet: [self coveredViews]];
if(isSub) {
NSSize size = [self frame].size;