A thread for Methods, Tips & Tricks for the Cocoa version of PureBasic.
The intention is to collect information from different threads and users in this thread, just like the
"API list for Mac" thread.
A little information :
Cocoa is object oriented and works by sending a message to a receiver.
The Objective-C language uses square brackets for this. For example to print a view it uses
[myView print:sender];
In this case, the
receiver (object myView) is told to print itself and is informed the
message was send by sender.
In this case, print: is called the
selector ( it selects the method to be used ).
PureBasic doesn't support square brackets like this so low level C functions are required to use this message sending system.
I created a user library to help with this
viewtopic.php?f=19&t=50688 .
If you prefer not to use a user library, simple tips will also work with importing the low level functions with ImportC
1 2 3 4 5
| ImportC ""
oClass(name.p-ascii) As "_objc_lookUpClass"
oMsg(receiver, selector, arg1 = #Null, arg2 = #Null, arg3 = #Null) As "_objc_msgSend"
oSel(name.p-ascii) As "_sel_registerName"
EndImport |
The biggest difference is that the user library has debug functionality built in so that if you type something wrong and therefore end up with a non existing selector, it will tell you about it if you compile with the debugger turned on. The ImportC way will simply crash the application in such a case.
A simple example to start with ...
The PureBasic gadgets, are extensions of the NSView class (
https://developer.apple.com/library/mac ... SView.html ).
This class has a method print: to print itself. So if you want to print a gadget, you can do it like this
oMsg(GadgetID(MyGadget), oSel("print:"), 0)
Note that
oSel("print:") needs to look up the selector in a table and returns a pointer.
Therefore if you use certain selectors a lot, it's best to look them up once in the beginning of your application, store the return value in a global variable and use that for the rest of your application.
The same goes for looking up classes like
oClass("NSNumber") .
____________________
Showing a visible ruler inside an EditorGadget
oMsg(GadgetID(MyEditorGadget), oSel("setRulerVisible:"), #True)
Partager