Moved PluginInfo::Type to ARDOUR::PluginType in ardour/types.h.
[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     $Id$
19 */
20
21 #include <climits>
22 #include <cerrno>
23 #include <cmath>
24 #include <string>
25
26 #include <pbd/stl_delete.h>
27 #include <pbd/xml++.h>
28 #include <pbd/failed_constructor.h>
29
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/audioengine.h>
40 #include <ardour/plugin.h>
41 #include <ardour/insert.h>
42 #include <ardour/ladspa_plugin.h>
43 #ifdef VST_SUPPORT
44 #include <ardour/vst_plugin.h>
45 #endif
46
47 #include <lrdf.h>
48
49 #include "ardour_ui.h"
50 #include "prompter.h"
51 #include "plugin_ui.h"
52 #include "utils.h"
53 #include "gui_thread.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 (AudioEngine &engine, boost::shared_ptr<PluginInsert> insert, bool scrollable)
65         : ArdourDialog ("plugin ui")
66 {
67         if (insert->plugin()->has_editor()) {
68
69 #ifdef VST_SUPPORT
70
71                 boost::shared_ptr<VSTPlugin> vp;
72
73                 if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) != 0) {
74                         
75                         
76                         VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
77                         
78                         _pluginui = vpu;
79                         get_vbox()->add (*vpu);
80                         vpu->package (*this);
81                         
82                 } else {
83 #endif
84                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
85                               << endmsg;
86                         throw failed_constructor ();
87 #ifdef VST_SUPPORT
88                 }
89 #endif
90
91         } else {
92
93                 LadspaPluginUI*  pu  = new LadspaPluginUI (engine, insert, scrollable);
94                 
95                 _pluginui = pu;
96                 get_vbox()->add (*pu);
97                 
98                 signal_map_event().connect (mem_fun (*pu, &LadspaPluginUI::start_updating));
99                 signal_unmap_event().connect (mem_fun (*pu, &LadspaPluginUI::stop_updating));
100         }
101
102         set_position (Gtk::WIN_POS_MOUSE);
103         set_name ("PluginEditor");
104         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
105
106         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)));
107         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
108
109         if (scrollable) {
110                 gint h = _pluginui->get_preferred_height ();
111                 if (h > 600) h = 600;
112                 set_default_size (450, h); 
113         }
114 }
115
116 PluginUIWindow::~PluginUIWindow ()
117 {
118 }
119
120 void
121 PluginUIWindow::plugin_going_away (ARDOUR::Redirect* ignored)
122 {
123         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginUIWindow::plugin_going_away), ignored));
124         
125         _pluginui->stop_updating(0);
126         delete_when_idle (this);
127 }
128
129 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
130         : insert (pi),
131           plugin (insert->plugin()),
132           save_button(_("Add")),
133           bypass_button (_("Bypass"))
134 {
135         //combo.set_use_arrows_always(true);
136         set_popdown_strings (combo, plugin->get_presets());
137         combo.set_size_request (100, -1);
138         combo.set_active_text ("");
139         combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
140
141         save_button.set_name ("PluginSaveButton");
142         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
143
144         bypass_button.set_name ("PluginBypassButton");
145         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
146 }
147
148 void
149 PlugUIBase::setting_selected()
150 {
151         if (combo.get_active_text().length() > 0) {
152                 if (!plugin->load_preset(combo.get_active_text())) {
153                         warning << string_compose(_("Plugin preset %1 not found"), combo.get_active_text()) << endmsg;
154                 }
155         }
156
157 }
158
159 void
160 PlugUIBase::save_plugin_setting ()
161 {
162         ArdourPrompter prompter (true);
163         prompter.set_prompt(_("Name of New Preset:"));
164         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
165         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
166
167         prompter.show_all();
168
169         switch (prompter.run ()) {
170         case Gtk::RESPONSE_ACCEPT:
171
172                 string name;
173
174                 prompter.get_result(name);
175
176                 if (name.length()) {
177                         if(plugin->save_preset(name)){
178                                 set_popdown_strings (combo, plugin->get_presets());
179                                 combo.set_active_text (name);
180                         }
181                 }
182                 break;
183         }
184 }
185
186 void
187 PlugUIBase::bypass_toggled ()
188 {
189         bool x;
190
191         if ((x = bypass_button.get_active()) == insert->active()) {
192                 insert->set_active (!x, this);
193         }
194 }