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