+
+/** Fill the bank_combo according to the current _channel */
+void
+PatchChangeDialog::fill_bank_combo ()
+{
+ _bank_combo.clear ();
+
+ boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
+
+ if (!cns) {
+ return;
+ }
+
+ for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
+ string n = (*i)->name ();
+ boost::replace_all (n, "_", " ");
+ _bank_combo.append_text (n);
+ }
+}
+
+/** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
+void
+PatchChangeDialog::set_active_bank_combo ()
+{
+ _current_patch_bank.reset ();
+
+ boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
+
+ if (!cns) {
+ return;
+ }
+
+ for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
+
+ string n = (*i)->name ();
+ boost::replace_all (n, "_", " ");
+
+ if ((*i)->number() == get_14bit_bank()) {
+ _current_patch_bank = *i;
+ _ignore_signals = true;
+ _bank_combo.set_active_text (n);
+ _ignore_signals = false;
+ return;
+ }
+ }
+
+ _ignore_signals = true;
+ _bank_combo.set_active (-1);
+ _ignore_signals = false;
+}
+
+/** Update _current_patch_bank and reflect the current value of
+ * bank_combo in the rest of the dialog.
+ */
+void
+PatchChangeDialog::bank_combo_changed ()
+{
+ if (_ignore_signals) {
+ return;
+ }
+
+ _current_patch_bank.reset ();
+
+ boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
+
+ if (!cns) {
+ return;
+ }
+
+ for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
+ string n = (*i)->name ();
+ boost::replace_all (n, "_", " ");
+ if (n == _bank_combo.get_active_text()) {
+ _current_patch_bank = *i;
+ }
+ }
+
+ if (_current_patch_bank == 0) {
+ return;
+ }
+
+ /* Reflect */
+
+ fill_patch_combo ();
+ set_active_patch_combo ();
+
+ _ignore_signals = true;
+ _bank_msb.set_value (_current_patch_bank->number() >> 7);
+ _bank_lsb.set_value (_current_patch_bank->number() & 127);
+ _ignore_signals = false;
+}
+
+/** Fill the contents of the patch combo */
+void
+PatchChangeDialog::fill_patch_combo ()
+{
+ _patch_combo.clear ();
+
+ if (_current_patch_bank == 0) {
+ return;
+ }
+
+ const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
+ for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
+ string n = (*j)->name ();
+ boost::replace_all (n, "_", " ");
+ _patch_combo.append_text (n);
+ }
+}
+
+/** Set the active value of the patch combo from the value of the _program entry */
+void
+PatchChangeDialog::set_active_patch_combo ()
+{
+ if (_ignore_signals) {
+ return;
+ }
+
+ if (_current_patch_bank == 0) {
+ _ignore_signals = true;
+ _patch_combo.set_active (-1);
+ _ignore_signals = false;
+ return;
+ }
+
+ const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
+ for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
+ string n = (*j)->name ();
+ boost::replace_all (n, "_", " ");
+
+ MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
+ if (key.program() == _program.get_value() - 1) {
+ _ignore_signals = true;
+ _patch_combo.set_active_text (n);
+ _ignore_signals = false;
+ return;
+ }
+ }
+
+ _ignore_signals = true;
+ _patch_combo.set_active (-1);
+ _ignore_signals = false;
+}
+
+/** Set _program from the current state of _patch_combo */
+void
+PatchChangeDialog::patch_combo_changed ()
+{
+ if (_ignore_signals || _current_patch_bank == 0) {
+ return;
+ }
+
+ const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
+
+ for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
+ string n = (*j)->name ();
+ boost::replace_all (n, "_", " ");
+
+ if (n == _patch_combo.get_active_text ()) {
+ _ignore_signals = true;
+ _program.set_value ((*j)->program_number() + 1);
+ _ignore_signals = false;
+ break;
+ }
+ }
+}
+
+void
+PatchChangeDialog::channel_changed ()
+{
+ fill_bank_combo ();
+ set_active_bank_combo ();
+ fill_patch_combo ();
+ set_active_patch_combo ();
+}
+
+void
+PatchChangeDialog::program_changed ()
+{
+ if (_ignore_signals) {
+ return;
+ }
+
+ set_active_patch_combo ();
+}
+
+void
+PatchChangeDialog::bank_changed ()
+{
+ if (_ignore_signals) {
+ return;
+ }
+
+ set_active_bank_combo ();
+ fill_patch_combo ();
+ set_active_patch_combo ();
+}
+