1 #ifndef _CtrlCore_CocoMM_h_
2 #define _CtrlCore_CocoMM_h_
3
4 #include <Core/config.h>
5
6 #ifdef PLATFORM_COCOA
7
8 #define Point NS_Point
9 #define Rect NS_Rect
10 #define Size NS_Size
11 #include <AppKit/AppKit.h>
12 #undef Point
13 #undef Rect
14 #undef Size
15
16 #include "CtrlCore.h"
17
18 namespace Upp {
19
20 template <class T>
21 struct CFRef {
22 T ptr;
23 T operator~() { return ptr; }
TCFRef24 operator T() { return ptr; }
25 T operator->() { return ptr; }
DetachCFRef26 T Detach() { T h = ptr; ptr = NULL; return h; }
CFRefCFRef27 CFRef(T p) { ptr = p; }
~CFRefCFRef28 ~CFRef() { if(ptr) CFRelease(ptr); }
29 };
30
31 struct AutoreleasePool {
32 NSAutoreleasePool *pool;
33
AutoreleasePoolAutoreleasePool34 AutoreleasePool() {
35 pool = [[NSAutoreleasePool alloc] init];
36 }
~AutoreleasePoolAutoreleasePool37 ~AutoreleasePool() {
38 [pool release];
39 }
40 };
41
42 CGImageRef createCGImage(const Image& img);
43
44 NSImage *GetNSImage(const Image& img);
45
46 WString ToWString(CFStringRef s);
47 String ToString(CFStringRef s);
48
ToWString(NSString * s)49 inline WString ToWString(NSString *s) { return ToWString((CFStringRef)s); }
ToString(NSString * s)50 inline String ToString(NSString *s) { return ToString((CFStringRef)s); }
51
GetCG(SystemDraw & w)52 inline CGContextRef GetCG(SystemDraw& w) { return (CGContextRef)w.GetCGHandle(); }
53
54 #define cgHandle (CGContextRef)handle
55
56 struct PointCG {
57 int x, y;
58
CGPointPointCG59 operator CGPoint() const { return CGPointMake(x, y); }
60
PointCGPointCG61 PointCG(int x, int y) : x(x), y(y) {}
62 PointCG();
63 };
64
65 struct RectCG {
66 int x, y, cx, cy;
67
CGRectRectCG68 operator CGRect() const { return CGRectMake(x, y, cx, cy); }
69
RectCGRectCG70 RectCG(int x, int y, int cx, int cy) : x(x), y(y), cx(cx), cy(cy) {}
71 RectCG();
72 };
73
74 }
75
76 @interface CocoView : NSView <NSWindowDelegate>
77 {
78 @public
79 Upp::Ptr<Upp::Ctrl> ctrl;
80 }
81 @end
82
83 @interface CocoWindow : NSWindow
84 {
85 @public
86 Upp::Ptr<Upp::Ctrl> ctrl;
87 bool active;
88 }
89 @end
90
91 struct Upp::MMCtrl {
92 static void SyncRect(CocoView *view);
93 };
94
95 struct Upp::CocoTop {
96 CocoWindow *window = NULL;
97 CocoView *view = NULL;
98 Ptr<Ctrl> owner;
99 };
100
101 void SyncRect(CocoView *view);
102
MakeRect(const CGRect & r,int dpi)103 inline Upp::Rect MakeRect(const CGRect& r, int dpi) {
104 return Upp::RectC(dpi * r.origin.x, dpi * r.origin.y, dpi * r.size.width, dpi * r.size.height);
105 }
106
CGRectDPI(const Upp::Rect & r)107 inline CGRect CGRectDPI(const Upp::Rect& r) {
108 if(Upp::IsUHDMode())
109 return CGRectMake(0.5 * r.left, 0.5 * r.top, 0.5 * r.GetWidth(), 0.5 * r.GetHeight());
110 else
111 return CGRectMake(r.left, r.top, r.GetWidth(), r.GetHeight());
112 }
113
114 #endif
115
116 #endif
117