add virtual method is_instrument() to PluginInfo (already existed for AUPluginInfo...
[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 (_("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 (0);
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 void
195 AddRouteDialog::maybe_update_name_template_entry ()
196 {
197         if (
198                 name_template_entry.get_text() != "" &&
199                 name_template_entry.get_text() != _("Audio") &&
200                 name_template_entry.get_text() != _("MIDI")  &&
201                 name_template_entry.get_text() != _("Bus")) {
202                 return;
203         }
204
205         if (audio_tracks_wanted ()) {
206                 name_template_entry.set_text (_("Audio"));
207         } else if (midi_tracks_wanted()) {
208                 name_template_entry.set_text (_("MIDI"));
209         } else {
210                 name_template_entry.set_text (_("Bus"));
211         }
212 }
213
214 void
215 AddRouteDialog::track_type_chosen ()
216 {
217         if (midi_tracks_wanted()) {
218                 channel_combo.set_sensitive (false);
219                 mode_combo.set_sensitive (false);
220                 instrument_combo.set_sensitive (true);
221                 configuration_label.set_sensitive (false);
222                 mode_label.set_sensitive (false);
223                 instrument_label.set_sensitive (true);
224         } else if (audio_tracks_wanted()) {
225                 mode_combo.set_sensitive (true);
226                 channel_combo.set_sensitive (true);
227                 instrument_combo.set_sensitive (false);
228                 configuration_label.set_sensitive (true);
229                 mode_label.set_sensitive (true);
230                 instrument_label.set_sensitive (false);
231         } else {
232                 mode_combo.set_sensitive (false);
233                 channel_combo.set_sensitive (true);
234                 instrument_combo.set_sensitive (false);
235                 configuration_label.set_sensitive (true);
236                 mode_label.set_sensitive (true);
237                 instrument_label.set_sensitive (false);
238         }
239
240         maybe_update_name_template_entry ();
241 }
242
243 bool
244 AddRouteDialog::audio_tracks_wanted ()
245 {
246         return track_bus_combo.get_active_row_number () == 0;
247 }
248
249 bool
250 AddRouteDialog::midi_tracks_wanted ()
251 {
252         return track_bus_combo.get_active_row_number () == 1;
253 }
254
255 string
256 AddRouteDialog::name_template ()
257 {
258         return name_template_entry.get_text ();
259 }
260
261 int
262 AddRouteDialog::count ()
263 {
264         return (int) floor (routes_adjustment.get_value ());
265 }
266
267 void
268 AddRouteDialog::refill_track_modes ()
269 {
270         vector<string> s;
271         
272         s.push_back (_("Normal"));
273
274         if (!ARDOUR::Profile->get_sae ()) {
275                 s.push_back (_("Non Layered"));
276                 s.push_back (_("Tape"));
277         }
278
279         set_popdown_strings (mode_combo, s);
280         mode_combo.set_active_text (s.front());
281 }
282
283 ARDOUR::TrackMode
284 AddRouteDialog::mode ()
285 {
286         if (ARDOUR::Profile->get_sae()) {
287                 return ARDOUR::Normal;
288         }
289
290         std::string str = mode_combo.get_active_text();
291         if (str == _("Normal")) {
292                 return ARDOUR::Normal;
293         } else if (str == _("Non Layered")){
294                 return ARDOUR::NonLayered;
295         } else if (str == _("Tape")) {
296                 return ARDOUR::Destructive;
297         } else {
298                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
299                       << endmsg;
300                 /*NOTREACHED*/
301         }
302         /* keep gcc happy */
303         return ARDOUR::Normal;
304 }
305
306 int
307 AddRouteDialog::channels ()
308 {
309         string str = channel_combo.get_active_text();
310
311         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
312                 if (str == (*i).name) {
313                         return (*i).channels;
314                 }
315         }
316
317         return 0;
318 }
319
320 string
321 AddRouteDialog::track_template ()
322 {
323         string str = channel_combo.get_active_text();
324
325         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
326                 if (str == (*i).name) {
327                         return (*i).template_path;
328                 }
329         }
330
331         return string();
332 }
333
334 void
335 AddRouteDialog::on_show ()
336 {
337         refill_channel_setups ();
338         refill_route_groups ();
339
340         Dialog::on_show ();
341 }
342
343 void
344 AddRouteDialog::refill_channel_setups ()
345 {
346         ChannelSetup chn;
347
348         route_templates.clear ();
349         channel_combo_strings.clear ();
350         channel_setups.clear ();
351
352         chn.name = _("Mono");
353         chn.channels = 1;
354         channel_setups.push_back (chn);
355
356         chn.name = _("Stereo");
357         chn.channels = 2;
358         channel_setups.push_back (chn);
359
360         chn.name = "separator";
361         channel_setups.push_back (chn);
362
363         ARDOUR::find_route_templates (route_templates);
364
365         if (!ARDOUR::Profile->get_sae()) {
366                 if (!route_templates.empty()) {
367                         vector<string> v;
368                         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
369                                 chn.name = x->name;
370                                 chn.channels = 0;
371                                 chn.template_path = x->path;
372                                 channel_setups.push_back (chn);
373                         }
374                 }
375
376                 /* clear template path for the rest */
377
378                 chn.template_path = "";
379
380                 chn.name = _("3 Channel");
381                 chn.channels = 3;
382                 channel_setups.push_back (chn);
383
384                 chn.name = _("4 Channel");
385                 chn.channels = 4;
386                 channel_setups.push_back (chn);
387
388                 chn.name = _("5 Channel");
389                 chn.channels = 5;
390                 channel_setups.push_back (chn);
391
392                 chn.name = _("6 Channel");
393                 chn.channels = 6;
394                 channel_setups.push_back (chn);
395
396                 chn.name = _("8 Channel");
397                 chn.channels = 8;
398                 channel_setups.push_back (chn);
399
400                 chn.name = _("12 Channel");
401                 chn.channels = 12;
402                 channel_setups.push_back (chn);
403
404                 chn.name = _("Custom");
405                 chn.channels = 0;
406                 channel_setups.push_back (chn);
407         }
408
409         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
410                 channel_combo_strings.push_back ((*i).name);
411         }
412
413         set_popdown_strings (channel_combo, channel_combo_strings);
414         channel_combo.set_active_text (channel_combo_strings.front());
415 }
416
417 void
418 AddRouteDialog::add_route_group (RouteGroup* g)
419 {
420         route_group_combo.insert_text (3, g->name ());
421 }
422
423 RouteGroup*
424 AddRouteDialog::route_group ()
425 {
426         if (route_group_combo.get_active_row_number () == 2) {
427                 return 0;
428         }
429
430         return _session->route_group_by_name (route_group_combo.get_active_text());
431 }
432
433 void
434 AddRouteDialog::refill_route_groups ()
435 {
436         route_group_combo.clear ();
437         route_group_combo.append_text (_("New Group..."));
438
439         route_group_combo.append_text ("separator");
440
441         route_group_combo.append_text (_("No Group"));
442
443         _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
444
445         route_group_combo.set_active (2);
446 }
447
448 void
449 AddRouteDialog::group_changed ()
450 {
451         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
452                 RouteGroup* g = new RouteGroup (*_session, "");
453
454                 PropertyList plist;
455                 plist.add (Properties::active, true);
456                 g->apply_changes (plist);
457
458                 RouteGroupDialog d (g, true);
459
460                 if (d.do_run ()) {
461                         delete g;
462                         route_group_combo.set_active (2);
463                 } else {
464                         _session->add_route_group (g);
465                         add_route_group (g);
466                         route_group_combo.set_active (3);
467                 }
468         }
469 }
470
471 bool
472 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
473 {
474         channel_combo.set_active (i);
475
476         return channel_combo.get_active_text () == "separator";
477 }
478
479 bool
480 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
481 {
482         route_group_combo.set_active (i);
483
484         return route_group_combo.get_active_text () == "separator";
485 }
486
487 void
488 AddRouteDialog::build_instrument_list ()
489 {
490         PluginInfoList all_plugs;
491         PluginManager& manager (PluginManager::instance());
492         TreeModel::Row row;
493
494         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
495 #ifdef WINDOWS_VST_SUPPORT
496         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
497 #endif
498 #ifdef LXVST_SUPPORT
499         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
500 #endif
501 #ifdef AUDIOUNIT_SUPPORT
502         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
503 #endif
504 #ifdef LV2_SUPPORT
505         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
506 #endif
507
508
509         instrument_list = ListStore::create (instrument_list_columns);
510
511         row = *(instrument_list->append());
512         row[instrument_list_columns.info_ptr] = PluginInfoPtr ();
513         row[instrument_list_columns.name] = _("-none-");
514
515         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
516
517                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
518
519                 if ((*i)->is_instrument()) {
520                         row = *(instrument_list->append());
521                         row[instrument_list_columns.name] = (*i)->name;
522                         row[instrument_list_columns.info_ptr] = *i;
523                 }
524         }
525 }
526
527 PluginInfoPtr
528 AddRouteDialog::requested_instrument ()
529 {
530         TreeModel::iterator iter = instrument_combo.get_active ();
531         TreeModel::Row row;
532         
533         if (iter) {
534                 row = (*iter);
535                 return row[instrument_list_columns.info_ptr];
536         }
537
538         return PluginInfoPtr();
539 }