Skip to content

Commit

Permalink
Fixed "Copy current receive event" in the Midi Event Send dialog Gran…
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Samarin committed Feb 5, 2022
1 parent 1f091a5 commit 2953fc6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 48 deletions.
43 changes: 37 additions & 6 deletions src/grandorgue/midi/MIDIEventDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "MIDIEventDialog.h"

#include <wx/bookctrl.h>
#include <wx/msgdlg.h>

#include "MIDIEventKeyDialog.h"
#include "MIDIEventRecvDialog.h"
Expand Down Expand Up @@ -73,14 +74,44 @@ void MIDIEventDialog::RegisterMIDIListener(GOMidi *midi) {
m_recvPage->RegisterMIDIListener(midi);
}

void MIDIEventDialog::OnApply(wxCommandEvent &event) { DoApply(); }
bool MIDIEventDialog::Validate() {
wxWindow *notValidTab = NULL;
wxString errMsg;

if (!notValidTab && m_sendPage && !m_sendPage->Validate(errMsg))
notValidTab = m_sendPage;
if (
!notValidTab && m_sendDivisionPage && !m_sendDivisionPage->Validate(errMsg))
notValidTab = m_sendPage;

// Distplay the tab that has problems
if (notValidTab) {
wxBookCtrlBase *const notebook = GetBookCtrl();
const int notValidPageId = notebook->FindPage(notValidTab);

if (notValidPageId != wxNOT_FOUND)
notebook->SetSelection(notValidPageId);
if (!errMsg.IsEmpty())
wxMessageBox(
errMsg, _("Invalid MIDI event"), wxOK | wxCENTRE | wxICON_ERROR);
}

return !notValidTab;
}

void MIDIEventDialog::OnApply(wxCommandEvent &event) {
if (Validate())
DoApply();
}

void MIDIEventDialog::OnOK(wxCommandEvent &event) {
DoApply();
if (HasDocument())
Destroy();
else
EndModal(wxID_OK);
if (Validate()) {
DoApply();
if (HasDocument())
Destroy();
else
EndModal(wxID_OK);
}
}

void MIDIEventDialog::OnCancel(wxCommandEvent &event) {
Expand Down
1 change: 1 addition & 0 deletions src/grandorgue/midi/MIDIEventDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MIDIEventDialog : public wxPropertySheetDialog, public GOView {
MIDIEventSendDialog *m_sendDivisionPage;
MIDIEventKeyDialog *m_keyPage;

bool Validate();
void DoApply();

void OnApply(wxCommandEvent &event);
Expand Down
109 changes: 67 additions & 42 deletions src/grandorgue/midi/MIDIEventSendDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,30 @@ MIDIEventSendDialog::MIDIEventSendDialog(

MIDIEventSendDialog::~MIDIEventSendDialog() {}

void MIDIEventSendDialog::DoApply() {
bool MIDIEventSendDialog::Validate(wxString &errMsg) {
StoreEvent();

bool isValid = true;

for (unsigned i = 0; i < m_midi.GetEventCount(); i++) {
const GOMidiSendEvent &e = m_midi.GetEvent(i);

if (e.type != MIDI_S_NONE && !e.deviceId) {
errMsg = _("Output device is not selected.\n"
"Select one, set the type to None or delete this event.");
m_current = i;
LoadEvent();
isValid = false;
break;
}
}
return isValid;
}

void MIDIEventSendDialog::DoApply() {
// Assume that Validate() has been called and it returned true

// Delete empty events.
bool empty_event;
do {
empty_event = false;
Expand All @@ -240,6 +261,7 @@ void MIDIEventSendDialog::DoApply() {
empty_event = true;
}
} while (empty_event);
// The event with index 0 is also deleted so the dialog can't be used more

m_original->Assign(m_midi);
}
Expand Down Expand Up @@ -372,7 +394,7 @@ void MIDIEventSendDialog::LoadEvent() {
m_MidiMap.GetDeviceLogicalNameById(e.deviceId) == m_device->GetString(i))
m_device->SetSelection(i);

m_channel->SetSelection(e.channel - 1);
m_channel->SetSelection(e.channel > 0 && e.channel <= 16 ? e.channel - 1 : 0);
m_key->SetValue(e.key);
m_LowValue->SetValue(e.low_value);
m_HighValue->SetValue(e.high_value);
Expand Down Expand Up @@ -422,23 +444,27 @@ void MIDIEventSendDialog::OnCopyClick(wxCommandEvent &event) {

GOMidiSendEvent MIDIEventSendDialog::CopyEvent() {
GOMidiReceiveEvent recv = m_recv->GetCurrentEvent();

GOMidiSendEvent e;
e.deviceId = 0;

// try to fill e.deviceId as the id of the bound output device of the input
// device
const GOMidiDeviceConfig *pInDev = recv.deviceId
? m_MidiIn.FindByLogicalName(
m_MidiMap.GetDeviceLogicalNameById(recv.deviceId))
: NULL;
const GOMidiDeviceConfig *pOutDev = pInDev ? pInDev->p_OutputDevice : NULL;

e.deviceId
= pOutDev ? m_MidiMap.GetDeviceIdByLogicalName(pOutDev->m_LogicalName) : 0;

e.type = MIDI_S_NONE;
e.channel = 1;
e.key = 1;
e.low_value = 0;
e.high_value = 127;

const GOMidiDeviceConfig *pInDev = m_MidiIn.FindByLogicalName(
m_MidiMap.GetDeviceLogicalNameById(recv.deviceId));
const GOMidiDeviceConfig *pOutDev = pInDev ? pInDev->p_OutputDevice : NULL;

if (!pOutDev)
return e;
e.deviceId = m_MidiMap.GetDeviceIdByLogicalName(pOutDev->m_LogicalName);
if (m_midi.GetType() == MIDI_SEND_MANUAL) {
switch (m_midi.GetType()) {
case MIDI_SEND_MANUAL:
if (
recv.type == MIDI_M_NOTE || recv.type == MIDI_M_NOTE_NO_VELOCITY
|| recv.type == MIDI_M_NOTE_SHORT_OCTAVE
Expand All @@ -449,9 +475,8 @@ GOMidiSendEvent MIDIEventSendDialog::CopyEvent() {
e.low_value = recv.low_value ? recv.low_value - 1 : 0;
e.high_value = recv.high_value;
}
return e;
}
if (m_midi.GetType() == MIDI_SEND_ENCLOSURE) {
break;
case MIDI_SEND_ENCLOSURE:
e.channel = recv.channel;
e.key = recv.key;
e.low_value = recv.low_value;
Expand All @@ -463,35 +488,35 @@ GOMidiSendEvent MIDIEventSendDialog::CopyEvent() {
e.type = MIDI_S_NRPN;
else if (recv.type == MIDI_M_RPN)
e.type = MIDI_S_RPN;
break;
default:
e.channel = recv.channel;
e.key = recv.key;
e.low_value = recv.low_value;

return e;
}

e.channel = recv.channel;
e.key = recv.key;
e.low_value = recv.low_value;
e.high_value = 127;

if (recv.type == MIDI_M_NOTE)
e.type = MIDI_S_NOTE;
else if (recv.type == MIDI_M_CTRL_CHANGE)
e.type = MIDI_S_CTRL;
else if (recv.type == MIDI_M_NRPN)
e.type = MIDI_S_NRPN;
else if (recv.type == MIDI_M_RPN)
e.type = MIDI_S_RPN;
else if (recv.type == MIDI_M_SYSEX_RODGERS_STOP_CHANGE)
e.type = MIDI_S_RODGERS_STOP_CHANGE;
else if (recv.type == MIDI_M_RPN_RANGE) {
e.type = MIDI_S_RPN_RANGE;
e.high_value = recv.high_value;
} else if (recv.type == MIDI_M_NRPN_RANGE) {
e.type = MIDI_S_NRPN_RANGE;
e.high_value = recv.high_value;
} else if (recv.type == MIDI_M_PGM_RANGE) {
e.type = MIDI_S_PGM_RANGE;
e.high_value = recv.high_value;
if (recv.type == MIDI_M_NOTE)
e.type = MIDI_S_NOTE;
else if (recv.type == MIDI_M_CTRL_CHANGE)
e.type = MIDI_S_CTRL;
else if (recv.type == MIDI_M_NRPN)
e.type = MIDI_S_NRPN;
else if (recv.type == MIDI_M_RPN)
e.type = MIDI_S_RPN;
else if (recv.type == MIDI_M_SYSEX_RODGERS_STOP_CHANGE)
e.type = MIDI_S_RODGERS_STOP_CHANGE;
else if (recv.type == MIDI_M_RPN_RANGE) {
e.type = MIDI_S_RPN_RANGE;
e.high_value = recv.high_value;
} else if (recv.type == MIDI_M_NRPN_RANGE) {
e.type = MIDI_S_NRPN_RANGE;
e.high_value = recv.high_value;
} else if (recv.type == MIDI_M_PGM_RANGE) {
e.type = MIDI_S_PGM_RANGE;
e.high_value = recv.high_value;
}
}
if (e.channel <= 0)
e.channel = 1;

return e;
}
1 change: 1 addition & 0 deletions src/grandorgue/midi/MIDIEventSendDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class MIDIEventSendDialog : public wxPanel {
GOConfig &config);
~MIDIEventSendDialog();

bool Validate(wxString &errMsg);
void DoApply();

DECLARE_EVENT_TABLE()
Expand Down

0 comments on commit 2953fc6

Please sign in to comment.