program cocoa1;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
uses
CocoaAll, classes, sysutils;
type
{ TMyDelegate }
TMyDelegate = objcclass(NSObject, NSWindowDelegateProtocol)
public
procedure HandleButtonClick_A(sender: id); message 'HandleButtonClick_A:';
end;
var
window: NSWindow;
window2: NSWindow;
lButton: NSButton;
lDelegate: TMyDelegate;
procedure Make2ndWnd;
begin
window2 := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 600, 600),
NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
NSBackingStoreBuffered, False).autorelease;
window2.center;
window2.settitle(NSSTR('2ndwnd'));
end;
procedure TMyDelegate.HandleButtonClick_A(sender: id);
begin
if window2=nil then Make2ndwnd;
window2.makeKeyAndorderfront(nil);
end;
begin
NSApp := NSApplication.sharedApplication;
NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 600, 600),
NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
NSBackingStoreBuffered, False).autorelease;
lDelegate := TMyDelegate.alloc.init;
window.setdelegate(lDelegate);
lButton:=NSButton.alloc.initWithFrame(NSMakeRect(50, 100, 120, 50)).autorelease;
window.contentView.addSubview(lButton);
lButton.setTitle(NSSTR('Show wnd'));
lButton.setButtonType(NSMomentaryLightButton);
lButton.setBezelStyle(NSRoundedBezelStyle);
lButton.setTarget(lDelegate);
lButton.setAction(ObjCSelector(lDelegate.HandleButtonClick_A));
//if NSWindow is created before NSApp.Run, then it shows; if it is created after NSApp.Run, then if hides
//Make2ndwnd;
window.center;
window.setTitle(NSSTR('1stwnd'));
window.makeKeyAndOrderFront(nil);
NSApp.activateIgnoringOtherApps(true);
NSApp.run;
end.