Lazarus

Programming => LCL => Topic started by: calebs on November 25, 2022, 09:33:42 pm

Title: problem with label alignment in pagecontrol
Post by: calebs on November 25, 2022, 09:33:42 pm
Hi everyone i have spotted this problem long ago and i think i've reported here also
It's been happening since a several versions before last (2.2.4).
tested on lazarus fixes (2.2.5) on windows 11 but it happens with previous stable versions also in windows 10 (i recall). WIll try on linux after
Test like this:

new application
drop pagecontrol
create two tabs
create 3 labels on each tab
set alignment to the first (in the two tabs) to left
set alignment to the sencond (ijn the two tabs) to center
set alignment to the third  (in the two tabs) to right.
on formshow write a number (or any text) on each label and this happens:
when i compile and run (f9) and focusing on the first tab when application runs the labels on the first tab are well aligned but in the sencond all are left aligned.
close and return to lazarus
focus on second tab and run again (f9) and labels on second tab are well aligned but in the first all appear left aligned.
it puzzles me so the only solution i found is use string functions to align labels and fixed font size for numbers appear well aligned.
Hope i was clear with the procedure.
Thanks to all!
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 25, 2022, 10:27:22 pm
Something is missing here: Are the labels autosized? And where are they? When AutoSize=true, how can you tell whether the text is left-aligned/centered/right-aligned?

When I place three labels on each tab, set their Align to alTop so that they extend over the full width of the tab and set the Alignment to taLeftJustify, taCenter and taRightJustify everything is fine. The same observation when I turn AutoSize off and grow the width of the labels so that they extend over the tab width.
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 26, 2022, 12:33:38 am
all other options are default ones.
they are put in the ttab one behind the other
its easy to know if they are rigth aligned, when i load numbers with 2 decimals if the align is right its easy to notice. (ej 12.23 and 4832.33)
Change the align to right only and you will see it
im not usign align only aligment
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 26, 2022, 12:48:18 am
Please post a project for clarification.
Title: Re: problem with label alignment in pagecontrol
Post by: WooBean on November 26, 2022, 09:13:03 am
Well, there is something indeed - when the code for setting captions of labels is put in FormCreate event (as I/we usually do) all seems OK but when it is written inside  FormShow the strange behaviour (described by TS) occurs. Tested on Lazarus 2.2.0 (rev lazarus_2_2_0) FPC 3.2.2 x86_64-win64-win32/win64, IDE set as docked. See below:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.   label1.caption:='111.11';
  4.   label2.caption:='222.22';
  5.   label3.caption:='333.33';
  6.   //label2.left:=label1.Left; //needed to keep position unchanged
  7.   //label3.left:=label1.Left; //needed to keep position unchanged
  8.  
  9.   label4.caption:='444.44';
  10.   label5.caption:='555.55';
  11.   label6.caption:='666.66';
  12.  
  13. end;
  14.  

 

Title: Re: problem with label alignment in pagecontrol
Post by: jamie on November 26, 2022, 07:26:57 pm
I can tell you that any GUI changes that effects the appearance should not be done in the Show event because it can cause retriggers.

 The OnShow event is to show what you have and what changes you make there should not directly affect the GUI layout in anyway.
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 26, 2022, 07:43:06 pm
all other options are default ones.
So, the labels have AutoSize=true? Then there is no way to distinguish left-aligned, centered and right-aligned single-line labels because the size of the label is adjusted such that it tightly encloses the text. I don't understand...
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 26, 2022, 08:01:24 pm
open, focus on first tab, run and watch alignment of labels... first column is left, center is center, right is right.
then change tab and watch missaligned.
Then click on the button below and change tab and do it several tabs...
now i find that labels moves far from their design position.
if you stop, focus on second tab and run.
second tab is as should be with alignment but first not...
play again with button and changin tabs and you will see
Title: Re: problem with label alignment in pagecontrol
Post by: howardpc on November 26, 2022, 08:30:09 pm
Not quite sure what you are after, but the attached adapted project uses childsizing for TabSheet1, with a number of other properties also changed, and TabSheet2 left as in your project1.
You can play with the various childsizing sub-properties to get the effect you want (whatever it is). Confusingly, I failed to rename the altered project, sorry.
Title: Re: problem with label alignment in pagecontrol
Post by: dsiders on November 26, 2022, 09:52:08 pm
open, focus on first tab, run and watch alignment of labels... first column is left, center is center, right is right.
then change tab and watch missaligned.
Then click on the button below and change tab and do it several tabs...
now i find that labels moves far from their design position.
if you stop, focus on second tab and run.
second tab is as should be with alignment but first not...
play again with button and changin tabs and you will see

So, rather than playing 20 questions... post a project or some code that demonstrates the error.
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 26, 2022, 09:53:35 pm
i've noticed in another program i have several labels on several tabs.
I fill them all with decimal numbers (xxx.xx) and i want to all to be right aligned (with right alignment)
The problem is when i run the project only the focused tab when i run the project is well aligned... all othar labels on other tab are aligned to left.
If i stop the program and recompile it or run it with other tab focused, this tab is aligned to right but others to the left.
But the property of the labels are ALL right alignment.

in the project above other problems arise
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 26, 2022, 10:02:52 pm
New version of the project.
i've added checks to use as guide for the alignment
you can see that right alignment is not working as expected (or at least i think they should be)
the numbers NOT align to the right (encolumned (dont know if its the right term))
in runtime If you change the tabs and press the button and change the tab again they move more!!
Title: Re: problem with label alignment in pagecontrol
Post by: dsiders on November 26, 2022, 10:30:44 pm
New version of the project.
i've added checks to use as guide for the alignment
you can see that right alignment is not working as expected (or at least i think they should be)
the numbers NOT align to the right (encolumned (dont know if its the right term))
in runtime If you change the tabs and press the button and change the tab again they move more!!


For any Label that is right-aligned, change the Anchors to Top, Right instead of Top, Left.
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 26, 2022, 11:23:15 pm
I see. I must confess that I had never realized that a label which has an anchor akLeft but Alignment taRightJustify keeps its right edge fixed when the Caption is changed. Almost sounds like a bug, but I checked with Delphi, and it shows that same unlogical behaviour.

Anyway, if you want a label to be right-aligned and keep its right edge fixed then you should set its akRight anchor, as dsiders already said. And I would even go a step further an use the anchor editor to anchor the right edge of the label to a fixed point, e.g. the right edge of the parent.

I am attaching a modified version of your first project in which the left labels are anchored to the left side of the tabsheet, the center labels are anchored to the center of the tabsheet, and the right labels are anchored to the right sides of the tabsheet. I also painted the background in yellow to exactly see the position of the labels.
Title: Re: problem with label alignment in pagecontrol
Post by: dsiders on November 26, 2022, 11:34:31 pm
I see. I must confess that I had never realized that a label which has an anchor akLeft but Alignment taRightJustify keeps its right edge fixed when the Caption is changed. Almost sounds like a bug, but I checked with Delphi, and it shows that same unlogical behaviour.

Seems logical to me. It's like inflating a balloon... it expands and contracts on the end that is not being held in place.
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 27, 2022, 12:01:25 am
I see. I must confess that I had never realized that a label which has an anchor akLeft but Alignment taRightJustify keeps its right edge fixed when the Caption is changed. Almost sounds like a bug, but I checked with Delphi, and it shows that same unlogical behaviour.

Seems logical to me. It's like inflating a balloon... it expands and contracts on the end that is not being held in place.
But this is just what I am saying: The anchor akLeft holds it fixed at the left, thus the balloon should grow to the right. This behaviour also contradicts other controls: A TStaticText with Alignment=taRightJustify, AutoSize=true and akLeft in Anchors grows to the right with the Caption. Or TCheckbox/TRadioButton always grows to the right, too - its default Alignment is taRightJustify.

But maybe all this is a misunderstanding of the Anchors. They probably only are used when the size of the parent changes. And in case of TCheckbox/TRadiobox the Alignment property probably is simply poorly named and means the position of the text in relation to the button.

This is a very confusing situation... Therefore it is even more important to use anchoring with the Anchor Editor for clarification.
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 27, 2022, 12:17:34 am
I see. I must confess that I had never realized that a label which has an anchor akLeft but Alignment taRightJustify keeps its right edge fixed when the Caption is changed. Almost sounds like a bug, but I checked with Delphi, and it shows that same unlogical behaviour.

Anyway, if you want a label to be right-aligned and keep its right edge fixed then you should set its akRight anchor, as dsiders already said. And I would even go a step further an use the anchor editor to anchor the right edge of the label to a fixed point, e.g. the right edge of the parent.

I am attaching a modified version of your first project in which the left labels are anchored to the left side of the tabsheet, the center labels are anchored to the center of the tabsheet, and the right labels are anchored to the right sides of the tabsheet. I also painted the background in yellow to exactly see the position of the labels.

thanks! it sound logical to me that if i put a label and i chose right alignment it would align to the rightest side of the label but then again, is confusing. The very rare thing is that when it happens inside a ttab, in the active tab when i compile it and run, appears ok and on the other tabs don't. That is what puzzles me. Then again, stop program, set index to tab 2 and compile again, the tab two aligns as expected but the one is left aligned. That's why i think its a bug
I never used anchors and border spacing but in the example you gave me is working as expected so i will implement it that way next time.
Thank you all!
Title: Re: problem with label alignment in pagecontrol
Post by: dsiders on November 27, 2022, 12:33:38 am
I see. I must confess that I had never realized that a label which has an anchor akLeft but Alignment taRightJustify keeps its right edge fixed when the Caption is changed. Almost sounds like a bug, but I checked with Delphi, and it shows that same unlogical behaviour.

Anyway, if you want a label to be right-aligned and keep its right edge fixed then you should set its akRight anchor, as dsiders already said. And I would even go a step further an use the anchor editor to anchor the right edge of the label to a fixed point, e.g. the right edge of the parent.

I am attaching a modified version of your first project in which the left labels are anchored to the left side of the tabsheet, the center labels are anchored to the center of the tabsheet, and the right labels are anchored to the right sides of the tabsheet. I also painted the background in yellow to exactly see the position of the labels.

thanks! it sound logical to me that if i put a label and i chose right alignment it would align to the rightest side of the label but then again, is confusing. The very rare thing is that when it happens inside a ttab, in the active tab when i compile it and run, appears ok and on the other tabs don't. That is what puzzles me. Then again, stop program, set index to tab 2 and compile again, the tab two aligns as expected but the one is left aligned. That's why i think its a bug
I never used anchors and border spacing but in the example you gave me is working as expected so i will implement it that way next time.
Thank you all!

Then you did not learn anything from this discussion. It has nothing to do with TPageControl... and everything to do with a random number of digits being stuffed into an auto-sized label. But never mind... you have your solution. Copy and paste.  We can all move on now.
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 27, 2022, 01:14:15 am
Im always learning and i will study these options i never used to solve the problem that i had.
Now you can get down a little from your pedestal and explain to me (and appearantly to all) why this happen in this project and why it is not a bug?
I'll try to express very clear the procedures so you can test and dignify me (and us) with your enlightment so i (or we) can keep learning from you.
Open lazarus project.
Focus on first tab
f9 (run if you dont know)
look at labels on screen
press the (only button)
watch all the numbers appear correctly RIGHT ALIGNED
change to tab 2
whach all the numbers NOT RIGHT ALIGNED.
Close running program (the one you are debuggin in lazarus, not the web browser)
click on second tab (tabsheet2)
press f9 again
look at the labels on screen
press again the only button
watch all the numbers IN TAB TWO corectly right aligned
now switch to tabsheet 1
watch the numbers NOT RIGHT ALIGNED.
so if you can explain why this happens and its not a bug or why it dependes on what tab is focused at compiling time i will be very very glad and (maybe) make an idol on my porch to venerate such high priest on the forum.
if not, well i thank you  for the only useful answer you give me about the anchors and take notice and investigate myself.
Thank you all for your time!

I see. I must confess that I had never realized that a label which has an anchor akLeft but Alignment taRightJustify keeps its right edge fixed when the Caption is changed. Almost sounds like a bug, but I checked with Delphi, and it shows that same unlogical behaviour.

Anyway, if you want a label to be right-aligned and keep its right edge fixed then you should set its akRight anchor, as dsiders already said. And I would even go a step further an use the anchor editor to anchor the right edge of the label to a fixed point, e.g. the right edge of the parent.

I am attaching a modified version of your first project in which the left labels are anchored to the left side of the tabsheet, the center labels are anchored to the center of the tabsheet, and the right labels are anchored to the right sides of the tabsheet. I also painted the background in yellow to exactly see the position of the labels.

thanks! it sound logical to me that if i put a label and i chose right alignment it would align to the rightest side of the label but then again, is confusing. The very rare thing is that when it happens inside a ttab, in the active tab when i compile it and run, appears ok and on the other tabs don't. That is what puzzles me. Then again, stop program, set index to tab 2 and compile again, the tab two aligns as expected but the one is left aligned. That's why i think its a bug
I never used anchors and border spacing but in the example you gave me is working as expected so i will implement it that way next time.
Thank you all!

Then you did not learn anything from this discussion. It has nothing to do with TPageControl... and everything to do with a random number of digits being stuffed into an auto-sized label. But never mind... you have your solution. Copy and paste.  We can all move on now.
Title: Re: problem with label alignment in pagecontrol
Post by: dsiders on November 27, 2022, 01:27:56 am
You can lead a horse to water, but you cannot make it drink. I'm done.
Title: Re: problem with label alignment in pagecontrol
Post by: jamie on November 27, 2022, 02:51:45 am
I wouldn't give up so quicky, I just ran that last test app and yes, there is a bug.

All labels are being process the same way and this is what I've found.

if the label is not showing, in this case the tab sheet that it lives on is not showing, then changing the contents of the text which forces the label to adjust itself for right justify, should move the left position of the label over, and it does not.

The page that is visible does adjust the label over to the left as much as it needs to make it show proper.

 Now you can do this in any combination, and it seems that the initial page that isn't visible does not move the label to the left.

 But after switching back and forth, it seems it never corrects itself, so this is an initial one time for the labels that are not visible and need to adjust the label to the left so that they can right justify the to the border.

 There is definitely a problem, and the last test app makes it easy to work with.
 
  I think @WP could look at this since he has a lot of time on the TLabel  ;D
have a good day.

EDIT:
  A little more detail.

   If seems it has nothing to do with the first time around, it's all to do about not being visible when the label is adjusted. So once it is sized to fit, it will never correct itself afterwards because the label is already long enough to fit the content.
 
  So, while not visible the right justify fails and simply does a left justify.

Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 27, 2022, 11:51:47 am
Let me assume first that the behaviour of a label with Anchors = [akLeft, akTop], Alignment = taRightJustify and AutoSize = true growing to the left when its caption is changed, is correct although it is not logical. I want to simplify the test project in order to understand what's going on - 18 labes is way too much...

The attached project contains a label with Anchors = [akLeft, akTop], Alignment = taRightJustify and AutoSize = true. I had dragged it to the very right of the form such that its right side aligns approximately with the right side of the form. While typing some text into the Caption property it can be seen that the right side of the label is held fixed and the label grows to the left. For the test I chose a rather narrow text for the label, e.g. '1'. And most of all, the label's Visible property has been set to false. In order to get around the mysteries happending during the OnShow/OnActivate events, I added a button which sets the label's Caption to a long text in the OnClick event and then sets Visible to true in order to show the label.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Label1.Caption := 'This is a long, long, long text.';
  4.   Label1.Visible := true;
  5. end;

Running this and clicking the button, you see that the text changes, but it stays at its original starting position. Due to the special treatment of Alignment = taRightJustify it should have moved to the left so that it ends at the right edge of the form.

I think this is the problem reported.

I compiled with Laz/main and with Laz 1.4.4 (the oldest Lazarus that I still have), and both show the same behaviour. Therefore, it is not a recently introduced regression, and probably exists since the beginning. I also converted the project to Delphi, and here the behaviour is "correct" in the sense that the text ends at the right side of the form.

One other experiment with the Laz/version: Exchange the two instructions in the ButtonClick handler so that the label is visible when its Caption is changed. - Now the behaviour is like in Delphi. It seems to be essential that the label is visible.

Following the debugger shows that the issue occurs in TControl.AdjustSize which executes the autosizing operation (due to the changed caption text) only when the control and all its parents are visible.

Please file a bug report. Maybe a developer picks it up and attempts to fix it (but I hope that this will not happen because the autosizing code is very complex, and in my experience it is very probably that something else will break...)

Even if this is not fixed, you always have the possibility to set the label Anchors to [akRight, akTop] (rather than [akLeft, akTop]), and the right alignment will always work. Of course, now the label will move when the size of the container changes. If you don't want this you can put the labels into a panel without akRight (or use the anchor editor to anchor them to other controls).
Title: Re: problem with label alignment in pagecontrol
Post by: calebs on November 27, 2022, 02:53:26 pm
Thanks jamie and wp.
Correct, this i have noticed a long a go and tryed to explain this because i have an application that shows balances on several tabs with a lot of labels and i wasn't aware of the problem until a client note it (the great beta-testers) and then discovered what wp explains. It seems to me there was a bug because the behaviour is not normal netiher expected with alignment.
But there is always an elevated guru like <you know who> that trolls in the road of knowledge and never could arrive to an explanation or if i was missing something.
I will look up to the solutions you gave me wp, for now i solved it with fixed font and filling zeros or spaces to the left of the number to correct the column items because i think left alignment always worked thanks to the anchors (i guess) like high priest shared with a simple mortal like me (or us), but using anchors is maybe a little less work.
I don't know how to fil a bug, never done it before but it's not a critical problem, only a  cosmethical one that can be fixed so many other ways so it's no hurry.
Many thanks to everyone and special one to the gods in the limbo that came down from time to time to help the feral horses of the world.
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 27, 2022, 03:03:36 pm
There is an old bug report about taRightJustify labels not working in the Delphi way. This has been fixed in 2016, but not for the case that the labels are invisible. I now assigned the report to Ondrej who had fixed the main incompatibility. Maybe he looks at it again.

There is no need for you to file another bug report.
Title: Re: problem with label alignment in pagecontrol
Post by: jamie on November 27, 2022, 08:23:51 pm
I see Ondrej put something in the trunk to check the left of the label but whatever he did it didn't solve anything from what I can see, it still fails.

 Maybe he is working on it still.
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 27, 2022, 10:45:58 pm
whatever he did it didn't solve anything from what I can see, it still fails.
No, he fixed the first level of visibility (Label.Visible = false).
Title: Re: problem with label alignment in pagecontrol
Post by: wp on November 30, 2022, 01:24:42 pm
whatever he did it didn't solve anything from what I can see, it still fails.
No, he fixed the first level of visibility (Label.Visible = false).
... but he met serious difficulties when extending the fix into the LCL (all parents of the label must be visible!), and in the end the changes would be too massive and risky so that he decided to revert the related changes and document them as Delphi-incompatibility issues:
- https://wiki.freepascal.org/Lazarus_2.4.0_release_notes#LCL
- https://wiki.freepascal.org/TLabel#Delphi_incompatibility_for_right-aligned_labels
Title: Re: problem with label alignment in pagecontrol
Post by: jamie on November 30, 2022, 03:09:19 pm
why not simply post pone the update of the label after a Text change has been issued until it becomes visible?

 So basically, don't to any of the calculations until visible. make a fPostPoneAlign etc.
TinyPortal © 2005-2018