8837e9f468f567b6fd3dbea0013f1caf59fc53f4
[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 "i18n.h"
43
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46 using namespace std;
47 using namespace PBD;
48 using namespace ARDOUR;
49
50 std::vector<std::string> AddRouteDialog::channel_combo_strings;
51
52 AddRouteDialog::AddRouteDialog ()
53         : ArdourDialog (_("Add Track or Bus"))
54         , routes_adjustment (1, 1, 128, 1, 4)
55         , routes_spinner (routes_adjustment)
56         , configuration_label (_("Configuration:"))
57         , mode_label (_("Track mode:"))
58         , instrument_label (_("Instrument:"))
59         , reasonable_synth_id(0)
60 {
61         set_name ("AddRouteDialog");
62         set_modal (true);
63         set_skip_taskbar_hint (true);
64         set_resizable (false);
65
66         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
67         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
68         channel_combo.set_name (X_("ChannelCountSelector"));
69         mode_combo.set_name (X_("ChannelCountSelector"));
70
71         refill_channel_setups ();
72         refill_route_groups ();
73         refill_track_modes ();
74
75         channel_combo.set_active_text (channel_combo_strings.front());
76
77         track_bus_combo.append_text (_("Audio Tracks"));
78         track_bus_combo.append_text (_("MIDI Tracks"));
79         track_bus_combo.append_text (_("Audio+MIDI Tracks"));
80         track_bus_combo.append_text (_("Busses"));
81         track_bus_combo.set_active (0);
82
83         build_instrument_list ();
84         instrument_combo.set_model (instrument_list);
85         instrument_combo.pack_start (instrument_list_columns.name);
86         instrument_combo.set_active (reasonable_synth_id);
87         instrument_combo.set_button_sensitivity (Gtk::SENSITIVITY_AUTO);
88
89         VBox* vbox = manage (new VBox);
90         Gtk::Label* l;
91
92         get_vbox()->set_spacing (4);
93
94         vbox->set_spacing (18);
95         vbox->set_border_width (5);
96
97         HBox *type_hbox = manage (new HBox);
98         type_hbox->set_spacing (6);
99
100         /* track/bus choice */
101
102         type_hbox->pack_start (*manage (new Label (_("Add:"))));
103         type_hbox->pack_start (routes_spinner);
104         type_hbox->pack_start (track_bus_combo);
105
106         vbox->pack_start (*type_hbox, false, true);
107
108         VBox* options_box = manage (new VBox);
109         Table *table2 = manage (new Table (3, 3, false));
110
111         options_box->set_spacing (6);
112         table2->set_row_spacings (6);
113         table2->set_col_spacing (1, 6);
114
115         l = manage (new Label (_("<b>Options</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
116         l->set_use_markup ();
117         options_box->pack_start (*l, false, true);
118
119         l = manage (new Label ("", Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
120         l->set_padding (8, 0);
121         table2->attach (*l, 0, 1, 0, 3, Gtk::FILL, Gtk::FILL, 0, 0);
122
123         int n = 0;
124
125         l = manage (new Label (_("Name:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
126         table2->attach (*l, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
127         table2->attach (name_template_entry, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
128         ++n;
129
130         /* Route configuration */
131
132         configuration_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
133         table2->attach (configuration_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
134         table2->attach (channel_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
135         ++n;
136
137         if (!ARDOUR::Profile->get_sae ()) {
138
139                 /* Track mode */
140
141                 mode_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
142                 table2->attach (mode_label, 1, 2, n, n + 1, Gtk::FILL, Gtk::EXPAND, 0, 0);
143                 table2->attach (mode_combo, 2, 3, n, n + 1, Gtk::FILL, Gtk::EXPAND | Gtk::FILL, 0, 0);
144                 ++n;
145
146         }
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         options_box->pack_start (*table2, false, true);
161         vbox->pack_start (*options_box, false, true);
162
163         get_vbox()->pack_start (*vbox, false, false);
164
165         track_bus_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::track_type_chosen));
166         channel_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::channel_combo_changed));
167         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
168         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
169         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
170
171         show_all_children ();
172
173         /* track template info will be managed whenever
174            this dialog is shown, via ::on_show()
175         */
176
177         add_button (Stock::CANCEL, RESPONSE_CANCEL);
178         add_button (Stock::ADD, RESPONSE_ACCEPT);
179
180         track_type_chosen ();
181 }
182
183 AddRouteDialog::~AddRouteDialog ()
184 {
185 }
186
187 void
188 AddRouteDialog::channel_combo_changed ()
189 {
190         maybe_update_name_template_entry ();
191         refill_track_modes ();
192 }
193
194 AddRouteDialog::TypeWanted
195 AddRouteDialog::type_wanted() const
196 {
197         switch (track_bus_combo.get_active_row_number ()) {
198         case 0:
199                 return AudioTrack;
200         case 1:
201                 return MidiTrack;
202         case 2:
203                 return MixedTrack;
204         default:
205                 break;
206         }
207
208         return AudioBus;
209 }
210
211 void
212 AddRouteDialog::maybe_update_name_template_entry ()
213 {
214         if (
215                 name_template_entry.get_text() != "" &&
216                 name_template_entry.get_text() != _("Audio") &&
217                 name_template_entry.get_text() != _("MIDI")  &&
218                 name_template_entry.get_text() != _("Audio+MIDI")  &&
219                 name_template_entry.get_text() != _("Bus")) {
220                 return;
221         }
222
223         switch (type_wanted()) {
224         case AudioTrack:
225                 name_template_entry.set_text (_("Audio"));
226                 break;
227         case MidiTrack:
228                 name_template_entry.set_text (_("MIDI"));
229                 break;
230         case MixedTrack:
231                 name_template_entry.set_text (_("Audio+MIDI"));
232                 break;
233         case AudioBus:
234                 name_template_entry.set_text (_("Bus"));
235                 break;
236         }
237 }
238
239 void
240 AddRouteDialog::track_type_chosen ()
241 {
242         switch (type_wanted()) {
243         case AudioTrack:
244                 mode_combo.set_sensitive (true);
245                 channel_combo.set_sensitive (true);
246                 instrument_combo.set_sensitive (false);
247                 configuration_label.set_sensitive (true);
248                 mode_label.set_sensitive (true);
249                 instrument_label.set_sensitive (false);
250                 break;
251         case MidiTrack:
252                 channel_combo.set_sensitive (false);
253                 mode_combo.set_sensitive (false);
254                 instrument_combo.set_sensitive (true);
255                 configuration_label.set_sensitive (false);
256                 mode_label.set_sensitive (false);
257                 instrument_label.set_sensitive (true);
258                 break;
259         case MixedTrack:
260                 {
261                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
262                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
263                                            true, MESSAGE_INFO, BUTTONS_OK, true);
264                         msg.set_position (WIN_POS_MOUSE);
265                         msg.run ();
266                 }
267                 channel_combo.set_sensitive (true);
268                 mode_combo.set_sensitive (true);
269                 instrument_combo.set_sensitive (true);
270                 configuration_label.set_sensitive (true);
271                 mode_label.set_sensitive (true);
272                 instrument_label.set_sensitive (true);
273                 break;
274         case AudioBus:
275                 mode_combo.set_sensitive (false);
276                 channel_combo.set_sensitive (true);
277                 instrument_combo.set_sensitive (false);
278                 configuration_label.set_sensitive (true);
279                 mode_label.set_sensitive (true);
280                 instrument_label.set_sensitive (false);
281                 break;
282         }
283
284         maybe_update_name_template_entry ();
285 }
286
287
288 string
289 AddRouteDialog::name_template () const
290 {
291         return name_template_entry.get_text ();
292 }
293
294 bool
295 AddRouteDialog::name_template_is_default() const
296 {
297         string n = name_template();
298
299         if (n == _("Audio") ||
300             n == _("MIDI") ||
301             n == _("Audio+MIDI") ||
302             n == _("Bus")) {
303                 return true;
304         }
305
306         return false;
307 }
308
309 int
310 AddRouteDialog::count ()
311 {
312         return (int) floor (routes_adjustment.get_value ());
313 }
314
315 void
316 AddRouteDialog::refill_track_modes ()
317 {
318         vector<string> s;
319         
320         s.push_back (_("Normal"));
321
322         if (!ARDOUR::Profile->get_sae ()) {
323                 s.push_back (_("Non Layered"));
324                 s.push_back (_("Tape"));
325         }
326
327         set_popdown_strings (mode_combo, s);
328         mode_combo.set_active_text (s.front());
329 }
330
331 ARDOUR::TrackMode
332 AddRouteDialog::mode ()
333 {
334         if (ARDOUR::Profile->get_sae()) {
335                 return ARDOUR::Normal;
336         }
337
338         std::string str = mode_combo.get_active_text();
339         if (str == _("Normal")) {
340                 return ARDOUR::Normal;
341         } else if (str == _("Non Layered")){
342                 return ARDOUR::NonLayered;
343         } else if (str == _("Tape")) {
344                 return ARDOUR::Destructive;
345         } else {
346                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
347                       << endmsg;
348                 /*NOTREACHED*/
349         }
350         /* keep gcc happy */
351         return ARDOUR::Normal;
352 }
353
354 ChanCount
355 AddRouteDialog::channels ()
356 {
357         ChanCount ret;
358         string str;
359         switch (type_wanted()) {
360         case AudioTrack:
361         case AudioBus:
362                 str = channel_combo.get_active_text();
363                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
364                         if (str == (*i).name) {
365                                 ret.set (DataType::AUDIO, (*i).channels);
366                                 break;
367                         }
368                 }
369                 ret.set (DataType::MIDI, 0);
370                 break;
371
372         case MidiTrack:
373                 ret.set (DataType::AUDIO, 0);
374                 ret.set (DataType::MIDI, 1);
375                 break;
376
377         case MixedTrack:
378                 str = channel_combo.get_active_text();
379                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
380                         if (str == (*i).name) {
381                                 ret.set (DataType::AUDIO, (*i).channels);
382                                 break;
383                         }
384                 }
385                 ret.set (DataType::MIDI, 1);
386                 break;
387         }
388                 
389         return ret;
390 }
391
392 string
393 AddRouteDialog::track_template ()
394 {
395         string str = channel_combo.get_active_text();
396
397         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
398                 if (str == (*i).name) {
399                         return (*i).template_path;
400                 }
401         }
402
403         return string();
404 }
405
406 void
407 AddRouteDialog::on_show ()
408 {
409         refill_channel_setups ();
410         refill_route_groups ();
411
412         Dialog::on_show ();
413 }
414
415 void
416 AddRouteDialog::refill_channel_setups ()
417 {
418         ChannelSetup chn;
419
420         route_templates.clear ();
421         channel_combo_strings.clear ();
422         channel_setups.clear ();
423
424         chn.name = _("Mono");
425         chn.channels = 1;
426         channel_setups.push_back (chn);
427
428         chn.name = _("Stereo");
429         chn.channels = 2;
430         channel_setups.push_back (chn);
431
432         chn.name = "separator";
433         channel_setups.push_back (chn);
434
435         ARDOUR::find_route_templates (route_templates);
436
437         if (!ARDOUR::Profile->get_sae()) {
438                 if (!route_templates.empty()) {
439                         vector<string> v;
440                         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
441                                 chn.name = x->name;
442                                 chn.channels = 0;
443                                 chn.template_path = x->path;
444                                 channel_setups.push_back (chn);
445                         }
446                 }
447
448                 /* clear template path for the rest */
449
450                 chn.template_path = "";
451
452                 chn.name = _("3 Channel");
453                 chn.channels = 3;
454                 channel_setups.push_back (chn);
455
456                 chn.name = _("4 Channel");
457                 chn.channels = 4;
458                 channel_setups.push_back (chn);
459
460                 chn.name = _("5 Channel");
461                 chn.channels = 5;
462                 channel_setups.push_back (chn);
463
464                 chn.name = _("6 Channel");
465                 chn.channels = 6;
466                 channel_setups.push_back (chn);
467
468                 chn.name = _("8 Channel");
469                 chn.channels = 8;
470                 channel_setups.push_back (chn);
471
472                 chn.name = _("12 Channel");
473                 chn.channels = 12;
474                 channel_setups.push_back (chn);
475
476                 chn.name = _("Custom");
477                 chn.channels = 0;
478                 channel_setups.push_back (chn);
479         }
480
481         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
482                 channel_combo_strings.push_back ((*i).name);
483         }
484
485         set_popdown_strings (channel_combo, channel_combo_strings);
486         channel_combo.set_active_text (channel_combo_strings.front());
487 }
488
489 void
490 AddRouteDialog::add_route_group (RouteGroup* g)
491 {
492         route_group_combo.insert_text (3, g->name ());
493 }
494
495 RouteGroup*
496 AddRouteDialog::route_group ()
497 {
498         if (!_session || route_group_combo.get_active_row_number () == 2) {
499                 return 0;
500         }
501
502         return _session->route_group_by_name (route_group_combo.get_active_text());
503 }
504
505 void
506 AddRouteDialog::refill_route_groups ()
507 {
508         route_group_combo.clear ();
509         route_group_combo.append_text (_("New Group..."));
510
511         route_group_combo.append_text ("separator");
512
513         route_group_combo.append_text (_("No Group"));
514
515         if (_session) {
516                 _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
517         }
518
519         route_group_combo.set_active (2);
520 }
521
522 void
523 AddRouteDialog::group_changed ()
524 {
525         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
526                 RouteGroup* g = new RouteGroup (*_session, "");
527
528                 PropertyList plist;
529                 plist.add (Properties::active, true);
530                 g->apply_changes (plist);
531
532                 RouteGroupDialog d (g, true);
533
534                 if (d.do_run ()) {
535                         delete g;
536                         route_group_combo.set_active (2);
537                 } else {
538                         if (_session) {
539                                 _session->add_route_group (g);
540                         }
541                         add_route_group (g);
542                         route_group_combo.set_active (3);
543                 }
544         }
545 }
546
547 bool
548 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
549 {
550         channel_combo.set_active (i);
551
552         return channel_combo.get_active_text () == "separator";
553 }
554
555 bool
556 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
557 {
558         route_group_combo.set_active (i);
559
560         return route_group_combo.get_active_text () == "separator";
561 }
562
563 void
564 AddRouteDialog::build_instrument_list ()
565 {
566         PluginInfoList all_plugs;
567         PluginManager& manager (PluginManager::instance());
568         TreeModel::Row row;
569
570         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
571 #ifdef WINDOWS_VST_SUPPORT
572         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
573 #endif
574 #ifdef LXVST_SUPPORT
575         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
576 #endif
577 #ifdef AUDIOUNIT_SUPPORT
578         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
579 #endif
580 #ifdef LV2_SUPPORT
581         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
582 #endif
583
584
585         instrument_list = ListStore::create (instrument_list_columns);
586
587         row = *(instrument_list->append());
588         row[instrument_list_columns.info_ptr] = PluginInfoPtr ();
589         row[instrument_list_columns.name] = _("-none-");
590
591         uint32_t n = 1;
592         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
593
594                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
595
596                 if ((*i)->is_instrument()) {
597                         row = *(instrument_list->append());
598                         row[instrument_list_columns.name] = (*i)->name;
599                         row[instrument_list_columns.info_ptr] = *i;
600                         if ((*i)->unique_id == "https://community.ardour.org/node/7596") {
601                                 reasonable_synth_id = n;
602                         }
603                         n++;
604                 }
605         }
606 }
607
608 PluginInfoPtr
609 AddRouteDialog::requested_instrument ()
610 {
611         TreeModel::iterator iter = instrument_combo.get_active ();
612         TreeModel::Row row;
613         
614         if (iter) {
615                 row = (*iter);
616                 return row[instrument_list_columns.info_ptr];
617         }
618
619         return PluginInfoPtr();
620 }