ASPiK SDK
cocoahelpers.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __cocoahelpers__
6 #define __cocoahelpers__
7 
8 #import "../../../crect.h"
9 #import "../../../cpoint.h"
10 #import "../../../ccolor.h"
11 
12 #if MAC_COCOA && defined (__OBJC__)
13 
14 #import <objc/runtime.h>
15 #import <objc/message.h>
16 #import <Cocoa/Cocoa.h>
17 struct VstKeyCode;
18 
19 #define HIDDEN __attribute__((__visibility__("hidden")))
20 
21 #if DEBUG
22 #define VSTGUI_CHECK_YES(x) { BOOL res = x; vstgui_assert (res == YES); }
23 #else
24 #define VSTGUI_CHECK_YES(x) x;
25 #endif
26 
27 //------------------------------------------------------------------------------------
28 inline HIDDEN id get_Objc_Value (id obj, const char* name)
29 {
30  Ivar ivar = class_getInstanceVariable ([obj class], name);
31  if (ivar)
32  {
33  id value = object_getIvar (obj, ivar);
34  return value;
35  }
36  return nil;
37 }
38 
39 //------------------------------------------------------------------------------------
40 inline HIDDEN void set_Objc_Value (id obj, const char* name, id value)
41 {
42  Ivar ivar = class_getInstanceVariable ([obj class], name);
43  if (ivar)
44  {
45  object_setIvar (obj, ivar, value);
46  }
47 }
48 
49 #define __OBJC_SUPER(x) objc_super __os; __os.receiver = x; __os.super_class = class_getSuperclass ([x class]);
50 #define SUPER &__os
51 #define OBJC_GET_VALUE(x,y) get_Objc_Value (x, #y)
52 #define OBJC_SET_VALUE(x,y,z) set_Objc_Value (x, #y, (id)z)
53 
54 extern HIDDEN Class generateUniqueClass (NSMutableString* className, Class baseClass);
55 extern HIDDEN VstKeyCode CreateVstKeyCodeFromNSEvent (NSEvent* theEvent);
56 extern HIDDEN NSString* GetVirtualKeyCodeString (int32_t virtualKeyCode);
57 extern HIDDEN int32_t eventButton (NSEvent* theEvent);
58 extern HIDDEN void convertPointToGlobal (NSView* view, NSPoint& p);
59 
60 //------------------------------------------------------------------------------------
61 // Helpers
62 //------------------------------------------------------------------------------------
63 HIDDEN inline NSRect nsRectFromCRect (const VSTGUI::CRect& rect)
64 {
65  NSRect r;
66  r.origin.x = static_cast<CGFloat> (rect.left);
67  r.origin.y = static_cast<CGFloat> (rect.top);
68  r.size.width = static_cast<CGFloat> (rect.getWidth ());
69  r.size.height = static_cast<CGFloat> (rect.getHeight ());
70  return r;
71 }
72 
73 //------------------------------------------------------------------------------------
74 HIDDEN inline NSPoint nsPointFromCPoint (const VSTGUI::CPoint& point)
75 {
76  NSPoint p = { static_cast<CGFloat>(point.x), static_cast<CGFloat>(point.y) };
77  return p;
78 }
79 
80 //------------------------------------------------------------------------------------
81 HIDDEN inline VSTGUI::CRect rectFromNSRect (const NSRect& rect)
82 {
83  VSTGUI::CRect r (rect.origin.x, rect.origin.y, 0, 0);
84  r.setWidth (rect.size.width);
85  r.setHeight (rect.size.height);
86  return r;
87 }
88 
89 //------------------------------------------------------------------------------------
90 HIDDEN inline VSTGUI::CPoint pointFromNSPoint (const NSPoint& point)
91 {
92  VSTGUI::CPoint p (point.x, point.y);
93  return p;
94 }
95 
96 //------------------------------------------------------------------------------------
97 HIDDEN inline NSColor* nsColorFromCColor (const VSTGUI::CColor& color)
98 {
99  return [NSColor colorWithDeviceRed:static_cast<CGFloat> (color.red/255.) green:static_cast<CGFloat> (color.green/255.) blue:static_cast<CGFloat> (color.blue/255.) alpha:static_cast<CGFloat> (color.alpha/255.)];
100 }
101 
102 //------------------------------------------------------------------------------------
103 HIDDEN inline NSImage* imageFromCGImageRef (CGImageRef image)
104 {
105  return [[NSImage alloc] initWithCGImage:image size:NSZeroSize];
106 }
107 
108 //------------------------------------------------------------------------------------
109 struct MacEventModifier
110 {
111  enum mask
112  {
113 #ifdef MAC_OS_X_VERSION_10_12
114  ShiftKeyMask = NSEventModifierFlagShift,
115  CommandKeyMask = NSEventModifierFlagCommand,
116  AlternateKeyMask = NSEventModifierFlagOption,
117  ControlKeyMask = NSEventModifierFlagControl
118 #else
119  ShiftKeyMask = NSShiftKeyMask,
120  CommandKeyMask = NSCommandKeyMask,
121  AlternateKeyMask = NSAlternateKeyMask,
122  ControlKeyMask = NSControlKeyMask
123 #endif
124  };
125 };
126 
127 //------------------------------------------------------------------------------------
128 namespace MacEventType
129 {
130 #ifdef MAC_OS_X_VERSION_10_12
131  static constexpr auto LeftMouseDown = ::NSEventTypeLeftMouseDown;
132  static constexpr auto LeftMouseDragged = ::NSEventTypeLeftMouseDragged;
133  static constexpr auto MouseMoved = ::NSEventTypeMouseMoved;
134 #else
135  static constexpr auto LeftMouseDown = ::NSLeftMouseDown;
136  static constexpr auto LeftMouseDragged = ::NSLeftMouseDragged;
137  static constexpr auto MouseMoved = ::NSMouseMoved;
138 #endif
139 }
140 
141 //------------------------------------------------------------------------------------
142 namespace MacWindowStyleMask
143 {
144 #ifdef MAC_OS_X_VERSION_10_12
145  static constexpr auto Borderless = ::NSWindowStyleMaskBorderless;
146  static constexpr auto Titled = ::NSWindowStyleMaskTitled;
147  static constexpr auto Resizable = ::NSWindowStyleMaskResizable;
148  static constexpr auto Miniaturizable = ::NSWindowStyleMaskMiniaturizable;
149  static constexpr auto Closable = ::NSWindowStyleMaskClosable;
150  static constexpr auto Utility = ::NSWindowStyleMaskUtilityWindow;
151 #else
152  static constexpr auto Borderless = ::NSBorderlessWindowMask;
153  static constexpr auto Titled = ::NSTitledWindowMask;
154  static constexpr auto Resizable = ::NSResizableWindowMask;
155  static constexpr auto Miniaturizable = ::NSMiniaturizableWindowMask;
156  static constexpr auto Closable = ::NSClosableWindowMask;
157  static constexpr auto Utility = ::NSUtilityWindowMask;
158 #endif
159 }
160 
161 #endif // MAC_COCOA
162 #endif // __cocoahelpers__
Rect structure.
Definition: crect.h:17
Definition: vstkeycode.h:12
RGBA Color structure.
Definition: ccolor.h:15
uint8_t blue
blue component [0..255]
Definition: ccolor.h:97
uint8_t green
green component [0..255]
Definition: ccolor.h:96
uint8_t alpha
alpha component [0..255]
Definition: ccolor.h:98
uint8_t red
red component [0..255]
Definition: ccolor.h:95
Point structure.
Definition: cpoint.h:17