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