Session Metadata: add a Description field.
[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         name_edited_by_user = false;
286 }
287
288 void
289 AddRouteDialog::track_type_chosen ()
290 {
291         switch (type_wanted()) {
292         case AudioTrack:
293                 mode_combo.set_sensitive (true);
294                 channel_combo.set_sensitive (true);
295                 instrument_combo.set_sensitive (false);
296                 configuration_label.set_sensitive (true);
297                 mode_label.set_sensitive (true);
298                 instrument_label.set_sensitive (false);
299                 route_group_combo.set_sensitive (true);
300                 strict_io_combo.set_sensitive (true);
301                 insert_at_combo.set_sensitive (true);
302                 break;
303         case MidiTrack:
304                 channel_combo.set_sensitive (false);
305                 mode_combo.set_sensitive (false);
306                 instrument_combo.set_sensitive (true);
307                 configuration_label.set_sensitive (false);
308                 mode_label.set_sensitive (false);
309                 instrument_label.set_sensitive (true);
310                 route_group_combo.set_sensitive (true);
311                 strict_io_combo.set_sensitive (true);
312                 insert_at_combo.set_sensitive (true);
313                 break;
314         case MixedTrack:
315                 {
316                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
317                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
318                                            true, MESSAGE_INFO, BUTTONS_OK, true);
319                         msg.set_position (WIN_POS_MOUSE);
320                         msg.run ();
321                 }
322                 channel_combo.set_sensitive (true);
323                 mode_combo.set_sensitive (true);
324                 instrument_combo.set_sensitive (true);
325                 configuration_label.set_sensitive (true);
326                 mode_label.set_sensitive (true);
327                 instrument_label.set_sensitive (true);
328                 route_group_combo.set_sensitive (true);
329                 strict_io_combo.set_sensitive (true);
330                 insert_at_combo.set_sensitive (true);
331                 break;
332         case AudioBus:
333                 mode_combo.set_sensitive (false);
334                 channel_combo.set_sensitive (true);
335                 instrument_combo.set_sensitive (false);
336                 configuration_label.set_sensitive (true);
337                 mode_label.set_sensitive (true);
338                 instrument_label.set_sensitive (false);
339                 route_group_combo.set_sensitive (true);
340                 strict_io_combo.set_sensitive (true);
341                 insert_at_combo.set_sensitive (true);
342                 break;
343         case VCAMaster:
344                 mode_combo.set_sensitive (false);
345                 channel_combo.set_sensitive (false);
346                 instrument_combo.set_sensitive (false);
347                 configuration_label.set_sensitive (false);
348                 mode_label.set_sensitive (false);
349                 instrument_label.set_sensitive (false);
350                 route_group_combo.set_sensitive (false);
351                 strict_io_combo.set_sensitive (false);
352                 insert_at_combo.set_sensitive (false);
353                 break;
354         case MidiBus:
355                 mode_combo.set_sensitive (false);
356                 channel_combo.set_sensitive (false);
357                 instrument_combo.set_sensitive (true);
358                 configuration_label.set_sensitive (false);
359                 mode_label.set_sensitive (true);
360                 instrument_label.set_sensitive (true);
361                 strict_io_combo.set_sensitive (true);
362                 insert_at_combo.set_sensitive (true);
363                 break;
364         }
365
366         maybe_update_name_template_entry ();
367 }
368
369
370 string
371 AddRouteDialog::name_template () const
372 {
373         return name_template_entry.get_text ();
374 }
375
376 bool
377 AddRouteDialog::name_template_is_default() const
378 {
379         string n = name_template();
380
381         if (n == _("Audio") ||
382             n == _("MIDI") ||
383             n == _("Audio+MIDI") ||
384             n == _("Bus") ||
385             n == VCA::default_name_template()) {
386                 return true;
387         }
388
389         return false;
390 }
391
392 int
393 AddRouteDialog::count ()
394 {
395         return (int) floor (routes_adjustment.get_value ());
396 }
397
398 void
399 AddRouteDialog::refill_track_modes ()
400 {
401         vector<string> s;
402
403         s.push_back (_("Normal"));
404 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
405         s.push_back (_("Non Layered"));
406 #endif
407         if (!ARDOUR::Profile->get_mixbus ()) {
408                 s.push_back (_("Tape"));
409         }
410
411         set_popdown_strings (mode_combo, s);
412         mode_combo.set_active_text (s.front());
413 }
414
415 ARDOUR::TrackMode
416 AddRouteDialog::mode ()
417 {
418         std::string str = mode_combo.get_active_text();
419         if (str == _("Normal")) {
420                 return ARDOUR::Normal;
421         } else if (str == _("Non Layered")){
422                 return ARDOUR::NonLayered;
423         } else if (str == _("Tape")) {
424                 return ARDOUR::Destructive;
425         } else {
426                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
427                       << endmsg;
428                 abort(); /*NOTREACHED*/
429         }
430         /* keep gcc happy */
431         return ARDOUR::Normal;
432 }
433
434 ChanCount
435 AddRouteDialog::channels ()
436 {
437         ChanCount ret;
438         string str;
439         switch (type_wanted()) {
440         case AudioTrack:
441         case AudioBus:
442                 str = channel_combo.get_active_text();
443                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
444                         if (str == (*i).name) {
445                                 ret.set (DataType::AUDIO, (*i).channels);
446                                 break;
447                         }
448                 }
449                 ret.set (DataType::MIDI, 0);
450                 break;
451
452         case MidiBus:
453         case MidiTrack:
454                 ret.set (DataType::AUDIO, 0);
455                 ret.set (DataType::MIDI, 1);
456                 break;
457
458         case MixedTrack:
459                 str = channel_combo.get_active_text();
460                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
461                         if (str == (*i).name) {
462                                 ret.set (DataType::AUDIO, (*i).channels);
463                                 break;
464                         }
465                 }
466                 ret.set (DataType::MIDI, 1);
467                 break;
468         default:
469                 break;
470         }
471
472         return ret;
473 }
474
475 string
476 AddRouteDialog::track_template ()
477 {
478         string str = channel_combo.get_active_text();
479
480         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
481                 if (str == (*i).name) {
482                         return (*i).template_path;
483                 }
484         }
485
486         return string();
487 }
488
489 void
490 AddRouteDialog::on_show ()
491 {
492         routes_spinner.grab_focus ();
493         name_edited_by_user = false;
494
495         refill_channel_setups ();
496         refill_route_groups ();
497
498         Dialog::on_show ();
499 }
500
501 void
502 AddRouteDialog::refill_channel_setups ()
503 {
504         ChannelSetup chn;
505
506         route_templates.clear ();
507
508         string channel_current_choice = channel_combo.get_active_text();
509
510         channel_combo_strings.clear ();
511         channel_setups.clear ();
512
513         chn.name = _("Mono");
514         chn.channels = 1;
515         channel_setups.push_back (chn);
516
517         chn.name = _("Stereo");
518         chn.channels = 2;
519         channel_setups.push_back (chn);
520
521         chn.name = "separator";
522         channel_setups.push_back (chn);
523
524         ARDOUR::find_route_templates (route_templates);
525
526         if (!route_templates.empty()) {
527                 vector<string> v;
528                 for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
529                         chn.name = x->name;
530                         chn.channels = 0;
531                         chn.template_path = x->path;
532                         channel_setups.push_back (chn);
533                 }
534         }
535
536         /* clear template path for the rest */
537
538         chn.template_path = "";
539
540         chn.name = _("3 Channel");
541         chn.channels = 3;
542         channel_setups.push_back (chn);
543
544         chn.name = _("4 Channel");
545         chn.channels = 4;
546         channel_setups.push_back (chn);
547
548         chn.name = _("5 Channel");
549         chn.channels = 5;
550         channel_setups.push_back (chn);
551
552         chn.name = _("6 Channel");
553         chn.channels = 6;
554         channel_setups.push_back (chn);
555
556         chn.name = _("8 Channel");
557         chn.channels = 8;
558         channel_setups.push_back (chn);
559
560         chn.name = _("12 Channel");
561         chn.channels = 12;
562         channel_setups.push_back (chn);
563
564         chn.name = _("Custom");
565         chn.channels = 0;
566         channel_setups.push_back (chn);
567
568         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
569                 channel_combo_strings.push_back ((*i).name);
570         }
571
572         set_popdown_strings (channel_combo, channel_combo_strings);
573
574         if (!channel_current_choice.empty()) {
575                 channel_combo.set_active_text (channel_current_choice);
576         } else {
577                 channel_combo.set_active_text (channel_combo_strings.front());
578         }
579 }
580
581 void
582 AddRouteDialog::add_route_group (RouteGroup* g)
583 {
584         route_group_combo.insert_text (3, g->name ());
585 }
586
587 RouteGroup*
588 AddRouteDialog::route_group ()
589 {
590         if (!_session || route_group_combo.get_active_row_number () == 2) {
591                 return 0;
592         }
593
594         return _session->route_group_by_name (route_group_combo.get_active_text());
595 }
596
597 bool
598 AddRouteDialog::use_strict_io() {
599         return strict_io_combo.get_active_row_number () == 1;
600 }
601
602 void
603 AddRouteDialog::refill_route_groups ()
604 {
605         route_group_combo.clear ();
606         route_group_combo.append_text (_("New Group..."));
607
608         route_group_combo.append_text ("separator");
609
610         route_group_combo.append_text (_("No Group"));
611
612         if (_session) {
613                 _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
614         }
615
616         route_group_combo.set_active (2);
617 }
618
619 void
620 AddRouteDialog::group_changed ()
621 {
622         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
623                 RouteGroup* g = new RouteGroup (*_session, "");
624                 RouteGroupDialog* d = new RouteGroupDialog (g, true);
625
626                 d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &AddRouteDialog::new_group_dialog_finished), d));
627                 d->present();
628         }
629 }
630
631 void
632 AddRouteDialog::new_group_dialog_finished (int r, RouteGroupDialog* d)
633 {
634         if (r == RESPONSE_OK) {
635
636                 if (!d->name_check()) {
637                         return;
638                 }
639
640                 if (_session) {
641                         _session->add_route_group (d->group());
642                 }
643
644                 add_route_group (d->group());
645                 route_group_combo.set_active (3);
646         } else {
647                 delete d->group ();
648                 route_group_combo.set_active (2);
649         }
650
651         delete_when_idle (d);
652 }
653
654 RouteDialogs::InsertAt
655 AddRouteDialog::insert_at ()
656 {
657         using namespace RouteDialogs;
658
659         std::string str = insert_at_combo.get_active_text();
660
661         if (str == _("First")) {
662                 return First;
663         } else if (str == _("After Selection")) {
664                 return AfterSelection;
665         } else if (str == _("Before Selection")){
666                 return BeforeSelection;
667         }
668         return Last;
669 }
670
671 bool
672 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
673 {
674         channel_combo.set_active (i);
675
676         return channel_combo.get_active_text () == "separator";
677 }
678
679 bool
680 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
681 {
682         route_group_combo.set_active (i);
683
684         return route_group_combo.get_active_text () == "separator";
685 }
686
687 PluginInfoPtr
688 AddRouteDialog::requested_instrument ()
689 {
690         return instrument_combo.selected_instrument();
691 }