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 "midi++/midnam_patch.h"
30 #include "ardour/beats_frames_converter.h"
31 #include "ardour/instrument_info.h"
33 #include "patch_change_dialog.h"
34 #include "gui_thread.h"
40 using namespace Gtkmm2ext;
42 /** @param tc If non-0, a time converter for this patch change. If 0, time control will be desensitized */
43 PatchChangeDialog::PatchChangeDialog (
44 const ARDOUR::BeatsFramesConverter* tc,
45 ARDOUR::Session* session,
46 Evoral::PatchChange<Evoral::Beats> const & patch,
47 ARDOUR::InstrumentInfo& info,
48 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 (Gtk::StockID(GTK_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::Beats>
143 PatchChangeDialog::patch () const
145 Evoral::Beats t = Evoral::Beats();
147 if (_time_converter) {
148 t = _time_converter->from (_time.current_time ());
151 return Evoral::PatchChange<Evoral::Beats> (
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 _bank_combo.clear ();
165 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
171 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
172 string n = (*i)->name ();
173 boost::replace_all (n, "_", " ");
174 _bank_combo.append_text (n);
178 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
180 PatchChangeDialog::set_active_bank_combo ()
182 _current_patch_bank.reset ();
184 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
190 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
192 string n = (*i)->name ();
193 boost::replace_all (n, "_", " ");
195 if ((*i)->number() == _bank.get_value () - 1) {
196 _current_patch_bank = *i;
197 _ignore_signals = true;
198 _bank_combo.set_active_text (n);
199 _ignore_signals = false;
204 _ignore_signals = true;
205 _bank_combo.set_active (-1);
206 _ignore_signals = false;
209 /** Update _current_patch_bank and reflect the current value of
210 * bank_combo in the rest of the dialog.
213 PatchChangeDialog::bank_combo_changed ()
215 if (_ignore_signals) {
219 _current_patch_bank.reset ();
221 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
227 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
228 string n = (*i)->name ();
229 boost::replace_all (n, "_", " ");
230 if (n == _bank_combo.get_active_text()) {
231 _current_patch_bank = *i;
235 if (_current_patch_bank == 0) {
242 set_active_patch_combo ();
244 _ignore_signals = true;
245 _bank.set_value (_current_patch_bank->number() + 1);
246 _ignore_signals = false;
249 /** Fill the contents of the patch combo */
251 PatchChangeDialog::fill_patch_combo ()
253 _patch_combo.clear ();
255 if (_current_patch_bank == 0) {
259 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
260 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
261 string n = (*j)->name ();
262 boost::replace_all (n, "_", " ");
263 _patch_combo.append_text (n);
267 /** Set the active value of the patch combo from the value of the _program entry */
269 PatchChangeDialog::set_active_patch_combo ()
271 if (_ignore_signals) {
275 if (_current_patch_bank == 0) {
276 _ignore_signals = true;
277 _patch_combo.set_active (-1);
278 _ignore_signals = false;
282 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
283 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
284 string n = (*j)->name ();
285 boost::replace_all (n, "_", " ");
287 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
288 if (key.program() == _program.get_value() - 1) {
289 _ignore_signals = true;
290 _patch_combo.set_active_text (n);
291 _ignore_signals = false;
296 _ignore_signals = true;
297 _patch_combo.set_active (-1);
298 _ignore_signals = false;
301 /** Set _program from the current state of _patch_combo */
303 PatchChangeDialog::patch_combo_changed ()
305 if (_ignore_signals || _current_patch_bank == 0) {
309 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
311 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
312 string n = (*j)->name ();
313 boost::replace_all (n, "_", " ");
315 if (n == _patch_combo.get_active_text ()) {
316 _ignore_signals = true;
317 _program.set_value ((*j)->program_number() + 1);
318 _ignore_signals = false;
325 PatchChangeDialog::channel_changed ()
328 set_active_bank_combo ();
330 set_active_patch_combo ();
334 PatchChangeDialog::program_changed ()
336 if (_ignore_signals) {
340 set_active_patch_combo ();
344 PatchChangeDialog::bank_changed ()
346 if (_ignore_signals) {
350 set_active_bank_combo ();
352 set_active_patch_combo ();