Find a path name for the id in the given menu, but only if it, and each sub-menu above it, is uniquely named among peer items
| 57 | // Find a path name for the id in the given menu, but only if it, and |
| 58 | // each sub-menu above it, is uniquely named among peer items |
| 59 | std::optional< wxArrayStringEx > FindPathName( wxMenu &theMenu, int id ) |
| 60 | { |
| 61 | wxMenuItem *pItem = nullptr; |
| 62 | wxMenu *pSubMenu = nullptr; |
| 63 | if ( !( pItem = theMenu.FindItem( id, &pSubMenu ) ) ) |
| 64 | return std::nullopt; |
| 65 | |
| 66 | // Gather path components, checking uniqueness at each level |
| 67 | wxArrayStringEx names; |
| 68 | for ( ; pSubMenu; pSubMenu = pSubMenu->GetParent() ) { |
| 69 | const auto &items = pSubMenu->GetMenuItems(); |
| 70 | const auto begin = items.begin(), end = items.end(); |
| 71 | if ( !names.empty() ) { |
| 72 | // Update pItem on second and later passes |
| 73 | if ( const auto iter = std::find_if( begin, end, |
| 74 | [&]( auto pNewItem ){ |
| 75 | return pNewItem->GetSubMenu() == pItem->GetMenu(); } ); |
| 76 | iter == end ) |
| 77 | return std::nullopt; |
| 78 | else |
| 79 | pItem = *iter; |
| 80 | } |
| 81 | auto name = pItem->GetItemLabelText(); |
| 82 | if ( 1 != std::count_if( begin, end, [&](auto item){ |
| 83 | return item->GetItemLabelText() == name; } ) ) |
| 84 | // nonuniqueness |
| 85 | return std::nullopt; |
| 86 | names.push_back( name ); |
| 87 | } |
| 88 | std::reverse( names.begin(), names.end() ); |
| 89 | return { names }; |
| 90 | } |
| 91 | |
| 92 | //! Singleton object listens to global wxEvent stream |
| 93 | struct Watcher : wxEventFilter |
no test coverage detected