| 44 | } |
| 45 | |
| 46 | unsigned CommonTrackPanelCell::DoContextMenu( const wxRect &rect, |
| 47 | wxWindow *pParent, const wxPoint *pPoint, AudacityProject *const pProject) |
| 48 | { |
| 49 | const auto items = GetMenuItems( rect, pPoint, pProject ); |
| 50 | if (items.empty()) |
| 51 | return RefreshCode::RefreshNone; |
| 52 | |
| 53 | auto &commandManager = CommandManager::Get(*pProject); |
| 54 | commandManager.UpdateMenus(); |
| 55 | |
| 56 | // Set up command context with extras |
| 57 | CommandContext context{ *pProject }; |
| 58 | SelectedRegion region; |
| 59 | if (pPoint) { |
| 60 | auto time = ViewInfo::Get(*pProject).PositionToTime(pPoint->x, rect.x); |
| 61 | region = { time, time }; |
| 62 | context.temporarySelection.pSelectedRegion = ®ion; |
| 63 | } |
| 64 | context.temporarySelection.pTrack = FindTrack().get(); |
| 65 | |
| 66 | auto flags = commandManager.GetUpdateFlags(); |
| 67 | |
| 68 | // Common dispatcher for the menu items |
| 69 | auto dispatcher = [&]( wxCommandEvent &evt ){ |
| 70 | auto idx = evt.GetId() - 1; |
| 71 | if (idx >= 0 && idx < items.size()) { |
| 72 | if (auto &action = items[idx].action) |
| 73 | action( context ); |
| 74 | else |
| 75 | commandManager.HandleTextualCommand( |
| 76 | items[idx].symbol.Internal(), context, flags, false); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | wxMenu menu; |
| 81 | int ii = 1; |
| 82 | for (const auto &item: items) { |
| 83 | if ( const auto &commandID = item.symbol.Internal(); |
| 84 | commandID.empty() ) |
| 85 | menu.AppendSeparator(); |
| 86 | else { |
| 87 | // Generate a menu item with the same shortcut key as in the toolbar |
| 88 | // menu, and as determined by keyboard preferences |
| 89 | auto label = |
| 90 | commandManager.FormatLabelForMenu( commandID, &item.symbol.Msgid() ); |
| 91 | menu.Append( ii, label ); |
| 92 | menu.Bind( wxEVT_COMMAND_MENU_SELECTED, dispatcher ); |
| 93 | bool enabled = item.enabled && |
| 94 | (item.action || commandManager.GetEnabled( commandID )); |
| 95 | menu.Enable( ii, enabled ); |
| 96 | } |
| 97 | ++ii; |
| 98 | } |
| 99 | |
| 100 | BasicUI::Point point; |
| 101 | if (pPoint) |
| 102 | point = { pPoint->x, pPoint->y }; |
| 103 | BasicMenu::Handle{ &menu }.Popup( |
nothing calls this directly
no test coverage detected