Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / plugin_setup_dialog.cc
1 /*
2  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <gtkmm/frame.h>
20 #include <gtkmm/label.h>
21 #include <gtkmm/stock.h>
22 #include <gtkmm/table.h>
23
24 #include "plugin_setup_dialog.h"
25 #include "pbd/i18n.h"
26
27 using namespace ARDOUR;
28 using namespace ArdourWidgets;
29 using namespace Gtk;
30
31 PluginSetupDialog::PluginSetupDialog (boost::shared_ptr<ARDOUR::Route> r, boost::shared_ptr<ARDOUR::PluginInsert> pi, ARDOUR::Route::PluginSetupOptions flags)
32         : ArdourDialog (_("Plugin Setup"), true, false)
33         , _route (r)
34         , _pi (pi)
35         , _keep_mapping (_("Copy I/O Map"), ArdourButton::led_default_elements)
36         , _fan_out (_("Fan out"), ArdourButton::led_default_elements)
37 {
38         assert (flags != Route::None);
39
40         Gtk::Table *tbl = manage (new Gtk::Table ());
41         tbl->set_spacings (6);
42         get_vbox()->pack_start (*tbl);
43         int row = 0;
44
45         if (flags & Route::CanReplace) {
46                 boost::shared_ptr<Processor> old = _route->the_instrument ();
47                 boost::shared_ptr<PluginInsert> opi = boost::dynamic_pointer_cast<PluginInsert> (old);
48                 assert (opi);
49
50                 opi->configured_io (_cur_inputs, _cur_outputs);
51
52                 Gtk::Label* l = manage (new Label (
53                                                 _("An Instrument plugin is already present.")
54                                                 ));
55                 tbl->attach (*l, 0, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
56
57                 l = manage (new Label (_("Replace"), ALIGN_END));
58                 tbl->attach (*l, 0, 1, row, row + 1, EXPAND|FILL, SHRINK);
59
60                 l = manage (new Label (string_compose ("'%1'", old->name ()), ALIGN_START));
61                 tbl->attach (*l, 1, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
62
63                 l = manage (new Label (_("with"), ALIGN_END));
64                 tbl->attach (*l, 0, 1, row, row + 1, EXPAND|FILL, SHRINK);
65
66                 l = manage (new Label (string_compose ("'%1'", pi->name ()), ALIGN_START));
67                 tbl->attach (*l, 1, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
68
69                 Box* box = manage (new HBox ());
70                 box->set_border_width (2);
71                 box->pack_start (_keep_mapping, true, true);
72                 Frame* f = manage (new Frame ());
73                 f->set_label (_("I/O Pin Mapping"));
74                 f->add (*box);
75                 tbl->attach (*f, 0, 1, row, row + 1, EXPAND|FILL, SHRINK, 0, 8);
76
77                 _keep_mapping.signal_clicked.connect (sigc::mem_fun (*this, &PluginSetupDialog::apply_mapping));
78                 add_button ("Replace", 2);
79         } else {
80
81                 Gtk::Label *l = manage (new Label (string_compose (
82                                                 _("Configure Plugin '%1'"), pi->name ()
83                                                 )));
84                 tbl->attach (*l, 0, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
85         }
86
87         if (flags & Route::MultiOut) {
88                 setup_output_presets ();
89                 Box* box = manage (new HBox ());
90                 box->set_border_width (2);
91                 box->pack_start (_out_presets, true, true);
92                 box->pack_start (_fan_out, false, false);
93                 Frame* f = manage (new Frame ());
94                 f->set_label (_("Output Configuration"));
95                 f->add (*box);
96                 tbl->attach (*f, 1, 2, row, row + 1, EXPAND|FILL, SHRINK, 0, 8);
97                 _fan_out.signal_clicked.connect (sigc::mem_fun (*this, &PluginSetupDialog::toggle_fan_out));
98         } else {
99                 _pi->set_preset_out (_pi->natural_output_streams ());
100                 update_sensitivity (_pi->natural_output_streams ().n_audio ());
101         }
102
103         _keep_mapping.set_active (false);
104         _fan_out.set_active (false);
105         apply_mapping ();
106
107         add_button (Stock::ADD, 0);
108         add_button (Stock::CANCEL, 1);
109         set_default_response (0);
110         show_all ();
111 }
112
113
114 void
115 PluginSetupDialog::setup_output_presets ()
116 {
117         // compare to PluginPinDialog::refill_output_presets ()
118         using namespace Menu_Helpers;
119         PluginOutputConfiguration ppc (_pi->plugin (0)->possible_output ());
120
121         _out_presets.AddMenuElem (MenuElem (_("Automatic"), sigc::bind (sigc::mem_fun (*this, &PluginSetupDialog::select_output_preset), 0)));
122
123         if (ppc.find (0) != ppc.end ()) {
124                 // anyting goes
125                 ppc.clear ();
126                 ppc.insert (1);
127                 ppc.insert (2);
128                 ppc.insert (8);
129                 ppc.insert (16);
130                 ppc.insert (24);
131                 ppc.insert (32);
132                 if (ppc.find (_cur_outputs.n_audio ()) == ppc.end ()) {
133                         ppc.insert (_cur_outputs.n_audio ());
134                 }
135         }
136
137         bool have_matching_io = false;
138
139         for (PluginOutputConfiguration::const_iterator i = ppc.begin () ; i != ppc.end (); ++i) {
140                 assert (*i > 0);
141                 _out_presets.AddMenuElem (MenuElem (preset_label (*i), sigc::bind (sigc::mem_fun (*this, &PluginSetupDialog::select_output_preset), *i)));
142                 if (*i == _cur_outputs.n_audio ()) {
143                         have_matching_io = true;
144                 }
145         }
146
147         if (have_matching_io) {
148                 select_output_preset (_cur_outputs.n_audio ());
149         } else {
150                 select_output_preset (0);
151         }
152 }
153
154 void
155 PluginSetupDialog::select_output_preset (uint32_t n_audio)
156 {
157         _pi->set_preset_out (ChanCount (DataType::AUDIO, n_audio));
158         _out_presets.set_text (preset_label (n_audio));
159         update_sensitivity (n_audio);
160 }
161
162 void
163 PluginSetupDialog::update_sensitivity (uint32_t n_audio)
164 {
165         if (_cur_outputs.n_audio () > 0 && _cur_outputs.n_audio () == n_audio) {
166                 // TODO check _cur_inputs if not reconfigurable?
167                 _keep_mapping.set_sensitive (true);
168         } else {
169                 _keep_mapping.set_sensitive (false);
170         }
171         _fan_out.set_sensitive (n_audio > 2);
172 }
173
174 bool
175 PluginSetupDialog::io_match () const
176 {
177         if (_cur_outputs.n_audio () > 0 && _cur_outputs.n_audio () == _pi->preset_out ().n_audio ()) {
178                 return true;
179         } else {
180                 return false;
181         }
182 }
183
184 void
185 PluginSetupDialog::apply_mapping ()
186 {
187         // toggle button
188         _keep_mapping.set_active (!_keep_mapping.get_active ());
189
190         boost::shared_ptr<Processor> old = _route->the_instrument ();
191         boost::shared_ptr<PluginInsert> opi = boost::dynamic_pointer_cast<PluginInsert> (old);
192
193         if (_keep_mapping.get_active () && opi && io_match ()) {
194                 _pi->pre_seed (_cur_inputs, _cur_outputs, opi->input_map (0), opi->output_map (0), opi->thru_map ());
195         } else {
196                 _pi->pre_seed (ChanCount (), ChanCount (), ChanMapping (), ChanMapping (), ChanMapping());
197         }
198 }
199
200 void
201 PluginSetupDialog::toggle_fan_out ()
202 {
203         _fan_out.set_active (!_fan_out.get_active ());
204 }
205
206 std::string
207 PluginSetupDialog::preset_label (uint32_t n_audio) const
208 {
209                 std::string rv;
210                 switch (n_audio) {
211                         case 0:
212                                 rv = _("Automatic");
213                                 break;
214                         case 1:
215                                 rv = _("Mono");
216                                 break;
217                         case 2:
218                                 rv = _("Stereo");
219                                 break;
220                         default:
221                                 rv = string_compose (P_("%1 Channel", "%1 Channels", n_audio), n_audio);
222                                 break;
223                 }
224                 return rv;
225 }