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