Any hints on how to implement a custom control so that it gets along with
sizers? What do I need to override in wxControl to get sizing to work
I'm working on a replacement for wxGauge to display sensor readings. It has
some special features like lower and upper bounds for "good" readings.
I had one working that was derived from wxPanel but it broke when I updated
wxWidgets to v3.1.0 tag. The control now always renders as 1 pixel high. I
tried re-basing the class to wxControl and now it fattens to fill the
containing flex grid sizer I'm using.
In my use case, I have a flex grid sizer with 3 columns: sensor name
(wxStaticText), numeric reading (wxTextCtrl), and my custom control to
provide a horizontal bar graph. The custom control should spread across the
width of the dialog that the fgsizer sits within.
My code for setting up the fgsizer looks like this:
wxFlexGridSizer* fgSizer = new wxFlexGridSizer( 0, 3, 0, 0 );
fgSizer->AddGrowableCol( 2 );
fgSizer->SetFlexibleDirection( wxHORIZONTAL );
fgSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_NONE );
for (unsigned i = 0; i < analogInputList.size(); ++i)
sbSizerAnalogInputs->Add( fgSizer, 0, wxEXPAND, 5 );
My code to add the custom control looks like this:
My custom control is derived from wxControl and overrides OnPaint. What
else do I need to override to get it to size properly so that it's the same
height as the adjacent text controls in the fgsizer? It should be neither
zero height nor spread vertically to fill the available parent sizer.
I suppose you must layout your own objects. So there are three handlers you should overwrite because sizers will call them: DoSetSize(), DoGetBestSize() and DoMoveWindow(). Also handle your own paint-event, where you must redraw the control.
Don't forget to call base::handlers (e.g. wxControl::DoSetSize(...) ) in all of them to let wxWidgets do its job.
On Sat, 18 Feb 2017 06:35:06 -0800 Kenneth Porter wrote:
KP> A bit of digging turned up that I needed to override DoGetBestSize to give
KP> the desired fatness.
You should prefer to override DoGetBestClientSize() in the new code, even
if overriding DoGetBestSize() also works.
You probably should also derive from wxWindow, not wxControl which is
supposed to be used for native controls and/or custom controls that have a
native button-like label (yes, it's not really clear, sorry).