Custom control and sizers

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom control and sizers

Kenneth Porter
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
"nicely"?

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)
         addAnalogInputControl(fgSizer, analogInputList[i]);
      sbSizerAnalogInputs->Add( fgSizer, 0, wxEXPAND, 5 );

My code to add the custom control looks like this:

   GoNoGoGauge* gauge = new Gauge( params,
sbSizerAnalogInputs->GetStaticBox(), wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxGA_HORIZONTAL );
   fgSizer->Add( gauge, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxEXPAND, 5 );

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.


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: Custom control and sizers

Kenneth Porter
A bit of digging turned up that I needed to override DoGetBestSize to give
the desired fatness. I copied the implementation from wxGauge.

<http://stackoverflow.com/questions/1560383/wxwidgets-custom-control-size-cannot-be-set-what-am-i-missing-out>

Layout guideline web link in src/msw/gauge.cpp says page has moved, now
here. Same advice, to make it 8 dialog units thick.

<https://msdn.microsoft.com/en-us/library/dn742486.aspx>

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re: Custom control and sizers

Manolo
In reply to this post by Kenneth Porter
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.

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|

Re[2]: Custom control and sizers

Vadim Zeitlin-4
In reply to this post by Kenneth Porter
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).

 Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/

attachment0 (203 bytes) Download Attachment