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