Event when click on menu

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
Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Event when click on menu

Post by Wanderer82 » Sun Jul 25, 2021 9:33 pm

Hi again

Is it possible somehow to write an event for when a menu is clicked? I mean the "top level menu" as "File" for example. So I want to open a dialog when a specific menu is clicked.

Thanks,
Thomas

User avatar
doublemax
Moderator
Moderator
Posts: 16397
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Event when click on menu

Post by doublemax » Sun Jul 25, 2021 9:48 pm

Please don't do this. When users click on items in the menubar, they expect a menu to open in which they can see what commands / actions are available. Immediately performing an action like opening a dialog would be quite confusing.

If you want those "direct to action" buttons, use a toolbar.
Use the source, Luke!

Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Re: Event when click on menu

Post by Wanderer82 » Mon Jul 26, 2021 8:57 am

Hm, somwhere I've already read that actually you shouldn't do this. But I'd like to have an item "Search" as a menu where a little search dialog pops up. I would find it nice to have it directly in the menu as to not overload my window with buttons. And it would be cool to know how to do it just for the sake of it.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5313
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Event when click on menu

Post by ONEEYEMAN » Mon Jul 26, 2021 12:17 pm

Hi,
You better have submenus created under the menu bar and use them to handle user action.
Also I'm not sure I understand your comment about "overloading window with buttons". Could you please explain?

Thank you.

User avatar
doublemax@work
Earned some good credits
Earned some good credits
Posts: 131
Joined: Wed Jul 29, 2020 6:06 pm

Re: Event when click on menu

Post by doublemax@work » Mon Jul 26, 2021 2:03 pm

Wanderer82 wrote:
Mon Jul 26, 2021 8:57 am
Hm, somwhere I've already read that actually you shouldn't do this. But I'd like to have an item "Search" as a menu where a little search dialog pops up. I would find it nice to have it directly in the menu as to not overload my window with buttons. And it would be cool to know how to do it just for the sake of it.
Try wxEVT_MENU_OPEN event.
https://docs.wxwidgets.org/trunk/classw ... event.html

Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Re: Event when click on menu

Post by Wanderer82 » Wed Jul 28, 2021 1:42 pm

ONEEYEMAN wrote:
Mon Jul 26, 2021 12:17 pm
Hi,
You better have submenus created under the menu bar and use them to handle user action.
Also I'm not sure I understand your comment about "overloading window with buttons". Could you please explain?

Thank you.
I mean that my program window is already "packed" with things so that I don't want to have another button or so for the "Search" part. I didn't mean "overload" in the programmatical sense ;-)

Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Re: Event when click on menu

Post by Wanderer82 » Sat Jul 31, 2021 11:15 am

Okay, I managed to get it to work using EVT_MENU_OPEN, but now my function is called with every menu I click. Somehow I have to link the specific menu to the function. How can I do this? I tried using event.GetMenu() but I don't know how to use the return value for an if-statement because the return value is of wxMenu type. Could someone pleas help me with this?

User avatar
doublemax
Moderator
Moderator
Posts: 16397
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Event when click on menu

Post by doublemax » Sat Jul 31, 2021 11:47 am

You have the pointer to the menu when you create it and add it to the menubar. Store it in a member variable, so you can use it for the if-check later.
Use the source, Luke!

Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Re: Event when click on menu

Post by Wanderer82 » Sat Jul 31, 2021 12:57 pm

Hm, so the first problem is, I don't know how to store the pointer in a member variable. I don't know of what kind this variable has to be.

And the second question: Is this the only way to do it? I mean, do I really have to generally use the EVT_OPEN_MENU on all menus and then put an if-check? Can't I just write en event only for this specific menu?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5313
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Event when click on menu

Post by ONEEYEMAN » Sat Jul 31, 2021 1:23 pm

Hi,
Can you show the code on how you bind the event?

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 16397
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Event when click on menu

Post by doublemax » Sat Jul 31, 2021 3:57 pm

Wanderer82 wrote:
Sat Jul 31, 2021 12:57 pm
Hm, so the first problem is, I don't know how to store the pointer in a member variable. I don't know of what kind this variable has to be.
wxMenu*
Wanderer82 wrote:
Sat Jul 31, 2021 12:57 pm
And the second question: Is this the only way to do it? I mean, do I really have to generally use the EVT_OPEN_MENU on all menus and then put an if-check? Can't I just write en event only for this specific menu?
No. Menu events are sent directly to the toplevel window, you can't Bind() an event handler to a specfic menu or menu item.
Use the source, Luke!

Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Re: Event when click on menu

Post by Wanderer82 » Sat Jul 31, 2021 5:16 pm

Okay, that's a pity. Well, what values will the type "wxMenu*" return? I mean, I have to store the value somewhere in a variable and compare it, so I have to know what value can be expected to be able to compare it against something.

So I have to create a variable of typ wxMenu* and assign it the value of what exactly? Sorry, I have to admit that these things are still like a little secret to me :roll:

User avatar
doublemax
Moderator
Moderator
Posts: 16397
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Event when click on menu

Post by doublemax » Sat Jul 31, 2021 5:35 pm

Wanderer82 wrote:
Sat Jul 31, 2021 5:16 pm
Okay, that's a pity. Well, what values will the type "wxMenu*" return? I mean, I have to store the value somewhere in a variable and compare it, so I have to know what value can be expected to be able to compare it against something.

So I have to create a variable of typ wxMenu* and assign it the value of what exactly? Sorry, I have to admit that these things are still like a little secret to me :roll:
Somewhere in your code you must have something like this (taken from 'minimal' sample):

Code: Select all

wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
"helpMenu" is the one you need. You add a member variable "wxMenu *helpMenu" to the MyFrame class and change the above code to:

Code: Select all

helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");
(Usually you would rename the variable to something like m_helpMenu to indicate that it's a member variable).

Then, in the wxEVT_MENU_OPEN menu handler:

Code: Select all

void MyFrame::OnMenuOpen(wxMenuEvent &event)
{
    if( event.GetMenu() == helpMenu )
    {
        wxLogDebug("help menu opened");
    }
}
Beware that opening a menu usually blocks the GUI, so the whole thing may not work as you intended.
Use the source, Luke!

Wanderer82
I live to help wx-kind
I live to help wx-kind
Posts: 166
Joined: Tue Jul 26, 2016 2:00 pm

Re: Event when click on menu

Post by Wanderer82 » Sat Jul 31, 2021 10:24 pm

Hm, I actually didn't have to add a member variable. Everything was already in place but not at the place from your "minimal sample". The variable was at the beginning inside the "wxWindow part". I only had to write the if-statement, the way you showed me. Plus, I had to take the already existing variable "wxMenu *Menu3" out of its "normal" position (inside the wxWindow part) as it couldn't find the variable (not declared in this scope). Why are these variables not declared in the .h-file as for example the menu items?

Also I noticed a behavior that I didn't necessarily want: If I click on "File" and open this menu and then move the mouse over the next menu "Search", the search dialog is shown as well. Is there a way to make the dialog only show up if the user also clicks on the menu?

User avatar
doublemax
Moderator
Moderator
Posts: 16397
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Event when click on menu

Post by doublemax » Sun Aug 01, 2021 7:53 am

Wanderer82 wrote:
Sat Jul 31, 2021 10:24 pm
Also I noticed a behavior that I didn't necessarily want: If I click on "File" and open this menu and then move the mouse over the next menu "Search", the search dialog is shown as well. Is there a way to make the dialog only show up if the user also clicks on the menu?
I can't think of any way to prevent this as we can't detect the mouse click and the wxEVT_MENU_OPEN event is the only information we have.
Use the source, Luke!

Post Reply