Move RDF-based preset stuff into LadspaPlugin, to make way for a set of evil hacks...
[ardour.git] / gtk2_ardour / vst_pluginui.cc
1 /*
2     Copyright (C) 2004 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 <fst.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtksocket.h>
23 #include "ardour/plugin_insert.h"
24 #include "ardour/vst_plugin.h"
25
26 #include "plugin_ui.h"
27
28 #include <gdk/gdkx.h>
29
30 using namespace Gtk;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 VSTPluginUI::VSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vp)
35         : PlugUIBase (pi),
36           vst (vp)
37 {
38         preset_model = ListStore::create (preset_columns);
39
40         CellRenderer* renderer = manage (new CellRendererText());
41         vst_preset_combo.pack_start (*renderer, true);
42         vst_preset_combo.add_attribute (*renderer, "text", 0);
43         vst_preset_combo.set_model (preset_model);
44
45         update_presets ();
46
47         fst_run_editor (vst->fst());
48
49         preset_box.set_spacing (6);
50         preset_box.set_border_width (6);
51         preset_box.pack_end (bypass_button, false, false, 10);
52         preset_box.pack_end (save_button, false, false);
53         preset_box.pack_end (vst_preset_combo, false, false);
54
55         vst_preset_combo.signal_changed().connect (sigc::mem_fun (*this, &VSTPluginUI::preset_chosen));
56
57         bypass_button.set_active (!insert->active());
58
59         pack_start (preset_box, false, false);
60         pack_start (socket, true, true);
61         pack_start (plugin_analysis_expander, true, true);
62 }
63
64 VSTPluginUI::~VSTPluginUI ()
65 {
66         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
67         // and then our PluginUIWindow does the rest
68 }
69
70 void
71 VSTPluginUI::preset_chosen ()
72 {
73         int const r = vst_preset_combo.get_active_row_number ();
74
75         if (r < vst->first_user_preset_index()) {
76                 /* This is a plugin-provided preset.
77                    We can't dispatch directly here; too many plugins expects only one GUI thread.
78                 */
79                 vst->fst()->want_program = r;
80         } else {
81                 /* This is a user preset.  This method knows about the direct dispatch restriction, too */
82                 TreeModel::iterator i = vst_preset_combo.get_active ();
83                 plugin->load_preset ((*i)[preset_columns.name]);
84         }
85         
86         socket.grab_focus ();
87 }
88
89 int
90 VSTPluginUI::get_preferred_height ()
91 {
92         return vst->fst()->height;
93 }
94
95 int
96 VSTPluginUI::get_preferred_width ()
97 {
98         return vst->fst()->width;
99 }
100
101 int
102 VSTPluginUI::package (Gtk::Window& win)
103 {
104         /* forward configure events to plugin window */
105
106         win.signal_configure_event().connect (sigc::bind (sigc::mem_fun (*this, &VSTPluginUI::configure_handler), &socket), false);
107
108         /*
109            this assumes that the window's owner understands the XEmbed protocol.
110         */
111
112         socket.add_id (fst_get_XID (vst->fst()));
113
114         fst_move_window_into_view (vst->fst());
115
116         return 0;
117 }
118
119 bool
120 VSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
121 {
122         XEvent event;
123         gint x, y;
124         GdkWindow* w;
125
126         if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
127                 return false;
128         }
129
130         event.xconfigure.type = ConfigureNotify;
131         event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
132         event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
133
134         /* The ICCCM says that synthetic events should have root relative
135          * coordinates. We still aren't really ICCCM compliant, since
136          * we don't send events when the real toplevel is moved.
137          */
138         gdk_error_trap_push ();
139         gdk_window_get_origin (w, &x, &y);
140         gdk_error_trap_pop ();
141
142         event.xconfigure.x = x;
143         event.xconfigure.y = y;
144         event.xconfigure.width = GTK_WIDGET(socket->gobj())->allocation.width;
145         event.xconfigure.height = GTK_WIDGET(socket->gobj())->allocation.height;
146
147         event.xconfigure.border_width = 0;
148         event.xconfigure.above = None;
149         event.xconfigure.override_redirect = False;
150
151         gdk_error_trap_push ();
152         XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
153         gdk_error_trap_pop ();
154
155         return false;
156 }
157
158 void
159 VSTPluginUI::update_presets ()
160 {
161         std::vector<Plugin::PresetRecord> presets = plugin->get_presets ();
162
163         preset_model->clear ();
164
165         int j = 0;
166         for (std::vector<Plugin::PresetRecord>::const_iterator i = presets.begin(); i != presets.end(); ++i) {
167                 TreeModel::Row row = *(preset_model->append ());
168                 row[preset_columns.name] = i->label;
169                 row[preset_columns.number] = j++;
170         }
171
172         if (presets.size() > 0) {
173                 vst->fst()->plugin->dispatcher (vst->fst()->plugin, effSetProgram, 0, 0, NULL, 0);
174         }
175
176         if (vst->fst()->current_program != -1) {
177                 vst_preset_combo.set_active (vst->fst()->current_program);
178         } else {
179                 vst_preset_combo.set_active (0);
180         }
181 }
182
183 typedef int (*error_handler_t)( Display *, XErrorEvent *);
184 static Display *the_gtk_display;
185 static error_handler_t wine_error_handler;
186 static error_handler_t gtk_error_handler;
187
188 static int
189 fst_xerror_handler( Display *disp, XErrorEvent *ev )
190 {
191         if (disp == the_gtk_display) {
192                 printf ("relaying error to gtk\n");
193                 return gtk_error_handler (disp, ev);
194         } else {
195                 printf( "relaying error to wine\n" );
196                 return wine_error_handler (disp, ev);
197         }
198 }
199
200 void
201 gui_init (int *argc, char **argv[])
202 {
203         wine_error_handler = XSetErrorHandler (NULL);
204         gtk_init (argc, argv);
205         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
206         gtk_error_handler = XSetErrorHandler( fst_xerror_handler );
207 }
208