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