2 Copyright (C) 2010 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <gtkmm/stock.h>
22 #include <gtkmm/table.h>
24 #include <boost/algorithm/string.hpp>
26 #include "gtkmm2ext/utils.h"
28 #include "ardour/midi_patch_manager.h"
29 #include "ardour/beats_frames_converter.h"
30 #include "ardour/instrument_info.h"
32 #include "patch_change_dialog.h"
33 #include "gui_thread.h"
39 using namespace Gtkmm2ext;
41 /** @param tc If non-0, a time converter for this patch change. If 0, time control will be desensitized */
42 PatchChangeDialog::PatchChangeDialog (
43 const ARDOUR::BeatsFramesConverter* tc,
44 ARDOUR::Session* session,
45 Evoral::PatchChange<Evoral::MusicalTime> const & patch,
46 ARDOUR::InstrumentInfo& info,
47 const Gtk::BuiltinStockID& ok,
50 : ArdourDialog (_("Patch Change"), true)
51 , _time_converter (tc)
53 , _time (X_("patchchangetime"), true, "", true, false)
54 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
55 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
56 , _bank (*manage (new Adjustment (1, 1, 16384, 1, 64)))
57 , _ignore_signals (false)
59 Table* t = manage (new Table (4, 2));
64 if (_time_converter) {
66 l = manage (left_aligned_label (_("Time")));
67 t->attach (*l, 0, 1, r, r + 1);
68 t->attach (_time, 1, 2, r, r + 1);
71 _time.set_session (session);
72 _time.set_mode (AudioClock::BBT);
73 _time.set (_time_converter->to (patch.time ()), true);
76 l = manage (left_aligned_label (_("Patch Bank")));
77 t->attach (*l, 0, 1, r, r + 1);
78 t->attach (_bank_combo, 1, 2, r, r + 1);
81 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
83 l = manage (left_aligned_label (_("Patch")));
84 t->attach (*l, 0, 1, r, r + 1);
85 t->attach (_patch_combo, 1, 2, r, r + 1);
88 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
90 l = manage (left_aligned_label (_("Channel")));
91 t->attach (*l, 0, 1, r, r + 1);
92 t->attach (_channel, 1, 2, r, r + 1);
95 _channel.set_value (patch.channel() + 1);
96 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
98 l = manage (left_aligned_label (_("Program")));
99 t->attach (*l, 0, 1, r, r + 1);
100 t->attach (_program, 1, 2, r, r + 1);
103 _program.set_value (patch.program () + 1);
104 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
106 l = manage (left_aligned_label (_("Bank")));
107 t->attach (*l, 0, 1, r, r + 1);
108 t->attach (_bank, 1, 2, r, r + 1);
111 _bank.set_value (patch.bank() + 1);
112 _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
114 get_vbox()->add (*t);
116 add_button (Stock::CANCEL, RESPONSE_CANCEL);
117 add_button (ok, RESPONSE_ACCEPT);
119 add_button (Stock::DELETE, RESPONSE_REJECT);
121 set_default_response (RESPONSE_ACCEPT);
124 set_active_bank_combo ();
125 bank_combo_changed ();
127 _info.Changed.connect (_info_changed_connection, invalidator (*this),
128 boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
134 PatchChangeDialog::instrument_info_changed ()
136 _bank_combo.clear ();
137 _patch_combo.clear ();
142 Evoral::PatchChange<Evoral::MusicalTime>
143 PatchChangeDialog::patch () const
145 Evoral::MusicalTime t = 0;
147 if (_time_converter) {
148 t = _time_converter->from (_time.current_time ());
151 return Evoral::PatchChange<Evoral::MusicalTime> (
153 _channel.get_value_as_int() - 1,
154 _program.get_value_as_int() - 1,
155 _bank.get_value_as_int() - 1
159 /** Fill the bank_combo according to the current _channel */
161 PatchChangeDialog::fill_bank_combo ()
163 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
169 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
170 string n = (*i)->name ();
171 boost::replace_all (n, "_", " ");
172 _bank_combo.append_text (n);
176 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
178 PatchChangeDialog::set_active_bank_combo ()
180 _current_patch_bank.reset ();
182 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
188 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
190 string n = (*i)->name ();
191 boost::replace_all (n, "_", " ");
193 if ((*i)->number() == _bank.get_value () - 1) {
194 _current_patch_bank = *i;
195 _ignore_signals = true;
196 _bank_combo.set_active_text (n);
197 _ignore_signals = false;
202 _ignore_signals = true;
203 _bank_combo.set_active (-1);
204 _ignore_signals = false;
207 /** Update _current_patch_bank and reflect the current value of
208 * bank_combo in the rest of the dialog.
211 PatchChangeDialog::bank_combo_changed ()
213 if (_ignore_signals) {
217 _current_patch_bank.reset ();
219 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
225 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
226 string n = (*i)->name ();
227 boost::replace_all (n, "_", " ");
228 if (n == _bank_combo.get_active_text()) {
229 _current_patch_bank = *i;
233 if (_current_patch_bank == 0) {
240 set_active_patch_combo ();
242 _ignore_signals = true;
243 _bank.set_value (_current_patch_bank->number() + 1);
244 _ignore_signals = false;
247 /** Fill the contents of the patch combo */
249 PatchChangeDialog::fill_patch_combo ()
251 _patch_combo.clear ();
253 if (_current_patch_bank == 0) {
257 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
258 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
259 string n = (*j)->name ();
260 boost::replace_all (n, "_", " ");
261 _patch_combo.append_text (n);
265 /** Set the active value of the patch combo from the value of the _program entry */
267 PatchChangeDialog::set_active_patch_combo ()
269 if (_ignore_signals) {
273 if (_current_patch_bank == 0) {
274 _ignore_signals = true;
275 _patch_combo.set_active (-1);
276 _ignore_signals = false;
280 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
281 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
282 string n = (*j)->name ();
283 boost::replace_all (n, "_", " ");
285 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
286 if (key.program_number == _program.get_value() - 1) {
287 _ignore_signals = true;
288 _patch_combo.set_active_text (n);
289 _ignore_signals = false;
294 _ignore_signals = true;
295 _patch_combo.set_active (-1);
296 _ignore_signals = false;
299 /** Set _program from the current state of _patch_combo */
301 PatchChangeDialog::patch_combo_changed ()
303 if (_ignore_signals || _current_patch_bank == 0) {
307 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
309 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
310 string n = (*j)->name ();
311 boost::replace_all (n, "_", " ");
313 if (n == _patch_combo.get_active_text ()) {
314 _ignore_signals = true;
315 _program.set_value ((*j)->program_number() + 1);
316 _ignore_signals = false;
323 PatchChangeDialog::channel_changed ()
326 set_active_bank_combo ();
328 set_active_patch_combo ();
332 PatchChangeDialog::program_changed ()
334 if (_ignore_signals) {
338 set_active_patch_combo ();
342 PatchChangeDialog::bank_changed ()
344 if (_ignore_signals) {
348 set_active_bank_combo ();
350 set_active_patch_combo ();