fix #5424: routes created from templates do not get names based on the user-supplied...
[ardour.git] / gtk2_ardour / add_route_dialog.cc
1 /*
2   Copyright (C) 2003 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstdio>
21 #include <cmath>
22
23 #include <sigc++/bind.h>
24 #include <gtkmm/stock.h>
25 #include <gtkmm/messagedialog.h>
26 #include <gtkmm/separator.h>
27 #include <gtkmm/table.h>
28
29 #include "pbd/error.h"
30 #include "pbd/convert.h"
31 #include "gtkmm2ext/utils.h"
32
33 #include "ardour/plugin_manager.h"
34 #include "ardour/profile.h"
35 #include "ardour/template_utils.h"
36 #include "ardour/route_group.h"
37 #include "ardour/session.h"
38
39 #include "utils.h"
40 #include "add_route_dialog.h"
41 #include "route_group_dialog.h"
42 #include "i18n.h"
43
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46 using namespace std;
47 using namespace PBD;
48 using namespace ARDOUR;
49
50 std::vector<std::string> AddRouteDialog::channel_combo_strings;
51
52 AddRouteDialog::AddRouteDialog (Session* s)
53         : ArdourDialog (_("Add Track or Bus"))
54         , routes_adjustment (1, 1, 128, 1, 4)
55         , routes_spinner (routes_adjustment)
56         , configuration_label (_("Configuration:"))
57         , mode_label (_("Track mode:"))
58         , instrument_label (_("Instrument:"))
59 {
60         set_session (s);
61
62         set_name ("AddRouteDialog");
63         set_position (Gtk::WIN_POS_MOUSE);
64         set_modal (true);
65         set_skip_taskbar_hint (true);
66         set_resizable (false);
67
68         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
69         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
70         channel_combo.set_name (X_("ChannelCountSelector"));
71         mode_combo.set_name (X_("ChannelCountSelector"));
72
73         refill_channel_setups ();
74         refill_route_groups ();
75         refill_track_modes ();
76
77         channel_combo.set_active_text (channel_combo_strings.front());
78
79         track_bus_combo.append_text (_("Audio Tracks"));
80         track_bus_combo.append_text (_("MIDI Tracks"));
81         track_bus_combo.append_text (_("Audio+MIDI Tracks"));
82         track_bus_combo.append_text (_("Busses"));
83         track_bus_combo.set_active (0);
84
85         build_instrument_list ();
86         instrument_combo.set_model (instrument_list);
87         instrument_combo.pack_start (instrument_list_columns.name);
88         instrument_combo.set_active (0);
89         instrument_combo.set_button_sensitivity (Gtk::SENSITIVITY_AUTO);
90
91         VBox* vbox = manage (new VBox);
92         Gtk::Label* l;
93
94         get_vbox()->set_spacing (4);
95
96         vbox->set_spacing (18);
97         vbox->set_border_width (5);
98
99         HBox *type_hbox = manage (new HBox);
100         type_hbox->set_spacing (6);
101
102         /* track/bus choice */
103
104         type_hbox->pack_start (*manage (new Label (_("Add:"))));
105         type_hbox->pack_start (routes_spinner);
106         type_hbox->pack_start (track_bus_combo);
107
108         vbox->pack_start (*type_hbox, false, true);
109
110         VBox* options_box = manage (new VBox);
111         Table *table2 = manage (new Table (3, 3, false));
112
113         options_box->set_spacing (6);
114         table2->set_row_spacings (6);
115         table2->set_col_spacing (1, 6);
116
117         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
118         l->set_use_markup ();
119         options_box->pack_start (*l, false, true);
120
121         l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
122         l->set_padding (8, 0);
123         table2->attach (*l, 0, 1, 0, 3, Gtk::FILL, Gtk::FILL, 0, 0);
124
125         int n = 0;
126
127         l = manage (new Label (_("Name:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
128         table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
129         table2->attach (name_template_entry, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
130         ++n;
131
132         /* Route configuration */
133
134         configuration_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
135         table2->attach (configuration_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
136         table2->attach (channel_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
137         ++n;
138
139         if (!ARDOUR::Profile->get_sae ()) {
140
141                 /* Track mode */
142
143                 mode_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
144                 table2->attach (mode_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
145                 table2->attach (mode_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
146                 ++n;
147
148         }
149
150         instrument_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
151         table2->attach (instrument_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
152         table2->attach (instrument_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
153         ++n;
154
155         /* Group choice */
156
157         l = manage (new Label (_("Group:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
158         table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
159         table2->attach (route_group_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
160         ++n;
161
162         options_box->pack_start (*table2, false, true);
163         vbox->pack_start (*options_box, false, true);
164
165         get_vbox()->pack_start (*vbox, false, false);
166
167         track_bus_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::track_type_chosen));
168         channel_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::channel_combo_changed));
169         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
170         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
171         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
172
173         show_all_children ();
174
175         /* track template info will be managed whenever
176            this dialog is shown, via ::on_show()
177         */
178
179         add_button (Stock::CANCEL, RESPONSE_CANCEL);
180         add_button (Stock::ADD, RESPONSE_ACCEPT);
181
182         track_type_chosen ();
183 }
184
185 AddRouteDialog::~AddRouteDialog ()
186 {
187 }
188
189 void
190 AddRouteDialog::channel_combo_changed ()
191 {
192         maybe_update_name_template_entry ();
193         refill_track_modes ();
194 }
195
196 AddRouteDialog::TypeWanted
197 AddRouteDialog::type_wanted() const
198 {
199         switch (track_bus_combo.get_active_row_number ()) {
200         case 0:
201                 return AudioTrack;
202         case 1:
203                 return MidiTrack;
204         case 2:
205                 return MixedTrack;
206         default:
207                 break;
208         }
209
210         return AudioBus;
211 }
212
213 void
214 AddRouteDialog::maybe_update_name_template_entry ()
215 {
216         if (
217                 name_template_entry.get_text() != "" &&
218                 name_template_entry.get_text() != _("Audio") &&
219                 name_template_entry.get_text() != _("MIDI")  &&
220                 name_template_entry.get_text() != _("Audio+MIDI")  &&
221                 name_template_entry.get_text() != _("Bus")) {
222                 return;
223         }
224
225         switch (type_wanted()) {
226         case AudioTrack:
227                 name_template_entry.set_text (_("Audio"));
228                 break;
229         case MidiTrack:
230                 name_template_entry.set_text (_("MIDI"));
231                 break;
232         case MixedTrack:
233                 name_template_entry.set_text (_("Audio+MIDI"));
234                 break;
235         case AudioBus:
236                 name_template_entry.set_text (_("Bus"));
237                 break;
238         }
239 }
240
241 void
242 AddRouteDialog::track_type_chosen ()
243 {
244         switch (type_wanted()) {
245         case AudioTrack:
246                 mode_combo.set_sensitive (true);
247                 channel_combo.set_sensitive (true);
248                 instrument_combo.set_sensitive (false);
249                 configuration_label.set_sensitive (true);
250                 mode_label.set_sensitive (true);
251                 instrument_label.set_sensitive (false);
252                 break;
253         case MidiTrack:
254                 channel_combo.set_sensitive (false);
255                 mode_combo.set_sensitive (false);
256                 instrument_combo.set_sensitive (true);
257                 configuration_label.set_sensitive (false);
258                 mode_label.set_sensitive (false);
259                 instrument_label.set_sensitive (true);
260                 break;
261         case MixedTrack:
262                 {
263                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
264                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
265                                            true, MESSAGE_INFO, BUTTONS_OK, true);
266                         msg.set_position (WIN_POS_MOUSE);
267                         msg.run ();
268                 }
269                 channel_combo.set_sensitive (true);
270                 mode_combo.set_sensitive (true);
271                 instrument_combo.set_sensitive (true);
272                 configuration_label.set_sensitive (true);
273                 mode_label.set_sensitive (true);
274                 instrument_label.set_sensitive (true);
275                 break;
276         case AudioBus:
277                 mode_combo.set_sensitive (false);
278                 channel_combo.set_sensitive (true);
279                 instrument_combo.set_sensitive (false);
280                 configuration_label.set_sensitive (true);
281                 mode_label.set_sensitive (true);
282                 instrument_label.set_sensitive (false);
283                 break;
284         }
285
286         maybe_update_name_template_entry ();
287 }
288
289
290 string
291 AddRouteDialog::name_template () const
292 {
293         return name_template_entry.get_text ();
294 }
295
296 bool
297 AddRouteDialog::name_template_is_default() const
298 {
299         string n = name_template();
300
301         if (n == _("Audio") ||
302             n == _("MIDI") ||
303             n == _("Audio+MIDI") ||
304             n == _("Bus")) {
305                 return true;
306         }
307
308         return false;
309 }
310
311 int
312 AddRouteDialog::count ()
313 {
314         return (int) floor (routes_adjustment.get_value ());
315 }
316
317 void
318 AddRouteDialog::refill_track_modes ()
319 {
320         vector<string> s;
321         
322         s.push_back (_("Normal"));
323
324         if (!ARDOUR::Profile->get_sae ()) {
325                 s.push_back (_("Non Layered"));
326                 s.push_back (_("Tape"));
327         }
328
329         set_popdown_strings (mode_combo, s);
330         mode_combo.set_active_text (s.front());
331 }
332
333 ARDOUR::TrackMode
334 AddRouteDialog::mode ()
335 {
336         if (ARDOUR::Profile->get_sae()) {
337                 return ARDOUR::Normal;
338         }
339
340         std::string str = mode_combo.get_active_text();
341         if (str == _("Normal")) {
342                 return ARDOUR::Normal;
343         } else if (str == _("Non Layered")){
344                 return ARDOUR::NonLayered;
345         } else if (str == _("Tape")) {
346                 return ARDOUR::Destructive;
347         } else {
348                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
349                       << endmsg;
350                 /*NOTREACHED*/
351         }
352         /* keep gcc happy */
353         return ARDOUR::Normal;
354 }
355
356 ChanCount
357 AddRouteDialog::channels ()
358 {
359         ChanCount ret;
360         string str;
361         switch (type_wanted()) {
362         case AudioTrack:
363         case AudioBus:
364                 str = channel_combo.get_active_text();
365                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
366                         if (str == (*i).name) {
367                                 ret.set (DataType::AUDIO, (*i).channels);
368                                 break;
369                         }
370                 }
371                 ret.set (DataType::MIDI, 0);
372                 break;
373
374         case MidiTrack:
375                 ret.set (DataType::AUDIO, 0);
376                 ret.set (DataType::MIDI, 1);
377                 break;
378
379         case MixedTrack:
380                 str = channel_combo.get_active_text();
381                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
382                         if (str == (*i).name) {
383                                 ret.set (DataType::AUDIO, (*i).channels);
384                                 break;
385                         }
386                 }
387                 ret.set (DataType::MIDI, 1);
388                 break;
389         }
390                 
391         return ret;
392 }
393
394 string
395 AddRouteDialog::track_template ()
396 {
397         string str = channel_combo.get_active_text();
398
399         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
400                 if (str == (*i).name) {
401                         return (*i).template_path;
402                 }
403         }
404
405         return string();
406 }
407
408 void
409 AddRouteDialog::on_show ()
410 {
411         refill_channel_setups ();
412         refill_route_groups ();
413
414         Dialog::on_show ();
415 }
416
417 void
418 AddRouteDialog::refill_channel_setups ()
419 {
420         ChannelSetup chn;
421
422         route_templates.clear ();
423         channel_combo_strings.clear ();
424         channel_setups.clear ();
425
426         chn.name = _("Mono");
427         chn.channels = 1;
428         channel_setups.push_back (chn);
429
430         chn.name = _("Stereo");
431         chn.channels = 2;
432         channel_setups.push_back (chn);
433
434         chn.name = "separator";
435         channel_setups.push_back (chn);
436
437         ARDOUR::find_route_templates (route_templates);
438
439         if (!ARDOUR::Profile->get_sae()) {
440                 if (!route_templates.empty()) {
441                         vector<string> v;
442                         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
443                                 chn.name = x->name;
444                                 chn.channels = 0;
445                                 chn.template_path = x->path;
446                                 channel_setups.push_back (chn);
447                         }
448                 }
449
450                 /* clear template path for the rest */
451
452                 chn.template_path = "";
453
454                 chn.name = _("3 Channel");
455                 chn.channels = 3;
456                 channel_setups.push_back (chn);
457
458                 chn.name = _("4 Channel");
459                 chn.channels = 4;
460                 channel_setups.push_back (chn);
461
462                 chn.name = _("5 Channel");
463                 chn.channels = 5;
464                 channel_setups.push_back (chn);
465
466                 chn.name = _("6 Channel");
467                 chn.channels = 6;
468                 channel_setups.push_back (chn);
469
470                 chn.name = _("8 Channel");
471                 chn.channels = 8;
472                 channel_setups.push_back (chn);
473
474                 chn.name = _("12 Channel");
475                 chn.channels = 12;
476                 channel_setups.push_back (chn);
477
478                 chn.name = _("Custom");
479                 chn.channels = 0;
480                 channel_setups.push_back (chn);
481         }
482
483         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
484                 channel_combo_strings.push_back ((*i).name);
485         }
486
487         set_popdown_strings (channel_combo, channel_combo_strings);
488         channel_combo.set_active_text (channel_combo_strings.front());
489 }
490
491 void
492 AddRouteDialog::add_route_group (RouteGroup* g)
493 {
494         route_group_combo.insert_text (3, g->name ());
495 }
496
497 RouteGroup*
498 AddRouteDialog::route_group ()
499 {
500         if (route_group_combo.get_active_row_number () == 2) {
501                 return 0;
502         }
503
504         return _session->route_group_by_name (route_group_combo.get_active_text());
505 }
506
507 void
508 AddRouteDialog::refill_route_groups ()
509 {
510         route_group_combo.clear ();
511         route_group_combo.append_text (_("New Group..."));
512
513         route_group_combo.append_text ("separator");
514
515         route_group_combo.append_text (_("No Group"));
516
517         _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
518
519         route_group_combo.set_active (2);
520 }
521
522 void
523 AddRouteDialog::group_changed ()
524 {
525         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
526                 RouteGroup* g = new RouteGroup (*_session, "");
527
528                 PropertyList plist;
529                 plist.add (Properties::active, true);
530                 g->apply_changes (plist);
531
532                 RouteGroupDialog d (g, true);
533
534                 if (d.do_run ()) {
535                         delete g;
536                         route_group_combo.set_active (2);
537                 } else {
538                         _session->add_route_group (g);
539                         add_route_group (g);
540                         route_group_combo.set_active (3);
541                 }
542         }
543 }
544
545 bool
546 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
547 {
548         channel_combo.set_active (i);
549
550         return channel_combo.get_active_text () == "separator";
551 }
552
553 bool
554 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
555 {
556         route_group_combo.set_active (i);
557
558         return route_group_combo.get_active_text () == "separator";
559 }
560
561 void
562 AddRouteDialog::build_instrument_list ()
563 {
564         PluginInfoList all_plugs;
565         PluginManager& manager (PluginManager::instance());
566         TreeModel::Row row;
567
568         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
569 #ifdef WINDOWS_VST_SUPPORT
570         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
571 #endif
572 #ifdef LXVST_SUPPORT
573         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
574 #endif
575 #ifdef AUDIOUNIT_SUPPORT
576         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
577 #endif
578 #ifdef LV2_SUPPORT
579         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
580 #endif
581
582
583         instrument_list = ListStore::create (instrument_list_columns);
584
585         row = *(instrument_list->append());
586         row[instrument_list_columns.info_ptr] = PluginInfoPtr ();
587         row[instrument_list_columns.name] = _("-none-");
588
589         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
590
591                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
592
593                 if ((*i)->is_instrument()) {
594                         row = *(instrument_list->append());
595                         row[instrument_list_columns.name] = (*i)->name;
596                         row[instrument_list_columns.info_ptr] = *i;
597                 }
598         }
599 }
600
601 PluginInfoPtr
602 AddRouteDialog::requested_instrument ()
603 {
604         TreeModel::iterator iter = instrument_combo.get_active ();
605         TreeModel::Row row;
606         
607         if (iter) {
608                 row = (*iter);
609                 return row[instrument_list_columns.info_ptr];
610         }
611
612         return PluginInfoPtr();
613 }