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