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