remove all of Gtk::Window::set_position (WIN_POS_MOUSE) for anything deriving from...
[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
50 std::vector<std::string> AddRouteDialog::channel_combo_strings;
51
52 AddRouteDialog::AddRouteDialog (Session* s)
53         : ArdourDialog (_("Add Track or Bus"))
54         , routes_adjustment (1, 1, 128, 1, 4)
55         , routes_spinner (routes_adjustment)
56         , configuration_label (_("Configuration:"))
57         , mode_label (_("Track mode:"))
58         , instrument_label (_("Instrument:"))
59 {
60         set_session (s);
61
62         set_name ("AddRouteDialog");
63         set_modal (true);
64         set_skip_taskbar_hint (true);
65         set_resizable (false);
66
67         name_template_entry.set_name (X_("AddRouteDialogNameTemplateEntry"));
68         // routes_spinner.set_name (X_("AddRouteDialogSpinner"));
69         channel_combo.set_name (X_("ChannelCountSelector"));
70         mode_combo.set_name (X_("ChannelCountSelector"));
71
72         refill_channel_setups ();
73         refill_route_groups ();
74         refill_track_modes ();
75
76         channel_combo.set_active_text (channel_combo_strings.front());
77
78         track_bus_combo.append_text (_("Audio Tracks"));
79         track_bus_combo.append_text (_("MIDI Tracks"));
80         track_bus_combo.append_text (_("Audio+MIDI Tracks"));
81         track_bus_combo.append_text (_("Busses"));
82         track_bus_combo.set_active (0);
83
84         build_instrument_list ();
85         instrument_combo.set_model (instrument_list);
86         instrument_combo.pack_start (instrument_list_columns.name);
87         instrument_combo.set_active (0);
88         instrument_combo.set_button_sensitivity (Gtk::SENSITIVITY_AUTO);
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         options_box->pack_start (*table2, false, true);
162         vbox->pack_start (*options_box, false, true);
163
164         get_vbox()->pack_start (*vbox, false, false);
165
166         track_bus_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::track_type_chosen));
167         channel_combo.signal_changed().connect (sigc::mem_fun (*this, &AddRouteDialog::channel_combo_changed));
168         channel_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::channel_separator));
169         route_group_combo.set_row_separator_func (sigc::mem_fun (*this, &AddRouteDialog::route_separator));
170         route_group_combo.signal_changed ().connect (sigc::mem_fun (*this, &AddRouteDialog::group_changed));
171
172         show_all_children ();
173
174         /* track template info will be managed whenever
175            this dialog is shown, via ::on_show()
176         */
177
178         add_button (Stock::CANCEL, RESPONSE_CANCEL);
179         add_button (Stock::ADD, RESPONSE_ACCEPT);
180
181         track_type_chosen ();
182 }
183
184 AddRouteDialog::~AddRouteDialog ()
185 {
186 }
187
188 void
189 AddRouteDialog::channel_combo_changed ()
190 {
191         maybe_update_name_template_entry ();
192         refill_track_modes ();
193 }
194
195 AddRouteDialog::TypeWanted
196 AddRouteDialog::type_wanted() const
197 {
198         switch (track_bus_combo.get_active_row_number ()) {
199         case 0:
200                 return AudioTrack;
201         case 1:
202                 return MidiTrack;
203         case 2:
204                 return MixedTrack;
205         default:
206                 break;
207         }
208
209         return AudioBus;
210 }
211
212 void
213 AddRouteDialog::maybe_update_name_template_entry ()
214 {
215         if (
216                 name_template_entry.get_text() != "" &&
217                 name_template_entry.get_text() != _("Audio") &&
218                 name_template_entry.get_text() != _("MIDI")  &&
219                 name_template_entry.get_text() != _("Audio+MIDI")  &&
220                 name_template_entry.get_text() != _("Bus")) {
221                 return;
222         }
223
224         switch (type_wanted()) {
225         case AudioTrack:
226                 name_template_entry.set_text (_("Audio"));
227                 break;
228         case MidiTrack:
229                 name_template_entry.set_text (_("MIDI"));
230                 break;
231         case MixedTrack:
232                 name_template_entry.set_text (_("Audio+MIDI"));
233                 break;
234         case AudioBus:
235                 name_template_entry.set_text (_("Bus"));
236                 break;
237         }
238 }
239
240 void
241 AddRouteDialog::track_type_chosen ()
242 {
243         switch (type_wanted()) {
244         case AudioTrack:
245                 mode_combo.set_sensitive (true);
246                 channel_combo.set_sensitive (true);
247                 instrument_combo.set_sensitive (false);
248                 configuration_label.set_sensitive (true);
249                 mode_label.set_sensitive (true);
250                 instrument_label.set_sensitive (false);
251                 break;
252         case MidiTrack:
253                 channel_combo.set_sensitive (false);
254                 mode_combo.set_sensitive (false);
255                 instrument_combo.set_sensitive (true);
256                 configuration_label.set_sensitive (false);
257                 mode_label.set_sensitive (false);
258                 instrument_label.set_sensitive (true);
259                 break;
260         case MixedTrack:
261                 {
262                         MessageDialog msg (_("Audio+MIDI tracks are intended for use <b>ONLY</b> with plugins that use both audio and MIDI input data\n\n"
263                                              "If you do not plan to use such a plugin, then use a normal audio or MIDI track instead."),
264                                            true, MESSAGE_INFO, BUTTONS_OK, true);
265                         msg.set_position (WIN_POS_MOUSE);
266                         msg.run ();
267                 }
268                 channel_combo.set_sensitive (true);
269                 mode_combo.set_sensitive (true);
270                 instrument_combo.set_sensitive (true);
271                 configuration_label.set_sensitive (true);
272                 mode_label.set_sensitive (true);
273                 instrument_label.set_sensitive (true);
274                 break;
275         case AudioBus:
276                 mode_combo.set_sensitive (false);
277                 channel_combo.set_sensitive (true);
278                 instrument_combo.set_sensitive (false);
279                 configuration_label.set_sensitive (true);
280                 mode_label.set_sensitive (true);
281                 instrument_label.set_sensitive (false);
282                 break;
283         }
284
285         maybe_update_name_template_entry ();
286 }
287
288
289 string
290 AddRouteDialog::name_template () const
291 {
292         return name_template_entry.get_text ();
293 }
294
295 bool
296 AddRouteDialog::name_template_is_default() const
297 {
298         string n = name_template();
299
300         if (n == _("Audio") ||
301             n == _("MIDI") ||
302             n == _("Audio+MIDI") ||
303             n == _("Bus")) {
304                 return true;
305         }
306
307         return false;
308 }
309
310 int
311 AddRouteDialog::count ()
312 {
313         return (int) floor (routes_adjustment.get_value ());
314 }
315
316 void
317 AddRouteDialog::refill_track_modes ()
318 {
319         vector<string> s;
320         
321         s.push_back (_("Normal"));
322
323         if (!ARDOUR::Profile->get_sae ()) {
324                 s.push_back (_("Non Layered"));
325                 s.push_back (_("Tape"));
326         }
327
328         set_popdown_strings (mode_combo, s);
329         mode_combo.set_active_text (s.front());
330 }
331
332 ARDOUR::TrackMode
333 AddRouteDialog::mode ()
334 {
335         if (ARDOUR::Profile->get_sae()) {
336                 return ARDOUR::Normal;
337         }
338
339         std::string str = mode_combo.get_active_text();
340         if (str == _("Normal")) {
341                 return ARDOUR::Normal;
342         } else if (str == _("Non Layered")){
343                 return ARDOUR::NonLayered;
344         } else if (str == _("Tape")) {
345                 return ARDOUR::Destructive;
346         } else {
347                 fatal << string_compose (X_("programming error: unknown track mode in add route dialog combo = %1"), str)
348                       << endmsg;
349                 /*NOTREACHED*/
350         }
351         /* keep gcc happy */
352         return ARDOUR::Normal;
353 }
354
355 ChanCount
356 AddRouteDialog::channels ()
357 {
358         ChanCount ret;
359         string str;
360         switch (type_wanted()) {
361         case AudioTrack:
362         case AudioBus:
363                 str = channel_combo.get_active_text();
364                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
365                         if (str == (*i).name) {
366                                 ret.set (DataType::AUDIO, (*i).channels);
367                                 break;
368                         }
369                 }
370                 ret.set (DataType::MIDI, 0);
371                 break;
372
373         case MidiTrack:
374                 ret.set (DataType::AUDIO, 0);
375                 ret.set (DataType::MIDI, 1);
376                 break;
377
378         case MixedTrack:
379                 str = channel_combo.get_active_text();
380                 for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
381                         if (str == (*i).name) {
382                                 ret.set (DataType::AUDIO, (*i).channels);
383                                 break;
384                         }
385                 }
386                 ret.set (DataType::MIDI, 1);
387                 break;
388         }
389                 
390         return ret;
391 }
392
393 string
394 AddRouteDialog::track_template ()
395 {
396         string str = channel_combo.get_active_text();
397
398         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
399                 if (str == (*i).name) {
400                         return (*i).template_path;
401                 }
402         }
403
404         return string();
405 }
406
407 void
408 AddRouteDialog::on_show ()
409 {
410         refill_channel_setups ();
411         refill_route_groups ();
412
413         Dialog::on_show ();
414 }
415
416 void
417 AddRouteDialog::refill_channel_setups ()
418 {
419         ChannelSetup chn;
420
421         route_templates.clear ();
422         channel_combo_strings.clear ();
423         channel_setups.clear ();
424
425         chn.name = _("Mono");
426         chn.channels = 1;
427         channel_setups.push_back (chn);
428
429         chn.name = _("Stereo");
430         chn.channels = 2;
431         channel_setups.push_back (chn);
432
433         chn.name = "separator";
434         channel_setups.push_back (chn);
435
436         ARDOUR::find_route_templates (route_templates);
437
438         if (!ARDOUR::Profile->get_sae()) {
439                 if (!route_templates.empty()) {
440                         vector<string> v;
441                         for (vector<TemplateInfo>::iterator x = route_templates.begin(); x != route_templates.end(); ++x) {
442                                 chn.name = x->name;
443                                 chn.channels = 0;
444                                 chn.template_path = x->path;
445                                 channel_setups.push_back (chn);
446                         }
447                 }
448
449                 /* clear template path for the rest */
450
451                 chn.template_path = "";
452
453                 chn.name = _("3 Channel");
454                 chn.channels = 3;
455                 channel_setups.push_back (chn);
456
457                 chn.name = _("4 Channel");
458                 chn.channels = 4;
459                 channel_setups.push_back (chn);
460
461                 chn.name = _("5 Channel");
462                 chn.channels = 5;
463                 channel_setups.push_back (chn);
464
465                 chn.name = _("6 Channel");
466                 chn.channels = 6;
467                 channel_setups.push_back (chn);
468
469                 chn.name = _("8 Channel");
470                 chn.channels = 8;
471                 channel_setups.push_back (chn);
472
473                 chn.name = _("12 Channel");
474                 chn.channels = 12;
475                 channel_setups.push_back (chn);
476
477                 chn.name = _("Custom");
478                 chn.channels = 0;
479                 channel_setups.push_back (chn);
480         }
481
482         for (ChannelSetups::iterator i = channel_setups.begin(); i != channel_setups.end(); ++i) {
483                 channel_combo_strings.push_back ((*i).name);
484         }
485
486         set_popdown_strings (channel_combo, channel_combo_strings);
487         channel_combo.set_active_text (channel_combo_strings.front());
488 }
489
490 void
491 AddRouteDialog::add_route_group (RouteGroup* g)
492 {
493         route_group_combo.insert_text (3, g->name ());
494 }
495
496 RouteGroup*
497 AddRouteDialog::route_group ()
498 {
499         if (route_group_combo.get_active_row_number () == 2) {
500                 return 0;
501         }
502
503         return _session->route_group_by_name (route_group_combo.get_active_text());
504 }
505
506 void
507 AddRouteDialog::refill_route_groups ()
508 {
509         route_group_combo.clear ();
510         route_group_combo.append_text (_("New Group..."));
511
512         route_group_combo.append_text ("separator");
513
514         route_group_combo.append_text (_("No Group"));
515
516         _session->foreach_route_group (sigc::mem_fun (*this, &AddRouteDialog::add_route_group));
517
518         route_group_combo.set_active (2);
519 }
520
521 void
522 AddRouteDialog::group_changed ()
523 {
524         if (_session && route_group_combo.get_active_text () == _("New Group...")) {
525                 RouteGroup* g = new RouteGroup (*_session, "");
526
527                 PropertyList plist;
528                 plist.add (Properties::active, true);
529                 g->apply_changes (plist);
530
531                 RouteGroupDialog d (g, true);
532
533                 if (d.do_run ()) {
534                         delete g;
535                         route_group_combo.set_active (2);
536                 } else {
537                         _session->add_route_group (g);
538                         add_route_group (g);
539                         route_group_combo.set_active (3);
540                 }
541         }
542 }
543
544 bool
545 AddRouteDialog::channel_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
546 {
547         channel_combo.set_active (i);
548
549         return channel_combo.get_active_text () == "separator";
550 }
551
552 bool
553 AddRouteDialog::route_separator (const Glib::RefPtr<Gtk::TreeModel> &, const Gtk::TreeModel::iterator &i)
554 {
555         route_group_combo.set_active (i);
556
557         return route_group_combo.get_active_text () == "separator";
558 }
559
560 void
561 AddRouteDialog::build_instrument_list ()
562 {
563         PluginInfoList all_plugs;
564         PluginManager& manager (PluginManager::instance());
565         TreeModel::Row row;
566
567         all_plugs.insert (all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
568 #ifdef WINDOWS_VST_SUPPORT
569         all_plugs.insert (all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
570 #endif
571 #ifdef LXVST_SUPPORT
572         all_plugs.insert (all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
573 #endif
574 #ifdef AUDIOUNIT_SUPPORT
575         all_plugs.insert (all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
576 #endif
577 #ifdef LV2_SUPPORT
578         all_plugs.insert (all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
579 #endif
580
581
582         instrument_list = ListStore::create (instrument_list_columns);
583
584         row = *(instrument_list->append());
585         row[instrument_list_columns.info_ptr] = PluginInfoPtr ();
586         row[instrument_list_columns.name] = _("-none-");
587
588         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
589
590                 if (manager.get_status (*i) == PluginManager::Hidden) continue;
591
592                 if ((*i)->is_instrument()) {
593                         row = *(instrument_list->append());
594                         row[instrument_list_columns.name] = (*i)->name;
595                         row[instrument_list_columns.info_ptr] = *i;
596                 }
597         }
598 }
599
600 PluginInfoPtr
601 AddRouteDialog::requested_instrument ()
602 {
603         TreeModel::iterator iter = instrument_combo.get_active ();
604         TreeModel::Row row;
605         
606         if (iter) {
607                 row = (*iter);
608                 return row[instrument_list_columns.info_ptr];
609         }
610
611         return PluginInfoPtr();
612 }