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