Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[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         : ArdourDialog ("plugin ui")
66 {
67         bool have_gui = false;
68         non_gtk_gui = false;
69
70         if (insert->plugin()->has_editor()) {
71                 switch (insert->type()) {
72                 case ARDOUR::VST:
73                         have_gui = create_vst_editor (insert);
74                         break;
75
76                 case ARDOUR::AudioUnit:
77                         have_gui = create_audiounit_editor (insert);
78                         break;
79                         
80                 case ARDOUR::LADSPA:
81                         error << _("Eh? LADSPA plugins don't have editors!") << endmsg;
82                         break;
83
84                 default:
85                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
86                               << endmsg;
87                         throw failed_constructor ();
88                 }
89
90         } 
91
92         if (!have_gui) {
93
94                 GenericPluginUI*  pu  = new GenericPluginUI (insert, scrollable);
95                 
96                 _pluginui = pu;
97                 get_vbox()->add (*pu);
98                 
99                 set_wmclass (X_("ardour_plugin_editor"), "Ardour");
100
101                 signal_map_event().connect (mem_fun (*pu, &GenericPluginUI::start_updating));
102                 signal_unmap_event().connect (mem_fun (*pu, &GenericPluginUI::stop_updating));
103         }
104
105         set_position (Gtk::WIN_POS_MOUSE);
106         set_name ("PluginEditor");
107         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
108
109         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)));
110         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
111
112         gint h = _pluginui->get_preferred_height ();
113         gint w = _pluginui->get_preferred_width ();
114
115         if (scrollable) {
116                 if (h > 600) h = 600;
117                 if (w > 600) w = 600;
118
119                 if (w < 0) {
120                         w = 450;
121                 }
122         }
123
124         set_default_size (w, h); 
125 }
126
127 PluginUIWindow::~PluginUIWindow ()
128 {
129 }
130
131 bool
132 PluginUIWindow::create_vst_editor(boost::shared_ptr<PluginInsert> insert)
133 {
134 #ifndef VST_SUPPORT
135         return false;
136 #else
137
138         boost::shared_ptr<VSTPlugin> vp;
139
140         if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) == 0) {
141                 error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
142                               << endmsg;
143                 throw failed_constructor ();
144         } else {
145                 VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
146         
147                 _pluginui = vpu;
148                 get_vbox()->add (*vpu);
149                 vpu->package (*this);
150         }
151
152         non_gtk_gui = true;
153         return true;
154 #endif
155 }
156
157 bool
158 PluginUIWindow::create_audiounit_editor (boost::shared_ptr<PluginInsert> insert)
159 {
160 #if !defined(HAVE_AUDIOUNITS) || !defined(GTKOSX)
161         return false;
162 #else
163         VBox* box;
164         _pluginui = create_au_gui (insert, &box);
165         get_vbox()->add (*box);
166         non_gtk_gui = true;
167
168         extern sigc::signal<void,bool> ApplicationActivationChanged;
169         ApplicationActivationChanged.connect (mem_fun (*this, &PluginUIWindow::app_activated));
170
171         return true;
172 #endif
173 }
174
175 void
176 PluginUIWindow::app_activated (bool yn)
177 {
178 #if defined (HAVE_AUDIOUNITS) && defined(GTKOSX)
179         if (yn) {
180                 _pluginui->activate ();
181         }
182         cerr << "activated ? " << yn << endl;
183 #endif
184 }
185
186 bool
187 PluginUIWindow::on_key_press_event (GdkEventKey* event)
188 {
189         if (non_gtk_gui) {
190                 return false;
191         }
192
193         if (!key_press_focus_accelerator_handler (*this, event)) {
194                 return PublicEditor::instance().on_key_press_event(event);
195         } else {
196                 return true;
197         }
198 }
199
200 bool
201 PluginUIWindow::on_key_release_event (GdkEventKey* event)
202 {
203         return true;
204 }
205
206 void
207 PluginUIWindow::plugin_going_away ()
208 {
209         ENSURE_GUI_THREAD(mem_fun(*this, &PluginUIWindow::plugin_going_away));
210         
211         _pluginui->stop_updating(0);
212         delete_when_idle (this);
213 }
214
215 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
216         : insert (pi),
217           plugin (insert->plugin()),
218           save_button(_("Add")),
219           bypass_button (_("Bypass")),
220           latency_gui (*pi, pi->session().frame_rate(), pi->session().get_block_size())
221 {
222         //combo.set_use_arrows_always(true);
223         set_popdown_strings (combo, plugin->get_presets());
224         combo.set_size_request (100, -1);
225         combo.set_active_text ("");
226         combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
227
228         save_button.set_name ("PluginSaveButton");
229         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
230
231         bypass_button.set_name ("PluginBypassButton");
232         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
233 }
234
235 void
236 PlugUIBase::setting_selected()
237 {
238         if (combo.get_active_text().length() > 0) {
239                 if (!plugin->load_preset(combo.get_active_text())) {
240                         warning << string_compose(_("Plugin preset %1 not found"), combo.get_active_text()) << endmsg;
241                 }
242         }
243
244 }
245
246 void
247 PlugUIBase::save_plugin_setting ()
248 {
249         ArdourPrompter prompter (true);
250         prompter.set_prompt(_("Name of New Preset:"));
251         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
252         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
253
254         prompter.show_all();
255
256         switch (prompter.run ()) {
257         case Gtk::RESPONSE_ACCEPT:
258
259                 string name;
260
261                 prompter.get_result(name);
262
263                 if (name.length()) {
264                         if(plugin->save_preset(name)){
265                                 set_popdown_strings (combo, plugin->get_presets());
266                                 combo.set_active_text (name);
267                         }
268                 }
269                 break;
270         }
271 }
272
273 void
274 PlugUIBase::bypass_toggled ()
275 {
276         bool x;
277
278         if ((x = bypass_button.get_active()) == insert->active()) {
279                 insert->set_active (!x);
280         }
281 }