Only show user-presets in favorite sidebar
[ardour.git] / gtk2_ardour / windows_vst_plugin_ui.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 <gtkmm.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtksocket.h>
23 #include "gtkmm2ext/gui_thread.h"
24 #include "fst.h"
25 #include "ardour/plugin_insert.h"
26 #include "ardour/windows_vst_plugin.h"
27
28 #include "windows_vst_plugin_ui.h"
29
30 #ifdef PLATFORM_WINDOWS
31 #include <gdk/gdkwin32.h>
32 #elif defined __APPLE__
33 // TODO
34 #else
35 #include <gdk/gdkx.h>
36 #endif
37
38 using namespace Gtk;
39 using namespace ARDOUR;
40 using namespace PBD;
41
42 WindowsVSTPluginUI::WindowsVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vp, GtkWidget *parent)
43         : VSTPluginUI (pi, vp)
44 {
45
46 #ifdef GDK_WINDOWING_WIN32
47         gtk_widget_realize(parent);
48         void* hWndHost = gdk_win32_drawable_get_handle(parent->window);
49
50         fst_run_editor (_vst->state(), hWndHost);
51         // TODO pack a placeholder (compare to VSTPluginUI::VSTPluginUI X11 socket)
52         // have placeholder use VSTPluginUI::get_preferred_height(), width()
53         // TODO pack plugin_analysis_expander at the bottom.
54 #else
55         fst_run_editor (_vst->state(), NULL);
56         pack_start (plugin_analysis_expander, true, true);
57 #endif
58 }
59
60 WindowsVSTPluginUI::~WindowsVSTPluginUI ()
61 {
62         fst_destroy_editor (_vst->state());
63
64         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
65         // and then our PluginUIWindow does the rest
66 }
67
68 void
69 WindowsVSTPluginUI::top_box_allocated (Gtk::Allocation& a)
70 {
71         int h = a.get_height() + 12; // 2 * 6px spacing
72         if (_vst->state()->voffset != h) {
73 #ifndef NDEBUG
74                 printf("WindowsVSTPluginUI:: update voffset to %d px\n", h);
75 #endif
76                 _vst->state()->voffset = h;
77                 resize_callback ();
78         }
79 }
80
81 void
82 WindowsVSTPluginUI::resize_callback ()
83 {
84         void* gtk_parent_window = _vst->state()->gtk_window_parent;
85         if (gtk_parent_window) {
86                 int width  = _vst->state()->width + _vst->state()->hoffset;
87                 int height = _vst->state()->height + _vst->state()->voffset;
88 #ifndef NDEBUG
89                 printf ("WindowsVSTPluginUI::resize_callback %d x %d\n", width, height);
90 #endif
91                 set_size_request (width, height);
92                 ((Gtk::Window*) gtk_parent_window)->set_size_request (width, height);
93                 ((Gtk::Window*) gtk_parent_window)->resize (width, height);
94                 fst_move_window_into_view (_vst->state ());
95         }
96 }
97
98 int
99 WindowsVSTPluginUI::package (Gtk::Window& win)
100 {
101 #ifndef NDEBUG
102         printf ("WindowsVSTPluginUI::package\n");
103 #endif
104         VSTPluginUI::package (win);
105         _vst->state()->gtk_window_parent = (void*) (&win);
106
107         _vst->VSTSizeWindow.connect (_resize_connection, invalidator (*this), boost::bind (&WindowsVSTPluginUI::resize_callback, this), gui_context());
108
109         resize_callback ();
110
111         return 0;
112 }
113
114 void
115 WindowsVSTPluginUI::forward_key_event (GdkEventKey* ev)
116 {
117         if (dispatch_effeditkey (ev)) {
118                 return;
119         }
120 #ifndef PLATFORM_WINDOWS /* linux + wine ; libs/fst/vstwin.c */
121         if (ev->type != GDK_KEY_PRESS) {
122                 return;
123         }
124
125         VSTState* fst = _vst->state ();
126         pthread_mutex_lock (&fst->lock);
127
128         if (fst->n_pending_keys == (sizeof (fst->pending_keys) * sizeof (VSTKey))) {
129                 /* buffer full */
130                 return;
131         }
132
133         int special_windows_key = 0;
134         int character_windows_key = 0;
135
136         switch (ev->keyval) {
137         case GDK_Left:
138                 special_windows_key = 0x25;
139                 break;
140         case GDK_Right:
141                 special_windows_key = 0x27;
142                 break;
143         case GDK_Up:
144                 special_windows_key = 0x26;
145                 break;
146         case GDK_Down:
147                 special_windows_key = 0x28;
148                 break;
149         case GDK_Return:
150         case GDK_KP_Enter:
151                 special_windows_key = 0xd;
152                 break;
153         default:
154                 character_windows_key = ev->keyval;
155                 break;
156         }
157
158         fst->pending_keys[fst->n_pending_keys].special = special_windows_key;
159         fst->pending_keys[fst->n_pending_keys].character = character_windows_key;
160         fst->n_pending_keys++;
161
162         pthread_mutex_unlock (&fst->lock);
163 #endif
164 }
165
166 int
167 WindowsVSTPluginUI::get_XID ()
168 {
169         return _vst->state()->xid;
170 }
171
172 #ifdef GDK_WINDOWING_X11
173 typedef int (*error_handler_t)( Display *, XErrorEvent *);
174 static Display *the_gtk_display;
175 static error_handler_t wine_error_handler;
176 static error_handler_t gtk_error_handler;
177
178 static int
179 fst_xerror_handler (Display* disp, XErrorEvent* ev)
180 {
181         if (disp == the_gtk_display) {
182                 printf ("relaying error to gtk\n");
183                 return gtk_error_handler (disp, ev);
184         } else {
185                 printf( "relaying error to wine\n" );
186                 return wine_error_handler (disp, ev);
187         }
188 }
189 #endif
190
191 void
192 windows_vst_gui_init (int *argc, char **argv[])
193 {
194         gtk_init (argc, argv);
195
196 #ifdef GDK_WINDOWING_X11
197         wine_error_handler = XSetErrorHandler (NULL);
198         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
199         gtk_error_handler = XSetErrorHandler (fst_xerror_handler);
200 #endif
201 }
202