summaryrefslogtreecommitdiff
path: root/src/SFML/Window/OSXCocoa/WindowImplCocoa.mm
blob: 070194df06a909ab8802332c74c7f09577124025 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2008 Lucas Soltic (elmerod@gmail.com) and Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#import <SFML/Window/OSXCocoa/WindowImplCocoa.hpp>
#import <SFML/Window/OSXCocoa/WindowController.h>
#import <SFML/Window/OSXCocoa/AppController.h>
#import <SFML/Window/WindowStyle.hpp>
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <Cocoa/Cocoa.h>
#import <iostream>


@implementation NSWindow (SFML)
- (BOOL)canBecomeKeyWindow
{
	return YES;
}
@end


namespace sf
{
namespace priv
{


#define ONCE(make) \
{ static int __done = 0;\
if (!__done) {\
make;\
__done = 1;\
} }


enum {
	ConsumedEvent,
	UnusedEvent
};


////////////////////////////////////////////////////////////
/// Structure containing all the members I can't directly put in the class definition
/// because I would have to hide them in a #ifdef __OBJC__ block and the object
/// allocator would allocate space for it as it would be called from a C++ code
/// that wouldn't see these members
////////////////////////////////////////////////////////////
struct objc_members {
	WindowController *controller;
	NSWindow *window;
	NSOpenGLContext *context;
	NSOpenGLView *view;
};

////////////////////////////////////////////////////////////
/// Pointer to the shared OpenGL context
////////////////////////////////////////////////////////////
static NSOpenGLContext *sharedContext = nil;


////////////////////////////////////////////////////////////
/// Private function declarations
////////////////////////////////////////////////////////////
static NSWindow *		MakeWindow(WindowSettings& params, unsigned long style, VideoMode& mode, NSString *title);
static NSOpenGLContext *MakeOpenGLContext(WindowSettings& params);
static NSOpenGLView	*	MakeOpenGLView(NSWindow *window, NSOpenGLContext *context, WindowSettings& params);
static void				ConfigureWindow(NSWindow *window, NSOpenGLView *view, WindowController *controller);
static Key::Code		KeyForVirtualCode(unsigned short vCode);
static Key::Code		KeyForUnicode(unsigned short uniCode);
static bool				IsTextEvent(NSEvent *event);
static bool				MouseInside(NSWindow *window, NSView *view);


////////////////////////////////////////////////////////////
/// Default constructor
/// (creates a dummy window to provide a valid OpenGL context)
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa() :
members(NULL),
useKeyRepeat(false),
mouseIn(false),
wheelStatus(0.0f),
fullscreen(false),
fullscreenMode(0, 0, 0),
desktopMode(0, 0, 0)
{
	Initialize();
	
	// -- Ceylo --
    // We just want to have a valid support for an OpenGL context
	
	// So we create the OpenGL context
	WindowSettings params(0, 0, 0);
	members->context = MakeOpenGLContext(params);
	
	if (members->context != nil) {
		sharedContext = [members->context retain];
		
		// And we make it the current active OpenGL context
		SetActive();
	} else {
		error(__FILE__, __LINE__, "unable to make the main shared OpenGL context");
	}
}


////////////////////////////////////////////////////////////
/// Create the window implementation from an existing control
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa(WindowHandle Handle, WindowSettings& params) :
members(NULL),
useKeyRepeat(false),
mouseIn(false),
wheelStatus(0.0f),
fullscreen(false),
fullscreenMode(0, 0, 0),
desktopMode(0, 0, 0)
{
	Initialize();
	
	// Register ourselves for event handling
	[[AppController sharedController] registerWindow:this];
	
	// Make a WindowController to handle notifications
	members->controller = [[WindowController controllerWithWindow:this] retain];
	
	// Use existing window
	members->window = [static_cast <NSWindow *> (Handle) retain];
	
	if (members->window != nil) {		
		// We make the OpenGL context, associate it to the OpenGL view
		// and add the view to our window
		members->context = MakeOpenGLContext(params);
		
		if (members->context != nil) {
			members->view = MakeOpenGLView(members->window, members->context, params);
			
			if (members->view != nil) {
				// initial mouse state
				mouseIn = MouseInside(members->window, members->view);
				
				// Initialize myWidth and myHeight members from base class with the window size
				myWidth = (unsigned) [members->window frame].size.width;
				myHeight = (unsigned) [members->window frame].size.height;
			} else {
				error(__FILE__, __LINE__, "failed to make OpenGL view for public window");
			}
		} else {
			error(__FILE__, __LINE__, "failed to make OpenGL context for public window");
		}
	} else {
		error(__FILE__, __LINE__, "invalid imported window");
	}
}


////////////////////////////////////////////////////////////
/// Create the window implementation
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, WindowSettings& params) :
members(NULL),
useKeyRepeat(false),
mouseIn(false),
wheelStatus(0.0f),
fullscreen(WindowStyle & Style::Fullscreen),
fullscreenMode(0, 0, 0),
desktopMode(0, 0, 0)
{
	Initialize();
	
	// Make a WindowController to handle notifications
	members->controller = [[WindowController controllerWithWindow:this] retain];
	
    // Create a new window with given size, title and style
	// First we define some objects used for our window
	NSString *title = [NSString stringWithUTF8String:Title.c_str()];
	
	// We create the window
	members->window = MakeWindow(params, WindowStyle, Mode, title);
	
	
	if (members->window != nil) {
		members->context = MakeOpenGLContext(params);
		
		if (members->context != nil) {
			// We make the OpenGL context, associate it to the OpenGL view
			// and add the view to our window
			members->view = MakeOpenGLView(members->window, members->context, params);
			
			if (members->view != nil) {
				// Set observers and some window settings
				ConfigureWindow(members->window, members->view, members->controller);
				
				// initial mouse state
				mouseIn = MouseInside(members->window, members->view);
				
				// We set the myWidth and myHeight members to the correct values
				myWidth = Mode.Width;
				myHeight = Mode.Height;
				
				if (WindowStyle & Style::Fullscreen) {
					fullscreenMode = Mode;
				}
			} else {
				error(__FILE__, __LINE__, "failed to make OpenGL view for public window");
			}
		} else {
			error(__FILE__, __LINE__, "failed to make OpenGL context for public window");
		}
	} else {
		error(__FILE__, __LINE__, "failed to make window");
	}
}


////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
WindowImplCocoa::~WindowImplCocoa()
{
    // Destroy the OpenGL context, the window and every resource allocated by this class
	Show(false);
	
	[[NSNotificationCenter defaultCenter] removeObserver:members->window];
	[[NSNotificationCenter defaultCenter] removeObserver:members->view];
	[members->controller release];
	
	[sharedContext release];
	[members->context release];
	[members->view release];
	[members->window release];
	
	[[AppController sharedController] unregisterWindow:this];
	free (members);
}


////////////////////////////////////////////////////////////
/// Check if there's an active context on the current thread
////////////////////////////////////////////////////////////
bool WindowImplCocoa::IsContextActive()
{
	return ([NSOpenGLContext currentContext] != NULL);
}


////////////////////////////////////////////////////////////
/// Handle a Cocoa NSEvent
////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleEvent(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	int eventStatus = UnusedEvent;
	
	switch ([event type]) {
		case NSKeyDown:
			eventStatus = HandleKeyDown(eventPtr);
			break;
			
		case NSKeyUp:
			eventStatus = HandleKeyUp(eventPtr);
			break;
			
		case NSFlagsChanged:
			eventStatus = HandleModifierKey(eventPtr);
			break;
			
		case NSScrollWheel:
			eventStatus = HandleMouseWheel(eventPtr);
			break;
		
		case NSLeftMouseDown:
		case NSRightMouseDown:
			eventStatus = HandleMouseDown(eventPtr);
			break;
			
		case NSLeftMouseUp:
		case NSRightMouseUp:
			eventStatus = HandleMouseUp(eventPtr);
			break;
			
		case NSMouseMoved:
		case NSLeftMouseDragged:
		case NSRightMouseDragged:
		case NSOtherMouseDragged:
			eventStatus = HandleMouseMove(eventPtr);
			break;
			
		default:
			break;
	}
	
	if (eventStatus == UnusedEvent) {
		[NSApp sendEvent:event];
	}
}


////////////////////////////////////////////////////////////
/// Handle event sent by the default NSNotificationCenter
////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleNotifiedEvent(Event& event)
{
	// Set myWidth and myHeight to correct value if
	// window size changed
	switch (event.Type) {
		case Event::Resized:
			myWidth = event.Size.Width;
			myHeight = event.Size.Height;
			break;
			
		default:
			break;
	}
	
	// And send the event
	SendEvent(event);
}


////////////////////////////////////////////////////////////
/// Handle a key down event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleKeyDown(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	
	Event sfEvent;
	unichar chr = 0, rawchr = 0;
	unsigned mods = [event modifierFlags];
	
	if ([[event characters] length]) {
		chr = [[event characters] characterAtIndex:0];
		
		// Note : I got a crash (out of bounds exception) while typing so now I test...
		if ([[event charactersIgnoringModifiers] length])
			rawchr = [[event charactersIgnoringModifiers] characterAtIndex:0];

	}
	
	if (mods & NSCommandKeyMask) {
		// Application commands
		[NSApp sendEvent:event];
	}
	
	// User events
	
	if (!useKeyRepeat && [event isARepeat]) {
		return ConsumedEvent;
	}
	
	// Is it also a text event ?
	if (IsTextEvent(event)) {
		sfEvent.Type = Event::TextEntered;
		sfEvent.Text.Unicode = chr;
		
		SendEvent(sfEvent);
	}
	
	// Anyway it's also a KeyPressed event
	sfEvent.Type = Event::KeyPressed;
	
	// Get the keys
	if (Key::Code(0) == (sfEvent.Key.Code = KeyForUnicode(chr))) {
		sfEvent.Key.Code = KeyForVirtualCode([event keyCode]);
	}
	
	// Get the modifiers
	sfEvent.Key.Alt = mods & NSAlternateKeyMask;
	sfEvent.Key.Control = mods & NSControlKeyMask;
	sfEvent.Key.Shift = mods & NSShiftKeyMask;
	
	SendEvent(sfEvent);
	
	return ConsumedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a key up event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleKeyUp(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	
	Event sfEvent;
	unsigned mods = [event modifierFlags];
	unichar chr = 0;
	
	if ([[event characters] length]) {
		chr = [[event characters] characterAtIndex:0];
	}
	
	if (mods & NSCommandKeyMask) {
		[NSApp sendEvent:event];
	}
	
	sfEvent.Type = Event::KeyReleased;
	
	// Get the code
	if (Key::Code(0) == (sfEvent.Key.Code = KeyForUnicode(chr))) {
		sfEvent.Key.Code = KeyForVirtualCode([event keyCode]);
	}
	
	// Get the modifiers
	sfEvent.Key.Alt = mods & NSAlternateKeyMask;
	sfEvent.Key.Control = mods & NSControlKeyMask;
	sfEvent.Key.Shift = mods & NSShiftKeyMask;
	
	SendEvent(sfEvent);
	
	return ConsumedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a key modifier event [Command, Option, Control, Shift]
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleModifierKey(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	Event sfEvent;
	unsigned mods = [event modifierFlags];
	
	sfEvent.Type = Event::KeyPressed;
	sfEvent.Key.Code = KeyForVirtualCode([event keyCode]);
	
	sfEvent.Key.Alt = mods & NSAlternateKeyMask;
	sfEvent.Key.Control = mods & NSControlKeyMask;
	sfEvent.Key.Shift = mods & NSShiftKeyMask;
	
	if (!(mods & NSAlternateKeyMask) &&
		(sfEvent.Key.Code == Key::LAlt || sfEvent.Key.Code == Key::RAlt)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	if (!(mods & NSControlKeyMask) &&
		(sfEvent.Key.Code == Key::LControl || sfEvent.Key.Code == Key::RControl)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	if (!(mods & NSShiftKeyMask) &&
		(sfEvent.Key.Code == Key::LShift || sfEvent.Key.Code == Key::RShift)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	if (!(mods & NSCommandKeyMask) &&
		(sfEvent.Key.Code == Key::LSystem || sfEvent.Key.Code == Key::RSystem)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	SendEvent(sfEvent);
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a mouse down event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseDown(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	Event sfEvent;
	NSPoint loc = {0, 0}, relativeLoc = {0, 0};
	unsigned mods = [event modifierFlags];
	
	switch ([event type]) {
		case NSLeftMouseDown:
			sfEvent.Type = Event::MouseButtonPressed;
			
			if (mods & NSControlKeyMask) {
				sfEvent.MouseButton.Button = Mouse::Right;
			} else {
				sfEvent.MouseButton.Button = Mouse::Left;
			}
			
			// Get mouse position
			loc = [NSEvent mouseLocation];
			
			relativeLoc = [members->window convertScreenToBase:loc];
			relativeLoc.y = [[members->window contentView] frame].size.height - relativeLoc.y;
			
			sfEvent.MouseButton.X = (int) relativeLoc.x;
			sfEvent.MouseButton.Y = (int) relativeLoc.y;
			
			SendEvent(sfEvent);
			break;
			
		case NSRightMouseDown:
			sfEvent.Type = Event::MouseButtonPressed;
			sfEvent.MouseButton.Button = Mouse::Right;
			
			// Get mouse position
			loc = [NSEvent mouseLocation];
			
			relativeLoc = [members->window convertScreenToBase:loc];
			relativeLoc.y = [[members->window contentView] frame].size.height - relativeLoc.y;
			
			sfEvent.MouseButton.X = (int) relativeLoc.x;
			sfEvent.MouseButton.Y = (int) relativeLoc.y;
			
			SendEvent(sfEvent);
			break;
			
		default:
			break;
	}
	
	return UnusedEvent;
	
}


////////////////////////////////////////////////////////////
/// Handle a mouse up event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseUp(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	Event sfEvent;
	NSPoint loc = {0, 0}, relativeLoc = {0, 0};
	unsigned mods = [event modifierFlags];
	
	switch ([event type]) {
		case NSLeftMouseUp:
			sfEvent.Type = Event::MouseButtonReleased;
			
			if (mods & NSControlKeyMask) {
				sfEvent.MouseButton.Button = Mouse::Right;
			} else {
				sfEvent.MouseButton.Button = Mouse::Left;
			}
			
			// Get mouse position
			loc = [NSEvent mouseLocation];
			
			relativeLoc = [members->window convertScreenToBase:loc];
			relativeLoc.y = [[members->window contentView] frame].size.height - relativeLoc.y;
			
			sfEvent.MouseButton.X = (int) relativeLoc.x;
			sfEvent.MouseButton.Y = (int) relativeLoc.y;
			
			SendEvent(sfEvent);
			break;
			
		case NSRightMouseUp:
			sfEvent.Type = Event::MouseButtonReleased;
			sfEvent.MouseButton.Button = Mouse::Right;
			
			// Get mouse position
			loc = [NSEvent mouseLocation];
			relativeLoc = [members->window convertScreenToBase:loc];
			relativeLoc.y = [[members->window contentView] frame].size.height - relativeLoc.y;
			
			sfEvent.MouseButton.X = (int) relativeLoc.x;
			sfEvent.MouseButton.Y = (int) relativeLoc.y;
			
			SendEvent(sfEvent);
			break;
			
		default:
			break;
	}
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a mouse move event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseMove(void *eventPtr)
{
	Event sfEvent;
	NSPoint loc = {0, 0}, relativeLoc = {0, 0};
	
	loc = [NSEvent mouseLocation];
	relativeLoc = [members->window convertScreenToBase:loc];
	relativeLoc.y = [[members->window contentView] frame].size.height - relativeLoc.y;
	sfEvent.Type = Event::MouseMoved;
	
	sfEvent.MouseMove.X = (int) relativeLoc.x;
	sfEvent.MouseMove.Y = (int) relativeLoc.y;
	
	SendEvent(sfEvent);
	
	// MouseEntered and MouseLeft events
	if (MouseInside(members->window, members->view) && !mouseIn) {
		sfEvent.Type = Event::MouseEntered;
		mouseIn = true;
		SendEvent(sfEvent);
	} else if (!MouseInside(members->window, members->view) && mouseIn) {
		sfEvent.Type = Event::MouseLeft;
		mouseIn = false;
		SendEvent(sfEvent);
	}
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a mouse wheel event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseWheel(void *eventPtr)
{
	NSEvent *event = static_cast <NSEvent *> (eventPtr);
	
	wheelStatus += [event deltaY];
	
	if (wheelStatus > 1.0f || wheelStatus < -1.0f) {
		Event sfEvent;
		sfEvent.Type = Event::MouseWheelMoved;
		sfEvent.MouseWheel.Delta = (int)wheelStatus;
		SendEvent(sfEvent);
		
		wheelStatus -= (int)wheelStatus;
	}
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Return a pointer to the NSWindow object
////////////////////////////////////////////////////////////
void *WindowImplCocoa::CocoaWindow(void)
{
	return static_cast <void *> (members->window);
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::Display
////////////////////////////////////////////////////////////
void WindowImplCocoa::Display()
{
	[members->context flushBuffer];
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::ProcessEvents
////////////////////////////////////////////////////////////
void WindowImplCocoa::ProcessEvents()
{
	if (![NSApp isRunning])
		return;
	
	[[AppController sharedController] processEvents];
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::MakeActive
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetActive(bool Active) const
{
	if (Active) {
		if ([NSOpenGLContext currentContext] != members->context)
			[members->context makeCurrentContext];
	} else {
		if ([NSOpenGLContext currentContext] == members->context)
			[NSOpenGLContext clearCurrentContext];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::UseVerticalSync
////////////////////////////////////////////////////////////
void WindowImplCocoa::UseVerticalSync(bool Enabled)
{
	GLint enable = (Enabled) ? 1 : 0;
	[members->context setValues:&enable forParameter:NSOpenGLCPSwapInterval];
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::ShowMouseCursor
////////////////////////////////////////////////////////////
void WindowImplCocoa::ShowMouseCursor(bool flag)
{
	if (flag) {
		[NSCursor unhide];
	} else {
		[NSCursor hide];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::SetCursorPosition
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetCursorPosition(unsigned int Left, unsigned int Top)
{
	// Change the cursor position (Left and Top are relative to this window)
	NSPoint pos = NSMakePoint ((float) Left, (float) Top);
	
	// Flip for SFML window coordinate system
	pos.y = [members->window frame].size.height - pos.y;
	
	// Adjust for view reference instead of window
	pos.y -= [members->window frame].size.height - [members->view frame].size.height;
	
	// Convert to screen coordinates
	NSPoint absolute = [members->window convertBaseToScreen:pos];
	
	// Flip screen coodinates
	absolute.y = [[NSScreen mainScreen] frame].size.height - absolute.y;
	
	// Move cursor
	CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, CGPointMake(absolute.x, absolute.y));
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::SetPosition
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetPosition(int Left, int Top)
{
	if (!fullscreen) {
		// Change the window position
		Top = (int) [[members->window screen] frame].size.height - Top;
		[members->window setFrameTopLeftPoint:NSMakePoint(Left, Top)];
	}
}


////////////////////////////////////////////////////////////
/// /see WindowImpl::SetSize
///
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetSize(unsigned int Width, unsigned int Height)
{
	if (!fullscreen) {
		[members->window setFrame:NSMakeRect([members->window frame].origin.x,
											 [members->window frame].origin.y,
											 (float) Width, (float) Height)
		 display:YES];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::Show
////////////////////////////////////////////////////////////
void WindowImplCocoa::Show(bool State)
{
	if (State) {
		// Register ourselves for event handling
		[[AppController sharedController] registerWindow:this];
		
		if (fullscreen) {
			desktopMode = VideoMode::GetDesktopMode();
			[NSMenu setMenuBarVisible:NO];
			
			CFDictionaryRef bestMode = CGDisplayBestModeForParameters (CGMainDisplayID(),
																	   fullscreenMode.BitsPerPixel,
																	   fullscreenMode.Width,
																	   fullscreenMode.Height,
																	   NULL);
			
			CGDisplaySwitchToMode(CGMainDisplayID(), bestMode);
			[members->window center];
		}
		
		// Show the window
		[members->window makeKeyAndOrderFront:nil];
	} else {
		// Close the window
		[members->window close];
		
		if (fullscreen) {
			CFDictionaryRef bestMode = CGDisplayBestModeForParameters (CGMainDisplayID(),
																	   desktopMode.BitsPerPixel,
																	   desktopMode.Width,
																	   desktopMode.Height,
																	   NULL);
			
			CGDisplaySwitchToMode(CGMainDisplayID(), bestMode);
			[NSMenu setMenuBarVisible:YES];
		}
		
		// Unregister ourselves from the event handler
		[[AppController sharedController] unregisterWindow:this];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::EnableKeyRepeat
////////////////////////////////////////////////////////////
void WindowImplCocoa::EnableKeyRepeat(bool Enabled)
{
	useKeyRepeat = Enabled;
}


////////////////////////////////////////////////////////////
/// see WindowImpl::SetIcon
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetIcon(unsigned int Width, unsigned int Height, const Uint8* Pixels)
{
	// Nothing to do
}


////////////////////////////////////////////////////////////
/// Make some allocations and initializations
////////////////////////////////////////////////////////////
void WindowImplCocoa::Initialize(void)
{
	// Allocate mem for the private objc members
	members = new objc_members;
	if (!members) {
		error(__FILE__, __LINE__, "couldn't allocate private objc members structure");
	}
	bzero(members, sizeof(*members));
	
	// Needed to always have an autorelease pool as soon as application is launched
	[SharedAppController resetPool];
	
	// Register application if needed and launch it
	[SharedAppController runApplication];
}


////////////////////////////////////////////////////////////
/// Make the window
////////////////////////////////////////////////////////////
static NSWindow *MakeWindow(WindowSettings& params, unsigned long style, VideoMode& mode, NSString *title)
{
	NSWindow *window = nil;
	
	NSRect frame = NSMakeRect (0.0f, 0.0f, (float) mode.Width, (float) mode.Height);
	unsigned int mask = 0;
	
	// We grab options from WindowStyle and add them to our window mask
	if (style & Style::None || style & Style::Fullscreen) {
		mask |= NSBorderlessWindowMask;
		
		if (style & style & Style::Fullscreen) {
			// Check display mode and put new values in 'mode' if needed
			boolean_t exact = true;
			CFDictionaryRef properties = CGDisplayBestModeForParameters(kCGDirectMainDisplay, mode.BitsPerPixel,
																		mode.Width, mode.Height, &exact);
			
			if (!properties)
				return nil;
			
			if (exact == false) {
				CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayWidth),
								 kCFNumberIntType, &mode.Width);
				
				CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayHeight),
								 kCFNumberIntType, &mode.Height);
				
				CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayBitsPerPixel),
								 kCFNumberIntType, &mode.BitsPerPixel);
			}
		}
		
	} else {
		if (style & Style::Titlebar) {
			mask |= NSTitledWindowMask;
			mask |= NSMiniaturizableWindowMask;
		}
		
		if (style & Style::Resize) {
			mask |= NSTitledWindowMask;
			mask |= NSMiniaturizableWindowMask;
			mask |= NSResizableWindowMask;
		}
		
		if (style & Style::Close) {
			mask |= NSTitledWindowMask;
			mask |= NSClosableWindowMask;
			mask |= NSMiniaturizableWindowMask;
		}
	}
	
	// Now we make the window with the values we got
	// Note: defer flag set to NO to be able to use OpenGL in our window
	window = [[NSWindow alloc] initWithContentRect:frame
										 styleMask:mask
										   backing:NSBackingStoreBuffered
											 defer:NO];
	
	if (window != nil) {
		// We set title and window position
		[window setTitle:title];
		[window center];
	}
	
	return window;
}


////////////////////////////////////////////////////////////
/// Make the OpenGL pixel format from the given attributes
////////////////////////////////////////////////////////////
static NSOpenGLContext *MakeOpenGLContext(WindowSettings& params)
{
	NSOpenGLPixelFormat *pixFormat = nil;
	NSOpenGLContext *context = nil;
	unsigned idx = 0, samplesIdx = 0;
	
	// Attributes list
	NSOpenGLPixelFormatAttribute attribs[15] = {(NSOpenGLPixelFormatAttribute) 0};
	
	// Accelerated, double buffered
	attribs[idx++] = NSOpenGLPFAClosestPolicy;
	attribs[idx++] = NSOpenGLPFADoubleBuffer;
	attribs[idx++] = NSOpenGLPFAAccelerated;
	
	// windowed context
	attribs[idx++] = NSOpenGLPFAWindow;
	
	// Color size ; usually 32 bits per pixel
	attribs[idx++] = NSOpenGLPFAColorSize;
	attribs[idx++] = (NSOpenGLPixelFormatAttribute) VideoMode::GetDesktopMode().BitsPerPixel;
	
	// Z-buffer size
	attribs[idx++] = NSOpenGLPFADepthSize;
	attribs[idx++] = (NSOpenGLPixelFormatAttribute) params.DepthBits;
	
	// Stencil bits (I don't really know what's that...)
	attribs[idx++] = NSOpenGLPFAStencilSize;
	attribs[idx++] = (NSOpenGLPixelFormatAttribute) params.StencilBits;
	
	// Antialiasing settings
	if (params.AntialiasingLevel) {
		samplesIdx = idx;
		
		attribs[idx++] = NSOpenGLPFASamples;
		attribs[idx++] = (NSOpenGLPixelFormatAttribute) params.AntialiasingLevel;
		
		attribs[idx++] = NSOpenGLPFASampleBuffers;
		attribs[idx++] = (NSOpenGLPixelFormatAttribute) GL_TRUE;
	}
	
	pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	
	// If pixel format creation fails and antialiasing level is
	// greater than 2, we set it to 2.
	if (pixFormat == nil && params.AntialiasingLevel > 2) {
		std::cerr << "Failed to find a pixel format supporting " << params.AntialiasingLevel << " antialiasing levels ; trying with 2 levels" << std::endl;
		params.AntialiasingLevel = attribs[samplesIdx + 1] = (NSOpenGLPixelFormatAttribute) 2;
		
		pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	}
	
	// If pixel format creation fails and antialiasing is enabled,
	// we disable it.
	if (pixFormat == nil && params.AntialiasingLevel > 0) {
		std::cerr << "Failed to find a pixel format supporting antialiasing ; antialiasing will be disabled" << std::endl;
		attribs[samplesIdx] = (NSOpenGLPixelFormatAttribute) nil;
		
		pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	}
	
	if (pixFormat) {
		context = [[NSOpenGLContext alloc] initWithFormat:pixFormat
											 shareContext:[sharedContext retain]];
		
		// Grab the effective properties from our OpenGL context
		GLint tmpColorSize = 0, tmpDepthSize = 0, tmpStencilBits = 0, tmpAntialiasingLevel = 0;
		
		if (context) {
			[pixFormat getValues:&tmpColorSize
					forAttribute:NSOpenGLPFAColorSize
				forVirtualScreen:[context currentVirtualScreen]];
			
			[pixFormat getValues:&tmpDepthSize
					forAttribute:NSOpenGLPFADepthSize
				forVirtualScreen:[context currentVirtualScreen]];
			
			[pixFormat getValues:&tmpStencilBits
					forAttribute:NSOpenGLPFAStencilSize
				forVirtualScreen:[context currentVirtualScreen]];
			
			[pixFormat getValues:&tmpAntialiasingLevel
					forAttribute:NSOpenGLPFASamples
				forVirtualScreen:[context currentVirtualScreen]];
		}
		
		
		params.DepthBits = (unsigned) tmpDepthSize;
		params.StencilBits = (unsigned) tmpStencilBits;
		params.AntialiasingLevel = (unsigned) tmpAntialiasingLevel;
		
		[pixFormat release];
	}
	
	return context;
}


static NSOpenGLView	* MakeOpenGLView(NSWindow *window, NSOpenGLContext *context, WindowSettings& params)
{
	assert(window != nil);
	assert(context != nil);
	
	NSOpenGLView *view = nil;
	
	
	// We make the NSOpenGLView
	view = [[NSOpenGLView alloc] initWithFrame:[[window contentView] bounds]
								   pixelFormat:nil];
	
	if (view) {
		// We add the NSOpenGLView to the window
		[[window contentView] addSubview:view];
		
		[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
		[view clearGLContext];
		[view setOpenGLContext:context];
		[context setView:view];
	} else {
		error(__FILE__, __LINE__, "failed to make view");
	}
	
	return view;
}


static void ConfigureWindow(NSWindow *window, NSOpenGLView *view, WindowController *controller)
{
	assert(window != nil);
	assert(view != nil);
	assert(controller != nil);
	
	// We need to update the OpenGL view when it changes
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc addObserver:controller
		   selector:@selector(viewFrameDidChange:)
			   name:NSViewFrameDidChangeNotification
			 object:view];
	
	// We want to know when our window got the focus
	[nc addObserver:controller
		   selector:@selector(windowDidBecomeMain:)
			   name:NSWindowDidBecomeMainNotification
			 object:window];
	
	// We want to know when our window lost the focus
	[nc addObserver:controller
		   selector:@selector(windowDidResignMain:)
			   name:NSWindowDidResignMainNotification
			 object:window];
	
	// We want to know when the user closes the window
	[nc addObserver:controller
		   selector:@selector(windowWillClose:)
			   name:NSWindowWillCloseNotification
			 object:window];
	
	// I want to re-center the window if it's a full screen one and moved by Spaces
	[nc addObserver:controller
		   selector:@selector(windowDidMove:)
			   name:NSWindowDidMoveNotification
			 object:window];
	
	
	// Needed not to make application crash when releasing the window in our destructor
	// (I prefer to take control of everything :P)
	[window setReleasedWhenClosed:NO];
	[window setAcceptsMouseMovedEvents:YES];
}


////////////////////////////////////////////////////////////
/// Return the SFML key corresponding to a key code
////////////////////////////////////////////////////////////
static Key::Code KeyForVirtualCode(unsigned short vCode)
{
	static struct {
		unsigned short code;
		Key::Code sfKey;
	} virtualTable[] =
	{
		{0x35, Key::Escape},
		{0x31, Key::Space},
		{0x24, Key::Return},	// main Return key
		{0x4C, Key::Return},	// pav Return key
		{0x33, Key::Back},
		{0x30, Key::Tab},
		{0x74, Key::PageUp},
		{0x79, Key::PageDown},
		{0x77, Key::End},
		{0x73, Key::Home},
		{0x72, Key::Insert},
		{0x75, Key::Delete},
		{0x45, Key::Add},
		{0x4E, Key::Subtract},
		{0x43, Key::Multiply},
		{0x4B, Key::Divide},
		
		{0x7A, Key::F1}, {0x78, Key::F2}, {0x63, Key::F3},
		{0x76, Key::F4}, {0x60, Key::F5}, {0x61, Key::F6},
		{0x62, Key::F7}, {0x64, Key::F8}, {0x65, Key::F9},
		{0x6D, Key::F10}, {0x67, Key::F11}, {0x6F, Key::F12},
		{0x69, Key::F13}, {0x6B, Key::F14}, {0x71, Key::F15},
		
		{0x7B, Key::Left},
		{0x7C, Key::Right},
		{0x7E, Key::Up},
		{0x7D, Key::Down},
		
		{0x52, Key::Numpad0}, {0x53, Key::Numpad1}, {0x54, Key::Numpad2},
		{0x55, Key::Numpad3}, {0x56, Key::Numpad4}, {0x57, Key::Numpad5},
		{0x58, Key::Numpad6}, {0x59, Key::Numpad7}, {0x5B, Key::Numpad8},
		{0x5C, Key::Numpad9},
		
		{0x1D, Key::Num0}, {0x12, Key::Num1}, {0x13, Key::Num2},
		{0x14, Key::Num3}, {0x15, Key::Num4}, {0x17, Key::Num5},
		{0x16, Key::Num6}, {0x1A, Key::Num7}, {0x1C, Key::Num8},
		{0x19, Key::Num9},
		
		{0x3B, Key::LControl},	//< Left Ctrl
		{0x3A, Key::LAlt},		//< Left Option/Alt
		{0x37, Key::LSystem},	//< Left Command
		{0x38, Key::LShift},	//< Left Shift
		{0x3E, Key::RControl},	//< Right Ctrl
		{0x3D, Key::RAlt},		//< Right Option/Alt
		{0x36, Key::RSystem},	//< Right Command
		{0x3C, Key::RShift},	//< Right Shift
		
		{0x39, Key::Code(0)}	//< Caps Lock
	};
	
	Key::Code result = Key::Code(0);
	
	for (unsigned i = 0;virtualTable[i].code;i++) {
		if (virtualTable[i].code == vCode) {
			result = virtualTable[i].sfKey;
			break;
		}
	}
	
	return result;
}


////////////////////////////////////////////////////////////
/// Return the SFML key corresponding to a unicode code
////////////////////////////////////////////////////////////
static Key::Code KeyForUnicode(unsigned short uniCode)
{
	static struct {
		unsigned short character;
		Key::Code sfKey;
	} unicodeTable[] =
	{
		{'!', Key::Code(0)}, //< No Key for this code
		{'"', Key::Code(0)}, //< No Key for this code
		{'#', Key::Code(0)}, //< No Key for this code
		{'$', Key::Code(0)}, //< No Key for this code
		{'%', Key::Code(0)}, //< No Key for this code
		{'&', Key::Code(0)}, //< No Key for this code
		{'\'', Key::Quote},
		{'(', Key::Code(0)}, //< No Key for this code
		{')', Key::Code(0)}, //< No Key for this code
		{'*', Key::Multiply},
		{'+', Key::Add},
		{',', Key::Comma},
		{'-', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'.', Key::Period},
		{'/', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'0', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'1', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'2', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'3', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'4', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'5', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'6', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'7', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'8', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'9', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{':', Key::Code(0)}, //< No Key for this code
		{';', Key::SemiColon},
		{'<', Key::Code(0)}, //< No Key for this code
		{'=', Key::Equal},
		{'>', Key::Code(0)}, //< No Key for this code
		{'?', Key::Code(0)}, //< No Key for this code
		{'@', Key::Code(0)}, //< No Key for this code
		{'A', Key::A}, {'B', Key::B}, {'C', Key::C},
		{'D', Key::D}, {'E', Key::E}, {'F', Key::F},
		{'G', Key::G}, {'H', Key::H}, {'I', Key::I},
		{'J', Key::J}, {'K', Key::K}, {'L', Key::L},
		{'M', Key::M}, {'N', Key::N}, {'O', Key::O},
		{'P', Key::P}, {'Q', Key::Q}, {'R', Key::R},
		{'S', Key::S}, {'T', Key::T}, {'U', Key::U},
		{'V', Key::V}, {'W', Key::W}, {'X', Key::X},
		{'Y', Key::Y}, {'Z', Key::Z},
		{'[', Key::LBracket},
		{'\\', Key::BackSlash},
		{']', Key::RBracket},
		{'^', Key::Code(0)}, //< No Key for this code
		{'_', Key::Code(0)}, //< No Key for this code
		{'`', Key::Code(0)}, //< No Key for this code
		{'a', Key::A}, {'b', Key::B}, {'c', Key::C},
		{'d', Key::D}, {'e', Key::E}, {'f', Key::F},
		{'g', Key::G}, {'h', Key::H}, {'i', Key::I},
		{'j', Key::J}, {'k', Key::K}, {'l', Key::L},
		{'m', Key::M}, {'n', Key::N}, {'o', Key::O},
		{'p', Key::P}, {'q', Key::Q}, {'r', Key::R},
		{'s', Key::S}, {'t', Key::T}, {'u', Key::U},
		{'v', Key::V}, {'w', Key::W}, {'x', Key::X},
		{'y', Key::Y}, {'z', Key::Z},
		{'{', Key::Code(0)}, //< No Key for this code
		{'|', Key::Code(0)}, //< No Key for this code
		{'}', Key::Code(0)}, //< No Key for this code
		{'~', Key::Tilde},
		{0, Key::Code(0)}
	};
	
	Key::Code result = Key::Code(0);
	
	for (unsigned i = 0;unicodeTable[i].character;i++) {
		if (unicodeTable[i].character == uniCode) {
			result = unicodeTable[i].sfKey;
			break;
		}
	}
	
	return result;
}


////////////////////////////////////////////////////////////
/// Return whether 'ev' must be considered as a TextEntered event
////////////////////////////////////////////////////////////
static bool IsTextEvent(NSEvent *event)
{
	bool res = false;
	
	if (event && [event type] == NSKeyDown && [[event characters] length]) {
		unichar code = [[event characters] characterAtIndex:0];
		
		// Codes from 0xF700 to 0xF8FF are non text keys (see NSEvent.h)
		if (code < 0xF700 || code > 0xF8FF)
			res = true;
	}
	
	return res;
}

	
////////////////////////////////////////////////////////////
/// Return whether the mouse is on our OpenGL view
////////////////////////////////////////////////////////////
static bool MouseInside(NSWindow *window, NSView *view)
{
	bool res = false;
	
	if (window && view && [window isVisible]) {
		NSPoint relativeToWindow = [window mouseLocationOutsideOfEventStream];
		NSPoint relativeToView = [view convertPoint:relativeToWindow fromView:nil];
		
		if (NSPointInRect (relativeToView, [view bounds]))
			res = true;
	}
	
	return res;
}

} // namespace priv

} // namespace sf