From f655a52fba3109570ec7cdf137c2173c7921edd2 Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Mon, 20 Apr 2020 07:17:28 -0700 Subject: [PATCH] Allow wxSizer::Fit() to work properly when called from TLW ctor on GTK3 Style information affecting sizes may not be updated by GTK until TLW is shown See #16088 --- src/common/sizer.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index da1a350598..33c8d39ce1 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -968,10 +968,29 @@ wxSize wxSizer::ComputeFittingWindowSize(wxWindow *window) return window->ClientToWindowSize(ComputeFittingClientSize(window)); } +#ifdef __WXGTK3__ +static void FitOnShow(wxShowEvent& event) +{ + wxWindow* win = static_cast(event.GetEventObject()); + wxSizer* sizer = win->GetSizer(); + if (sizer) + sizer->Fit(win); + win->Unbind(wxEVT_SHOW, FitOnShow); +} +#endif + wxSize wxSizer::Fit( wxWindow *window ) { wxCHECK_MSG( window, wxDefaultSize, "window can't be NULL" ); +#ifdef __WXGTK3__ + // GTK3 updates cached style information before showing a TLW, + // which may affect best size calculations, so add a handler to + // redo the calculations at that time + if (!window->IsShown() && window->IsTopLevel()) + window->Bind(wxEVT_SHOW, FitOnShow); +#endif + // set client size window->SetClientSize(ComputeFittingClientSize(window)); @@ -1009,6 +1028,17 @@ void wxSizer::Layout() RepositionChildren(minSize); } +#ifdef __WXGTK3__ +static void SetSizeHintsOnShow(wxShowEvent& event) +{ + wxWindow* win = static_cast(event.GetEventObject()); + wxSizer* sizer = win->GetSizer(); + if (sizer) + sizer->SetSizeHints(win); + win->Unbind(wxEVT_SHOW, SetSizeHintsOnShow); +} +#endif + void wxSizer::SetSizeHints( wxWindow *window ) { // Preserve the window's max size hints, but set the @@ -1020,6 +1050,12 @@ void wxSizer::SetSizeHints( wxWindow *window ) // otherwise SetClientSize() could have no effect if there already are // size hints in effect that forbid requested client size. +#ifdef __WXGTK3__ + // see comment in Fit() + if (!window->IsShown() && window->IsTopLevel()) + window->Bind(wxEVT_SHOW, SetSizeHintsOnShow); +#endif + const wxSize clientSize = ComputeFittingClientSize(window); window->SetMinClientSize(clientSize);