Static text displays incorrect in sizer

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
LookAtMyTruckerHat
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Apr 30, 2021 12:43 am

Static text displays incorrect in sizer

Post by LookAtMyTruckerHat »

I'm having an issue where the static text in my sizer is not showing up correctly when the window is first created; if I drag the text portion outside the parent window or minimize the MDI child then maximize it, the text shows correctly. Attached are images of what the text looks like before and after I min/max.

Any help would be much appreciated.

windows 10
wxwidgets 3.1.4
visual studio community 2019
version 16.1.3

Code: Select all

cTestStaticTextFrame::cTestStaticTextFrame(wxMDIParentFrame* parent, wxString name) 
	: wxMDIChildFrame(parent, wxID_ANY, name)
{
	this->SetBackgroundColour(wxColour("#ededed")) ;
	this->SetInitialSize(wxSize(300, 190)) ;

	wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL) ;
	this->SetSizer(vbox) ;

	wxFlexGridSizer* fgs = new wxFlexGridSizer(3, 2, 0, 0) ;
	fgs->AddGrowableCol(1, 1) ;

	wxStaticText* name_label = new wxStaticText(this, wxID_ANY, wxString("Name")) ;
	wxTextCtrl* name_input = new wxTextCtrl(this, wxID_ANY, wxString("")) ;
	fgs->Add(name_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT, 5) ;
	fgs->Add(name_input, 1, wxEXPAND|wxALL, 5) ;

	wxStaticText* description_label = new wxStaticText(this, wxID_ANY, wxString("Description")) ;
	wxTextCtrl* description_input = new wxTextCtrl(this, wxID_ANY, wxString("")) ;
	fgs->Add(description_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT, 5) ;
	fgs->Add(description_input, 1, wxEXPAND|wxALL, 5) ;

	wxStaticText* active_label = new wxStaticText(this, wxID_ANY, wxString("Active")) ;
	wxCheckBox* active_checkbox = new wxCheckBox(this, wxID_ANY, wxString("")) ;
	fgs->Add(active_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT, 5) ;
	fgs->Add(active_checkbox, 1, wxEXPAND|wxALL, 5) ;

	wxBoxSizer* hbox_btns = new wxBoxSizer(wxHORIZONTAL) ;
	wxButton* help_button = new wxButton(this, 10001, wxString("Help")) ;
	wxButton* ok_button = new wxButton(this, 10003, "Ok") ;
	wxButton* cancel_button = new wxButton(this, 10002, wxString("Cancel")) ;
	hbox_btns->Add(help_button, 0, wxALIGN_LEFT, 10) ;
	hbox_btns->AddStretchSpacer(1) ;
	hbox_btns->Add(ok_button, 0, wxALIGN_RIGHT, 10) ;
	hbox_btns->Add(cancel_button, 0, wxALIGN_RIGHT, 10) ;

	vbox->Add(fgs, 1, wxTOP|wxLEFT|wxRIGHT|wxEXPAND|wxALL, 10) ;
	vbox->Add(hbox_btns, 1, wxLEFT|wxRIGHT|wxTOP|wxEXPAND, 10) ;
}
Attachments
after minmax.PNG
after minmax.PNG (9.55 KiB) Viewed 1579 times
initial window.PNG
initial window.PNG (9.63 KiB) Viewed 1579 times
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Static text displays incorrect in sizer

Post by evstevemd »

Quick look just shows this line might be the issue

Code: Select all

this->SetInitialSize(wxSize(300, 190)) ;
Why do you want initial size? Why not let the sizers take care of it?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
LookAtMyTruckerHat
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Apr 30, 2021 12:43 am

Re: Static text displays incorrect in sizer

Post by LookAtMyTruckerHat »

I just kept that to keep the window from having a huge width. If I remove it the child window is stretched really wide, but the text is still messed up.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Static text displays incorrect in sizer

Post by doublemax »

I can't reproduce the issue with your code.

First find out if it's a redraw or a layout issue. When you have the "bad" case, move a window from another application across your. If this fixes the issue, adding a Refresh() call might help. If not, try adding a Layout() call at the end.

Instead of setting a fixed size, it's better to let the sizer do that.

Comment out two lines:

Code: Select all

this->SetBackgroundColour(wxColour("#ededed")) ;
//this->SetInitialSize(wxSize(300, 190)) ;

wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL) ;
//this->SetSizer(vbox) ;
Change two lines and add one at the end:

Code: Select all

vbox->Add(fgs, 0,  wxALL|wxEXPAND, 10) ;
vbox->Add(hbox_btns, 0, wxALL|wxEXPAND, 10) ;

SetSizerAndFit(vbox);
Use the source, Luke!
LookAtMyTruckerHat
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Apr 30, 2021 12:43 am

Re: Static text displays incorrect in sizer

Post by LookAtMyTruckerHat »

Those code changes fixed it; Apparently me not letting the sizer control the layout was the problem.

Many thanks for the help!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Static text displays incorrect in sizer

Post by PB »

BTW, having such a frame is a bit unusual, it looks more like a dialog.

If you do really use a frame, you should consider creating a wxPanel as a sole child of the frame and create the actual controls as the children of that wxPanel.

This will automatically give the frame background the "correct" color but more importantly, it will make the keyboard navigation between controls work.

Additionally, there is wxStdDialogButtonSizer which ensures the button are placed in the platform-correct order. For example MSW has the OK button precede the Cancel button; however, other platforms have the opposite order. Not sure if this sizer can actually be used in a frame: As wrote above, frames with OK and Cancel buttons are not commonly used.
LookAtMyTruckerHat
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Apr 30, 2021 12:43 am

Re: Static text displays incorrect in sizer

Post by LookAtMyTruckerHat »

Ooh ok. It's a wxMDIChildFrame specifically. The keyboard nav was something I noticed but was going to work out later.

I'll have to look into the dialog class; I just kinda started something else with a frame and kept going with it. Especially if it would help with the key navigation.

Is it generally better to use a wxPanel inside frames like that, as a parent for all the contained elements I mean?

Thanks for the suggestion.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Static text displays incorrect in sizer

Post by doublemax »

LookAtMyTruckerHat wrote: Sat May 01, 2021 8:54 pm Is it generally better to use a wxPanel inside frames like that, as a parent for all the contained elements I mean?
Yes
Use the source, Luke!
Post Reply