1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-25 08:38:39 +02:00

DeviceToolBar.cpp: Better event handling -

Make OnChoice only do the action associated with the combo box being interacted with.
Also refactor associated input and output device code into new function.
This commit is contained in:
mchinen 2011-02-06 23:34:32 +00:00
parent 046593405f
commit a9437f795b
2 changed files with 46 additions and 47 deletions

View File

@ -675,7 +675,7 @@ int DeviceToolBar::ChangeHost()
hostSelectionIndex = mHost->GetSelection(); hostSelectionIndex = mHost->GetSelection();
wxString oldHost = gPrefs->Read(wxT("/AudioIO/Host"), wxT("")); wxString oldHost = gPrefs->Read(wxT("/AudioIO/Host"), wxT(""));
wxString newHost = hostSelectionIndex >= 0 ? mHost->GetString(mHost->GetSelection()) : wxString newHost = hostSelectionIndex >= 0 ? mHost->GetString(hostSelectionIndex) :
oldHost; oldHost;
if (oldHost == newHost) if (oldHost == newHost)
@ -757,57 +757,54 @@ void DeviceToolBar::SetDevices(DeviceSourceMap *in, DeviceSourceMap *out)
} }
} }
void DeviceToolBar::ChangeDevice(bool isInput)
{
int newIndex = -1;
wxChoice *combo = isInput ? mInput :mOutput;
size_t i;
int selectionIndex = mInput->GetSelection();
std::vector<DeviceSourceMap> &maps = isInput ? mInputDeviceSourceMaps : mOutputDeviceSourceMaps;
wxString host = gPrefs->Read(wxT("/AudioIO/Host"), wxT(""));
// Find device indices for input and output
if (selectionIndex >= 0 ) {
wxString newDevice = combo->GetStringSelection();
for (i = 0; i < maps.size(); ++i) {
wxString name;
name = MakeDeviceSourceString(&maps[i]);
if (name == newDevice && maps[i].hostString == host) {
newIndex = i;
}
}
}
if (newIndex < 0) {
wxLogDebug(wxT("DeviceToolBar::OnChoice(): couldn't find device indices"));
return;
}
SetDevices(isInput ? &maps[newIndex] : NULL,
isInput ? NULL : &maps[newIndex]);
}
void DeviceToolBar::OnChoice(wxCommandEvent &event) void DeviceToolBar::OnChoice(wxCommandEvent &event)
{ {
int inputSelectionIndex;
int outputSelectionIndex; int outputSelectionIndex;
int channelsSelectionIndex;
wxObject *eventObject = event.GetEventObject();
//if we've changed hosts, we've handled the device switching already. //if we've changed hosts, we've handled the device switching already.
if (!ChangeHost()) { if (eventObject == mHost) {
inputSelectionIndex = mInput->GetSelection(); ChangeHost();
outputSelectionIndex = mOutput->GetSelection(); } else if (eventObject == mInputChannels) {
channelsSelectionIndex = mInputChannels->GetSelection(); int channelsSelectionIndex = mInputChannels->GetSelection();
if (channelsSelectionIndex >= 0)
if (channelsSelectionIndex >= 0) { gPrefs->Write(wxT("/AudioIO/RecordChannels"),channelsSelectionIndex + 1);
gPrefs->Write(wxT("/AudioIO/RecordChannels"), } else if (eventObject == mInput) {
channelsSelectionIndex + 1); ChangeDevice(true);
} }
else if (eventObject == mOutput) {
wxString host = gPrefs->Read(wxT("/AudioIO/Host"), wxT("")); ChangeDevice(false);
int newInIndex = -1, newOutIndex = -1;
size_t i;
// Find device indices for input and output
if (inputSelectionIndex >= 0 ) {
wxString newInput = mInput->GetStringSelection();
for (i = 0; i < mInputDeviceSourceMaps.size(); ++i) {
wxString name;
name = MakeDeviceSourceString(&mInputDeviceSourceMaps[i]);
if (name == newInput && mInputDeviceSourceMaps[i].hostString == host) {
newInIndex = i;
}
}
}
if (outputSelectionIndex >= 0) {
wxString newOutput = mOutput->GetStringSelection();
for (i = 0; i < mOutputDeviceSourceMaps.size(); ++i) {
wxString name;
name = MakeDeviceSourceString(&mOutputDeviceSourceMaps[i]);
if (name == newOutput && mOutputDeviceSourceMaps[i].hostString == host)
newOutIndex = i;
}
}
// This shouldn't happen for new choices (it's OK for old ones)
if (newInIndex < 0 || newOutIndex < 0) {
wxLogDebug(wxT("DeviceToolBar::OnChoice(): couldn't find device indices"));
return;
}
SetDevices(newInIndex >= 0 ? &mInputDeviceSourceMaps[newInIndex] : NULL,
newOutIndex >= 0 ? &mOutputDeviceSourceMaps[newOutIndex] : NULL);
} }
if (gAudioIO) { if (gAudioIO) {
@ -884,6 +881,7 @@ void DeviceToolBar::ShowComboDialog(wxChoice *combo, const wxString &title)
if (dlg.ShowModal() == wxID_OK) if (dlg.ShowModal() == wxID_OK)
{ {
wxCommandEvent dummyEvent; wxCommandEvent dummyEvent;
dummyEvent.SetEventObject(combo);
// SetSelection() doesn't send an event, so we call OnChoice explicitly // SetSelection() doesn't send an event, so we call OnChoice explicitly
combo->SetSelection(c->GetSelection()); combo->SetSelection(c->GetSelection());
OnChoice(dummyEvent); OnChoice(dummyEvent);

View File

@ -65,6 +65,7 @@ class DeviceToolBar:public ToolBar {
private: private:
int ChangeHost(); int ChangeHost();
void ChangeDevice(bool isInput);
void FillHostDevices(); void FillHostDevices();
void FillInputChannels(); void FillInputChannels();
void SetDevices(DeviceSourceMap *in, DeviceSourceMap *out); void SetDevices(DeviceSourceMap *in, DeviceSourceMap *out);