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