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