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