merge from trunk
[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 <gtkmm/widget.h>
31 #include <gtkmm2ext/click_box.h>
32 #include <gtkmm2ext/fastmeter.h>
33 #include <gtkmm2ext/barcontroller.h>
34 #include <gtkmm2ext/utils.h>
35 #include <gtkmm2ext/doi.h>
36 #include <gtkmm2ext/slider_controller.h>
37
38 #include <midi++/manager.h>
39
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 #include "public_editor.h"
55
56 #include "i18n.h"
57
58 using namespace std;
59 using namespace ARDOUR;
60 using namespace PBD;
61 using namespace Gtkmm2ext;
62 using namespace Gtk;
63 using namespace sigc;
64
65 PluginUIWindow::PluginUIWindow (boost::shared_ptr<PluginInsert> insert, bool scrollable)
66         : ArdourDialog ("plugin ui")
67 {
68         if (insert->plugin()->has_editor()) {
69
70 #ifdef VST_SUPPORT
71
72                 boost::shared_ptr<VSTPlugin> vp;
73
74                 if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (insert->plugin())) != 0) {
75                         
76                         
77                         VSTPluginUI* vpu = new VSTPluginUI (insert, vp);
78                         
79                         _pluginui = vpu;
80                         get_vbox()->add (*vpu);
81                         vpu->package (*this);
82                         
83                 } else {
84 #endif
85                         error << _("unknown type of editor-supplying plugin (note: no VST support in this version of ardour)")
86                               << endmsg;
87                         throw failed_constructor ();
88 #ifdef VST_SUPPORT
89                 }
90 #endif
91
92         } else {
93
94                 LadspaPluginUI*  pu  = new LadspaPluginUI (insert, scrollable);
95                 
96                 _pluginui = pu;
97                 get_vbox()->add (*pu);
98                 
99                 signal_map_event().connect (mem_fun (*pu, &LadspaPluginUI::start_updating));
100                 signal_unmap_event().connect (mem_fun (*pu, &LadspaPluginUI::stop_updating));
101         }
102
103         set_position (Gtk::WIN_POS_MOUSE);
104         set_name ("PluginEditor");
105         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
106
107         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)));
108         insert->GoingAway.connect (mem_fun(*this, &PluginUIWindow::plugin_going_away));
109
110         if (scrollable) {
111                 gint h = _pluginui->get_preferred_height ();
112                 if (h > 600) h = 600;
113                 set_default_size (450, h); 
114         }
115
116 }
117
118 PluginUIWindow::~PluginUIWindow ()
119 {
120 }
121 bool
122 PluginUIWindow::on_key_press_event (GdkEventKey* event)
123 {
124         return PublicEditor::instance().on_key_press_event(event);
125 }
126
127 bool
128 PluginUIWindow::on_key_release_event (GdkEventKey* event)
129 {
130         return PublicEditor::instance().on_key_release_event(event);
131 }
132
133 void
134 PluginUIWindow::plugin_going_away (ARDOUR::Redirect* ignored)
135 {
136         ENSURE_GUI_THREAD(bind (mem_fun(*this, &PluginUIWindow::plugin_going_away), ignored));
137         
138         _pluginui->stop_updating(0);
139         delete_when_idle (this);
140 }
141
142 PlugUIBase::PlugUIBase (boost::shared_ptr<PluginInsert> pi)
143         : insert (pi),
144           plugin (insert->plugin()),
145           save_button(_("Add")),
146           bypass_button (_("Bypass"))
147 {
148         //combo.set_use_arrows_always(true);
149         set_popdown_strings (combo, plugin->get_presets());
150         combo.set_size_request (100, -1);
151         combo.set_active_text ("");
152         combo.signal_changed().connect(mem_fun(*this, &PlugUIBase::setting_selected));
153
154         save_button.set_name ("PluginSaveButton");
155         save_button.signal_clicked().connect(mem_fun(*this, &PlugUIBase::save_plugin_setting));
156
157         bypass_button.set_name ("PluginBypassButton");
158         bypass_button.signal_toggled().connect (mem_fun(*this, &PlugUIBase::bypass_toggled));
159 }
160
161 void
162 PlugUIBase::setting_selected()
163 {
164         if (combo.get_active_text().length() > 0) {
165                 if (!plugin->load_preset(combo.get_active_text())) {
166                         warning << string_compose(_("Plugin preset %1 not found"), combo.get_active_text()) << endmsg;
167                 }
168         }
169
170 }
171
172 void
173 PlugUIBase::save_plugin_setting ()
174 {
175         ArdourPrompter prompter (true);
176         prompter.set_prompt(_("Name of New Preset:"));
177         prompter.add_button (Gtk::Stock::ADD, Gtk::RESPONSE_ACCEPT);
178         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
179
180         prompter.show_all();
181
182         switch (prompter.run ()) {
183         case Gtk::RESPONSE_ACCEPT:
184
185                 string name;
186
187                 prompter.get_result(name);
188
189                 if (name.length()) {
190                         if(plugin->save_preset(name)){
191                                 set_popdown_strings (combo, plugin->get_presets());
192                                 combo.set_active_text (name);
193                         }
194                 }
195                 break;
196         }
197 }
198
199 void
200 PlugUIBase::bypass_toggled ()
201 {
202         bool x;
203
204         if ((x = bypass_button.get_active()) == insert->active()) {
205                 insert->set_active (!x, this);
206         }
207 }