changes associated with save/restore of AutomationControl id's
[ardour2.git] / gtk2_ardour / vst_pluginui.cc
blob6c1b359fd4b0e232e0728d24d49d840257bb1714
1 /*
2 Copyright (C) 2004 Paul Davis
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.
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.
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.
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"
26 #include "plugin_ui.h"
28 #include <gdk/gdkx.h>
30 using namespace Gtk;
31 using namespace ARDOUR;
32 using namespace PBD;
34 VSTPluginUI::VSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vp)
35 : PlugUIBase (pi),
36 vst (vp)
38 create_preset_store ();
40 fst_run_editor (vst->fst());
42 preset_box.set_spacing (6);
43 preset_box.set_border_width (6);
44 preset_box.pack_end (bypass_button, false, false, 10);
45 preset_box.pack_end (save_button, false, false);
46 preset_box.pack_end (vst_preset_combo, false, false);
48 vst_preset_combo.signal_changed().connect (sigc::mem_fun (*this, &VSTPluginUI::preset_chosen));
50 bypass_button.set_active (!insert->active());
52 pack_start (preset_box, false, false);
53 pack_start (socket, true, true);
54 pack_start (plugin_analysis_expander, true, true);
57 VSTPluginUI::~VSTPluginUI ()
59 // plugin destructor destroys the custom GUI, via Windows fun-and-games,
60 // and then our PluginUIWindow does the rest
63 void
64 VSTPluginUI::preset_chosen ()
66 // we can't dispatch directly here, too many plugins only expects one GUI thread.
67 vst->fst()->want_program = vst_preset_combo.get_active_row_number ();
68 socket.grab_focus ();
71 int
72 VSTPluginUI::get_preferred_height ()
74 return vst->fst()->height;
77 int
78 VSTPluginUI::get_preferred_width ()
80 return vst->fst()->width;
83 int
84 VSTPluginUI::package (Gtk::Window& win)
86 /* forward configure events to plugin window */
88 win.signal_configure_event().connect (sigc::bind (sigc::mem_fun (*this, &VSTPluginUI::configure_handler), &socket), false);
91 this assumes that the window's owner understands the XEmbed protocol.
94 socket.add_id (fst_get_XID (vst->fst()));
96 fst_move_window_into_view (vst->fst());
98 return 0;
101 bool
102 VSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
104 XEvent event;
105 gint x, y;
106 GdkWindow* w;
108 if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
109 return false;
112 event.xconfigure.type = ConfigureNotify;
113 event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
114 event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
116 /* The ICCCM says that synthetic events should have root relative
117 * coordinates. We still aren't really ICCCM compliant, since
118 * we don't send events when the real toplevel is moved.
120 gdk_error_trap_push ();
121 gdk_window_get_origin (w, &x, &y);
122 gdk_error_trap_pop ();
124 event.xconfigure.x = x;
125 event.xconfigure.y = y;
126 event.xconfigure.width = GTK_WIDGET(socket->gobj())->allocation.width;
127 event.xconfigure.height = GTK_WIDGET(socket->gobj())->allocation.height;
129 event.xconfigure.border_width = 0;
130 event.xconfigure.above = None;
131 event.xconfigure.override_redirect = False;
133 gdk_error_trap_push ();
134 XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
135 gdk_error_trap_pop ();
137 return false;
140 void
141 VSTPluginUI::create_preset_store ()
143 FST *fst = vst->fst();
144 int vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, NULL, 0.0f);
146 preset_model = ListStore::create (preset_columns);
148 for (int i = 0; i < fst->plugin->numPrograms; ++i) {
149 char buf[100];
150 TreeModel::Row row = *(preset_model->append());
152 snprintf (buf, 90, "preset %d", i);
154 if (vst_version >= 2) {
155 fst->plugin->dispatcher (fst->plugin, 29, i, 0, buf, 0.0);
158 row[preset_columns.name] = buf;
159 row[preset_columns.number] = i;
162 if (fst->plugin->numPrograms > 0) {
163 fst->plugin->dispatcher( fst->plugin, effSetProgram, 0, 0, NULL, 0.0 );
166 vst_preset_combo.set_model (preset_model);
168 CellRenderer* renderer = manage (new CellRendererText());
169 vst_preset_combo.pack_start (*renderer, true);
170 vst_preset_combo.add_attribute (*renderer, "text", 0);
172 if (vst->fst()->current_program != -1) {
173 vst_preset_combo.set_active (vst->fst()->current_program);
174 } else {
175 vst_preset_combo.set_active (0);
179 typedef int (*error_handler_t)( Display *, XErrorEvent *);
180 static Display *the_gtk_display;
181 static error_handler_t wine_error_handler;
182 static error_handler_t gtk_error_handler;
184 static int
185 fst_xerror_handler( Display *disp, XErrorEvent *ev )
187 if (disp == the_gtk_display) {
188 printf ("relaying error to gtk\n");
189 return gtk_error_handler (disp, ev);
190 } else {
191 printf( "relaying error to wine\n" );
192 return wine_error_handler (disp, ev);
196 void
197 gui_init (int *argc, char **argv[])
199 wine_error_handler = XSetErrorHandler (NULL);
200 gtk_init (argc, argv);
201 the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
202 gtk_error_handler = XSetErrorHandler( fst_xerror_handler );