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