Put the sidechain ports into a dedicated tab in PortMatrix
[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
32 #include "gtkmm2ext/utils.h"
33 #include "gtkmm2ext/doi.h"
34
35 #include "widgets/tooltips.h"
36
37 #include "ardour/plugin_manager.h"
38 #include "ardour/profile.h"
39 #include "ardour/template_utils.h"
40 #include "ardour/route_group.h"
41 #include "ardour/session.h"
42 #include "ardour/vca.h"
43
44 #include "LuaBridge/LuaBridge.h"
45
46 #include "add_route_dialog.h"
47 #include "ardour_ui.h"
48 #include "route_group_dialog.h"
49 #include "utils.h"
50
51 #include "pbd/i18n.h"
52
53 using namespace Gtk;
54 using namespace Gtkmm2ext;
55 using namespace std;
56 using namespace PBD;
57 using namespace ARDOUR;
58 using namespace ARDOUR_UI_UTILS;
59
60 std::vector<std::string> AddRouteDialog::channel_combo_strings;
61 std::vector<std::pair<std::string,std::string> > AddRouteDialog::builtin_types;
62
63 AddRouteDialog::AddRouteDialog ()
64         : ArdourDialog (_("Add Track/Bus/VCA"))
65         , routes_adjustment (1, 1, 128, 1, 4)
66         , routes_spinner (routes_adjustment)
67         , configuration_label (_("Configuration:"))
68         , manual_label (_("Configuration:"))
69         , add_label (_("Add:"))
70         , name_label (_("Name:"))
71         , group_label (_("Group:"))
72         , insert_label (_("Position:"))
73         , strict_io_label (_("Pin Mode:"))
74         , mode_label (_("Record Mode:"))
75         , instrument_label (_("Instrument:"))
76         , name_edited_by_user (false)
77 {
78         set_name ("AddRouteDialog");
79         set_skip_taskbar_hint (true);
80         set_resizable (false);
81         set_position (WIN_POS_MOUSE);
82
83         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
84         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
85         channel_combo.set_name (X_("ChannelCountSelector"));
86         mode_combo.set_name (X_("ChannelCountSelector"));
87
88         refill_track_modes ();
89
90         if (builtin_types.empty()) {
91                 builtin_types.push_back (
92                         std::pair<string,string>(_("Audio Tracks"),  _(" \
93 Use the settings, below, to create 1 or more new Audio tracks.\n \
94 \n\n \
95 You may select:\n \
96 * The number of tracks to add.\n \
97 * A Name for the new track(s).\n \
98 * Mono, Stereo, or Multichannel operation for the new track(s).\n \
99 * A Group which the track will be assigned to.\n \
100 * The Pin Connections mode. (see tooltip for details).\n \
101 * Normal (non-destructive) or Tape (destructive) recording mode.\n \
102 \n \
103 The track will be added in the location specified by \"Position\".\n \
104 ")
105                 ));
106                 builtin_types.push_back (
107                         std::pair<string,string>(_("MIDI Tracks"),  _(" \
108 Use the settings, below, to create 1 or more new MIDI tracks.\n \
109 \n\n \
110 You may select:\n \
111 * The number of tracks to add.\n \
112 * A Name for the track(s).\n \
113 * An Instrument plugin (or select \"None\" to drive an external device)\n \
114 * A Group which the track will be assigned to.\n \
115 * The Pin Connections mode. (see tooltip for details)\n \
116 \n \
117 The track will be added in the location specified by \"Position\".\n \
118 ")
119                 ));
120                 builtin_types.push_back (
121                         std::pair<string,string>(_("Audio+MIDI Tracks"),   _(" \
122 Use the settings, below, to create 1 or more new Audio+MIDI tracks.\n \
123 \n\n \
124 You may select:\n \
125 * The number of tracks to add.\n \
126 * A Name for the track(s).\n \
127 * An Instrument plugin (or select \"None\" to drive an external device)\n \
128 * A Group which will be assigned to the track(s).\n \
129 * Pin Connections mode. (see tooltip for details).\n \
130 * Normal (non-destructive) or Tape (destructive) recording mode.\n \
131 \n \
132 The track will be added in the location specified by \"Position\".\n \
133 ")
134                 ));
135                 builtin_types.push_back (
136                         std::pair<string,string>(_("Audio Busses"),  _(" \
137 Use the settings, below, to create new Audio Tracks.\n \
138 \n\n \
139 You may select:\n \
140 * The number of buses to add.\n \
141 * A Name for the track(s).\n \
142 * An Instrument plugin (or select \"None\" to drive an external device)\n \
143 * A Group which will be assigned to the track(s).\n \
144 * Pin Connections mode. (see tooltip for details).\n \
145 * Normal (non-destructive) or Tape (destructive) recording mode.\n \
146 \n \
147 The track will be added in the location specified by \"Position\".\n \
148 ")
149                 ));
150                 builtin_types.push_back (
151                         std::pair<string,string>(_("MIDI Busses"),  _(" \
152 Use the settings, below, to create new MIDI Busses.\n \
153 \n \
154 MIDI Busses can combine the output of multiple tracks. \n \
155 MIDI Buses are sometimes used to host a single \"heavy\" instrument plugin which is fed from multiple MIDI tracks.  \
156 \n\n \
157 You may select:\n \
158 * The number of buses to add.\n \
159 * A Name for the track(s).\n \
160 * An Instrument plugin (or select \"None\" to drive an external device)\n \
161 * A Group which will be assigned to the track(s).\n \
162 * Pin Connections mode. (see tooltip for details).\n \
163 \n \
164 The track will be added in the location specified by \"Position\".\n \
165 ")
166                 ));
167                 builtin_types.push_back (
168                         std::pair<string,string>(_("VCA Masters"),   _(" \
169 Use the settings, below, to create 1 or more VCA Master(s).\n \
170 \n\n \
171 You may select:\n \
172 * The number of buses to add.\n \
173 * A name for the new VCAs.  \"%n\" will be replaced by an index number for each VCA.\n \
174 ")
175                 ));
176         }
177
178         insert_at_combo.append_text (_("First"));
179         insert_at_combo.append_text (_("Before Selection"));
180         insert_at_combo.append_text (_("After Selection"));
181         insert_at_combo.append_text (_("Last"));
182         insert_at_combo.set_active (3);
183
184         strict_io_combo.append_text (_("Flexible-I/O"));
185         strict_io_combo.append_text (_("Strict-I/O"));
186         strict_io_combo.set_active (Config->get_strict_io () ? 1 : 0);
187
188         /* top-level VBox */
189         VBox* vbox = manage (new VBox);
190         get_vbox()->set_spacing (4);
191         vbox->set_spacing (18);
192         vbox->set_border_width (5);
193
194         /* this box contains the template chooser, and the template details */
195         HBox* template_hbox = manage (new HBox);
196         template_hbox->set_spacing (8);
197
198         /* scrollbars for the template chooser and template descriptions.... */
199         Gtk::ScrolledWindow *template_scroller = manage (new Gtk::ScrolledWindow());
200         template_scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
201         template_scroller->add (trk_template_chooser);
202
203         Gtk::ScrolledWindow *desc_scroller = manage (new Gtk::ScrolledWindow());
204         desc_scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
205         desc_scroller->add (trk_template_desc);
206
207         /* this is the outer sample that surrounds the description and the settings-table */
208         trk_template_outer_frame.set_name (X_("TextHighlightFrame"));
209
210         /* this is the "inner frame" that surrounds the description text */
211         trk_template_desc_frame.set_name (X_("TextHighlightFrame"));
212         trk_template_desc_frame.add (*desc_scroller);
213
214         /* template_chooser is the treeview showing available templates */
215         trk_template_model = TreeStore::create (track_template_columns);
216         trk_template_chooser.set_model (trk_template_model);
217         trk_template_chooser.append_column (_("Template/Type"), track_template_columns.name);
218 #ifdef MIXBUS
219         trk_template_chooser.append_column (_("Modified With"), track_template_columns.modified_with);
220 #endif
221         trk_template_chooser.set_headers_visible (true);
222         trk_template_chooser.get_selection()->set_mode (SELECTION_SINGLE);
223         trk_template_chooser.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::trk_template_row_selected));
224         trk_template_chooser.set_sensitive (true);
225
226         /* template_desc is the textview that displays the currently selected template's description */
227         trk_template_desc.set_editable (false);
228         trk_template_desc.set_can_focus (false);
229         trk_template_desc.set_wrap_mode (Gtk::WRAP_WORD);
230         trk_template_desc.set_size_request(400,200);
231         trk_template_desc.set_name (X_("TextOnBackground"));
232         trk_template_desc.set_border_width (6);
233
234         Table *settings_table = manage (new Table (2, 6, false));
235         settings_table->set_row_spacings (8);
236         settings_table->set_col_spacings        (4);
237         settings_table->set_col_spacing (3, 20);
238         settings_table->set_border_width        (12);
239
240         VBox* settings_vbox = manage (new VBox);
241         settings_vbox->pack_start(trk_template_desc_frame , true, true);
242         settings_vbox->pack_start(*settings_table , true, true);
243         settings_vbox->set_border_width (4);
244
245         trk_template_outer_frame.add (*settings_vbox);
246
247         template_hbox->pack_start (*template_scroller, true, true);
248         template_hbox->pack_start (trk_template_outer_frame, true, true);
249
250         vbox->pack_start (*template_hbox, true, true);
251
252
253         /* Now pack the "settings table" with manual controls (these controls are sensitized by the left-side selection) */
254
255         int n = 0;
256
257         HBox *separator_hbox = manage (new HBox);
258         separator_hbox->pack_start (manual_label, false, false);
259         separator_hbox->pack_start (*(manage (new Gtk::HSeparator)), true, true);
260         separator_hbox->set_spacing (6);
261         settings_table->attach (*separator_hbox, 0, 6, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
262
263         ++n;
264
265         /* Number */
266         add_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
267         settings_table->attach (add_label, 0, 1, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
268         Gtk::Alignment *align = manage (new Alignment (0, .5, 0, 0));
269         align->add (routes_spinner);
270         settings_table->attach (*align, 1, 2, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
271
272         ++n;
273
274         /* Name */
275         name_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
276         settings_table->attach (name_label, 0, 1, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
277         settings_table->attach (name_template_entry, 1, 3, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
278
279         /* Route configuration */
280         configuration_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
281         settings_table->attach (configuration_label, 4, 5, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
282         settings_table->attach (channel_combo, 5, 6, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
283
284         ++n;
285
286         /* instrument choice (for MIDI) */
287         instrument_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
288         settings_table->attach (instrument_label, 0, 1, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
289         settings_table->attach (instrument_combo, 1, 3, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
290
291         /* Group choice */
292         group_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
293         settings_table->attach (group_label, 4, 5, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
294         settings_table->attach (route_group_combo, 5, 6, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
295
296         ++n;
297
298         /* New Route's I/O is.. {strict/flexible} */
299         if (Profile->get_mixbus ()) {
300                 strict_io_combo.set_active (1);
301         } else {
302                 strict_io_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
303                 settings_table->attach (strict_io_label, 0, 1, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
304                 settings_table->attach (strict_io_combo, 1, 3, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
305
306                 ArdourWidgets::set_tooltip (strict_io_combo,
307                                 _("With strict-i/o enabled, Effect Processors will not modify the number of channels on a track. The number of output channels will always match the number of input channels."));
308
309                 /* recording mode */
310                 mode_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
311                 settings_table->attach (mode_label, 4, 5, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
312                 settings_table->attach (mode_combo, 5, 6, n, n + 1, Gtk::FILL, Gtk::SHRINK, 0, 0);
313
314                 ++n;
315         }
316
317         HBox* outer_box = manage (new HBox);
318         outer_box->set_spacing (4);
319
320         /* New route will be inserted at.. */
321         insert_label.set_alignment (Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER);
322         outer_box->pack_start (insert_label, false, false);
323         outer_box->pack_start (insert_at_combo, false, false);
324
325         /* quick-add button (add item but don't close dialog) */
326         Gtk::Button* addnoclose_button = manage (new Gtk::Button(_("Add selected items (and leave dialog open)")));
327         addnoclose_button->set_can_default ();
328         addnoclose_button->signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), Add));
329         outer_box->pack_end (*addnoclose_button, false, false);
330
331         vbox->pack_start (*outer_box, true, true);
332
333         get_vbox()->pack_start (*vbox, false, false);
334
335         name_template_entry.signal_insert_text ().connect (sigc::mem_fun (*this, &AddRouteDialog::name_template_entry_insertion));
336         name_template_entry.signal_delete_text ().connect (sigc::mem_fun (*this, &AddRouteDialog::name_template_entry_deletion));
337         channel_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::channel_combo_changed));
338         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
339         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
340         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
341
342         routes_spinner.signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), AddAndClose));
343         name_template_entry.signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), AddAndClose));
344         trk_template_chooser.signal_row_activated ().connect (sigc::hide (sigc::hide (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), AddAndClose))));
345
346         show_all_children ();
347
348         /* track template info will be managed whenever
349          * this dialog is shown, via ::on_show()
350          */
351
352         add_button (_("Add and Close"), AddAndClose);
353         set_response_sensitive (AddAndClose, true);
354         set_default_response (AddAndClose);
355
356         refill_channel_setups ();
357 }
358
359 AddRouteDialog::~AddRouteDialog ()
360 {
361 }
362
363 void
364 AddRouteDialog::on_response (int r)
365 {
366         reset_name_edited ();
367         /* Don't call ArdourDialog::on_response() because that will
368            automatically hide the dialog.
369         */
370         Gtk::Dialog::on_response (r);
371 }
372
373 void
374 AddRouteDialog::trk_template_row_selected ()
375 {
376         if (trk_template_chooser.get_selection()->count_selected_rows() != 1) {
377                 return;
378         }
379
380         TreeIter iter = trk_template_chooser.get_selection ()->get_selected ();
381         assert (iter);
382
383         string d = (*iter)[track_template_columns.description];
384         trk_template_desc.get_buffer ()->set_text (d);
385
386         const string n = (*iter)[track_template_columns.name];
387         const string p = (*iter)[track_template_columns.path];
388
389         if (p.substr (0, 11) == "urn:ardour:") {
390                 /* lua script - meta-template */
391                 const std::map<std::string, std::string> rs (ARDOUR_UI::instance()->route_setup_info (p.substr (11)));
392
393                 trk_template_desc.set_sensitive (true);
394
395                 add_label.set_sensitive (rs.find ("how_many") != rs.end ());
396                 name_label.set_sensitive (rs.find ("name") != rs.end());
397                 group_label.set_sensitive (rs.find ("group") != rs.end());
398                 configuration_label.set_sensitive (rs.find ("channels") != rs.end ());
399                 mode_label.set_sensitive (rs.find ("track_mode") != rs.end ());
400                 instrument_label.set_sensitive (rs.find ("instrument") != rs.end ());
401                 strict_io_label.set_sensitive (rs.find ("strict_io") != rs.end());
402
403                 routes_spinner.set_sensitive (rs.find ("how_many") != rs.end ());
404                 name_template_entry.set_sensitive (rs.find ("name") != rs.end ());
405                 route_group_combo.set_sensitive (rs.find ("group") != rs.end());
406                 channel_combo.set_sensitive (rs.find ("channels") != rs.end ());
407                 mode_combo.set_sensitive (rs.find ("track_mode") != rs.end ());
408                 instrument_combo.set_sensitive (rs.find ("instrument") != rs.end ());
409                 strict_io_combo.set_sensitive (rs.find ("strict_io") != rs.end());
410
411                 bool any_enabled = rs.find ("how_many") != rs.end ()
412                         || rs.find ("name") != rs.end ()
413                         || rs.find ("group") != rs.end()
414                         || rs.find ("channels") != rs.end ()
415                         || rs.find ("track_mode") != rs.end ()
416                         || rs.find ("instrument") != rs.end ()
417                         || rs.find ("strict_io") != rs.end();
418
419                 manual_label.set_sensitive (any_enabled);
420
421                 std::map<string,string>::const_iterator it;
422
423                 if ((it = rs.find ("name")) != rs.end()) {
424                         name_template_entry.set_text (it->second);
425                 } else {
426                         name_template_entry.set_text ("");
427                 }
428
429                 if ((it = rs.find ("how_many")) != rs.end()) {
430                         if (atoi (it->second.c_str()) > 0) {
431                                 routes_adjustment.set_value (atoi (it->second.c_str()));
432                         }
433                 }
434
435                 if ((it = rs.find ("track_mode")) != rs.end()) {
436                         switch ((ARDOUR::TrackMode) atoi (it->second.c_str())) {
437                                 case ARDOUR::Normal:
438                                         mode_combo.set_active_text (_("Normal"));
439                                         break;
440                                 case ARDOUR::Destructive:
441                                         if (!ARDOUR::Profile->get_mixbus ()) {
442                                                 mode_combo.set_active_text (_("Tape"));
443                                         }
444                                         break;
445                                 default: // "NonLayered" enum is still present for session-format compat
446                                         break;
447                         }
448                 }
449
450                 if ((it = rs.find ("strict_io")) != rs.end()) {
451                         if (it->second == X_("true")) {
452                                 strict_io_combo.set_active (1);
453                         } else if (it->second == X_("false")) {
454                                 strict_io_combo.set_active (0);
455                         }
456                 }
457
458                 if ((it = rs.find ("channels")) != rs.end()) {
459                         uint32_t channels = atoi (it->second.c_str());
460                         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
461                                 if ((*i).channels == channels) {
462                                         channel_combo.set_active_text ((*i).name);
463                                         break;
464                                 }
465                         }
466                 }
467
468         } else if (!p.empty ()) {
469                 /* user-template */
470                 trk_template_desc.set_sensitive (true);
471
472                 manual_label.set_sensitive (true);
473                 add_label.set_sensitive (true);
474                 name_label.set_sensitive (true);
475                 group_label.set_sensitive (false);
476                 strict_io_label.set_sensitive (false);
477                 configuration_label.set_sensitive (false);
478                 mode_label.set_sensitive (false);
479                 instrument_label.set_sensitive (false);
480
481                 routes_spinner.set_sensitive (true);
482                 name_template_entry.set_sensitive (true);
483                 channel_combo.set_sensitive (false);
484                 mode_combo.set_sensitive (false);
485                 instrument_combo.set_sensitive (false);
486                 strict_io_combo.set_sensitive (false);
487                 route_group_combo.set_sensitive (false);
488
489         } else {
490                 /* all manual mode */
491                 trk_template_desc.set_sensitive (false);
492
493                 manual_label.set_sensitive (true);
494                 add_label.set_sensitive (true);
495                 name_label.set_sensitive (true);
496                 group_label.set_sensitive (true);
497                 strict_io_label.set_sensitive (true);
498
499                 routes_spinner.set_sensitive (true);
500                 name_template_entry.set_sensitive (true);
501                 track_type_chosen ();
502         }
503 }
504
505
506 void
507 AddRouteDialog::name_template_entry_insertion (Glib::ustring const &,int*)
508 {
509         if (name_template ().empty ()) {
510                 name_edited_by_user = false;
511         } else {
512                 name_edited_by_user = true;
513         }
514 }
515
516 void
517 AddRouteDialog::name_template_entry_deletion (int, int)
518 {
519         if (name_template ().empty ()) {
520                 name_edited_by_user = false;
521         } else {
522                 name_edited_by_user = true;
523         }
524 }
525
526 void
527 AddRouteDialog::channel_combo_changed ()
528 {
529         refill_track_modes ();
530 }
531
532 std::string
533 AddRouteDialog::get_template_path ()
534 {
535         string p;
536
537         if (trk_template_chooser.get_selection()->count_selected_rows() > 0) {
538                 TreeIter iter = trk_template_chooser.get_selection()->get_selected();
539
540                 if (iter) {
541                         string n = (*iter)[track_template_columns.name];
542                         if (n != _("Manual Configuration")) {
543                                 p = (*iter)[track_template_columns.path];
544                         }
545                 }
546         }
547
548         return p;
549 }
550
551
552 AddRouteDialog::TypeWanted
553 AddRouteDialog::type_wanted()
554 {
555         if (trk_template_chooser.get_selection()->count_selected_rows() != 1) {
556                 return AudioTrack;
557         }
558         TreeIter iter = trk_template_chooser.get_selection ()->get_selected ();
559         assert (iter);
560
561         const string str = (*iter)[track_template_columns.name];
562         if (str == _("Audio Busses")) {
563                 return AudioBus;
564         } else if (str == _("MIDI Busses")){
565                 return MidiBus;
566         } else if (str == _("MIDI Tracks")){
567                 return MidiTrack;
568         } else if (str == _("Audio+MIDI Tracks")) {
569                 return MixedTrack;
570         } else if (str == _("Audio Tracks")) {
571                 return AudioTrack;
572         } else if (str == _("VCA Masters")) {
573                 return VCAMaster;
574         } else {
575                 assert (0);
576                 return AudioTrack;
577         }
578 }
579
580 void
581 AddRouteDialog::maybe_update_name_template_entry ()
582 {
583         if (name_edited_by_user) {
584                 return;
585         }
586
587         switch (type_wanted()) {
588         case AudioTrack:
589                 name_template_entry.set_text (_("Audio"));
590                 break;
591         case MidiTrack:
592                 name_template_entry.set_text (_("MIDI"));
593                 break;
594         case MixedTrack:
595                 name_template_entry.set_text (_("Audio+MIDI"));
596                 break;
597         case AudioBus:
598         case MidiBus:
599                 name_template_entry.set_text (_("Bus"));
600                 break;
601         case VCAMaster:
602                 name_template_entry.set_text (VCA::default_name_template());
603                 break;
604         }
605         /* ignore programatic change, restore false */
606         reset_name_edited ();
607 }
608
609 void
610 AddRouteDialog::track_type_chosen ()
611 {
612         switch (type_wanted()) {
613         case AudioTrack:
614
615                 configuration_label.set_sensitive (true);
616                 channel_combo.set_sensitive (true);
617
618                 mode_label.set_sensitive (true);
619                 mode_combo.set_sensitive (true);
620
621                 instrument_label.set_sensitive (false);
622                 instrument_combo.set_sensitive (false);
623
624                 group_label.set_sensitive (true);
625                 route_group_combo.set_sensitive (true);
626
627                 strict_io_label.set_sensitive (true);
628                 strict_io_combo.set_sensitive (true);
629
630                 insert_label.set_sensitive (true);
631                 insert_at_combo.set_sensitive (true);
632
633                 break;
634         case MidiTrack:
635
636                 configuration_label.set_sensitive (false);
637                 channel_combo.set_sensitive (false);
638
639                 mode_label.set_sensitive (false);
640                 mode_combo.set_sensitive (false);
641
642                 instrument_label.set_sensitive (true);
643                 instrument_combo.set_sensitive (true);
644
645                 group_label.set_sensitive (true);
646                 route_group_combo.set_sensitive (true);
647
648                 strict_io_label.set_sensitive (true);
649                 strict_io_combo.set_sensitive (true);
650
651                 insert_label.set_sensitive (true);
652                 insert_at_combo.set_sensitive (true);
653
654                 break;
655         case MixedTrack:
656                 {
657                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
658                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
659                                            true, MESSAGE_INFO, BUTTONS_OK, true);
660                         msg.set_position (WIN_POS_MOUSE);
661                         msg.run ();
662                 }
663
664                 configuration_label.set_sensitive (true);
665                 channel_combo.set_sensitive (true);
666
667                 mode_label.set_sensitive (true);
668                 mode_combo.set_sensitive (true);
669
670                 instrument_label.set_sensitive (true);
671                 instrument_combo.set_sensitive (true);
672
673                 group_label.set_sensitive (true);
674                 route_group_combo.set_sensitive (true);
675
676                 strict_io_label.set_sensitive (true);
677                 strict_io_combo.set_sensitive (true);
678
679                 insert_label.set_sensitive (true);
680                 insert_at_combo.set_sensitive (true);
681
682                 break;
683         case AudioBus:
684
685                 configuration_label.set_sensitive (true);
686                 channel_combo.set_sensitive (true);
687
688                 mode_label.set_sensitive (false);
689                 mode_combo.set_sensitive (false);
690
691                 instrument_label.set_sensitive (false);
692                 instrument_combo.set_sensitive (false);
693
694                 group_label.set_sensitive (true);
695                 route_group_combo.set_sensitive (true);
696
697                 strict_io_label.set_sensitive (true);
698                 strict_io_combo.set_sensitive (true);
699
700                 insert_label.set_sensitive (true);
701                 insert_at_combo.set_sensitive (true);
702
703                 break;
704         case VCAMaster:
705
706                 configuration_label.set_sensitive (false);
707                 channel_combo.set_sensitive (false);
708
709                 mode_label.set_sensitive (false);
710                 mode_combo.set_sensitive (false);
711
712                 instrument_label.set_sensitive (false);
713                 instrument_combo.set_sensitive (false);
714
715                 group_label.set_sensitive (false);
716                 route_group_combo.set_sensitive (false);
717
718                 strict_io_label.set_sensitive (false);
719                 strict_io_combo.set_sensitive (false);
720
721                 insert_label.set_sensitive (false);
722                 insert_at_combo.set_sensitive (false);
723
724                 break;
725         case MidiBus:
726
727                 configuration_label.set_sensitive (false);
728                 channel_combo.set_sensitive (false);
729
730                 mode_label.set_sensitive (false);
731                 mode_combo.set_sensitive (false);
732
733                 instrument_label.set_sensitive (true);
734                 instrument_combo.set_sensitive (true);
735
736                 group_label.set_sensitive (true);
737                 route_group_combo.set_sensitive (true);
738
739                 strict_io_label.set_sensitive (true);
740                 strict_io_combo.set_sensitive (true);
741
742                 insert_label.set_sensitive (true);
743                 insert_at_combo.set_sensitive (true);
744
745                 break;
746         }
747
748         maybe_update_name_template_entry ();
749 }
750
751 string
752 AddRouteDialog::name_template () const
753 {
754         return name_template_entry.get_text ();
755 }
756
757 bool
758 AddRouteDialog::name_template_is_default () const
759 {
760         string n = name_template();
761
762         if (n == _("Audio") ||
763             n == _("MIDI") ||
764             n == _("Audio+MIDI") ||
765             n == _("Bus") ||
766             n == VCA::default_name_template()) {
767                 return true;
768         }
769
770         return false;
771 }
772
773 int
774 AddRouteDialog::count ()
775 {
776         return (int) floor (routes_adjustment.get_value ());
777 }
778
779 void
780 AddRouteDialog::refill_track_modes ()
781 {
782         vector<string> s;
783
784         s.push_back (_("Normal"));
785         if (!ARDOUR::Profile->get_mixbus ()) {
786                 s.push_back (_("Tape"));
787         }
788
789         set_popdown_strings (mode_combo, s);
790         mode_combo.set_active_text (s.front());
791 }
792
793 ARDOUR::TrackMode
794 AddRouteDialog::mode ()
795 {
796         std::string str = mode_combo.get_active_text();
797         if (str == _("Normal")) {
798                 return ARDOUR::Normal;
799         } else if (str == _("Non Layered")){
800                 return ARDOUR::NonLayered;
801         } else if (str == _("Tape")) {
802                 return ARDOUR::Destructive;
803         } else {
804                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
805                       << endmsg;
806                 abort(); /*NOTREACHED*/
807         }
808         /* keep gcc happy */
809         return ARDOUR::Normal;
810 }
811
812 uint32_t
813 AddRouteDialog::channel_count ()
814 {
815         string str = channel_combo.get_active_text();
816         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
817                 if (str == (*i).name) {
818                         return (*i).channels;
819                 }
820         }
821         return 0;
822 }
823
824 ChanCount
825 AddRouteDialog::channels ()
826 {
827         ChanCount ret;
828         switch (type_wanted()) {
829         case AudioTrack:
830         case AudioBus:
831                 ret.set (DataType::AUDIO, channel_count ());
832                 ret.set (DataType::MIDI, 0);
833                 break;
834
835         case MidiBus:
836         case MidiTrack:
837                 ret.set (DataType::AUDIO, 0);
838                 ret.set (DataType::MIDI, 1);
839                 break;
840
841         case MixedTrack:
842                 ret.set (DataType::AUDIO, channel_count ());
843                 ret.set (DataType::MIDI, 1);
844                 break;
845         default:
846                 break;
847         }
848
849         return ret;
850 }
851
852 void
853 AddRouteDialog::on_show ()
854 {
855         routes_spinner.grab_focus ();
856         reset_name_edited ();
857
858         refill_route_groups ();
859         refill_channel_setups ();
860
861         Dialog::on_show ();
862 }
863
864 void
865 AddRouteDialog::refill_channel_setups ()
866 {
867         ChannelSetup chn;
868
869         string channel_current_choice = channel_combo.get_active_text();
870
871         channel_combo_strings.clear ();
872         channel_setups.clear ();
873
874         chn.name = _("Mono");
875         chn.channels = 1;
876         channel_setups.push_back (chn);
877
878         chn.name = _("Stereo");
879         chn.channels = 2;
880         channel_setups.push_back (chn);
881
882         if (!ARDOUR::Profile->get_mixbus()) {
883
884                 chn.name = "separator";
885                 channel_setups.push_back (chn);
886
887                 chn.name = _("3 Channel");
888                 chn.channels = 3;
889                 channel_setups.push_back (chn);
890
891                 chn.name = _("4 Channel");
892                 chn.channels = 4;
893                 channel_setups.push_back (chn);
894
895                 chn.name = _("5 Channel");
896                 chn.channels = 5;
897                 channel_setups.push_back (chn);
898
899                 chn.name = _("6 Channel");
900                 chn.channels = 6;
901                 channel_setups.push_back (chn);
902
903                 chn.name = _("8 Channel");
904                 chn.channels = 8;
905                 channel_setups.push_back (chn);
906
907                 chn.name = _("12 Channel");
908                 chn.channels = 12;
909                 channel_setups.push_back (chn);
910
911                 chn.name = _("Custom");
912                 chn.channels = 0;
913                 channel_setups.push_back (chn);
914         }
915
916         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
917                 channel_combo_strings.push_back ((*i).name);
918         }
919
920         trk_template_model->clear();
921         bool selected_default = false;
922
923         for (std::vector<std::pair<std::string,std::string> >::const_iterator i = builtin_types.begin(); i != builtin_types.end(); ++i) {
924                 TreeModel::Row row = *(trk_template_model->append ());
925                 row[track_template_columns.name] = (*i).first;
926                 row[track_template_columns.path] = "";
927                 row[track_template_columns.description] = (*i).second;
928                 row[track_template_columns.modified_with] = "";
929
930                 if (!selected_default && !Profile->get_mixbus ()) {
931                         trk_template_chooser.get_selection()->select(row);
932                         selected_default = true;
933                 }
934         }
935
936         /* Add any Lua scripts (factory templates) found in the scripts folder */
937         LuaScriptList& ms (LuaScripting::instance ().scripts (LuaScriptInfo::EditorAction));
938         for (LuaScriptList::const_iterator s = ms.begin(); s != ms.end(); ++s) {
939                 if (!((*s)->subtype & LuaScriptInfo::RouteSetup)) {
940                         continue;
941                 }
942                 TreeModel::Row row;
943                 if ((*s)->name == "Create Audio Tracks Interactively" && Profile->get_mixbus ()) {
944                         /* somewhat-special, Ben says: "most-used template" */
945                         row = *(trk_template_model->prepend ());
946                 } else {
947                         row = *(trk_template_model->append ());
948                 }
949                 row[track_template_columns.name] = (*s)->name;
950                 row[track_template_columns.path] = "urn:ardour:" + (*s)->path;
951                 row[track_template_columns.description] = (*s)->description;
952                 row[track_template_columns.modified_with] = _("{Factory Template}");
953
954                 if ((*s)->name == "Create Audio Tracks Interactively" && Profile->get_mixbus ()) {
955                         trk_template_chooser.get_selection()->select(row);
956                         selected_default = true;
957                 }
958         }
959
960         if (!selected_default && !trk_template_model->children().empty()) {
961                 TreeModel::Children rows = trk_template_model->children();
962                 trk_template_chooser.get_selection()->select(rows[0]);
963         }
964
965         std::vector<ARDOUR::TemplateInfo> route_templates;
966         ARDOUR::find_route_templates (route_templates);
967
968         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
969                 TreeModel::Row row = *(trk_template_model->append ());
970
971                 row[track_template_columns.name] = x->name;
972                 row[track_template_columns.path] = x->path;
973                 row[track_template_columns.description] = x->description;
974                 row[track_template_columns.modified_with] = x->modified_with;
975         }
976
977         set_popdown_strings (channel_combo, channel_combo_strings);
978
979         if (!channel_current_choice.empty()) {
980                 channel_combo.set_active_text (channel_current_choice);
981         } else {
982                 channel_combo.set_active_text (channel_combo_strings.front());
983         }
984 }
985
986 void
987 AddRouteDialog::add_route_group (RouteGroup* g)
988 {
989         route_group_combo.insert_text (3, g->name ());
990 }
991
992 RouteGroup*
993 AddRouteDialog::route_group ()
994 {
995         if (!_session || route_group_combo.get_active_row_number () == 2) {
996                 return 0;
997         }
998
999         return _session->route_group_by_name (route_group_combo.get_active_text());
1000 }
1001
1002 bool
1003 AddRouteDialog::use_strict_io() {
1004         return strict_io_combo.get_active_row_number () == 1;
1005 }
1006
1007 void
1008 AddRouteDialog::refill_route_groups ()
1009 {
1010         route_group_combo.clear ();
1011         route_group_combo.append_text (_("New Group..."));
1012
1013         route_group_combo.append_text ("separator");
1014
1015         route_group_combo.append_text (_("No Group"));
1016
1017         if (_session) {
1018                 _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
1019         }
1020
1021         route_group_combo.set_active (2);
1022 }
1023
1024 void
1025 AddRouteDialog::group_changed ()
1026 {
1027         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
1028                 RouteGroup* g = new RouteGroup (*_session, "");
1029                 RouteGroupDialog* d = new RouteGroupDialog (g, true);
1030
1031                 d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &AddRouteDialog::new_group_dialog_finished), d));
1032                 d->present();
1033         }
1034 }
1035
1036 void
1037 AddRouteDialog::new_group_dialog_finished (int r, RouteGroupDialog* d)
1038 {
1039         if (r == RESPONSE_OK) {
1040
1041                 if (!d->name_check()) {
1042                         return;
1043                 }
1044
1045                 if (_session) {
1046                         _session->add_route_group (d->group());
1047                 }
1048
1049                 add_route_group (d->group());
1050                 route_group_combo.set_active (3);
1051         } else {
1052                 delete d->group ();
1053                 route_group_combo.set_active (2);
1054         }
1055
1056         delete_when_idle (d);
1057 }
1058
1059 RouteDialogs::InsertAt
1060 AddRouteDialog::insert_at ()
1061 {
1062         using namespace RouteDialogs;
1063
1064         std::string str = insert_at_combo.get_active_text();
1065
1066         if (str == _("First")) {
1067                 return First;
1068         } else if (str == _("After Selection")) {
1069                 return AfterSelection;
1070         } else if (str == _("Before Selection")){
1071                 return BeforeSelection;
1072         }
1073         return Last;
1074 }
1075
1076 bool
1077 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
1078 {
1079         channel_combo.set_active (i);
1080
1081         return channel_combo.get_active_text () == "separator";
1082 }
1083
1084 bool
1085 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
1086 {
1087         route_group_combo.set_active (i);
1088
1089         return route_group_combo.get_active_text () == "separator";
1090 }
1091
1092 PluginInfoPtr
1093 AddRouteDialog::requested_instrument ()
1094 {
1095         return instrument_combo.selected_instrument();
1096 }