fix lack of focus in new session name entry by removing previous efforts to fix it!
[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 #include "gui_thread.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace Gtk;
39 using namespace Gtkmm2ext;
40
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,
48         bool allow_delete
49         )
50         : ArdourDialog (_("Patch Change"), true)
51         , _time_converter (tc)
52         , _info (info)
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)
58 {
59         Table* t = manage (new Table (4, 2));
60         Label* l;
61         t->set_spacings (6);
62         int r = 0;
63
64         if (_time_converter) {
65                 
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);
69                 ++r;
70
71                 _time.set_session (session);
72                 _time.set_mode (AudioClock::BBT);
73                 _time.set (_time_converter->to (patch.time ()), true);
74         }
75
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);
79         ++r;
80
81         _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
82
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);
86         ++r;
87         
88         _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
89
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);
93         ++r;
94
95         _channel.set_value (patch.channel() + 1);
96         _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
97
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);
101         ++r;
102
103         _program.set_value (patch.program () + 1);
104         _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
105
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);
109         ++r;
110
111         _bank.set_value (patch.bank() + 1);
112         _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
113
114         get_vbox()->add (*t);
115
116         add_button (Stock::CANCEL, RESPONSE_CANCEL);
117         add_button (ok, RESPONSE_ACCEPT);
118         if (allow_delete) {
119                 add_button (Stock::DELETE, RESPONSE_REJECT);
120         }
121         set_default_response (RESPONSE_ACCEPT);
122
123         fill_bank_combo ();
124         set_active_bank_combo ();
125         bank_combo_changed ();
126
127         _info.Changed.connect (_info_changed_connection, invalidator (*this), 
128                                boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
129
130         show_all ();
131 }
132
133 void
134 PatchChangeDialog::instrument_info_changed ()
135 {
136         _bank_combo.clear ();
137         _patch_combo.clear ();
138         fill_bank_combo ();
139         fill_patch_combo ();
140 }
141
142 Evoral::PatchChange<Evoral::MusicalTime>
143 PatchChangeDialog::patch () const
144 {
145         Evoral::MusicalTime t = 0;
146
147         if (_time_converter) {
148                 t = _time_converter->from (_time.current_time ());
149         }
150
151         return Evoral::PatchChange<Evoral::MusicalTime> (
152                 t,
153                 _channel.get_value_as_int() - 1,
154                 _program.get_value_as_int() - 1,
155                 _bank.get_value_as_int() - 1
156                 );
157 }
158
159 /** Fill the bank_combo according to the current _channel */
160 void
161 PatchChangeDialog::fill_bank_combo ()
162 {
163         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
164
165         if (!cns) {
166                 return;
167         }
168
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);
173         }
174 }
175
176 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
177 void
178 PatchChangeDialog::set_active_bank_combo ()
179 {
180         _current_patch_bank.reset ();
181         
182         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
183
184         if (!cns) {
185                 return;
186         }
187
188         for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
189
190                 string n = (*i)->name ();
191                 boost::replace_all (n, "_", " ");
192
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;
198                         return;
199                 }
200         }
201
202         _ignore_signals = true;
203         _bank_combo.set_active (-1);
204         _ignore_signals = false;
205 }
206
207 /** Update _current_patch_bank and reflect the current value of
208  *  bank_combo in the rest of the dialog.
209  */
210 void
211 PatchChangeDialog::bank_combo_changed ()
212 {
213         if (_ignore_signals) {
214                 return;
215         }
216         
217         _current_patch_bank.reset ();
218
219         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
220
221         if (!cns) {
222                 return;
223         }
224
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;
230                 }
231         }
232
233         if (_current_patch_bank == 0) {
234                 return;
235         }
236
237         /* Reflect */
238
239         fill_patch_combo ();
240         set_active_patch_combo ();
241
242         _ignore_signals = true;
243         _bank.set_value (_current_patch_bank->number() + 1);
244         _ignore_signals = false;
245 }
246
247 /** Fill the contents of the patch combo */
248 void
249 PatchChangeDialog::fill_patch_combo ()
250 {
251         _patch_combo.clear ();
252
253         if (_current_patch_bank == 0) {
254                 return;
255         }
256
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);
262         }
263 }
264
265 /** Set the active value of the patch combo from the value of the _program entry */
266 void
267 PatchChangeDialog::set_active_patch_combo ()
268 {
269         if (_ignore_signals) {
270                 return;
271         }
272
273         if (_current_patch_bank == 0) {
274                 _ignore_signals = true;
275                 _patch_combo.set_active (-1);
276                 _ignore_signals = false;
277                 return;
278         }
279         
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, "_", " ");
284
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;
290                         return;
291                 }
292         }
293
294         _ignore_signals = true;
295         _patch_combo.set_active (-1);
296         _ignore_signals = false;
297 }       
298
299 /** Set _program from the current state of _patch_combo */
300 void
301 PatchChangeDialog::patch_combo_changed ()
302 {
303         if (_ignore_signals || _current_patch_bank == 0) {
304                 return;
305         }
306
307         const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
308
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, "_", " ");
312
313                 if (n == _patch_combo.get_active_text ()) {
314                         _ignore_signals = true;
315                         _program.set_value ((*j)->program_number() + 1);
316                         _ignore_signals = false;
317                         break;
318                 }
319         }
320 }
321
322 void
323 PatchChangeDialog::channel_changed ()
324 {
325         fill_bank_combo ();
326         set_active_bank_combo ();
327         fill_patch_combo ();
328         set_active_patch_combo ();
329 }
330
331 void
332 PatchChangeDialog::program_changed ()
333 {
334         if (_ignore_signals) {
335                 return;
336         }
337
338         set_active_patch_combo ();
339 }
340
341 void
342 PatchChangeDialog::bank_changed ()
343 {
344         if (_ignore_signals) {
345                 return;
346         }
347
348         set_active_bank_combo ();
349         fill_patch_combo ();
350         set_active_patch_combo ();
351 }
352