picoe / Eto

Cross platform GUI framework for desktop and mobile applications in .NET
Other
3.58k stars 325 forks source link

Gtk2 Labels don't align with correct vertical spacing #347

Closed k5jae closed 9 years ago

k5jae commented 9 years ago

Here is a sample that shows the issue: sample

The red markings is where there is an extra space. The extra space disappears if the window is resized. It seems like the issue is being caused by the menu item as it collapses correctly when the menu item is removed. This is with GTK2 under Linux.

EDIT: It isn't the "menu" per se. I can expose this behavior with other widgets as well.

Code sample below:

    public class MainForm : Form
    {
        public MainForm ()
        {
            // Must use gtk-sharp2 for this sample to show the issue...
            ClientSize = new Size(800, 600);
            Title = "labelTest";

            var clickMe = new Command {
                MenuText = "Click Me!",
            };
            clickMe.Executed += (sender, e) => MessageBox.Show (this, "I was clicked!");

            Menu = new MenuBar ();

            // if the menu item add below is commented out, the labels align correctly.
            Menu.Items.Add (new ButtonMenuItem { Text = "&File", Items = { clickMe } });

            Content = new TableLayout {
                Rows = {
                    new TableRow (new Label{ Text = "This is a short text that can be viewed" }),
                    new TableRow (new Label{ Text = "This is a long text that can be viewed and it may render with additional space below" }),
                    new TableRow (new Label{ Text = "This is a short text that can be viewed" }),
                    new TableRow (new Label{ Text = "This is a long text that can be viewed and it may render with additional space below" }),
                    new TableRow (new Label{ Text = "Resize the window will case labels to align correctly." })
                }
            };
        }
    }
k5jae commented 9 years ago

Another data point: as soon as I press the ALT key for menu shortcut, the labels spread out again.

cwensley commented 9 years ago

Thanks for the report! I think I have this one fixed. Please let me know if there's any problems.

k5jae commented 9 years ago

I grabbed the latest and it still created the extra space for me. However, by looking at your changes I was able to quickly determine what was going on for me. When the label first gets created it gets an OnSizeRequested call. At this point the label has a smaller width than the final (in my case), which cause it to wrap. The call to Layout.GetPixelSize returns both width and height, which is used to set the widget size. When SetWrapWidth is called, the code sets the correct width, but the height is not addressed, meaning the extra line of widget height is unchanged. Here is a tiny tiny patch that I used to change the height of the widget, once the new width was known. This way if Pango decides that the text is not long enough to wrap, the widget is collapsed in the Y-axis. It works for my application. See diff below:

diff --git a/Source/Eto.Gtk/Forms/Controls/LabelHandler.cs b/Source/Eto.Gtk/Forms/Controls/LabelHandler.cs
index 97d9fc9..0d9d496 100644
--- a/Source/Eto.Gtk/Forms/Controls/LabelHandler.cs
+++ b/Source/Eto.Gtk/Forms/Controls/LabelHandler.cs
@@ -60,7 +60,13 @@ namespace Eto.GtkSharp.Forms.Controls
                        {
                                if (!IsRealized || SingleLineMode || width == 0)
                                        return;
+
                                Layout.Width = (int)(width * Pango.Scale.PangoScale);
+                               #if GTK2
+                               int pixWidth, pixHeight;
+                               Layout.GetPixelSize(out pixWidth, out pixHeight);
+                               HeightRequest = pixHeight;
+                               #endif
                                if (wrapWidth != width)
                                {
                                        wrapWidth = width;
cwensley commented 9 years ago

Excellent, thanks for checking into that. I'll take a look and give it another go.

k5jae commented 9 years ago

I'm still manually applying this patch. It has been working well for me. Will you merge this into 'develop' branch soon?

cwensley commented 9 years ago

Hm, I can't seem to reproduce the issue.. do you have a case where you can still reproduce this?

cwensley commented 9 years ago

I figured it out. Thanks again for submitting the issue and the patch! I included the changes for Gtk3 as well since it fixes the size of wrapped labels.

k5jae commented 9 years ago

Cool. I've been off the grid for a couple of weeks. Looking forward to getting the update

Jae On Jun 29, 2015 1:30 AM, "Curtis Wensley" notifications@github.com wrote:

I figured it out. Thanks again for submitting the issue and the patch! I included the changes for Gtk3 as well since it fixes the size of wrapped labels.

— Reply to this email directly or view it on GitHub https://github.com/picoe/Eto/issues/347#issuecomment-116497430.