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