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