4c7ae5b8a009464a5e2f406f38550e6234b58856
[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
61 #include "i18n.h"
62
63 using namespace std;
64 using namespace ARDOUR;
65 using namespace PBD;
66 using namespace Gtkmm2ext;
67 using namespace Gtk;
68 using namespace sigc;
69
70 PluginUIWindow::PluginUIWindow (Gtk::Window* win, boost::shared_ptr<PluginInsert> insert, bool scrollable)
71         : parent (win)
72 {
73         bool have_gui = false;
74         non_gtk_gui = false;
75         was_visible = false;
76
77         Label* label = manage (new Label());
78         label->set_markup ("<b>THIS IS THE PLUGIN UI</b>");
79
80         if (insert->plugin()->has_editor()) {
81                 switch (insert->type()) {
82                 case ARDOUR::VST:
83                         have_gui = create_vst_editor (insert);
84                         break;
85
86                 case ARDOUR::AudioUnit:
87                         have_gui = create_audiounit_editor (insert);
88                         break;
89                         
90                 case ARDOUR::LADSPA:
91                         error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
92                         break;
93
94                 case ARDOUR::LV2:
95                         have_gui = create_lv2_editor (insert);
96                         break;
97
98                 default:
99 #ifndef VST_SUPPORT
100                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
101                               << endmsg;
102 #else
103                         error << _("unknown type of editor-supplying plugin")
104                               << endmsg;
105 #endif
106                         throw failed_constructor ();
107                 }
108
109         } 
110
111         if (!have_gui) {
112
113                 GenericPluginUI*  pu  = new GenericPluginUI (insert, scrollable);
114                 
115                 _pluginui = pu;
116                 add( *pu );
117
118                 /*
119                 Gtk::HBox *hbox = new Gtk::HBox();
120                 hbox->pack_start( *pu);
121                 // TODO: this should be nicer
122                 hbox->pack_start( eqgui_bin );
123                 
124                 add (*manage(hbox));
125                 */
126
127                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
128
129                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
130                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
131         }
132
133         // set_position (Gtk::WIN_POS_MOUSE);
134         set_name ("PluginEditor");
135         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
136
137         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
138         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
139
140         gint h = _pluginui->get_preferred_height ();
141         gint w = _pluginui->get_preferred_width ();
142
143         if (scrollable) {
144                 if (h > 600) h = 600;
145                 if (w > 600) w = 600;
146
147                 if (w < 0) {
148                         w = 450;
149                 }
150         }
151
152         set_default_size (w, h); 
153 }
154
155 PluginUIWindow::~PluginUIWindow ()
156 {
157 }
158
159 void
160 PluginUIWindow::set_parent (Gtk::Window* win)
161 {
162         parent = win;
163 }
164
165 void
166 PluginUIWindow::on_map ()
167 {
168         Window::on_map ();
169         set_keep_above (true);
170 }
171
172 bool
173 PluginUIWindow::on_enter_notify_event (GdkEventCrossing *ev)
174 {
175         Keyboard::the_keyboard().enter_window (ev, this);
176         return false;
177 }
178
179 bool
180 PluginUIWindow::on_leave_notify_event (GdkEventCrossing *ev)
181 {
182         Keyboard::the_keyboard().leave_window (ev, this);
183         return false;
184 }
185
186 bool
187 PluginUIWindow::on_focus_in_event (GdkEventFocus *ev)
188 {
189         Window::on_focus_in_event (ev);
190         //Keyboard::the_keyboard().magic_widget_grab_focus ();
191         return false;
192 }
193
194 bool
195 PluginUIWindow::on_focus_out_event (GdkEventFocus *ev)
196 {
197         Window::on_focus_out_event (ev);
198         //Keyboard::the_keyboard().magic_widget_drop_focus ();
199         return false;
200 }
201
202 void
203 PluginUIWindow::on_show ()
204 {
205         if (_pluginui) {
206                 _pluginui->update_presets ();
207         }
208
209         Window::on_show ();
210
211         if (parent) {
212                 // set_transient_for (*parent);
213         }
214 }
215
216 void
217 PluginUIWindow::on_hide ()
218 {
219         Window::on_hide ();
220 }
221
222 bool
223 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
224 {
225 #ifndef VST_SUPPORT
226         return false;
227 #else
228
229         boost::shared_ptr<VSTPlugin> vp;
230
231         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
232                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
233                               << endmsg;
234                 throw failed_constructor ();
235         } else {
236                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
237         
238                 _pluginui = vpu;
239                 add (*vpu);
240                 vpu->package (*this);
241         }
242
243         non_gtk_gui = true;
244         return true;
245 #endif
246 }
247
248 bool
249 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
250 {
251 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
252         return false;
253 #else
254         VBox* box;
255         _pluginui = create_au_gui (insert, &box);
256         add (*box);
257         non_gtk_gui = true;
258
259         extern sigc::signal<void,bool> ApplicationActivationChanged;
260         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
261
262         return true;
263 #endif
264 }
265
266 void
267 PluginUIWindow::app_activated (bool yn)
268 {
269 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
270         cerr << "APP activated ? " << yn << endl;
271         if (_pluginui) {
272                 if (yn) {
273                         if (was_visible) {
274                                 _pluginui->activate ();
275                                 present ();
276                                 was_visible = true;
277                         }
278                 } else {
279                         was_visible = is_visible();
280                         hide ();
281                         _pluginui->deactivate ();
282                 }
283         } 
284 #endif
285 }
286
287 bool
288 PluginUIWindow::create_lv2_editor(boost::shared_ptr<PluginInsert> insert)
289 {
290 #ifndef HAVE_LV2
291         return false;
292 #else
293
294         boost::shared_ptr<LV2Plugin> vp;
295         
296         if ((vp = boost::dynamic_pointer_cast<LV2Plugin> (insert->plugin())) == 0) {
297                 error << _("create_lv2_editor called on non-LV2 plugin") << endmsg;
298                 throw failed_constructor ();
299         } else {
300                 LV2PluginUI* lpu = new LV2PluginUI (insert, vp);
301                 _pluginui = lpu;
302                 add (*lpu);
303                 lpu->package (*this);
304         }
305
306         non_gtk_gui = false;
307         return true;
308 #endif
309 }
310
311 bool
312 PluginUIWindow::on_key_press_event (GdkEventKey* event)
313 {
314         if (!key_press_focus_accelerator_handler (*this, event)) {
315                 return PublicEditor::instance().on_key_press_event(event);
316         } else {
317                 return true;
318         }
319 }
320
321 bool
322 PluginUIWindow::on_key_release_event (GdkEventKey* event)
323 {
324         return true;
325 }
326
327 void
328 PluginUIWindow::plugin_going_away ()
329 {
330         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
331         
332         if (_pluginui) {
333                 _pluginui->stop_updating(0);
334         }
335         delete_when_idle (this);
336 }
337
338 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
339         : insert (pi),
340           plugin (insert->plugin()),
341           save_button(_("Add")),
342           bypass_button (_("Bypass")),
343           latency_gui (*pi, pi->session().frame_rate(), pi->session().get_block_size())
344 {
345         //preset_combo.set_use_arrows_always(true);
346         update_presets();
347         preset_combo.set_size_request (100, -1);
348         preset_combo.set_active_text ("");
349         preset_combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
350
351         save_button.set_name ("PluginSaveButton");
352         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
353
354         insert->ActiveChanged.connect (bind(
355                         mem_fun(*this, &PlugUIBase::processor_active_changed),
356                         boost::weak_ptr<Processor>(insert)));
357         
358         bypass_button.set_active (!pi->active());
359
360         bypass_button.set_name ("PluginBypassButton");
361         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
362         focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
363
364         focus_button.signal_button_release_event().connect (mem_fun(*this, &PlugUIBase::focus_toggled));
365         focus_button.add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
366
367         /* these images are not managed, so that we can remove them at will */
368
369         focus_out_image = new Image (get_icon (X_("computer_keyboard")));
370         focus_in_image = new Image (get_icon (X_("computer_keyboard_active")));
371         
372         focus_button.add (*focus_out_image);
373
374         ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to focus all keyboard events on this plugin window"), "");
375         ARDOUR_UI::instance()->set_tip (&bypass_button, _("Click to enable/disable this plugin"), "");
376 }
377
378 void
379 PlugUIBase::processor_active_changed (boost::weak_ptr<Processor> weak_p)
380 {
381         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlugUIBase::processor_active_changed), weak_p));
382         boost::shared_ptr<Processor> p (weak_p);
383         if (p) {
384                 bypass_button.set_active (!p->active());
385         }
386 }
387
388 void
389 PlugUIBase::setting_selected()
390 {
391         if (preset_combo.get_active_text().length() > 0) {
392                 const Plugin::PresetRecord* pr = plugin->preset_by_label(preset_combo.get_active_text());
393                 if (pr) {
394                         plugin->load_preset(pr->uri);
395                 } else {
396                         warning << string_compose(_("Plugin preset %1 not found"),
397                                         preset_combo.get_active_text()) << endmsg;
398                 }
399         }
400 }
401
402 void
403 PlugUIBase::save_plugin_setting ()
404 {
405         ArdourPrompter prompter (true);
406         prompter.set_prompt(_("Name of New Preset:"));
407         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
408         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
409         prompter.set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
410
411         prompter.show_all();
412
413         switch (prompter.run ()) {
414         case Gtk::RESPONSE_ACCEPT:
415                 string name;
416                 prompter.get_result(name);
417                 if (name.length()) {
418                         if (plugin->save_preset(name)) {
419                                 update_presets();
420                                 preset_combo.set_active_text (name);
421                         }
422                 }
423                 break;
424         }
425 }
426
427 void
428 PlugUIBase::bypass_toggled ()
429 {
430         bool x;
431
432         if ((x = bypass_button.get_active()) == insert->active()) {
433                 if (x) {
434                         insert->deactivate ();
435                 } else {
436                         insert->activate ();
437                 }
438         }
439 }
440
441 bool
442 PlugUIBase::focus_toggled (GdkEventButton* ev)
443 {
444         if (Keyboard::the_keyboard().some_magic_widget_has_focus()) {
445                 Keyboard::the_keyboard().magic_widget_drop_focus();
446                 focus_button.remove ();
447                 focus_button.add (*focus_out_image);
448                 focus_out_image->show ();
449                 ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to focus all keyboard events on this plugin window"), "");
450         } else {
451                 Keyboard::the_keyboard().magic_widget_grab_focus();
452                 focus_button.remove ();
453                 focus_button.add (*focus_in_image);
454                 focus_in_image->show ();
455                 ARDOUR_UI::instance()->set_tip (&focus_button, _("Click to remove keyboard focus from this plugin window"), "");
456         }
457
458         return true;
459 }
460
461 void
462 PlugUIBase::update_presets ()
463 {
464         vector<string> preset_labels;
465         vector<ARDOUR::Plugin::PresetRecord> presets = plugin->get_presets();
466         for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator i = presets.begin();
467                    i != presets.end(); ++i) {
468                 preset_labels.push_back(i->label);
469         }
470         set_popdown_strings (preset_combo, preset_labels);
471 }