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