[PB Cocoa] Methods, Tips & Tricks

Mac OSX specific forum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

[PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

A thread for Methods, Tips & Tricks for the Cocoa version of PureBasic.

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];
PureBasic doesn't support square brackets like this so a different approach is required to use this message sending system.
CocoaMessage is what you need in this case.

Syntax :
CocoaMessage(@ReturnValue, Object, Method.s [,...])

When 0 is passed for the return value address, the result is returned from the procedure itself if it fits in the PB Integer type.
When 0 is passed for object, a class name can be passed together with the first part of the method separated by a space character.
When non integer values have to be passed, they can be passed by reference by adding a @ after a colon character.
You can also pass the address of a PureBasic string using $ when a NSString is expected. CocoaMessage does the conversion for you internally.


Example :

Code: Select all

MyTransform = CocoaMessage(0, 0, "NSAffineTransform transform"); get an identity transform

sx.CGFloat = 5.5
sy.CGFloat = 20
CocoaMessage(0, MyTransForm, "scaleXBy:@", @sx, "yBy:@", @sy); scale x by 5.5, y by 20

MyTransformStruct.NSAffineTransform
CocoaMessage(@MyTransformStruct, MyTransForm, "transformStruct"); get the transform structure

Debug MyTransformStruct\m11; debug outputs 5.5
Most PureBasic gadgets are based on classes from the AppKit framework.
http://developer.apple.com/library/mac/ ... index.html

When going through the examples in this thread, it sometimes can happen that constants which are defined in a source were added to
PureBasic itself in a later version. If that happens you can simply remove the constant declaration from the example code.


Methods, Tips & Tricks
Last edited by wilbert on Tue Feb 20, 2024 8:07 am, edited 163 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Printing a gadget

Code: Select all

CocoaMessage(0, GadgetID(MyGadget), "print:", #nil)
Last edited by wilbert on Fri Oct 05, 2012 3:22 pm, edited 4 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: [PB Cocoa] Methods, Tips & Tricks

Post by srod »

Hi Wilbert,

thanks for this (the thread + the tip above), very informative.

How much of the Cocoa API's can actually be accessed like this without having to write c wrapper functions?
I may look like a mule, but I'm not a complete ass.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Hi Srod,

Cocoa is totally object oriented so if you mean using it without importing specific functions for each object like Carbon requires, the answer is that you can access all of it.
Last edited by wilbert on Sat Sep 22, 2012 8:41 am, edited 1 time in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: [PB Cocoa] Methods, Tips & Tricks

Post by srod »

Thanks Wilbert. I understand.

As soon as I am done with the ObjectiveC book I am reading then I will dive head first into a book on Cocoa that I have. This should get me up to scratch. :)
I may look like a mule, but I'm not a complete ass.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

WilliamL asked me about how to get the black dot in the window close button on PB Cocoa.
It can be done by sending a simple message to the window.

CocoaMessage(0, WindowID(0), "setDocumentEdited:", #YES)

Code: Select all

If OpenWindow(0, 0, 0, 400, 300, "Dot example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  CocoaMessage(0, WindowID(0), "setDocumentEdited:", #YES)
  
  Repeat 
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Fri Oct 05, 2012 3:16 pm, edited 2 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Example of alternating row colors for the ListViewGadget ...

Code: Select all

If OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ListViewGadget(0, 10, 10, 250, 120)
  
  CocoaMessage(0, GadgetID(0), "setUsesAlternatingRowBackgroundColors:", #YES)
  
  For a = 1 To 12
    AddGadgetItem (0, -1, "Item " + Str(a) + " of the Listview")
  Next
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Fri Oct 05, 2012 3:16 pm, edited 2 times in total.
WilliamL
Addict
Addict
Posts: 1214
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [PB Cocoa] Methods, Tips & Tricks

Post by WilliamL »

Here are more examples from wilbert to disable/enable scrollbars, make a button a default button (any size!), and right justify a string gadget.

Code: Select all

#NSRoundedBezelStyle = 1 ; for default button
#NSRightTextAlignment = 1 ; for right align string gadget

If OpenWindow(0, 0, 0, 270, 260, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ListViewGadget(0, 10, 10, 250, 180)
  
  For a = 1 To 12
    AddGadgetItem (0, -1, "Item " + Str(a) + " of the Listview")
  Next
  
  ButtonGadget(1, 10, 200, 80, 30, "Button")
  
  StringGadget(2, 10,230,120,22,"String gadget")
  
   ; alternating colors
   CocoaMessage(0,GadgetID(0),"setUsesAlternatingRowBackgroundColors:",#True)
  
   ; enable/disable scrollers
   ScrollView = CocoaMessage(0,GadgetID(0),"enclosingScrollView")
   CocoaMessage(0,ScrollView,"setHasVerticalScroller:", #False) ; "setHasHorizontalScroller:"

    ; set default button cell
    ButtonCell = CocoaMessage(0,GadgetID(1),"cell")
    CocoaMessage(0,GadgetID(1),"setBezelStyle:", #NSRoundedBezelStyle)
    CocoaMessage(0,WindowID(0),"setDefaultButtonCell:", ButtonCell)
   
   ;set string gadget to right-justified
    CocoaMessage(0,GadgetID(2),"setAlignment:", #NSRightTextAlignment)

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Last edited by WilliamL on Tue Aug 20, 2013 4:48 pm, edited 5 times in total.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
User avatar
Kukulkan
Addict
Addict
Posts: 1348
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Kukulkan »

This is great! Thanks!

And how to read values? I'm interested in reading the system default font name and size using NSFont object (https://developer.apple.com/library/mac ... rence.html).
I want to use these values together with LoadFont() function.

Any small code snippet for me? :)

Kukulkan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

I don't think NSFont has a default font size.
What can be done is return a system font and get the name of that.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Some local information

Code: Select all

CurrentLocale = CocoaMessage(0, 0, "NSLocale currentLocale")

LanguageCode = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleLanguageCodeKey")
CocoaMessage(@LanguageCode, LanguageCode, "UTF8String")
Debug PeekS(LanguageCode, -1, #PB_UTF8); Show the language code

DecimalSeparator = CocoaMessage(0, CurrentLocale, "objectForKey:$", @"kCFLocaleDecimalSeparatorKey")
CocoaMessage(@DecimalSeparator, DecimalSeparator, "UTF8String")
Debug PeekS(DecimalSeparator, -1, #PB_UTF8); Show the decimal separator



TimeZone = CocoaMessage(0, 0, "NSTimeZone localTimeZone")
Debug CocoaMessage(0, TimeZone, "secondsFromGMT"); Show seconds from GMT
Last edited by wilbert on Mon Sep 24, 2012 8:10 am, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Loading a rtf file

Code: Select all

If OpenWindow(0, 0, 0, 320, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 10, 10, 300, 130)
  
  AttributedString = CocoaMessage(0, 0, "NSAttributedString alloc")
  CocoaMessage(@AttributedString, AttributedString, "initWithPath:$", @"filename.rtf", "documentAttributes:", #Null)
  If AttributedString
    TextStorage = CocoaMessage(0, GadgetID(0), "textStorage")
    CocoaMessage(0, TextStorage, "setAttributedString:", AttributedString)
    CocoaMessage(0, AttributedString, "release")
  EndIf
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Mon Sep 24, 2012 7:48 am, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Setting the application badge label

Code: Select all

App = CocoaMessage(0, 0, "NSApplication sharedApplication")
DockTile = CocoaMessage(0, App, "dockTile")
CocoaMessage(0, DockTile, "setBadgeLabel:$", @"Pure")

MessageRequester("", "Badge label set")
Set a key equivalent for a button gadget (alt+b in this example)

Code: Select all

If OpenWindow(0, 0, 0, 222, 200, "ButtonGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0, 10, 10, 200, 30, "Button")
  
  ButtonID = GadgetID(0)
  CocoaMessage(0, ButtonID, "setKeyEquivalent:$", @"b")
  CocoaMessage(0, ButtonID, "setKeyEquivalentModifierMask:", 1 << 19); 1 << 19 = alt
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Last edited by wilbert on Mon Sep 24, 2012 8:48 am, edited 2 times in total.
User avatar
Kukulkan
Addict
Addict
Posts: 1348
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Kukulkan »

Sorry for misusing this thread, but here are the experts and CocoaMessage seems the key :wink:

I need to get the file which was double-clicked to run my application. My application is launched because the Info.plist points to my executable. Sadly, the file is not delivered as commandline parameter. It is done like described here: http://developer.apple.com/library/ios/ ... Types.html

How to get this UIApplicationLaunchOptionsURLKey value? I try'd severat attempts, but with no success (and it costs huge efforts to always recompile, change Info.plist and try to double click). Is there any expert who knows how to get the filename?

Kukulkan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

@Kukulkan, Fred offered a solution in your other thread.

Enabling the OS X 10.7+ fullscreen button with the CocoaMessage command ...

Code: Select all

NewCollectionBehaviour = CocoaMessage(0, WindowID(0), "collectionBehavior") | $80
CocoaMessage(0, WindowID(0), "setCollectionBehavior:", NewCollectionBehaviour)
Setting the window alpha value

Code: Select all

If OpenWindow(0, 0, 0, 222, 200, "Window Alpha", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  alpha.CGFloat = 0.7
  CocoaMessage(0, WindowID(0), "setAlphaValue:@", @alpha)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Last edited by wilbert on Fri Oct 05, 2012 3:10 pm, edited 1 time in total.
Post Reply