Recent

Author Topic: delphi conversion first impression + suggestion  (Read 2551 times)

mikeg

  • Newbie
  • Posts: 3
delphi conversion first impression + suggestion
« on: September 22, 2020, 10:57:31 pm »
I've used Delphi since 1994, but never updated beyond Delphi 6.

I recently decided to convert a simple Delphi 6 drawgrid program to Lazarus/FreePascal to make it 64-bit.

The only time I previously used Lazarus/FreePascal was in 2011 to convert a 32-bit DLL to 64-bit.  As I recall, at that time you had to install Lazarus and FreePascal separately.

This time I was pleasantly surprised to find a single download let me install everything I needed, and the Delphi conversion menu option made the conversion relatively quick and easy.

My first compilation failed because a Windows globalalloc() and globalfree() call was being made.  It wasn't clear to me why the conversion didn't include the windows.pas unit like the original program did, so I added windows.pas to my uses and tried compiling again.

Now various trect references failed, like drawgrid.selection := tgridrect(rect(...

Apparently a Windows trect and a FreePascal trect weren't the same, for some unclear reason.  The easiest solution was to remove windows.pas from my uses, and change globalalloc()/globalfree() to getmem()/freemem().  Now the program compiled and ran.

Unfortunately, grid row selection wasn't working properly.  My code used the .VisibleRowCount property to compute the middle row in the grid, and that property was always returning zero.  Apparently the property is declared but not actually implemented.

It would probably be much better if declared but unimplemented features caused an exception, otherwise bugs are introduced.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: delphi conversion first impression + suggestion
« Reply #1 on: September 22, 2020, 11:35:06 pm »
Hi!

The Trect problem is a known problem:

TRect is defined in unit Types and in unit Windows.

Do a explicit call to Windows.Trect and this trouble is gone.

Winni

PS.: .VisibleRowCount is implemented but does another rounding than Delphi:
In Delphi only the complete rows are counted. In Lazarus the partial shown rows are also counted.

So in 95% you can do Lazarus.VisibleRows := Delphi.Visible.Rows + 1.
But sometimes this will fail.

« Last Edit: September 22, 2020, 11:39:54 pm by winni »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4458
  • I like bugs.
Re: delphi conversion first impression + suggestion
« Reply #2 on: September 23, 2020, 01:24:22 am »
My first compilation failed because a Windows globalalloc() and globalfree() call was being made.  It wasn't clear to me why the conversion didn't include the windows.pas unit like the original program did, so I added windows.pas to my uses and tried compiling again.
The converter by default does a cross-platform project, but you can turn the setting off to support only Windows.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

mikeg

  • Newbie
  • Posts: 3
Re: delphi conversion first impression + suggestion
« Reply #3 on: September 23, 2020, 03:08:39 am »
Turns out the problem wasn't .VisibleRowCount, but a difference in grid selection.

In Delphi 6, DrawGrid1.Selection := tgridrect(rect(1,3,4,3)); selects row 3 regardless of the goRangeSelect option being true or false.

In FreePascal, DrawGrid1.Selection := tgridrect(rect(1,3,4,3)); selects row 1 if goRangeSelect is false, and shows two selections if goRangeSelect is true.

This can be demonstrated by setting .Selection in FormCreate() in a new project with only a default DrawGrid1 component and goRowSelect set true.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: delphi conversion first impression + suggestion
« Reply #4 on: September 23, 2020, 09:26:48 am »
Now various trect references failed, like drawgrid.selection := tgridrect(rect(...

Apparently a Windows trect and a FreePascal trect weren't the same, for some unclear reason.  The easiest solution was to remove windows.pas from my uses, and change globalalloc()/globalfree() to getmem()/freemem().  Now the program compiled and ran.

In those cases you need to put the Windows unit before the Types unit. This way the usual Types.TRect is found first.

Though at least in FPC 3.0.4 and newer Windows.TRect and Types.TRect should be the same (the later forwards to the former), thus I wonder what is wrong here...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: delphi conversion first impression + suggestion
« Reply #5 on: September 23, 2020, 06:20:49 pm »
Hi!

The Trect problem is a known problem:

TRect is defined in unit Types and in unit Windows.

Only in 3.0.2 and earlier.

For win32/win64/wince types.pp contains

  TRect  = Windows.TRect;

afaik in 3.0.4 and up.

Probably it is "rect" being a (t)rect creation macro translated to procedure in windows, vs an alias fro trect.

Iow pay attention to rect vs trect for the easiest way to resolve it.
« Last Edit: September 23, 2020, 06:22:42 pm by marcov »

mikeg

  • Newbie
  • Posts: 3
Re: delphi conversion first impression + suggestion
« Reply #6 on: September 24, 2020, 05:45:06 pm »
The trect issue was only a compile-time problem circumvented by removing windows.pas from the uses.

What remains is a run-time discrepancy between Delphi 6 and FreePascal in what DrawGrid1.Selection does.  To demonstrate, create a new GUI application.  Drop a DrawGrid on the canvas.  Put DrawGrid1.Selection := tgridrect(rect(1,3,4,3)); in FormCreate().

Delphi 6 selects row 3 regardless of the goRangeSelect option being true or false.

FreePascal selects row 1 if goRangeSelect is false, and shows two selections if goRangeSelect is true.

I don't know if that would be considered a bug or a feature.

 

TinyPortal © 2005-2018