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