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