+// MacOS needs to know about submenus somewhere within this menu
+// before it can be displayed, also hide special menu items
+// like preferences that are handled by the OS
+void wxMenu::MacBeforeDisplay( bool isSubMenu )
+{
+ wxMenuItem* previousItem = NULL ;
+ size_t pos ;
+ wxMenuItemList::compatibility_iterator node;
+ wxMenuItem *item;
+
+ for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++)
+ {
+ item = (wxMenuItem *)node->GetData();
+ wxMenu* subMenu = item->GetSubMenu() ;
+ if (subMenu)
+ {
+ subMenu->MacBeforeDisplay( true ) ;
+ }
+ else // normal item
+ {
+ // what we do here is to hide the special items which are
+ // shown in the application menu anyhow -- it doesn't make
+ // sense to show them in their normal place as well
+ if ( item->GetId() == wxApp::s_macAboutMenuItemId ||
+ item->GetId() == wxApp::s_macPreferencesMenuItemId ||
+ item->GetId() == wxApp::s_macExitMenuItemId )
+
+ {
+ ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ),
+ pos + 1, kMenuItemAttrHidden, 0 );
+
+ // also check for a separator which was used just to
+ // separate this item from the others, so don't leave
+ // separator at the menu start or end nor 2 consecutive
+ // separators
+ wxMenuItemList::compatibility_iterator nextNode = node->GetNext();
+ wxMenuItem *next = nextNode ? nextNode->GetData() : NULL;
+
+ size_t posSeptoHide;
+ if ( !previousItem && next && next->IsSeparator() )
+ {
+ // next (i.e. second as we must be first) item is
+ // the separator to hide
+ wxASSERT_MSG( pos == 0, _T("should be the menu start") );
+ posSeptoHide = 2;
+ }
+ else if ( GetMenuItems().GetCount() == pos + 1 &&
+ previousItem != NULL &&
+ previousItem->IsSeparator() )
+ {
+ // prev item is a trailing separator we want to hide
+ posSeptoHide = pos;
+ }
+ else if ( previousItem && previousItem->IsSeparator() &&
+ next && next->IsSeparator() )
+ {
+ // two consecutive separators, this is one too many
+ posSeptoHide = pos;
+ }
+ else // no separators to hide
+ {
+ posSeptoHide = 0;
+ }
+
+ if ( posSeptoHide )
+ {
+ // hide the separator as well
+ ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ),
+ posSeptoHide,
+ kMenuItemAttrHidden,
+ 0 );
+ }
+ }
+ }
+
+ previousItem = item ;
+ }
+
+ if ( isSubMenu )
+ ::InsertMenu(MAC_WXHMENU( GetHMenu()), -1);
+}
+
+// undo all changes from the MacBeforeDisplay call
+void wxMenu::MacAfterDisplay( bool isSubMenu )
+{
+ if ( isSubMenu )
+ ::DeleteMenu(MacGetMenuId());
+
+ wxMenuItemList::compatibility_iterator node;
+ wxMenuItem *item;
+
+ for (node = GetMenuItems().GetFirst(); node; node = node->GetNext())
+ {
+ item = (wxMenuItem *)node->GetData();
+ wxMenu* subMenu = item->GetSubMenu() ;
+ if (subMenu)
+ {
+ subMenu->MacAfterDisplay( true ) ;
+ }
+ else
+ {
+ // no need to undo hidings
+ }
+ }
+}
+
+wxInt32 wxMenu::MacHandleCommandProcess( wxMenuItem* item, int id, wxWindow* targetWindow )
+{
+ OSStatus result = eventNotHandledErr ;
+ if (item->IsCheckable())
+ item->Check( !item->IsChecked() ) ;
+
+ if ( SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) )
+ result = noErr ;
+ else
+ {
+ if ( targetWindow != NULL )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED , id);
+ event.SetEventObject(targetWindow);
+ event.SetInt(item->IsCheckable() ? item->IsChecked() : -1);
+
+ if ( targetWindow->HandleWindowEvent(event) )
+ result = noErr ;
+ }
+ }
+ return result;
+}
+
+wxInt32 wxMenu::MacHandleCommandUpdateStatus(wxMenuItem* WXUNUSED(item),
+ int id,
+ wxWindow* targetWindow)
+{
+ OSStatus result = eventNotHandledErr ;
+ wxUpdateUIEvent event(id);
+ event.SetEventObject( this );
+
+ bool processed = false;
+
+ // Try the menu's event handler
+ {
+ wxEvtHandler *handler = GetEventHandler();
+ if ( handler )
+ processed = handler->ProcessEvent(event);
+ }
+
+ // Try the window the menu was popped up from
+ // (and up through the hierarchy)
+ if ( !processed )
+ {
+ const wxMenuBase *menu = this;
+ while ( menu )
+ {
+ wxWindow *win = menu->GetInvokingWindow();
+ if ( win )
+ {
+ processed = win->HandleWindowEvent(event);
+ break;
+ }
+
+ menu = menu->GetParent();
+ }
+ }
+
+ if ( !processed && targetWindow != NULL)
+ {
+ processed = targetWindow->HandleWindowEvent(event);
+ }
+
+ if ( processed )
+ {
+ // if anything changed, update the changed attribute
+ if (event.GetSetText())
+ SetLabel(id, event.GetText());
+ if (event.GetSetChecked())
+ Check(id, event.GetChecked());
+ if (event.GetSetEnabled())
+ Enable(id, event.GetEnabled());
+
+ result = noErr ;
+ }
+ return result;
+}
+