- if ( GetMenuBar() )
- {
- // simple case - a normal menu attached to the menubar
- return GetMenuBar();
- }
-
- // we're a popup menu but the trouble is that only the top level popup menu
- // has a pointer to the invoking window, so we must walk up the menu chain
- // if needed
- wxWindow *win = GetInvokingWindow();
- if ( win )
- {
- // we already have it
- return win;
- }
-
- wxMenu *menu = GetParent();
- while ( menu )
- {
- // We are a submenu of a menu of a menubar
- if (menu->GetMenuBar())
- return menu->GetMenuBar();
-
- win = menu->GetInvokingWindow();
- if ( win )
- break;
-
- menu = menu->GetParent();
- }
-
- // we're probably going to crash in the caller anyhow, but try to detect
- // this error as soon as possible
- wxASSERT_MSG( win, _T("menu without any associated window?") );
-
- // also remember it in this menu so that we don't have to search for it the
- // next time
- wxConstCast(this, wxMenu)->m_invokingWindow = win;
-
- return win;