Fix thinkos in cubasish theme
[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                 _fan_out.set_active (true);
99         } else {
100                 _pi->set_preset_out (_pi->natural_output_streams ());
101                 update_sensitivity (_pi->natural_output_streams ().n_audio ());
102                 _fan_out.set_active (false);
103         }
104
105         _keep_mapping.set_active (false);
106         apply_mapping ();
107
108         add_button (Stock::ADD, 0);
109         add_button (Stock::CANCEL, 1);
110         set_default_response (0);
111         show_all ();
112 }
113
114
115 void
116 PluginSetupDialog::setup_output_presets ()
117 {
118         // compare to PluginPinDialog::refill_output_presets ()
119         using namespace Menu_Helpers;
120         PluginOutputConfiguration ppc (_pi->plugin (0)->possible_output ());
121
122         _out_presets.AddMenuElem (MenuElem (_("Automatic"), sigc::bind (sigc::mem_fun (*this, &PluginSetupDialog::select_output_preset), 0)));
123
124         if (ppc.find (0) != ppc.end ()) {
125                 // anyting goes
126                 ppc.clear ();
127                 ppc.insert (1);
128                 ppc.insert (2);
129                 ppc.insert (8);
130                 ppc.insert (16);
131                 ppc.insert (24);
132                 ppc.insert (32);
133                 if (ppc.find (_cur_outputs.n_audio ()) == ppc.end ()) {
134                         ppc.insert (_cur_outputs.n_audio ());
135                 }
136         }
137
138         bool have_matching_io = false;
139
140         for (PluginOutputConfiguration::const_iterator i = ppc.begin () ; i != ppc.end (); ++i) {
141                 assert (*i > 0);
142                 _out_presets.AddMenuElem (MenuElem (preset_label (*i), sigc::bind (sigc::mem_fun (*this, &PluginSetupDialog::select_output_preset), *i)));
143                 if (*i == _cur_outputs.n_audio ()) {
144                         have_matching_io = true;
145                 }
146         }
147
148         if (have_matching_io) {
149                 select_output_preset (_cur_outputs.n_audio ());
150         } else if (ppc.size() == 1 && _pi->strict_io ()) {
151                 select_output_preset (*ppc.begin ());
152         } else {
153                 select_output_preset (0);
154         }
155 }
156
157 void
158 PluginSetupDialog::select_output_preset (uint32_t n_audio)
159 {
160         _pi->set_preset_out (ChanCount (DataType::AUDIO, n_audio));
161         _out_presets.set_text (preset_label (n_audio));
162         update_sensitivity (n_audio);
163 }
164
165 void
166 PluginSetupDialog::update_sensitivity (uint32_t n_audio)
167 {
168         if (_cur_outputs.n_audio () > 0 && _cur_outputs.n_audio () == n_audio) {
169                 // TODO check _cur_inputs if not reconfigurable?
170                 _keep_mapping.set_sensitive (true);
171         } else {
172                 _keep_mapping.set_sensitive (false);
173         }
174         _fan_out.set_sensitive (n_audio > 2);
175 }
176
177 bool
178 PluginSetupDialog::io_match () const
179 {
180         if (_cur_outputs.n_audio () > 0 && _cur_outputs.n_audio () == _pi->preset_out ().n_audio ()) {
181                 return true;
182         } else {
183                 return false;
184         }
185 }
186
187 void
188 PluginSetupDialog::apply_mapping ()
189 {
190         // toggle button
191         _keep_mapping.set_active (!_keep_mapping.get_active ());
192
193         boost::shared_ptr<Processor> old = _route->the_instrument ();
194         boost::shared_ptr<PluginInsert> opi = boost::dynamic_pointer_cast<PluginInsert> (old);
195
196         if (_keep_mapping.get_active () && opi && io_match ()) {
197                 _pi->pre_seed (_cur_inputs, _cur_outputs, opi->input_map (0), opi->output_map (0), opi->thru_map ());
198         } else {
199                 _pi->pre_seed (ChanCount (), ChanCount (), ChanMapping (), ChanMapping (), ChanMapping());
200         }
201 }
202
203 void
204 PluginSetupDialog::toggle_fan_out ()
205 {
206         _fan_out.set_active (!_fan_out.get_active ());
207 }
208
209 std::string
210 PluginSetupDialog::preset_label (uint32_t n_audio) const
211 {
212                 std::string rv;
213                 switch (n_audio) {
214                         case 0:
215                                 rv = _("Automatic");
216                                 break;
217                         case 1:
218                                 rv = _("Mono");
219                                 break;
220                         case 2:
221                                 rv = _("Stereo");
222                                 break;
223                         default:
224                                 rv = string_compose (P_("%1 Channel", "%1 Channels", n_audio), n_audio);
225                                 break;
226                 }
227                 return rv;
228 }