Hi eranif.
Wonder if you could include the following code in a future release. It searches a menu for items based on name. comes in handy if you dynamically create menus from XML or other input and you want to avoid double creation of the same menu item.
Code to be added to header, flat_menu.h
Code: Select all
/*******************************************************/
protected:
virtual int FindMenuItemPos(const wxString & sLabel, wxFlatMenu **pMenu, bool bRecursive) const
public:
/**
* \brief Finds the first menu item object associated with the given name/label and, optionally, the (sub)menu it belongs to.
* \param sLabel menu item label
* \param pMenu submenu
* \param bRecursive search submenus recursively
* \return menu item
*/
wxFlatMenuItem * FindItem(const wxString & sLabel, wxFlatMenu **pMenu = NULL, bool bRecursive = true) const;
In the cpp file
Code: Select all
/*******************************************************/
wxFlatMenuItem * wxFlatMenu::FindItem(const wxString & sLabel, wxFlatMenu **pMenu, bool bRecursive ) const
{
wxFlatMenuItem * pRet = 0L;
if( pMenu != 0L )
{
int nIdx = FindMenuItemPos(sLabel, pMenu, bRecursive);
if( wxNOT_FOUND != nIdx )
pRet = (*pMenu)->m_itemsArr[nIdx];
}
else
{
wxFlatMenu *pParentMenu = 0L;
int nIdx = FindMenuItemPos(sLabel, &pParentMenu, bRecursive);
if( wxNOT_FOUND != nIdx )
pRet = pParentMenu->m_itemsArr[nIdx];
}
return pRet;
}
int wxFlatMenu::FindMenuItemPos(const wxString & sLabel, wxFlatMenu **pMenu, bool bRecursive) const
{
wxASSERT_MSG(pMenu, wxT("menu is NULL"));
*pMenu = 0L;
wxFlatMenuItem *pItem = 0L;
int i = 0;
int nIdx = wxNOT_FOUND;
for (; i < (int)m_itemsArr.size(); i++ )
{
pItem = m_itemsArr[i];
if ( pItem->GetLabel().Cmp(sLabel) == 0 )
{
*pMenu = (wxFlatMenu *)this;
nIdx = i;
break;
}
else if ( pItem->IsSubMenu() && bRecursive)
{
nIdx = pItem->GetSubMenu()->FindMenuItemPos(sLabel, pMenu, bRecursive);
if( wxNOT_FOUND != nIdx )
break;
}
else
{
pItem = 0L;
}
}
return nIdx;
}
It's similar to the wxMenu FindItem() methods, just that you can specify if you want to recurse into submenus or not.
Regards,
Robert