Manually calling an Event Handler Function? Topic is solved

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.
Post Reply
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Manually calling an Event Handler Function?

Post by tuk1 »

I want to reuse the code within an event handler function and wondering the best way to go..

Code: Select all

EVT_BUTTON( ID_BUTTON, MAIN_FRAME::OnButtonClick )

Code: Select all

void MAIN_FRAME::OnButtonClick( wxCommandEvent& WXUNUSED( event ) )
{
              >>>>> Reusuable code
}

Call the function via wx event handler system?
this->GetEventHandler()->AddPendingEvent( wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, ID_BUTTON ) );


or

Call the function directly?
this->OnButtonClick( wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, ID_BUTTON ) );

Can you say the reason why with your answer.

Thanks!
Last edited by tuk1 on Mon Jun 04, 2018 5:13 pm, edited 3 times in total.
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Manually calling an Event Handler Function?

Post by PB »

The usual way is having a separate function that can be called from anywhere including an event handler. Calling event handler directly may not be the best choice. I.e.

Code: Select all


void MAIN_FRAME::DoSomething()
{
...
}


void MAIN_FRAME::OnButtonClick( wxCommandEvent& WXUNUSED( event ) )
{
	DoSomething();
}

tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: Manually calling an Event Handler Function?

Post by tuk1 »

I was hoping to avoid adding any extra functions.

What is the problem with adding/creating the event manually or calling the handler function directly as shown in the OP?
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Manually calling an Event Handler Function?

Post by coderrc »

for multi-thread signaling I use QueueEvent so that the UI functions get called from the UI thread.

If its all on the same thread, then I will either call the function directly with a dummy parameter, or do what PB suggests. For me the choice depends on whether or not the code in question does any work with UI elements. If it does UI work, I dont want to invite the possibility of invoking a UI function from a non UI thread. I also prefer to separate GUI logic from non-gui logic as much as possible.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Manually calling an Event Handler Function?

Post by ONEEYEMAN »

Hi,
Why you want to avoid a function? This is a normal C++ (and C for that matter) practice...
Besides, this is re-usable code. You might just call it from somewhere else - a place which doesn't need to do an event and doesn't care about an event.

Thank you.
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: Manually calling an Event Handler Function?

Post by tuk1 »

Lets say we have an event handler function which clears the Model data when a user hits the Clear Button:

Code: Select all

void MAIN_FRAME::OnButtonClick_ClearModelData( wxCommandEvent& WXUNUSED( event ) )
{
	//---------------- | CLEAR LOG DATA |---------------- 

	........................
	........................
	Clear Model Data
}
And, we have 3 other event handler functions, which react to different events and do different stuff, but also want to clear the Model Data as part of their work.

Code: Select all

void MAIN_FRAME::OnSomeEvent1( wxCommandEvent& WXUNUSED( event ) )
{
	//---------------- | CLEAR LOG DATA |---------------- 

	........................
	........................
	Clear Model Data
}
void MAIN_FRAME::OnSomeEvent2( wxCommandEvent& WXUNUSED( event ) )
void MAIN_FRAME::OnSomeEvent3( wxCommandEvent& WXUNUSED( event ) )
---

Yes, we could have a separate, non-handler function which does the work of actually clearing the Model data, this function can then be called by any event handler functions or any other UI functions that need to clear the Model data:

Code: Select all

void MAIN_FRAME::Clear_Model( )
My aversion to this design is because it essentially duplicates the purpose of OnButtonClick_ClearModelData(), implementing 2 functions which do the same task, I'm not convinced this is good coding practice, adding proxy functions makes the source page longer and more complicated than it has to be.

....if we already have a function that does the task we need, then why not just call that function, thus re-using existing code instead of adding unnecessary complexity? The handler function can be called directly or via the event handler system by manually pushing an event.

If we're going to add complexity to our code we should have a good reason for doing so, right?

Is this essentially a style issue or is there a technical reason for going one way or the other?
Last edited by tuk1 on Tue Jun 05, 2018 9:25 am, edited 1 time in total.
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Manually calling an Event Handler Function?

Post by PB »

The purpose of an event handler is to handle a specific event. You generally do not call such a function to do other things, it would imply bad code design. In contrast, you use event handlers to call a function that performs a task and which can be called from multiple places.
tuk1
Earned some good credits
Earned some good credits
Posts: 114
Joined: Sun Oct 08, 2017 9:36 am

Re: Manually calling an Event Handler Function?

Post by tuk1 »

PB wrote:The purpose of an event handler is to handle a specific event. You generally do not call such a function to do other things, it would imply bad code design. In contrast, you use event handlers to call a function that performs a task and which can be called from multiple places.
Ok, I'm sold..
wxWidgets(v3.2.2.1) - Vs2022(v143) - Win10(x64) - DialogBlocks(v5.16.5_Unicode)
Post Reply