Search code examples
c++togglehidewxwidgetsmenubar

Toggleable MenuBar in wxWidgets (when to hide?)


I would like to make the menubar in my wxWidgets application toggleable in the way for example Firefox and Windows Explorer are doing it. This means when it's hidden and you press the ALT-Key it get's shown. This is relatively easy: i save the menu when it is created in a member variable m_pMenuBarand capture wxEVT_CHAR_HOOK enabling the menu with wxFrame::SetMenuBar(m_pMenuBar) when GetKeyCode() == WXK_ALT.

But now i wonder when to disable the menu again. I know disabling can be done with wxFrame::SetMenuBar(nullptr). Obviously when a menu item is clicked in the corresponding wxEVT_MENU event. But in programs that support such toggleable menus the menu also gets hidden when you press a key (that is not a shortcut) or click somewhere to the screen (and don't hit the menu). It feels like the menu "looses it's focus and disapears". Since i am already capturing wxEVT_CHAR_HOOK, hiding on key press would be possible. The thing with the mouse is more complicated. I tried a timer that checks if mouse buttons are pressed, but that seems rather ugly and doesn't work when you select sub menus.

So my question is: when or on which events should i hide the menu?

Thank you very much! jffmichi


Solution

  • This looks like one of the rare situations when overriding wxApp::FilterEvent() would be appropriate: once the menu bar is shown, set a flag and check for it in your overridden version of this method whenever you get a mouse event. There is still a question of how to detect whether the click occurs over a menu or not, you will probably need to count wxEVT_MENU_{OPEN,CLOSE} and only dismiss the menu bar when no menus are currently opened.

    Finally notice that you shouldn't try to do any of this under OS X where the menu bar can't be hidden at all (but you can switch to the full screen mode).