From ef54d065c721ce013d2dc96284629441aba1672b Mon Sep 17 00:00:00 2001 From: Lluis Sanchez Date: Tue, 12 Nov 2013 21:04:43 +0100 Subject: [PATCH] [GTK] Another fix for label alignment --- Testing/Tests/LabelTests.cs | 30 ++++++++++++++---- Testing/Tests/ReferenceImageVerifierDialog.cs | 21 ++++++++++-- ...abel.AlignCenter => Label.AlignCenter.png} | Bin ...erWrapped => Label.AlignCenterWrapped.png} | Bin .../Label.AlignCenterWrappedChangeText.png | Bin 0 -> 2116 bytes .../{Label.AlignLeft => Label.AlignLeft.png} | Bin ...LeftWrapped => Label.AlignLeftWrapped.png} | Bin ...{Label.AlignRight => Label.AlignRight.png} | Bin ...ghtWrapped => Label.AlignRightWrapped.png} | Bin Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs | 19 +++++++---- 10 files changed, 54 insertions(+), 16 deletions(-) rename Testing/Tests/ReferenceImages/{Label.AlignCenter => Label.AlignCenter.png} (100%) rename Testing/Tests/ReferenceImages/{Label.AlignCenterWrapped => Label.AlignCenterWrapped.png} (100%) create mode 100644 Testing/Tests/ReferenceImages/Label.AlignCenterWrappedChangeText.png rename Testing/Tests/ReferenceImages/{Label.AlignLeft => Label.AlignLeft.png} (100%) rename Testing/Tests/ReferenceImages/{Label.AlignLeftWrapped => Label.AlignLeftWrapped.png} (100%) rename Testing/Tests/ReferenceImages/{Label.AlignRight => Label.AlignRight.png} (100%) rename Testing/Tests/ReferenceImages/{Label.AlignRightWrapped => Label.AlignRightWrapped.png} (100%) diff --git a/Testing/Tests/LabelTests.cs b/Testing/Tests/LabelTests.cs index 349f7d18..abb168dd 100644 --- a/Testing/Tests/LabelTests.cs +++ b/Testing/Tests/LabelTests.cs @@ -51,7 +51,7 @@ namespace Xwt var la = new Label ("Some text here"); la.TextAlignment = Alignment.Center; la.BackgroundColor = Xwt.Drawing.Colors.LightGray; - CheckWidgetRender ("Label.AlignCenter", la); + CheckWidgetRender ("Label.AlignCenter.png", la); } [Test] @@ -62,7 +62,25 @@ namespace Xwt la.Wrap = WrapMode.Word; la.WidthRequest = 200; la.BackgroundColor = Xwt.Drawing.Colors.LightGray; - CheckWidgetRender ("Label.AlignCenterWrapped", la); + CheckWidgetRender ("Label.AlignCenterWrapped.png", la); + } + + [Test] + public void AlignCenterWrappedChangeText () + { + var la = new Label ("Some text here"); + la.TextAlignment = Alignment.Center; + la.Wrap = WrapMode.Word; + la.WidthRequest = 200; + + using (var win = new Window { Width = 200, Height = 100 }) { + win.Content = la; + ShowWindow (win); + la.Text = "Some text here Some text here"; + WaitForEvents (); + var img = Toolkit.CurrentEngine.RenderWidget (la); + ReferenceImageManager.CheckImage ("Label.AlignCenterWrappedChangeText.png", img); + } } [Test] @@ -71,7 +89,7 @@ namespace Xwt var la = new Label ("Some text here"); la.TextAlignment = Alignment.Start; la.BackgroundColor = Xwt.Drawing.Colors.LightGray; - CheckWidgetRender ("Label.AlignLeft", la); + CheckWidgetRender ("Label.AlignLeft.png", la); } [Test] @@ -82,7 +100,7 @@ namespace Xwt la.Wrap = WrapMode.Word; la.WidthRequest = 200; la.BackgroundColor = Xwt.Drawing.Colors.LightGray; - CheckWidgetRender ("Label.AlignLeftWrapped", la); + CheckWidgetRender ("Label.AlignLeftWrapped.png", la); } [Test] @@ -91,7 +109,7 @@ namespace Xwt var la = new Label ("Some text here"); la.TextAlignment = Alignment.End; la.BackgroundColor = Xwt.Drawing.Colors.LightGray; - CheckWidgetRender ("Label.AlignRight", la); + CheckWidgetRender ("Label.AlignRight.png", la); } [Test] @@ -102,7 +120,7 @@ namespace Xwt la.Wrap = WrapMode.Word; la.WidthRequest = 200; la.BackgroundColor = Xwt.Drawing.Colors.LightGray; - CheckWidgetRender ("Label.AlignRightWrapped", la); + CheckWidgetRender ("Label.AlignRightWrapped.png", la); } } } diff --git a/Testing/Tests/ReferenceImageVerifierDialog.cs b/Testing/Tests/ReferenceImageVerifierDialog.cs index ceea784d..d04cfdf6 100644 --- a/Testing/Tests/ReferenceImageVerifierDialog.cs +++ b/Testing/Tests/ReferenceImageVerifierDialog.cs @@ -55,13 +55,28 @@ namespace Xwt nameLabel.Font = nameLabel.Font.WithWeight (Xwt.Drawing.FontWeight.Bold); img1 = new ImageView (); - table.Add (img1, 0, 2, hexpand:true, vexpand:true); + var frame = new FrameBox { + Content = img1, + BorderColor = Xwt.Drawing.Colors.Gray, + BorderWidth = 1 + }; + table.Add (frame, 0, 2, hexpand:true, vexpand:true, hpos:WidgetPlacement.Center, vpos:WidgetPlacement.Center); imgDiff = new ImageView (); - table.Add (imgDiff, 1, 2, hexpand:true, vexpand:true); + frame = new FrameBox { + Content = imgDiff, + BorderColor = Xwt.Drawing.Colors.Gray, + BorderWidth = 1 + }; + table.Add (frame, 1, 2, hexpand:true, vexpand:true, hpos:WidgetPlacement.Center, vpos:WidgetPlacement.Center); img2 = new ImageView (); - table.Add (img2, 2, 2, hexpand:true, vexpand:true); + frame = new FrameBox { + Content = img2, + BorderColor = Xwt.Drawing.Colors.Gray, + BorderWidth = 1 + }; + table.Add (frame, 2, 2, hexpand:true, vexpand:true, hpos:WidgetPlacement.Center, vpos:WidgetPlacement.Center); var buttonBox = new HBox (); table.Add (buttonBox, 0, 3, colspan:2, hexpand:true); diff --git a/Testing/Tests/ReferenceImages/Label.AlignCenter b/Testing/Tests/ReferenceImages/Label.AlignCenter.png similarity index 100% rename from Testing/Tests/ReferenceImages/Label.AlignCenter rename to Testing/Tests/ReferenceImages/Label.AlignCenter.png diff --git a/Testing/Tests/ReferenceImages/Label.AlignCenterWrapped b/Testing/Tests/ReferenceImages/Label.AlignCenterWrapped.png similarity index 100% rename from Testing/Tests/ReferenceImages/Label.AlignCenterWrapped rename to Testing/Tests/ReferenceImages/Label.AlignCenterWrapped.png diff --git a/Testing/Tests/ReferenceImages/Label.AlignCenterWrappedChangeText.png b/Testing/Tests/ReferenceImages/Label.AlignCenterWrappedChangeText.png new file mode 100644 index 0000000000000000000000000000000000000000..f1398bc9167dfa0d2129c371563c688bd1309702 GIT binary pat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D-)5p2^Rd+7{_%qkGv+SUMy-e2OF0nFWDT?T1k4>XP5kEom|=V7F6 z=DzpS5mpD6DLo{&&Yu(15iN}vXq3H>K|D|ty(TO4dW-qCIAKcMkh|+JW?Sscr+}G% zvh#>$ftri-ko9?o<$=J52BA68yPJL?$S~}c1FaAG$FkVRU>E6$R+W885}8|NX4Xq- zI$D|Kq&wV{t7Z&X+vs#f>|UULRT=Y;=_HPGrrDF%9UE$IddDCR`DVHcPbS_22kWdZ z(ld~2=&;LCZfCaqL$+>P34m}qWqp4Y!-(#??o4I%2$G*&zjxHrmbw*HIcj7R!Po)o zah2p={q*-=xJ46SvhIJ1+55SVJ!J?w;O(u9kr`IO*Sf-PF`jl~apv(vvoI z>t>=DA9iTmz#1~QHQ?=hcvaVKN2J^bKDS|J*S-C*bo0zFmoWW?FJ92y?uw8nT6w|e zA{(M2{d>IWD@Pq(QO8lf2`Y{1FJ!CdxeOwC3zHX!{hh^2_t)L|lh``V_lW%_1de6? z5l^7|%K=pZ2?yyEP@BqVhIh7j2&tm;J6y*#W=ZwbMG1gsF&dvwY_2kC0%%FH|8FNYqytDzHf zikg6>v&Km1$IHN+|H!7O<+ToMT2=MeD@pFFdv7e)oy?TPCE*pP%Gj?9N%eb2Q>|il zDVD{RDkSNNauvBguSCFKQ@(`mHT)T%R9ND+RsrNtgL3D-dZcP(*JRPVKpP^WllB04 oG%8^QK5|z4KWyB8AJcEwO^oIP@tK)UC$|8wxM7WJFma9h8z8jqqyPW_ literal 0 HcmV?d00001 diff --git a/Testing/Tests/ReferenceImages/Label.AlignLeft b/Testing/Tests/ReferenceImages/Label.AlignLeft.png similarity index 100% rename from Testing/Tests/ReferenceImages/Label.AlignLeft rename to Testing/Tests/ReferenceImages/Label.AlignLeft.png diff --git a/Testing/Tests/ReferenceImages/Label.AlignLeftWrapped b/Testing/Tests/ReferenceImages/Label.AlignLeftWrapped.png similarity index 100% rename from Testing/Tests/ReferenceImages/Label.AlignLeftWrapped rename to Testing/Tests/ReferenceImages/Label.AlignLeftWrapped.png diff --git a/Testing/Tests/ReferenceImages/Label.AlignRight b/Testing/Tests/ReferenceImages/Label.AlignRight.png similarity index 100% rename from Testing/Tests/ReferenceImages/Label.AlignRight rename to Testing/Tests/ReferenceImages/Label.AlignRight.png diff --git a/Testing/Tests/ReferenceImages/Label.AlignRightWrapped b/Testing/Tests/ReferenceImages/Label.AlignRightWrapped.png similarity index 100% rename from Testing/Tests/ReferenceImages/Label.AlignRightWrapped rename to Testing/Tests/ReferenceImages/Label.AlignRightWrapped.png diff --git a/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs b/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs index c9e7ea74..e7a7f691 100644 --- a/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs +++ b/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs @@ -175,15 +175,20 @@ namespace Xwt.GtkBackend Label.Layout.GetPixelSize (out unused, out wrapHeight); if (wrapWidth != args.Allocation.Width || oldHeight != wrapHeight) { wrapWidth = args.Allocation.Width; - // GTK renders the text using the calculated pixel width, not the allocated width. - // If the calculated width is smaller and text is not left aligned, then a gap is - // shown at the right of the label. We then have the adjust the allocation. - if (Label.Justify == Gtk.Justification.Right) - Label.Xpad = wrapWidth - unused; - else if (Label.Justify == Gtk.Justification.Center) - Label.Xpad = (wrapWidth - unused) / 2; Label.QueueResize (); } + // GTK renders the text using the calculated pixel width, not the allocated width. + // If the calculated width is smaller and text is not left aligned, then a gap is + // shown at the right of the label. We then have the adjust the allocation. + if (Label.Justify == Gtk.Justification.Right) { + var w = wrapWidth - unused; + if (w != Label.Xpad) + Label.Xpad = w; + } else if (Label.Justify == Gtk.Justification.Center) { + var w = (wrapWidth - unused) / 2; + if (w != Label.Xpad) + Label.Xpad = w; + } } void HandleLabelDynamicSizeRequest (object o, Gtk.SizeRequestedArgs args)