1 /**
2 Dynamic bindings to the Appkit framework.
3 
4 Copyright: Guillaume Piolat 2016.
5 License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 */
7 module bindbc.cocoa.appkit;
8 
9 import std.string;
10 
11 import bindbc.cocoa.runtime;
12 import bindbc.cocoa.foundation;
13 import bindbc.cocoa.coreimage;
14 import bindbc.cocoa.dynloadcoregraphics;
15 
16 version(X86)
17     version = AnyX86;
18 version(X86_64)
19     version = AnyX86;
20 
21 
22 // free functions
23 extern (C) nothrow @nogc
24 {
25     alias bool function() pfNSApplicationLoad;
26     alias int function(int argc, const(char)** argv) pfNSApplicationMain;
27 }
28 
29 __gshared
30 {
31     pfNSApplicationLoad NSApplicationLoad;
32     pfNSApplicationMain NSApplicationMain;
33 }
34 
35 alias NSApplicationActivationPolicy = NSInteger;
36 enum : NSApplicationActivationPolicy
37 {
38    NSApplicationActivationPolicyRegular = 0,
39    NSApplicationActivationPolicyAccessory = 1,
40    NSApplicationActivationPolicyProhibited = 2
41 }
42 
43 struct NSApplication
44 {
45 nothrow @nogc:
46 
47     NSObject parent;
48     alias parent this;
49 
50     mixin NSObjectTemplate!(NSApplication, "NSApplication");
51 
52     public static NSApplication sharedApplication ()
53     {
54         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
55         id result = (cast(fun_t)objc_msgSend)(lazyClass!"NSApplication", sel!"sharedApplication");
56         return NSApplication(result);
57     }
58 
59     void setDelegate (id object)
60     {
61         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
62         (cast(fun_t)objc_msgSend)(_id, sel!"setDelegate:", object.id);
63     }
64 
65     void setActivationPolicy(NSApplicationActivationPolicy policy)
66     {
67         alias fun_t = extern(C) void function (id, SEL, NSApplicationActivationPolicy) nothrow @nogc;
68         (cast(fun_t)objc_msgSend)(_id, sel!"setActivationPolicy:", policy);
69     }
70 
71     void activateIgnoringOtherApps(BOOL b)
72     {
73         alias fun_t = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
74         (cast(fun_t)objc_msgSend)(_id, sel!"activateIgnoringOtherApps:", b);
75     }
76 
77     void run ()
78     {
79         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
80         (cast(fun_t)objc_msgSend)(_id, sel!"run");
81     }
82 
83     void stop (id sender)
84     {
85         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
86         (cast(fun_t)objc_msgSend)(_id, sel!"stop:", sender);
87     }
88 
89     void sendEvent(NSEvent event)
90     {
91         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
92         (cast(fun_t)objc_msgSend)(_id, sel!"sendEvent:", event._id);
93     }
94 }
95 
96 
97 // NSResponder
98 
99 struct NSResponder
100 {
101 nothrow @nogc:
102     NSObject parent;
103     alias parent this;
104 
105     mixin NSObjectTemplate!(NSResponder, "NSResponder");
106 }
107 
108 
109 // NSView
110 
111 alias NSBorderType = NSUInteger;
112 enum : NSBorderType
113 {
114     NSNoBorder     = 0,
115     NSLineBorder   = 1,
116     NSBezelBorder  = 2,
117     NSGrooveBorder = 3
118 }
119 
120 alias NSAutoresizingMaskOptions = NSUInteger;
121 enum : NSAutoresizingMaskOptions
122 {
123     NSViewNotSizable     = 0,
124     NSViewMinXMargin     = 1,
125     NSViewWidthSizable   = 2,
126     NSViewMaxXMargin     = 4,
127     NSViewMinYMargin     = 8,
128     NSViewHeightSizable  = 16,
129     NSViewMaxYMargin     = 32
130 }
131 
132 
133 struct NSView
134 {
135 nothrow @nogc:
136 
137     NSResponder parent;
138     alias parent this;
139 
140     mixin NSObjectTemplate!(NSView, "NSView");
141 
142     void initWithFrame(NSRect rect)
143     {
144         alias fun_t = extern(C) void function (id, SEL, NSRect) nothrow @nogc;
145         (cast(fun_t)objc_msgSend)(_id, sel!"initWithFrame:", rect);
146     }
147 
148     void addSubview(NSView subView)
149     {
150         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
151         (cast(fun_t)objc_msgSend)(_id, sel!"addSubview:", subView._id);
152     }
153 
154     void removeFromSuperview()
155     {
156         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
157         (cast(fun_t)objc_msgSend)(_id, sel!"removeFromSuperview");
158     }
159 
160     NSWindow window()
161     {
162         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
163         id result = (cast(fun_t)objc_msgSend)(_id, sel!"window");
164         return NSWindow(result);
165     }
166 
167     void setNeedsDisplayInRect(NSRect rect)
168     {
169         alias fun_t = extern(C) void function (id, SEL, NSRect) nothrow @nogc;
170         (cast(fun_t)objc_msgSend)(_id, sel!"setNeedsDisplayInRect:", rect);
171     }
172 
173     NSPoint convertPoint(NSPoint point, NSView view)
174     {
175         alias fun_t = extern(C) NSPoint function (id, const(SEL), NSPoint, id) nothrow @nogc;
176         return (cast(fun_t)objc_msgSend)(_id, sel!"convertPoint:fromView:", point, view._id);
177     }
178 
179     NSRect convertRect(NSRect rect, NSView view)
180     {
181         alias fun_t = extern(C) NSRect function (id, const(SEL), NSRect, id) nothrow @nogc;
182         version(AnyX86)
183             return (cast(fun_t)objc_msgSend_stret)(_id, sel!"convertRect:fromView:", rect, view._id);
184         else
185             return (cast(fun_t)objc_msgSend      )(_id, sel!"convertRect:fromView:", rect, view._id);
186     }
187 
188     NSRect frame()
189     {
190         alias fun_t = extern(C) NSRect function (id, const(SEL)) nothrow @nogc;
191         version(AnyX86)
192             return (cast(fun_t)objc_msgSend_stret)(_id, sel!"frame");
193         else
194             return (cast(fun_t)objc_msgSend      )(_id, sel!"frame");
195     }
196 
197     NSRect bounds()
198     {
199         alias fun_t = extern(C) NSRect function (id, const(SEL)) nothrow @nogc;
200         version(AnyX86)
201             return (cast(fun_t)objc_msgSend_stret)(_id, sel!"bounds");
202         else
203             return (cast(fun_t)objc_msgSend      )(_id, sel!"bounds");
204     }
205 
206     CALayer layer()
207     {
208         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
209         id result = (cast(fun_t)objc_msgSend)(_id, sel!"layer");
210         return CALayer(result);
211     }
212 
213     void setWantsLayer(BOOL value)
214     {
215         alias fun_t = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
216         (cast(fun_t)objc_msgSend)(_id, sel!"setWantsLayer:", value);
217     }
218 
219     void getRectsBeingDrawn(const(NSRect)** rects, NSInteger* count)
220     {
221         alias fun_t = extern(C) void function (id, SEL, const(NSRect)**, NSInteger*) nothrow @nogc;
222         (cast(fun_t)objc_msgSend)(_id, sel!"getRectsBeingDrawn:count:", rects, count);
223     }
224 
225     void setFrameSize(NSSize newSize)
226     {
227         alias fun_t = extern(C) void function (id, SEL, NSSize) nothrow @nogc;
228         (cast(fun_t)objc_msgSend)(_id, sel!"setFrameSize:", newSize);
229     }
230 
231     void addTrackingArea(NSTrackingArea trackingArea)
232     {
233         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
234         (cast(fun_t)objc_msgSend)(_id, sel!"addTrackingArea:", trackingArea._id);
235     }
236 
237     void removeTrackingArea(NSTrackingArea trackingArea)
238     {
239         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
240         (cast(fun_t)objc_msgSend)(_id, sel!"removeTrackingArea:", trackingArea._id);
241     }
242 }
243 
244 enum NSTrackingMouseEnteredAndExited = 1;
245 enum NSTrackingActiveAlways = 0x80;
246 
247 // NSTrackingArea
248 
249 struct NSTrackingArea
250 {
251 nothrow @nogc:
252 
253     NSObject parent;
254     alias parent this;
255 
256     mixin NSObjectTemplate!(NSTrackingArea, "NSTrackingArea");
257 
258     void initWithRect(NSRect rect, NSUInteger options, NSView owner, void* userData)
259     {
260         alias fun_t = extern(C) void function (id, SEL, NSRect, NSUInteger, id, void*) nothrow @nogc;
261         (cast(fun_t)objc_msgSend)(_id, sel!"initWithRect:options:owner:userInfo:", rect, options, owner._id, userData);
262     }
263 }
264 
265 __gshared
266 {
267     NSString kCAContentsFormatRGBA8Uint;
268 }
269 
270 
271 // CALayer
272 struct CALayer
273 {
274 nothrow @nogc:
275 
276     NSObject parent;
277     alias parent this;
278 
279     mixin NSObjectTemplate!(CALayer, "CALayer");
280 
281     void setDrawsAsynchronously(BOOL value)
282     {
283         alias fun_t = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
284         (cast(fun_t)objc_msgSend)(_id, sel!"setDrawsAsynchronously:", value);
285     }
286 
287     void setOpaque(BOOL value)
288     {
289         alias fun_t = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
290         (cast(fun_t)objc_msgSend)(_id, sel!"setOpaque:", value);
291     }
292 
293     void setContentsFormat(NSString fmt)
294     {
295         alias fun_t = extern(C) void function (id, SEL, id) nothrow @nogc;
296         (cast(fun_t)objc_msgSend)(_id, sel!"setContentsFormat:", fmt._id);
297     }
298 }
299 
300 
301 // NSWindow
302 
303 alias NSBackingStoreType = NSUInteger;
304 enum : NSBackingStoreType
305 {
306     NSBackingStoreRetained     = 0,
307     NSBackingStoreNonretained  = 1,
308     NSBackingStoreBuffered     = 2
309 }
310 
311 enum : NSUInteger
312 {
313    NSBorderlessWindowMask = 0,
314    NSTitledWindowMask = 1 << 0,
315    NSClosableWindowMask = 1 << 1,
316    NSMiniaturizableWindowMask = 1 << 2,
317    NSResizableWindowMask = 1 << 3,
318    NSTexturedBackgroundWindowMask = 1 << 8
319 }
320 
321 struct NSWindow
322 {
323 nothrow @nogc:
324 
325     NSResponder parent;
326     alias parent this;
327 
328     mixin NSObjectTemplate!(NSWindow, "NSWindow");
329 
330     void initWithContentRect(NSRect contentRect)
331     {
332         alias fun_t = extern(C) void function (id, SEL, NSRect) nothrow @nogc;
333         (cast(fun_t)objc_msgSend)(_id, sel!"initWithContentRect:", contentRect);
334     }
335 
336     void initWithContentRect(NSRect contentRect, NSUInteger windowStyle, NSBackingStoreType bufferingType, BOOL deferCreation)
337     {
338         alias fun_t = extern(C) void function (id, SEL, NSRect, NSUInteger, NSBackingStoreType, BOOL) nothrow @nogc;
339         (cast(fun_t)objc_msgSend)(_id, sel!"initWithContentRect:styleMask:backing:defer:", contentRect, windowStyle, bufferingType, deferCreation);
340     }
341 
342     NSView contentView()
343     {
344         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
345         id result = (cast(fun_t)objc_msgSend)(_id, sel!"contentView");
346         return NSView(result);
347     }
348 
349     void makeKeyAndOrderFront()
350     {
351         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
352         (cast(fun_t)objc_msgSend)(_id, sel!"makeKeyAndOrderFront:");
353     }
354 
355     void orderFront()
356     {
357         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
358         (cast(fun_t)objc_msgSend)(_id, sel!"orderFront:");
359     }
360 
361     bool makeFirstResponder(NSResponder responder)
362     {
363         alias fun_t = extern(C) BOOL function (id, SEL, id) nothrow @nogc;
364         BOOL result = (cast(fun_t)objc_msgSend)(_id, sel!"makeFirstResponder:", responder._id);
365         return result != NO;
366     }
367 
368     NSEvent nextEventMatchingMask(NSUInteger eventMask)
369     {
370         alias fun_t = extern(C) id function (id, SEL, NSUInteger) nothrow @nogc;
371         id result = (cast(fun_t)objc_msgSend)(_id, sel!"nextEventMatchingMask:", eventMask);
372         return NSEvent(result);
373     }
374 
375     NSPoint mouseLocationOutsideOfEventStream()
376     {
377         alias fun_t = extern(C) NSPoint function (id, SEL) nothrow @nogc;
378         return (cast(fun_t)objc_msgSend)(_id, sel!"mouseLocationOutsideOfEventStream");
379     }
380 
381     void setAcceptsMouseMovedEvents(bool b)
382     {
383         alias fun_t = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
384         (cast(fun_t)objc_msgSend)(_id, sel!"setAcceptsMouseMovedEvents:", b ? YES : NO);
385     }
386 
387     void center()
388     {
389         alias fun_t = extern(C) void function (id, const(SEL)) nothrow @nogc;
390         (cast(fun_t)objc_msgSend)(_id, sel!"center");
391     }
392 
393     void setIsVisible(BOOL value)
394     {
395         alias fun_p = extern(C) void function (id, SEL, BOOL) nothrow @nogc;
396         (cast(fun_p)objc_msgSend)(_id, sel!"setIsVisible:", value);
397     }
398 
399     void setTitle(NSString value)
400     {
401         alias fun_p = extern(C) void function (id, SEL, id) nothrow @nogc;
402         (cast(fun_p)objc_msgSend)(_id, sel!"setTitle:", value._id);
403     }
404 }
405 
406 
407 alias NSEventType = int;
408 enum : NSEventType
409 {
410     NSLeftMouseDown       = 1,
411     NSLeftMouseUp         = 2,
412     NSRightMouseDown      = 3,
413     NSRightMouseUp        = 4,
414     NSMouseMoved          = 5,
415     NSLeftMouseDragged    = 6,
416     NSRightMouseDragged   = 7,
417     NSMouseEntered        = 8,
418     NSMouseExited         = 9,
419     NSKeyDown             = 10,
420     NSKeyUp               = 11,
421     NSFlagsChanged        = 12,
422     NSAppKitDefined       = 13,
423     NSSystemDefined       = 14,
424     NSApplicationDefined  = 15,
425     NSPeriodic            = 16,
426     NSCursorUpdate        = 17,
427     NSScrollWheel         = 22,
428     NSTabletPoint         = 23,
429     NSTabletProximity     = 24,
430     NSOtherMouseDown      = 25,
431     NSOtherMouseUp        = 26,
432     NSOtherMouseDragged   = 27,
433     NSRotate              = 18,
434     NSBeginGesture        = 19,
435     NSEndGesture          = 20,
436     NSMagnify             = 30,
437     NSSwipe               = 31
438 }
439 
440 enum : NSUInteger
441 {
442     NSLeftMouseDownMask      = 1 << NSLeftMouseDown,
443     NSLeftMouseUpMask        = 1 << NSLeftMouseUp,
444     NSRightMouseDownMask     = 1 << NSRightMouseDown,
445     NSRightMouseUpMask       = 1 << NSRightMouseUp,
446     NSMouseMovedMask         = 1 << NSMouseMoved,
447     NSLeftMouseDraggedMask   = 1 << NSLeftMouseDragged,
448     NSRightMouseDraggedMask  = 1 << NSRightMouseDragged,
449     NSMouseEnteredMask       = 1 << NSMouseEntered,
450     NSMouseExitedMask        = 1 << NSMouseExited,
451     NSKeyDownMask            = 1 << NSKeyDown,
452     NSKeyUpMask              = 1 << NSKeyUp,
453     NSFlagsChangedMask       = 1 << NSFlagsChanged,
454     NSAppKitDefinedMask      = 1 << NSAppKitDefined,
455     NSSystemDefinedMask      = 1 << NSSystemDefined,
456     NSApplicationDefinedMask = 1 << NSApplicationDefined,
457     NSPeriodicMask           = 1 << NSPeriodic,
458     NSCursorUpdateMask       = 1 << NSCursorUpdate,
459     NSScrollWheelMask        = 1 << NSScrollWheel,
460     NSTabletPointMask        = 1 << NSTabletPoint,
461     NSTabletProximityMask    = 1 << NSTabletProximity,
462     NSOtherMouseDownMask     = 1 << NSOtherMouseDown,
463     NSOtherMouseUpMask       = 1 << NSOtherMouseUp,
464     NSOtherMouseDraggedMask  = 1 << NSOtherMouseDragged,
465     NSRotateMask             = 1 << NSRotate,
466     NSBeginGestureMask       = 1 << NSBeginGesture,
467     NSEndGestureMask         = 1 << NSEndGesture,
468     NSMagnifyMask            = 1 << NSMagnify,
469     NSSwipeMask              = 1 << NSSwipe,
470     NSAnyEventMask           = 0xffffffffU,
471 }
472 
473 /// Keycodes
474 enum : ushort
475 {
476     kVK_ANSI_A                    = 0x00,
477     kVK_ANSI_S                    = 0x01,
478     kVK_ANSI_D                    = 0x02,
479     kVK_ANSI_F                    = 0x03,
480     kVK_ANSI_H                    = 0x04,
481     kVK_ANSI_G                    = 0x05,
482     kVK_ANSI_Z                    = 0x06,
483     kVK_ANSI_X                    = 0x07,
484     kVK_ANSI_C                    = 0x08,
485     kVK_ANSI_V                    = 0x09,
486     kVK_ANSI_B                    = 0x0B,
487     kVK_ANSI_Q                    = 0x0C,
488     kVK_ANSI_W                    = 0x0D,
489     kVK_ANSI_E                    = 0x0E,
490     kVK_ANSI_R                    = 0x0F,
491     kVK_ANSI_Y                    = 0x10,
492     kVK_ANSI_T                    = 0x11,
493     kVK_ANSI_1                    = 0x12,
494     kVK_ANSI_2                    = 0x13,
495     kVK_ANSI_3                    = 0x14,
496     kVK_ANSI_4                    = 0x15,
497     kVK_ANSI_6                    = 0x16,
498     kVK_ANSI_5                    = 0x17,
499     kVK_ANSI_Equal                = 0x18,
500     kVK_ANSI_9                    = 0x19,
501     kVK_ANSI_7                    = 0x1A,
502     kVK_ANSI_Minus                = 0x1B,
503     kVK_ANSI_8                    = 0x1C,
504     kVK_ANSI_0                    = 0x1D,
505     kVK_ANSI_RightBracket         = 0x1E,
506     kVK_ANSI_O                    = 0x1F,
507     kVK_ANSI_U                    = 0x20,
508     kVK_ANSI_LeftBracket          = 0x21,
509     kVK_ANSI_I                    = 0x22,
510     kVK_ANSI_P                    = 0x23,
511     kVK_ANSI_L                    = 0x25,
512     kVK_ANSI_J                    = 0x26,
513     kVK_ANSI_Quote                = 0x27,
514     kVK_ANSI_K                    = 0x28,
515     kVK_ANSI_Semicolon            = 0x29,
516     kVK_ANSI_Backslash            = 0x2A,
517     kVK_ANSI_Comma                = 0x2B,
518     kVK_ANSI_Slash                = 0x2C,
519     kVK_ANSI_N                    = 0x2D,
520     kVK_ANSI_M                    = 0x2E,
521     kVK_ANSI_Period               = 0x2F,
522     kVK_ANSI_Grave                = 0x32,
523     kVK_ANSI_KeypadDecimal        = 0x41,
524     kVK_ANSI_KeypadMultiply       = 0x43,
525     kVK_ANSI_KeypadPlus           = 0x45,
526     kVK_ANSI_KeypadClear          = 0x47,
527     kVK_ANSI_KeypadDivide         = 0x4B,
528     kVK_ANSI_KeypadEnter          = 0x4C,
529     kVK_ANSI_KeypadMinus          = 0x4E,
530     kVK_ANSI_KeypadEquals         = 0x51,
531     kVK_ANSI_Keypad0              = 0x52,
532     kVK_ANSI_Keypad1              = 0x53,
533     kVK_ANSI_Keypad2              = 0x54,
534     kVK_ANSI_Keypad3              = 0x55,
535     kVK_ANSI_Keypad4              = 0x56,
536     kVK_ANSI_Keypad5              = 0x57,
537     kVK_ANSI_Keypad6              = 0x58,
538     kVK_ANSI_Keypad7              = 0x59,
539     kVK_ANSI_Keypad8              = 0x5B,
540     kVK_ANSI_Keypad9              = 0x5C
541 }
542 
543 /// Keycodes for keys that are independent of keyboard layout.
544 enum : ushort
545 {
546     kVK_Return                    = 0x24,
547     kVK_Tab                       = 0x30,
548     kVK_Space                     = 0x31,
549     kVK_Delete                    = 0x33,
550     kVK_Escape                    = 0x35,
551     kVK_Command                   = 0x37,
552     kVK_Shift                     = 0x38,
553     kVK_CapsLock                  = 0x39,
554     kVK_Option                    = 0x3A,
555     kVK_Control                   = 0x3B,
556     kVK_RightShift                = 0x3C,
557     kVK_RightOption               = 0x3D,
558     kVK_RightControl              = 0x3E,
559     kVK_Function                  = 0x3F,
560     kVK_F17                       = 0x40,
561     kVK_VolumeUp                  = 0x48,
562     kVK_VolumeDown                = 0x49,
563     kVK_Mute                      = 0x4A,
564     kVK_F18                       = 0x4F,
565     kVK_F19                       = 0x50,
566     kVK_F20                       = 0x5A,
567     kVK_F5                        = 0x60,
568     kVK_F6                        = 0x61,
569     kVK_F7                        = 0x62,
570     kVK_F3                        = 0x63,
571     kVK_F8                        = 0x64,
572     kVK_F9                        = 0x65,
573     kVK_F11                       = 0x67,
574     kVK_F13                       = 0x69,
575     kVK_F16                       = 0x6A,
576     kVK_F14                       = 0x6B,
577     kVK_F10                       = 0x6D,
578     kVK_F12                       = 0x6F,
579     kVK_F15                       = 0x71,
580     kVK_Help                      = 0x72,
581     kVK_Home                      = 0x73,
582     kVK_PageUp                    = 0x74,
583     kVK_ForwardDelete             = 0x75,
584     kVK_F4                        = 0x76,
585     kVK_End                       = 0x77,
586     kVK_F2                        = 0x78,
587     kVK_PageDown                  = 0x79,
588     kVK_F1                        = 0x7A,
589     kVK_LeftArrow                 = 0x7B,
590     kVK_RightArrow                = 0x7C,
591     kVK_DownArrow                 = 0x7D,
592     kVK_UpArrow                   = 0x7E
593 }
594 
595 /// ISO keyboards only.
596 enum : ushort
597 {
598   kVK_ISO_Section               = 0x0A
599 }
600 
601 ///JIS keyboards only.
602 enum : ushort
603 {
604   kVK_JIS_Yen                   = 0x5D,
605   kVK_JIS_Underscore            = 0x5E,
606   kVK_JIS_KeypadComma           = 0x5F,
607   kVK_JIS_Eisu                  = 0x66,
608   kVK_JIS_Kana                  = 0x68
609 }
610 
611 alias NSEventModifierFlags = int;
612 enum : NSEventModifierFlags
613 {
614    NSAlphaShiftKeyMask = 1 << 16,
615    NSShiftKeyMask      = 1 << 17,
616    NSControlKeyMask    = 1 << 18,
617    NSAlternateKeyMask  = 1 << 19,
618    NSCommandKeyMask    = 1 << 20,
619    NSNumericPadKeyMask = 1 << 21,
620    NSHelpKeyMask       = 1 << 22,
621    NSFunctionKeyMask   = 1 << 23,
622    NSDeviceIndependentModifierFlagsMask = 0xffff0000U
623 }
624 
625 struct NSEvent
626 {
627 nothrow @nogc:
628 
629     NSObject parent;
630     alias parent this;
631 
632     mixin NSObjectTemplate!(NSEvent, "NSEvent");
633 
634     NSWindow window()
635     {
636         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
637         id result = (cast(fun_t)objc_msgSend)(_id, sel!"window");
638         return NSWindow(result);
639     }
640 
641     NSEventType type()
642     {
643         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
644         id result = (cast(fun_t)objc_msgSend)(_id, sel!"type");
645         return cast(NSEventType)result;
646     }
647 
648     int clickCount()
649     {
650         alias fun_t = extern(C) NSInteger function (id, SEL) nothrow @nogc;
651         return cast(int)( (cast(fun_t)objc_msgSend)(_id, sel!"clickCount") );
652     }
653 
654     int buttonNumber()
655     {
656         alias fun_t = extern(C) NSInteger function (id, SEL) nothrow @nogc;
657         return cast(int)( (cast(fun_t)objc_msgSend)(_id, sel!"buttonNumber") );
658     }
659 
660     uint pressedMouseButtons()
661     {
662         alias fun_t = extern(C) NSUInteger function (id, SEL) nothrow @nogc;
663         return cast(uint)( (cast(fun_t)objc_msgSend)(getClassID(), sel!"pressedMouseButtons") );
664     }
665 
666     NSEventModifierFlags modifierFlags()
667     {
668         alias fun_t = extern(C) NSEventModifierFlags function (id, SEL) nothrow @nogc;
669         return cast(uint)( (cast(fun_t)objc_msgSend)(_id, sel!"modifierFlags") );
670     }
671 
672     NSPoint mouseLocation()
673     {
674         alias fun_t = extern(C) NSPoint function (id, SEL) nothrow @nogc;
675         return (cast(fun_t)objc_msgSend)(getClassID(), sel!"mouseLocation");
676     }
677 
678     double deltaX()
679     {
680         alias fun_t = extern(C) double function (id, SEL) nothrow @nogc;
681         version(X86)
682             return (cast(fun_t)objc_msgSend_fpret)(_id, sel!"deltaX");
683         else version(X86_64)
684             return (cast(fun_t)objc_msgSend)(_id, sel!"deltaX");
685         else
686             return (cast(fun_t)objc_msgSend)(_id, sel!"deltaX");
687     }
688 
689     double deltaY()
690     {
691         alias fun_t = extern(C) double function (id, SEL) nothrow @nogc;
692         version(X86)
693             return (cast(fun_t)objc_msgSend_fpret)(_id, sel!"deltaY");
694         else version(X86_64)
695             return (cast(fun_t)objc_msgSend)(_id, sel!"deltaY");
696         else
697             return (cast(fun_t)objc_msgSend)(_id, sel!"deltaY");
698     }
699 
700     ushort keyCode()
701     {
702         alias fun_t = extern(C) ushort function (id, SEL) nothrow @nogc;
703         return (cast(fun_t)objc_msgSend)(_id, sel!"keyCode");
704     }
705 
706     NSString charactersIgnoringModifiers()
707     {
708         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
709         auto id = (cast(fun_t)objc_msgSend)(_id, sel!"charactersIgnoringModifiers");
710         return NSString(id);
711     }
712 
713     NSPoint locationInWindow()
714     {
715         alias fun_t = extern(C) NSPoint function (id, SEL) nothrow @nogc;
716         fun_t fun = cast(fun_t)objc_msgSend;
717         SEL sel = sel!"locationInWindow";
718         return fun(_id, sel);
719     }
720 }
721 
722 struct NSGraphicsContext
723 {
724 nothrow @nogc:
725     NSObject parent;
726     alias parent this;
727 
728     mixin NSObjectTemplate!(NSGraphicsContext, "NSGraphicsContext");
729 
730     static NSGraphicsContext currentContext()
731     {
732         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
733         id result = (cast(fun_t)objc_msgSend)(getClassID(), sel!"currentContext");
734         return NSGraphicsContext(result);
735     }
736 
737     void saveGraphicsState()
738     {
739         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
740         (cast(fun_t)objc_msgSend)(_id, sel!"saveGraphicsState");
741     }
742 
743     void restoreGraphicsState()
744     {
745         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
746         (cast(fun_t)objc_msgSend)(_id, sel!"restoreGraphicsState");
747     }
748 
749     bool flipped()
750     {
751         alias fun_t = extern(C) BOOL function (id, SEL) nothrow @nogc;
752         return (cast(fun_t)objc_msgSend)(_id, sel!"flipped") != NO;
753     }
754 
755     CIContext getCIContext()
756     {
757         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
758         id result = (cast(fun_t)objc_msgSend)(_id, sel!"CIContext");
759         return CIContext(result);
760     }
761 
762     CGContextRef getCGContext()
763     {
764         alias fun_t = extern(C) CGContextRef function (id, SEL) nothrow @nogc;
765         CGContextRef result = (cast(fun_t)objc_msgSend)(_id, sel!"CGContext");
766         return result;
767     }
768 }
769 
770 struct NSColorSpace
771 {
772 nothrow @nogc:
773 
774     NSObject parent;
775     alias parent this;
776 
777     mixin NSObjectTemplate!(NSColorSpace, "NSColorSpace");
778 
779     static NSColorSpace sRGBColorSpace()
780     {
781         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
782         return NSColorSpace( (cast(fun_t)objc_msgSend)(getClassID(), sel!"sRGBColorSpace") );
783     }
784 
785     // Should the NSColorSpace outlive the returned reference? Documentation says nothing.
786     CGColorSpaceRef CGColorSpace()
787     {
788         alias fun_t = extern(C) CGColorSpaceRef function (id, SEL) nothrow @nogc;
789         return (cast(fun_t)objc_msgSend)(_id, sel!"CGColorSpace");
790     }
791 }
792 
793 struct NSCursor
794 {
795 nothrow @nogc:
796 
797     NSObject parent;
798     alias parent this;
799 
800     mixin NSObjectTemplate!(NSCursor, "NSCursor");
801 
802     static NSCursor arrowCursor()
803     {
804         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
805         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"arrowCursor") );
806     }
807 
808     static NSCursor crosshairCursor()
809     {
810         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
811         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"crosshairCursor") );
812     }
813 
814     static NSCursor pointingHandCursor()
815     {
816         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
817         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"pointingHandCursor") );
818     }
819 
820     static NSCursor openHandCursor()
821     {
822         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
823         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"openHandCursor") );
824     }
825 
826     static NSCursor closedHandCursor()
827     {
828         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
829         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"closedHandCursor") );
830     }
831 
832     static NSCursor resizeLeftRightCursor()
833     {
834         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
835         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"resizeLeftRightCursor") );
836     }
837 
838     static NSCursor resizeUpDownCursor()
839     {
840         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
841         return NSCursor( (cast(fun_t)objc_msgSend)(getClassID(), sel!"resizeUpDownCursor") );
842     }
843 
844     static void hide()
845     {
846         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
847         (cast(fun_t)objc_msgSend)(getClassID(), sel!"hide");
848     }
849 
850     static void unhide()
851     {
852         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
853         (cast(fun_t)objc_msgSend)(getClassID(), sel!"unhide");
854     }
855 
856     static void pop()
857     {
858         alias fun_t = extern(C) id function (id, SEL) nothrow @nogc;
859         (cast(fun_t)objc_msgSend)(getClassID(), sel!"pop");
860     }
861 
862     void push()
863     {
864         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
865         objc_msgSend(_id, sel!"push");
866     }
867 
868     void set()
869     {
870         alias fun_t = extern(C) void function (id, SEL) nothrow @nogc;
871         objc_msgSend(_id, sel!"set");
872     }
873 }