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
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
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
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
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
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
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
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)
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, \