Overloading a dynamic event handler in derived class.

66 views
Skip to first unread message

Stanislav Tihohod

unread,
Oct 23, 2021, 6:17:45 PM10/23/21
to wx-users
I have a wxPanel-derived class that has a child glcanvas for drawing.
I want to process some events of the glcanvas (e.g. wxEVT_SIZE) in the code of the panel.
So in my constructor of the panel class I do:

m_pGLView = new CGLView(this, GLAttr, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
m_pGLView->Bind(wxEVT_SIZE, &CGLViewPanel::OnViewSizeChanged, this);

It works ok.
But now I want to derive several classes from my panel class and process wxEVT_SIZE event differently in these classes.
As I understood from the documentation, making OnViewSizeChanged a virtual function and overloading it in panel-derived classes will not help - because the code that calls handler ignores virtuality of the handler.
So what is the correct way to do what I want? Should I somehow re-bind the handler in the derieved class' constructor? What is the correct way to do this? Unbind the handler set in the panel's base class constructor and bind it again in the derived class? Or just calling wxEvtHandler again is enough? In general, what happens if I try to bind several handlers to the same event emitted from the same window?

Ian McInerney

unread,
Oct 23, 2021, 6:49:20 PM10/23/21
to wx-u...@googlegroups.com
Instead of trying to use the event tables for doing this dispatching to the child classes, you could add a new virtual method to the base class that you then call from your CGLViewPanel::OnViewSizeChanged handler. That virtual function is overridden in the child classes, and the proper overload will be called from the event handler. So you only ever need to bind in the base class and override a single function in the child classes.

-Ian

--
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/51deee76-8eee-4d3b-8f1a-f769e4582b04n%40googlegroups.com.


--
Ian McInerney
mcia...@gmail.com

No electrons were harmed in the making of this message

Vadim Zeitlin

unread,
Oct 23, 2021, 8:14:01 PM10/23/21
to wx-u...@googlegroups.com
On Sat, 23 Oct 2021 15:14:16 -0700 (PDT) Stanislav Tihohod wrote:

ST> But now I want to derive several classes from my panel class and process
ST> wxEVT_SIZE event differently in these classes.
ST> As I understood from the documentation, making OnViewSizeChanged a virtual
ST> function and overloading it in panel-derived classes will not help -
ST> because the code that calls handler ignores virtuality of the handler.

This is actually not true if you're using Bind() (and not the legacy event
table macros), so you could just do this directly and it will work with
overrides in the derived classes.

ST> In general, what happens if I try to bind several handlers to the
ST> same event emitted from the same window?

They will each be called in turn until one of them does _not_ call
event.Skip(). I.e. by default, processing will stop with the first handler,
but if event.Skip() is called, the next one will be called, and if it
doesn't call event.Skip() neither, the next one will be and so on.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Reply all
Reply to author
Forward
0 new messages