Question

Currently in my project I have a panel with things laid out in it like this:

  • wxBoxSizer(wxVertical)
    • wxSearchControl(wxExpand)
    • wxListControl (wxExpand)
    • wxBoxSizer(wxHorizontal, wxALIGN_CENTER)
      • wxButton (wxALL)
      • wxButton (wxALL)

The issue is that when the panel is expanded it creates wasted space underneath where the buttons are and the opposite problem also occurs when the panel is contracted, meaning the buttons are removed from sight completely before contracting the wxListControl which is undesirable behaviour. The desired behaviour would be something like: When expanding I want everything to reach it's 'nice size' then the only thing that would continue to expand is the wxListControl, when contracting the wxListControl should contract first until it hits a minimum size and then the buttons and search control should contract. The following pictures should highlight the issues.

**

Wasted space at the bottom: http://i.stack.imgur.com/lsXOv.png

Buttons are mangled: http://i.stack.imgur.com/U50In.png

So here is my code (only edited for the small width, bad naming kept and everything):

lbxEntityList = new wxListCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                               wxLC_REPORT | wxLC_EDIT_LABELS | wxLC_HRULES,
                               wxDefaultValidator, wxT("Entity List Control")); 

lbxEntityList->AppendColumn("Entities",wxLIST_FORMAT_LEFT, 80);

srchControl = new wxSearchCtrl(this, wxID_ANY, "", wxDefaultPosition,
                               wxDefaultSize);

#ifndef __WXMAC__
    srchControl->ShowSearchButton( true );
#endif

srchControl->ShowCancelButton( false );

btn_RemoveEntity = new wxButton( this, wxID_ANY, wxT("Remove Entity"),
                                wxDefaultPosition, wxDefaultSize, 0);

btn_AddEntity = new wxButton(this, wxID_ANY, wxT("Add Entity"),
                             wxDefaultPosition, wxDefaultSize, 0);

sizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* o = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(srchControl, wxSizerFlags().Expand().Border(wxALL, 10));
sizer->Add( lbxEntityList, wxSizerFlags().Expand().Border(wxALL, 10));

sizer->Add(o, 1, wxALIGN_CENTER, 5);
o->Add( btn_RemoveEntity, 0, wxALL, 5 );
o->Add( btn_AddEntity, 0, wxALL, 5 );

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

The only things I have attempted is constantly trying random different sizers in different configurations with different flags in wxFormBuilder in a vain attempt to get it working, but that always ends fruitlessly. I have looked at several sizer tutorials and the only conclusion I get to is that I need to write my own sizer, I am hoping I am missing something and I won't have to do that.

Was it helpful?

Solution

I cross-posted this question to the wx-users mailing list and received this answer:

In a vertical sizer, the EXPAND flag only controls the size of the control in horizontal direction. For the vertical direction, the "proportion" parameter is relevant.

Set a proportion of 0 for the search control and the bottom horizontal box sizer.

Set a proportion of 1 for the list box.

That should give you the behavior you want.

Although the answer doesn't provide the correct contracting behaviour it does provide the correct expanding behaviour which in my case is more important.

OTHER TIPS

Sizers provide a first-pass default behaviour when the window containing them is resized.

However, if you require something other than the default behaviour, you must handle the window resize event yourself, and recalculate the widget sizes.

In your case, you will need to determine the total 'nice size' of the other widgets, subtract this from the new size of the window, and resize the ListCtrl so as to fill the remainder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top