Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

Post by heinermueller »

Hello Everybody,

we add controls into a wxFlexGridSizer which is inside a wxScrolledWindow. When the dialog shows up, it is not resized to a proper size. Dialog creation and setup is like this:

Code: Select all

dialog_config_mismatch d(nullptr, wxID_ANY, _(warn));
wxFont font(wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxEmptyString);

auto s1 = new wxStaticText(&d, wxID_ANY, display_path, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
s1->Wrap(-1);
s1->SetFont(font);
d.m_mainsizer->Add(s1, 0, wxALL, 5);

auto s2 = new wxStaticText(&d, wxID_ANY, t.default_value, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
s2->Wrap(-1);
d.m_mainsizer->Add(s2, 0, wxALL, 5);

auto r1 = new wxRadioButton(&d, wxID_ANY, wxString() << system_value, wxDefaultPosition, wxDefaultSize, wxRB_GROUP|wxALIGN_LEFT);
r1->SetValue(true);
d.m_mainsizer->Add(r1, 0, wxALL, 5);

auto r2 = new wxRadioButton(&d, wxID_ANY, wxString() << t.project_value, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
d.m_mainsizer->Add(r2, 0, wxALL, 5);

auto s3 = new wxStaticText(&d, wxID_ANY, t.restart_flag, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_HORIZONTAL);
d.m_mainsizer->Add(s3, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
	
d.m_mainsizer->Layout();
d.m_scrolledWindow1->SetSize(600, 200);
d.m_scrolledWindow1->FitInside();

d.m_main_vertical_boxsizer->Fit(&d);
d.Layout();
So the wxScrolledWindow size should be 600x200px, and the wxFlexGridSizer (which is a bit larger, ca. 600x400) should be inside with scrollbars. But the dialog looks like this:


Image


This is how the dialog is set up in wxFormBuilder:

Image


It should display another grid row with 5 wxStaticText controls. Does anyone know why the dialog does not expand to the 600x200px requested in

Code: Select all

d.m_scrolledWindow1->SetSize(600, 200);
Best,
Heiner
Last edited by heinermueller on Mon May 13, 2019 3:51 pm, edited 1 time in total.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

Post by catalin »

I wonder how you (or the UI builder's creators) manage to output such convoluted code. But the first thing that is obviously wrong is the parent of the UI elements ("&d" everywhere). From your description there is an intermediary scrolled window there, in which case it should be the parent of all elements that it is supposed to contain and its sizer is supposed to layout.
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

Post by heinermueller »

Thanks for the parent hint! When i resize the window, it draws and works correctly now. The dialog opens almost with the correct size now. This code sequence:

Code: Select all

	
const wxSize desired_client_size(600, 250);
d.m_scrolledWindow1->SetSize(desired_client_size);
d.m_scrolledWindow1->FitInside();
d.m_mainsizer->Layout();
d.m_mainsizer->SetSizeHints(&d);
d.Layout();
if (d.ShowModal())
{
...
}
results in a bit too small of a dialog window:

Image

How can we avoid the horizontal scroll bar in this case, let the dialog have the exact client to match the wxScrolledWindow size with scrollbar? Thanks for helping advice!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

Post by doublemax »

You can get the width of a vertical scrollbar with:

Code: Select all

#include <wx/settings.h>
///
wxSystemSettings::GetMetric(wxSYS_VSCROLL_X)
Make the client area of the dialog bigger by that amount, so that there is always room for the scrollbar.

BTW:

Code: Select all

d.m_scrolledWindow1->SetSize(desired_client_size);
d.m_scrolledWindow1->FitInside();
d.m_mainsizer->Layout();
d.m_mainsizer->SetSizeHints(&d);
d.Layout();
Manipulating a member variable of the dialog "from outside" is ugly. This code should be in the dialog constructor.
Use the source, Luke!
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

Post by heinermueller »

Hi doublemax,

Code: Select all

wxSystemSettings::GetMetric(wxSYS_VSCROLL_X)
Thanks for the hint - am i expected to calculate the dialog width or can i automate this? Actually i expected that

Code: Select all

d.Layout();
would take a scrollbar existence/visibility into account and resize the dialogs client size accordingly. Is there a possibility to automate this?

PS
Manipulating a member variable of the dialog "from outside" is ugly.
True, its done "straight and rough" here just for explanation purpose :D

PPS
Fantastic forum here, really helpful and quick!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Dynamically add controls into wxFlexGridsizer inside wxScrolledWindow - no resize

Post by doublemax »

Thanks for the hint - am i expected to calculate the dialog width?
Yes.
...would take a scrollbar existence/visibility into account and resize the dialogs client size accordingly. Is there a possibility to automate this?
No.
Use the source, Luke!
Post Reply