Page 3 of 7

Re: [PB Cocoa] Cocoa companion library

Posted: Sun Aug 12, 2012 11:49 am
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...

Re: [PB Cocoa] Cocoa companion library

Posted: Sun Aug 12, 2012 12:04 pm
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)

Re: [PB Cocoa] Cocoa companion library

Posted: Sun Aug 12, 2012 1:41 pm
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

Re: [PB Cocoa] Cocoa companion library

Posted: Sun Aug 12, 2012 10:45 pm
by jamirokwai
Wow. Great work!
Thanks a lot.

Re: [PB Cocoa] Cocoa companion library

Posted: Mon Aug 13, 2012 10:46 am
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.

Re: [PB Cocoa] Cocoa companion library

Posted: Wed Aug 15, 2012 7:57 am
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

Re: [PB Cocoa] Cocoa companion library

Posted: Sat Aug 18, 2012 6:50 am
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

Re: [PB Cocoa] Cocoa companion library

Posted: Sat Aug 18, 2012 7:59 am
by J. Baker
Thanks Wilbert! Really loving this lib! ;)

Re: [PB Cocoa] Cocoa companion library

Posted: Sun Aug 19, 2012 5:01 pm
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.

Re: [PB Cocoa] Cocoa companion library

Posted: Sun Aug 19, 2012 6:08 pm
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

Re: [PB Cocoa] Cocoa companion library

Posted: Tue Aug 21, 2012 1:56 am
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

Re: [PB Cocoa] Cocoa companion library

Posted: Tue Aug 21, 2012 6:14 am
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.

Re: [PB Cocoa] Cocoa companion library

Posted: Tue Aug 21, 2012 10:54 am
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

Re: [PB Cocoa] Cocoa companion library

Posted: Tue Aug 21, 2012 12:22 pm
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).

Re: [PB Cocoa] Cocoa companion library

Posted: Tue Aug 21, 2012 2:15 pm
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??