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.

Image

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);
  box->SetSizer(grid);

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

  grid->AddSpacer(10);
  grid->AddSpacer(10);
  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);

  grid->AddGrowableCol(0);

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

Re: wxStaticBox appearance on Windows 10

Posted: Wed Mar 13, 2019 6:05 pm
by ONEEYEMAN
Hi,
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

This SSCCE

Code: Select all

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

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

        AddNotebookPage(notebook, true);
        AddNotebookPage(notebook, false);
        notebook->Layout();
    }

    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";
        }
        else
        {   // wxFlexGridSizer is set to wxStaticBox, will display wrong            
            wxStaticBox* staticBox = new wxStaticBox(page, wxID_ANY, "Static Box Wrong");
            staticBox->SetSizer(AddNotebookPageControls(staticBox));                            
            pageSizer->Add(staticBox, wxSizerFlags().Expand().Border());        
            pageTitle = "Incorrect layout";        
        }                                
        page->SetSizer(pageSizer); 
        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());
        flexGridSizer->AddGrowableCol(1);

        return flexGridSizer;
    }   
};

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

without workaround
statbox-incorrect.png
statbox-incorrect.png (3.44 KiB) Viewed 280 times
with workaround
statbox-correct.png
statbox-correct.png (3.74 KiB) Viewed 280 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);
page->SetSizer(pageSizer);

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");
vbox->Add(button);

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

pageSizer->Add(boxSizer);