Stop transport rec button going red immediately on being clicked. Fixes #3340.
[ArdourMidi.git] / gtk2_ardour / plugin_ui.cc
blobc575a3053ea6118d36dbb78982991f1c2726ccd2
1 /*
2 Copyright (C) 2000 Paul Davis
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.
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.
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.
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
24 #include <climits>
25 #include <cerrno>
26 #include <cmath>
27 #include <string>
29 #include "pbd/stl_delete.h"
30 #include "pbd/xml++.h"
31 #include "pbd/failed_constructor.h"
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>
42 #include "midi++/manager.h"
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
56 #include <lrdf.h>
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"
69 #include "i18n.h"
71 using namespace std;
72 using namespace ARDOUR;
73 using namespace PBD;
74 using namespace Gtkmm2ext;
75 using namespace Gtk;
77 PluginUIWindow::PluginUIWindow (Gtk::Window* win, boost::shared_ptr<PluginInsert> insert, bool scrollable)
78 : parent (win)
80 bool have_gui = false;
81 non_gtk_gui = false;
82 was_visible = false;
84 Label* label = manage (new Label());
85 label->set_markup ("<b>THIS IS THE PLUGIN UI</b>");
87 if (insert->plugin()->has_editor()) {
88 switch (insert->type()) {
89 case ARDOUR::VST:
90 have_gui = create_vst_editor (insert);
91 break;
93 case ARDOUR::AudioUnit:
94 have_gui = create_audiounit_editor (insert);
95 break;
97 case ARDOUR::LADSPA:
98 error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
99 break;
101 case ARDOUR::LV2:
102 have_gui = create_lv2_editor (insert);
103 break;
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 ();
118 if (!have_gui) {
120 GenericPluginUI* pu = new GenericPluginUI (insert, scrollable);
122 _pluginui = pu;
123 add (*pu);
126 Gtk::HBox *hbox = new Gtk::HBox();
127 hbox->pack_start( *pu);
128 // TODO: this should be nicer
129 hbox->pack_start( eqgui_bin );
131 add (*manage(hbox));
134 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
136 signal_map_event().connect (sigc::mem_fun (*pu, &GenericPluginUI::start_updating));
137 signal_unmap_event().connect (sigc::mem_fun (*pu, &GenericPluginUI::stop_updating));
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);
144 signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
145 insert->DropReferences.connect (death_connection, invalidator (*this), boost::bind (&PluginUIWindow::plugin_going_away, this), gui_context());
147 gint h = _pluginui->get_preferred_height ();
148 gint w = _pluginui->get_preferred_width ();
150 if (scrollable) {
151 if (h > 600) h = 600;
152 if (w > 600) w = 600;
154 if (w < 0) {
155 w = 450;
159 set_default_size (w, h);
162 PluginUIWindow::~PluginUIWindow ()
164 delete _pluginui;
167 void
168 PluginUIWindow::set_parent (Gtk::Window* win)
170 parent = win;
173 void
174 PluginUIWindow::on_map ()
176 Window::on_map ();
177 set_keep_above (true);
180 bool
181 PluginUIWindow::on_enter_notify_event (GdkEventCrossing *ev)
183 Keyboard::the_keyboard().enter_window (ev, this);
184 return false;
187 bool
188 PluginUIWindow::on_leave_notify_event (GdkEventCrossing *ev)
190 Keyboard::the_keyboard().leave_window (ev, this);
191 return false;
194 bool
195 PluginUIWindow::on_focus_in_event (GdkEventFocus *ev)
197 Window::on_focus_in_event (ev);
198 //Keyboard::the_keyboard().magic_widget_grab_focus ();
199 return false;
202 bool
203 PluginUIWindow::on_focus_out_event (GdkEventFocus *ev)
205 Window::on_focus_out_event (ev);
206 //Keyboard::the_keyboard().magic_widget_drop_focus ();
207 return false;
210 void
211 PluginUIWindow::on_show ()
213 set_role("plugin_ui");
215 if (_pluginui) {
216 _pluginui->update_presets ();
219 if (_pluginui) {
220 if (_pluginui->on_window_show (_title)) {
221 Window::on_show ();
225 if (parent) {
226 // set_transient_for (*parent);
230 void
231 PluginUIWindow::on_hide ()
233 Window::on_hide ();
235 if (_pluginui) {
236 _pluginui->on_window_hide ();
240 void
241 PluginUIWindow::set_title(const Glib::ustring& title)
243 //cout << "PluginUIWindow::set_title(\"" << title << "\"" << endl;
244 Gtk::Window::set_title(title);
245 _title = title;
248 bool
249 #ifdef VST_SUPPORT
250 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
251 #else
252 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert>)
253 #endif
255 #ifndef VST_SUPPORT
256 return false;
257 #else
259 boost::shared_ptr<VSTPlugin> vp;
261 if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
262 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
263 << endmsg;
264 throw failed_constructor ();
265 } else {
266 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
268 _pluginui = vpu;
269 add (*vpu);
270 vpu->package (*this);
273 non_gtk_gui = true;
274 return true;
275 #endif
278 bool
279 #if defined (HAVE_AUDIOUNITS) && defined (GTKOSX)
280 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
281 #else
282 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert>)
283 #endif
285 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
286 return false;
287 #else
288 VBox* box;
289 _pluginui = create_au_gui (insert, &box);
290 add (*box);
291 non_gtk_gui = true;
293 extern sigc::signal<void,bool> ApplicationActivationChanged;
294 ApplicationActivationChanged.connect (sigc::mem_fun (*this, &PluginUIWindow::app_activated));
296 return true;
297 #endif
300 void
301 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
302 PluginUIWindow::app_activated (bool yn)
303 #else
304 PluginUIWindow::app_activated (bool)
305 #endif
307 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
308 cerr << "APP activated ? " << yn << endl;
309 if (_pluginui) {
310 if (yn) {
311 if (was_visible) {
312 _pluginui->activate ();
313 present ();
314 was_visible = true;
316 } else {
317 was_visible = is_visible();
318 hide ();
319 _pluginui->deactivate ();
322 #endif
325 bool
326 PluginUIWindow::create_lv2_editor(boost::shared_ptr<PluginInsert> insert)
328 #ifndef HAVE_SLV2
329 return false;
330 #else
332 boost::shared_ptr<LV2Plugin> vp;
334 if ((vp = boost::dynamic_pointer_cast<LV2Plugin> (insert->plugin())) == 0) {
335 error << _("create_lv2_editor called on non-LV2 plugin") << endmsg;
336 throw failed_constructor ();
337 } else {
338 LV2PluginUI* lpu = new LV2PluginUI (insert, vp);
339 _pluginui = lpu;
340 add (*lpu);
341 lpu->package (*this);
344 non_gtk_gui = false;
345 return true;
346 #endif
349 bool
350 PluginUIWindow::on_key_press_event (GdkEventKey* event)
352 return relay_key_press (event, this);
355 bool
356 PluginUIWindow::on_key_release_event (GdkEventKey *)
358 return true;
361 void
362 PluginUIWindow::plugin_going_away ()
364 ENSURE_GUI_THREAD (*this, &PluginUIWindow::plugin_going_away)
366 if (_pluginui) {
367 _pluginui->stop_updating(0);
370 death_connection.disconnect ();
372 delete_when_idle (this);
375 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
376 : insert (pi),
377 plugin (insert->plugin()),
378 save_button(_("Add")),
379 bypass_button (_("Bypass")),
380 latency_gui (0),
381 plugin_analysis_expander (_("Plugin analysis"))
383 //preset_combo.set_use_arrows_always(true);
384 update_presets();
385 preset_combo.set_size_request (100, -1);
386 preset_combo.set_active_text ("");
387 preset_combo.signal_changed().connect(sigc::mem_fun(*this, &PlugUIBase::setting_selected));
388 no_load_preset = false;
390 save_button.set_name ("PluginSaveButton");
391 save_button.signal_clicked().connect(sigc::mem_fun(*this, &PlugUIBase::save_plugin_setting));
393 insert->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&PlugUIBase::processor_active_changed, this, boost::weak_ptr<Processor>(insert)), gui_context());
395 bypass_button.set_active (!pi->active());
397 bypass_button.set_name ("PluginBypassButton");
398 bypass_button.signal_toggled().connect (sigc::mem_fun(*this, &PlugUIBase::bypass_toggled));
399 focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
401 focus_button.signal_button_release_event().connect (sigc::mem_fun(*this, &PlugUIBase::focus_toggled));
402 focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
404 /* these images are not managed, so that we can remove them at will */
406 focus_out_image = new Image (get_icon (X_("computer_keyboard")));
407 focus_in_image = new Image (get_icon (X_("computer_keyboard_active")));
409 focus_button.add (*focus_out_image);
411 ARDOUR_UI::instance()->set_tip (focus_button, string_compose (_("Click to allow the plugin to receive keyboard events that %1 would normally use as a shortcut"), PROGRAM_NAME));
412 ARDOUR_UI::instance()->set_tip (bypass_button, _("Click to enable/disable this plugin"));
414 plugin_analysis_expander.property_expanded().signal_changed().connect( sigc::mem_fun(*this, &PlugUIBase::toggle_plugin_analysis));
415 plugin_analysis_expander.set_expanded(false);
417 insert->DropReferences.connect (death_connection, invalidator (*this), boost::bind (&PlugUIBase::plugin_going_away, this), gui_context());
420 PlugUIBase::~PlugUIBase()
422 delete latency_gui;
425 void
426 PlugUIBase::plugin_going_away ()
428 /* drop references to the plugin/insert */
429 insert.reset ();
430 plugin.reset ();
431 death_connection.disconnect ();
434 void
435 PlugUIBase::set_latency_label ()
437 char buf[64];
438 nframes_t l = insert->effective_latency ();
439 nframes_t sr = insert->session().frame_rate();
441 if (l < sr / 1000) {
442 snprintf (buf, sizeof (buf), "latency (%d samples)", l);
443 } else {
444 snprintf (buf, sizeof (buf), "latency (%.2f msecs)", (float) l / ((float) sr / 1000.0f));
447 latency_label.set_text (buf);
450 void
451 PlugUIBase::latency_button_clicked ()
453 if (!latency_gui) {
454 latency_gui = new LatencyGUI (*(insert.get()), insert->session().frame_rate(), insert->session().get_block_size());
455 latency_dialog = new ArdourDialog ("Edit Latency", false, false);
456 latency_dialog->get_vbox()->pack_start (*latency_gui);
457 latency_dialog->signal_hide().connect (sigc::mem_fun (*this, &PlugUIBase::set_latency_label));
460 latency_dialog->show_all ();
463 void
464 PlugUIBase::processor_active_changed (boost::weak_ptr<Processor> weak_p)
466 ENSURE_GUI_THREAD (*this, &PlugUIBase::processor_active_changed, weak_p)
467 boost::shared_ptr<Processor> p (weak_p);
468 if (p) {
469 bypass_button.set_active (!p->active());
473 void
474 PlugUIBase::setting_selected()
476 if (no_load_preset) {
477 return;
480 if (preset_combo.get_active_text().length() > 0) {
481 const Plugin::PresetRecord* pr = plugin->preset_by_label(preset_combo.get_active_text());
482 if (pr) {
483 plugin->load_preset(pr->uri);
484 } else {
485 warning << string_compose(_("Plugin preset %1 not found"),
486 preset_combo.get_active_text()) << endmsg;
491 void
492 PlugUIBase::save_plugin_setting ()
494 ArdourPrompter prompter (true);
495 prompter.set_prompt(_("Name of New Preset:"));
496 prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
497 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
498 prompter.set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
500 prompter.show_all();
501 prompter.present ();
503 switch (prompter.run ()) {
504 case Gtk::RESPONSE_ACCEPT:
505 string name;
506 prompter.get_result(name);
507 if (name.length()) {
508 if (plugin->save_preset(name)) {
509 update_presets();
510 no_load_preset = true;
511 preset_combo.set_active_text (name);
512 no_load_preset = false;
515 break;
519 void
520 PlugUIBase::bypass_toggled ()
522 bool x;
524 if ((x = bypass_button.get_active()) == insert->active()) {
525 if (x) {
526 insert->deactivate ();
527 } else {
528 insert->activate ();
533 bool
534 PlugUIBase::focus_toggled (GdkEventButton*)
536 if (Keyboard::the_keyboard().some_magic_widget_has_focus()) {
537 Keyboard::the_keyboard().magic_widget_drop_focus();
538 focus_button.remove ();
539 focus_button.add (*focus_out_image);
540 focus_out_image->show ();
541 ARDOUR_UI::instance()->set_tip (focus_button, string_compose (_("Click to allow the plugin to receive keyboard events that %1 would normally use as a shortcut"), PROGRAM_NAME));
542 } else {
543 Keyboard::the_keyboard().magic_widget_grab_focus();
544 focus_button.remove ();
545 focus_button.add (*focus_in_image);
546 focus_in_image->show ();
547 ARDOUR_UI::instance()->set_tip (focus_button, string_compose (_("Click to allow normal use of %1 keyboard shortcuts"), PROGRAM_NAME));
550 return true;
553 void
554 PlugUIBase::toggle_plugin_analysis()
556 if (plugin_analysis_expander.get_expanded() &&
557 !plugin_analysis_expander.get_child()) {
558 // Create the GUI
559 PluginEqGui *foo = new PluginEqGui(insert);
560 plugin_analysis_expander.add( *foo );
561 plugin_analysis_expander.show_all();
564 Gtk::Widget *gui;
566 if (!plugin_analysis_expander.get_expanded() &&
567 (gui = plugin_analysis_expander.get_child())) {
568 // Hide & remove
569 gui->hide();
570 //plugin_analysis_expander.remove(*gui);
571 plugin_analysis_expander.remove();
573 delete gui;
575 Gtk::Widget *toplevel = plugin_analysis_expander.get_toplevel();
576 if (!toplevel) {
577 std::cerr << "No toplevel widget?!?!" << std::endl;
578 return;
581 Gtk::Container *cont = dynamic_cast<Gtk::Container *>(toplevel);
582 if (!cont) {
583 std::cerr << "Toplevel widget is not a container?!?" << std::endl;
584 return;
587 Gtk::Allocation alloc(0, 0, 50, 50); // Just make it small
588 toplevel->size_allocate(alloc);
592 void
593 PlugUIBase::update_presets ()
595 vector<string> preset_labels;
596 vector<ARDOUR::Plugin::PresetRecord> presets = plugin->get_presets();
598 no_load_preset = true;
600 for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator i = presets.begin(); i != presets.end(); ++i) {
601 preset_labels.push_back(i->label);
604 set_popdown_strings (preset_combo, preset_labels);
606 no_load_preset = false;