[PB Cocoa] Methods, Tips & Tricks

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Change height of rows in ListIconGadget (works also with ListViewGadget):

Code: Select all

Define RowHeight.CGFloat

OpenWindow(0, 200, 100, 420, 220, "Change ListIcon's row height")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, 102, "Name", 100, #PB_ListIcon_GridLines)
AddGadgetColumn(0, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth) - 8)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(0, -1, "Didi Foundit" + #LF$ + "321 Logo Drive, Mouse House, Downtown")
Frame3DGadget(1, 20, GadgetY(0) + GadgetHeight(0) + 10, WindowWidth(0) - 40, 90, "Row height:")
TrackBarGadget(2, GadgetX(1) + 10, GadgetY(1) + 30, GadgetWidth(1) - 20, 23, 15, 26, #PB_TrackBar_Ticks)
CocoaMessage(0, GadgetID(2), "setAllowsTickMarkValuesOnly:", #YES)
TextGadget(3, GadgetX(1) + 10, GadgetY(2) + GadgetHeight(2) + 5, GadgetWidth(1) - 10, 20, "15     16     17     18     19     20     21     22     23     24     25")

; ----- Read current row height and set TrackBar to that value
CocoaMessage(@RowHeight, GadgetID(0), "rowHeight")
SetGadgetState(2, RowHeight)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 2
        RowHeight = GetGadgetState(2)
        ; ----- Set new row height
        CocoaMessage(0, GadgetID(0), "setRowHeight:@", @RowHeight)
      EndIf
  EndSelect
ForEver
jag
New User
New User
Posts: 2
Joined: Sun Nov 25, 2012 2:33 am
Location: New Hampshire

Re: [PB Cocoa] Methods, Tips & Tricks

Post by jag »

Hello,

The examples for using CocoaMessage() to access cocoa objects are very interesting, and I would like to experiment with them. What I am missing is a good example of how and what to specify at the beginning of my program to use them. Am I supposed to use Import... EndImport or OpenLibrary() and a prototype? I am trying to use the precompiled library from the zip file. I am a beginner with PB, so a simple example would be a big help, because I am stumped. I have tried searching the forum, but can't seem to find a complete enough example.

Thanks,

John
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Mindphazer »

You need to download the file (first post of this thread), unzip it, then copy the file CocoaMsg_x86 (or x64, depending on Purebasic version) into /Purebasic/purelibraries/userlibraries
Restart compiler, then you can use CocoaMessage function as it was a native PB command.
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Hide title line in ListIconGadget:

Code: Select all

OpenWindow(0, 200, 100, 430, 125, "ListIcon Example")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 50, "Name", 110)
AddGadgetColumn(0, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth) - 8)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(0, -1, "Didi Foundit" + #LF$ + "321 Logo Drive, Mouse House, Downtown")
ButtonGadget(1, WindowWidth(0) / 2 - 70, WindowHeight(0) - 30, 140, 25, "Hide title header")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1 And EventType() = #PB_EventType_LeftClick
        CocoaMessage(0, GadgetID(0), "setHeaderView:", 0)
        DisableGadget(1, #True)
      EndIf
  EndSelect
ForEver
jag
New User
New User
Posts: 2
Joined: Sun Nov 25, 2012 2:33 am
Location: New Hampshire

Re: [PB Cocoa] Methods, Tips & Tricks

Post by jag »

Thanks - Mindphazer,

That information was exactly what I needed.

John
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Mindphazer »

jag wrote:Thanks - Mindphazer,

That information was exactly what I needed.

John
You're welcome :-)
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Change location of tabs in PanelGadget:

Code: Select all

#NSTopTabsBezelBorder    = 0
#NSLeftTabsBezelBorder   = 1
#NSBottomTabsBezelBorder = 2
#NSRightTabsBezelBorder  = 3

OpenWindow(0, 270, 100, 300, 310, "Change tab location")
PanelGadget(0, 10, 10, WindowWidth(0) - 20, 180)
AddGadgetItem (0, -1, "Tab 1")
AddGadgetItem (0, -1, "Tab 2")
CloseGadgetList()
Frame3DGadget(1, 30, 210, WindowWidth(0) - 60, 80, "Tab location")
OptionGadget(2, 130, GadgetY(1) + 20, 80, 20, "Top")
OptionGadget(3, 50, GadgetY(1) + 35, 80, 20, "Left")
OptionGadget(4, 130, GadgetY(1) + 50, 80, 20, "Bottom")
OptionGadget(5, 210, GadgetY(1) + 35, 80, 20, "Right")
SetGadgetState(2, #True)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      SelectedGadget = EventGadget()

      If SelectedGadget >= 2 And SelectedGadget <= 5
        CocoaMessage(0, GadgetID(0), "setTabViewType:", SelectedGadget - 2)
      EndIf
  EndSelect
ForEver
Change color theme of selected tab in PanelGadget:

Code: Select all

If OSVersion() > #PB_OS_MacOSX_10_6
  MessageRequester("Info", "Sorry, but 'setColorTint:' seems to be broken beginning with Lion...")
  End
EndIf

#NSBlueControlTint = 1
#NSClearControlTint = 7

OpenWindow(0, 270, 100, 300, 250, "Change tab selection color")
PanelGadget(0, 10, 10, WindowWidth(0) - 20, 160)
AddGadgetItem (0, -1, "Tab 1")
AddGadgetItem (0, -1, "Tab 2")
CloseGadgetList()
Frame3DGadget(1, 50, 180, WindowWidth(0) - 100, 50, "Tab selection color")
OptionGadget(2, 70, GadgetY(1) + 20, 80, 20, "Blue")
OptionGadget(3, 150, GadgetY(1) + 20, 80, 20, "Graphite")
SetGadgetState(2, #True)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
          CocoaMessage(0, GadgetID(0), "setControlTint:", #NSBlueControlTint)
        Case 3
          CocoaMessage(0, GadgetID(0), "setControlTint:", #NSClearControlTint)
      EndSelect
  EndSelect
ForEver
Update: Sorry for all Lion and Mountain Lion users, but the above example doesn't work in all MacOS X versions following Snow Leopard. I therefore added an error message for Lion and newer MacOS X versions. It seems that "setColorTint:" is broken beginning with Lion at least in the NSTabView (PanelGadget) and NSProgressIndicator (ProgressBarGadget):
http://stackoverflow.com/questions/1053 ... int-broken
http://stackoverflow.com/questions/7720 ... -no-effect
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: [PB Cocoa] Methods, Tips & Tricks

Post by kenmo »

Hello Wilbert! I don't fully know what CocoaMessage() is doing, but thank you for all these quick and useful examples!
This Sound example plays MP3 for me but it won't seem to load OGG Vorbis... is it possible?

Also there is a significant delay, I think it is loading the whole file into memory, is it possible to stream it instead, similar to the native PB Sound library?

I'm trying to find an alternate method to stream OGG music on Mac... the Sound library crashes in some conditions (I posted a bug report)... tomorrow I will try the Movie library but it's usually not recommended for games I think.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

kenmo wrote:This Sound example plays MP3 for me but it won't seem to load OGG Vorbis... is it possible?

Also there is a significant delay, I think it is loading the whole file into memory, is it possible to stream it instead, similar to the native PB Sound library?
I don't know about other users but I don't experience the delay.

As for playing ogg, that won't work. It uses the decoders built into the system.
It can play MP3 and AAC but to play OGG, you will need the PureBasic commands.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: [PB Cocoa] Methods, Tips & Tricks

Post by kenmo »

About the delay, I should mention I am using it in a 60 FPS game... so even a slight delay can be noticeable.

That's strange that they don't support OGG... it's an open standard and I thought it was pretty common these days.

I think I can get the PB library working properly, but still thanks for your Cocoa example!
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wombats »

Hi,

I've been trying to download the lib, but the download link (http://www.waterlijn.info/pb/cocoa/CocoaMsg_source.zip) hasn't been working for a few days now. :/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

wombats wrote:Hi,

I've been trying to download the lib, but the download link hasn't been working for a few days now. :/
It works fine when I try it.
Can anyone else confirm the download problem ?
Last edited by wilbert on Wed May 31, 2017 3:11 pm, edited 1 time in total.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Mindphazer »

Working for me too.
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wombats »

I wonder why it's not working for me, then. D: I've tried several times, with different browsers, and it just won't go.

"Oops! Google Chrome could not connect to http://www.waterlijn.info"

Edit: I just tried it through a proxy, and it downloaded. Thanks for making this!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Methods, Tips & Tricks

Post by wilbert »

Fred will include the command into PureBasic itself in the future so you won't need a separate library anymore.
It will be under a different name since the current name CocoaMessage is not in line with the other PureBasic commands.
In case you already have a lot of code using CocoaMessage when it is added, you can of course use a small macro to use it with its current name or do a search and replace.
Post Reply