Lazarus

Programming => LCL => Topic started by: iteh on September 21, 2019, 02:45:24 pm

Title: [SOLVED] TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 02:45:24 pm
lazarus 2.0.4 (fpc 3.0.4)

I can not resize components based on TCustomLabel (TDBText, TLabel): if I set autosize = false, anchors akLeft and akRight and wordwrap = true, then in designtime (with the mouse) and in runtime (via label1.width := 123) I cannot change the width of the component.

if wordwrap = false, then the width changes.

if anchors = akRight only (and not akLeft), then increasing the label1.width value stretches the dimensions of the component to the left (the right border does not change, and the "left" position decreases and the width increases).

is it a bug or feature?
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 06:55:05 pm
Its not a bug that I can see,  it's just the way you are using the anchors.

 When you want to resize the control when it is anchored you need to resize the control it is anchored to, not the control itself.

  In the designer this is hard to do because the parent control is being overlaid and mouse operations are a struggling  mess. Use the OI to select the parent control then directly change the size values.
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 07:11:09 pm
but it normally resizes (even at design time) if set wordwrap = false.

all other (tedit, tmemo, ..) components (in design time too) normally resize and with akLeft + akRight anchors.

in runtime/designtime TLabel normally get new width and resize if wordwrap = false (with akLeft + akRight anchors).

the problem only with "wordwrap = true and akLeft and akRight" and only for TCustomLabel based components.

Any other combinations (not wordwrap = true and autosize = false and akLeft and akRight) of TCustomLabel is ok and other components (for example, tmemo also with wordwrap + left and right anchors) - also is ok !

only tcustomlabel has this bug (?) .. :(


demo example: Form1: TForm, Button1: TButton, Label1: TLabel, Memo1: TMemo (all with default properties) and button1.OnClick code:

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   oldW: Integer;
  4. begin
  5.   Label1.Anchors := [akLeft,akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.  
  9.   oldW := Label1.Width;
  10.   Label1.Width := oldW * 2;
  11.  
  12.   if Label1.Width = oldW then
  13.     ShowMessage('label: anchors with wordwrap - failed')
  14.   else
  15.     ShowMessage('label: anchors with wordwrap - ok');
  16.  
  17.   Label1.WordWrap := false;
  18.   oldW := Label1.Width;
  19.   Label1.Width := oldW * 2;
  20.  
  21.   if Label1.Width = oldW then
  22.     ShowMessage('label: anchors wo wordwrap - failed')
  23.   else
  24.     ShowMessage('label: anchors wo wordwrap - ok');
  25.  
  26.   Memo1.Anchors := [akLeft,akRight];
  27.   Memo1.WordWrap := true;
  28.  
  29.   oldW := Memo1.Width;
  30.   Memo1.Width := oldW * 2;
  31.  
  32.   if Memo1.Width = oldW then
  33.     ShowMessage('memo: anchors with wordwrap - failed')
  34.   else
  35.     ShowMessage('memo: anchors with wordwrap - ok');
  36.  
  37.   Memo1.WordWrap := false;
  38.   oldW := Memo1.Width;
  39.   Memo1.Width := oldW * 2;
  40.  
  41.   if Memo1.Width = oldW then
  42.     ShowMessage('memo: anchors wo wordwrap - failed')
  43.   else
  44.     ShowMessage('memo: anchors wo wordwrap - ok');
  45. end;

and I see:

label: anchors with wordwrap - failed
label: anchors wo wordwrap - ok
memo: anchors with wordwrap - ok
memo: anchors wo wordwrap - ok

(addition)

and another bug only with TCustomLabel:

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   oldW, oldPos: Integer;
  4. begin
  5.   Label1.Anchors := [akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.  
  9.   oldW := Label1.Width;
  10.   oldPos := Label1.Left;
  11.   Label1.Width := oldW * 2;
  12.  
  13.   if (Label1.Width = oldW) or (Label1.Left <> oldPos) then
  14.     ShowMessage('label: akRight with wordwrap - failed')
  15.   else
  16.     ShowMessage('label: akRight with wordwrap - ok');
  17.  
  18.   Label1.WordWrap := false;
  19.   oldW := Label1.Width;
  20.   oldPos := Label1.Left;
  21.   Label1.Width := oldW * 2;
  22.  
  23.   if (Label1.Width = oldW) or (Label1.Left <> oldPos) then
  24.     ShowMessage('label: akRight wo wordwrap - failed')
  25.   else
  26.     ShowMessage('label: akRight wo wordwrap - ok');
  27.  
  28.   Memo1.Anchors := [akRight];
  29.   Memo1.WordWrap := true;
  30.  
  31.   oldW := Memo1.Width;
  32.   oldPos := Memo1.Left;
  33.   Memo1.Width := oldW * 2;
  34.  
  35.   if (Memo1.Width = oldW) or (Memo1.Left <> oldPos) then
  36.     ShowMessage('memo: akRight with wordwrap - failed')
  37.   else
  38.     ShowMessage('memo: akRight with wordwrap - ok');
  39.  
  40.   Memo1.WordWrap := false;
  41.   oldW := Memo1.Width;
  42.   oldPos := Memo1.Left;
  43.   Memo1.Width := oldW * 2;
  44.  
  45.   if (Memo1.Width = oldW) or (Memo1.Left <> oldPos) then
  46.     ShowMessage('memo: akRight wo wordwrap - failed')
  47.   else
  48.     ShowMessage('memo: akRight wo wordwrap - ok');
  49. end;

label: akRight with wordwrap - failed
label: akRight wo wordwrap - ok
memo: akRight with wordwrap - ok
memo: akRight wo wordwrap - ok

if this is a true bug - please send someone a request in the lazarus / fpc bugtrack, I never did this :)
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 08:26:18 pm
Your test is flawed,

 The first test you do discovered the width isn't what it was at the start.

 Then you decide to redefine the oldW with the current width again which is now 2 * larger because of the first test you did and carry on...

 On top of all this, the TLabel is a graphics control and depends on a parent to maintain it, thus this means you need to inform the parent of the change so that it can adjust the Tlable again...

 Basically a Label.Parent.repaint or update or something.
or maybe just doing The labelBase.Update /Repaint etc..

 I don't know what to tell you except that a TCustomLabel really isn't a good control to base things on.

Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 09:03:55 pm
I specifically after each change reassign oldw to the current size, each time out of four, i.e. the test is correct (in my opinion).

about TCustomLabel - we saw a problem with the standard TDBText (which is also based on TCustomLabel), and then we repeated it with TLabel as an example.

then I will look for a replacement TDBText from third-party component packages :(
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 09:16:12 pm
Please examine your code.. You did not restore the Label.Width back to its original width but simple redefined the OldW := Label.Width witch is now a different value than what it was originally. So the second cycle of your test does not have the original width
of the control before the first test..

 Since the first test failed, it is assumed the WIDTH is reporting different form the original WIDTH, but you did not account for this on your next test inline, you simply redefined the OLDW with now a different width..

 Please try to Update the control when changing the Size before referencing the size again.


Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 09:35:47 pm
(Sorry for the long phrases - I use Google translator)

I thought that the absolute size is not important (and did not return to the original one), and it is only important to check that resizing does not work correctly with certain TLabel properties, but in the same conditions it works correctly for other components (TMemo).

I checked whether the size changes at all, it does not matter from which to which, but whether the expected change is going at all.

but the same problem in design time - as soon as I set autosize = false, wordwrap = true and akLeft + akRight - I can no longer resize TLabel on the form.

but it’s worth removing wordwrap (in designtime) - and everything changes normally (in designtime).

and this is only with tlabel (well, and with tdbtext and other from TCustomLabel), all other components are normal.

I changed the code to resize it from the original - this did not help.
I also added .update - it didn’t help:

Code: Pascal  [Select]
  1. var
  2.   oldW: Integer;
  3. begin
  4.   oldW := Label1.Width;
  5.   Label1.Anchors := [akLeft,akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.   Label1.Width := oldW * 2;
  9.   Label1.Update;
  10.  
  11.   if Label1.Width = oldW then
  12.     ShowMessage('label: anchors with wordwrap - failed')
  13.   else
  14.     ShowMessage('label: anchors with wordwrap - ok');
  15.   Label1.Width := oldW;
  16.  
  17.   Label1.WordWrap := false;
  18.   Label1.Width := oldW * 2;
  19.   Label1.Update;
  20.  
  21.   if Label1.Width = oldW then
  22.     ShowMessage('label: anchors wo wordwrap - failed')
  23.   else
  24.     ShowMessage('label: anchors wo wordwrap - ok');
  25. end;

label: anchors with wordwrap - failed
label: anchors wo wordwrap - ok

That is, even in designtime we can check: two TLabels on the form (label1, label2), set akLeft and akRight for both labels, autosize = false for both labels and wordwrap = true for label1, wordwrap = false for label2.

then I try to change the width value for both in the OI - where wordwrap = true it does not change.
and only on TLabels (and TDBText), on all other components without problems..
Title: Re: TCustomLabel constraints in design and runtime
Post by: wp on September 21, 2019, 09:56:20 pm
Code: Pascal  [Select]
  1. var
  2.   oldW: Integer;
  3. begin
  4.   oldW := Label1.Width;
  5.   Label1.Anchors := [akLeft,akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.   Label1.Width := oldW * 2;
This code does not work. Anchors and Width may be conflicting requirements. Setting both left and right anchors of the label means that the left and right edges of the label always have a constant distance to the label's parent - whatever you type as Width will be ignored because it is calculated from the parent's width. If you want to specify the width explicitly you must anchor the label only at one side and keep the other side floating.

BTW, it is not a good idea to turn off label autosizing because the label may be truncated when your program runs in a different operating system or when text changes (maybe due to translation).

[EDIT]
Sorry - talking nonsense here... I was confusing with Align=alTop or Align= alBottom.
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 10:03:06 pm
This code does not work. Anchors and Width may be conflicting requirements. Setting both left and right anchors of the label means that the left and right edges of the label always have a constant distance to the label's parent - whatever you type as Width will be ignored

I was confused that:
1) for other (tedit, tmemo, ..) components everything works fine (the anchor to both edges does not interfere with manually changing the width)
2) for tlabel also works fine if set wordwrap to false
3) for all components (even if there is an anchor left and right) we can change the sizes in designtime - for all but tlabel / tdbtext / other from tcustomlabel.

and the problem with resizing in designtime for tlabel, anchored to left+right, only if wordwrap = true.
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 10:06:06 pm
I don't know what to tell you, it seems to work on my end except for maybe 1 pixel off for the after calculation.
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   OldW:Integer;
  4. begin
  5.   OldW := Label1.Width;
  6.   Label1.WordWrap := True;
  7.   Label1.Width := Label1.Width Shl 2;
  8.   Caption := OldW.ToString+','+Label1.Width.Tostring;
  9. end;                                                    
  10.  

No matter what Combination I come up with, all that does for the first time around is the second width value is always 1 higher than the original width, but this happens only once because I can keep cycling that code and the adjusted value remains constant..
 
 This is only one PIXEL, you are talking about something totally different..

 Btw. a Tpanel is parenting the Tlabel control and I can adjust the width of the Panel and the label will follow through.
 Also I setup the anchors in the OI using the anchor editor and selecting the control it to anchor to.

 I noticed you did not specify a any controls to anchor to ?

Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 10:16:51 pm
I don't know what to tell you, it seems to work on my end except for maybe 1 pixel off for the after calculation.
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   OldW:Integer;
  4. begin
  5.   OldW := Label1.Width;
  6.   Label1.WordWrap := True;
  7.   Label1.Width := Label1.Width Shl 2;
  8.   Caption := OldW.ToString+','+Label1.Width.Tostring;
  9. end;                                                    
  10.  
can show your lfm file, what is the anchor for TLabel for?

I anchored only through the checkbox akLeft and akRight in OI, without the anchor editor. maybe this is the case..

Also I setup the anchors in the OI using the anchor editor and selecting the control it to anchor to.

 I noticed you did not specify a any controls to anchor to ?

no, I did not specify the control to which the anchor for the label goes. only the fact of the presence of an anchor.

I am ready to agree with all the arguments that I am doing something wrong, but I am very confused that the same actions and properties in other components work differently. the problem is with tcustomlabel and its "followers".

can you tell me - where in the LCL source code can I find the code that is responsible for redrawing TCustomLabel in designtime? to see there why the resizing (even in designtime) depends on the wordwrap parameter.
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 10:24:13 pm
please can you see what is wrong with the label properties in my attached project ?

archive from projects - here for such simple parameters I can neither increase the width of label1 in designtime nor even just set a new value for width in designtime.

but at the same time I normally set the same parameters and sizes for memo1 in the same project.

and if I remove the checkbox from wordwrap (for label1 in OI) - I can also immediately resize label1 in designtime.

where is the mistake ?
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 10:25:14 pm
If you are doing this at runtime, its a detailed step..

Use Code completion on the Control or a control and you'll see AnchorSideLeft, Top,Right etc.

Its a TAnchorSide class which I believe you can find in the help file but that field determines the control in which that side is connected to, also there is a SIDE property for you you need to specify of which side of the control you are attaching to.

 If you experiment with the Anchor editor you can get this to work in the OI..

 Right click on the control when it is setting on the form and you'll see an anchor editor.
 
 experiment with that, the Anchors don't do anything until you specify a control.
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 10:30:43 pm
experiment with that, the Anchors don't do anything until you specify a control.

it's true ? I almost never specify a control, because by default (empty, if not specified) everything works well too - the binding goes to the parent component, which is what the anchor editor says: "left empty for anchoring to parent".

I specify only akLeft and akRight, but I don’t specify which control to attach to - and an empty field (indicating only the fact of binding, without specifying the component) means "anchor to the parent component", isn’t that so? or are we talking about different things.
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 10:33:16 pm
I always specify the control. Please do that yourself and see what happens...


maybe your parent control isn't what you think it is ?
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 10:43:25 pm
I always specify the control. Please do that yourself and see what happens...


maybe your parent control isn't what you think it is ?

or am I doing something wrong or if we explicitly indicate the anchor component (for example, the form) and the left and right anchors, then the anchor goes initially to the edges of the form. then we can set the distance to the edge in the anchor editor, right?

but if we made such a setting (indicate not only the presence of the anchor, but also what it is attached to), then we lose the ability (if you set only akLeft / akRight in the OI, without the anchor editor, then we had such an opportunity) to resize and position component on the form just with the mouse, right?

but again - if I do not indicate what to bind to, then this always works correctly for all components except tlabel and then for tlabel it also works correctly if wordwrap = false even if there are anchors.

I’m trying to understand why everything works correctly if there is wordwrap but no anchor or if there is an anchor but no wordwrap, but the combination of “both anchors + wordwrap” gives the wrong behavior but only for tlabel.

what's wrong with tcustomlabel ..
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 10:48:43 pm
Look, if there is another control sitting on the form left of your label, it will slide under it if you don't specify that control that is left to it for example.

 Without specifying the attached controls, it uses the parent which could be in your case the form, so it's limited only to the form, it does not care what controls are between it and the edge of the form. It will simply expand under a control etc.

 if you do have other controls sitting around the label you need to attached to them instead and not the parent .

 I've never seen the behavior you are showing..
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 10:54:39 pm
change your check point code to this..

 Abs(Label1.Width-OldW)>1
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 11:02:43 pm
change your check point code to this..

 Abs(Label1.Width-OldW)>1

no, it does not work.
I can even simply set color for label’s background and visually verify that the label’s dimensions do not change as the width increases .... until I do wordwrap := false

here it’s not a calculation error, it’s some kind of architectural “bug” - because under the indicated conditions we can’t even specify a new width in designtime although no constraints are specified.
it is not clear who forbids resizing even in designtime and only when wordwrap = true


I therefore previously attached the archive with the project. everything is there by default, only 3-4 properties are set for the components ... but I can’t understand why the "stupid" tlabel behaves this way when all the others behave normally.

I need the ability to simultaneously set the binding to the left/right edges of the form (or rather, to the edges of the parent component on which I "put" the label) so that the dimensions of the label change automatically when the dimensions of the parent component change (in the attached example, the form) but also so that I can in designtime use the mouse to move the component within the parent and resize it with the mouse as well, so that wordwrap is true.

I can do this with tmemo and any other components (including those that do not have wordwrap), but I can’t understand how I can achieve the same for tlabel / tdbtext :(
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 11:07:12 pm
probably for now the best solution is not to use components inherited from tcustomlabel simultaneously with left-right anchors and wordwrap in situations where I need to programmatically change the width of this particular component or often need to manually resize them with the mouse in designtime .. maybe someday developers correct this behavior.
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 11:18:16 pm
I have absolutely no idea why you keep attaching WORDWRAP on this problem?

 The example you posted I have down loaded and tested it, its only off by 1 Pixel when you attempt to reset the size? I showed you how to make your example work which seems to have nothing to do with the problem you are claiming..

 I have played endlessly over here with WORWRAP on/off, different combinations, loaded the Label with a long statement etc... other than it adding one pixel to the size of the label, there is no ill effects..

  I think you better look deeper in your code because this isn't where it's at.

 I am always interested in finding solutions of there is a real problem because I like you use the LCL and need it to work so when I see an issue posted I want to investigate it to ensure it is a real issue and not the posters code causing it.

 From what I've found here, you have something else going on in your code that is causing the issue because the example project you posted does not prove anything that supports your theory about the word wrap being on or off...

 With that, good luck finding your bug, I am now happy that knowing there isn't any real issue here.


Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 11:23:06 pm
you tried the archive I posted and without any changes it opened and you were able to resize the label in designtime ??
if so, then the problem is not in the project, but in some settings in my IDE.

You can take my project from the archive and just in designtime (OI) set width for label1 to any (for example, 100) value - will the sizes of label1 increase?
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 11:28:10 pm
The only way the LABEL is going to give you an issue in the OI is if you have AUTOSIZE
on or anchored the right side to something..
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 21, 2019, 11:30:35 pm
can do the following:
1) open the project from my archive and without changing anything try to stretch the length of label1 to the right in designtime with the mouse. will it work out?
2) in the OI for label1 set wordwrap to false and try again to stretch the length of label1 to the right with the mouse.

write to me what happens.

for me - it all depends on wordwrap and with wordwrap=true it is NOT stretched, with wordwrap=false ("2" above) it is stretched.

if it’s different with you, write, I don’t understand why this is not visible, I have it repeated in 100% of cases.

let's try my simplest project from the archive. if your copy of the IDE has a different behavior - problems with the settings of my IDE, if the same - then it all depends on wordwrap as I wrote above.
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 11:44:12 pm
It's your test app that is off.

You didn't correctly code it to show the real problem.

I'll recode the block for you ..
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   oldW: Integer;
  4. begin
  5.   oldW := Label1.Width;
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.   Label1.Width := oldW * 2;
  9.   if  Abs(Label1.Width-OldW)< 2 then
  10.     ShowMessage('label: anchors with wordwrap - failed')
  11.   else
  12.     ShowMessage('label: anchors with wordwrap - ok');
  13.   Label1.Width := oldW;
  14.  
  15.   Label1.WordWrap := false;
  16.   Label1.Width := oldW * 2;
  17.   if Label1.Width = oldW then
  18.     ShowMessage('label: anchors wo wordwrap - failed')
  19.   else
  20.     ShowMessage('label: anchors wo wordwrap - ok');
  21. end;                                        
  22.  

Put a long line in the Label caption
Title: Re: TCustomLabel constraints in design and runtime
Post by: wp on September 21, 2019, 11:52:17 pm
I confirm iteh's observation. Debugged into the LCL and found that the new Label width is set correctly by DoSetBounds, but down the short procedure, if WidthChanged and WordWrap both are true, the width is reset back to its old value by AdjustSize. When I comment AdjustSize for a test then iteh's test program behaves correctly.

Unfortunately, the SetBounds routines in TControl and its descendants are extremely complicated (for my understanding) and I don't know a quick solution:

Code: Pascal  [Select]
  1. procedure TCustomLabel.DoSetBounds(ALeft, ATop, AWidth, AHeight: integer);
  2. var
  3.   WidthChanged: Boolean;
  4. begin
  5.   WidthChanged:=AWidth<>Width;
  6.   inherited DoSetBounds(ALeft, ATop, AWidth, AHeight);
  7.   if OptimalFill and (not AutoSize) then
  8.     AdjustFontForOptimalFill;
  9.   if WidthChanged and WordWrap then begin
  10.     InvalidatePreferredSize;
  11.     AdjustSize;     // <----------- here the width is reset back to the old value
  12.   end;
  13. end;
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 21, 2019, 11:57:44 pm
Well that is wrong, there is no reason to be checking for WordWrap there.
it should be checking for AutoSize instead.
Title: Re: TCustomLabel constraints in design and runtime
Post by: iteh on September 22, 2019, 12:08:21 am
I confirm iteh's observation. Debugged into the LCL and found that the new Label width is set correctly by DoSetBounds, but down the short procedure, if WidthChanged and WordWrap both are true, the width is reset back to its old value by AdjustSize. When I comment AdjustSize for a test then iteh's test program behaves correctly.

Unfortunately, the SetBounds routines in TControl and its descendants are extremely complicated (for my understanding) and I don't know a quick solution:
Thank you very much for the detailed analysis of the reason!

I commented out the line you specified in \lazarus\lcl\include\customlabel.inc (checked under windows), recompiled and the problem disappeared in both designtime and runtime.

can I ask you to issue a request in the bug track for lazarus, so that the developers correct this in the next release?
Title: Re: TCustomLabel constraints in design and runtime
Post by: jamie on September 22, 2019, 12:23:03 am
there is something strange because I made a local attempt to override that and turn off the WordWrap but it seem to ignore that. Humm..
 
 Oh well.

 This is my solution..
Code: Pascal  [Select]
  1. TLabel = Class(StdCtrls.TLabel)
  2.     procedure AdjustSize override;
  3.   end;                                        
  4. procedure TLabel.AdjustSize;
  5. begin
  6.   if Not AutoSize Then exit;
  7.   Inherited AdjustSize;
  8. end;                                      
  9.  
I override the AdjustSize and that works because commented out the Adjustsize won't adjust the box if autoSize is true..
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: jamie on September 22, 2019, 12:57:04 am
I hope that the deleted line you did does not make it into the release because that is wrong, if needs to test for AutoSize before it can ignore it, otherwise you just made another bug
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: iteh on September 22, 2019, 01:12:26 am
But is it correct to do autosize with wordwrap ?

you can get ambiguity: if the line is long, then do you need to expand the width in autosize and put everything in one line or do you need to reduce the width, but increase the height and make a word break ?

In rad studio delphi the whole text is analyzed there and the TLabel width is set according to the longest word, and then wordwrap works and the height is adjusted to fit the whole text.

Is a similar algorithm implemented in AdjustSize so that both AutoSize and WordWrap work simultaneously ?
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: jamie on September 22, 2019, 01:30:26 am
I am just saying that removing it all together introduces another problem where those that want auto size to work with word wrap on will not have it..

 the process can be ignored if in designer mode, it does not need to be there but other than that, if should be this in the code..

 if AutoSize then AdjustSize. instead of removing it all together.

 You still get your fix and others still have a working autosize with wordWrap.
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: iteh on September 22, 2019, 02:09:01 am
Please, try your solution - it doesn’t work for me, but the wp solution just works correctly:
1) akLeft, akRight, wordwrap = true, autosize = false - word wrap is done based on the width set by the user, but the height is not adjusted
2) akLeft, akRight, wordwrap = true, autosize = true - word wrap is done based on the width set by the user and the height is adjusted to fit the entire text
3) akLeft, akRight, wordwrap = false, autosize = true - the text is in one line and the width is adjusted
4) akLeft only, wordwrap = true, autosize = true - the text is in one line and the width is adjusted

and that’s all in the solution from wp.

but if I write “if autosize then adjustsize;” instead of the commented line, then the above rule 2 ("2" above) do not work and rule 4 works the same as in the solution from wp.

Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: jamie on September 22, 2019, 02:15:00 am
For you, you can't use AutoSize. In your code you would need to turn that off of course.

 For the IDE operation you also need to test for the Designer state so if AutoSize is on the IDE designer will ignore it and allow you to move it with word wrap.
Code: Pascal  [Select]
  1. if (csDesignInteractive in ControlStyle) or (Not AutoSize) Then exit;
  2.   Inherited AdjustSize;                                                  
  3.  

This is the concept of the code.
 This allows others to use the AutoSize if they wish..
what you are doing is removing that wish from those that may still want it as it is and it seems apparent that you don't have your AutoSize on anyways so this should work for you.

P.S.
 I may have the wrong Flag in use for Designer state, I need to check into that more

Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: iteh on September 22, 2019, 02:27:07 am
you are mistaken, I can use autosize and it works successfully - which I indicated by listing 4 combinations above and how they work out.

also note that analyzing only designtime and "not autosize" is incorrect - you need not only to be able to increase the width in designtime by mouse, but also give the user the ability to manually set the width for different conditions.

but this feature is blocked by your patch - it does not allow to assign a new value to width if wordwrap=true and autosize=true, for this case you code will apply the old bug (which wp fixed) and call the function that will return the previous width: the user will call width := nn, and your code will simply cancel it.

(added)

if you think I'm wrong and your patch does not have all these shortcomings that I indicated about it - let's not discuss it here, but publish your patch in the Lazarus bugtrack branch, let the developers confirm the correctness of your code there and add it to the next release and that’s it.

here the initial problem is already clear, then everyone will make a private patch for himself, but we still need to solve the issue of editing the error in the general branch.
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: iteh on September 22, 2019, 02:47:08 am
wp code prevents canceling width changes.

in my opinion, even autosize should not block the width - the width should always be changed by the user, but autosize (with wordwrap=true) is responsible for adjusting only the height.

based on this concept - the patch from wp is correct.

if you want autosize to block manual changes to the width, then (only in my opinion!) the meaning of wordwrap + autosize is completely lost: after all, we will not be able to set the fixed width we need for wordwrap, and all the text will always be in one line (autosize will still fit the width of the entire text).

but this is just my vision of how autosize should work :)
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: jamie on September 22, 2019, 02:55:01 am
did you notice I was trying to test if the control was in Design mode?

Have it your way, I don't care.. I've never had an issue with that to be honest, I just
set word wrap when I get the control where I want it and it stays in place..
   
 So more testing with others need to be done, that wasn't put there for no reason and I am sure someone will complain.

 Mean while I got bigger fish to fry...

 Have a good day.
Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: wp on September 22, 2019, 12:27:27 pm
I am attaching a test program which checks several conditions for a TLabel. Each button click toggles the label width between 100 and 200 pixels. The label background is drawn in yellow to show whether the command was obeyed. The only situation which is not correct is the combination "left- and right-anchored" AND "WordWrap" (Button "(4)...") - here the width does not change. Left+right anchoring without WordWrap is correct (Button "(2)..."), as well as Wordwrap with left-anchoring only (Button "(3)..."). (Maybe this already has been found above, I did not read everything).

Removing the "AdjustSize" line in "TCustomLabel.DoSetbounds" (or the entire "if" block containing it) seems to fix the issue without introducing other issues. But maybe my test is not complete... This code was originally introduced by Mattias in r24917 (April 25, 2010: "LCL: TCustomLabel: AdjustSize if WordWrap and Width changed") - too long gone to ask why.

The final button "(6)..." tests the AutoSize behavior which has been annoying me for some time because it ignores WordWrap and expands an already wrapped text to a single line. It should behave like Delphi which applies the AutoSize to the height only and keeps the preset width of the multi-line label.


Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: iteh on September 22, 2019, 05:14:31 pm
The final button "(6)..." tests the AutoSize behavior which has been annoying me for some time because it ignores WordWrap and expands an already wrapped text to a single line. It should behave like Delphi which applies the AutoSize to the height only and keeps the preset width of the multi-line label.

in Delphi (tested on Delphi 10.1 “Berlin”), everything is not going smoothly with the “button 6” test:

the second (autosize=false wordwrap=true) looks the same as in lazarus (after the first "click" - everything is on one line and remains so, after disabling autosize - the width does not come back).

but the first one (autosize=false wordwrap=false) is drawn once "as it should" (there is an auto-fit of the width, in a larger direction, based on the maximum length of the word in the line, and then the auto-fit of the height is already done using word wrapping), and with subsequent "clicks "is already similar to lazarus - the maximum line length without any wordwrap.

Only if we add the same "smart" algorithm to Lazarus: if both autosize and wordwrap are enabled at the same time, then adjust the width (or just to the side an increase in length, as in delphi now, or to either side) to the maximum word length, and then adjust the height so that the whole line fits through wordwrap. And repeat this algorithm both when autosize and wordwrap are turned on at the same time and when the contents of the string change.

Title: Re: [SOLVED] TCustomLabel constraints in design and runtime
Post by: jamie on September 23, 2019, 12:58:30 am
I Checked my old Delphi and it behaves a little different than Lazarus..

If I don't use AutoSize the WordWrap feature does not effect the sizing at design time or runtime. I can turn it on/off etc..

 if I enable autosize, then the first time it does this it will resize the box but only the first time.

 With AutoSize still on, if I enable WordWrap it will then Resize the label again but only once.

 Basically, if AutoSize is on, it only resizes the label for the initial state of that or turning the WordWRap on.

 Putting that all together, with any combination of those I am able to size the label in the designer via the mouse or OI with no issues, the size will remain where I set it until I play with those AutoSize setting etc.. So it seems the sizing methods are only getting called when playing with the properties while in the designer.

 In Lazarus it seems once you turn on the AutoSize it's locked.