Only show user-presets in favorite sidebar
[ardour.git] / gtk2_ardour / plugin_setup_dialog.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2011 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <gtkmm/frame.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/stock.h>
23 #include <gtkmm/table.h>
24
25 #include "plugin_setup_dialog.h"
26 #include "pbd/i18n.h"
27
28 using namespace ARDOUR;
29 using namespace ArdourWidgets;
30 using namespace Gtk;
31
32 PluginSetupDialog::PluginSetupDialog (boost::shared_ptr<ARDOUR::Route> r, boost::shared_ptr<ARDOUR::PluginInsert> pi, ARDOUR::Route::PluginSetupOptions flags)
33         : ArdourDialog (_("Plugin Setup"), true, false)
34         , _route (r)
35         , _pi (pi)
36         , _keep_mapping (_("Copy I/O Map"), ArdourButton::led_default_elements)
37         , _fan_out (_("Fan out"), ArdourButton::led_default_elements)
38 {
39         assert (flags != Route::None);
40
41         Gtk::Table *tbl = manage (new Gtk::Table ());
42         tbl->set_spacings (6);
43         get_vbox()->pack_start (*tbl);
44         int row = 0;
45
46         if (flags & Route::CanReplace) {
47                 boost::shared_ptr<Processor> old = _route->the_instrument ();
48                 boost::shared_ptr<PluginInsert> opi = boost::dynamic_pointer_cast<PluginInsert> (old);
49                 assert (opi);
50
51                 opi->configured_io (_cur_inputs, _cur_outputs);
52
53                 Gtk::Label* l = manage (new Label (
54                                                 _("An Instrument plugin is already present.")
55                                                 ));
56                 tbl->attach (*l, 0, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
57
58                 l = manage (new Label (_("Replace"), ALIGN_END));
59                 tbl->attach (*l, 0, 1, row, row + 1, EXPAND|FILL, SHRINK);
60
61                 l = manage (new Label (string_compose ("'%1'", old->name ()), ALIGN_START));
62                 tbl->attach (*l, 1, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
63
64                 l = manage (new Label (_("with"), ALIGN_END));
65                 tbl->attach (*l, 0, 1, row, row + 1, EXPAND|FILL, SHRINK);
66
67                 l = manage (new Label (string_compose ("'%1'", pi->name ()), ALIGN_START));
68                 tbl->attach (*l, 1, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
69
70                 Box* box = manage (new HBox ());
71                 box->set_border_width (2);
72                 box->pack_start (_keep_mapping, true, true);
73                 Frame* f = manage (new Frame ());
74                 f->set_label (_("I/O Pin Mapping"));
75                 f->add (*box);
76                 tbl->attach (*f, 0, 1, row, row + 1, EXPAND|FILL, SHRINK, 0, 8);
77
78                 _keep_mapping.signal_clicked.connect (sigc::mem_fun (*this, &PluginSetupDialog::apply_mapping));
79                 add_button ("Replace", 2);
80         } else {
81
82                 Gtk::Label *l = manage (new Label (string_compose (
83                                                 _("Configure Plugin '%1'"), pi->name ()
84                                                 )));
85                 tbl->attach (*l, 0, 2, row, row + 1, EXPAND|FILL, SHRINK); ++row;
86         }
87
88         if (flags & Route::MultiOut) {
89                 setup_output_presets ();
90                 Box* box = manage (new HBox ());
91                 box->set_border_width (2);
92                 box->pack_start (_out_presets, true, true);
93                 box->pack_start (_fan_out, false, false);
94                 Frame* f = manage (new Frame ());
95                 f->set_label (_("Output Configuration"));
96                 f->add (*box);
97                 tbl->attach (*f, 1, 2, row, row + 1, EXPAND|FILL, SHRINK, 0, 8);
98                 _fan_out.signal_clicked.connect (sigc::mem_fun (*this, &PluginSetupDialog::toggle_fan_out));
99         } else {
100                 _pi->set_preset_out (_pi->natural_output_streams ());
101                 update_sensitivity (_pi->natural_output_streams ().n_audio ());
102         }
103
104         _keep_mapping.set_active (false);
105         _fan_out.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 {
151                 select_output_preset (0);
152         }
153 }
154
155 void
156 PluginSetupDialog::select_output_preset (uint32_t n_audio)
157 {
158         _pi->set_preset_out (ChanCount (DataType::AUDIO, n_audio));
159         _out_presets.set_text (preset_label (n_audio));
160         update_sensitivity (n_audio);
161 }
162
163 void
164 PluginSetupDialog::update_sensitivity (uint32_t n_audio)
165 {
166         if (_cur_outputs.n_audio () > 0 && _cur_outputs.n_audio () == n_audio) {
167                 // TODO check _cur_inputs if not reconfigurable?
168                 _keep_mapping.set_sensitive (true);
169         } else {
170                 _keep_mapping.set_sensitive (false);
171         }
172         _fan_out.set_sensitive (n_audio > 2);
173 }
174
175 bool
176 PluginSetupDialog::io_match () const
177 {
178         if (_cur_outputs.n_audio () > 0 && _cur_outputs.n_audio () == _pi->preset_out ().n_audio ()) {
179                 return true;
180         } else {
181                 return false;
182         }
183 }
184
185 void
186 PluginSetupDialog::apply_mapping ()
187 {
188         // toggle button
189         _keep_mapping.set_active (!_keep_mapping.get_active ());
190
191         boost::shared_ptr<Processor> old = _route->the_instrument ();
192         boost::shared_ptr<PluginInsert> opi = boost::dynamic_pointer_cast<PluginInsert> (old);
193
194         if (_keep_mapping.get_active () && opi && io_match ()) {
195                 _pi->pre_seed (_cur_inputs, _cur_outputs, opi->input_map (0), opi->output_map (0), opi->thru_map ());
196         } else {
197                 _pi->pre_seed (ChanCount (), ChanCount (), ChanMapping (), ChanMapping (), ChanMapping());
198         }
199 }
200
201 void
202 PluginSetupDialog::toggle_fan_out ()
203 {
204         _fan_out.set_active (!_fan_out.get_active ());
205 }
206
207 std::string
208 PluginSetupDialog::preset_label (uint32_t n_audio) const
209 {
210                 std::string rv;
211                 switch (n_audio) {
212                         case 0:
213                                 rv = _("Automatic");
214                                 break;
215                         case 1:
216                                 rv = _("Mono");
217                                 break;
218                         case 2:
219                                 rv = _("Stereo");
220                                 break;
221                         default:
222                                 rv = string_compose (P_("%1 Channel", "%1 Channels", n_audio), n_audio);
223                                 break;
224                 }
225                 return rv;
226 }