Page 6 of 16

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Mon Nov 26, 2012 7:24 pm
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

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Thu Nov 29, 2012 5:39 am
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.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Thu Nov 29, 2012 6:26 am
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.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 12:50 am
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!

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 1:34 pm
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. :/

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 3:24 pm
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 ?

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 3:45 pm
by Mindphazer
Working for me too.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 4:54 pm
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!

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 5:49 pm
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.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Fri Nov 30, 2012 6:30 pm
by wombats
That's good to know.

I downloaded this because I wanted to see if I could get the ListIcon gadget to select items when the gadget is right-clicked. I've looked around online, and I can't seem to get anything to work with CocoaMessage.

I found this (http://stackoverflow.com/questions/7637 ... ight-click), but I'm not sure how to make it work in PB...I've never looked at Cocoa before, so it's all pretty confusing for me. Could you offer any pointers, do you think?

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Sat Dec 01, 2012 8:36 am
by wilbert
wombats wrote:I downloaded this because I wanted to see if I could get the ListIcon gadget to select items when the gadget is right-clicked.
Something like this should do the trick but maybe someone knows an easier solution

Code: Select all

EnableExplicit

Define AllowMultipleSelection = #YES

Define CursorLocation.NSPoint
Define ListIconGadgetID.i, SelectedRow.i, IndexSet.i

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")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_RightClick
        CursorLocation\x = WindowMouseX(0)
        CursorLocation\y = WindowHeight(0) - WindowMouseY(0)
        ListIconGadgetID = GadgetID(0)
        CocoaMessage(@CursorLocation, ListIconGadgetID, "convertPoint:@", @CursorLocation, "fromView:", #nil)
        CocoaMessage(@SelectedRow, ListIconGadgetID, "rowAtPoint:@", @CursorLocation)
        CocoaMessage(@IndexSet, 0, "NSIndexSet indexSetWithIndex:", SelectedRow)
        CocoaMessage(0, ListIconGadgetID, "selectRowIndexes:", IndexSet, "byExtendingSelection:", AllowMultipleSelection)
      EndIf
  EndSelect
ForEver
Edit:
Updated the code so it uses EnableExplicit and added option to specify allowing multiple selection.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Sat Dec 01, 2012 2:22 pm
by Shardik
This is a somewhat simpler approach which doesn't allow multi-selection by a right click like wilbert's example. It determines the location of the right click, converts the location to the row index and simply selects that row with SetGadgetState():

Code: Select all

EnableExplicit

Define CursorLocation.NSPoint
Define RowHeight.CGFloat
Define SelectedRow.I

OpenWindow(0, 200, 100, 430, 96, "ListIcon Example")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "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")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_RightClick
        CursorLocation\x = WindowMouseX(0)
        CursorLocation\y = WindowHeight(0) - WindowMouseY(0)
        CocoaMessage(@CursorLocation, GadgetID(0), "convertPoint:@", @CursorLocation, "fromView:", 0)
        SelectedRow = CocoaMessage(0, GadgetID(0), "rowAtPoint:@", @CursorLocation)
        SetGadgetState(0, SelectedRow)
      EndIf
  EndSelect
ForEver
Update: I changed the detection of the right mouse click's location from

Code: Select all

        CursorLocation\x = WindowMouseX(0) - GadgetX(0)
        CursorLocation\y = WindowMouseY(0) - GadgetY(0) - 17
        SelectedRow = CocoaMessage(0, GadgetID(0), "rowAtPoint:@", @CursorLocation)
        SetGadgetState(0, SelectedRow)
to Wilbert's updated approach

Code: Select all

        CursorLocation\x = WindowMouseX(0)
        CursorLocation\y = WindowHeight(0) - WindowMouseY(0)
        CocoaMessage(@CursorLocation, GadgetID(0), "convertPoint:@", @CursorLocation, "fromView:", 0)
        SelectedRow = CocoaMessage(0, GadgetID(0), "rowAtPoint:@", @CursorLocation)
        SetGadgetState(0, SelectedRow)
because my previous solution didn't correctly select a row in ListIconGadgets with a scrollbar. Thank you for your hint and heads up via PM, Wilbert!

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Sat Dec 01, 2012 3:07 pm
by wombats
Thank you both! That's wonderful.

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Sat Dec 01, 2012 9:18 pm
by Shardik
Select entire column in ListIconGadget by clicking on column header and get index of selected column:

Code: Select all

OpenWindow(0, 200, 100, 430, 125, "Select entire column by clicking on column header")
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 - 90, WindowHeight(0) - 30, 180, 25, "Display selected column")

; ----- Allow selection of entire column by clicking on column header
CocoaMessage(0, GadgetID(0), "setAllowsColumnSelection:", #YES)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1 And EventType() = #PB_EventType_LeftClick
        ; ----- Get selected column
        SelectedColumn = CocoaMessage(0, GadgetID(0), "selectedColumn")

        If SelectedColumn = -1
          Debug "No column is currently selected!"
        Else
          Debug "Column " + Str(SelectedColumn) + " is currently selected!"
        EndIf
      EndIf
  EndSelect
ForEver

Re: [PB Cocoa] Methods, Tips & Tricks

Posted: Tue Dec 04, 2012 5:57 pm
by wilbert
Save an image to a file using png encoding.

Code: Select all

Procedure WriteImageData(File, Image)
  Protected ImageReps = CocoaMessage(0, ImageID(Image), "representations")
  Protected PNGData = CocoaMessage(0, 0, "NSBitmapImageRep representationOfImageRepsInArray:", ImageReps, "usingType:", #NSPNGFileType, "properties:", #nil)
  Protected Length = CocoaMessage(0, PNGData, "length")
  If Length
    ProcedureReturn WriteData(File, CocoaMessage(0, PNGData, "bytes"), Length)  
  Else
    ProcedureReturn -1
  EndIf
EndProcedure


If CreateImage(1, 255, 255)
  StartDrawing(ImageOutput(1))
  For k=0 To 255
    FrontColor(RGB(k,0, k))
    Line(0, k, 255, 1)
  Next
  StopDrawing()
EndIf

If CreateFile(0, "MyImages.dat")
  WriteImageData(0, 1)
  CloseFile(0) 
EndIf