Marginal cleanup.
[ardour.git] / gtk2_ardour / patch_change_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
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.
9
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.
14
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.
18
19 */
20
21 #include <gtkmm/stock.h>
22 #include <gtkmm/table.h>
23 #include <boost/algorithm/string.hpp>
24 #include "gtkmm2ext/utils.h"
25 #include "ardour/midi_patch_manager.h"
26 #include "ardour/beats_frames_converter.h"
27 #include "patch_change_dialog.h"
28 #include "i18n.h"
29
30 using namespace std;
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33
34 /** @param tc If non-0, a time converter for this patch change.  If 0, time control will be desensitized */
35 PatchChangeDialog::PatchChangeDialog (
36         const ARDOUR::BeatsFramesConverter* tc,
37         ARDOUR::Session* session,
38         Evoral::PatchChange<Evoral::MusicalTime> const & patch,
39         string const & model_name,
40         string const & custom_device_node,
41         const Gtk::BuiltinStockID& ok
42         )
43         : ArdourDialog (_("Patch Change"), true)
44         , _time_converter (tc)
45         , _model_name (model_name)
46         , _custom_device_mode (custom_device_node)
47         , _time (X_("patchchangetime"), true, "", true, false)
48         , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
49         , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
50         , _bank (*manage (new Adjustment (1, 1, 16384, 1, 64)))
51         , _ignore_signals (false)
52 {
53         Table* t = manage (new Table (4, 2));
54         Label* l;
55         t->set_spacings (6);
56         int r = 0;
57
58         if (_time_converter) {
59                 
60                 l = manage (left_aligned_label (_("Time")));
61                 t->attach (*l, 0, 1, r, r + 1);
62                 t->attach (_time, 1, 2, r, r + 1);
63                 ++r;
64
65                 _time.set_session (session);
66                 _time.set_mode (AudioClock::BBT);
67                 _time.set (_time_converter->to (patch.time ()), true);
68         }
69
70         l = manage (left_aligned_label (_("Patch Bank")));
71         t->attach (*l, 0, 1, r, r + 1);
72         t->attach (_bank_combo, 1, 2, r, r + 1);
73         ++r;
74
75         _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
76
77         l = manage (left_aligned_label (_("Patch")));
78         t->attach (*l, 0, 1, r, r + 1);
79         t->attach (_patch_combo, 1, 2, r, r + 1);
80         ++r;
81         
82         _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
83
84         l = manage (left_aligned_label (_("Channel")));
85         t->attach (*l, 0, 1, r, r + 1);
86         t->attach (_channel, 1, 2, r, r + 1);
87         ++r;
88
89         _channel.set_value (patch.channel() + 1);
90         _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
91
92         l = manage (left_aligned_label (_("Program")));
93         t->attach (*l, 0, 1, r, r + 1);
94         t->attach (_program, 1, 2, r, r + 1);
95         ++r;
96
97         _program.set_value (patch.program () + 1);
98         _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
99
100         l = manage (left_aligned_label (_("Bank")));
101         t->attach (*l, 0, 1, r, r + 1);
102         t->attach (_bank, 1, 2, r, r + 1);
103         ++r;
104
105         _bank.set_value (patch.bank() + 1);
106         _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
107
108         get_vbox()->add (*t);
109
110         add_button (Stock::CANCEL, RESPONSE_CANCEL);
111         add_button (ok, RESPONSE_ACCEPT);
112         set_default_response (RESPONSE_ACCEPT);
113
114         fill_bank_combo ();
115         set_active_bank_combo ();
116         bank_combo_changed ();
117
118         show_all ();
119 }
120
121 Evoral::PatchChange<Evoral::MusicalTime>
122 PatchChangeDialog::patch () const
123 {
124         Evoral::MusicalTime t = 0;
125
126         if (_time_converter) {
127                 t = _time_converter->from (_time.current_time ());
128         }
129
130         return Evoral::PatchChange<Evoral::MusicalTime> (
131                 t,
132                 _channel.get_value_as_int() - 1,
133                 _program.get_value_as_int() - 1,
134                 _bank.get_value_as_int() - 1
135                 );
136 }
137
138 /** Fill the bank_combo according to the current _channel */
139 void
140 PatchChangeDialog::fill_bank_combo ()
141 {
142         MIDI::Name::ChannelNameSet::PatchBanks const * banks = get_banks ();
143         if (banks == 0) {
144                 return;
145         }
146
147         for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = banks->begin(); i != banks->end(); ++i) {
148                 string n = (*i)->name ();
149                 boost::replace_all (n, "_", " ");
150                 _bank_combo.append_text (n);
151         }
152 }
153
154 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
155 void
156 PatchChangeDialog::set_active_bank_combo ()
157 {
158         _current_patch_bank.reset ();
159         
160         MIDI::Name::ChannelNameSet::PatchBanks const * banks = get_banks ();
161         if (banks == 0) {
162                 return;
163         }
164
165         for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = banks->begin(); i != banks->end(); ++i) {
166                 string n = (*i)->name ();
167                 boost::replace_all (n, "_", " ");
168
169                 MIDI::Name::PatchPrimaryKey const * key = (*i)->patch_primary_key ();
170                 if (key && ((key->msb << 7) | key->lsb) == _bank.get_value () - 1) {
171                         _current_patch_bank = *i;
172                         _ignore_signals = true;
173                         _bank_combo.set_active_text (n);
174                         _ignore_signals = false;
175                         return;
176                 }
177         }
178
179         _ignore_signals = true;
180         _bank_combo.set_active (-1);
181         _ignore_signals = false;
182 }
183
184 /** Update _current_patch_bank and reflect the current value of
185  *  bank_combo in the rest of the dialog.
186  */
187 void
188 PatchChangeDialog::bank_combo_changed ()
189 {
190         if (_ignore_signals) {
191                 return;
192         }
193         
194         _current_patch_bank.reset ();
195
196         MIDI::Name::ChannelNameSet::PatchBanks const * banks = get_banks ();
197         if (banks == 0) {
198                 return;
199         }
200
201         for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = banks->begin(); i != banks->end(); ++i) {
202                 string n = (*i)->name ();
203                 boost::replace_all (n, "_", " ");
204                 if (n == _bank_combo.get_active_text()) {
205                         _current_patch_bank = *i;
206                 }
207         }
208
209         if (_current_patch_bank == 0) {
210                 return;
211         }
212
213         /* Reflect */
214
215         fill_patch_combo ();
216         set_active_patch_combo ();
217
218         MIDI::Name::PatchPrimaryKey const * key = _current_patch_bank->patch_primary_key ();
219         if (key) {
220                 _ignore_signals = true;
221                 _bank.set_value (((key->msb << 7) | key->lsb) + 1);
222                 _ignore_signals = false;
223         }
224 }
225
226 /** Fill the contents of the patch combo */
227 void
228 PatchChangeDialog::fill_patch_combo ()
229 {
230         _patch_combo.clear ();
231
232         if (_current_patch_bank == 0) {
233                 return;
234         }
235
236         const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
237         for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
238                 string n = (*j)->name ();
239                 boost::replace_all (n, "_", " ");
240                 _patch_combo.append_text (n);
241         }
242 }
243
244 /** Set the active value of the patch combo from the value of the _program entry */
245 void
246 PatchChangeDialog::set_active_patch_combo ()
247 {
248         if (_ignore_signals) {
249                 return;
250         }
251
252         if (_current_patch_bank == 0) {
253                 _ignore_signals = true;
254                 _patch_combo.set_active (-1);
255                 _ignore_signals = false;
256                 return;
257         }
258         
259         const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
260         for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
261                 string n = (*j)->name ();
262                 boost::replace_all (n, "_", " ");
263
264                 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
265                 if (key.program_number == _program.get_value() - 1) {
266                         _ignore_signals = true;
267                         _patch_combo.set_active_text (n);
268                         _ignore_signals = false;
269                         return;
270                 }
271         }
272
273         _ignore_signals = true;
274         _patch_combo.set_active (-1);
275         _ignore_signals = false;
276 }       
277
278 /** Set _program from the current state of _patch_combo */
279 void
280 PatchChangeDialog::patch_combo_changed ()
281 {
282         if (_ignore_signals || _current_patch_bank == 0) {
283                 return;
284         }
285
286         const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
287         for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
288                 string n = (*j)->name ();
289                 boost::replace_all (n, "_", " ");
290                 if (n == _patch_combo.get_active_text ()) {
291                         MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
292                         _ignore_signals = true;
293                         _program.set_value (key.program_number + 1);
294                         _ignore_signals = false;
295                 }
296         }
297 }
298
299 void
300 PatchChangeDialog::channel_changed ()
301 {
302         fill_bank_combo ();
303         set_active_bank_combo ();
304         fill_patch_combo ();
305         set_active_patch_combo ();
306 }
307
308 void
309 PatchChangeDialog::program_changed ()
310 {
311         if (_ignore_signals) {
312                 return;
313         }
314
315         set_active_patch_combo ();
316 }
317
318 void
319 PatchChangeDialog::bank_changed ()
320 {
321         if (_ignore_signals) {
322                 return;
323         }
324
325         set_active_bank_combo ();
326         fill_patch_combo ();
327         set_active_patch_combo ();
328 }
329
330 MIDI::Name::ChannelNameSet::PatchBanks const *
331 PatchChangeDialog::get_banks ()
332 {
333         MIDI::Name::MidiPatchManager& mpm = MIDI::Name::MidiPatchManager::instance ();
334         boost::shared_ptr<MIDI::Name::ChannelNameSet> channel_name_set = mpm.find_channel_name_set (
335                 _model_name, _custom_device_mode, _channel.get_value_as_int() - 1
336                 );
337
338         if (!channel_name_set) {
339                 return 0;
340         }
341
342         return &channel_name_set->patch_banks ();
343 }