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