[fpc-devel] An enhanced TRect

Graeme Geldenhuys graemeg.lists at gmail.com
Fri Aug 10 15:47:09 CEST 2007


Hi,

In fpGUI I created a enhanced Rectangle structure called TfpgRect. It
is defined as follows:

  TfpgCoord = integer;  // could change to floating point in the future.

  TfpgRect = object  // not class for static allocations
    Top: TfpgCoord;
    Left: TfpgCoord;
    Width: TfpgCoord;
    Height: TfpgCoord;
    procedure SetRect(aLeft, aTop, aWidth, aHeight: TfpgCoord);
    function  Bottom: TfpgCoord;
    function  Right: TfpgCoord;
    procedure SetBottom(Value: TfpgCoord);
    procedure SetRight(Value: TfpgCoord);
  end;

TfpgRect prevents minor calculation errors or 'on the spot
calculations' for parameters like when calculating the Width of a
rectangle etc.  It promotes the use of Top, Left, Width and Height and
calculates for you Right and Bottom. You can also set Right and
Bottom, in which case it calculates Width and Height.

Anyway, a fpGUI user asked me the following.  Could TfpgRect replace
or merge with TRect in FPC itself.  He found the same problem
(limitations) mentioned below in many applications and constantly have
to define similar custom rectangle structures for is applications and
floating point rectangle structures.

Would such a merge be possible?  What would the implications be? I
couldn't give him an exact answer.  For one, Windows API calls require
a TRect which doesn't support Width and Height in the structure, etc..
I'm sure other platforms will have similar issues.

What's your thoughts on this?  Is it possible to merge this structure
into FPC itself replacing TRect?

Regards,
  - Graeme -



---------[ Some more background information about TfpgRect ]-------------

Top, Left, Width and Height seems to be less confusing, and that is
why functions like Canvas.Rectangle(), Canvas.FillRectangle(), etc.
rather us it in fpGUI.

By confusing I mean: Say the Rectangle function was defined using two
sets of coordinates. Rectangle(x1, y1, x2, y2: integer);
Then what would the size of the rectangle be when you call:
  Rectangle(0, 0, 1, 1);
A 1x1 or a 2x2 rectangle?
X11 would have drawn a 2x2 rectangle. GDI would have drawn a 1x1
rectangle. In the case of fpGUI, we have now standardized it to be a
1x1 rectangle. So no matter the platform, DrawLine, FillRectangle,
DrawRectangle will produce the exact same size output. [For details as
to why there was a difference, read the LineTo or Rectangle API
documentation of GDI on MSDN.]

This is why fpGUI rather promotes X, Y, Width and Height as parameters
for such functions. This more clearly describes what the developers
intention was. TfpgRect does the same.

-------------------------------------------------------



More information about the fpc-devel mailing list