Lazarus

Programming => LCL => Topic started by: lucamar on July 04, 2019, 01:18:01 pm

Title: PaintBox vs. Canvas dimensions
Post by: lucamar on July 04, 2019, 01:18:01 pm
Antecedents
As shown in the attached image, the Width and Height of a TPaintBox and its Canvas are different, which means this code will not do the expected:
Code: Pascal  [Select][+][-]
  1.   with PaintBox.Canvas do begin
  2.     Brush.Color := clCream;
  3.     FillRect(0, 0, Width, Height);
  4.     Frame(0, 0, Width, Height);
  5.   end;
while this one wiill do it:
Code: Pascal  [Select][+][-]
  1.   with PaintBox do begin
  2.     Canvas.Brush.Color := clCream;
  3.     Canvas.FillRect(0, 0, Width, Height);
  4.     Canvas.Frame(0, 0, Width, Height);
  5.   end;

In case it matters, the PaintBox is aligned to Client with "BorderSpacing.Around = 6". The dimensions of the canvas are the same than those of the form's client area.

Question:
Is this a known (and "won't solve") problem or should I report in the bug tracker? Or is it how it's supposed to work?

Environment
Linux-i686 3.2.0/GTK 2.24.10
Lazarus/FPC: 2.0.2/3.0.4 (vanilla gtk install)
Title: Re: PaintBox vs. Canvas dimensions
Post by: wp on July 04, 2019, 01:35:09 pm
As shown in the attached image, the Width and Height of a TPaintBox and its Canvas are different, which means this code will not do the expected:
Code: Pascal  [Select][+][-]
  1.   with PaintBox.Canvas do begin
  2.     Brush.Color := clCream;
  3.     FillRect(0, 0, Width, Height);
  4.     Frame(0, 0, Width, Height);
  5.   end;
I always thought that the Canvas is infinite, it does not have neither a Width nor a Height. In this case, Width and Height in above code would refer to the parameters of the Form ( a dangerous usage of the "with" syntax BTW).

But I am wrong, there are properties TCanvas.Width and TCanvas.Height indeed. Peeking into the source code shows
Code: Pascal  [Select][+][-]
  1. function TCanvas.GetWidth: integer;
  2. var
  3.   p: TPoint;
  4. begin
  5.   if HandleAllocated then begin
  6.     GetDeviceSize(Handle,p);
  7.     Result:=p.x;
  8.   end else
  9.     Result:=0;
  10. end;
and GetDeviceSize leads into a widgetset-related method. In case of Windows, this calls "WindowFromDC" which in fact seems to do what I suspected above: it links the extent of the canvas to the exent of the form. Of course, every widgetset may handle this case differently. So, for device-independent code, I would refrain from referring to Canvas.Width and Canvas.Height, like I always did.

Just my 2 cents...
Title: Re: PaintBox vs. Canvas dimensions
Post by: lucamar on July 04, 2019, 01:46:13 pm
Yes, as I said the PaintBox' Canvas is getting the dimensions either of the form client area or of the paintbox before applying BorderSpacing.

I suspect the former, rather than the later, because TPaintBox is a TGraphicControl.

The question is: Is this already known and as should be or a bug?

BTW: Yes, it's a rather dangerous use of "with" but it's just a small test and I was in something of a hurry :-[
And I checked it did what it was suposed to do ...
(famous last words, huh? :) )
TinyPortal © 2005-2018