Event Handling in submenu's of a wxMenu

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
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Event Handling in submenu's of a wxMenu

Post by joemaniaci »

I have two wxMenuItems that are submenus and when I set them up this way, the second event is not fired.

Code: Select all

	    wxMenu *menuBorders = new wxMenu;
	    menuBorders->Append(ID_FSAVE, wxT("Save as Single &Image"));
	    menuBorders->Append(ID_TSAVE, wxT("Save as &Multi-Page TIFF"));
	    this->AppendSubMenu(menuBorders, wxT("Save &Triview..."));
However if I just attach them to (this) like so...

Code: Select all

	this->Append(ID_FSAVE, wxT("Save as Single &Image"));
	this->Append(ID_TSAVE, wxT("Save as &Multi-Page TIFF"));
The command event is now captured by my event handler. I haven't found anything that mentions if a submenu needs to be managed any differently. My event handler is declared as a EVT_MENU. I'm using 3.0.2 on windows 7. Using Bind() doesn't work either.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Event Handling in submenu's of a wxMenu

Post by ONEEYEMAN »

Hi,
Did you look at the menu sample to see how to handle the menu creation/processing?

Thank you.
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Event Handling in submenu's of a wxMenu

Post by joemaniaci »

In the 'Standard items demo' submenu, is it supposed to just print the ID when you click on a menuitem such as Add or are Dialog windows supposed to pop up as well? Because I am not seeing anything pop up from that particular menu item. Quit works, but Add or Print Preview have no dialogs popup. I guess it doesn't matter since they showed the events being caught.

I compiled using mingw32-make -f makefile.gcc SHARED=1 UNICODE=1 BUILD=debug CXXFLAGS="-std=gnu++11"
without issues.

Syntactically I don't see them doing anything different other than capturing the events to log them before skipping them.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Event Handling in submenu's of a wxMenu

Post by ONEEYEMAN »

Hi,
Are you trying to create your own handler for the menu or you are using the standard one?
Are you using event table or dynamic binding?
Could you please copy'n'paste the event binding code (either how do you call Bind() or the event table macros)?

Also I presume in the code you posted 'this' is referencing wxMenuBar/wxMenu, right?
And finally its a little suspicious that you said "second event is not fired". What about the first one? Are you talking about first clicking the menu and clicking it second time? Or you are referencing "ID_FSAVE" and "ID_ISAVE"?

Thank you.
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Event Handling in submenu's of a wxMenu

Post by joemaniaci »

ONEEYEMAN wrote:Hi,
Are you trying to create your own handler for the menu or you are using the standard one?
Are you using event table or dynamic binding?
Could you please copy'n'paste the event binding code (either how do you call Bind() or the event table macros)?

Also I presume in the code you posted 'this' is referencing wxMenuBar/wxMenu, right?
And finally its a little suspicious that you said "second event is not fired". What about the first one? Are you talking about first clicking the menu and clicking it second time? Or you are referencing "ID_FSAVE" and "ID_ISAVE"?

Thank you.
I've tried

Code: Select all

EVT_MENU( ID_TSAVE, FileMenu::OnCompareSave )
EVT_MENU( ID_TSAVEM, FileMenu::OnCompareSave )

and

Bind(wxEVT_MENU, &FileMenu::OnCompareSave , this, ID_TSAVE );
Bind(wxEVT_MENU, &FileMenu::OnCompareSave , this, ID_TSAVEM );
My handler has signature

Code: Select all

void FileMenu::OnCompareSave(wxCommandEvent& evt)
My this is a wxMenu and yeh, it's the second item in the submenu that isn't firing. Actually I've been tweaking them so the enum values have changed, but now neither item works unless I append them to (this) instead of a new wxMenu*. I think I actually had the first one set to the default menu macro instead of ID_TSAVE when it was working earlier, but now I can't remember what it is.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Event Handling in submenu's of a wxMenu

Post by ONEEYEMAN »

Hi,

Code: Select all

EVT_MENU( ID_TSAVE, FileMenu::OnCompareSave )
EVT_MENU( ID_TSAVEM, FileMenu::OnCompareSave )
and in your original post:

Code: Select all

       wxMenu *menuBorders = new wxMenu;
       menuBorders->Append(ID_FSAVE, wxT("Save as Single &Image"));
       menuBorders->Append(ID_TSAVE, wxT("Save as &Multi-Page TIFF"));
       this->AppendSubMenu(menuBorders, wxT("Save &Triview..."));
Do you see the difference?

Thank you.
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Event Handling in submenu's of a wxMenu

Post by joemaniaci »

Yeh, I've been tweaking things since I posted the initial issue, so names have changed, but having the same issue. I use the same event handler now for both and use evt.GetID() to set the different behavior for both menuItems. But I still have the same issue of them working when Appended to (this), instead of the wxMenu* that gets appended to (this) as a submenu

Code: Select all

this->Append( ID_TSAVE, wxT("Save as Single &Image"));
this->Append( ID_TSAVEM, wxT("Save as &Multi-Page TIFF"));
Fires the events

Code: Select all

wxMenu *menuBorders = new wxMenu;
menuBorders->Append( ID_TSAVE, wxT("Save as Single &Image"));
menuBorders->Append( ID_TSAVEM, wxT("Save as &Multi-Page TIFF"));
this->AppendSubMenu(menuBorders, wxT("Save &Triview..."));
This does not.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Event Handling in submenu's of a wxMenu

Post by doublemax »

I haven't researched the sources, but i think menu events are usually sent directly to the wxFrame. The fact that you can catch some events in a class derived from wxMenuBar is just a lucky coincidence ;)
Use the source, Luke!
joemaniaci
Experienced Solver
Experienced Solver
Posts: 52
Joined: Thu Dec 08, 2016 2:42 pm

Re: Event Handling in submenu's of a wxMenu

Post by joemaniaci »

doublemax wrote:I haven't researched the sources, but i think menu events are usually sent directly to the wxFrame. The fact that you can catch some events in a class derived from wxMenuBar is just a lucky coincidence ;)
I'm starting to feel like my attempt to encapsulate every wxMenu into their own class, that gets appended to my wxMenuBar is just a terrible idea.

EDIT: So is the solution to build and handle anything wxMenuBar related in the wxFrame derived class?

It seems to be the one exception on the event page of the wxwidgets wiki,
A wxMenuBar doesn't send events to itself, but to its parent frame. This might seem inconsistent, but it's actually convenient in all common situations.
Is it potentially a bug that some of my items didn't send events to the parent frame? Even when I explicitly skipped the events that were working, just to see if they got processed by my wxFrame, the wxFrame wasn't picking them up.
Post Reply