* first working version of editing MIDI channels of individual notes, see: http:...
[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/plugin_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 (Gtk::Window* win, boost::shared_ptr<PluginInsert> insert, bool scrollable)
65         : parent (win)
66 {
67         bool have_gui = false;
68         non_gtk_gui = false;
69         was_visible = false;
70
71         Label* label = manage (new Label());
72         label->set_markup ("<b>THIS IS THE PLUGIN UI</b>");
73
74         if (insert->plugin()->has_editor()) {
75                 switch (insert->type()) {
76                 case ARDOUR::VST:
77                         have_gui = create_vst_editor (insert);
78                         break;
79
80                 case ARDOUR::AudioUnit:
81                         have_gui = create_audiounit_editor (insert);
82                         break;
83                         
84                 case ARDOUR::LADSPA:
85                         error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
86                         break;
87
88                 default:
89 #ifndef VST_SUPPORT
90                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
91                               << endmsg;
92 #else
93                         error << _("unknown type of editor-supplying plugin")
94                               << endmsg;
95 #endif
96                         throw failed_constructor ();
97                 }
98
99         } 
100
101         if (!have_gui) {
102
103                 GenericPluginUI*  pu  = new GenericPluginUI (insert, scrollable);
104                 
105                 _pluginui = pu;
106                 add (*pu);
107                 
108                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
109
110                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
111                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
112         }
113
114         // set_position (Gtk::WIN_POS_MOUSE);
115         set_name ("PluginEditor");
116         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
117
118         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)), false);
119         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
120
121         gint h = _pluginui->get_preferred_height ();
122         gint w = _pluginui->get_preferred_width ();
123
124         if (scrollable) {
125                 if (h > 600) h = 600;
126                 if (w > 600) w = 600;
127
128                 if (w < 0) {
129                         w = 450;
130                 }
131         }
132
133         set_default_size (w, h); 
134 }
135
136 PluginUIWindow::~PluginUIWindow ()
137 {
138 }
139
140 void
141 PluginUIWindow::set_parent (Gtk::Window* win)
142 {
143         parent = win;
144 }
145
146 void
147 PluginUIWindow::on_map ()
148 {
149         Window::on_map ();
150         set_keep_above (true);
151 }
152
153 void
154 PluginUIWindow::on_show ()
155 {
156         if (_pluginui) {
157                 _pluginui->update_presets ();
158         }
159
160         Window::on_show ();
161
162         if (parent) {
163                 // set_transient_for (*parent);
164         }
165 }
166
167 void
168 PluginUIWindow::on_hide ()
169 {
170         Window::on_hide ();
171 }
172
173 bool
174 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
175 {
176 #ifndef VST_SUPPORT
177         return false;
178 #else
179
180         boost::shared_ptr<VSTPlugin> vp;
181
182         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
183                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
184                               << endmsg;
185                 throw failed_constructor ();
186         } else {
187                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
188         
189                 _pluginui = vpu;
190                 add (*vpu);
191                 vpu->package (*this);
192         }
193
194         non_gtk_gui = true;
195         return true;
196 #endif
197 }
198
199 bool
200 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
201 {
202 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
203         return false;
204 #else
205         VBox* box;
206         _pluginui = create_au_gui (insert, &box);
207         add (*box);
208         non_gtk_gui = true;
209
210         extern sigc::signal<void,bool> ApplicationActivationChanged;
211         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
212
213         return true;
214 #endif
215 }
216
217 void
218 PluginUIWindow::app_activated (bool yn)
219 {
220 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
221         cerr << "APP activated ? " << yn << endl;
222         if (_pluginui) {
223                 if (yn) {
224                         if (was_visible) {
225                                 _pluginui->activate ();
226                                 present ();
227                                 was_visible = true;
228                         }
229                 } else {
230                         was_visible = is_visible();
231                         hide ();
232                         _pluginui->deactivate ();
233                 }
234         } 
235 #endif
236 }
237
238 bool
239 PluginUIWindow::on_key_press_event (GdkEventKey* event)
240 {
241         if (non_gtk_gui) {
242                 return false;
243         }
244
245         if (!key_press_focus_accelerator_handler (*this, event)) {
246                 return PublicEditor::instance().on_key_press_event(event);
247         } else {
248                 return true;
249         }
250 }
251
252 bool
253 PluginUIWindow::on_key_release_event (GdkEventKey* event)
254 {
255         return true;
256 }
257
258 void
259 PluginUIWindow::plugin_going_away ()
260 {
261         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
262         
263         if (_pluginui) {
264                 _pluginui->stop_updating(0);
265         }
266         delete_when_idle (this);
267 }
268
269 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
270         : insert (pi),
271           plugin (insert->plugin()),
272           save_button(_("Add")),
273           bypass_button (_("Bypass")),
274           latency_gui (*pi, pi->session().frame_rate(), pi->session().get_block_size())
275 {
276         //preset_combo.set_use_arrows_always(true);
277         set_popdown_strings (preset_combo, plugin->get_presets());
278         preset_combo.set_size_request (100, -1);
279         preset_combo.set_active_text ("");
280         preset_combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
281
282         save_button.set_name ("PluginSaveButton");
283         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
284
285         insert->ActiveChanged.connect (bind(
286                         mem_fun(*this, &PlugUIBase::processor_active_changed),
287                         boost::weak_ptr<Processor>(insert)));
288         
289         bypass_button.set_active (!pi->active());
290
291         bypass_button.set_name ("PluginBypassButton");
292         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
293 }
294
295 void
296 PlugUIBase::processor_active_changed (boost::weak_ptr<Processor> weak_p)
297 {
298         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PlugUIBase::processor_active_changed), weak_p));
299         boost::shared_ptr<Processor> p (weak_p);
300         if (p) {
301                 bypass_button.set_active (!p->active());
302         }
303 }
304
305 void
306 PlugUIBase::setting_selected()
307 {
308         if (preset_combo.get_active_text().length() > 0) {
309                 if (!plugin->load_preset(preset_combo.get_active_text())) {
310                         warning << string_compose(_("Plugin preset %1 not found"), preset_combo.get_active_text()) << endmsg;
311                 }
312         }
313 }
314
315 void
316 PlugUIBase::save_plugin_setting ()
317 {
318         ArdourPrompter prompter (true);
319         prompter.set_prompt(_("Name of New Preset:"));
320         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
321         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
322
323         prompter.show_all();
324
325         switch (prompter.run ()) {
326         case Gtk::RESPONSE_ACCEPT:
327
328                 string name;
329
330                 prompter.get_result(name);
331
332                 if (name.length()) {
333                         if(plugin->save_preset(name)){
334                                 set_popdown_strings (preset_combo, plugin->get_presets());
335                                 preset_combo.set_active_text (name);
336                         }
337                 }
338                 break;
339         }
340 }
341
342 void
343 PlugUIBase::bypass_toggled ()
344 {
345         bool x;
346
347         if ((x = bypass_button.get_active()) == insert->active()) {
348                 insert->set_active (!x);
349                 if (insert->active()) {
350                         bypass_button.set_label (_("Bypass"));
351                 } else {
352                         bypass_button.set_label (_("Active"));
353                 }
354         }
355 }
356
357 void
358 PlugUIBase::update_presets ()
359 {
360         set_popdown_strings (preset_combo, plugin->get_presets());
361 }