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