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