[PB Cocoa] Cocoa companion library

Mac OSX specific forum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

[PB Cocoa] Cocoa companion library

Post by wilbert »

A lib to work with the Cocoa version of PureBasic.
The lib supports both x86 and x64. PPC is not supported.

x86 / x64 static library archive : http://www.w73.nl/pb/cocoa/libPBCocoa.zip
help file : http://www.w73.nl/pb/cocoa/help.zip
source : http://www.w73.nl/pb/cocoa/source.zip

Code: Select all

Command list
============

oAutorelease (Object)
oAutoreleasePool ()
oClass (ClassName.s)
oClassAddMethod (Class, Selector, Imp, Types.s)
oClassExists (ClassName.s)
oClassName (Object)
oCreateClass (Superclass, ClassName.s)
oDescription (Object)
oInheritance (Object)
oRegisterClass (Class)
oRelease (Object)
oRetain (Object)
oSel (Selector.s)

AppleScript (Script.s) - Execute AppleScript code.

Application_MemoryUsage() - Returns the memory usage of the application in bytes.
Application_SetBadgeText (Text.s) - Sets the badge text of the application.
Application_Uptime () - Returns the uptime of the application in seconds.

DoubleToNSNumber (Number.d [, Autorelease]) - Convert Double to NSNumber.

Editor_CatchRTF (GadgetID, *MemoryAddress, Size) - Catch rtf from memory.
Editor_LoadRTF (GadgetID, FileName.s) - Loads a rtf file.
Editor_PrintContent (GadgetID) - Print editor content.
Editor_SetBackgroundPattern (GadgetID, ImageID) - Sets a pattern to use as a background.
Editor_SetHTMLText (GadgetID, HTMLText.s) - Sets HTML text.
Editor_SetWordWrap (GadgetID, Wrap) - Sets word wrap to #True or #False.

EnableFullScreenButton (WindowID) - Enable OSX 10.7+ full screen button.

Gadget_BringToFront (GadgetID) - Bring gadget to front.
Gadget_SendToBack (GadgetID) - Send gadget to back.
Gadget_SetFilter (GadgetID, FilterObject) - Sets a gadget filter.

Image_Catch (*MemoryAddress, Size) - Catch image from memory.
Image_FilteredImage (ImageObject, FilterObject, CropToSource)
Image_GetHeight (ImageObject) - Gets the height of the image object.
Image_GetWidth (ImageObject) - Gets the width of the image object.
Image_Load (FileName.s) - Load image.
Image_Save (ImageObject, FileName.s, Type [, Compression.f]) - Saves the image object.

ImageFilter_ExposureAdjust (EV.f)
ImageFilter_GaussianBlur (Radius.f)
ImageFilter_Grayscale ()
ImageFilter_Posterize (Levels)

Local_DecimalSeparator () - Returns the decimal separator.
Local_Language () - Returns the local language code.
Local_OffsetFromGMT () - Returns the offset in seconds from GMT.

MacOSVersion () - Returns the OS version.

MainBundle_GetInfo (Key.s) - Gets info from main bundle Info.plist file.
MainBundle_ResourcePath () - Returns the resource path.

Menu_ItemAtIndex (MenuObject, Index1 [, Index2 [, Index3 [, Index4]]]) - Get menu item for index. 
Menu_ItemWithTitle (MenuObject, Title.s, ExactMatch) - Search for item with title.
Menu_SetItemEnabled (MenuItemObject, Enabled) - Sets the item to disabled(0), enabled(1) or hidden(-1).
Menu_SetItemKey (MenuItemObject, Key.s, Modifier) - Sets the key equivalent for the item.
Menu_SetItemToolTip (MenuItemObject, Tip.s) - Sets a tip for the item.

Message_OSD (Message.s [, FontSize.f]) - Display an OSD message.

Midi_Catch (*MemoryAddress, Size) - Catch midi from memory.
Midi_Load (FileName.s) - Load midi.
Midi_Play (MidiPlayer [, Loop]) - Starts playing midi.
Midi_Release (MidiPlayer) - Release the MidiPlayer object.
Midi_SendEvent (Status, Data1, Data2)
Midi_SetVolume (Volume.f) - Sets the volume for midi playback.
Midi_Stop (MidiPlayer) - Stops playing midi.

NSApp () - Returns the application instance.
NSNumberToDouble (Number) - Convert NSNumber to Double.
NSNumberToQuad (Number) - Convert NSNumber to Quad.
NSStringToString (String) - Convert NSString to String.

Notification_Observe (Object, NotificationName.s)
Notification_RemoveObserve (Object, NotificationName.s)
Notification_SetCallback (@NotificationCallback())

PDFViewer_CatchPDF (PDFViewerObject, *MemoryAddress, Size) - Catch pdf from memory.
PDFViewer_Create (WindowID, x, y, width, height) - Create a pdf viewer object.
PDFViewer_LoadPDF (PDFViewerObject, FileName.s) - Loads a pdf file.

QuadToNSNumber (Number.q [, Autorelease]) - Convert Quad to NSNumber.

SearchField_Create (WindowID, x, y, width, height) - Create a search field object.
SearchField_GetText (SearchField) - Gets the text of the search field.

SetTargetActionCallback(Control, @Callback())

SlideShow_PresentImages (@ImageID_Array(), ArraySize, AutoPlay) - Presents a slideshow of images.
SlideShow_PresentFiles (@FileNames_Array(), ArraySize, AutoPlay) - Presents a slideshow of files.

Sound_Catch (*MemoryAddress, Size) - Catch sound from memory.
Sound_IsPlaying (SoundObject) - Returns #True is the sound object is playing.
Sound_Load (FileName.s) - Load sound.
Sound_Play (SoundObject [, Loop]) - Starts playing the sound object.
Sound_Release (SoundObject) - Releases the sound object.
Sound_SetVolume (SoundObject, Volume.f) - Sets the volume of the sound object.
Sound_Stop (SoundObject) - Stops playing the sound object.

StringToNSString (String.s [, Autorelease]) - Convert String to NSString.

ToolBar_GetItemPos (ToolbarItemObject) - Gets the horizontal coordinate for an item containing a gadget.
ToolBar_ItemAtIndex (ToolbarID, Index) - Gets toolbar item for index.
ToolBar_SetItemGadget (ToolbarItemObject, GadgetID) - Sets the toolbar item gadget.
ToolBar_SetItemImage (ToolbarItemObject, ImageID) - Sets the toolbar item image.
ToolBar_SetItemLabel (ToolbarItemObject, Label.s) - Sets the toolbar item label.
ToolBar_SetMode (ToolbarID, DisplayMode, SizeMode) - Sets the toolbar mode.

UUID_Create() - Returns a universally unique identifier.

Window_SetAlpha (WindowID, Alpha.f) - Sets the alpha value of the window.
Window_SetBackgroundPattern (WindowID, ImageID) - Sets a pattern to use as a background for a window.
Last edited by wilbert on Wed May 31, 2017 3:12 pm, edited 41 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by wilbert »

Simple example enabling Full Screen button on OSX 10.7+

Code: Select all

SoundName = StringToNSString("Purr")
PurrSound = oRetain(CocoaMessage(0, 0, "NSSound soundNamed:", SoundName))
oRelease(SoundName)

If OpenWindow(0, 0, 0, 600, 340, "OSX 10.7+ Full Screen", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  
  Window = WindowID(0)
  
  EnableFullScreenButton(Window)
  
  ButtonGadget(0, 10, 10, 200, 30, "Print Window")
  CocoaMessage(0, GadgetID(0), "setSound:", PurrSound)
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
        Case 0
          CocoaMessage(0, Window, "print:", 0)    
      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Sat Sep 15, 2012 3:11 pm, edited 3 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by wilbert »

More complex example creating a delegate class from within PB.

Code: Select all

EnableExplicit


Procedure.s GetFrameURL(frame, provisional = #False)
  Protected source
  If provisional
    CocoaMessage(@source, frame, "provisionalDataSource")
  Else
    CocoaMessage(@source, frame, "dataSource")
  EndIf
  Protected urlString = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, source, "request"), "URL"), "absoluteString")
  ProcedureReturn NSStringToString(urlString)
EndProcedure


ProcedureC DidStartProvisionalLoad(obj, sel, webview, frame); load starts
  Protected frameUrl.s = GetFrameURL(frame, #True)
  Debug "Load : " + frameUrl
  If frameUrl = "http://www.purebasic.com/news.php"
    CocoaMessage(0, frame, "stopLoading")
    MessageRequester("", "No news today!")
  EndIf
EndProcedure

ProcedureC ServerRedirect(obj, sel, webview, frame); server redirect
  Debug "Redirect : " + GetFrameURL(frame, #True)
EndProcedure

ProcedureC LoadComplete(obj, sel, webview, frame); load complete
  Debug "Finished loading : " + GetFrameURL(frame)
EndProcedure

ProcedureC Anchor(obj, sel, webview, frame); anchor within page
  Debug "Anchor : " + GetFrameURL(frame)
EndProcedure

Define FrameLoadDelegateClass = oCreateClass(oClass("NSObject"), "PB_FrameLoadDelegate"); PB_FrameLoadDelegate class extends NSObject
oClassAddMethod(FrameLoadDelegateClass, oSel("webView:didStartProvisionalLoadForFrame:"), @DidStartProvisionalLoad(), "v@:@@")
oClassAddMethod(FrameLoadDelegateClass, oSel("webView:didReceiveServerRedirectForProvisionalLoadForFrame:"), @ServerRedirect(), "v@:@@")
oClassAddMethod(FrameLoadDelegateClass, oSel("webView:didFinishLoadForFrame:"), @LoadComplete(), "v@:@@")
oClassAddMethod(FrameLoadDelegateClass, oSel("webView:didChangeLocationWithinPageForFrame:"), @Anchor(), "v@:@@")
oRegisterClass(FrameLoadDelegateClass)
Define FrameLoadDelegate = CocoaMessage(0, 0, "PB_FrameLoadDelegate new")




If OpenWindow(0, 0, 0, 600, 340, "Load Delegate", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ButtonGadget(0, 10, 10, 100, 30, "Load page")
  WebGadget(1, 10, 50, 580, 280, "")
  
  Define WebView = GadgetID(1)
  CocoaMessage(0, WebView, "setFrameLoadDelegate:", FrameLoadDelegate)
  
  Repeat
    Define EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
          
        Case 0
          SetGadgetText(1, "http://www.purebasic.com")
          
      EndSelect
    EndIf      
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Sat Sep 15, 2012 3:13 pm, edited 2 times in total.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by J. Baker »

Nice examples Wilbert! Love the full screen function! ;)

Now if a window screen could stretch this would be great for games too. Maybe that will get fixed in the next beta. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by wilbert »

J. Baker wrote:Now if a window screen could stretch this would be great for games too. Maybe that will get fixed in the next beta. :D
I forgot to enable resizing, updated the code example so the window should resize now to full screen.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by Polo »

Great for the fullscreen button!!
My mac just decided not to start up anymore, on the day of PB Cocoa release... I'm a little bit frustrated here!
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by J. Baker »

wilbert wrote:
J. Baker wrote:Now if a window screen could stretch this would be great for games too. Maybe that will get fixed in the next beta. :D
I forgot to enable resizing, updated the code example so the window should resize now to full screen.
Awesome! The window resized like it should. But a WindowScreen still doesn't but that's been an issue for some time now. Not really related to your code as your lib seems to be just fine. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Fred
Administrator
Administrator
Posts: 16680
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by Fred »

Nice add-ons
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by fsw »

This is pretty good stuff.
Thank you Wilbert :D

I am to provide the public with beneficial shocks.
Alfred Hitshock
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by wilbert »

Thanks for the comments :D

Added gadget reordering
If you click a button, it will be brought to the front.

Code: Select all

If OpenWindow(0, 0, 0, 600, 340, "Gadget Reordering", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  
  EnableFullScreenButton(WindowID(0))
  
  RandomSeed(0)
  For i = 0 To 30
    ButtonGadget(i, Random(500), Random(310), 100, 30, "Button " + Str(i))
  Next
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Gadget_BringToFront(GadgetID(EventGadget()))
    EndIf
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Also added simple notification observing now

Code: Select all

ProcedureC MyNotificationCallback(Object, Name.s, userInfo)
  
  Application_SetBadgeText(Str(Len(GetGadgetText(0))))
  Debug "Text did change"
  
EndProcedure

Notification_SetCallback(@MyNotificationCallback())

If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 8, 8, 306, 133)
  Notification_Observe(GadgetID(0), "NSTextDidChangeNotification")
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Sat Sep 15, 2012 3:14 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by wilbert »

I added Application_MemoryUsage() to check the amount of bytes the application is currently using.
It can be useful to find memory leaks.

I also made some changes that make the lib possibly also work on OS X 10.5 (intel).
If someone still has 10.5 on an intel mac, feel free to try and let me know if it works.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by J. Baker »

Very nice! Being able to check memory usage will come in handy. Thanks Wilbert! :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by Shardik »

J. Baker wrote:Being able to check memory usage will come in handy.
If you need to monitor memory usage in your own programs (like
the activity monitor does) I have already demonstrated how to do
this in Carbon programs using a mach kernel function:
http://www.purebasic.fr/english/viewtop ... 62&start=6
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cocoa companion library [PB 4.70+, OS X 10.6+]

Post by wilbert »

I didn't know about that thread Shardik.
Nevertheless, I still feel this library needs this kind of functionality built in.
It's very ease with Cocoa to forget to release an object and get a memory leak this way.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

Added several commands to extend the functionality of the editor gadget

Editor_CatchRTF (GadgetID, *MemoryAddress, Size) - Catch rtf from memory.
Editor_LoadRTF (GadgetID, FileName.s) - Loads a rtf file.
Editor_PrintContent (GadgetID) - Print editor content.
Editor_SetBackgroundPattern (GadgetID, ImageID) - Sets a pattern to use as a background.
Editor_SetHTMLText (GadgetID, HTMLText.s) - Sets HTML text.
Editor_SetWordWrap (GadgetID, Wrap) - Sets word wrap to #True or #False.


Example

Code: Select all

CreateImage(0, 16, 16)
StartDrawing(ImageOutput(0))
Box(0, 0, 16, 16, $ffffff)
Circle(8, 8, 6, $80f0f0)
StopDrawing()

If OpenWindow(0, 0, 0, 316, 216, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 8, 8, 300, 200)
  EditorID = GadgetID(0)
  
  Editor_SetBackgroundPattern(EditorID, ImageID(0))
  Editor_SetWordWrap(EditorID, #True)
  Editor_SetHTMLText(EditorID, "<font face='Helvetica' size='5'><u>HTML text output</u><br>Different styles by using <b>html</b> tags is easy with Cocoa.</font>")
  
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Post Reply