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