Creating Cocoa window after app.run lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- mullich · 2018-07-11 12:22 Hi, I'm trying to show a window on button click. If I create a window beforehand (line 57 in the code below), everything OK. But if I create a window after calling NSApp.run, it hides. Here is the code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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(N SMakeRect(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.                                       Am i doing something wrong? How I can create and show new window in a runtime? ---------------------------------------------------------------------- Phil · 2018-07-11 16:04 Quote from: mullich on July 11, 2018, 02:22:09 pmAm i doing something wrong? How I can create and show new window in a runtime? Probably the autorelease call on window2 is causing the window to be released prematurely. By default, I believe, the window will be released when it is closed. As a result of that, you probably also don't want the check for nil in your button handler - it needs to be created each time since the previously created windows was released when closed. Do you want to show window2 modally? If so use NSApp.runModalForWindow / NSApp.stopModal or NSApp.beginSheet_modalForWindow_... / NSApp.endSheet. Or look at the code in Lazarus LCL implementation for Cocoa (cocoawsforms.pp) for the various ways contemplated there. Inevitable questions: Why are trying to roll your own instead of using... (1) Lazarus LCL with Cocoa widgetset (2) Xcode with Pascal (see ProjectXC here: https://macpgmr.github.io) ---------------------------------------------------------------------- mullich · 2018-07-12 11:40 QuoteProbably the autorelease call on window2 is causing the window to be released prematurely. Thank you so much! That settles it. Everything works now. As for you questions, I'm making a multiwindow app with my own controls, some windows will be modal, some not, and each will have a OpenGlView on it. I've tried using runmodalforwindow method, but for some reason, OpenGl doesn't work with it. ---------------------------------------------------------------------- Phil · 2018-07-12 12:14 Quote from: mullich on July 12, 2018, 01:40:03 pmI've tried using runmodalforwindow method, but for some reason, OpenGl doesn't work with it. I'm not familiar with OpenGL. One thing you could test would be a modal Lazarus LCL form with your OpenGL code in it, just to see if it's any different. Be sure to compile for the Cocoa widgetset and Lazarus trunk SVN code would be ideal.