Get wxEvtHandler from event???

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Get wxEvtHandler from event???

Post by Mick P. »

As near as I can tell wxWidgets isn't exposing the event's handler to the event itself. This seems... well odd. Is there a solution? I have to do hack because a bad menu design, and don't want to derive a custom wxFrame class just so I can add a method to bind an event to. The frame is the handler, whereas the "object" is a wxMenu, and not the window itself as usual.

Is there are a global API to get the top of event stack or something? TBH wxWidgets is oftentimes a pain the neck :lol:

I'm mostly curious, since I can do some casting magic.

Code: Select all

void
wxAppConsoleBase::HandleEvent(wxEvtHandler *handler,
                              wxEventFunction func,
                              wxEvent& event) const
{
    // by default, simply call the handler
    (handler->*func)(event);
}

void wxAppConsoleBase::CallEventHandler(wxEvtHandler *handler,
                                        wxEventFunctor& functor,
                                        wxEvent& event) const
{
    // If the functor holds a method then, for backward compatibility, call
    // HandleEvent():
    wxEventFunction eventFunction = functor.GetEvtMethod();

    if ( eventFunction )
        HandleEvent(handler, eventFunction, event);
    else
        functor(handler, event);
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get wxEvtHandler from event???

Post by ONEEYEMAN »

Hi,
Why?
What problem are you trying to solve?

Can you post some (pseudo-)code?

Thank you.
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Get wxEvtHandler from event???

Post by Mick P. »

Just curious, woold like to be able to Bind an event procedure to a wxFrame for example without having to make it a method (this-call) just to get the "this" pointer, when I know that the EvtHandler itself is the wxFrame object.

I can see it's possible to attach multiple handlers to the base object, but if code knows there is one, it seems like there might be a way to get the wxEvtHandler from the event object, or from a global state. Like wxEventLoop is a global stack. I learned that yesterday.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Get wxEvtHandler from event???

Post by evstevemd »

Hi,
Mick P. wrote: Mon Sep 02, 2019 10:44 am Just curious, woold like to be able to Bind an event procedure to a wxFrame for example without having to make it a method (this-call) just to get the "this" pointer, when I know that the EvtHandler itself is the wxFrame object.

I can see it's possible to attach multiple handlers to the base object, but if code knows there is one, it seems like there might be a way to get the wxEvtHandler from the event object, or from a global state. Like wxEventLoop is a global stack. I learned that yesterday.
Am not sure if I understood correctly your question, but if I did then you do something like

Code: Select all

MainFrame *f = new MainFrame(...);
wxCommandEvent e(...);
wxPostEvent(f, e);
//.....

Code: Select all

Bind(wxEVT_MENU, &SomeClass::OnSomeMethod, this);

Code: Select all

void SomeClass::OnSomeMethod(wxCommandEvent& e)
{
	MainFrame f = dynamic_cast<MainFrame*>(e.GetEventObject());
	if(f)
	{
		//call methods of MainFrame here!
	}
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get wxEvtHandler from event???

Post by ONEEYEMAN »

Hi,
I think he wants to do something like this:

Code: Select all

void SomeGenericFunction()
{
}

void MainFrame::MainFrame()
{
// creating controls and lay them out
Bind( EVT_USER, &SomeGenericFunction );
}
No 'this' present.

Thank you.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Get wxEvtHandler from event???

Post by evstevemd »

Hi,
ONEEYEMAN wrote: Wed Sep 04, 2019 7:22 pm Hi,
I think he wants to do something like this:

Code: Select all

void SomeGenericFunction()
{
}

void MainFrame::MainFrame()
{
// creating controls and lay them out
Bind( EVT_USER, &SomeGenericFunction );
}
No 'this' present.

Thank you.
I have not tested but with bind you can achieve that I believe.

Code: Select all

void SomeGenericFunction(wxCommandEvent& e)
{
	//work here
}

void MainFrame::MainFrame()
{
// creating controls and lay them out
Bind( EVT_USER, &SomeGenericFunction );
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get wxEvtHandler from event???

Post by ONEEYEMAN »

One can only guess... ;-)
It is up to the OP at this time to clarify what he wants.

Thank you.
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Get wxEvtHandler from event???

Post by Mick P. »

I think I am clear.

I want to not use "this" but to also use an API to get the wxEvtHandler, which in most cases is equal to "this". That is, I want to use a procedure that is not thiscall, but still be able to get the window pointer (handler) from events that don't assign that to GetEventObject (Many do, but not all do.)
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: Get wxEvtHandler from event???

Post by alys666 »

Mick P. wrote: Sun Sep 01, 2019 7:58 am As near as I can tell wxWidgets isn't exposing the event's handler to the event itself. This seems... well odd.
Event in such event systems is just a piece of data, distributed to a list of registered hooks.
How to understand "an exposing a handler to event" if an event is just a parameter, given to hook.
how to expose a function to its actual parameter, and for what?
why parameter must know to which function it is given?
in this case handler already knows everything about himself, and his <this>.
event is passed to list of handlers, and if execution flow is outside handler body - <this> is undefined, if inside some handler body - handler knows his <this>.
if some handler is registered to "event" - it's just inserted in a list of hooks, which will be polled with some actual data of this event class.
so what do you want - to know which list of handlers is associated with this event? the list is dynamic, handlers can be registered and unregistered in any time.
if you want to know <this> - <this> is defined only inside handler body.
ubuntu 20.04, wxWidgets 3.2.1
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get wxEvtHandler from event???

Post by ONEEYEMAN »

Hi,
Mick P. wrote: Thu Sep 05, 2019 3:47 pm I think I am clear.
No, you are not.
Ever heard of the phrase - "picture worth a thousand words".
In this case - just an MCVE will suffice. Or a pseudo-code. Or maybe, if you are lazy enough, point out whose scenario is more correct - mine or evstevemd.
Mick P. wrote: Thu Sep 05, 2019 3:47 pm I want to not use "this" but to also use an API to get the wxEvtHandler, which in most cases is equal to "this". That is, I want to use a procedure that is not thiscall, but still be able to get the window pointer (handler) from events that don't assign that to GetEventObject (Many do, but not all do.)
This still doesn't explain it.
See above.

Thank you.
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: Get wxEvtHandler from event???

Post by Kvaz1r »

Why it should been stored there? If you have event you are already in handlers. And event can have any amount(none, only one or several) of handlers, it just redundant information at all.

Btw, do you know where it's implemented this way?
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Get wxEvtHandler from event???

Post by Mick P. »

Kvaz1r wrote: Thu Sep 05, 2019 5:53 pm Why it should been stored there? If you have event you are already in handlers. And event can have any amount(none, only one or several) of handlers, it just redundant information at all.

Btw, do you know where it's implemented this way?
In the code I posted in the top post.

It was just an innocent question. If every event requires a handler, I don't know why wxWidgets hides the handler. It's a weird design choice, and I thought that maybe people familiar with wxWidgets would know if that is indeed the case, and if so, if there is a compelling reason to do it like that. EDITED: I suppose the reason, if so, is I guess that a handler is not actually required if the event object is constructed on the stack. But in that case, it would just be a hack to simulate an event in local code, so it doesn't seem like a compelling case.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get wxEvtHandler from event???

Post by ONEEYEMAN »

Hi,
But what problem you are trying to solve?

And just to add to the discussion - not all events can be handled.
Or some events can have multiple event handlers.
Or some events can be handled both in parent and the child classes.

How do you handle all possible scenarios?

Thank you.
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: Get wxEvtHandler from event???

Post by DavidHart »

If every event requires a handler
But it doesn't. An event may be fired, ignored and die of neglect.

However, more important is that an event might have multiple handlers e.g. after multiple Bind()s. If so, it gets handled by the first in the list, then either dies or, if event.Skip() is called, is passed to the next potential handler (often in the base-class) which repeats the process.

IOW, there is no 'this' associated with an event (unless you choose to add it yourself) because it wouldn't necessarily match all/any of the handlers.
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: Get wxEvtHandler from event???

Post by alys666 »

Mick P. wrote: Thu Sep 05, 2019 6:14 pm It was just an innocent question. If every event requires a handler, I don't know why wxWidgets hides the handler. It's a weird design choice, and I thought that maybe people familiar with wxWidgets would know if that is indeed the case, and if so, if there is a compelling reason to do it like that. EDITED: I suppose the reason, if so, is I guess that a handler is not actually required if the event object is constructed on the stack. But in that case, it would just be a hack to simulate an event in local code, so it doesn't seem like a compelling case.
Event does not require a handler. Event is a passive data object, and if somebody wants to consume it - he must register a handler, and number of handlers registered for this event in not limited.
so an event can be sequentially given to all handlers registered to listen it. when event dispatcher calls next handler with this event - you can say that event is exposed to this handler, formally you can say vice versa that a handler is exposed to event, but only if execution flow is inside this handler body.
ubuntu 20.04, wxWidgets 3.2.1
Post Reply