[PB Cocoa] Cocoa companion library

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

Re: [PB Cocoa] Cocoa companion library

Post by Shardik »

Fred wrote:You can use a WindowedScreen(), and use glcommand in it (indeed you need to use opengl subsystem on windows). Should work on all OS
No, it's not necessary to use opengl as a subsystem in Windows. I have demonstrated this already in my cross-platform OpenGL example. kenmo even reported:
kenmo wrote:It works perfectly for me, under XP Pro, on a system which PB's opengl subsystem does NOT work! Interesting.

(When I choose the "opengl" subsystem, all 2D Drawing and Sprites show up as blank white... apparently I'm the only one here with this problem :? )
Unfortunately my example currently doesn't work with the Mac cocoa version PB 4.70 Beta 1.5 x86 and x64 although with carbon subsystem on x86 it does. With cocoa the window is displayed and all functions report success but the triangle isn't displayed...
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

Shardik wrote:Unfortunately my example currently doesn't work with the Mac cocoa version PB 4.70 Beta 1.5 x86 and x64 although with carbon subsystem on x86 it does. With cocoa the window is displayed and all functions report success but the triangle isn't displayed...
Cocoa uses NSGL, Carbon uses AGL. Maybe that's the reason ? (just guessing, I have no knowledge of OpenGL)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB Cocoa] Cocoa companion library

Post by Shardik »

wilbert wrote:Cocoa uses NSGL, Carbon uses AGL. Maybe that's the reason ? (just guessing, I have no knowledge of OpenGL)
I think you may be right. Unfortunately I have no knowledge of neither how to integrate cocoa funktions into PureBasic nor am I an expert in OpenGL. The linked example was my first try... :lol:

But the following example utilizes a WindowedScreen and - like Fred stated - it only needs a WindowedScreen and gl commands to render OpenGL graphics. My linked example seems to have made things more complicate than needed... :wink:
It runs on Macs with PB 4.61, PB 4.70 Beta 1.5 x86 and x64 and with PB 4.70 Beta 1.5 x86 with subsystem carbon:

Code: Select all

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW  = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4

EnableExplicit

ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
  glBegin_(PrimitiveID.I) As "_glBegin"
  glClear_(BuffersToClear.I) As "_glClear"
  glColor3f_(Red.F, Green.F, Blue.F) As "_glColor3f"
  glEnd_() As "_glEnd"
  glLoadIdentity_() As "_glLoadIdentity"
  glMatrixMode_(Target.I) As "_glMatrixMode"
  glTranslatef_(x.F, y.F, z.F) As "_glTranslatef"
  gluPerspective_(FieldOfViewAngle.D, WidthToHeightRatio.D, DistanceToNearPlane.D, DistanceToFarPlane.D) As "_gluPerspective"
  glVertex3f_(x.F, y.F, z.F) As "_glVertex3f"
  glViewport_(x.I, y.I, Width.I, Height.I) As "_glViewport"
EndImport

If InitSprite() = 0
  MessageRequester("Error", "Can't open screen & sprite environment!")
  End
EndIf

OpenWindow(0, 270, 100, 640, 480, "MacOS X Cocoa & Carbon OpenGL demo", #PB_Window_SystemMenu)
SetWindowColor(0, 0)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

Repeat
  glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
  glMatrixMode_(#GL_PROJECTION)
  glLoadIdentity_()
  gluPerspective_(30.0, Abs(WindowWidth(0) / WindowHeight(0)), 0.1, 500.0)
  glMatrixMode_(#GL_MODELVIEW)
  glLoadIdentity_()
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glLoadIdentity_()
  glTranslatef_(0.0, 0.0, -8.0)
  glColor3f_(1.0, 1.0, 1.0)
  glBegin_(#GL_TRIANGLES)      ; Start drawing a triangle
  glColor3f_ ( 1.0,  0.0, 0.0) ; Set top point of triangle to Red
  glVertex3f_( 0.0,  1.0, 0.0) ; First point of the triangle
  glColor3f_ ( 0.0,  1.0, 0.0) ; Set left point of triangle to Green
  glVertex3f_(-1.0, -1.0, 0.0) ; Second point of the triangle
  glColor3f_ ( 0.0,  0.0, 1.0) ; Set right point of triangle to Blue
  glVertex3f_( 1.0, -1.0, 0.0) ; Third point of the triangle
  glEnd_()                     ; Done drawing the triangle
  FlipBuffers()
Until WaitWindowEvent() = #PB_Event_CloseWindow
jamirokwai
Enthusiast
Enthusiast
Posts: 796
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: [PB Cocoa] Cocoa companion library

Post by jamirokwai »

Wow. Great work!
Thanks a lot.
Regards,
JamiroKwai
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

jesperbrannmark wrote:When you are talking about "opengl" text in a normal window.
I dont really understand that. I have seen this in multiple Mac OS X applications that look great.
Image
Is this what you mean? If so... yes please :D
So like a warning message that fades in and fades out - semi transparent with white text.. very nice.
I was tempted to try to create something similar.
It was more complicated as I expected but I added a similar feature to my lib.

Message_OSD (Message.s [, FontSize.f]) - Display an OSD message.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

Added midi playback and image filter support.

MidiPlayer = Midi_Catch (*MemoryAddress, Size)
MidiPlayer = Midi_Load (FileName.s)
Midi_Play (MidiPlayer)
Midi_Release (MidiPlayer)
Midi_SendEvent (Status, Data1, Data2)
Midi_SetVolume (Volume.f)
Midi_Stop (MidiPlayer)

FilterObject = ImageFilter_ExposureAdjust (EV.f)
FilterObject = ImageFilter_GaussianBlur (Radius.f)
FilterObject = ImageFilter_Grayscale ()
FilterObject = ImageFilter_Posterize (Levels)
ImageObject = Image_FilteredImage (ImageObject, FilterObject, CropToSource)

Code: Select all

MidiPlayer = Midi_Load("test.mid")
Midi_Play(MidiPlayer)

Message_OSD("Playing MIDI")

If OpenWindow(0, 0, 0, 400, 300, "Midi Playback", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  Repeat 
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
  
EndIf
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

Added a few commands to extend the PureBasic menu capabilities.
They allow not only to enable / disable an item but also to hide it and to set a key equivalent for an item.

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.


Unfortunately MenuID() doesn't return a NSMenu object so currently when it comes to PureBasic created menus, you can only use the commands on the main menu (use 0 for MenuObject).

Code: Select all

If OpenWindow(0, 100, 150, 195, 260, "Menu")
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
    MenuItem( 1, "Load...")
    MenuItem( 2, "Save")
    MenuItem( 3, "Save As...")
  EndIf
  
  PreferencesItem = Menu_ItemAtIndex(0, 0, 2); preferences item is at index (0, 2) of main menu

  ; alternative approach ...
  ; PreferencesItem = Menu_ItemWithTitle(0, "Preferences", #False); find preferences item

  Menu_SetItemEnabled(PreferencesItem, #False); disable preferences
  
  SaveItem = Menu_ItemAtIndex(0, 1, 1); save iten is at index (1, 1) of main menu
  Menu_SetItemKey(SaveItem, "s", %0010); set Ctrl+S as key  ( Bit Mask = Command - Alt - Ctrl - Shift )
  Menu_SetItemToolTip(SaveItem, "This is the save item")
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
  Until Quit = 1
  
EndIf
Last edited by wilbert on Sat Aug 18, 2012 8:18 am, edited 2 times in total.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: [PB Cocoa] Cocoa companion library

Post by J. Baker »

Thanks Wilbert! Really loving this lib! ;)
www.posemotion.com

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


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

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

J. Baker wrote:Thanks Wilbert! Really loving this lib! ;)
I'm trying to put in some useful things :D

Added
Result.s = MainBundle_GetInfo(Key.s)
It allows you to quickly get information from the Info.plist file in the main bundle.

Code: Select all

Debug MainBundle_GetInfo("CFBundleSignature")
Could be useful in case you need to use the CFBundleIdentifier or some other information from that file inside your application.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: [PB Cocoa] Cocoa companion library

Post by J. Baker »

wilbert wrote:
J. Baker wrote:Thanks Wilbert! Really loving this lib! ;)
I'm trying to put in some useful things :D

Added
Result.s = MainBundle_GetInfo(Key.s)
It allows you to quickly get information from the Info.plist file in the main bundle.

Code: Select all

Debug MainBundle_GetInfo("CFBundleSignature")
Could be useful in case you need to use the CFBundleIdentifier or some other information from that file inside your application.
Nice idea! I'm sure it will come in handy for something. :D
www.posemotion.com

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


Even the vine knows it surroundings but the man with eyes does not.
DK5UR
User
User
Posts: 23
Joined: Mon Jun 23, 2008 9:44 pm
Location: Laubach

Re: [PB Cocoa] Cocoa companion library

Post by DK5UR »

Thanks for the work, it helps me a lot on my way to understand cocoa :)
Needless to say i'm a noob on MacOS :(
I'm stuck with the ToolBar right now.

Didn't have any idea how to change the image of a ToolBarImageButton, or figure out the x and y of a specific ToolBarButton to positioning a "dropdown" menu.

Take care

Heinz
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

DK5UR wrote:Didn't have any idea how to change the image of a ToolBarImageButton, or figure out the x and y of a specific ToolBarButton to positioning a "dropdown" menu.
PureBasic doesn't support these things.
You can try for yourself to access things through the Cocoa objects or I can add the functionality to my library if that would be useful.
DK5UR
User
User
Posts: 23
Joined: Mon Jun 23, 2008 9:44 pm
Location: Laubach

Re: [PB Cocoa] Cocoa companion library

Post by DK5UR »

wilbert wrote:PureBasic doesn't support these things.
I know that, in Windows i have found a way to do these things. But in cocoa it's not so easy, for me :cry:
wilbert wrote:... or I can add the functionality to my library if that would be useful.
It's an often requested feature for PB for years, so if you find a minute ... :D

Your source code is window into a new fantastic world, which helps me to understand how it works.

Heinz
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: [PB Cocoa] Cocoa companion library

Post by wilbert »

DK5UR wrote:Your source code is window into a new fantastic world, which helps me to understand how it works.
Never thought of it that way :)

Added

Result = ToolBar_GetItemPos (ToolbarItemObject) - Gets the horizontal coordinate for an item containing a gadget.
Result = 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.

DisplayMode :
1 = Icons and labels
2 = Icons only

SizeMode :
1 = 32 by 32 pixel icons.
2 = 24 by 24 pixel icons.

Code: Select all

If OpenWindow(0, 0, 0, 300, 200, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  StringGadget(0, 0, 0, 96, 32, "")
  
  CreateImage(0, 32, 32)
  StartDrawing(ImageOutput(0))
  Box(0,0,32,32,RGB(255,255,255))
  Box(4,4,24,24,RGB(255,0,0))
  StopDrawing()
  
  CreateToolBar(0, WindowID(0))
  TB_ID = ToolBarID(0)
  ToolBar_SetMode(TB_ID, 1, 1)
  
  ToolBarImageButton(0, ImageID(0))
  Item = ToolBar_ItemAtIndex(TB_ID, 0)
  ToolBar_SetItemLabel(Item, "Button")
  
  ToolBarImageButton(1, ImageID(0))
  Item = ToolBar_ItemAtIndex(TB_ID, 1)
  ToolBar_SetItemLabel(Item, "String gadget")
  ToolBar_SetItemGadget(Item, GadgetID(0))
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
  
EndIf
Getting the horizontal position unfortunately only works when an item has a gadget inside it.
If you set an item gadget, it is reparented (taken of the window and added to the toolbar item).
Last edited by wilbert on Tue Aug 21, 2012 3:04 pm, edited 1 time in total.
DK5UR
User
User
Posts: 23
Joined: Mon Jun 23, 2008 9:44 pm
Location: Laubach

Re: [PB Cocoa] Cocoa companion library

Post by DK5UR »

Thanks for the good work, wilbert.
Works like charm, but we should add another parameter to ToolBar_SetMode. setAllowsUserCustomization: is a good idea, otherwise i try to the change the image at the wrong ToolBarButton, in case the user has customized the ToolBar.

Code: Select all

void PB_ToolBar_SetMode(NSToolbar * toolbar, NSUInteger displayMode, NSUInteger sizeMode,BOOL userCustomization)
{
    [toolbar setDisplayMode:displayMode];
    [toolbar setSizeMode:sizeMode];
    [toolbar setAllowsUserCustomization:userCustomization];
}
Like this??
Post Reply