wxGTK: wxChoice renders as too short vertically if Layout() is called while it's empty (Issue #23382)

57 views
Skip to first unread message

Eevee

unread,
Mar 27, 2023, 6:46:26 PM3/27/23
to wx-...@googlegroups.com, Subscribed

Description

This code:

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="wxchoice size test")

        box = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(box)

        choice1 = wx.Choice(self)
        choice1.Set(["Item one", "Item two"])
        box.Add(choice1)

        choice2 = wx.Choice(self, -1, wx.DefaultPosition, wx.DefaultSize, ["Item one", "Item two"])
        box.Add(choice2)

app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()

Produces this window:

image

If I then select (as the user) an item in each dropdown, it's clear that the first one is too short to contain a line of text:

Screenshot_20230327_160947

If I extend the constructor as follows to select an item before layout happens automatically, the problem is resolved:

    def __init__(self):
        # ... same as before ...
        choice1.SetSelection(0)
        choice2.SetSelection(0)

However if I invoke layout manually first, the problem is not resolved:

    def __init__(self):
        # ... same as before ...
        self.Layout()
        choice1.SetSelection(0)
        choice2.SetSelection(0)
        # calling it a second time doesn't make any difference
        #self.Layout()

I'm guessing empty text is being measured as very short?

A similar thing happens with wxTextCtrl (the vertical padding is almost nonexistent), but curiously that happens even when it's constructed with non-empty text, and it only reproduces with C++ — it's fine via wxPython.

I know this used to work, but can't be much more specific than that, unfortunately.

Platform and version information

Arch Linux x64
wxGTK 3.2.2
GTK 3.24.36
Using theme breeze-gtk 5.26.5 on X11

Patch against minimal sample

diff --git a/samples/minimal/minimal.cpp b/samples/minimal/minimal.cpp
index 470e765423..ed488971d4 100644
--- a/samples/minimal/minimal.cpp
+++ b/samples/minimal/minimal.cpp
@@ -144,6 +144,9 @@ MyFrame::MyFrame(const wxString& title)
     // set the frame icon
     SetIcon(wxICON(sample));
 
+    auto main_sizer = new wxBoxSizer(wxVERTICAL);
+    SetSizer(main_sizer);
+
 #if wxUSE_MENUBAR
     // create a menu bar
     wxMenu *fileMenu = new wxMenu;
@@ -167,7 +170,7 @@ MyFrame::MyFrame(const wxString& title)
     wxButton* aboutBtn = new wxButton(this, wxID_ANY, "About...");
     aboutBtn->Bind(wxEVT_BUTTON, &MyFrame::OnAbout, this);
     sizer->Add(aboutBtn, wxSizerFlags().Center());
-    SetSizer(sizer);
+    main_sizer->Add(sizer);
 #endif // wxUSE_MENUBAR/!wxUSE_MENUBAR
 
 #if wxUSE_STATUSBAR
@@ -175,6 +178,21 @@ MyFrame::MyFrame(const wxString& title)
     CreateStatusBar(2);
     SetStatusText("Welcome to wxWidgets!");
 #endif // wxUSE_STATUSBAR
+
+    const wxString choice_labels[] = {"Item one", "Item two"};
+    auto choice1 = new wxChoice(this, wxID_ANY);
+    choice1->Set(2, choice_labels);
+    main_sizer->Add(choice1);
+    auto choice2 = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 2, choice_labels);
+    main_sizer->Add(choice2);
+
+    // wxTextCtrl is similarly very cramped vertically...  though not under wxPython for some reason
+    main_sizer->Add(new wxTextCtrl(this, wxID_ANY, "hello world"));
+
+    Layout();
+    // These fix it, unless you also uncomment Layout()
+    //choice1->SetSelection(0);
+    //choice2->SetSelection(0);
 }


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382@github.com>

Ian McInerney

unread,
Mar 29, 2023, 9:14:16 AM3/29/23
to wx-...@googlegroups.com, Subscribed

I believe GTK will only size the combobox based on the currently selected string, so there is an existing workaround in the wxChoice code that does this manually for the width of the box (wxChoice::DoGetBestSize() calls wxChoiceBase::DoGetBestSize(), which iterates through every single item in the box to find its width. That function discards the height, so I guess we should instead try to find the height as well in that function.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1488587860@github.com>

paulcor

unread,
Mar 30, 2023, 12:05:07 PM3/30/23
to wx-...@googlegroups.com, Subscribed

The minimal sample with your changes looks fine for me. Might be a problem with your theme.

That function discards the height

We get the height from GTK, in wxChoice::DoGetSizeFromTextSize().


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1490558795@github.com>

Eevee

unread,
Mar 30, 2023, 2:20:41 PM3/30/23
to wx-...@googlegroups.com, Subscribed

That's strange; this theme is a first-class KDE project, and this regressed somewhere, but the theme seems to be largely stable.

I don't see this happening anywhere in gimp, firefox, thunderbird, etc. either — for comparison, left is slade (wx), right is gimp (gtk)

Screenshot_20230330_115224


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1490732927@github.com>

Ian McInerney

unread,
Mar 31, 2023, 6:35:59 AM3/31/23
to wx-...@googlegroups.com, Subscribed

I have definitely seen weird sizing behavior with GTK comboboxes in the past. The specific case I had was a bitmap combobox (so wxBitmapComboBox, which is a GTK combobox at its core) was not getting sized appropriately when there was no selection. The way to fix it was select an item, compute the minimum size, apply that size, then deselect the item (e.g. this code: https://gitlab.com/kicad/code/kicad/-/commit/1931677316c35124434dfbd62a98e33cb99581d2). I wouldn't be surprised if there is something in the GTK cell that is being used to get the height of the combobox that doesn't like the empty text.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1491707085@github.com>

taler21

unread,
Nov 21, 2023, 5:45:07 AM11/21/23
to wx-...@googlegroups.com, Subscribed

I can confirm that this issue is theme specific.

It can also be seen in the unmodified widgets sample on the 'Choice' page, where the wxChoice is initially empty.

On openSUSE Leap 15.5 with the default theme Breeze the wxChoice is displayed truncated.
widgets-sample-choice_Breeze

But with the theme Adwaita it looks correct.
widgets-sample-choice_Adwaita

Platform and version information

  • wxWidgets version you use: 3.2.4
  • wxWidgets port you use: wxGTK
  • OS and its version: openSUSE Leap 15.5
    • GTK version: 3.24.34
    • Which GDK backend is used: X11
    • Desktop environment : KDE
    • Current theme: Breeze


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1820668897@github.com>

paulcor

unread,
Nov 22, 2023, 1:42:42 AM11/22/23
to wx-...@googlegroups.com, Subscribed

Thanks, I can reproduce with the Breeze theme.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1822198649@github.com>

taler21

unread,
Nov 22, 2023, 3:07:22 AM11/22/23
to wx-...@googlegroups.com, Subscribed

Thanks for the quick fix, works well.

Please also backport this improvement to 3.2. TIA!


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1822285542@github.com>

paulcor

unread,
Nov 23, 2023, 11:33:34 AM11/23/23
to wx-...@googlegroups.com, Subscribed

Closed #23382 as completed.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issue/23382/issue_event/11049555450@github.com>

VZ

unread,
Nov 23, 2023, 11:44:29 AM11/23/23
to wx-...@googlegroups.com, Subscribed

Paul, could you please update docs/changes.txt in 3.2 too? TIA!


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23382/1824706976@github.com>

Reply all
Reply to author
Forward
0 new messages