fix some layering/display problems with AU GUIs (requires new patches for GDK/Quartz)
[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 <gtkmm2ext/click_box.h>
31 #include <gtkmm2ext/fastmeter.h>
32 #include <gtkmm2ext/barcontroller.h>
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/doi.h>
35 #include <gtkmm2ext/slider_controller.h>
36
37 #include <midi++/manager.h>
38
39 #include <ardour/plugin.h>
40 #include <ardour/insert.h>
41 #include <ardour/ladspa_plugin.h>
42 #ifdef VST_SUPPORT
43 #include <ardour/vst_plugin.h>
44 #endif
45
46 #include <lrdf.h>
47
48 #include "ardour_ui.h"
49 #include "prompter.h"
50 #include "plugin_ui.h"
51 #include "utils.h"
52 #include "gui_thread.h"
53 #include "public_editor.h"
54
55 #include "i18n.h"
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace PBD;
60 using namespace Gtkmm2ext;
61 using namespace Gtk;
62 using namespace sigc;
63
64 PluginUIWindow::PluginUIWindow (boost::shared_ptr<PluginInsert> insert, bool scrollable)
65 {
66         bool have_gui = false;
67         non_gtk_gui = false;
68
69         if (insert->plugin()->has_editor()) {
70                 switch (insert->type()) {
71                 case ARDOUR::VST:
72                         have_gui = create_vst_editor (insert);
73                         break;
74
75                 case ARDOUR::AudioUnit:
76                         have_gui = create_audiounit_editor (insert);
77                         break;
78                         
79                 case ARDOUR::LADSPA:
80                         error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
81                         break;
82
83                 default:
84 #ifndef VST_SUPPORT
85                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
86                               << endmsg;
87 #else
88                         error << _("unknown type of editor-supplying plugin")
89                               << endmsg;
90 #endif
91                         throw failed_constructor ();
92                 }
93
94         } 
95
96         if (!have_gui) {
97
98                 GenericPluginUI*  pu  = new GenericPluginUI (insert, scrollable);
99                 
100                 _pluginui = pu;
101                 add (*pu);
102                 
103                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
104
105                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
106                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
107         }
108
109         // set_position (Gtk::WIN_POS_MOUSE);
110         set_name ("PluginEditor");
111         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
112
113         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
114         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
115
116         gint h = _pluginui->get_preferred_height ();
117         gint w = _pluginui->get_preferred_width ();
118
119         if (scrollable) {
120                 if (h > 600) h = 600;
121                 if (w > 600) w = 600;
122
123                 if (w < 0) {
124                         w = 450;
125                 }
126         }
127
128         set_default_size (w, h); 
129 }
130
131 PluginUIWindow::~PluginUIWindow ()
132 {
133 }
134
135 void
136 PluginUIWindow::on_map ()
137 {
138         Window::on_map ();
139         set_keep_above (true);
140 }
141
142 void
143 PluginUIWindow::on_show ()
144 {
145         if (_pluginui) {
146                 _pluginui->update_presets ();
147         }
148         Window::on_show ();
149 }
150
151 void
152 PluginUIWindow::on_hide ()
153 {
154         Window::on_hide ();
155 }
156
157 bool
158 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
159 {
160 #ifndef VST_SUPPORT
161         return false;
162 #else
163
164         boost::shared_ptr<VSTPlugin> vp;
165
166         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
167                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
168                               << endmsg;
169                 throw failed_constructor ();
170         } else {
171                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
172         
173                 _pluginui = vpu;
174                 add (*vpu);
175                 vpu->package (*this);
176         }
177
178         non_gtk_gui = true;
179         return true;
180 #endif
181 }
182
183 bool
184 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
185 {
186 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
187         return false;
188 #else
189         VBox* box;
190         _pluginui = create_au_gui (insert, &box);
191         add (*box);
192         non_gtk_gui = true;
193
194         extern sigc::signal<void,bool> ApplicationActivationChanged;
195         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
196
197         return true;
198 #endif
199 }
200
201 void
202 PluginUIWindow::app_activated (bool yn)
203 {
204 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
205         cerr << "APP activated ? " << yn << endl;
206         if (_pluginui) {
207                 if (yn) {
208                         _pluginui->activate ();
209                         present ();
210                 } else {
211                         hide ();
212                         _pluginui->deactivate ();
213                 }
214         } 
215 #endif
216 }
217
218 bool
219 PluginUIWindow::on_key_press_event (GdkEventKey* event)
220 {
221         if (non_gtk_gui) {
222                 return false;
223         }
224
225         if (!key_press_focus_accelerator_handler (*this, event)) {
226                 return PublicEditor::instance().on_key_press_event(event);
227         } else {
228                 return true;
229         }
230 }
231
232 bool
233 PluginUIWindow::on_key_release_event (GdkEventKey* event)
234 {
235         return true;
236 }
237
238 void
239 PluginUIWindow::plugin_going_away ()
240 {
241         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
242         
243         if (_pluginui) {
244                 _pluginui->stop_updating(0);
245         }
246         delete_when_idle (this);
247 }
248
249 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
250         : insert (pi),
251           plugin (insert->plugin()),
252           save_button(_("Add")),
253           bypass_button (_("Bypass"))
254 {
255         //preset_combo.set_use_arrows_always(true);
256         set_popdown_strings (preset_combo, plugin->get_presets());
257         preset_combo.set_size_request (100, -1);
258         preset_combo.set_active_text ("");
259         preset_combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
260
261         save_button.set_name ("PluginSaveButton");
262         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
263
264         insert->active_changed.connect (mem_fun(*this, &PlugUIBase::redirect_active_changed));
265         bypass_button.set_active (!pi->active());
266
267         bypass_button.set_name ("PluginBypassButton");
268         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
269 }
270
271 void
272 PlugUIBase::redirect_active_changed (Redirect* r, void* src)
273 {
274         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlugUIBase::redirect_active_changed), r, src));
275         bypass_button.set_active (!r->active());
276 }
277
278 void
279 PlugUIBase::setting_selected()
280 {
281         if (preset_combo.get_active_text().length() > 0) {
282                 if (!plugin->load_preset(preset_combo.get_active_text())) {
283                         warning << string_compose(_("Plugin preset %1 not found"), preset_combo.get_active_text()) << endmsg;
284                 }
285         }
286 }
287
288 void
289 PlugUIBase::save_plugin_setting ()
290 {
291         ArdourPrompter prompter (true);
292         prompter.set_prompt(_("Name of New Preset:"));
293         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
294         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
295
296         prompter.show_all();
297
298         switch (prompter.run ()) {
299         case Gtk::RESPONSE_ACCEPT:
300
301                 string name;
302
303                 prompter.get_result(name);
304
305                 if (name.length()) {
306                         if(plugin->save_preset(name)){
307                                 set_popdown_strings (preset_combo, plugin->get_presets());
308                                 preset_combo.set_active_text (name);
309                         }
310                 }
311                 break;
312         }
313 }
314
315 void
316 PlugUIBase::bypass_toggled ()
317 {
318         bool x;
319
320         if ((x = bypass_button.get_active()) == insert->active()) {
321                 insert->set_active (!x, this);
322                 if (insert->active()) {
323                         bypass_button.set_label (_("Bypass"));
324                 } else {
325                         bypass_button.set_label (_("Active"));
326                 }
327         }
328 }
329
330 void
331 PlugUIBase::update_presets ()
332 {
333         set_popdown_strings (preset_combo, plugin->get_presets());
334 }