part 2 of 3 of the 2.8 -> 3.0 merge
[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 "plugin_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                 add( *pu );
118
119                 /*
120                 Gtk::HBox *hbox = new Gtk::HBox();
121                 hbox->pack_start( *pu);
122                 // TODO: this should be nicer
123                 hbox->pack_start( eqgui_bin );
124                 
125                 add (*manage(hbox));
126                 */
127
128                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
129
130                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
131                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
132         }
133
134         // set_position (Gtk::WIN_POS_MOUSE);
135         set_name ("PluginEditor");
136         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
137
138         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
139         death_connection = insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
140         
141         gint h = _pluginui->get_preferred_height ();
142         gint w = _pluginui->get_preferred_width ();
143
144         if (scrollable) {
145                 if (h > 600) h = 600;
146                 if (w > 600) w = 600;
147
148                 if (w < 0) {
149                         w = 450;
150                 }
151         }
152
153         set_default_size (w, h); 
154 }
155
156 PluginUIWindow::~PluginUIWindow ()
157 {
158         delete _pluginui;
159 }
160
161 void
162 PluginUIWindow::set_parent (Gtk::Window* win)
163 {
164         parent = win;
165 }
166
167 void
168 PluginUIWindow::on_map ()
169 {
170         Window::on_map ();
171         set_keep_above (true);
172 }
173
174 bool
175 PluginUIWindow::on_enter_notify_event (GdkEventCrossing *ev)
176 {
177         Keyboard::the_keyboard().enter_window (ev, this);
178         return false;
179 }
180
181 bool
182 PluginUIWindow::on_leave_notify_event (GdkEventCrossing *ev)
183 {
184         Keyboard::the_keyboard().leave_window (ev, this);
185         return false;
186 }
187
188 bool
189 PluginUIWindow::on_focus_in_event (GdkEventFocus *ev)
190 {
191         Window::on_focus_in_event (ev);
192         //Keyboard::the_keyboard().magic_widget_grab_focus ();
193         return false;
194 }
195
196 bool
197 PluginUIWindow::on_focus_out_event (GdkEventFocus *ev)
198 {
199         Window::on_focus_out_event (ev);
200         //Keyboard::the_keyboard().magic_widget_drop_focus ();
201         return false;
202 }
203
204 void
205 PluginUIWindow::on_show ()
206 {
207         if (_pluginui) {
208                 _pluginui->update_presets ();
209         }
210
211         Window::on_show ();
212
213         if (parent) {
214                 // set_transient_for (*parent);
215         }
216 }
217
218 void
219 PluginUIWindow::on_hide ()
220 {
221         Window::on_hide ();
222 }
223
224 bool
225 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
226 {
227 #ifndef VST_SUPPORT
228         return false;
229 #else
230
231         boost::shared_ptr<VSTPlugin> vp;
232
233         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
234                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
235                               << endmsg;
236                 throw failed_constructor ();
237         } else {
238                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
239         
240                 _pluginui = vpu;
241                 add (*vpu);
242                 vpu->package (*this);
243         }
244
245         non_gtk_gui = true;
246         return true;
247 #endif
248 }
249
250 bool
251 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
252 {
253 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
254         return false;
255 #else
256         VBox* box;
257         _pluginui = create_au_gui (insert, &box);
258         add (*box);
259         non_gtk_gui = true;
260
261         extern sigc::signal<void,bool> ApplicationActivationChanged;
262         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
263
264         return true;
265 #endif
266 }
267
268 void
269 PluginUIWindow::app_activated (bool yn)
270 {
271 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
272         cerr << "APP activated ? " << yn << endl;
273         if (_pluginui) {
274                 if (yn) {
275                         if (was_visible) {
276                                 _pluginui->activate ();
277                                 present ();
278                                 was_visible = true;
279                         }
280                 } else {
281                         was_visible = is_visible();
282                         hide ();
283                         _pluginui->deactivate ();
284                 }
285         } 
286 #endif
287 }
288
289 bool
290 PluginUIWindow::create_lv2_editor(boost::shared_ptr<PluginInsert> insert)
291 {
292 #ifndef HAVE_LV2
293         return false;
294 #else
295
296         boost::shared_ptr<LV2Plugin> vp;
297         
298         if ((vp = boost::dynamic_pointer_cast<LV2Plugin> (insert->plugin())) == 0) {
299                 error << _("create_lv2_editor called on non-LV2 plugin") << endmsg;
300                 throw failed_constructor ();
301         } else {
302                 LV2PluginUI* lpu = new LV2PluginUI (insert, vp);
303                 _pluginui = lpu;
304                 add (*lpu);
305                 lpu->package (*this);
306         }
307
308         non_gtk_gui = false;
309         return true;
310 #endif
311 }
312
313 bool
314 PluginUIWindow::on_key_press_event (GdkEventKey* event)
315 {
316         if (!key_press_focus_accelerator_handler (*this, event)) {
317                 return PublicEditor::instance().on_key_press_event(event);
318         } else {
319                 return true;
320         }
321 }
322
323 bool
324 PluginUIWindow::on_key_release_event (GdkEventKey* event)
325 {
326         return true;
327 }
328
329 void
330 PluginUIWindow::plugin_going_away ()
331 {
332         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
333         
334         if (_pluginui) {
335                 _pluginui->stop_updating(0);
336         }
337
338         death_connection.disconnect ();
339
340         delete_when_idle (this);
341 }
342
343 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
344         : insert (pi),
345           plugin (insert->plugin()),
346           save_button(_("Add")),
347           bypass_button (_("Bypass")),
348           latency_gui (*pi, pi->session().frame_rate(), pi->session().get_block_size()),
349           eqgui_toggle (_("Freq Analysis"))
350 {
351         //preset_combo.set_use_arrows_always(true);
352         update_presets();
353         preset_combo.set_size_request (100, -1);
354         preset_combo.set_active_text ("");
355         preset_combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
356
357         save_button.set_name ("PluginSaveButton");
358         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
359
360         insert->ActiveChanged.connect (bind(
361                         mem_fun(*this, &PlugUIBase::processor_active_changed),
362                         boost::weak_ptr<Processor>(insert)));
363
364         eqgui_toggle.set_active (false);
365         eqgui_toggle.signal_toggled().connect( mem_fun(*this, &PlugUIBase::toggle_plugin_analysis));
366
367         
368         bypass_button.set_active (!pi->active());
369
370         bypass_button.set_name ("PluginBypassButton");
371         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
372         focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
373
374         focus_button.signal_button_release_event().connect (mem_fun(*this, &PlugUIBase::focus_toggled));
375         focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
376
377         /* these images are not managed, so that we can remove them at will */
378
379         focus_out_image = new Image (get_icon (X_("computer_keyboard")));
380         focus_in_image = new Image (get_icon (X_("computer_keyboard_active")));
381         
382         focus_button.add (*focus_out_image);
383
384         ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to allow the plugin to receive keyboard events that Ardour would normally use as a shortcut"), "");
385         ARDOUR_UI::instance()->set_tip (&bypass_button, _("Click to enable/disable this plugin"), "");
386
387         plugin_eq_bin.set_expanded(true);
388 }
389
390 PlugUIBase::~PlugUIBase()
391 {
392 }
393
394 void
395 PlugUIBase::processor_active_changed (boost::weak_ptr<Processor> weak_p)
396 {
397         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlugUIBase::processor_active_changed), weak_p));
398         boost::shared_ptr<Processor> p (weak_p);
399         if (p) {
400                 bypass_button.set_active (!p->active());
401         }
402 }
403
404 void
405 PlugUIBase::setting_selected()
406 {
407         if (preset_combo.get_active_text().length() > 0) {
408                 const Plugin::PresetRecord* pr = plugin->preset_by_label(preset_combo.get_active_text());
409                 if (pr) {
410                         plugin->load_preset(pr->uri);
411                 } else {
412                         warning << string_compose(_("Plugin preset %1 not found"),
413                                         preset_combo.get_active_text()) << endmsg;
414                 }
415         }
416 }
417
418 void
419 PlugUIBase::save_plugin_setting ()
420 {
421         ArdourPrompter prompter (true);
422         prompter.set_prompt(_("Name of New Preset:"));
423         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
424         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
425         prompter.set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
426
427         prompter.show_all();
428         prompter.present ();
429
430         switch (prompter.run ()) {
431         case Gtk::RESPONSE_ACCEPT:
432                 string name;
433                 prompter.get_result(name);
434                 if (name.length()) {
435                         if (plugin->save_preset(name)) {
436                                 update_presets();
437                                 preset_combo.set_active_text (name);
438                         }
439                 }
440                 break;
441         }
442 }
443
444 void
445 PlugUIBase::bypass_toggled ()
446 {
447         bool x;
448
449         if ((x = bypass_button.get_active()) == insert->active()) {
450                 if (x) {
451                         insert->deactivate ();
452                 } else {
453                         insert->activate ();
454                 }
455         }
456 }
457
458 bool
459 PlugUIBase::focus_toggled (GdkEventButton* ev)
460 {
461         if (Keyboard::the_keyboard().some_magic_widget_has_focus()) {
462                 Keyboard::the_keyboard().magic_widget_drop_focus();
463                 focus_button.remove ();
464                 focus_button.add (*focus_out_image);
465                 focus_out_image->show ();
466                 ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to allow the plugin to receive keyboard events that Ardour would normally use as a shortcut"), "");
467         } else {
468                 Keyboard::the_keyboard().magic_widget_grab_focus();
469                 focus_button.remove ();
470                 focus_button.add (*focus_in_image);
471                 focus_in_image->show ();
472                 ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to allow normal use of Ardour keyboard shortcuts"), "");
473         }
474
475         return true;
476 }
477
478 void
479 PlugUIBase::toggle_plugin_analysis()
480 {
481         if (eqgui_toggle.get_active() && !plugin_eq_bin.get_child()) {
482                 // Create the GUI
483                 PluginEqGui *foo = new PluginEqGui(insert);
484                 plugin_eq_bin.add( *foo );
485                 plugin_eq_bin.show_all();
486         } 
487         
488         Gtk::Widget *gui;
489
490         if (!eqgui_toggle.get_active() && (gui = plugin_eq_bin.get_child())) {
491                 // Hide & remove
492                 gui->hide();
493                 //plugin_eq_bin.remove(*gui);
494                 plugin_eq_bin.remove();
495
496                 delete gui;
497
498                 Gtk::Widget *toplevel = plugin_eq_bin.get_toplevel();
499                 if (!toplevel) {
500                         std::cerr << "No toplevel widget?!?!" << std::endl;
501                         return;
502                 }
503
504                 Gtk::Container *cont = dynamic_cast<Gtk::Container *>(toplevel);
505                 if (!cont) {
506                         std::cerr << "Toplevel widget is not a container?!?" << std::endl;
507                         return;
508                 }
509
510                 Gtk::Allocation alloc(0, 0, 50, 50); // Just make it small
511                 toplevel->size_allocate(alloc);
512         }
513 }
514
515 void
516 PlugUIBase::update_presets ()
517 {
518         vector<string> preset_labels;
519         vector<ARDOUR::Plugin::PresetRecord> presets = plugin->get_presets();
520         for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator i = presets.begin();
521                    i != presets.end(); ++i) {
522                 preset_labels.push_back(i->label);
523         }
524         set_popdown_strings (preset_combo, preset_labels);
525 }