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