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