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