1c5a56f531259f0b5a2395f7c19a6629a3afa6b6
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <climits>
25 #include <cerrno>
26 #include <cmath>
27 #include <string>
28
29 #include "pbd/stl_delete.h"
30 #include "pbd/xml++.h"
31 #include "pbd/failed_constructor.h"
32
33 #include <gtkmm/widget.h>
34 #include <gtkmm/box.h>
35 #include <gtkmm2ext/click_box.h>
36 #include <gtkmm2ext/fastmeter.h>
37 #include <gtkmm2ext/barcontroller.h>
38 #include <gtkmm2ext/utils.h>
39 #include <gtkmm2ext/doi.h>
40 #include <gtkmm2ext/slider_controller.h>
41
42 #include "midi++/manager.h"
43
44 #include "ardour/plugin.h"
45 #include "ardour/plugin_insert.h"
46 #include "ardour/ladspa_plugin.h"
47 #ifdef VST_SUPPORT
48 #include "ardour/vst_plugin.h"
49 #endif
50 #ifdef HAVE_SLV2
51 #include "ardour/lv2_plugin.h"
52 #include "lv2_plugin_ui.h"
53 #endif
54
55 #include <lrdf.h>
56
57 #include "ardour_dialog.h"
58 #include "ardour_ui.h"
59 #include "prompter.h"
60 #include "plugin_ui.h"
61 #include "utils.h"
62 #include "gui_thread.h"
63 #include "public_editor.h"
64 #include "keyboard.h"
65 #include "latency_gui.h"
66 #include "plugin_eq_gui.h"
67
68 #include "i18n.h"
69
70 using namespace std;
71 using namespace ARDOUR;
72 using namespace PBD;
73 using namespace Gtkmm2ext;
74 using namespace Gtk;
75 using namespace sigc;
76
77 PluginUIWindow::PluginUIWindow (Gtk::Window* win, boost::shared_ptr<PluginInsert> insert, bool scrollable)
78         : parent (win)
79 {
80         bool have_gui = false;
81         non_gtk_gui = false;
82         was_visible = false;
83
84         Label* label = manage (new Label());
85         label->set_markup ("<b>THIS IS THE PLUGIN UI</b>");
86
87         if (insert->plugin()->has_editor()) {
88                 switch (insert->type()) {
89                 case ARDOUR::VST:
90                         have_gui = create_vst_editor (insert);
91                         break;
92
93                 case ARDOUR::AudioUnit:
94                         have_gui = create_audiounit_editor (insert);
95                         break;
96                         
97                 case ARDOUR::LADSPA:
98                         error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
99                         break;
100
101                 case ARDOUR::LV2:
102                         have_gui = create_lv2_editor (insert);
103                         break;
104
105                 default:
106 #ifndef VST_SUPPORT
107                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
108                               << endmsg;
109 #else
110                         error << _("unknown type of editor-supplying plugin")
111                               << endmsg;
112 #endif
113                         throw failed_constructor ();
114                 }
115
116         } 
117
118         if (!have_gui) {
119
120                 GenericPluginUI*  pu  = new GenericPluginUI (insert, scrollable);
121                 
122                 _pluginui = pu;
123                 add( *pu );
124
125                 /*
126                 Gtk::HBox *hbox = new Gtk::HBox();
127                 hbox->pack_start( *pu);
128                 // TODO: this should be nicer
129                 hbox->pack_start( eqgui_bin );
130                 
131                 add (*manage(hbox));
132                 */
133
134                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
135
136                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
137                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
138         }
139
140         // set_position (Gtk::WIN_POS_MOUSE);
141         set_name ("PluginEditor");
142         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
143
144         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
145         death_connection = insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
146         
147         gint h = _pluginui->get_preferred_height ();
148         gint w = _pluginui->get_preferred_width ();
149
150         if (scrollable) {
151                 if (h > 600) h = 600;
152                 if (w > 600) w = 600;
153
154                 if (w < 0) {
155                         w = 450;
156                 }
157         }
158
159         set_default_size (w, h); 
160 }
161
162 PluginUIWindow::~PluginUIWindow ()
163 {
164         delete _pluginui;
165 }
166
167 void
168 PluginUIWindow::set_parent (Gtk::Window* win)
169 {
170         parent = win;
171 }
172
173 void
174 PluginUIWindow::on_map ()
175 {
176         Window::on_map ();
177         set_keep_above (true);
178 }
179
180 bool
181 PluginUIWindow::on_enter_notify_event (GdkEventCrossing *ev)
182 {
183         Keyboard::the_keyboard().enter_window (ev, this);
184         return false;
185 }
186
187 bool
188 PluginUIWindow::on_leave_notify_event (GdkEventCrossing *ev)
189 {
190         Keyboard::the_keyboard().leave_window (ev, this);
191         return false;
192 }
193
194 bool
195 PluginUIWindow::on_focus_in_event (GdkEventFocus *ev)
196 {
197         Window::on_focus_in_event (ev);
198         //Keyboard::the_keyboard().magic_widget_grab_focus ();
199         return false;
200 }
201
202 bool
203 PluginUIWindow::on_focus_out_event (GdkEventFocus *ev)
204 {
205         Window::on_focus_out_event (ev);
206         //Keyboard::the_keyboard().magic_widget_drop_focus ();
207         return false;
208 }
209
210 void
211 PluginUIWindow::on_show ()
212 {
213         if (_pluginui) {
214                 _pluginui->update_presets ();
215         }
216
217         Window::on_show ();
218
219         if (parent) {
220                 // set_transient_for (*parent);
221         }
222 }
223
224 void
225 PluginUIWindow::on_hide ()
226 {
227         Window::on_hide ();
228 }
229
230 bool
231 #ifdef VST_SUPPORT
232 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
233 #else
234 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert>)
235 #endif  
236 {
237 #ifndef VST_SUPPORT
238         return false;
239 #else
240
241         boost::shared_ptr<VSTPlugin> vp;
242
243         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
244                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
245                               << endmsg;
246                 throw failed_constructor ();
247         } else {
248                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
249         
250                 _pluginui = vpu;
251                 add (*vpu);
252                 vpu->package (*this);
253         }
254
255         non_gtk_gui = true;
256         return true;
257 #endif
258 }
259
260 bool
261 #if defined (HAVE_AUDIOUNITS) && defined (GTKOSX)
262 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
263 #else
264 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert>)
265 #endif  
266 {
267 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
268         return false;
269 #else
270         VBox* box;
271         _pluginui = create_au_gui (insert, &box);
272         add (*box);
273         non_gtk_gui = true;
274
275         extern sigc::signal<void,bool> ApplicationActivationChanged;
276         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
277
278         return true;
279 #endif
280 }
281
282 void
283 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
284 PluginUIWindow::app_activated (bool yn)
285 #else
286 PluginUIWindow::app_activated (bool)
287 #endif  
288 {
289 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
290         cerr << "APP activated ? " << yn << endl;
291         if (_pluginui) {
292                 if (yn) {
293                         if (was_visible) {
294                                 _pluginui->activate ();
295                                 present ();
296                                 was_visible = true;
297                         }
298                 } else {
299                         was_visible = is_visible();
300                         hide ();
301                         _pluginui->deactivate ();
302                 }
303         } 
304 #endif
305 }
306
307 bool
308 PluginUIWindow::create_lv2_editor(boost::shared_ptr<PluginInsert> insert)
309 {
310 #ifndef HAVE_SLV2
311         return false;
312 #else
313
314         boost::shared_ptr<LV2Plugin> vp;
315         
316         if ((vp = boost::dynamic_pointer_cast<LV2Plugin> (insert->plugin())) == 0) {
317                 error << _("create_lv2_editor called on non-LV2 plugin") << endmsg;
318                 throw failed_constructor ();
319         } else {
320                 LV2PluginUI* lpu = new LV2PluginUI (insert, vp);
321                 _pluginui = lpu;
322                 add (*lpu);
323                 lpu->package (*this);
324         }
325
326         non_gtk_gui = false;
327         return true;
328 #endif
329 }
330
331 bool
332 PluginUIWindow::on_key_press_event (GdkEventKey* event)
333 {
334         if (!key_press_focus_accelerator_handler (*this, event)) {
335                 return PublicEditor::instance().on_key_press_event(event);
336         } else {
337                 return true;
338         }
339 }
340
341 bool
342 PluginUIWindow::on_key_release_event (GdkEventKey *)
343 {
344         return true;
345 }
346
347 void
348 PluginUIWindow::plugin_going_away ()
349 {
350         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
351         
352         if (_pluginui) {
353                 _pluginui->stop_updating(0);
354         }
355
356         death_connection.disconnect ();
357
358         delete_when_idle (this);
359 }
360
361 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
362         : insert (pi),
363           plugin (insert->plugin()),
364           save_button(_("Add")),
365           bypass_button (_("Bypass")),
366           latency_gui (0),
367           plugin_analysis_expander (_("Plugin analysis"))
368 {
369         //preset_combo.set_use_arrows_always(true);
370         update_presets();
371         preset_combo.set_size_request (100, -1);
372         preset_combo.set_active_text ("");
373         preset_combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
374
375         save_button.set_name ("PluginSaveButton");
376         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
377
378         insert->ActiveChanged.connect (bind(
379                         mem_fun(*this, &PlugUIBase::processor_active_changed),
380                         boost::weak_ptr<Processor>(insert)));
381
382         bypass_button.set_active (!pi->active());
383
384         bypass_button.set_name ("PluginBypassButton");
385         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
386         focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
387
388         focus_button.signal_button_release_event().connect (mem_fun(*this, &PlugUIBase::focus_toggled));
389         focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
390
391         /* these images are not managed, so that we can remove them at will */
392
393         focus_out_image = new Image (get_icon (X_("computer_keyboard")));
394         focus_in_image = new Image (get_icon (X_("computer_keyboard_active")));
395         
396         focus_button.add (*focus_out_image);
397
398         ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to allow the plugin to receive keyboard events that Ardour would normally use as a shortcut"), "");
399         ARDOUR_UI::instance()->set_tip (&bypass_button, _("Click to enable/disable this plugin"), "");
400
401         plugin_analysis_expander.property_expanded().signal_changed().connect( mem_fun(*this, &PlugUIBase::toggle_plugin_analysis));
402         plugin_analysis_expander.set_expanded(false);
403
404         insert->GoingAway.connect (mem_fun (*this, &PlugUIBase::plugin_going_away));
405 }
406
407 PlugUIBase::~PlugUIBase()
408 {
409         delete latency_gui;
410 }
411
412 void
413 PlugUIBase::plugin_going_away ()
414 {
415         /* drop references to the plugin/insert */
416         insert.reset ();
417         plugin.reset ();
418 }
419
420 void
421 PlugUIBase::set_latency_label ()
422 {
423         char buf[64];
424         nframes_t l = insert->effective_latency ();
425         nframes_t sr = insert->session().frame_rate();
426
427         if (l < sr / 1000) {
428                 snprintf (buf, sizeof (buf), "latency (%d samples)", l);
429         } else {
430                 snprintf (buf, sizeof (buf), "latency (%.2f msecs)", (float) l / ((float) sr / 1000.0f));
431         }
432
433         latency_label.set_text (buf);
434 }
435
436 void
437 PlugUIBase::latency_button_clicked ()
438 {
439         if (!latency_gui) {
440                 latency_gui = new LatencyGUI (*(insert.get()), insert->session().frame_rate(), insert->session().get_block_size());
441                 latency_dialog = new ArdourDialog ("Edit Latency", false, false);
442                 latency_dialog->get_vbox()->pack_start (*latency_gui);
443                 latency_dialog->signal_hide().connect (mem_fun (*this, &PlugUIBase::set_latency_label));
444         }
445
446         latency_dialog->show_all ();
447 }
448
449 void
450 PlugUIBase::processor_active_changed (boost::weak_ptr<Processor> weak_p)
451 {
452         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlugUIBase::processor_active_changed), weak_p));
453         boost::shared_ptr<Processor> p (weak_p);
454         if (p) {
455                 bypass_button.set_active (!p->active());
456         }
457 }
458
459 void
460 PlugUIBase::setting_selected()
461 {
462         if (preset_combo.get_active_text().length() > 0) {
463                 const Plugin::PresetRecord* pr = plugin->preset_by_label(preset_combo.get_active_text());
464                 if (pr) {
465                         plugin->load_preset(pr->uri);
466                 } else {
467                         warning << string_compose(_("Plugin preset %1 not found"),
468                                         preset_combo.get_active_text()) << endmsg;
469                 }
470         }
471 }
472
473 void
474 PlugUIBase::save_plugin_setting ()
475 {
476         ArdourPrompter prompter (true);
477         prompter.set_prompt(_("Name of New Preset:"));
478         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
479         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
480         prompter.set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
481
482         prompter.show_all();
483         prompter.present ();
484
485         switch (prompter.run ()) {
486         case Gtk::RESPONSE_ACCEPT:
487                 string name;
488                 prompter.get_result(name);
489                 if (name.length()) {
490                         if (plugin->save_preset(name)) {
491                                 update_presets();
492                                 preset_combo.set_active_text (name);
493                         }
494                 }
495                 break;
496         }
497 }
498
499 void
500 PlugUIBase::bypass_toggled ()
501 {
502         bool x;
503
504         if ((x = bypass_button.get_active()) == insert->active()) {
505                 if (x) {
506                         insert->deactivate ();
507                 } else {
508                         insert->activate ();
509                 }
510         }
511 }
512
513 bool
514 PlugUIBase::focus_toggled (GdkEventButton*)
515 {
516         if (Keyboard::the_keyboard().some_magic_widget_has_focus()) {
517                 Keyboard::the_keyboard().magic_widget_drop_focus();
518                 focus_button.remove ();
519                 focus_button.add (*focus_out_image);
520                 focus_out_image->show ();
521                 ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to allow the plugin to receive keyboard events that Ardour would normally use as a shortcut"), "");
522         } else {
523                 Keyboard::the_keyboard().magic_widget_grab_focus();
524                 focus_button.remove ();
525                 focus_button.add (*focus_in_image);
526                 focus_in_image->show ();
527                 ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to allow normal use of Ardour keyboard shortcuts"), "");
528         }
529
530         return true;
531 }
532
533 void
534 PlugUIBase::toggle_plugin_analysis()
535 {
536         if (plugin_analysis_expander.get_expanded() && 
537             !plugin_analysis_expander.get_child()) {
538                 // Create the GUI
539                 PluginEqGui *foo = new PluginEqGui(insert);
540                 plugin_analysis_expander.add( *foo );
541                 plugin_analysis_expander.show_all();
542         } 
543         
544         Gtk::Widget *gui;
545
546         if (!plugin_analysis_expander.get_expanded() && 
547             (gui = plugin_analysis_expander.get_child())) {
548                 // Hide & remove
549                 gui->hide();
550                 //plugin_analysis_expander.remove(*gui);
551                 plugin_analysis_expander.remove();
552
553                 delete gui;
554
555                 Gtk::Widget *toplevel = plugin_analysis_expander.get_toplevel();
556                 if (!toplevel) {
557                         std::cerr << "No toplevel widget?!?!" << std::endl;
558                         return;
559                 }
560
561                 Gtk::Container *cont = dynamic_cast<Gtk::Container *>(toplevel);
562                 if (!cont) {
563                         std::cerr << "Toplevel widget is not a container?!?" << std::endl;
564                         return;
565                 }
566
567                 Gtk::Allocation alloc(0, 0, 50, 50); // Just make it small
568                 toplevel->size_allocate(alloc);
569         }
570 }
571
572 void
573 PlugUIBase::update_presets ()
574 {
575         vector<string> preset_labels;
576         vector<ARDOUR::Plugin::PresetRecord> presets = plugin->get_presets();
577         for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator i = presets.begin();
578                    i != presets.end(); ++i) {
579                 preset_labels.push_back(i->label);
580         }
581         set_popdown_strings (preset_combo, preset_labels);
582 }