Page 1 of 1

wxStaticBox appearance on Windows 10

Posted: Wed Mar 13, 2019 5:18 pm
by rjinman
The image is fairly self-explanatory.


As you can see, the labels on the static box have a white background, which looks pretty terrible, but the main problem is that they are overlapped by the box's contents. This is despite adding spacers to the sizer, which seems to have no effect on Windows.

Code: Select all

wxStaticBox* ParamsPage::constructRenderParamsPanel(wxWindow* parent) {
  auto box = new wxStaticBox(parent, wxID_ANY, wxGetTranslation("General"));

  auto grid = new wxFlexGridSizer(2);

  auto lblMaxI = constructLabel(box, wxGetTranslation("Max iterations"));
  string strMaxI = std::to_string(DEFAULT_MAX_ITERATIONS);
  m_txtMaxIterations = constructTextBox(box, strMaxI);
  auto lblZoomAmount = constructLabel(box,
                                      wxGetTranslation("Zoom amount"));
  m_txtZoomAmount = constructTextBox(box, std::to_string(DEFAULT_ZOOM));

  grid->Add(lblMaxI, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
  grid->Add(m_txtMaxIterations, 0, wxEXPAND | wxRIGHT, 10);
  grid->Add(lblZoomAmount, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
  grid->Add(m_txtZoomAmount, 0, wxEXPAND | wxRIGHT, 10);


  return box;
Any help would be much appreciated. Thanks.

Re: wxStaticBox appearance on Windows 10

Posted: Wed Mar 13, 2019 6:05 pm
Which version of Windows this is?
Can you build the widgets or layout sample and see whether they work for you?

Thank you.

Re: wxStaticBox appearance on Windows 10

Posted: Wed Mar 13, 2019 6:14 pm
by rjinman
I just realised I was wrong that the spacers have no effect. I've "fixed" the problem by being extremely liberal with my use of padding. If I were to remove the spacers completely, it would look very broken.

This is Windows 10 Enterprise, Version 10.0.17134 Build 17134.

Re: wxStaticBox appearance on Windows 10

Posted: Wed Mar 13, 2019 6:34 pm
by doublemax
Try to use a wxStaticBoxSizer instead of a pure wxStaticBox. I'd assume it leaves enough space for the label by default.

Re: wxStaticBox appearance on Windows 10

Posted: Wed Mar 13, 2019 8:39 pm
by PB
I can confirm that
(a) there is an issue if the approach in the OP is used
(b) the simple workaround suggested by doublemax works


Code: Select all

#include <wx/wx.h>
#include <wx/notebook.h>

class MyFrame: public wxFrame
    MyFrame() : wxFrame (NULL, wxID_ANY, "Test")
        wxNotebook* notebook = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxSize(800, 600));

        AddNotebookPage(notebook, true);
        AddNotebookPage(notebook, false);

    void AddNotebookPage(wxNotebook* notebook, bool useStaticBoxSizer)
        wxPanel* page = new wxPanel(notebook, wxID_ANY);        
        wxBoxSizer* pageSizer = new wxBoxSizer(wxVERTICAL);
        wxString pageTitle;

        if ( useStaticBoxSizer )
        {   // wxFlexGridSizer is added to wxStaticBoxSizer, will display properly                        
            wxStaticBoxSizer* staticBoxSizer = new wxStaticBoxSizer(wxVERTICAL, page, "Static Box Correct");       
            staticBoxSizer->Add(AddNotebookPageControls(staticBoxSizer->GetStaticBox()), wxSizerFlags().Expand().Border());        
            pageSizer->Add(staticBoxSizer, wxSizerFlags().Expand().Border());
            pageTitle = "Correct layout";
        {   // wxFlexGridSizer is set to wxStaticBox, will display wrong            
            wxStaticBox* staticBox = new wxStaticBox(page, wxID_ANY, "Static Box Wrong");
            pageSizer->Add(staticBox, wxSizerFlags().Expand().Border());        
            pageTitle = "Incorrect layout";        
        notebook->AddPage(page, pageTitle);  

    wxSizer* AddNotebookPageControls(wxWindow* parent)
        wxFlexGridSizer* flexGridSizer = new wxFlexGridSizer(2);
        wxStaticText* label = new wxStaticText(parent, wxID_ANY, "Label");
        wxTextCtrl* textCtrl = new wxTextCtrl(parent, wxID_ANY);

        flexGridSizer->Add(label, wxSizerFlags().Expand().Border());
        flexGridSizer->Add(textCtrl, wxSizerFlags().Expand().Border());

        return flexGridSizer;

class MyApp : public wxApp
    bool OnInit()
        (new MyFrame())->Show();               
        return true;
produces (Win 10 v1809, wxWidgets GIT master) this

without workaround
statbox-incorrect.png (3.44 KiB) Viewed 185 times
with workaround
statbox-correct.png (3.74 KiB) Viewed 185 times

Re: wxStaticBox appearance on Windows 10

Posted: Thu Mar 14, 2019 12:44 pm
by rjinman
Thanks, that works. I'd tried using a wxStaticBoxSizer before with no luck, but I was adding the static box's sizer to the static box itself rather than to the wxStaticBoxSizer.

For example:

Code: Select all

auto page = new wxNotebookPage(parent, wxID_ANY);
auto pageSizer = new wxBoxSizer(wxVERTICAL);

auto boxSizer = new wxStaticBoxSizer(wxVERTICAL, page, "My Static Box");
auto box = boxSizer->GetStaticBox();
auto vbox = new wxBoxSizer(wxVERTICAL);
auto button = new wxButton(box, wxID_ANY, "Click me");

box->SetSizer(vbox); // Wrong
boxSizer->Add(vbox); // Correct