Hello,
I've spent a good part of the day trying to figure out how to implement an NSWindow 'windowWillResize:toSize:' delegate in an existing TForm class.
After many trials and errors and wiki posts, this is what I came out with:
unit PreviewWin;
{$mode objfpc}
{$H+}{$LongStrings On}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
interface
uses
Classes, SysUtils, LazFileUtils, LResources, LMessages, Forms, Controls, Graphics,
Dialogs, ComCtrls, ExtCtrls, StdCtrls,
CocoaWindows, MacOSAll, CocoaAll;
type
{ TPreviewWindowDelegate }
TPreviewWindowDelegate = objcclass(NSObject, NSWindowDelegateProtocol)
public
function windowWillResize_toSize(sender: NSWindow; frameSize: NSSize): NSSize; message 'windowWillResize:toSize:';
end;
{ TFrm_Preview }
TFrm_Preview = class(TForm)
private
{ private declarations }
FPreserveAspect: Bool;
FAspectRatio: Double;
previewWindowDelegate: TPreviewWindowDelegate;
end;
var
Frm_Preview: TFrm_Preview;
implementation
{$R *.lfm}
{ TPreviewWindowDelegate }
function TPreviewWindowDelegate.windowWillResize_toSize(sender: NSWindow; frameSize: NSSize): NSSize;
begin
result := frameSize;
if Frm_Preview.FPreserveAspect then
begin
// forse result to match forced aspectRatio size
result.height := result.width / Frm_Preview.FAspectRatio
end;
end;
{ TFrm_Preview }
procedure TFrm_Preview.FormCreate(Sender: TObject);
var
win: NSWindow;
begin
previewWindowDelegate := TPreviewWindowDelegate.alloc.init();
//NSWindow(Self.Handle).setDelegate(previewWindowDelegate); // is this enough instead of the following code?
win := TCocoaWindowContent(Self.Handle).lclOwnWindow;
if Assigned(win) then
begin
win.setDelegate(previewWindowDelegate);
end;
FPreserveAspect := True;
FAspectRatio := 4 / 3;
end;
procedure TFrm_Preview.FormDestroy(Sender: TObject);
begin
previewWindowDelegate.release;
end;
... but it still not building and spits some generic unknown error and I don't know what else to try...
previewwin.pas(46,1) Error: Internal error 200609171Can you please help me understand what's wrong and how to do it correctly?
edit: Lazarus 2.1.0 r64158M FPC 3.2.0 x86_64-darwin-cocoa