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,
49 : ArdourDialog (_("Patch Change"), true)
50 , _time_converter (tc)
52 , _time (X_("patchchangetime"), true, "", true, false)
53 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
54 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
55 , _bank (*manage (new Adjustment (1, 1, 16384, 1, 64)))
56 , _ignore_signals (false)
58 Table* t = manage (new Table (4, 2));
63 if (_time_converter) {
65 l = manage (left_aligned_label (_("Time")));
66 t->attach (*l, 0, 1, r, r + 1);
67 t->attach (_time, 1, 2, r, r + 1);
70 _time.set_session (session);
71 _time.set_mode (AudioClock::BBT);
72 _time.set (_time_converter->to (patch.time ()), true);
75 l = manage (left_aligned_label (_("Patch Bank")));
76 t->attach (*l, 0, 1, r, r + 1);
77 t->attach (_bank_combo, 1, 2, r, r + 1);
80 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
82 l = manage (left_aligned_label (_("Patch")));
83 t->attach (*l, 0, 1, r, r + 1);
84 t->attach (_patch_combo, 1, 2, r, r + 1);
87 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
89 l = manage (left_aligned_label (_("Channel")));
90 t->attach (*l, 0, 1, r, r + 1);
91 t->attach (_channel, 1, 2, r, r + 1);
94 _channel.set_value (patch.channel() + 1);
95 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
97 l = manage (left_aligned_label (_("Program")));
98 t->attach (*l, 0, 1, r, r + 1);
99 t->attach (_program, 1, 2, r, r + 1);
102 _program.set_value (patch.program () + 1);
103 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
105 l = manage (left_aligned_label (_("Bank")));
106 t->attach (*l, 0, 1, r, r + 1);
107 t->attach (_bank, 1, 2, r, r + 1);
110 _bank.set_value (patch.bank() + 1);
111 _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
113 get_vbox()->add (*t);
115 add_button (Stock::CANCEL, RESPONSE_CANCEL);
116 add_button (ok, RESPONSE_ACCEPT);
118 add_button (Stock::DELETE, RESPONSE_REJECT);
120 set_default_response (RESPONSE_ACCEPT);
123 set_active_bank_combo ();
124 bank_combo_changed ();
126 _info.Changed.connect (_info_changed_connection, invalidator (*this),
127 boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
133 PatchChangeDialog::instrument_info_changed ()
135 _bank_combo.clear ();
136 _patch_combo.clear ();
141 Evoral::PatchChange<Evoral::MusicalTime>
142 PatchChangeDialog::patch () const
144 Evoral::MusicalTime t = 0;
146 if (_time_converter) {
147 t = _time_converter->from (_time.current_time ());
150 return Evoral::PatchChange<Evoral::MusicalTime> (
152 _channel.get_value_as_int() - 1,
153 _program.get_value_as_int() - 1,
154 _bank.get_value_as_int() - 1
158 /** Fill the bank_combo according to the current _channel */
160 PatchChangeDialog::fill_bank_combo ()
162 _bank_combo.clear ();
164 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
170 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
171 string n = (*i)->name ();
172 boost::replace_all (n, "_", " ");
173 _bank_combo.append_text (n);
177 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
179 PatchChangeDialog::set_active_bank_combo ()
181 _current_patch_bank.reset ();
183 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
189 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
191 string n = (*i)->name ();
192 boost::replace_all (n, "_", " ");
194 if ((*i)->number() == _bank.get_value () - 1) {
195 _current_patch_bank = *i;
196 _ignore_signals = true;
197 _bank_combo.set_active_text (n);
198 _ignore_signals = false;
203 _ignore_signals = true;
204 _bank_combo.set_active (-1);
205 _ignore_signals = false;
208 /** Update _current_patch_bank and reflect the current value of
209 * bank_combo in the rest of the dialog.
212 PatchChangeDialog::bank_combo_changed ()
214 if (_ignore_signals) {
218 _current_patch_bank.reset ();
220 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
226 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
227 string n = (*i)->name ();
228 boost::replace_all (n, "_", " ");
229 if (n == _bank_combo.get_active_text()) {
230 _current_patch_bank = *i;
234 if (_current_patch_bank == 0) {
241 set_active_patch_combo ();
243 _ignore_signals = true;
244 _bank.set_value (_current_patch_bank->number() + 1);
245 _ignore_signals = false;
248 /** Fill the contents of the patch combo */
250 PatchChangeDialog::fill_patch_combo ()
252 _patch_combo.clear ();
254 if (_current_patch_bank == 0) {
258 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
259 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
260 string n = (*j)->name ();
261 boost::replace_all (n, "_", " ");
262 _patch_combo.append_text (n);
266 /** Set the active value of the patch combo from the value of the _program entry */
268 PatchChangeDialog::set_active_patch_combo ()
270 if (_ignore_signals) {
274 if (_current_patch_bank == 0) {
275 _ignore_signals = true;
276 _patch_combo.set_active (-1);
277 _ignore_signals = false;
281 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
282 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
283 string n = (*j)->name ();
284 boost::replace_all (n, "_", " ");
286 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
287 if (key.program_number == _program.get_value() - 1) {
288 _ignore_signals = true;
289 _patch_combo.set_active_text (n);
290 _ignore_signals = false;
295 _ignore_signals = true;
296 _patch_combo.set_active (-1);
297 _ignore_signals = false;
300 /** Set _program from the current state of _patch_combo */
302 PatchChangeDialog::patch_combo_changed ()
304 if (_ignore_signals || _current_patch_bank == 0) {
308 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
310 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
311 string n = (*j)->name ();
312 boost::replace_all (n, "_", " ");
314 if (n == _patch_combo.get_active_text ()) {
315 _ignore_signals = true;
316 _program.set_value ((*j)->program_number() + 1);
317 _ignore_signals = false;
324 PatchChangeDialog::channel_changed ()
327 set_active_bank_combo ();
329 set_active_patch_combo ();
333 PatchChangeDialog::program_changed ()
335 if (_ignore_signals) {
339 set_active_patch_combo ();
343 PatchChangeDialog::bank_changed ()
345 if (_ignore_signals) {
349 set_active_bank_combo ();
351 set_active_patch_combo ();