Search code examples
c++wxwidgetssizer

Are sizers a good way to deal with a form?


I am moving an application of mine from an old GUI (where I used fixed sizes) to wxWidgets (where I am trying to learn the basis of sizers).

Inside the application I have several forms with quite complex structure. Nature of data and workflow suggest me to keep all these data together in a single screen. But this screen has to be properly designed be usable.

Here's the old version of the form ...

form with fix layout

... and here's more or less what I could come up with using sizers ...

form with wxSizers

Please note that I could not simply use a wxGridSizer, since some of the widgets span throughout the row, some other get out of the fixed grid etc.

Obviously I could keep working on it to make it less ugly, but I found myself thinking maybe sizers are not the correct tool to do it. To achieve what I want I would need something similar to wxGridSizer with some sort of colspan/rowspan mechanism, and maybe that wouldn't be enough.

So I started thinking to build a "Form Sizer" by myself that could go something like

int w,h;
GetClientSize(&w,&h);
MyFormSizer *formSizer(w,h);
formSizer->SetDimension( widget1, 100, 20 );
formSizer->SetDimension( widget2, 500, 20 );
formSizer->newRow();
formSizer->SetDimension( widget3, 100, 20 );
formSizer->SetDimension( widget4, 250, 20 );
formSizer->SetDimension( widget5, 250, 20 );
formSizer->Layout();

The user would set the relative sizes of every widget, to block the geometry of the form completely. To make this geometry fit the wxPanel when resizing Layout() would calculate an appropriate scaling factor f and then would set all sizes and positions doing something like

widget1->SetSize(CalculatedX, CalculatedY, 100/f, 20/f);
CalculatedX+=100/f;
widget2->SetSize(CalculatedX, CalculatedY, 500/f, 20/f);
CalculatedX+=0;
CalculatedY+=20/f;

and so on (this is just a scratch, take it as the rough idea; there should be mechanisms to avoid making everything too small to fit the text in the widgets and/or to scale the font size accordingly and such stuff).

Does it seem reasonable? How would you face the topic? This choice is going to affect quite a bit of my future developing, so any answer, criticism and comment will be very welcome


Solution

  • wxGridSizer might not fulfil your needs but more flexible classes such as wxGridBagSizer should do what you want. This class allows you to set items to span multiple rows and columns by setting wxGBSpan when calling wxGridBagSizer::Add.