* This class provides a hack to use popup menu instead of the dropdown list. * This allows to organize the list of items in a more user-friendly way. */
| 54 | * This allows to organize the list of items in a more user-friendly way. |
| 55 | */ |
| 56 | class SnapModePopup final : public wxComboPopup |
| 57 | { |
| 58 | public: |
| 59 | explicit SnapModePopup(AudacityProject& project) |
| 60 | : mProject { project } |
| 61 | , mSnappingModeChangedSubscription(ProjectSnap::Get(project).Subscribe( |
| 62 | [this](auto& msg) { |
| 63 | UpdateCurrentIndex(msg.newSnapTo); |
| 64 | |
| 65 | auto comboCtrl = GetComboCtrl(); |
| 66 | |
| 67 | comboCtrl->SetValue( |
| 68 | GetSnapToLabel(msg.newSnapTo).Translation()); |
| 69 | |
| 70 | comboCtrl->SetName(GetComboCtrl()->GetValue()); |
| 71 | })) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void Init () override |
| 76 | { |
| 77 | // Build a linear list from all the items in the snap functions registry |
| 78 | SnapFunctionsRegistry::Visit([this](const SnapRegistryItem& item, auto&) { |
| 79 | mSnapToList.push_back(item.name); |
| 80 | }); |
| 81 | |
| 82 | UpdateCurrentIndex(ReadSnapTo()); |
| 83 | } |
| 84 | |
| 85 | bool Create(wxWindow* parent) override |
| 86 | { |
| 87 | mControl = safenew wxWindow(parent, wxID_ANY); |
| 88 | |
| 89 | // This call cannot happen in Init(), because the combobox is not yet in a valid |
| 90 | // state. Doing a deferred call from Init() is unsafe, |
| 91 | // as in some cases Audacity recreates the combobox multiple times before the next |
| 92 | // event loop iteration. |
| 93 | GetComboCtrl()->SetValue(GetStringValue()); |
| 94 | |
| 95 | return mControl; |
| 96 | } |
| 97 | |
| 98 | wxWindow* GetControl() override |
| 99 | { |
| 100 | return mControl; |
| 101 | } |
| 102 | |
| 103 | wxString GetStringValue() const override |
| 104 | { |
| 105 | return GetSnapToLabel(ReadSnapTo()).Translation(); |
| 106 | } |
| 107 | |
| 108 | void OnPopup() override |
| 109 | { |
| 110 | // Build a popup menu based on snap functions registry |
| 111 | wxMenu menu; |
| 112 | std::vector<wxMenu*> menuStack{ &menu }; |
| 113 |