Recent

Author Topic: Objective Pascal TForm delegate 'windowWillResize:toSize:'  (Read 633 times)

Shebuka

  • Sr. Member
  • ****
  • Posts: 425
Objective Pascal TForm delegate 'windowWillResize:toSize:'
« on: February 11, 2021, 05:53:15 pm »
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:

Code: Pascal  [Select][+][-]
  1. unit PreviewWin;
  2.  
  3. {$mode objfpc}
  4. {$H+}{$LongStrings On}
  5.  
  6. {$modeswitch objectivec1}
  7. {$modeswitch objectivec2}
  8.  
  9. interface
  10.  
  11. uses
  12.   Classes, SysUtils, LazFileUtils, LResources, LMessages, Forms, Controls, Graphics,
  13.   Dialogs, ComCtrls, ExtCtrls, StdCtrls,
  14.   CocoaWindows, MacOSAll, CocoaAll;
  15.  
  16. type
  17.   { TPreviewWindowDelegate }
  18.  
  19.   TPreviewWindowDelegate = objcclass(NSObject, NSWindowDelegateProtocol)
  20.   public
  21.     function windowWillResize_toSize(sender: NSWindow; frameSize: NSSize): NSSize; message 'windowWillResize:toSize:';
  22.   end;
  23.  
  24.   { TFrm_Preview }
  25.  
  26.   TFrm_Preview = class(TForm)
  27.   private
  28.     { private declarations }    
  29.     FPreserveAspect: Bool;
  30.     FAspectRatio: Double;
  31.  
  32.     previewWindowDelegate: TPreviewWindowDelegate;  
  33.   end;
  34.  
  35. var
  36.   Frm_Preview: TFrm_Preview;
  37.  
  38.  
  39. implementation
  40.  
  41. {$R *.lfm}
  42.  
  43.  
  44. { TPreviewWindowDelegate }
  45.  
  46. function TPreviewWindowDelegate.windowWillResize_toSize(sender: NSWindow; frameSize: NSSize): NSSize;
  47. begin
  48.   result := frameSize;
  49.  
  50.   if Frm_Preview.FPreserveAspect then
  51.   begin
  52.     // forse result to match forced aspectRatio size
  53.     result.height := result.width / Frm_Preview.FAspectRatio
  54.   end;
  55. end;
  56.  
  57.  
  58. { TFrm_Preview }
  59.  
  60. procedure TFrm_Preview.FormCreate(Sender: TObject);
  61. var                                    
  62.   win: NSWindow;
  63. begin
  64.   previewWindowDelegate := TPreviewWindowDelegate.alloc.init();
  65.  
  66.   //NSWindow(Self.Handle).setDelegate(previewWindowDelegate);   // is this enough instead of the following code?
  67.  
  68.   win := TCocoaWindowContent(Self.Handle).lclOwnWindow;
  69.   if Assigned(win) then
  70.   begin
  71.     win.setDelegate(previewWindowDelegate);
  72.   end;
  73.  
  74.   FPreserveAspect := True;
  75.   FAspectRatio := 4 / 3;  
  76. end;
  77.  
  78. procedure TFrm_Preview.FormDestroy(Sender: TObject);
  79. begin
  80.   previewWindowDelegate.release;
  81. end;
  82.  

... 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 200609171

Can 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
« Last Edit: February 12, 2021, 01:00:22 pm by Shebuka »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: Objective Pascal TForm delegate 'windowWillResize:toSize:'
« Reply #1 on: February 11, 2021, 08:07:36 pm »
try to disable debug info and recompile the project
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Seenkao

  • Full Member
  • ***
  • Posts: 151
Re: Objective Pascal TForm delegate 'windowWillResize:toSize:'
« Reply #2 on: February 11, 2021, 09:40:28 pm »
try to disable debug info and recompile the project
Может эту проблему уже начнут решать? С каких пор, считается, что если включена отладка, то код с включенной отладкой может выдавать ошибки?

Eng: Maybe this problem will already be solved? Since when is it believed that if debugging is enabled, then code with debugging enabled can generate errors?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 867
Re: Objective Pascal TForm delegate 'windowWillResize:toSize:'
« Reply #3 on: February 11, 2021, 11:30:52 pm »
The internal error comes from using DWARFv3 (-gw3). There's indeed a bug in the compiler related to generating debug information for Objective-Pascal with that format. Switching to DWARFv2 (-gw) will fix that.
« Last Edit: February 13, 2021, 09:08:27 am by Jonas Maebe »

Shebuka

  • Sr. Member
  • ****
  • Posts: 425
Re: Objective Pascal TForm delegate 'windowWillResize:toSize:'
« Reply #4 on: February 12, 2021, 12:54:54 pm »
The internal error comes from using DWARFv3 (-gw3). There's indeed a bug in the compiler related to generating debug information for Objective-Pascal with that format. Switching to DWARFv3 (-gw) will fix that.

Setting 'Type of debug info' to 'Dwarf with sets (-gw -godwarfsets)' solved the build problem and now the app builds and the form is resizing with given aspect ratio.

But I also have several errors:

Compile Project, Mode: Debug, Target: XXXXXX: Success, Errors: 8
Error: <stdin>:181956:10: warning: section "__datacoal_nt" is deprecated
Error: .section __DATA, __datacoal_nt, coalesced
Error: <stdin>:181956:10: note: change section name to "__data"
Error: .section __DATA, __datacoal_nt, coalesced
Error: <stdin>:182146:10: warning: section "__datacoal_nt" is deprecated
Error: .section __DATA, __datacoal_nt, coalesced
Error: <stdin>:182146:10: note: change section name to "__data"
Error: .section __DATA, __datacoal_nt, coalesced


Do I simply ignore them?

Shebuka

  • Sr. Member
  • ****
  • Posts: 425
Re: Objective Pascal TForm delegate 'windowWillResize:toSize:'
« Reply #5 on: February 12, 2021, 12:59:29 pm »
I also don't fully understand from different snippets if the Handle is the actual NSWindow or if I need to get the lclOwnWindow out of handle?

Code: Pascal  [Select][+][-]
  1.   NSWindow(Self.Handle).setDelegate(previewWindowDelegate);

VS

Code: Pascal  [Select][+][-]
  1.   win := TCocoaWindowContent(Self.Handle).lclOwnWindow;
  2.   if Assigned(win) then
  3.   begin
  4.     win.setDelegate(previewWindowDelegate);
  5.   end;

What's (more) correct?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 867
Re: Objective Pascal TForm delegate 'windowWillResize:toSize:'
« Reply #6 on: February 13, 2021, 09:08:09 am »
But I also have several errors:

Compile Project, Mode: Debug, Target: XXXXXX: Success, Errors: 8
Error: <stdin>:181956:10: warning: section "__datacoal_nt" is deprecated
Error: .section __DATA, __datacoal_nt, coalesced
Error: <stdin>:181956:10: note: change section name to "__data"
Error: .section __DATA, __datacoal_nt, coalesced
Error: <stdin>:182146:10: warning: section "__datacoal_nt" is deprecated
Error: .section __DATA, __datacoal_nt, coalesced
Error: <stdin>:182146:10: note: change section name to "__data"
Error: .section __DATA, __datacoal_nt, coalesced


Do I simply ignore them?
Yes. These are actually (harmless) warnings from the assembler, but for some reason Lazarus shows them as errors.

 

TinyPortal © 2005-2018