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