[PB Cocoa] Methods, Tips & Tricks

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

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Set ToolTip for column header in ListIconGadget:

Code: Select all

Procedure SetColumnHeaderToolTip(ListIconID.I, ColumnIndex.I, ToolTipText.S)
  Protected ColumnObject.I

  CocoaMessage(@ColumnObject, CocoaMessage(0, GadgetID(ListIconID), "tableColumns"), "objectAtIndex:", ColumnIndex)
  CocoaMessage(0, ColumnObject, "setHeaderToolTip:$", @ToolTipText)
EndProcedure

OpenWindow(0, 270, 100, 410, 67, "ListIcon Example")
ListIconGadget(0, 5, 5, 400, 57, "Name", 110)
AddGadgetColumn(0, 1, "Address", 285)
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")

SetColumnHeaderToolTip(0, 0, "Firstname & Surname")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Polo wrote:Love the fact you guys so quickly made that much Cocoa code! :)
You have to thank wilbert for his brilliant CocoaMessage userlib and for his numerous Cocoa examples although I will try to catch up... :lol:

It would be very nice if Fred would decide to implement the CocoaMessage() command natively in PureBasic - perhaps with a more PureBasic-like name such as SendCommand(), SendCocoaCommand() or SendObjectMessage()... :wink:
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 »

Polo wrote:Love the fact you guys so quickly made that much Cocoa code! :)
The Cocoa classes are documented pretty well, that makes it easier to come up with examples :)
What also helps is that you can easily 'inspect' an object to see what classes it inherits from. And of course you don't have to import every function.
Shardik wrote:It would be very nice if Fred would decide to implement the CocoaMessage() command natively in PureBasic - perhaps with a more PureBasic-like name such as SendCommand(), SendCocoaCommand() or SendObjectMessage()... :wink:
I agree it would be nice. It makes it easier to post solutions to questions in other threads without having to refer all the time to the userlib.
If the command would get another name, so be it. The only thing I wouldn't look forward to is editing all my examples in this case :wink:
Anyway, I chose the name to be short and starting with a lowercase character on purpose, to make it different from the other cross platform commands. I know PureBasic is all about being cross platform and I understand Fred might hesitate to add a platform specific command.
Shardik wrote:I will try to catch up... :lol:
Great :) I'm looking forward to more examples.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Toggle grid lines in ListIconGadget between vertical, horizontal, combined vertical + horizontal and none:

Code: Select all

EnableExplicit

#NSTableViewGridNone                     = 0
#NSTableViewSolidVerticalGridLineMask    = 1 << 0
#NSTableViewSolidHorizontalGridLineMask  = 1 << 1

Define GridLineStyle.I

OpenWindow(0, 200, 100, 430, 220, "ListIcon Example")

ListIconGadget(0, 10, 10, WindowWidth(0) - 20, 75, "Name", 110)
AddGadgetColumn(0, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth))
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, 90, 100, 250, 108, "Grid line style:")
OptionGadget(2, 100, 120, 240, 20, "No grid lines")
OptionGadget(3, 100, 140, 240, 20, "Solid vertical")
OptionGadget(4, 100, 160, 240, 20, "Solid horizontal")
OptionGadget(5, 100, 180, 240, 20, "Solid horizontal and vertical")
SetGadgetState(2, #True)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
          GridLineStyle = #NSTableViewGridNone
        Case 3
          GridLineStyle = #NSTableViewSolidVerticalGridLineMask
        Case 4
          GridLineStyle = #NSTableViewSolidHorizontalGridLineMask
        Case 5
          GridLineStyle = #NSTableViewSolidHorizontalGridLineMask | #NSTableViewSolidVerticalGridLineMask
      EndSelect

      CocoaMessage(0, GadgetID(0), "setGridStyleMask:", GridLineStyle)
  EndSelect
ForEver
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Shardik »

Semi-transparent window and transparent ListIconGadget:

Image

Code: Select all

Define Alpha.CGFloat = 0.8

OpenWindow(0, 200, 100, 430, 95, "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))
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")

CocoaMessage(0, WindowID(0), "setOpaque:", #NO)
CocoaMessage(0, WindowID(0), "setAlphaValue:@", @Alpha)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Window with background image and transparent ListIconGadget:

Code: Select all

UseJPEGImageDecoder()

OpenWindow(0, 200, 100, 430, 300, "Window with background image + transparent ListIcon")
ListIconGadget(0, 10, 100, WindowWidth(0) - 20, 75, "Name", 110)
AddGadgetColumn(0, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth))
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")

If LoadImage(0, #PB_Compiler_Home + "Examples/3D/Data/Textures/Clouds.jpg")
  ContentView = CocoaMessage(0, WindowID(0), "contentView")
  CocoaMessage(0, ContentView, "setWantsLayer:", #YES)
  Layer = CocoaMessage(0, ContentView, "layer")
  CocoaMessage(0, Layer, "setContents:", ImageID(0))
EndIf

CocoaMessage(0, GadgetID(0), "setBackgroundColor:", CocoaMessage(0, 0, "NSColor clearColor"))
CocoaMessage(0, CocoaMessage(0, GadgetID(0), "enclosingScrollView"), "setDrawsBackground:", #NO)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Update: In the first example I had to change the declaration of variable Alpha from Float to CGFloat in order to run correctly in 32 and 64 environments (Thank you for your hint, wilbert!).
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Kukulkan »

Great list of tips here :D Thank you all!

Any idea how to enable wordwrap in EditorGadget on MacOS using CocoaMessage() command?

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 wrote:Any idea how to enable wordwrap in EditorGadget on MacOS using CocoaMessage() command?
As far as I know there is no single command solution.

Edit :
My initial code didn't seem to work properly when toggling wordwrap.
Here's an alternative solution from Shardik

Code: Select all

Procedure EditorGadget_SetWordWrap(EditorGadgetID, wrap)
  Protected Size.NSSize
  Protected Container.i = CocoaMessage(0, EditorGadgetID, "textContainer")
  CocoaMessage(@Size, Container, "containerSize")
  If wrap
    Size\width = GadgetWidth(0) - 2
  Else
    Size\width = $FFFF
  EndIf
  CocoaMessage(0, Container, "setContainerSize:@", @Size)
EndProcedure

OpenWindow(0, 270, 100, 250, 110, "Word Wrap Test", #PB_Window_SystemMenu)
EditorGadget(0, 10, 10, 230, 60)
ButtonGadget(1, 60, 80, 140, 25, "Toggle Word Wrap")

For i = 1 To 5
  Text$ = Text$ + "This is a word wrap test - "
Next i

SetGadgetText(0, Text$)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        WordWrap ! 1
        EditorGadget_SetWordWrap(GadgetID(0), WordWrap)
      EndIf
  EndSelect
ForEver
Last edited by wilbert on Mon Jan 07, 2013 2:03 pm, edited 1 time in total.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Kukulkan »

Thanks! Seem to work here. It is a shame that the EditorGadget does not support such flag...
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 »

Calling AppleScript

Code: Select all

Procedure.s AppleScript(Script.s)
  Protected retVal.s, strVal, numItems, i
  Protected aScript = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSAppleScript alloc"), "initWithSource:$", @Script), "autorelease")
  Protected eventDesc = CocoaMessage(0, aScript, "executeAndReturnError:", #nil)
  If eventDesc
    numItems = CocoaMessage(0, eventDesc, "numberOfItems")
    If numItems
      For i = 1 To numItems
        strVal = CocoaMessage(0, CocoaMessage(0, eventDesc, "descriptorAtIndex:", i), "stringValue")
        If strVal
          retVal + PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8)
          If i <> numItems : retVal + #LF$ : EndIf
        EndIf
      Next
    Else
      strVal = CocoaMessage(0, eventDesc, "stringValue")
      If strVal : retVal = PeekS(CocoaMessage(0, strVal, "UTF8String"), -1, #PB_UTF8) : EndIf
    EndIf
  EndIf
  ProcedureReturn retVal 
EndProcedure

MessageRequester("", AppleScript("tell application " + Chr(34) + "Finder" + Chr(34) + " To get the name of every item in the desktop"))
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 »

Window live resize

Code: Select all

; *** import the required functions ***

ImportC ""
  sel_registerName(str.p-ascii)
  class_addMethod(class, selector, imp, types.p-ascii)
EndImport

; *** required variables ***

Global notificationCenter = CocoaMessage(0, 0, "NSNotificationCenter defaultCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")

Define selector


; *** Window0 resize callback ***

ProcedureC Window0_Resize(obj, sel, notification)
  ResizeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
EndProcedure


; *** main code ***

If OpenWindow(0, 0, 0, 320, 170, "Window live resize", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  
  EditorGadget(0, 10, 10, 300, 150)
  
  selector = sel_registerName("Window0_Resize:")
  class_addMethod(delegateClass, selector, @Window0_Resize(), "v@:@")
  CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWindowDidResizeNotification", "object:", WindowID(0))
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWindowDidResizeNotification", "object:", #nil)
Edit : For newer versions of PureBasic, use BindEvent()
Last edited by wilbert on Fri Jan 10, 2014 7:54 am, edited 2 times in total.
Fred
Administrator
Administrator
Posts: 16622
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Fred »

Don't forget to remove the observer when the window is closed. You can also use SetWindowCallback() in 5.00
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 »

Fred wrote:Don't forget to remove the observer when the window is closed. You can also use SetWindowCallback() in 5.00
I added the code to remove the observer.

Can you document the SetWindowCallback() please since it seems to be incompatible with the Windows example in the help file.
Fred
Administrator
Administrator
Posts: 16622
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [PB Cocoa] Methods, Tips & Tricks

Post by Fred »

The call is like this:

Code: Select all

Procedure WindowCallback(Event, Window, Gadget, EventType)
EndProcedure
Not all events are supported, only those which needs a live tracking (like size, splitter)
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 »

Thanks Fred, it's working fine.

My previous example, now with SetWindowCallback()

Code: Select all

; *** Window callback ***

ProcedureC WindowCallback(Event, Window, Gadget, EventType)
  If Event = #PB_Event_SizeWindow
    If Window = 0
      ResizeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
    EndIf
  EndIf
EndProcedure


; *** main code ***

If OpenWindow(0, 0, 0, 320, 170, "Window live resize", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  
  EditorGadget(0, 10, 10, 300, 150)
  
  SetWindowCallback(@WindowCallback()); activate the callback
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Wed Oct 17, 2012 6:11 pm, 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 »

Check modifier keys

Code: Select all

#NSKeyDown            = 10
#NSKeyUp              = 11
#NSFlagsChanged       = 12

#NSAlphaShiftKeyMask = 1 << 16
#NSShiftKeyMask      = 1 << 17
#NSControlKeyMask    = 1 << 18
#NSAlternateKeyMask  = 1 << 19
#NSCommandKeyMask    = 1 << 20

Global sharedApplication = CocoaMessage(0, 0, "NSApplication sharedApplication")
Define currentEvent, type, modifierFlags


If OpenWindow(0, 0, 0, 320, 170, "Test modifierFlags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  EditorGadget(0, 10, 10, 300, 150)
  
  Repeat
    
    Event = WaitWindowEvent()
    currentEvent = CocoaMessage(0, sharedApplication, "currentEvent")
    If currentEvent
      type = CocoaMessage(0, currentEvent, "type")
      If type = #NSFlagsChanged
        modifierFlags = CocoaMessage(0, currentEvent, "modifierFlags")
        
        SetGadgetText(0, "Modifier keys pressed")
        AddGadgetItem(0, -1, "=====================")
        If modifierFlags & #NSAlphaShiftKeyMask  
          AddGadgetItem(0, -1, "Caps lock is on")
        Else
          AddGadgetItem(0, -1, "Caps lock is off")
        EndIf
        If modifierFlags & #NSShiftKeyMask
          AddGadgetItem(0, -1, "Shift key is pressed")
        EndIf
        If modifierFlags & #NSControlKeyMask
          AddGadgetItem(0, -1, "Ctrl key is pressed")
        EndIf
        If modifierFlags & #NSAlternateKeyMask
          AddGadgetItem(0, -1, "Alt key is pressed")
        EndIf
        If modifierFlags & #NSCommandKeyMask
          AddGadgetItem(0, -1, "Cmd key is pressed")
        EndIf
        
      EndIf
    EndIf
    
  Until Event = #PB_Event_CloseWindow
EndIf
Last edited by wilbert on Sun Oct 21, 2012 6:54 am, edited 1 time in total.
Post Reply