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