It is currently Wed Sep 28, 2016 1:00 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 196 posts ]  Go to page 1, 2, 3, 4, 5 ... 14  Next
Author Message
 Post subject: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Tue Aug 07, 2012 6:26 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
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:
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

Be aware that a lot of the newer OSX frameworks like CloudKit (to work with iCloud) or MapKit (to work with maps) are 64 bit only.
You will only be able to use those frameworks using the 64 bit version of PureBasic.

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 Fri Sep 16, 2016 5:50 am, edited 152 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Tue Aug 07, 2012 6:43 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
Printing a gadget
Code:
CocoaMessage(0, GadgetID(MyGadget), "print:", #nil)


Last edited by wilbert on Fri Oct 05, 2012 3:22 pm, edited 4 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Tue Aug 07, 2012 10:28 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10227
Location: Beyond the pale...
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.

Grid Controls Package
Arctic Reports


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Wed Aug 08, 2012 5:24 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Wed Aug 08, 2012 8:32 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10227
Location: Beyond the pale...
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.

Grid Controls Package
Arctic Reports


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Thu Aug 16, 2012 4:58 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
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:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Tue Sep 04, 2012 6:12 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
Example of alternating row colors for the ListViewGadget ...
Code:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Thu Sep 06, 2012 9:43 pm 
Offline
Addict
Addict

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 1032
Location: Seattle, USA
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:
#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

_________________
MacBook Pro-r, OSX 10.11.6, Command Line Tools 7.3 (10.11), PB-5.50x64


Last edited by WilliamL on Tue Aug 20, 2013 4:48 pm, edited 5 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Sep 14, 2012 9:43 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 06, 2005 2:35 pm
Posts: 939
Location: germany
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

_________________
When somebody says "Expect the unexpected" slap them in the face and say" You didn’t expect that, did you?"


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Sep 14, 2012 6:30 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Sat Sep 15, 2012 3:45 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
Some local information
Code:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Mon Sep 17, 2012 11:50 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
Loading a rtf file
Code:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Wed Sep 19, 2012 2:33 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
Setting the application badge label
Code:
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:
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Thu Sep 20, 2012 4:12 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 06, 2005 2:35 pm
Posts: 939
Location: germany
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

_________________
When somebody says "Expect the unexpected" slap them in the face and say" You didn’t expect that, did you?"


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB Cocoa] Methods, Tips & Tricks
PostPosted: Fri Sep 21, 2012 12:10 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2631
Location: Netherlands
@Kukulkan, Fred offered a solution in your other thread.

Enabling the OS X 10.7+ fullscreen button with the CocoaMessage command ...
Code:
NewCollectionBehaviour = CocoaMessage(0, WindowID(0), "collectionBehavior") | $80
CocoaMessage(0, WindowID(0), "setCollectionBehavior:", NewCollectionBehaviour)


Setting the window alpha value
Code:
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.

Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 196 posts ]  Go to page 1, 2, 3, 4, 5 ... 14  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye