View Issue Details

IDProjectCategoryView StatusLast Update
0021041FPCRTLpublic2016-03-19 19:04
ReporterszaliAssigned ToMarco van de Voort 
PrioritynormalSeverityfeatureReproducibilityalways
Status resolvedResolutionfixed 
PlatformallOSallOS Versionall
Product VersionProduct Build 
Target VersionFixed in Version 
Summary0021041: Extend the TRect type with some useful methods
DescriptionI think TRect should at least have two *inline* methods for querying Width and Height - that would simplify some code A LOT! Not speaking of loads of new methods introduced by Delphi 2006(? as far as I remember). I attach the record header as published by Borland or what they are called now. I think there are plenty of useful methods there. (But maybe this feature is already planned.)
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files
  • advanced_trect.patch (2,959 bytes)
    Index: objpas/types.pp
    ===================================================================
    --- objpas/types.pp	(revision 32252)
    +++ objpas/types.pp	(working copy)
    @@ -90,6 +90,14 @@
       packed
     {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
       record
    +    private
    +      function getheight: integer;
    +      function getwidth: integer;
    +      procedure setheight(AValue: integer);
    +      procedure setwidth(AValue: integer);
    +    public
    +      property height:integer read getheight write setheight;
    +      property width:integer read getwidth write setwidth;
         case Integer of
           0: (Left,Top,Right,Bottom : Longint);
           1: (TopLeft,BottomRight : TPoint);
    @@ -478,5 +486,28 @@
     end;
     
     
    +{$ifndef Windows}
    +{ RECT }
     
    +function TRect.getheight: integer;
    +begin
    +  result:=bottom-top;
    +end;
    +
    +function TRect.getwidth: integer;
    +begin
    + result:=right-left;
    +end;
    +
    +procedure TRect.setheight(AValue: integer);
    +begin
    + right:=left+avalue;
    +end;
    +
    +procedure TRect.setwidth(AValue: integer);
    +begin
    + bottom:=top+avalue;
    +end;
    +{$endif}
    +
     end.
    Index: win/wininc/func.inc
    ===================================================================
    --- win/wininc/func.inc	(revision 32252)
    +++ win/wininc/func.inc	(working copy)
    @@ -2389,5 +2389,27 @@
       IMAGE_FIRST_SECTION:=PIMAGE_SECTION_HEADER(OFS(ntheader^.OptionalHeader) + ntheader^.FileHeader.SizeOfOptionalHeader);
     end;
     
    +{ RECT }
    +
    +function RECT.getheight: integer;
    +begin
    +  result:=bottom-top;
    +end;
    +
    +function RECT.getwidth: integer;
    +begin
    + result:=right-left;
    +end;
    +
    +procedure RECT.setheight(AValue: integer);
    +begin
    + right:=left+avalue;
    +end;
    +
    +procedure RECT.setwidth(AValue: integer);
    +begin
    + bottom:=top+avalue;
    +end;
    +
     {$endif read_implementation}
     
    Index: win/wininc/struct.inc
    ===================================================================
    --- win/wininc/struct.inc	(revision 32252)
    +++ win/wininc/struct.inc	(working copy)
    @@ -223,10 +223,20 @@
          TPOINT = POINT;
          PPOINT = ^POINT;
     
    +     { RECT }
    +
          RECT = record
    -          case Integer of
    -             0: (Left,Top,Right,Bottom : Longint);
    -             1: (TopLeft,BottomRight : TPoint);
    +     private
    +       function getheight: integer; inline;
    +       function getwidth: integer; inline;
    +       procedure setheight(AValue: integer);
    +       procedure setwidth(AValue: integer);
    +     public
    +       property height:integer read getheight write setheight;
    +       property width:integer read getwidth write setwidth;
    +       case Integer of
    +         0: (Left,Top,Right,Bottom : Longint);
    +         1: (TopLeft,BottomRight : TPoint);
            end;
          LPRECT = ^RECT;
          _RECT = RECT;
    Index: windows.pp
    ===================================================================
    --- windows.pp	(revision 32252)
    +++ windows.pp	(working copy)
    @@ -22,6 +22,7 @@
     
     { stuff like array of const is used }
     {$mode objfpc}
    +{$modeswitch ADVANCEDRECORDS}
     {$inline on}
     {$calling stdcall}
     
    
    advanced_trect.patch (2,959 bytes)

Relationships

related to 0029013 closedMarco van de Voort FPC Can't build Win64 version of compiler. 
has duplicate 0029148 resolvedMattias Gaertner Lazarus Lazarus(FPC?)'s TRect is poor 

Activities

Florian

2012-01-07 00:12

administrator   ~0055526

I've removed the file to avoid any copyright trouble, the needed info is available here: http://docwiki.embarcadero.com/VCL/en/Types.TRect

Paul Ishenin

2012-01-08 11:18

developer   ~0055579

Maybe to wait while records constructor are available

Hans-Peter Diettrich

2012-01-16 09:25

reporter   ~0055789

Last edited: 2012-01-16 09:34

Retyping TRect = Object instead of Record will allow to add methods.

The variant part can be emulated by properties or, when this causes problems with passing properties as var parameters, the Object type could be merged with the Record type inside the compiler.

Paul Ishenin

2012-01-20 09:42

developer   ~0055877

records allow to add methods. only constructors are not implemented

Marco van de Voort

2012-01-20 10:38

manager   ~0055882

Last edited: 2012-01-20 10:38

As said many times, tobject has a VMT option that record doesn't have (and probably is the reason for the creation of "methods in records"), and thus they can't be merged.

szali

2012-01-23 20:26

reporter   ~0055984

0055579:
I find Width and Height very-very useful syntactic sugar - and they could be added now, as functions. (I don't really know if records can have properties in Free Pascal.) There is no point in waiting for ctors IMHO.

Sven Barth

2012-01-23 20:53

manager   ~0055985

Paul wants to wait, because then all changes to TRect can be done at once. Until then you can use a record helper:

unit MyRectHelper;

{$mode objfpc}
{$modeswitch advancedrecords} // this modeswitch is not needed in the using unit

interface

uses
  Types;

type
  TRectHelper = record helper for TRect
  private
    function GetWidth: LongInt;
    function GetHeight: LongInt;
  public
    property Width: LongInt read GetWidth;
    property Height: LongInt read GetHeight;
  end;

implementation

function TRectHelper.GetWidth: LongInt;
begin
  Result := Right - Left;
end;

function TRectHelper.GetHeight: LongInt;
begin
  Result := Bottom - Top;
end;

end.

As long as MyRectUnit is in scope you can use the properties as if they'd belong to TRect:

var
  r: TRect;
begin
  r := Rect(3, 5, 7, 10);
  Writeln(r.Width, ' ', r.Height);
end.

Tested with 2.7.1 and 2.6.0.

Regards,
Sven

szali

2012-01-25 01:07

reporter   ~0056018

0055985:
OK, I understand.
Thanks anyway for publishing your code (I've already written a helper for myself but other people may find it useful as well).

Marco van de Voort

2015-11-06 16:41

manager  

advanced_trect.patch (2,959 bytes)
Index: objpas/types.pp
===================================================================
--- objpas/types.pp	(revision 32252)
+++ objpas/types.pp	(working copy)
@@ -90,6 +90,14 @@
   packed
 {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
   record
+    private
+      function getheight: integer;
+      function getwidth: integer;
+      procedure setheight(AValue: integer);
+      procedure setwidth(AValue: integer);
+    public
+      property height:integer read getheight write setheight;
+      property width:integer read getwidth write setwidth;
     case Integer of
       0: (Left,Top,Right,Bottom : Longint);
       1: (TopLeft,BottomRight : TPoint);
@@ -478,5 +486,28 @@
 end;
 
 
+{$ifndef Windows}
+{ RECT }
 
+function TRect.getheight: integer;
+begin
+  result:=bottom-top;
+end;
+
+function TRect.getwidth: integer;
+begin
+ result:=right-left;
+end;
+
+procedure TRect.setheight(AValue: integer);
+begin
+ right:=left+avalue;
+end;
+
+procedure TRect.setwidth(AValue: integer);
+begin
+ bottom:=top+avalue;
+end;
+{$endif}
+
 end.
Index: win/wininc/func.inc
===================================================================
--- win/wininc/func.inc	(revision 32252)
+++ win/wininc/func.inc	(working copy)
@@ -2389,5 +2389,27 @@
   IMAGE_FIRST_SECTION:=PIMAGE_SECTION_HEADER(OFS(ntheader^.OptionalHeader) + ntheader^.FileHeader.SizeOfOptionalHeader);
 end;
 
+{ RECT }
+
+function RECT.getheight: integer;
+begin
+  result:=bottom-top;
+end;
+
+function RECT.getwidth: integer;
+begin
+ result:=right-left;
+end;
+
+procedure RECT.setheight(AValue: integer);
+begin
+ right:=left+avalue;
+end;
+
+procedure RECT.setwidth(AValue: integer);
+begin
+ bottom:=top+avalue;
+end;
+
 {$endif read_implementation}
 
Index: win/wininc/struct.inc
===================================================================
--- win/wininc/struct.inc	(revision 32252)
+++ win/wininc/struct.inc	(working copy)
@@ -223,10 +223,20 @@
      TPOINT = POINT;
      PPOINT = ^POINT;
 
+     { RECT }
+
      RECT = record
-          case Integer of
-             0: (Left,Top,Right,Bottom : Longint);
-             1: (TopLeft,BottomRight : TPoint);
+     private
+       function getheight: integer; inline;
+       function getwidth: integer; inline;
+       procedure setheight(AValue: integer);
+       procedure setwidth(AValue: integer);
+     public
+       property height:integer read getheight write setheight;
+       property width:integer read getwidth write setwidth;
+       case Integer of
+         0: (Left,Top,Right,Bottom : Longint);
+         1: (TopLeft,BottomRight : TPoint);
        end;
      LPRECT = ^RECT;
      _RECT = RECT;
Index: windows.pp
===================================================================
--- windows.pp	(revision 32252)
+++ windows.pp	(working copy)
@@ -22,6 +22,7 @@
 
 { stuff like array of const is used }
 {$mode objfpc}
+{$modeswitch ADVANCEDRECORDS}
 {$inline on}
 {$calling stdcall}
 
advanced_trect.patch (2,959 bytes)

Marco van de Voort

2015-11-06 16:46

manager   ~0087159

I yesterday tried to implement some of the TRect helper methods in 3.1.1 and
property and Lazarus refused to start due to WMSIZE loops.

I haven't investigated deeper yet, but I suspect the heavy use of WITH in
interfaces etc is the cause. (if you assign width and height of a
component in a component method in a WITH with a TRECT as argument, then now
width and height of the trect are assigned instead of the component's ?

Probably method calling (setwidth/setheight?) is dangerous too.

Marco van de Voort

2015-11-15 20:49

manager   ~0087302

A first version van a patch has been committed to trunk

Jonas Maebe

2015-11-16 08:32

manager   ~0087310

The patch seems to break bootstrapping with FPC 2.6.4 on Win64, see 0029013

Cyrax

2015-11-16 11:49

reporter   ~0087314

Last edited: 2015-11-16 13:19

View 2 revisions

Related bug reports

http://bugs.freepascal.org/view.php?id=29012
http://bugs.freepascal.org/view.php?id=29017

Marco van de Voort

2016-03-12 23:13

manager   ~0090953

A lot has happened since the original report.

What is actually left to be done here, or can this be closed?

Marco van de Voort

2016-03-19 19:04

manager   ~0091210

No reaction -> close this very general report, please open a specific bugreport if something is wrong

Issue History

Date Modified Username Field Change
2012-01-07 00:08 szali New Issue
2012-01-07 00:08 szali File Added: trect.txt
2012-01-07 00:11 Florian File Deleted: trect.txt
2012-01-07 00:12 Florian Note Added: 0055526
2012-01-08 11:18 Paul Ishenin Note Added: 0055579
2012-01-16 09:25 Hans-Peter Diettrich Note Added: 0055789
2012-01-16 09:34 Hans-Peter Diettrich Note Edited: 0055789
2012-01-20 09:42 Paul Ishenin Note Added: 0055877
2012-01-20 10:38 Marco van de Voort Note Added: 0055882
2012-01-20 10:38 Marco van de Voort Note Edited: 0055882
2012-01-23 20:26 szali Note Added: 0055984
2012-01-23 20:53 Sven Barth Note Added: 0055985
2012-01-25 01:07 szali Note Added: 0056018
2015-11-06 16:41 Marco van de Voort File Added: advanced_trect.patch
2015-11-06 16:46 Marco van de Voort Note Added: 0087159
2015-11-15 20:49 Marco van de Voort Note Added: 0087302
2015-11-16 08:32 Jonas Maebe Relationship added related to 0029013
2015-11-16 08:32 Jonas Maebe Note Added: 0087310
2015-11-16 11:49 Cyrax Note Added: 0087314
2015-11-16 13:19 Cyrax Note Edited: 0087314 View Revisions
2015-12-08 11:17 Mattias Gaertner Relationship added has duplicate 0029148
2016-03-12 23:13 Marco van de Voort Note Added: 0090953
2016-03-12 23:13 Marco van de Voort Assigned To => Marco van de Voort
2016-03-12 23:13 Marco van de Voort Status new => feedback
2016-03-19 19:04 Marco van de Voort Note Added: 0091210
2016-03-19 19:04 Marco van de Voort Status feedback => resolved
2016-03-19 19:04 Marco van de Voort Resolution open => fixed