OSC: add param to plugin_list response which indicates plugin enable state (OnkelDead)
[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         , mode_label (_("Record Mode:"))
64         , instrument_label (_("Instrument:"))
65         , name_edited_by_user (false)
66 {
67         set_name ("AddRouteDialog");
68         set_skip_taskbar_hint (true);
69         set_resizable (false);
70         set_position (WIN_POS_MOUSE);
71
72         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
73         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
74         channel_combo.set_name (X_("ChannelCountSelector"));
75         mode_combo.set_name (X_("ChannelCountSelector"));
76
77         refill_track_modes ();
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                 ArdourWidgets::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         name_template_entry.signal_insert_text ().connect (sigc::mem_fun (*this, &AddRouteDialog::name_template_entry_insertion));
188         name_template_entry.signal_delete_text ().connect (sigc::mem_fun (*this, &AddRouteDialog::name_template_entry_deletion));
189         track_bus_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::track_type_chosen));
190         channel_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::channel_combo_changed));
191         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
192         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
193         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
194
195         show_all_children ();
196
197         /* track template info will be managed whenever
198            this dialog is shown, via ::on_show()
199         */
200
201         add_button (_("Add and Close"), AddAndClose);
202         add_button (_("Add"), Add);
203         set_response_sensitive (AddAndClose, true);
204         set_default_response (AddAndClose);
205
206         track_type_chosen ();
207 }
208
209 AddRouteDialog::~AddRouteDialog ()
210 {
211 }
212
213 void
214 AddRouteDialog::on_response (int r)
215 {
216         name_edited_by_user = false;
217         /* Don't call ArdourDialog::on_response() because that will
218            automatically hide the dialog.
219         */
220         Gtk::Dialog::on_response (r);
221 }
222
223 void
224 AddRouteDialog::name_template_entry_insertion (Glib::ustring const &,int*)
225 {
226         name_edited_by_user = true;
227 }
228
229 void
230 AddRouteDialog::name_template_entry_deletion (int, int)
231 {
232         name_edited_by_user = true;
233 }
234
235 void
236 AddRouteDialog::channel_combo_changed ()
237 {
238         refill_track_modes ();
239 }
240
241 AddRouteDialog::TypeWanted
242 AddRouteDialog::type_wanted() const
243 {
244         std::string str = track_bus_combo.get_active_text();
245         if (str == _("Audio Busses")) {
246                 return AudioBus;
247         } else if (str == _("MIDI Busses")){
248                 return MidiBus;
249         } else if (str == _("MIDI Tracks")){
250                 return MidiTrack;
251         } else if (str == _("Audio+MIDI Tracks")) {
252                 return MixedTrack;
253         } else if (str == _("Audio Tracks")) {
254                 return AudioTrack;
255         } else {
256                 return VCAMaster;
257         }
258 }
259
260 void
261 AddRouteDialog::maybe_update_name_template_entry ()
262 {
263         if (name_edited_by_user) {
264                 return;
265         }
266
267         switch (type_wanted()) {
268         case AudioTrack:
269                 name_template_entry.set_text (_("Audio"));
270                 break;
271         case MidiTrack:
272                 name_template_entry.set_text (_("MIDI"));
273                 break;
274         case MixedTrack:
275                 name_template_entry.set_text (_("Audio+MIDI"));
276                 break;
277         case AudioBus:
278         case MidiBus:
279                 name_template_entry.set_text (_("Bus"));
280                 break;
281         case VCAMaster:
282                 name_template_entry.set_text (VCA::default_name_template());
283                 break;
284         }
285 }
286
287 void
288 AddRouteDialog::track_type_chosen ()
289 {
290         switch (type_wanted()) {
291         case AudioTrack:
292                 mode_combo.set_sensitive (true);
293                 channel_combo.set_sensitive (true);
294                 instrument_combo.set_sensitive (false);
295                 configuration_label.set_sensitive (true);
296                 mode_label.set_sensitive (true);
297                 instrument_label.set_sensitive (false);
298                 route_group_combo.set_sensitive (true);
299                 strict_io_combo.set_sensitive (true);
300                 insert_at_combo.set_sensitive (true);
301                 break;
302         case MidiTrack:
303                 channel_combo.set_sensitive (false);
304                 mode_combo.set_sensitive (false);
305                 instrument_combo.set_sensitive (true);
306                 configuration_label.set_sensitive (false);
307                 mode_label.set_sensitive (false);
308                 instrument_label.set_sensitive (true);
309                 route_group_combo.set_sensitive (true);
310                 strict_io_combo.set_sensitive (true);
311                 insert_at_combo.set_sensitive (true);
312                 break;
313         case MixedTrack:
314                 {
315                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
316                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
317                                            true, MESSAGE_INFO, BUTTONS_OK, true);
318                         msg.set_position (WIN_POS_MOUSE);
319                         msg.run ();
320                 }
321                 channel_combo.set_sensitive (true);
322                 mode_combo.set_sensitive (true);
323                 instrument_combo.set_sensitive (true);
324                 configuration_label.set_sensitive (true);
325                 mode_label.set_sensitive (true);
326                 instrument_label.set_sensitive (true);
327                 route_group_combo.set_sensitive (true);
328                 strict_io_combo.set_sensitive (true);
329                 insert_at_combo.set_sensitive (true);
330                 break;
331         case AudioBus:
332                 mode_combo.set_sensitive (false);
333                 channel_combo.set_sensitive (true);
334                 instrument_combo.set_sensitive (false);
335                 configuration_label.set_sensitive (true);
336                 mode_label.set_sensitive (true);
337                 instrument_label.set_sensitive (false);
338                 route_group_combo.set_sensitive (true);
339                 strict_io_combo.set_sensitive (true);
340                 insert_at_combo.set_sensitive (true);
341                 break;
342         case VCAMaster:
343                 mode_combo.set_sensitive (false);
344                 channel_combo.set_sensitive (false);
345                 instrument_combo.set_sensitive (false);
346                 configuration_label.set_sensitive (false);
347                 mode_label.set_sensitive (false);
348                 instrument_label.set_sensitive (false);
349                 route_group_combo.set_sensitive (false);
350                 strict_io_combo.set_sensitive (false);
351                 insert_at_combo.set_sensitive (false);
352                 break;
353         case MidiBus:
354                 mode_combo.set_sensitive (false);
355                 channel_combo.set_sensitive (false);
356                 instrument_combo.set_sensitive (true);
357                 configuration_label.set_sensitive (false);
358                 mode_label.set_sensitive (true);
359                 instrument_label.set_sensitive (true);
360                 strict_io_combo.set_sensitive (true);
361                 insert_at_combo.set_sensitive (true);
362                 break;
363         }
364
365         maybe_update_name_template_entry ();
366 }
367
368
369 string
370 AddRouteDialog::name_template () const
371 {
372         return name_template_entry.get_text ();
373 }
374
375 bool
376 AddRouteDialog::name_template_is_default() const
377 {
378         string n = name_template();
379
380         if (n == _("Audio") ||
381             n == _("MIDI") ||
382             n == _("Audio+MIDI") ||
383             n == _("Bus") ||
384             n == VCA::default_name_template()) {
385                 return true;
386         }
387
388         return false;
389 }
390
391 int
392 AddRouteDialog::count ()
393 {
394         return (int) floor (routes_adjustment.get_value ());
395 }
396
397 void
398 AddRouteDialog::refill_track_modes ()
399 {
400         vector<string> s;
401
402         s.push_back (_("Normal"));
403 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
404         s.push_back (_("Non Layered"));
405 #endif
406         if (!ARDOUR::Profile->get_mixbus ()) {
407                 s.push_back (_("Tape"));
408         }
409
410         set_popdown_strings (mode_combo, s);
411         mode_combo.set_active_text (s.front());
412 }
413
414 ARDOUR::TrackMode
415 AddRouteDialog::mode ()
416 {
417         std::string str = mode_combo.get_active_text();
418         if (str == _("Normal")) {
419                 return ARDOUR::Normal;
420         } else if (str == _("Non Layered")){
421                 return ARDOUR::NonLayered;
422         } else if (str == _("Tape")) {
423                 return ARDOUR::Destructive;
424         } else {
425                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
426                       << endmsg;
427                 abort(); /*NOTREACHED*/
428         }
429         /* keep gcc happy */
430         return ARDOUR::Normal;
431 }
432
433 ChanCount
434 AddRouteDialog::channels ()
435 {
436         ChanCount ret;
437         string str;
438         switch (type_wanted()) {
439         case AudioTrack:
440         case AudioBus:
441                 str = channel_combo.get_active_text();
442                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
443                         if (str == (*i).name) {
444                                 ret.set (DataType::AUDIO, (*i).channels);
445                                 break;
446                         }
447                 }
448                 ret.set (DataType::MIDI, 0);
449                 break;
450
451         case MidiBus:
452         case MidiTrack:
453                 ret.set (DataType::AUDIO, 0);
454                 ret.set (DataType::MIDI, 1);
455                 break;
456
457         case MixedTrack:
458                 str = channel_combo.get_active_text();
459                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
460                         if (str == (*i).name) {
461                                 ret.set (DataType::AUDIO, (*i).channels);
462                                 break;
463                         }
464                 }
465                 ret.set (DataType::MIDI, 1);
466                 break;
467         default:
468                 break;
469         }
470
471         return ret;
472 }
473
474 string
475 AddRouteDialog::track_template ()
476 {
477         string str = channel_combo.get_active_text();
478
479         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
480                 if (str == (*i).name) {
481                         return (*i).template_path;
482                 }
483         }
484
485         return string();
486 }
487
488 void
489 AddRouteDialog::on_show ()
490 {
491         routes_spinner.grab_focus ();
492         name_edited_by_user = false;
493
494         refill_channel_setups ();
495         refill_route_groups ();
496
497         Dialog::on_show ();
498 }
499
500 void
501 AddRouteDialog::refill_channel_setups ()
502 {
503         ChannelSetup chn;
504
505         route_templates.clear ();
506
507         string channel_current_choice = channel_combo.get_active_text();
508
509         channel_combo_strings.clear ();
510         channel_setups.clear ();
511
512         chn.name = _("Mono");
513         chn.channels = 1;
514         channel_setups.push_back (chn);
515
516         chn.name = _("Stereo");
517         chn.channels = 2;
518         channel_setups.push_back (chn);
519
520         chn.name = "separator";
521         channel_setups.push_back (chn);
522
523         ARDOUR::find_route_templates (route_templates);
524
525         if (!route_templates.empty()) {
526                 vector<string> v;
527                 for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
528                         chn.name = x->name;
529                         chn.channels = 0;
530                         chn.template_path = x->path;
531                         channel_setups.push_back (chn);
532                 }
533         }
534
535         /* clear template path for the rest */
536
537         chn.template_path = "";
538
539         chn.name = _("3 Channel");
540         chn.channels = 3;
541         channel_setups.push_back (chn);
542
543         chn.name = _("4 Channel");
544         chn.channels = 4;
545         channel_setups.push_back (chn);
546
547         chn.name = _("5 Channel");
548         chn.channels = 5;
549         channel_setups.push_back (chn);
550
551         chn.name = _("6 Channel");
552         chn.channels = 6;
553         channel_setups.push_back (chn);
554
555         chn.name = _("8 Channel");
556         chn.channels = 8;
557         channel_setups.push_back (chn);
558
559         chn.name = _("12 Channel");
560         chn.channels = 12;
561         channel_setups.push_back (chn);
562
563         chn.name = _("Custom");
564         chn.channels = 0;
565         channel_setups.push_back (chn);
566
567         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
568                 channel_combo_strings.push_back ((*i).name);
569         }
570
571         set_popdown_strings (channel_combo, channel_combo_strings);
572
573         if (!channel_current_choice.empty()) {
574                 channel_combo.set_active_text (channel_current_choice);
575         } else {
576                 channel_combo.set_active_text (channel_combo_strings.front());
577         }
578 }
579
580 void
581 AddRouteDialog::add_route_group (RouteGroup* g)
582 {
583         route_group_combo.insert_text (3, g->name ());
584 }
585
586 RouteGroup*
587 AddRouteDialog::route_group ()
588 {
589         if (!_session || route_group_combo.get_active_row_number () == 2) {
590                 return 0;
591         }
592
593         return _session->route_group_by_name (route_group_combo.get_active_text());
594 }
595
596 bool
597 AddRouteDialog::use_strict_io() {
598         return strict_io_combo.get_active_row_number () == 1;
599 }
600
601 void
602 AddRouteDialog::refill_route_groups ()
603 {
604         route_group_combo.clear ();
605         route_group_combo.append_text (_("New Group..."));
606
607         route_group_combo.append_text ("separator");
608
609         route_group_combo.append_text (_("No Group"));
610
611         if (_session) {
612                 _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
613         }
614
615         route_group_combo.set_active (2);
616 }
617
618 void
619 AddRouteDialog::group_changed ()
620 {
621         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
622                 RouteGroup* g = new RouteGroup (*_session, "");
623                 RouteGroupDialog* d = new RouteGroupDialog (g, true);
624
625                 d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &AddRouteDialog::new_group_dialog_finished), d));
626                 d->present();
627         }
628 }
629
630 void
631 AddRouteDialog::new_group_dialog_finished (int r, RouteGroupDialog* d)
632 {
633         if (r == RESPONSE_OK) {
634
635                 if (!d->name_check()) {
636                         return;
637                 }
638
639                 if (_session) {
640                         _session->add_route_group (d->group());
641                 }
642
643                 add_route_group (d->group());
644                 route_group_combo.set_active (3);
645         } else {
646                 delete d->group ();
647                 route_group_combo.set_active (2);
648         }
649
650         delete_when_idle (d);
651 }
652
653 RouteDialogs::InsertAt
654 AddRouteDialog::insert_at ()
655 {
656         using namespace RouteDialogs;
657
658         std::string str = insert_at_combo.get_active_text();
659
660         if (str == _("First")) {
661                 return First;
662         } else if (str == _("After Selection")) {
663                 return AfterSelection;
664         } else if (str == _("Before Selection")){
665                 return BeforeSelection;
666         }
667         return Last;
668 }
669
670 bool
671 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
672 {
673         channel_combo.set_active (i);
674
675         return channel_combo.get_active_text () == "separator";
676 }
677
678 bool
679 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
680 {
681         route_group_combo.set_active (i);
682
683         return route_group_combo.get_active_text () == "separator";
684 }
685
686 PluginInfoPtr
687 AddRouteDialog::requested_instrument ()
688 {
689         return instrument_combo.selected_instrument();
690 }