forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjc.lcb
More file actions
613 lines (469 loc) · 19.8 KB
/
objc.lcb
File metadata and controls
613 lines (469 loc) · 19.8 KB
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
/* Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
This module provides utility handlers for converting to and from Obj-C types.
*/
module com.livecode.objc
use com.livecode.foreign
/****/
public foreign type ObjcObject binds to "MCObjcObjectTypeInfo"
public foreign type ObjcId binds to "MCObjcIdTypeInfo"
public foreign type ObjcRetainedId binds to "MCObjcRetainedIdTypeInfo"
public foreign type ObjcAutoreleasedId binds to "MCObjcAutoreleasedIdTypeInfo"
/****/
private foreign handler MCStringCreateWithCFStringRef(in pNSString as ObjcId, out rString as String) returns CBool binds to "MCStringCreateWithCFStringRef"
private foreign handler MCStringConvertToCFStringRef(in pString as String, out rNSString as ObjcRetainedId) returns CBool binds to "MCStringConvertToCFStringRef"
/**/
/**
Summary: Convert a String into an Objective-C string
Parameters:
pString: The String to convert
Returns:
A Objective-C object of type NSString
Description
Use <StringToNSString> to convert a variable of type String to an Objective-C
string object (NSString).
*/
public handler StringToNSString(in pString as String) returns ObjcObject
variable tNSString as ObjcRetainedId
unsafe
MCStringConvertToCFStringRef(pString, tNSString)
end unsafe
return tNSString
end handler
/**
Summary: Convert a Objective-C string into a String
Parameters:
pObjcString: The NSString to convert
Returns:
A String value
Description
Use <StringFromNSString> to convert an Objective-C string object (NSString) to
a variable of type String.
*/
public handler StringFromNSString(in pObjcString as ObjcObject) returns String
variable tString as String
unsafe
MCStringCreateWithCFStringRef(pObjcString, tString)
end unsafe
return tString
end handler
/****/
private foreign handler MCDataCreateWithCFDataRef(in pNSData as ObjcId, out rData as Data) returns CBool binds to "MCDataCreateWithCFDataRef"
private foreign handler MCDataConvertToCFDataRef(in pData as Data, out rNSData as ObjcRetainedId) returns CBool binds to "MCDataConvertToCFDataRef"
/**/
/**
Summary: Convert a Data into an Objective-C data
Parameters:
pData: The Data to convert
Returns:
A Objective-C object of type NSData
Description
Use <DataToNSData> to convert a variable of type Data to an Objective-C
data object (NSData).
*/
public handler DataToNSData(in pData as Data) returns ObjcObject
variable tNSData as ObjcRetainedId
unsafe
MCDataConvertToCFDataRef(pData, tNSData)
end unsafe
return tNSData
end handler
/**
Summary: Convert a Objective-C data into a Data
Parameters:
pObjcData: The NSData to convert
Returns:
A Data value
Description
Use <DataFromNSData> to convert an Objective-C data object (NSData) to
a variable of type Data.
*/
public handler DataFromNSData(in pObjcData as ObjcObject) returns Data
variable tData as Data
unsafe
MCDataCreateWithCFDataRef(pObjcData, tData)
end unsafe
return tData
end handler
/****/
private foreign handler MCNumberCreateWithCFNumberRef(in pNSNumber as ObjcId, out rNumber as Number) returns CBool binds to "MCNumberCreateWithCFNumberRef"
private foreign handler MCNumberConvertToCFNumberRef(in pNumber as Number, out rNSNumber as ObjcRetainedId) returns CBool binds to "MCNumberConvertToCFNumberRef"
/**/
/**
Summary: Convert a Number into an Objective-C number
Parameters:
pNumber: The Number to convert
Returns:
A Objective-C object of type NSNumber
Description
Use <NumberToNSNumber> to convert a variable of type Number to an Objective-C
number object (NSNumber).
*/
public handler NumberToNSNumber(in pNumber as Number) returns ObjcObject
variable tNSNumber as ObjcRetainedId
unsafe
MCNumberConvertToCFNumberRef(pNumber, tNSNumber)
end unsafe
return tNSNumber
end handler
/**
Summary: Convert a Objective-C number into a Number
Parameters:
pObjcNumber: The NSNumber to convert
Returns:
A Number value
Description
Use <NumberFromNSNumber> to convert an Objective-C number object (NSNumber) to
a variable of type Number.
*/
public handler NumberFromNSNumber(in pObjcNumber as ObjcObject) returns Number
variable tNumber as Number
unsafe
MCNumberCreateWithCFNumberRef(pObjcNumber, tNumber)
end unsafe
return tNumber
end handler
/****/
private foreign handler MCProperListCreateWithCFArrayRef(in pNSArray as ObjcId, in pUseLists as CBool, out rList as List) returns CBool binds to "MCProperListCreateWithCFArrayRef"
private foreign handler MCProperListConvertToCFArrayRef(in pList as List, in pUseLists as CBool, out rNSArray as ObjcRetainedId) returns CBool binds to "MCProperListConvertToCFArrayRef"
/**/
/**
Summary: Convert a List into an Objective-C array
Parameters:
pNumber: The List to convert
Returns:
A Objective-C object of type NSArray
Description
Use <ListToNSArray> to convert a variable of type List to an Objective-C
array object (NSArray).
*/
public handler ListToNSArray(in pList as List) returns ObjcObject
variable tNSArray as ObjcRetainedId
unsafe
MCProperListConvertToCFArrayRef(pList, true, tNSArray)
end unsafe
return tNSArray
end handler
/**
Summary: Convert a Objective-C array into a List
Parameters:
pObjcNumber: The NSArray to convert
Returns:
A Number value
Description
Use <ListFromNSArray> to convert an Objective-C array object (NSArray) to
a variable of type List.
*/
public handler ListFromNSArray(in pObjcArray as ObjcObject) returns List
variable tList as List
unsafe
MCProperListCreateWithCFArrayRef(pObjcArray, true, tList)
end unsafe
return tList
end handler
/****/
private foreign handler MCArrayCreateWithCFDictionaryRef(in pNSDictionary as ObjcId, in pUseLists as CBool, out rArray as Array) returns CBool binds to "MCArrayCreateWithCFDictionaryRef"
private foreign handler MCArrayConvertToCFDictionaryRef(in pArray as Array, in pUseLists as CBool, out rNSDictionary as ObjcRetainedId) returns CBool binds to "MCArrayConvertToCFDictionaryRef"
/**
Summary: Convert an Array into an Objective-C dictionary
Parameters:
pNumber: The Array to convert
Returns:
A Objective-C object of type NSDictionary
Description
Use <ArrayToNSDictionary> to convert a variable of type Array to an Objective-C
dictionary object (NSDictionary).
*/
public handler ArrayToNSDictionary(in pArray as Array) returns ObjcObject
variable tNSArray as ObjcRetainedId
unsafe
MCArrayConvertToCFDictionaryRef(pArray, true, tNSArray)
end unsafe
return tNSArray
end handler
/**
Summary: Convert a Objective-C dictionary into a Array
Parameters:
pObjcNumber: The NSDictionary to convert
Returns:
An Array value
Description
Use <ArrayFromNSDictionary> to convert an Objective-C dictionary object (NSDictionary) to
a variable of type Array.
*/
public handler ArrayFromNSDictionary(in pObjcArray as ObjcObject) returns Array
variable tArray as Array
unsafe
MCArrayCreateWithCFDictionaryRef(pObjcArray, true, tArray)
end unsafe
return tArray
end handler
/****/
public handler type ObjcActionProxyHandler(in pSender as ObjcObject, in pContext as optional any) returns nothing
private foreign handler MCObjcObjectCreateActionProxy(in pHandler as optional any, in pValue as optional any) returns ObjcObject binds to "MCObjcObjectCreateActionProxy"
private foreign handler MCObjcObjectGetActionProxySelector() returns UIntPtr binds to "MCObjcObjectGetActionProxySelector"
public handler ObjcProxyGetTarget(in pHandler as ObjcActionProxyHandler, in pContext as optional any) returns ObjcObject
unsafe
return MCObjcObjectCreateActionProxy(pHandler, pContext)
end unsafe
end handler
public handler ObjcProxyGetAction() returns UIntPtr
unsafe
return MCObjcObjectGetActionProxySelector()
end unsafe
end handler
/**/
/****/
private foreign handler _MCObjcObjectCreateWithId_AsPointer(in pPointer as optional Pointer, out rObject as ObjcObject) returns CBool binds to "MCObjcObjectCreateWithId"
private foreign handler _MCObjcObjectGetId_AsPointer(in pObject as ObjcObject) returns optional Pointer binds to "MCObjcObjectGetId"
/**/
/**
Summary: Convert a Pointer into an ObjcObject
Parameters:
pPointer: The Pointer to convert
Returns:
An ObjcObject wrapping the Pointer
Description:
Use <PointerToObjcObject> to convert a variable of type Pointer to one of type
ObjcObject.
*/
public handler PointerToObjcObject(in pPointer as optional Pointer) returns optional ObjcObject
variable tObject as optional ObjcObject
unsafe
_MCObjcObjectCreateWithId_AsPointer(pPointer, tObject)
end unsafe
return tObject
end handler
/**
Summary: Convert an ObjcObject into a Pointer
Parameters:
pObjcObject: The ObjcObject to convert
Returns:
The Pointer wrapped by the ObjcObject
Description:
Use <PointerFromObjcObject> to convert a variable of type ObjcObject to one of
type Pointer.
*/
public handler PointerFromObjcObject(in pObjcObject as ObjcObject) returns optional Pointer
variable tPointer as Pointer
unsafe
put _MCObjcObjectGetId_AsPointer(pObjcObject) into tPointer
end unsafe
return tPointer
end handler
/****/
private foreign handler _MCObjcCreateDelegate(in pProtocol as String, in pHandlerMapping as Array, out rDelegate as ObjcObject) returns CBool \
binds to "MCObjcCreateDelegate"
/**
Create an Objective-C object with LCB implementations of methods of a protocol.
Example:
handler ControlIsValidObjectCallback(in pControl as ObjcId, in pObject as ObjcId) returns CSChar
return 1
end handler
public handler GetNSControlTextEditingDelegate() returns ObjcObject
return CreateObjcDelegate("NSControlTextEditingDelegate", \
{ "control:isValidObject:": \
ControlIsValidObjectCallback })
end handler
Parameters:
pProtocol: The name of the protocol
pHandlerMapping: A mapping from the protocol's selector names to LCB handlers
Returns: An Objective-C object which calls the appropriate LCB handler
on receiving any of the specified selectors.
Description:
Use the <CreateObjcDelegate> handler to create instances of Objective-C
delegate classes with LCB implementations of protocol methods. Once
created these can be set in the usual way on an instance of the
appropriate class (by binding to `-setDelegate:`), typically so that
callbacks triggered by user interaction with a widget can be handled in
LCB.
If any context is required to be passed as a parameter to the callback,
use <CreateObjcDelegateWithContext>.
Some protocols consist of purely optional methods. In this case the
information about the protocol's methods are not available from the
objective-c runtime API. In this situation <CreateObjcInformalDelegate>
should be used instead.
References:
CreateObjcDelegateWithContext (handler),
CreateObjcInformalDelegate (handler),
CreateObjcInformalDelegateWithContext (handler)
*/
public handler CreateObjcDelegate(in pProtocol as String, in pHandlerMapping as Array) returns optional ObjcObject
unsafe
variable tObj as ObjcObject
if _MCObjcCreateDelegate(pProtocol, pHandlerMapping, tObj) then
return tObj
end if
return nothing
end unsafe
end handler
private foreign handler _MCObjcCreateDelegateWithContext(in pProtocol as String, in pHandlerMapping as Array, in pContext as optional any, out rDelegate as ObjcObject) returns CBool \
binds to "MCObjcCreateDelegateWithContext"
/**
Create an Objective-C object with LCB implementations of methods of a protocol.
Example:
handler NumberOfItemsInMenuCallback(in pContext as String, in pMenu as ObjcId) returns CLong
-- pContext is the tag for this menu
return the number of elements in mMenuArray[pContext]
end handler
public handler GetNSMenuDelegate(in pMenuTag as String) returns ObjcObject
return CreateObjcDelegateWithContext("NSMenuDelegate", \
{ "numberOfItemsInMenu:": \
NumberOfItemsInMenuCallback }, \
pMenuTag)
end handler
Parameters:
pProtocol: The name of the protocol
pHandlerMapping: A mapping from the protocol's selector names to LCB handlers
pContext: Any context required in the callback.
Returns: An Objective-C object which calls the appropriate LCB handler
on receiving any of the specified selectors.
Description:
Use the <CreateObjcDelegateWithContext> handler to create instances of
Objective-C delegate classes with LCB implementations of protocol
methods. Once created these can be set in the usual way on an instance
of the appropriate class (by binding to `-setDelegate:`), typically so
that callbacks triggered by user interaction with a widget can be
handled in LCB.
If no context is required to be passed as a parameter to the callback,
use <CreateObjcDelegate>.
Some protocols consist of purely optional methods. In this case the
information about the protocol's methods are not available from the
objective-c runtime API. In this situation
<CreateObjcInformalDelegateWithContext> should be used instead.
References:
CreateObjcDelegate (handler), CreateObjcInformalDelegate (handler),
CreateObjcInformalDelegateWithContext (handler)
*/
public handler CreateObjcDelegateWithContext(in pProtocol as String, in pHandlerMapping as Array, in pContext as any) returns optional ObjcObject
unsafe
variable tObj as ObjcObject
if _MCObjcCreateDelegateWithContext(pProtocol, pHandlerMapping, \
pContext, tObj) then
return tObj
end if
return nothing
end unsafe
end handler
private foreign handler _MCObjcCreateInformalDelegate(in pProtocol as List, in pHandlerMapping as Array, out rDelegate as ObjcObject) returns CBool \
binds to "MCObjcCreateInformalDelegate"
/**
Create an Objective-C object with LCB implementations of methods of an informal protocol.
Example:
foreign handler Objc_NSPortMessageGetMsgId(in pMessage as ObjcId) returns UInt32 \
binds to "objc:NSPort.-msgid"
handler HandlePortMessageCallback(in pMessage as ObjcId) returns nothing
post "portMessage" with [Objc_NSPortMessageGetMsgId(pMessage)]
end handler
foreign handler Objc_NSPortDelegateHandlePortMessage(in pTarget as ObjcId, in pPortMessage as ObjcId) returns nothing \
binds to "objc:.-handlePortMessage:"
public handler GetNSPortDelegate()
return CreateObjcInformalDelegate([Objc_NSPortDelegateHandlePortMessage], \
{ "handlePortMessage:": \
HandlePortMessageCalback })
end handler
Parameters:
pProtocol: A list of foreign handlers which bind to protocol methods
pHandlerMapping: A mapping from the protocol's selector names to LCB handlers
Returns: An Objective-C object which calls the appropriate LCB handler
on receiving any of the specified selectors.
Description:
Use the <CreateObjcInformalDelegate> handler to create instances of
Objective-C delegate classes with LCB implementations of protocol
methods, when the Protocol object cannot be found at runtime. This
occurs for example when all of the protocol methods are optional.
Instead of a protocol name (as with <CreateObjcDelegate>), the
<pProtocol> argument of <CreateObjcInformalDelegate> must be a proper
list of foreign handlers for each of the methods of the protocol for
which LCB callbacks are provided in the <pHandlerMapping> array.
Once created an informal delegate can be set in the usual way on an
instance of the appropriate class (by binding to `-setDelegate:`),
typically so that callbacks triggered by user interaction with a widget
can be handled in LCB.
If any context is required to be passed as a parameter to the callback,
use <CreateObjcInformalDelegateWithContext>.
If the protocol can be resolved at runtime, it is generally easier to
use the <CreateObjcDelegate> handler.
References:
CreateObjcDelegate (handler), CreateObjcDelegateWithContext (handler),
CreateObjcInformalDelegateWithContext (handler)
*/
public handler CreateObjcInformalDelegate(in pProtocol as List, in pHandlerMapping as Array) returns optional ObjcObject
unsafe
variable tObj as ObjcObject
if _MCObjcCreateInformalDelegate(pProtocol, pHandlerMapping, \
tObj) then
return tObj
end if
return nothing
end unsafe
end handler
private foreign handler _MCObjcCreateInformalDelegateWithContext(in pProtocol as List, in pHandlerMapping as Array, in pContext as optional any, out rDelegate as ObjcObject) returns CBool \
binds to "MCObjcCreateInformalDelegateWithContext"
/**
Create an Objective-C object with LCB implementations of methods of an informal protocol.
Example:
foreign handler Objc_NSPortMessageGetMsgId(in pMessage as ObjcId) returns UInt32 \
binds to "objc:NSPort.-msgid"
handler HandlePortMessageCallback(in pPortIndex as Integer, in pMessage as ObjcId) returns nothing
post "portMessage" with [mPortList[pPortIndex]["name"], \
Objc_NSPortMessageGetMsgId(pMessage)]
end handler
foreign handler Objc_NSPortDelegateHandlePortMessage(in pTarget as ObjcId, in pPortMessage as ObjcId) returns nothing \
binds to "objc:.-handlePortMessage:"
public handler GetNSPortDelegate(in pPortIndex as Integer)
return CreateObjcInformalDelegateWithContext([Objc_NSPortDelegateHandlePortMessage], \
{ "handlePortMessage:": \
HandlePortMessageCalback }, \
pPortIndex)
end handler
Parameters:
pProtocol: A list of foreign handlers which bind to protocol methods
pHandlerMapping: A mapping from the protocol's selector names to LCB handlers
Returns: An Objective-C object which calls the appropriate LCB handler
on receiving any of the specified selectors.
Description:
Use the <CreateObjcInformalDelegateWithContext> handler to create instances of
Objective-C delegate classes with LCB implementations of protocol
methods, when the Protocol object cannot be found at runtime. This
occurs for example when all of the protocol methods are optional.
Instead of a protocol name (as with <CreateObjcDelegateWithContext>),
the <pProtocol> argument of <CreateObjcInformalDelegateWithContext> must
be a proper list of foreign handlers for each of the methods of the
protocol for which LCB callbacks are provided in the <pHandlerMapping>
array.
Once created an informal delegate can be set in the usual way on an
instance of the appropriate class (by binding to `-setDelegate:`),
typically so that callbacks triggered by user interaction with a widget
can be handled in LCB.
If no context is required to be passed as a parameter to the callback,
use <CreateObjcInformalDelegate>.
If the protocol can be resolved at runtime, it is generally easier to
use the <CreateObjcDelegateWithContext> handler.
References:
CreateObjcDelegate (handler), CreateObjcDelegateWithContext (handler),
CreateObjcInformalDelegate (handler)
*/
public handler CreateObjcInformalDelegateWithContext(in pProtocol as List, in pHandlerMapping as Array, in pContext as any) returns optional ObjcObject
unsafe
variable tObj as ObjcObject
if _MCObjcCreateInformalDelegateWithContext(pProtocol, \
pHandlerMapping, \
pContext, \
tObj) then
return tObj
end if
return nothing
end unsafe
end handler
end module