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