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