make AU Cocoa plugin views with "client-side-windows" versions of GTK+; make keyboard...
[ardour2.git] / gtk2_ardour / vst_pluginui.cc
blob19a082606567e2e21ee252a5dabca641fafccdf5
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/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 (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);
56 VSTPluginUI::~VSTPluginUI ()
58 // plugin destructor destroys the custom GUI, via Windows fun-and-games,
59 // and then our PluginUIWindow does the rest
62 void
63 VSTPluginUI::preset_chosen ()
65 // we can't dispatch directly here, too many plugins only expects one GUI thread.
66 vst->fst()->want_program = vst_preset_combo.get_active_row_number ();
67 socket.grab_focus ();
70 int
71 VSTPluginUI::get_preferred_height ()
73 return vst->fst()->height;
76 int
77 VSTPluginUI::get_preferred_width ()
79 return vst->fst()->width;
82 int
83 VSTPluginUI::package (Gtk::Window& win)
85 /* forward configure events to plugin window */
87 win.signal_configure_event().connect (bind (mem_fun (*this, &VSTPluginUI::configure_handler), &socket), false);
90 this assumes that the window's owner understands the XEmbed protocol.
93 socket.add_id (fst_get_XID (vst->fst()));
95 fst_move_window_into_view (vst->fst());
97 return 0;
100 bool
101 VSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
103 XEvent event;
104 gint x, y;
105 GdkWindow* w;
107 if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
108 return false;
111 event.xconfigure.type = ConfigureNotify;
112 event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
113 event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
115 /* The ICCCM says that synthetic events should have root relative
116 * coordinates. We still aren't really ICCCM compliant, since
117 * we don't send events when the real toplevel is moved.
119 gdk_error_trap_push ();
120 gdk_window_get_origin (w, &x, &y);
121 gdk_error_trap_pop ();
123 event.xconfigure.x = x;
124 event.xconfigure.y = y;
125 event.xconfigure.width = GTK_WIDGET(socket->gobj())->allocation.width;
126 event.xconfigure.height = GTK_WIDGET(socket->gobj())->allocation.height;
128 event.xconfigure.border_width = 0;
129 event.xconfigure.above = None;
130 event.xconfigure.override_redirect = False;
132 gdk_error_trap_push ();
133 XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
134 gdk_error_trap_pop ();
136 return false;
139 void
140 VSTPluginUI::create_preset_store ()
142 FST *fst = vst->fst();
143 int vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, NULL, 0.0f);
145 preset_model = ListStore::create (preset_columns);
147 for (int i = 0; i < fst->plugin->numPrograms; ++i) {
148 char buf[100];
149 TreeModel::Row row = *(preset_model->append());
151 snprintf (buf, 90, "preset %d", i);
153 if (vst_version >= 2) {
154 fst->plugin->dispatcher (fst->plugin, 29, i, 0, buf, 0.0);
157 row[preset_columns.name] = buf;
158 row[preset_columns.number] = i;
161 if (fst->plugin->numPrograms > 0) {
162 fst->plugin->dispatcher( fst->plugin, effSetProgram, 0, 0, NULL, 0.0 );
165 vst_preset_combo.set_model (preset_model);
167 CellRenderer* renderer = manage (new CellRendererText());
168 vst_preset_combo.pack_start (*renderer, true);
169 vst_preset_combo.add_attribute (*renderer, "text", 0);
171 if (vst->fst()->current_program != -1) {
172 vst_preset_combo.set_active (vst->fst()->current_program);
173 } else {
174 vst_preset_combo.set_active (0);
178 typedef int (*error_handler_t)( Display *, XErrorEvent *);
179 static Display *the_gtk_display;
180 static error_handler_t wine_error_handler;
181 static error_handler_t gtk_error_handler;
183 static int
184 fst_xerror_handler( Display *disp, XErrorEvent *ev )
186 if (disp == the_gtk_display) {
187 printf ("relaying error to gtk\n");
188 return gtk_error_handler (disp, ev);
189 } else {
190 printf( "relaying error to wine\n" );
191 return wine_error_handler (disp, ev);
195 void
196 gui_init (int *argc, char **argv[])
198 wine_error_handler = XSetErrorHandler (NULL);
199 gtk_init (argc, argv);
200 the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
201 gtk_error_handler = XSetErrorHandler( fst_xerror_handler );