part 2 of 3 of the 2.8 -> 3.0 merge
[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/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         create_preset_store ();
39
40         fst_run_editor (vst->fst());
41
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);
47
48         vst_preset_combo.signal_changed().connect (mem_fun (*this, &VSTPluginUI::preset_chosen));
49
50         bypass_button.set_active (!insert->active());
51         
52         pack_start (preset_box, false, false);
53         pack_start (socket, true, true);
54 }
55
56 VSTPluginUI::~VSTPluginUI ()
57 {
58         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
59         // and then our PluginUIWindow does the rest
60 }
61
62 void
63 VSTPluginUI::preset_chosen ()
64 {
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 ();
68 }
69
70 int
71 VSTPluginUI::get_preferred_height ()
72 {
73         return vst->fst()->height;
74 }
75
76 int
77 VSTPluginUI::get_preferred_width ()
78 {
79         return vst->fst()->width;
80 }
81
82 int
83 VSTPluginUI::package (Gtk::Window& win)
84 {
85         /* forward configure events to plugin window */
86
87         win.signal_configure_event().connect (bind (mem_fun (*this, &VSTPluginUI::configure_handler), &socket), false);
88
89         /*
90            this assumes that the window's owner understands the XEmbed protocol.
91         */
92         
93         socket.add_id (fst_get_XID (vst->fst()));
94
95         fst_move_window_into_view (vst->fst());
96
97         return 0;
98 }
99
100 bool
101 VSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
102 {
103         XEvent event;
104         gint x, y;
105         GdkWindow* w;
106
107         if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
108                 return false;
109         }
110
111         event.xconfigure.type = ConfigureNotify;
112         event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
113         event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
114
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.
118          */
119         gdk_error_trap_push ();
120         gdk_window_get_origin (w, &x, &y);
121         gdk_error_trap_pop ();
122
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;
127
128         event.xconfigure.border_width = 0;
129         event.xconfigure.above = None;
130         event.xconfigure.override_redirect = False;
131
132         gdk_error_trap_push ();
133         XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
134         gdk_error_trap_pop ();
135
136         return false;
137 }
138
139 void
140 VSTPluginUI::create_preset_store ()
141 {
142         FST *fst = vst->fst();
143         int vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, NULL, 0.0f);
144
145         preset_model = ListStore::create (preset_columns);
146
147         for (int i = 0; i < fst->plugin->numPrograms; ++i) {
148                 char buf[100];
149                 TreeModel::Row row = *(preset_model->append());
150
151                 snprintf (buf, 90, "preset %d", i);
152
153                 if (vst_version >= 2) {
154                         fst->plugin->dispatcher (fst->plugin, 29, i, 0, buf, 0.0);
155                 }
156                 
157                 row[preset_columns.name] = buf;
158                 row[preset_columns.number] = i;
159         }
160         
161         if (fst->plugin->numPrograms > 0) {
162                 fst->plugin->dispatcher( fst->plugin, effSetProgram, 0, 0, NULL, 0.0 );
163         }
164         
165         vst_preset_combo.set_model (preset_model);
166
167         CellRenderer* renderer = manage (new CellRendererText());
168         vst_preset_combo.pack_start (*renderer, true);
169         vst_preset_combo.add_attribute (*renderer, "text", 0);
170
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);
175         }
176 }
177
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;
182
183 static int 
184 fst_xerror_handler( Display *disp, XErrorEvent *ev )
185 {
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);
192         }
193 }
194
195 void
196 gui_init (int *argc, char **argv[])
197 {
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 );
202 }
203