clean up port insert port count/config mess, maybe
[ardour.git] / gtk2_ardour / plugin_ui.cc
1 /*
2     Copyright (C) 2000 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 <climits>
21 #include <cerrno>
22 #include <cmath>
23 #include <string>
24
25 #include <pbd/stl_delete.h>
26 #include <pbd/xml++.h>
27 #include <pbd/failed_constructor.h>
28
29 #include <gtkmm/widget.h>
30 #include <gtkmm2ext/click_box.h>
31 #include <gtkmm2ext/fastmeter.h>
32 #include <gtkmm2ext/barcontroller.h>
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/doi.h>
35 #include <gtkmm2ext/slider_controller.h>
36
37 #include <midi++/manager.h>
38
39 #include <ardour/plugin.h>
40 #include <ardour/insert.h>
41 #include <ardour/ladspa_plugin.h>
42 #ifdef VST_SUPPORT
43 #include <ardour/vst_plugin.h>
44 #endif
45
46 #include <lrdf.h>
47
48 #include "ardour_ui.h"
49 #include "prompter.h"
50 #include "plugin_ui.h"
51 #include "utils.h"
52 #include "gui_thread.h"
53 #include "public_editor.h"
54
55 #include "i18n.h"
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace PBD;
60 using namespace Gtkmm2ext;
61 using namespace Gtk;
62 using namespace sigc;
63
64 PluginUIWindow::PluginUIWindow (Gtk::Window* win, boost::shared_ptr<PluginInsert> insert, bool scrollable)
65         : parent (win)
66 {
67         bool have_gui = false;
68         non_gtk_gui = false;
69
70         if (insert->plugin()->has_editor()) {
71                 switch (insert->type()) {
72                 case ARDOUR::VST:
73                         have_gui = create_vst_editor (insert);
74                         break;
75
76                 case ARDOUR::AudioUnit:
77                         have_gui = create_audiounit_editor (insert);
78                         break;
79                         
80                 case ARDOUR::LADSPA:
81                         error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
82                         break;
83
84                 default:
85 #ifndef VST_SUPPORT
86                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
87                               << endmsg;
88 #else
89                         error << _("unknown type of editor-supplying plugin")
90                               << endmsg;
91 #endif
92                         throw failed_constructor ();
93                 }
94
95         } 
96
97         if (!have_gui) {
98
99                 GenericPluginUI*  pu  = new GenericPluginUI (insert, scrollable);
100                 
101                 _pluginui = pu;
102                 add (*pu);
103                 
104                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
105
106                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
107                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
108         }
109
110         // set_position (Gtk::WIN_POS_MOUSE);
111         set_name ("PluginEditor");
112         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
113
114         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
115         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
116
117         gint h = _pluginui->get_preferred_height ();
118         gint w = _pluginui->get_preferred_width ();
119
120         if (scrollable) {
121                 if (h > 600) h = 600;
122                 if (w > 600) w = 600;
123
124                 if (w < 0) {
125                         w = 450;
126                 }
127         }
128
129         set_default_size (w, h); 
130 }
131
132 PluginUIWindow::~PluginUIWindow ()
133 {
134 }
135
136 void
137 PluginUIWindow::set_parent (Gtk::Window* win)
138 {
139         parent = win;
140 }
141
142 void
143 PluginUIWindow::on_map ()
144 {
145         Window::on_map ();
146         set_keep_above (true);
147 }
148
149 void
150 PluginUIWindow::on_show ()
151 {
152         if (_pluginui) {
153                 _pluginui->update_presets ();
154         }
155
156         Window::on_show ();
157
158         if (parent) {
159                 cerr << "plugin becomes transient for " << parent << endl;
160                 // set_transient_for (*parent);
161         }
162 }
163
164 void
165 PluginUIWindow::on_hide ()
166 {
167         Window::on_hide ();
168 }
169
170 bool
171 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
172 {
173 #ifndef VST_SUPPORT
174         return false;
175 #else
176
177         boost::shared_ptr<VSTPlugin> vp;
178
179         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
180                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
181                               << endmsg;
182                 throw failed_constructor ();
183         } else {
184                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
185         
186                 _pluginui = vpu;
187                 add (*vpu);
188                 vpu->package (*this);
189         }
190
191         non_gtk_gui = true;
192         return true;
193 #endif
194 }
195
196 bool
197 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
198 {
199 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
200         return false;
201 #else
202         VBox* box;
203         _pluginui = create_au_gui (insert, &box);
204         add (*box);
205         non_gtk_gui = true;
206
207         extern sigc::signal<void,bool> ApplicationActivationChanged;
208         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
209
210         return true;
211 #endif
212 }
213
214 void
215 PluginUIWindow::app_activated (bool yn)
216 {
217 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
218         cerr << "APP activated ? " << yn << endl;
219         if (_pluginui) {
220                 if (yn) {
221                         _pluginui->activate ();
222                         present ();
223                 } else {
224                         hide ();
225                         _pluginui->deactivate ();
226                 }
227         } 
228 #endif
229 }
230
231 bool
232 PluginUIWindow::on_key_press_event (GdkEventKey* event)
233 {
234         if (non_gtk_gui) {
235                 return false;
236         }
237
238         if (!key_press_focus_accelerator_handler (*this, event)) {
239                 return PublicEditor::instance().on_key_press_event(event);
240         } else {
241                 return true;
242         }
243 }
244
245 bool
246 PluginUIWindow::on_key_release_event (GdkEventKey* event)
247 {
248         return true;
249 }
250
251 void
252 PluginUIWindow::plugin_going_away ()
253 {
254         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
255         
256         if (_pluginui) {
257                 _pluginui->stop_updating(0);
258         }
259         delete_when_idle (this);
260 }
261
262 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
263         : insert (pi),
264           plugin (insert->plugin()),
265           save_button(_("Add")),
266           bypass_button (_("Bypass"))
267 {
268         //preset_combo.set_use_arrows_always(true);
269         set_popdown_strings (preset_combo, plugin->get_presets());
270         preset_combo.set_size_request (100, -1);
271         preset_combo.set_active_text ("");
272         preset_combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
273
274         save_button.set_name ("PluginSaveButton");
275         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
276
277         insert->active_changed.connect (mem_fun(*this, &PlugUIBase::redirect_active_changed));
278         bypass_button.set_active (!pi->active());
279
280         bypass_button.set_name ("PluginBypassButton");
281         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
282 }
283
284 void
285 PlugUIBase::redirect_active_changed (Redirect* r, void* src)
286 {
287         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlugUIBase::redirect_active_changed), r, src));
288         bypass_button.set_active (!r->active());
289 }
290
291 void
292 PlugUIBase::setting_selected()
293 {
294         if (preset_combo.get_active_text().length() > 0) {
295                 if (!plugin->load_preset(preset_combo.get_active_text())) {
296                         warning << string_compose(_("Plugin preset %1 not found"), preset_combo.get_active_text()) << endmsg;
297                 }
298         }
299 }
300
301 void
302 PlugUIBase::save_plugin_setting ()
303 {
304         ArdourPrompter prompter (true);
305         prompter.set_prompt(_("Name of New Preset:"));
306         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
307         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
308
309         prompter.show_all();
310
311         switch (prompter.run ()) {
312         case Gtk::RESPONSE_ACCEPT:
313
314                 string name;
315
316                 prompter.get_result(name);
317
318                 if (name.length()) {
319                         if(plugin->save_preset(name)){
320                                 set_popdown_strings (preset_combo, plugin->get_presets());
321                                 preset_combo.set_active_text (name);
322                         }
323                 }
324                 break;
325         }
326 }
327
328 void
329 PlugUIBase::bypass_toggled ()
330 {
331         bool x;
332
333         if ((x = bypass_button.get_active()) == insert->active()) {
334                 insert->set_active (!x, this);
335                 if (insert->active()) {
336                         bypass_button.set_label (_("Bypass"));
337                 } else {
338                         bypass_button.set_label (_("Active"));
339                 }
340         }
341 }
342
343 void
344 PlugUIBase::update_presets ()
345 {
346         set_popdown_strings (preset_combo, plugin->get_presets());
347 }