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