redesign Route and VCA objects to inherit from ARDOUR::Stripable
[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 #include "gtkmm2ext/utils.h"
32
33 #include "ardour/plugin_manager.h"
34 #include "ardour/profile.h"
35 #include "ardour/template_utils.h"
36 #include "ardour/route_group.h"
37 #include "ardour/session.h"
38 #include "ardour/vca.h"
39
40 #include "utils.h"
41 #include "add_route_dialog.h"
42 #include "route_group_dialog.h"
43 #include "tooltips.h"
44 #include "i18n.h"
45
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48 using namespace std;
49 using namespace PBD;
50 using namespace ARDOUR;
51 using namespace ARDOUR_UI_UTILS;
52
53 std::vector<std::string> AddRouteDialog::channel_combo_strings;
54
55 AddRouteDialog::AddRouteDialog ()
56         : ArdourDialog (_("Add Track/Bus/VCA"))
57         , routes_adjustment (1, 1, 128, 1, 4)
58         , routes_spinner (routes_adjustment)
59         , configuration_label (_("Configuration:"))
60         , mode_label (_("Record Mode:"))
61         , instrument_label (_("Instrument:"))
62 {
63         set_name ("AddRouteDialog");
64         set_modal (true);
65         set_skip_taskbar_hint (true);
66         set_resizable (false);
67
68         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
69         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
70         channel_combo.set_name (X_("ChannelCountSelector"));
71         mode_combo.set_name (X_("ChannelCountSelector"));
72
73         refill_channel_setups ();
74         refill_route_groups ();
75         refill_track_modes ();
76
77         channel_combo.set_active_text (channel_combo_strings.front());
78
79         track_bus_combo.append_text (_("Audio Tracks"));
80         track_bus_combo.append_text (_("MIDI Tracks"));
81         track_bus_combo.append_text (_("Audio+MIDI Tracks"));
82         track_bus_combo.append_text (_("Audio Busses"));
83         track_bus_combo.append_text (_("MIDI Busses"));
84         track_bus_combo.append_text (_("VCA Masters"));
85         track_bus_combo.set_active (0);
86
87         insert_at_combo.append_text (_("First"));
88         insert_at_combo.append_text (_("Before Selection"));
89         insert_at_combo.append_text (_("After Selection"));
90         insert_at_combo.append_text (_("Last"));
91         insert_at_combo.set_active (3);
92
93         strict_io_combo.append_text (_("Flexible-I/O"));
94         strict_io_combo.append_text (_("Strict-I/O"));
95         strict_io_combo.set_active (Config->get_strict_io () ? 1 : 0);
96
97         VBox* vbox = manage (new VBox);
98         Gtk::Label* l;
99
100         get_vbox()->set_spacing (4);
101
102         vbox->set_spacing (18);
103         vbox->set_border_width (5);
104
105         HBox *type_hbox = manage (new HBox);
106         type_hbox->set_spacing (6);
107
108         /* track/bus choice */
109
110         type_hbox->pack_start (*manage (new Label (_("Add:"))));
111         type_hbox->pack_start (routes_spinner);
112         type_hbox->pack_start (track_bus_combo);
113
114         vbox->pack_start (*type_hbox, false, true);
115
116         VBox* options_box = manage (new VBox);
117         Table *table2 = manage (new Table (3, 3, false));
118
119         options_box->set_spacing (6);
120         table2->set_row_spacings (6);
121         table2->set_col_spacing (1, 6);
122
123         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
124         l->set_use_markup ();
125         options_box->pack_start (*l, false, true);
126
127         l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
128         l->set_padding (8, 0);
129         table2->attach (*l, 0, 1, 0, 3, Gtk::FILL, Gtk::FILL, 0, 0);
130
131         int n = 0;
132
133         l = manage (new Label (_("Name:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
134         table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
135         table2->attach (name_template_entry, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
136         ++n;
137
138         /* Route configuration */
139
140         configuration_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
141         table2->attach (configuration_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
142         table2->attach (channel_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
143         ++n;
144
145         mode_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
146         table2->attach (mode_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
147         table2->attach (mode_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
148         ++n;
149
150         instrument_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
151         table2->attach (instrument_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
152         table2->attach (instrument_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
153         ++n;
154
155         /* Group choice */
156
157         l = manage (new Label (_("Group:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
158         table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
159         table2->attach (route_group_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
160         ++n;
161
162         /* New route will be inserted at.. */
163         l = manage (new Label (_("Insert:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
164         table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
165         table2->attach (insert_at_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
166         ++n;
167
168         /* New Route's Routing is.. */
169
170         if (Profile->get_mixbus ()) {
171                 strict_io_combo.set_active (1);
172         } else {
173                 l = manage (new Label (_("Output Ports:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
174                 table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
175                 table2->attach (strict_io_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
176
177                 ARDOUR_UI_UTILS::set_tooltip (strict_io_combo,
178                                 _("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."));
179                 ++n;
180         }
181
182         options_box->pack_start (*table2, false, true);
183         vbox->pack_start (*options_box, false, true);
184
185         get_vbox()->pack_start (*vbox, false, false);
186
187         track_bus_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::track_type_chosen));
188         channel_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::channel_combo_changed));
189         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
190         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
191         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
192
193         show_all_children ();
194
195         /* track template info will be managed whenever
196            this dialog is shown, via ::on_show()
197         */
198
199         add_button (Stock::CANCEL, RESPONSE_CANCEL);
200         add_button (Stock::ADD, RESPONSE_ACCEPT);
201         set_response_sensitive (RESPONSE_ACCEPT, true);
202         set_default_response (RESPONSE_ACCEPT);
203
204         track_type_chosen ();
205 }
206
207 AddRouteDialog::~AddRouteDialog ()
208 {
209 }
210
211 void
212 AddRouteDialog::channel_combo_changed ()
213 {
214         maybe_update_name_template_entry ();
215         refill_track_modes ();
216 }
217
218 AddRouteDialog::TypeWanted
219 AddRouteDialog::type_wanted() const
220 {
221         std::string str = track_bus_combo.get_active_text();
222         if (str == _("Audio Busses")) {
223                 return AudioBus;
224         } else if (str == _("MIDI Busses")){
225                 return MidiBus;
226         } else if (str == _("MIDI Tracks")){
227                 return MidiTrack;
228         } else if (str == _("Audio+MIDI Tracks")) {
229                 return MixedTrack;
230         } else if (str == _("Audio Tracks")) {
231                 return AudioTrack;
232         } else {
233                 return VCAMaster;
234         }
235 }
236
237 void
238 AddRouteDialog::maybe_update_name_template_entry ()
239 {
240         if (
241                 name_template_entry.get_text() != "" &&
242                 name_template_entry.get_text() != _("VCA") &&
243                 name_template_entry.get_text() != _("Audio") &&
244                 name_template_entry.get_text() != _("MIDI")  &&
245                 name_template_entry.get_text() != _("Audio+MIDI")  &&
246                 name_template_entry.get_text() != _("Bus")) {
247                 strict_io_combo.set_sensitive (false);
248                 return;
249         }
250
251         strict_io_combo.set_sensitive (true);
252
253         switch (type_wanted()) {
254         case AudioTrack:
255                 name_template_entry.set_text (_("Audio"));
256                 break;
257         case MidiTrack:
258                 name_template_entry.set_text (_("MIDI"));
259                 break;
260         case MixedTrack:
261                 name_template_entry.set_text (_("Audio+MIDI"));
262                 break;
263         case AudioBus:
264         case MidiBus:
265                 name_template_entry.set_text (_("Bus"));
266                 break;
267         case VCAMaster:
268                 name_template_entry.set_text (VCA::default_name_template());
269                 break;
270         }
271 }
272
273 void
274 AddRouteDialog::track_type_chosen ()
275 {
276         switch (type_wanted()) {
277         case AudioTrack:
278                 mode_combo.set_sensitive (true);
279                 channel_combo.set_sensitive (true);
280                 instrument_combo.set_sensitive (false);
281                 configuration_label.set_sensitive (true);
282                 mode_label.set_sensitive (true);
283                 instrument_label.set_sensitive (false);
284                 route_group_combo.set_sensitive (true);
285                 break;
286         case MidiTrack:
287                 channel_combo.set_sensitive (false);
288                 mode_combo.set_sensitive (false);
289                 instrument_combo.set_sensitive (true);
290                 configuration_label.set_sensitive (false);
291                 mode_label.set_sensitive (false);
292                 instrument_label.set_sensitive (true);
293                 route_group_combo.set_sensitive (true);
294                 break;
295         case MixedTrack:
296                 {
297                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
298                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
299                                            true, MESSAGE_INFO, BUTTONS_OK, true);
300                         msg.set_position (WIN_POS_MOUSE);
301                         msg.run ();
302                 }
303                 channel_combo.set_sensitive (true);
304                 mode_combo.set_sensitive (true);
305                 instrument_combo.set_sensitive (true);
306                 configuration_label.set_sensitive (true);
307                 mode_label.set_sensitive (true);
308                 instrument_label.set_sensitive (true);
309                 route_group_combo.set_sensitive (true);
310                 break;
311         case AudioBus:
312                 mode_combo.set_sensitive (false);
313                 channel_combo.set_sensitive (true);
314                 instrument_combo.set_sensitive (false);
315                 configuration_label.set_sensitive (true);
316                 mode_label.set_sensitive (true);
317                 instrument_label.set_sensitive (false);
318                 route_group_combo.set_sensitive (true);
319                 break;
320         case VCAMaster:
321                 mode_combo.set_sensitive (false);
322                 channel_combo.set_sensitive (false);
323                 instrument_combo.set_sensitive (false);
324                 configuration_label.set_sensitive (false);
325                 mode_label.set_sensitive (false);
326                 instrument_label.set_sensitive (false);
327                 route_group_combo.set_sensitive (false);
328                 break;
329         case MidiBus:
330                 mode_combo.set_sensitive (false);
331                 channel_combo.set_sensitive (false);
332                 instrument_combo.set_sensitive (true);
333                 configuration_label.set_sensitive (false);
334                 mode_label.set_sensitive (true);
335                 instrument_label.set_sensitive (true);
336                 break;
337         }
338
339         maybe_update_name_template_entry ();
340 }
341
342
343 string
344 AddRouteDialog::name_template () const
345 {
346         return name_template_entry.get_text ();
347 }
348
349 bool
350 AddRouteDialog::name_template_is_default() const
351 {
352         string n = name_template();
353
354         if (n == _("Audio") ||
355             n == _("MIDI") ||
356             n == _("Audio+MIDI") ||
357             n == _("Bus") ||
358             n == _("VCA")) {
359                 return true;
360         }
361
362         return false;
363 }
364
365 int
366 AddRouteDialog::count ()
367 {
368         return (int) floor (routes_adjustment.get_value ());
369 }
370
371 void
372 AddRouteDialog::refill_track_modes ()
373 {
374         vector<string> s;
375
376         s.push_back (_("Normal"));
377         s.push_back (_("Non Layered"));
378         s.push_back (_("Tape"));
379
380         set_popdown_strings (mode_combo, s);
381         mode_combo.set_active_text (s.front());
382 }
383
384 ARDOUR::TrackMode
385 AddRouteDialog::mode ()
386 {
387         std::string str = mode_combo.get_active_text();
388         if (str == _("Normal")) {
389                 return ARDOUR::Normal;
390         } else if (str == _("Non Layered")){
391                 return ARDOUR::NonLayered;
392         } else if (str == _("Tape")) {
393                 return ARDOUR::Destructive;
394         } else {
395                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
396                       << endmsg;
397                 abort(); /*NOTREACHED*/
398         }
399         /* keep gcc happy */
400         return ARDOUR::Normal;
401 }
402
403 ChanCount
404 AddRouteDialog::channels ()
405 {
406         ChanCount ret;
407         string str;
408         switch (type_wanted()) {
409         case AudioTrack:
410         case AudioBus:
411                 str = channel_combo.get_active_text();
412                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
413                         if (str == (*i).name) {
414                                 ret.set (DataType::AUDIO, (*i).channels);
415                                 break;
416                         }
417                 }
418                 ret.set (DataType::MIDI, 0);
419                 break;
420
421         case MidiBus:
422         case MidiTrack:
423                 ret.set (DataType::AUDIO, 0);
424                 ret.set (DataType::MIDI, 1);
425                 break;
426
427         case MixedTrack:
428                 str = channel_combo.get_active_text();
429                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
430                         if (str == (*i).name) {
431                                 ret.set (DataType::AUDIO, (*i).channels);
432                                 break;
433                         }
434                 }
435                 ret.set (DataType::MIDI, 1);
436                 break;
437         default:
438                 break;
439         }
440
441         return ret;
442 }
443
444 string
445 AddRouteDialog::track_template ()
446 {
447         string str = channel_combo.get_active_text();
448
449         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
450                 if (str == (*i).name) {
451                         return (*i).template_path;
452                 }
453         }
454
455         return string();
456 }
457
458 void
459 AddRouteDialog::on_show ()
460 {
461         refill_channel_setups ();
462         refill_route_groups ();
463
464         Dialog::on_show ();
465 }
466
467 void
468 AddRouteDialog::refill_channel_setups ()
469 {
470         ChannelSetup chn;
471
472         route_templates.clear ();
473         channel_combo_strings.clear ();
474         channel_setups.clear ();
475
476         chn.name = _("Mono");
477         chn.channels = 1;
478         channel_setups.push_back (chn);
479
480         chn.name = _("Stereo");
481         chn.channels = 2;
482         channel_setups.push_back (chn);
483
484         chn.name = "separator";
485         channel_setups.push_back (chn);
486
487         ARDOUR::find_route_templates (route_templates);
488
489         if (!route_templates.empty()) {
490                 vector<string> v;
491                 for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
492                         chn.name = x->name;
493                         chn.channels = 0;
494                         chn.template_path = x->path;
495                         channel_setups.push_back (chn);
496                 }
497         }
498
499         /* clear template path for the rest */
500
501         chn.template_path = "";
502
503         chn.name = _("3 Channel");
504         chn.channels = 3;
505         channel_setups.push_back (chn);
506
507         chn.name = _("4 Channel");
508         chn.channels = 4;
509         channel_setups.push_back (chn);
510
511         chn.name = _("5 Channel");
512         chn.channels = 5;
513         channel_setups.push_back (chn);
514
515         chn.name = _("6 Channel");
516         chn.channels = 6;
517         channel_setups.push_back (chn);
518
519         chn.name = _("8 Channel");
520         chn.channels = 8;
521         channel_setups.push_back (chn);
522
523         chn.name = _("12 Channel");
524         chn.channels = 12;
525         channel_setups.push_back (chn);
526
527         chn.name = _("Custom");
528         chn.channels = 0;
529         channel_setups.push_back (chn);
530
531         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
532                 channel_combo_strings.push_back ((*i).name);
533         }
534
535         set_popdown_strings (channel_combo, channel_combo_strings);
536         channel_combo.set_active_text (channel_combo_strings.front());
537 }
538
539 void
540 AddRouteDialog::add_route_group (RouteGroup* g)
541 {
542         route_group_combo.insert_text (3, g->name ());
543 }
544
545 RouteGroup*
546 AddRouteDialog::route_group ()
547 {
548         if (!_session || route_group_combo.get_active_row_number () == 2) {
549                 return 0;
550         }
551
552         return _session->route_group_by_name (route_group_combo.get_active_text());
553 }
554
555 bool
556 AddRouteDialog::use_strict_io() {
557         return strict_io_combo.get_active_row_number () == 1;
558 }
559
560 void
561 AddRouteDialog::refill_route_groups ()
562 {
563         route_group_combo.clear ();
564         route_group_combo.append_text (_("New Group..."));
565
566         route_group_combo.append_text ("separator");
567
568         route_group_combo.append_text (_("No Group"));
569
570         if (_session) {
571                 _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
572         }
573
574         route_group_combo.set_active (2);
575 }
576
577 void
578 AddRouteDialog::group_changed ()
579 {
580         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
581                 RouteGroup* g = new RouteGroup (*_session, "");
582                 RouteGroupDialog d (g, true);
583
584                 if (d.do_run ()) {
585                         delete g;
586                         route_group_combo.set_active (2);
587                 } else {
588                         if (_session) {
589                                 _session->add_route_group (g);
590                         }
591                         add_route_group (g);
592                         route_group_combo.set_active (3);
593                 }
594         }
595 }
596
597 AddRouteDialog::InsertAt
598 AddRouteDialog::insert_at ()
599 {
600         std::string str = insert_at_combo.get_active_text();
601
602         if (str == _("First")) {
603                 return First;
604         } else if (str == _("After Selection")) {
605                 return AfterSelection;
606         } else if (str == _("Before Selection")){
607                 return BeforeSelection;
608         }
609         return Last;
610 }
611
612 bool
613 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
614 {
615         channel_combo.set_active (i);
616
617         return channel_combo.get_active_text () == "separator";
618 }
619
620 bool
621 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
622 {
623         route_group_combo.set_active (i);
624
625         return route_group_combo.get_active_text () == "separator";
626 }
627
628 PluginInfoPtr
629 AddRouteDialog::requested_instrument ()
630 {
631         return instrument_combo.selected_instrument();
632 }