In my exploration of declares I have come to another question:
Doing the declares and creating a system object is fairly easy once one has got the basics.
But sometimes it is needed to catch their internal draw methods or forward an event to the Xojo instance that handles it.
What I believe to understand that far:
You cannot tweak the methods in a system class but have to set up an own subclass of it.
So I start with a branch in the constructor:
if SubClass = NIL then // create a global subclass first
dim objectClass As ptr=NSClassFromString(ClassName)
declare function objc_allocateClassPair lib "libobjc.dylib" (superclass as Ptr, name as CString, extraBytes as Integer) as Ptr
subclass = objc_allocateClassPair (objectClass, SubClassName, 0)
That works so far. Next would be to add own methods for superclass methods and events I want to handle myself.
Far as I found out this should be possible somehow with something like:
[code]Function class_replacemethod (aclass as ptr, name as cfstringref, method as ptr, types as cstring) As ptr
//used to tweak an event or method call to a personal one:
#if targetmacos
declare function class_replaceMethod lib “libobjc.dylib” (cls as ptr, name as Ptr, imp as Ptr, types as CString) as ptr
return class_replaceMethod (aclass ,NSSelectorFromString(name), method, types)
#endif
End Function[/code]
and
declare sub objc_registerClassPair lib "libobjc.dylib" (cls as Ptr)
dim res as ptr = class_replaceMethod (subclass, OriginalMethodName, AddressOf myMethod, "v@:")
objc_registerClassPair subclass
but that’s where I fail. res returns NIL.
Could someone of you point me to an understandable reference on the C parameter types that “v@:” etc. ? Google has left me alone with thator everything I could find was too cryptic.
And do I have to do something different if it’s a method or a Notification/ Event handler? Do I have to address the delegate (or create one) in order for this to work?
Thanks a lot!