[Q] Cocoa class delegates

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

[Q] Cocoa class delegates

Post by shaosean » Wed Dec 06, 2017 5:00 pm

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?

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1206
Joined: Thu Apr 11, 2013 11:27 am

Re: [Q] Cocoa class delegates

Post by LCMark » Wed Dec 06, 2017 8:12 pm

@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!).

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Wed Dec 06, 2017 9:08 pm

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 :)

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 192
Joined: Thu Apr 18, 2013 2:48 pm

Re: [Q] Cocoa class delegates

Post by livecodeali » Wed Dec 06, 2017 10:54 pm

Yep, for both :D

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Wed Dec 06, 2017 10:59 pm

Quit replying and get coding!! ;-)

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Sat Dec 23, 2017 4:13 am

So excited.. dp11 was released, time to get busy on coding :D

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Sun Dec 31, 2017 8:41 am

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..

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 192
Joined: Thu Apr 18, 2013 2:48 pm

Re: [Q] Cocoa class delegates

Post by livecodeali » Mon Jan 08, 2018 6:36 pm

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.

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Tue Jan 09, 2018 5:11 am

Thanks. I'll give it a try tomorrow.

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Tue Jan 09, 2018 2:19 pm

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)

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 192
Joined: Thu Apr 18, 2013 2:48 pm

Re: [Q] Cocoa class delegates

Post by livecodeali » Tue Jan 09, 2018 3:19 pm

In what way is it not working? Is there an error message?

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: [Q] Cocoa class delegates

Post by shaosean » Tue Jan 09, 2018 5:55 pm

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

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 192
Joined: Thu Apr 18, 2013 2:48 pm

Re: [Q] Cocoa class delegates

Post by livecodeali » Tue Jan 16, 2018 2:10 pm

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.

Post Reply

Return to “LiveCode Builder”