+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+// wxDeepCopyMenu
+//
+// Performs a top-to-bottom copy of the input menu and all of its
+// submenus.
+//
+// This is mostly needed for 2.4 compatability. However wxPython and others
+// still use this way of setting the taskbarmenu.
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+wxMenu* wxDeepCopyMenu(wxMenu* menu)
+{
+ if (!menu)
+ return NULL;
+
+ //
+ // NB: Here we have to perform a deep copy of the menu,
+ // copying each and every menu item from menu to m_pMenu.
+ // Other implementations use wxWindow::PopupMenu here,
+ // which idle execution until the user selects something,
+ // but since the mac handles this internally, we can't -
+ // and have no way at all to idle it while the dock menu
+ // is being shown before menu goes out of scope (it may
+ // not be on the heap, and may expire right after this function
+ // is done - we need it to last until the carbon event is triggered -
+ // that's when the user right clicks).
+ //
+ // Also, since there is no equal (assignment) operator
+ // on either wxMenu or wxMenuItem, we have to do all the
+ // dirty work ourselves.
+ //
+
+ // perform a deep copy of the menu
+ wxMenuItemList& theList = menu->GetMenuItems();
+ wxMenuItemList::compatibility_iterator theNode = theList.GetFirst();
+
+ // create the main menu
+ wxMenu* m_pMenu = new wxMenu(menu->GetTitle());
+
+ while (theNode != NULL)
+ {
+ wxMenuItem* theItem = theNode->GetData();
+ m_pMenu->Append(
+ new wxMenuItem(
+ m_pMenu, // parent menu
+ theItem->GetId(), // id
+ theItem->GetText(), // text label
+ theItem->GetHelp(), // status bar help string
+ theItem->GetKind(), // menu flags - checkable, separator, etc.
+ wxDeepCopyMenu(theItem->GetSubMenu()) // submenu
+ ));
+ theNode = theNode->GetNext();
+ }
+
+ return m_pMenu;
+}