1 unit CocoaUtils;
2
3 {$mode objfpc}{$H+}
4 {$modeswitch objectivec1}
5
6 interface
7
8 uses
9 MacOSAll, CocoaAll,
10 Types, LCLType;
11
12 type
13 { NSLCLDebugExtension }
14
15 NSLCLDebugExtension = objccategory(NSObject)
lclClassNamenull16 function lclClassName: shortstring; message 'lclClassName';
17 end;
18
19 const
20 NSNullRect : NSRect = (origin:(x:0; y:0); size:(width:0; height:0));
21
GetNSPointnull22 function GetNSPoint(x,y: single): NSPoint; inline;
23
GetCGRectnull24 function GetCGRect(x1, y1, x2, y2: Integer): CGRect;
CGRectToRectnull25 function CGRectToRect(const c: CGRect): TRect;
26
GetNSRectnull27 function GetNSRect(x, y, width, height: Integer): NSRect; inline;
RectToNSRectnull28 function RectToNSRect(const r: TRect): NSRect;
29
30 procedure NSToLCLRect(const ns: NSRect; var lcl: TRect); overload;
31 procedure NSToLCLRect(const ns: NSRect; ParentHeight: Single; var lcl: TRect); overload;
32
33 procedure LCLToNSRect(const lcl: TRect; var ns: NSRect); overload;
34 procedure LCLToNSRect(const lcl: TRect; ParentHeight: Single; var ns: NSRect); overload;
35
CreateParamsToNSRectnull36 function CreateParamsToNSRect(const params: TCreateParams): NSRect;
37
NSStringUtf8null38 function NSStringUtf8(s: PChar): NSString;
NSStringUtf8null39 function NSStringUtf8(const s: String): NSString;
NSStringToStringnull40 function NSStringToString(ns: NSString): String;
41
GetNSObjectViewnull42 function GetNSObjectView(obj: NSObject): NSView;
43 procedure AddViewToNSObject(ctrl: NSView; obj: NSObject);
44 procedure AddViewToNSObject(ctrl: NSView; obj: NSObject; X,Y: integer);
45
46 procedure SetNSText(text: NSText; const s: String); inline;
GetNSTextnull47 function GetNSText(text: NSText): string; inline;
48
49 procedure SetNSControlValue(c: NSControl; const S: String); inline;
GetNSControlValuenull50 function GetNSControlValue(c: NSControl): String; inline;
51
52 implementation
53
54 const
55 DEFAULT_CFSTRING_ENCODING = kCFStringEncodingUTF8;
56
CFStringToStrnull57 function CFStringToStr(AString: CFStringRef; Encoding: CFStringEncoding = DEFAULT_CFSTRING_ENCODING): String;
58 var
59 Str: Pointer;
60 StrSize: CFIndex;
61 StrRange: CFRange;
62 begin
63 if AString = nil then
64 begin
65 Result := '';
66 Exit;
67 end;
68
69 // Try the quick way first
70 Str := CFStringGetCStringPtr(AString, Encoding);
71 if Str <> nil then
72 Result := PChar(Str)
73 else
74 begin
75 // if that doesn't work this will
76 StrRange.location := 0;
77 StrRange.length := CFStringGetLength(AString);
78
79 CFStringGetBytes(AString, StrRange, Encoding,
80 Ord('?'), False, nil, 0, StrSize);
81 SetLength(Result, StrSize);
82
83 if StrSize > 0 then
84 CFStringGetBytes(AString, StrRange, Encoding,
85 Ord('?'), False, @Result[1], StrSize, StrSize);
86 end;
87 end;
88
89 function GetNSObjectView(obj: NSObject): NSView;
90 begin
91 Result:=nil;
92 if not Assigned(obj) then Exit;
93 if obj.isKindOfClass_(NSView) then Result:=NSView(obj)
94 else if obj.isKindOfClass_(NSWindow) then Result:=NSWindow(obj).contentView;
95 end;
96
97 procedure AddViewToNSObject(ctrl: NSView; obj: NSObject);
98 var
99 view : NSView;
100 begin
101 view:=GetNSObjectView(obj);
102 if not Assigned(view) then Exit;
103 view.addSubView(ctrl);
104 end;
105
106 procedure AddViewToNSObject(ctrl: NSView; obj: NSObject; X,Y: integer);
107 begin
108 AddViewToNSObject(ctrl, obj);
109 //SetViewFramePos(ctrl, x,y);
110 end;
111
112 function GetNSPoint(x, y: single): NSPoint;
113 begin
114 Result.x:=x;
115 Result.y:=y;
116 end;
117
118 function GetNSRect(x, y, width, height: Integer): NSRect;
119 begin
120 Result.origin.x:=x;
121 Result.origin.y:=y;
122 Result.size.width:=width;
123 Result.size.height:=height;
124 end;
125
126 function GetCGRect(x1, y1, x2, y2: Integer): CGRect;
127 begin
128 Result.origin.x:=x1;
129 Result.origin.y:=y1;
130 Result.size.width:=x2-x1;
131 Result.size.height:=y2-y1;
132 end;
133
134 function CGRectToRect(const c:CGRect):TRect;
135 begin
136 Result.Left:=round(c.origin.x);
137 Result.Top:=round(c.origin.y);
138 Result.Right:=round(c.origin.x+c.size.width);
139 Result.Bottom:=round(c.origin.y+c.size.height);
140 end;
141
142 function RectToNSRect(const r: TRect): NSRect;
143 begin
144 Result:=GetNSRect(r.Left,r.Top,r.Right-r.Left,r.Bottom-r.Top);
145 end;
146
147 procedure NSToLCLRect(const ns: NSRect; var lcl: TRect);
148 begin
149 lcl.Left:=round(ns.origin.x);
150 lcl.Top:=round(ns.origin.y);
151 lcl.Right:=round(ns.origin.x+ns.size.width);
152 lcl.Bottom:=round(ns.origin.y+ns.size.height);
153 end;
154
155 procedure NSToLCLRect(const ns: NSRect; ParentHeight: Single; var lcl: TRect);
156 begin
157 lcl.Left:=Round(ns.origin.x);
158 lcl.Top:=Round(ParentHeight-ns.size.height-ns.origin.y);
159 lcl.Right:=Round(ns.origin.x+ns.size.width);
160 lcl.Bottom:=Round(lcl.Top+ns.size.height);
161 end;
162
163 procedure LCLToNSRect(const lcl: TRect; var ns: NSRect); overload;
164 begin
165 ns.origin.x:=lcl.Left;
166 ns.origin.y:=lcl.Top;
167 ns.size.width:=lcl.Right-lcl.Left;
168 ns.size.height:=lcl.Bottom-lcl.Top;
169 end;
170
171 procedure LCLToNSRect(const lcl: TRect; ParentHeight: Single; var ns: NSRect); overload;
172 begin
173 ns.origin.x:=lcl.left;
174 ns.origin.y:=ParentHeight-(lcl.bottom-lcl.Top)-lcl.Top;
175 ns.size.width:=lcl.Right-lcl.Left;
176 ns.size.height:=lcl.Bottom-lcl.Top;
177 end;
178
179
180 function CreateParamsToNSRect(const params: TCreateParams): NSRect;
181 begin
182 with params do Result:=GetNSRect(X,Y,Width,Height);
183 end;
184
185 function NSStringUtf8(s: PChar): NSString;
186 var
187 cf : CFStringRef;
188 begin
189 {NSString and CFStringRef are interchangable}
190 cf:=CFStringCreateWithCString(nil, S, kCFStringEncodingUTF8);
191 Result:=NSString(cf);
192 end;
193
194 function NSStringUtf8(const s: String): NSString;
195 var
196 cf : CFStringRef;
197 begin
198 {NSString and CFStringRef are interchangable}
199 cf:=CFStringCreateWithCString(nil, Pointer(PChar(S)), kCFStringEncodingUTF8);
200 Result:=NSString(cf);
201 end;
202
203 function NSStringToString(ns: NSString): String;
204 begin
205 Result:=CFStringToStr(CFStringRef(ns));
206 end;
207
208 procedure SetNSText(text: NSText; const s: String); inline;
209 var
210 ns : NSString;
211 begin
212 if Assigned(text) then
213 begin
214 ns:=NSStringUTF8(s);
215 text.setString(ns);
216 ns.release;
217 end;
218 end;
219
220 function GetNSText(text: NSText): string; inline;
221 begin
222 if Assigned(text) then
223 Result := NSStringToString(text.string_)
224 else
225 Result:='';
226 end;
227
228 procedure SetNSControlValue(c: NSControl; const S: String); inline;
229 var
230 ns : NSString;
231 begin
232 if Assigned(c) then
233 begin
234 ns:=NSStringUtf8(S);
235 c.setStringValue(ns);
236 ns.release;
237 end;
238 end;
239
240 function GetNSControlValue(c: NSControl): String; inline;
241 begin
242 if Assigned(c) then
243 Result:=NSStringToString(c.stringValue)
244 else
245 Result:='';
246 end;
247
248
249 { NSLCLDebugExtension }
250
251 function NSLCLDebugExtension.lclClassName: shortstring;
252 begin
253 Result:=NSStringToString(self.className);
254 end;
255
256 initialization
257
258 end.
259
260