Page 1 of 1

[Q] Cocoa class delegates

Posted: Wed Dec 06, 2017 5:00 pm
by shaosean
Is there a way of linking a delegate to a widget? If not, would using the objc_addProtocol method be the way to go about it?

Re: [Q] Cocoa class delegates

Posted: Wed Dec 06, 2017 8:12 pm
by LCMark
@shaosean: Ali and I are working on it :)

It should be possible to do from LCB by using the objc runtime API (things like objc_addProtocol), however, it would be very fiddly (and not general, you'd have to write code to create a delegate on a per-delegate basis as you would need to write wrappers explicitly for each method signature you need) - indeed, the time it would take me to try and then to explain is probably more than it will take to finish the in-engine implementation that Ali and I have been working on. Its the last missing piece of Obj-C FFI, so its high priority (we need it to finish the Map widget on iOS).

The support will be present in the next 9 DP (which we're trying to get out the door asap!).

Re: [Q] Cocoa class delegates

Posted: Wed Dec 06, 2017 9:08 pm
by shaosean
Just to make sure I'm clear, this will be for iOS and macOS?

I've used the objc_ methods before, tis not fun, so will be awaiting the next DP :)

Re: [Q] Cocoa class delegates

Posted: Wed Dec 06, 2017 10:54 pm
by livecodeali
Yep, for both :D

Re: [Q] Cocoa class delegates

Posted: Wed Dec 06, 2017 10:59 pm
by shaosean
Quit replying and get coding!! ;-)

Re: [Q] Cocoa class delegates

Posted: Sat Dec 23, 2017 4:13 am
by shaosean
So excited.. dp11 was released, time to get busy on coding :D

Re: [Q] Cocoa class delegates

Posted: Sun Dec 31, 2017 8:41 am
by shaosean
Need some help with this..

Delegate method
toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag;


I've converted it to the following, but get an error in the objc.lcb file..

handler InsertedIntoToolbar(in Toolbar as ObjcObject, in Identifier as ObjcObject, in Flag as Boolean) returns optional ObjcAutoreleasedId


The error message (we really need to copy out of the error dialog)
Callback handler InsertedIntoToolbar() parameters must conform to protocol method types
LCB File: objc.lcb
LCB Line: 406

And line 406 from the objc.lcb file:
if _MCObjcCreateDelegate(pProtocol, pHandlerMapping, tObj) then

The whole function is:
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


Basically boils down to, am I using the right parameter types for the delegate? Another language showed the delegate as having ID and SEL as the first two parameters and then Toolbar, Identifier and Flag but that gave the same error message..

Re: [Q] Cocoa class delegates

Posted: Mon Jan 08, 2018 6:36 pm
by livecodeali
Hi! Sorry for the delay in replying to this, just got back from the holidays.

There's a slight wrinkle with BOOL being typedef'd to different primitive types on Mac (signed char) and iOS (bool) - for this reason there is some special-case code that allows a CBool to be used as the parameter in a delegate callback where BOOL (i.e. CSChar on Mac) is expected. However this special-casing has not been extended to things that bridge to CBool, such as the LCB Boolean type (it probably should do, worth filing a bug I think).

tl;dr - change the type of the Flag parameter to CBool and you should get type conformance.

Re: [Q] Cocoa class delegates

Posted: Tue Jan 09, 2018 5:11 am
by shaosean
Thanks. I'll give it a try tomorrow.

Re: [Q] Cocoa class delegates

Posted: Tue Jan 09, 2018 2:19 pm
by shaosean
Well, that did indeed fix that issue, but still not working.. Perhaps someone smarter than me can get it working in LCB (it's not really that hard to implement the NSToolbar in Objective-C)

Re: [Q] Cocoa class delegates

Posted: Tue Jan 09, 2018 3:19 pm
by livecodeali
In what way is it not working? Is there an error message?

Re: [Q] Cocoa class delegates

Posted: Tue Jan 09, 2018 5:55 pm
by shaosean
If I use "CreateObjcDelegate" it bails out by calling "toolbarAllowedItemIdentifiers:" a few hundred times until it crashes.. I have set the delegate to the Toolbar and then assigned it to the window..

If I set the delegate to the delegate returned from "CreateObjcDelegate" the toolbar gets added to the window, but no toolbar items and doesn't seem to be calling the handlers I set up for the delegate..

If I use "CreateObjcInformalDelegate" it bails out with (regardless of what is set as the delegate)
0 com.runrev.livecode 0x000000010d463c64 MCValueGetTypeCode + 4
1 com.runrev.livecode 0x000000010d45325f MCStringConvertToUTF8 + 31
2 com.runrev.livecode 0x000000010d74c43c ffi_closure_unix64 + 3022772
3 com.runrev.livecode 0x000000010d74e35a MCScriptBuiltinThrow + 5514
4 com.runrev.livecode 0x000000010d74d2f7 MCScriptBuiltinThrow + 1319
... a lot more lines

Re: [Q] Cocoa class delegates

Posted: Tue Jan 16, 2018 2:10 pm
by livecodeali
Could you post the code you are using, or send it to ali dot lloyd at livecode dot com? I'll try and figure out what's going on.