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