ptformat: Make PT import more resilient to bad user choices and display messages
[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
39 #include "utils.h"
40 #include "add_route_dialog.h"
41 #include "route_group_dialog.h"
42 #include "tooltips.h"
43 #include "i18n.h"
44
45 using namespace Gtk;
46 using namespace Gtkmm2ext;
47 using namespace std;
48 using namespace PBD;
49 using namespace ARDOUR;
50 using namespace ARDOUR_UI_UTILS;
51
52 std::vector<std::string> AddRouteDialog::channel_combo_strings;
53
54 AddRouteDialog::AddRouteDialog ()
55         : ArdourDialog (_("Add Track or Bus"))
56         , routes_adjustment (1, 1, 128, 1, 4)
57         , routes_spinner (routes_adjustment)
58         , configuration_label (_("Configuration:"))
59         , mode_label (_("Record Mode:"))
60         , instrument_label (_("Instrument:"))
61 {
62         set_name ("AddRouteDialog");
63         set_modal (true);
64         set_skip_taskbar_hint (true);
65         set_resizable (false);
66
67         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
68         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
69         channel_combo.set_name (X_("ChannelCountSelector"));
70         mode_combo.set_name (X_("ChannelCountSelector"));
71
72         refill_channel_setups ();
73         refill_route_groups ();
74         refill_track_modes ();
75
76         channel_combo.set_active_text (channel_combo_strings.front());
77
78         track_bus_combo.append_text (_("Audio Tracks"));
79         track_bus_combo.append_text (_("MIDI Tracks"));
80         track_bus_combo.append_text (_("Audio+MIDI Tracks"));
81         track_bus_combo.append_text (_("Audio Busses"));
82         track_bus_combo.append_text (_("MIDI Busses"));
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 (Stock::CANCEL, RESPONSE_CANCEL);
198         add_button (Stock::ADD, RESPONSE_ACCEPT);
199         set_response_sensitive (RESPONSE_ACCEPT, true);
200         set_default_response (RESPONSE_ACCEPT);
201
202         track_type_chosen ();
203 }
204
205 AddRouteDialog::~AddRouteDialog ()
206 {
207 }
208
209 void
210 AddRouteDialog::channel_combo_changed ()
211 {
212         maybe_update_name_template_entry ();
213         refill_track_modes ();
214 }
215
216 AddRouteDialog::TypeWanted
217 AddRouteDialog::type_wanted() const
218 {
219         std::string str = track_bus_combo.get_active_text();
220         if (str == _("Audio Busses")) {
221                 return AudioBus;
222         } else if (str == _("MIDI Busses")){
223                 return MidiBus;
224         } else if (str == _("MIDI Tracks")){
225                 return MidiTrack;
226         } else if (str == _("Audio+MIDI Tracks")) {
227                 return MixedTrack;
228         } else {
229                 return AudioTrack;
230         }
231 }
232
233 void
234 AddRouteDialog::maybe_update_name_template_entry ()
235 {
236         if (
237                 name_template_entry.get_text() != "" &&
238                 name_template_entry.get_text() != _("Audio") &&
239                 name_template_entry.get_text() != _("MIDI")  &&
240                 name_template_entry.get_text() != _("Audio+MIDI")  &&
241                 name_template_entry.get_text() != _("Bus")) {
242                 strict_io_combo.set_sensitive (false);
243                 return;
244         }
245
246         strict_io_combo.set_sensitive (true);
247
248         switch (type_wanted()) {
249         case AudioTrack:
250                 name_template_entry.set_text (_("Audio"));
251                 break;
252         case MidiTrack:
253                 name_template_entry.set_text (_("MIDI"));
254                 break;
255         case MixedTrack:
256                 name_template_entry.set_text (_("Audio+MIDI"));
257                 break;
258         case AudioBus:
259         case MidiBus:
260                 name_template_entry.set_text (_("Bus"));
261                 break;
262         }
263 }
264
265 void
266 AddRouteDialog::track_type_chosen ()
267 {
268         switch (type_wanted()) {
269         case AudioTrack:
270                 mode_combo.set_sensitive (true);
271                 channel_combo.set_sensitive (true);
272                 instrument_combo.set_sensitive (false);
273                 configuration_label.set_sensitive (true);
274                 mode_label.set_sensitive (true);
275                 instrument_label.set_sensitive (false);
276                 break;
277         case MidiTrack:
278                 channel_combo.set_sensitive (false);
279                 mode_combo.set_sensitive (false);
280                 instrument_combo.set_sensitive (true);
281                 configuration_label.set_sensitive (false);
282                 mode_label.set_sensitive (false);
283                 instrument_label.set_sensitive (true);
284                 break;
285         case MixedTrack:
286                 {
287                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
288                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
289                                            true, MESSAGE_INFO, BUTTONS_OK, true);
290                         msg.set_position (WIN_POS_MOUSE);
291                         msg.run ();
292                 }
293                 channel_combo.set_sensitive (true);
294                 mode_combo.set_sensitive (true);
295                 instrument_combo.set_sensitive (true);
296                 configuration_label.set_sensitive (true);
297                 mode_label.set_sensitive (true);
298                 instrument_label.set_sensitive (true);
299                 break;
300         case AudioBus:
301                 mode_combo.set_sensitive (false);
302                 channel_combo.set_sensitive (true);
303                 instrument_combo.set_sensitive (false);
304                 configuration_label.set_sensitive (true);
305                 mode_label.set_sensitive (true);
306                 instrument_label.set_sensitive (false);
307                 break;
308         case MidiBus:
309                 mode_combo.set_sensitive (false);
310                 channel_combo.set_sensitive (false);
311                 instrument_combo.set_sensitive (true);
312                 configuration_label.set_sensitive (false);
313                 mode_label.set_sensitive (true);
314                 instrument_label.set_sensitive (true);
315                 break;
316         }
317
318         maybe_update_name_template_entry ();
319 }
320
321
322 string
323 AddRouteDialog::name_template () const
324 {
325         return name_template_entry.get_text ();
326 }
327
328 bool
329 AddRouteDialog::name_template_is_default() const
330 {
331         string n = name_template();
332
333         if (n == _("Audio") ||
334             n == _("MIDI") ||
335             n == _("Audio+MIDI") ||
336             n == _("Bus")) {
337                 return true;
338         }
339
340         return false;
341 }
342
343 int
344 AddRouteDialog::count ()
345 {
346         return (int) floor (routes_adjustment.get_value ());
347 }
348
349 void
350 AddRouteDialog::refill_track_modes ()
351 {
352         vector<string> s;
353
354         s.push_back (_("Normal"));
355         s.push_back (_("Non Layered"));
356         s.push_back (_("Tape"));
357
358         set_popdown_strings (mode_combo, s);
359         mode_combo.set_active_text (s.front());
360 }
361
362 ARDOUR::TrackMode
363 AddRouteDialog::mode ()
364 {
365         std::string str = mode_combo.get_active_text();
366         if (str == _("Normal")) {
367                 return ARDOUR::Normal;
368         } else if (str == _("Non Layered")){
369                 return ARDOUR::NonLayered;
370         } else if (str == _("Tape")) {
371                 return ARDOUR::Destructive;
372         } else {
373                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
374                       << endmsg;
375                 abort(); /*NOTREACHED*/
376         }
377         /* keep gcc happy */
378         return ARDOUR::Normal;
379 }
380
381 ChanCount
382 AddRouteDialog::channels ()
383 {
384         ChanCount ret;
385         string str;
386         switch (type_wanted()) {
387         case AudioTrack:
388         case AudioBus:
389                 str = channel_combo.get_active_text();
390                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
391                         if (str == (*i).name) {
392                                 ret.set (DataType::AUDIO, (*i).channels);
393                                 break;
394                         }
395                 }
396                 ret.set (DataType::MIDI, 0);
397                 break;
398
399         case MidiBus:
400         case MidiTrack:
401                 ret.set (DataType::AUDIO, 0);
402                 ret.set (DataType::MIDI, 1);
403                 break;
404
405         case MixedTrack:
406                 str = channel_combo.get_active_text();
407                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
408                         if (str == (*i).name) {
409                                 ret.set (DataType::AUDIO, (*i).channels);
410                                 break;
411                         }
412                 }
413                 ret.set (DataType::MIDI, 1);
414                 break;
415         }
416
417         return ret;
418 }
419
420 string
421 AddRouteDialog::track_template ()
422 {
423         string str = channel_combo.get_active_text();
424
425         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
426                 if (str == (*i).name) {
427                         return (*i).template_path;
428                 }
429         }
430
431         return string();
432 }
433
434 void
435 AddRouteDialog::on_show ()
436 {
437         refill_channel_setups ();
438         refill_route_groups ();
439
440         Dialog::on_show ();
441 }
442
443 void
444 AddRouteDialog::refill_channel_setups ()
445 {
446         ChannelSetup chn;
447
448         route_templates.clear ();
449         channel_combo_strings.clear ();
450         channel_setups.clear ();
451
452         chn.name = _("Mono");
453         chn.channels = 1;
454         channel_setups.push_back (chn);
455
456         chn.name = _("Stereo");
457         chn.channels = 2;
458         channel_setups.push_back (chn);
459
460         chn.name = "separator";
461         channel_setups.push_back (chn);
462
463         ARDOUR::find_route_templates (route_templates);
464
465         if (!route_templates.empty()) {
466                 vector<string> v;
467                 for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
468                         chn.name = x->name;
469                         chn.channels = 0;
470                         chn.template_path = x->path;
471                         channel_setups.push_back (chn);
472                 }
473         }
474
475         /* clear template path for the rest */
476
477         chn.template_path = "";
478
479         chn.name = _("3 Channel");
480         chn.channels = 3;
481         channel_setups.push_back (chn);
482
483         chn.name = _("4 Channel");
484         chn.channels = 4;
485         channel_setups.push_back (chn);
486
487         chn.name = _("5 Channel");
488         chn.channels = 5;
489         channel_setups.push_back (chn);
490
491         chn.name = _("6 Channel");
492         chn.channels = 6;
493         channel_setups.push_back (chn);
494
495         chn.name = _("8 Channel");
496         chn.channels = 8;
497         channel_setups.push_back (chn);
498
499         chn.name = _("12 Channel");
500         chn.channels = 12;
501         channel_setups.push_back (chn);
502
503         chn.name = _("Custom");
504         chn.channels = 0;
505         channel_setups.push_back (chn);
506
507         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
508                 channel_combo_strings.push_back ((*i).name);
509         }
510
511         set_popdown_strings (channel_combo, channel_combo_strings);
512         channel_combo.set_active_text (channel_combo_strings.front());
513 }
514
515 void
516 AddRouteDialog::add_route_group (RouteGroup* g)
517 {
518         route_group_combo.insert_text (3, g->name ());
519 }
520
521 RouteGroup*
522 AddRouteDialog::route_group ()
523 {
524         if (!_session || route_group_combo.get_active_row_number () == 2) {
525                 return 0;
526         }
527
528         return _session->route_group_by_name (route_group_combo.get_active_text());
529 }
530
531 bool
532 AddRouteDialog::use_strict_io() {
533         return strict_io_combo.get_active_row_number () == 1;
534 }
535
536 void
537 AddRouteDialog::refill_route_groups ()
538 {
539         route_group_combo.clear ();
540         route_group_combo.append_text (_("New Group..."));
541
542         route_group_combo.append_text ("separator");
543
544         route_group_combo.append_text (_("No Group"));
545
546         if (_session) {
547                 _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
548         }
549
550         route_group_combo.set_active (2);
551 }
552
553 void
554 AddRouteDialog::group_changed ()
555 {
556         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
557                 RouteGroup* g = new RouteGroup (*_session, "");
558                 RouteGroupDialog d (g, true);
559
560                 if (d.do_run ()) {
561                         delete g;
562                         route_group_combo.set_active (2);
563                 } else {
564                         if (_session) {
565                                 _session->add_route_group (g);
566                         }
567                         add_route_group (g);
568                         route_group_combo.set_active (3);
569                 }
570         }
571 }
572
573 AddRouteDialog::InsertAt
574 AddRouteDialog::insert_at ()
575 {
576         std::string str = insert_at_combo.get_active_text();
577
578         if (str == _("First")) {
579                 return First;
580         } else if (str == _("After Selection")) {
581                 return AfterSelection;
582         } else if (str == _("Before Selection")){
583                 return BeforeSelection;
584         }
585         return Last;
586 }
587
588 bool
589 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
590 {
591         channel_combo.set_active (i);
592
593         return channel_combo.get_active_text () == "separator";
594 }
595
596 bool
597 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
598 {
599         route_group_combo.set_active (i);
600
601         return route_group_combo.get_active_text () == "separator";
602 }
603
604 PluginInfoPtr
605 AddRouteDialog::requested_instrument ()
606 {
607         return instrument_combo.selected_instrument();
608 }