What does OnUpdateUI event handler do in anitest sample?

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
geefawkes
In need of some credit
In need of some credit
Posts: 3
Joined: Tue May 11, 2021 1:20 am

What does OnUpdateUI event handler do in anitest sample?

Post by geefawkes »

I'm going through the samples provided in wxWidgets and I came across the animation sample. I can make sense of the majority of the code. However, I cannot seem to understand what this portion of the code does.

Code: Select all

void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event))
{
    GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
    GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
    GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE)->Enable(!m_animationCtrl->IsPlaying());
}
I've tried commenting out all three lines in the code, but the app seems to run without an issue. I've left the binding line

Code: Select all

EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI)
intact while experimenting.

What does this particular event handler do and how is it related to the animation?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What does OnUpdateUI event handler do in anitest sample?

Post by doublemax »

The use of wxUpdateUIEvent is not related to animation, it can be used in many situations.

When ever the state of m_animationCtrl changes, you also want to change some gui elemtents (enable/disable buttons, check menu items, etc). Instead of calling a dedicated function each time you change the m_animationCtrl, you can do it in an wxUpdateUIEvent handler.

Personally, i'm not a big fan of wxUpdateUIEvent, it feels like a "brute-force-attack" to update your GUI. And if you have many controls, it can slow down your app significantly.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: What does OnUpdateUI event handler do in anitest sample?

Post by ONEEYEMAN »

Hi,
The other scenario is - when your application is in some particular state and you know the access to some menu should be disabled at this point but other menus should be enabled.

The best way to achieve it is thru the wxUpdateUI event.

However they can slow down the application, like doublemax said, since they are sent during the IDLE time.

Thank you.
geefawkes
In need of some credit
In need of some credit
Posts: 3
Joined: Tue May 11, 2021 1:20 am

Re: What does OnUpdateUI event handler do in anitest sample?

Post by geefawkes »

Thanks for the reply, guys!
doublemax wrote: Tue May 11, 2021 5:19 am When ever the state of m_animationCtrl changes, you also want to change some gui elemtents
What would cause the state to change? How can I change the state so I can see the wxUpdateUIEvent getting called and my event handler doing its thing (for laughs and giggles)?

Cool! wxUpdateUIEvent bad - use at your own peril to slow down your application. Gotcha!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: What does OnUpdateUI event handler do in anitest sample?

Post by ONEEYEMAN »

Hi,
It is not necessary a bad thing as in my example.
You have some other means to disable the menus, but it is more cumbersome and using update ui events here will simplify the code significantly.

The point is - those events are sent during the IDLE time of the application and, depending on what is happening with the system it may slow down the application.

Thank you.
geefawkes
In need of some credit
In need of some credit
Posts: 3
Joined: Tue May 11, 2021 1:20 am

Re: What does OnUpdateUI event handler do in anitest sample?

Post by geefawkes »

Hey guys,

I found a simple use case of wxUpdateUIEvent in one of the samples that provided me with a better understanding of how it works. I thought I'd share it for anyone else who is also getting started.

In the widgets sample, the activityindicator.cpp file makes use of wxUpdateUIEvent to update the text in wxStaticText field when the "Start" or the "Stop" button is pressed. The code involving the wxUpdateUIEvent is as follows:

void OnUpdateIsRunning(wxUpdateUIEvent& event)
{
event.SetText(m_indicator && m_indicator->IsRunning()
? "Indicator is running"
: "Indicator is stopped");
}
Here are some sshots of the app as the text changes.
Image
Image

You can find the sample by running the project found in [wxwidgets_dir]/samples/widgets

There are still some things that I don't understand about the code that I'll try to figure out as I go through more samples. For instance,
1. How is the OnUpdateIsRunning triggered? I don't see anything explicitly linking the buttons to this event. So, how does the event know it should get triggered on button press?
2. The SetText() method is invoked on event argument even though the static text field doesn't have a handle, but still manages to update the text. I find it interesting as I would have taken a different approach (but I find this approach prettier).
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What does OnUpdateUI event handler do in anitest sample?

Post by doublemax »

geefawkes wrote: Wed May 19, 2021 3:03 am 1. How is the OnUpdateIsRunning triggered? I don't see anything explicitly linking the buttons to this event. So, how does the event know it should get triggered on button press?
wxUpdateUIEvents are triggered at idle time, i.e. when ever any other event is triggered.
geefawkes wrote: Wed May 19, 2021 3:03 am 2. The SetText() method is invoked on event argument even though the static text field doesn't have a handle, but still manages to update the text. I find it interesting as I would have taken a different approach (but I find this approach prettier).
That's part of the wxUpdateUIEvent magic ;)
Use the source, Luke!
Post Reply