wxGridBagSizer bug with MaxSize? or I'm confused??

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
DaveNadler
Experienced Solver
Experienced Solver
Posts: 51
Joined: Thu Nov 17, 2011 2:13 pm

wxGridBagSizer bug with MaxSize? or I'm confused??

Post by DaveNadler » Mon May 18, 2020 9:55 pm

Hi - I'm trying to limit the MAXIMUM vertical size of a cell in a wxGridBagSizer.
At doublemax's suggestion, I wrote a minimal app to debug, and I think something's
wrong with the sizer. First: This shows the desired behavior:
http://www.nadler.com/backups/wxGridBag ... g_Demo.mp4
The code unfortunately only works when I set the MinSize, and ignores
the maxsize. Here's the entire test:

Greatly appreciate any explanation, if I'm completely misunderstanding,
or if this is an actual bug...
Thanks!
Best Regards, Dave

Code: Select all

// frame constructor
MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
{
    // set the frame icon
    SetIcon(wxICON(sample));

    // create a menu bar
  #if 1
    wxMenu *fileMenu = new wxMenu;
    // the "About" item should be in the help menu
    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
    fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
    // now append the freshly created menu to the menu bar...
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, "&File");
    menuBar->Append(helpMenu, "&Help");
    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);
  #endif

    /* THIS DOES NOT WORK: this->SetSize(wxSize(200, 300)); */
    /* But This does */ this->SetMinSize(wxSize(200, 300));

    wxPanel* leftTopBluePanel = new wxPanel(this, wxID_ANY);
    leftTopBluePanel->SetBackgroundColour(*wxBLUE);
    wxPanel* leftBottomGreenPanel = new wxPanel(this, wxID_ANY);
    leftBottomGreenPanel->SetBackgroundColour(*wxGREEN);
    wxPanel* rightRedTwoRowPanel = new wxPanel(this, wxID_ANY);
    rightRedTwoRowPanel->SetBackgroundColour(*wxRED);

    // Set size constraints of leftBottom panel
    wxSize leftBottomGreenPanelSize(/*let X expand*/-1,/*but constrain y*/120);
  #if 0
    leftBottomGreenPanel->SetMaxSize(leftBottomGreenPanelSize); //  Does not work...
  #elif 0
    leftBottomPanel->SetSize(leftBottomGreenPanelSize); // Does not work...
  #else
    // BUG: SetMinSize actually sets the maximum size...
    leftBottomGreenPanel->SetMinSize(leftBottomGreenPanelSize);
  #endif

    // GridBag:
    // - column 0 is left 2 panels
    // - column 1 is right panel
    wxGridBagSizer* gbSizer = new wxGridBagSizer(); // can grow in both dimensions
    // column 0
    gbSizer->Add(leftTopBluePanel, wxGBPosition(0, 0), wxGBSpan(1, 1), wxALIGN_LEFT | wxALIGN_TOP | wxEXPAND, 5);
    gbSizer->Add(leftBottomGreenPanel, wxGBPosition(1, 0), wxGBSpan(1, 1), wxALIGN_LEFT | wxEXPAND, 5);
    // column 1
    gbSizer->Add(rightRedTwoRowPanel, wxGBPosition(0, 1), wxGBSpan(2, 1), wxEXPAND, 5);

    // Both columns can grow, top row can grow
    gbSizer->AddGrowableCol(0);
    gbSizer->AddGrowableCol(1);
    gbSizer->AddGrowableRow(0);
    // Note 2nd row is NOT added as Growable...

    gbSizer->Fit(this);
    this->SetSizer(gbSizer);
    this->Layout();

#if wxUSE_STATUSBAR
    // create a status bar just for fun (by default with 1 pane only)
    CreateStatusBar(2);
    SetStatusText("wxGridBagSizer Bug Demo");
#endif // wxUSE_STATUSBAR
}

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGridBagSizer bug with MaxSize? or I'm confused??

Post by doublemax » Mon May 18, 2020 10:43 pm

The code works correctly. If you remove the SetMinSize() you'll notice that the respective panel isn't visible at all or only a 1 pixel high stripe. That's because an empty wxPanel has a "best size" of (0,0), so the sizer algorithm will distribute the whole available space to the first row. SetMinSize() just assures that it doesn't get shrunk to zero size.

The clipped statusbar text is because of CreateStatusBar(2). This will split the statusbar into 2 areas of equal width, and the right area is empty in your case.
Use the source, Luke!

DaveNadler
Experienced Solver
Experienced Solver
Posts: 51
Joined: Thu Nov 17, 2011 2:13 pm

Re: wxGridBagSizer bug with MaxSize? or I'm confused??

Post by DaveNadler » Tue May 19, 2020 2:02 pm

Thanks as always Doublemax. Got it on the status bar.

However, I still don't get it on SetMaxSize.
If I change the code to use SetMaxSize (change first #if 0 to #if 1),
the code does not work as I expect...

Thanks!
Best Regards, Dave

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGridBagSizer bug with MaxSize? or I'm confused??

Post by doublemax » Wed May 20, 2020 9:46 am

If I change the code to use SetMaxSize (change first #if 0 to #if 1),
the code does not work as I expect...
What exactly do you expect?

As the bottom row is not set to grow, its height will be determined by the minimum size of the cell contents. The maximum size does not play a role in this calculation. Only if you set the row to be growable, it would.
Use the source, Luke!

Post Reply