Recent

Author Topic: [SOLVED] TPanel does not shrink, even if AutoSize=false  (Read 6326 times)

linuxfan

  • Jr. Member
  • **
  • Posts: 57
[SOLVED] TPanel does not shrink, even if AutoSize=false
« on: May 22, 2016, 09:26:17 am »
Hello all.

I am writing a download manager, which has a window containing dynamic "download items". A single download item is a TPanel descendant, which in turn contains other panels filled with labels and so on. One of those panels contains three labels to show 1) the url; 2) the local filename with yellow background; 3) the local filename with green background. The latter two labels are used as a progress bar.

The code is this ("my" is the "download item", a TPanel descendant):
Code: Pascal  [Select][+][-]
  1.   my.pnUrls     := TPanel.Create(my);
  2.   my.pnUrls.AutoSize := false;
  3.   my.pnUrls.Align := alClient;
  4.    my.lFrom := TLabel.Create(my);
  5.    my.lFrom.AutoSize := false;
  6.    my.lFrom.Layout := tlCenter;
  7.    my.lFrom.Align := alTop;
  8.    my.lFrom.Caption := '  From '+OriginatingUrl;
  9.    my.lFrom.Parent := my.pnUrls;
  10.    my.lFrom.OnDblClick := my.DownloadDblClick;
  11.    ...

The panel pnUrls does NEVER shrink less than the width of lFrom, despite the panel and the label AutoSize properties are set to false, even if I resize the form containing these download items.

I expected this: lFrom has Autosize=false and Align=alTop; it should get whatever width its parent gives - instead lFrom keeps its preferred width, and its parent pnUrls adapts. Just the contrary of what should happen.

Sometimes the urls are so long they don't fit in the screen... :-), but this is not a real problem; the problem is that pnUrls should impose its width to the contained AlignedTop labels.

Regards,
linuxfan


« Last Edit: May 24, 2016, 07:36:27 am by linuxfan »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TPanel does not shrink, even if AutoSize=false
« Reply #1 on: May 22, 2016, 02:13:58 pm »
Does calling
Code: Pascal  [Select][+][-]
  1. my.lfrom.InvalidatePreferredSize;

help at all?

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4571
  • I like bugs.
Re: TPanel does not shrink, even if AutoSize=false
« Reply #2 on: May 22, 2016, 02:28:11 pm »
The Panel does not shrink because it is "alClient" aligned.
Code: Pascal  [Select][+][-]
  1. my.pnUrls.Align := alClient;
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

linuxfan

  • Jr. Member
  • **
  • Posts: 57
Re: TPanel does not shrink, even if AutoSize=false
« Reply #3 on: May 22, 2016, 04:54:19 pm »
> InvalidatePreferredSize

I will try this, thanks.


> The Panel does not shrink because it is "alClient" aligned

This is interesting. Exactly because pnUrls is alClient, it must take what is offered to it, not viceversa. If it was *not* alClient, you could be right. Of all the alignment modes, alClient is the only one that states, implicitly, that the parent has the last word about size. And this does not happen.

I forgot to say that I am using Lazarus 1.6 under win32. I will try that under Linux/gtk too, but I suspect that this behaviour is in LCL.


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4571
  • I like bugs.
Re: TPanel does not shrink, even if AutoSize=false
« Reply #4 on: May 22, 2016, 05:18:15 pm »
Of all the alignment modes, alClient is the only one that states, implicitly, that the parent has the last word about size. And this does not happen.

Ok, true. Now I read your original post properly: "even if I resize the form ...". Then an alClient aligned Panel should follow the form's size indeed.
Can you please attach a simple demo project. Compressed, only sources.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TPanel does not shrink, even if AutoSize=false
« Reply #5 on: May 22, 2016, 06:56:49 pm »
The panel pnUrls does NEVER shrink less than the width of lFrom, despite the panel and the label AutoSize properties are set to false, even if I resize the form containing these download items.

Who is the parent of pnUrls? your code does not show one.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4571
  • I like bugs.
Re: TPanel does not shrink, even if AutoSize=false
« Reply #6 on: May 23, 2016, 12:35:54 am »
Who is the parent of pnUrls? your code does not show one.

Yes, the lack of parent is the most probable cause. I failed to see it earlier.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

linuxfan

  • Jr. Member
  • **
  • Posts: 57
Re: TPanel does not shrink, even if AutoSize=false
« Reply #7 on: May 23, 2016, 07:50:35 am »
Please find attached a test project. It is a single form: clicking on the rightmost button you create a dummy download item; then resize the window, or try to use a very long URL (modifying the source), and see the effect.

In my first post I didn't show the rest of the code, because I thought it was not important - of course there must be a parent... wincontrols without parent are not visible!, if I missed the parent, I couldn't see a shrinking problem!  :D

The parent of pnUrls is set as last thing, because LCL documentation says it is best to do so.

Regards,
linuxfan

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: TPanel does not shrink, even if AutoSize=false
« Reply #8 on: May 23, 2016, 08:36:28 am »
The main item "my" has a TScrollBox as its parent:
Code: Pascal  [Select][+][-]
  1.   my.Parent := scbMain;

Now when you shrink the form, scbMain provides the needed space for its children. I don't see a problem with that. Making the form as the parent instead, would not that give you the desired effect?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TPanel does not shrink, even if AutoSize=false
« Reply #9 on: May 23, 2016, 08:39:49 am »
I think your difficulty arises because you make the parent of "my" (TDBDownload) a scrollbox, yet set its Align property to alClient. Aligning to a scrollbox's client area gives it infinite resizeability.
If you want to constrain the size of "my" you will have to give it a different Align property, or parent it in a container that does not scroll to accommodate its clients.

linuxfan

  • Jr. Member
  • **
  • Posts: 57
Re: TPanel does not shrink, even if AutoSize=false
« Reply #10 on: May 23, 2016, 10:48:37 am »
Thanks all for help. But... did you try to run the project?

A scrollbox is needed, and anyway the problem is not there.

The geometry is, from grand parent: form>scrollbox>TDBDownload>pnUrls>lFrom.

The problem is that lFrom, having AutoSize=false AND Align=alTop, should take its width from its parent, pnUrls. In turn pnUrls, having Autosize=false and Align=alClient, should take its width from its parent, TBDownload. TDBDownload, in turn, being Autosize=false and Align=alTop, should take its width from the scrollbox, which is alClient, so it should take its width from the form.

Well, everything works if the width of lFrom is smaller than the width of its parent: reducing the form width reduces the scrollbox, which in turn reduces TDBDownload, which reduces pnUrls. All correct. But this correctness stops when pnUrls should become less wide than one of its children. It doesn't shrink. TDBDownload, in turn, stops shrinking - but again it should, otherwise what is the meaning of it being aligned ALTOP?

May be I am overlooking a big point. But all I want is to clip a TLabel inside a container!

While I am at it, I can add that probably I will clip the caption instead, so a long url will read "http://somehost.com/.... thedocument.pdf", but this is another topic.

Regards,
linuxfan


linuxfan

  • Jr. Member
  • **
  • Posts: 57
Re: [SOLVED] TPanel does not shrink, even if AutoSize=false
« Reply #11 on: May 24, 2016, 07:54:00 am »
Quote
I think your difficulty arises because you make the parent of "my" (TDBDownload) a scrollbox, yet set its Align property to alClient. Aligning to a scrollbox's client area gives it infinite resizeability.
If you want to constrain the size of "my" you will have to give it a different Align property, or parent it in a container that does not scroll to accommodate its clients.

As many here said, the problem was actually the scroll box. But a scroll box or something similar is needed: a download manager window can contain many items.

I played in the object inspector with the properties of scbMain (TScrollBox). ChildSizing was promising, but didn't help. What solved instead was to set HorzScrollBar.Visible to false. That, does exactly what I want.

The logic behind this fact is misleading, even if not totally wrong. TScrollbox has a ChildSizing.ShrinkHorizontal property which can be set to crsHomogeneousChildResize. It let me think that it should resize its children....

Anyway, solved. I still think that Autosize property does not behave correctly when set to false.
Regards,
linuxfan

 

TinyPortal © 2005-2018