Page 1 of 16

[PB Cocoa] Methods, Tips & Tricks

Posted: Tue Aug 07, 2012 6:26 am
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Tue Aug 07, 2012 6:43 am
by wilbert
Printing a gadget

Code: Select all

CocoaMessage(0, GadgetID(MyGadget), "print:", #nil)

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Tue Aug 07, 2012 10:28 pm
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?

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Wed Aug 08, 2012 5:24 am
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.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Wed Aug 08, 2012 8:32 am
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. :)

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Thu Aug 16, 2012 4:58 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Tue Sep 04, 2012 6:12 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Thu Sep 06, 2012 9:43 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Sep 14, 2012 9:43 am
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Sep 14, 2012 6:30 pm
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.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Sat Sep 15, 2012 3:45 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Mon Sep 17, 2012 11:50 am
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Wed Sep 19, 2012 2:33 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Thu Sep 20, 2012 4:12 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Sep 21, 2012 12:10 pm
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