Improvements to the plugin eq gui:
[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/processor.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         fst_run_editor (vst->fst());
39
40         preset_box.pack_end (bypass_button, false, false, 10);
41         preset_box.pack_end (save_button, false, false);
42         preset_box.pack_end (preset_combo, false, false);
43         preset_box.pack_end (eqgui_toggle, false, false);
44
45
46         bypass_button.set_active (!insert->active());
47         
48         pack_start (preset_box, false, false);
49         pack_start (socket, true, true);
50         pack_start (plugin_eq_bin, true, true);
51 }
52
53 VSTPluginUI::~VSTPluginUI ()
54 {
55         // nothing to do here - plugin destructor destroys the GUI
56 }
57
58 int
59 VSTPluginUI::get_preferred_height ()
60 {
61         return vst->fst()->height;
62 }
63
64 int
65 VSTPluginUI::get_preferred_width ()
66 {
67         return vst->fst()->width;
68 }
69
70 int
71 VSTPluginUI::package (Gtk::Window& win)
72 {
73         /* forward configure events to plugin window */
74
75         win.signal_configure_event().connect (bind (mem_fun (*this, &VSTPluginUI::configure_handler), &socket), false);
76
77         /*
78            this assumes that the window's owner understands the XEmbed protocol.
79         */
80         
81         socket.add_id (fst_get_XID (vst->fst()));
82
83         fst_move_window_into_view (vst->fst());
84
85         return 0;
86 }
87
88 bool
89 VSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
90 {
91         XEvent event;
92         gint x, y;
93         GdkWindow* w;
94
95         if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
96                 return false;
97         }
98
99         event.xconfigure.type = ConfigureNotify;
100         event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
101         event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
102
103         /* The ICCCM says that synthetic events should have root relative
104          * coordinates. We still aren't really ICCCM compliant, since
105          * we don't send events when the real toplevel is moved.
106          */
107         gdk_error_trap_push ();
108         gdk_window_get_origin (w, &x, &y);
109         gdk_error_trap_pop ();
110
111         event.xconfigure.x = x;
112         event.xconfigure.y = y;
113         event.xconfigure.width = GTK_WIDGET(socket->gobj())->allocation.width;
114         event.xconfigure.height = GTK_WIDGET(socket->gobj())->allocation.height;
115
116         event.xconfigure.border_width = 0;
117         event.xconfigure.above = None;
118         event.xconfigure.override_redirect = False;
119
120         gdk_error_trap_push ();
121         XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
122         gdk_error_trap_pop ();
123
124         return false;
125 }
126
127 typedef int (*error_handler_t)( Display *, XErrorEvent *);
128 static Display *the_gtk_display;
129 static error_handler_t wine_error_handler;
130 static error_handler_t gtk_error_handler;
131
132 static int
133 fst_xerror_handler( Display *disp, XErrorEvent *ev )
134 {
135        if (disp == the_gtk_display) {
136                printf ("relaying error to gtk\n");
137                return gtk_error_handler (disp, ev);
138        } else {
139                printf( "relaying error to wine\n" );
140                return wine_error_handler (disp, ev);
141        }
142 }
143
144 void
145 gui_init (int *argc, char **argv[])
146 {
147        wine_error_handler = XSetErrorHandler (NULL);
148        gtk_init (argc, argv);
149        the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
150        gtk_error_handler = XSetErrorHandler( fst_xerror_handler );
151 }