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