#include "wx/intl.h"
#include "wx/log.h"
#include "wx/menu.h"
+ #include "wx/frame.h"
#endif
#include "wx/stockitem.h"
wxItemKind kind,
wxMenu *subMenu)
{
+ switch ( id )
+ {
+ case wxID_ANY:
+ m_id = wxWindow::NewControlId();
+ break;
+
+ case wxID_SEPARATOR:
+ m_id = wxID_SEPARATOR;
+
+ // there is a lot of existing code just doing Append(wxID_SEPARATOR)
+ // and it makes sense to omit the following optional parameters,
+ // including the kind one which doesn't default to wxITEM_SEPARATOR,
+ // of course, so override it here
+ kind = wxITEM_SEPARATOR;
+ break;
+
+ case wxID_NONE:
+ // (popup) menu titles in wxMSW use this ID to indicate that
+ // it's not a real menu item, so we don't want the check below to
+ // apply to it
+ m_id = id;
+ break;
+
+ default:
+ // ids are limited to 16 bits under MSW so portable code shouldn't
+ // use ids outside of this range (negative ids generated by wx are
+ // fine though)
+ wxASSERT_MSG( (id >= 0 && id < SHRT_MAX) ||
+ (id >= wxID_AUTO_LOWEST && id <= wxID_AUTO_HIGHEST),
+ wxS("invalid id value") );
+ m_id = id;
+ }
+
// notice that parentMenu can be NULL: the item can be attached to the menu
// later with SetMenu()
m_subMenu = subMenu;
m_isEnabled = true;
m_isChecked = false;
- m_id = id;
m_kind = kind;
- if (m_id == wxID_ANY)
- m_id = wxWindow::NewControlId();
- if (m_id == wxID_SEPARATOR)
- m_kind = wxITEM_SEPARATOR;
SetItemLabel(text);
SetHelp(help);
bool processed = false;
- // Try the menu's event handler
- // if ( !processed )
- {
- wxEvtHandler *handler = GetEventHandler();
- if ( handler )
- processed = handler->SafelyProcessEvent(event);
- }
+ // Try the menu's event handler first
+ wxEvtHandler *handler = GetEventHandler();
+ if ( handler )
+ processed = handler->SafelyProcessEvent(event);
- // Try the window the menu was popped up from (and up through the
- // hierarchy)
+ // Try the window the menu was popped up from or its menu bar belongs to
if ( !processed )
{
- const wxMenuBase *menu = this;
- while ( menu )
- {
- wxWindow *win = menu->GetInvokingWindow();
- if ( win )
- {
- processed = win->HandleWindowEvent(event);
- break;
- }
-
- menu = menu->GetParent();
- }
+ wxWindow * const win = GetWindow();
+ if ( win )
+ processed = win->HandleWindowEvent(event);
}
return processed;
m_menuBar = NULL;
}
+// ----------------------------------------------------------------------------
+// wxMenu invoking window handling
+// ----------------------------------------------------------------------------
+
+void wxMenuBase::SetInvokingWindow(wxWindow *win)
+{
+ wxASSERT_MSG( !GetParent(),
+ "should only be called for top level popup menus" );
+ wxASSERT_MSG( !IsAttached(),
+ "menus attached to menu bar can't have invoking window" );
+
+ m_invokingWindow = win;
+}
+
+wxWindow *wxMenuBase::GetWindow() const
+{
+ // only the top level menus have non-NULL invoking window or a pointer to
+ // the menu bar so recurse upwards until we find it
+ const wxMenuBase *menu = this;
+ while ( menu->GetParent() )
+ {
+ menu = menu->GetParent();
+ }
+
+ return menu->GetMenuBar() ? menu->GetMenuBar()->GetFrame()
+ : menu->GetInvokingWindow();
+}
+
// ----------------------------------------------------------------------------
// wxMenu functions forwarded to wxMenuItem
// ----------------------------------------------------------------------------
return node->GetData();
}
-bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title))
+bool wxMenuBarBase::Append(wxMenu *menu, const wxString& title)
{
wxCHECK_MSG( menu, false, wxT("can't append NULL menu") );
+ wxCHECK_MSG( !title.empty(), false, wxT("can't append menu with empty title") );
m_menus.Append(menu);
menu->Attach(this);