Manually creating a Cocoa control - What's missing?
Posted: Thu Oct 31, 2013 6:22 pm
As I would like to use Cocoa controls which are not provided by PureBasic directly, I evaluated the possibilities of creating them from code within a PureBasic (5.11 64 Bit) application. I tested it already from within an Xcode (5.0.1) project. The following code (inserted into AppDelegate's implementation method applicationDidFinishLaunching) works fine:
However, if I attempt to mimic this from within a PureBasic application, the button seems to exist but not being visible. Try this short example:
Is it possible that PureBasic's windows are working slightly different concerning the contentView? Or am I just messing something up again with pointers'n'stuff? 
Code: Select all
NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 130, 40)];
[[[self window] contentView] addSubview: myButton];
[myButton setTitle: @"Button title!"];
Code: Select all
EnableExplicit
OpenWindow( 0, 0, 0, 600, 400, "Manual Cocoa Controls", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget )
Global *Window = WindowID( 0 )
Global *Frame.NSRect = AllocateMemory( SizeOf( NSRect ) )
*Frame\origin\x = 100;
*Frame\origin\y = 100;
*Frame\size\width = 100;
*Frame\size\height = 25;
Global *Button
CocoaMessage( @*Button, #Null, "NSButton alloc" )
Debug "Button address: " + *Button
Global InitResult.i
CocoaMessage( @InitResult, *Button, "initWithFrame:", *Frame )
Debug "Initialization result: "+ Str( InitResult )
Global *ButtonFrameTest.NSRect
CocoaMessage( @*ButtonFrameTest, *Button, "frame" )
Debug "Button frame: " + Str( *ButtonFrameTest\origin\x ) + ", " + Str( *ButtonFrameTest\origin\y ) + ", " + Str( *ButtonFrameTest\size\width ) + ", " + Str( *ButtonFrameTest\size\height )
Global *ContentView
CocoaMessage( @*ContentView, *Window, "contentView" )
Debug "Content view: " + Str( *ContentView )
Global AddSubviewResult
CocoaMessage( @AddSubviewResult, *ContentView, "addSubview:", *Button )
Debug "AddSubviewResult: " + AddSubviewResult
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
EndSelect
ForEver
