remove long-lived bug that tried to make a non-existent action insensitive
[ardour.git] / gtk2_ardour / vst_plugin_ui.cc
1 /*
2     Copyright (C) 2000-2010 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 "ardour/vst_plugin.h"
22 #include "ardour/vst_types.h"
23 #include "ardour/plugin_insert.h"
24 #include "vst_plugin_ui.h"
25
26 #ifdef PLATFORM_WINDOWS
27 #include <gdk/gdkwin32.h>
28 #elif defined __APPLE__
29 // TODO
30 #else
31 #include <gdk/gdkx.h>
32 #endif
33
34 VSTPluginUI::VSTPluginUI (boost::shared_ptr<ARDOUR::PluginInsert> insert, boost::shared_ptr<ARDOUR::VSTPlugin> plugin)
35         : PlugUIBase (insert)
36         , _vst (plugin)
37 {
38         Gtk::HBox* box = manage (new Gtk::HBox);
39         box->set_spacing (6);
40         box->set_border_width (6);
41         box->pack_end (focus_button, false, false);
42         box->pack_end (bypass_button, false, false, 4);
43         if (insert->controls().size() > 0) {
44                 box->pack_end (reset_button, false, false, 4);
45         }
46         box->pack_end (delete_button, false, false);
47         box->pack_end (save_button, false, false);
48         box->pack_end (add_button, false, false);
49         box->pack_end (_preset_combo, false, false);
50         box->pack_end (_preset_modified, false, false);
51         box->pack_end (pin_management_button, false, false);
52
53         bypass_button.set_active (!insert->active ());
54
55         pack_start (*box, false, false);
56         box->signal_size_allocate().connect (sigc::mem_fun (*this, &VSTPluginUI::top_box_allocated));
57 #ifdef GDK_WINDOWING_X11
58         pack_start (_socket, true, true);
59         _socket.set_border_width (0);
60 #endif
61 }
62
63 VSTPluginUI::~VSTPluginUI ()
64 {
65
66 }
67
68 void
69 VSTPluginUI::preset_selected (ARDOUR::Plugin::PresetRecord preset)
70 {
71 #ifdef GDK_WINDOWING_X11
72         _socket.grab_focus ();
73 #endif
74         PlugUIBase::preset_selected (preset);
75 }
76
77 int
78 VSTPluginUI::get_preferred_height ()
79 {
80         return _vst->state()->height + _vst->state()->voffset;
81 }
82
83 int
84 VSTPluginUI::get_preferred_width ()
85 {
86         return _vst->state()->width + _vst->state()->hoffset;
87 }
88
89 int
90 VSTPluginUI::package (Gtk::Window& win)
91 {
92 #ifdef GDK_WINDOWING_X11
93         /* Forward configure events to plugin window */
94         win.signal_configure_event().connect (sigc::mem_fun (*this, &VSTPluginUI::configure_handler), false);
95
96         /* This assumes that the window's owner understands the XEmbed protocol */
97         _socket.add_id (get_XID ());
98         _socket.set_size_request(
99                         _vst->state()->width + _vst->state()->hoffset,
100                         _vst->state()->height + _vst->state()->voffset);
101 #endif
102
103         return 0;
104 }
105
106 bool
107 VSTPluginUI::on_window_show(const std::string& title)
108 {
109         _vst->state()->gui_shown = 1;
110         return PlugUIBase::on_window_show(title);
111 }
112
113 void
114 VSTPluginUI::on_window_hide()
115 {
116         _vst->state()->gui_shown = 0;
117         PlugUIBase::on_window_hide();
118 }
119
120
121 bool
122 VSTPluginUI::configure_handler (GdkEventConfigure*)
123 {
124 #ifdef GDK_WINDOWING_X11
125         XEvent event;
126         gint x, y;
127         GdkWindow* w;
128         Window xw = _vst->state()->linux_plugin_ui_window;
129
130         if ((w = _socket.gobj()->plug_window) == 0) {
131                 return false;
132         }
133
134         event.xconfigure.type = ConfigureNotify;
135         event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
136         event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
137
138         /* The ICCCM says that synthetic events should have root relative
139          * coordinates. We still aren't really ICCCM compliant, since
140          * we don't send events when the real toplevel is moved.
141          */
142         gdk_error_trap_push ();
143         gdk_window_get_origin (w, &x, &y);
144         gdk_error_trap_pop ();
145
146         event.xconfigure.x = x;
147         event.xconfigure.y = y;
148         event.xconfigure.width = GTK_WIDGET (_socket.gobj())->allocation.width;
149         event.xconfigure.height = GTK_WIDGET (_socket.gobj())->allocation.height;
150
151         event.xconfigure.border_width = 0;
152         event.xconfigure.above = None;
153         event.xconfigure.override_redirect = False;
154
155         gdk_error_trap_push ();
156         XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
157         /* if the plugin does adds itself to the parent,
158          * but ardour re-parents it, we have a pointer to
159          * the socket's child and need to resize the
160          * child window (e.g. JUCE, u-he)
161          */
162         if (xw) {
163                 XMoveResizeWindow (GDK_WINDOW_XDISPLAY (w), xw,
164                                 0, 0, _vst->state()->width, _vst->state()->height);
165                 XMapRaised (GDK_WINDOW_XDISPLAY (w), xw);
166                 XFlush (GDK_WINDOW_XDISPLAY (w));
167         }
168         gdk_error_trap_pop ();
169 #endif
170
171         return false;
172 }
173
174 bool
175 VSTPluginUI::dispatch_effeditkey (GdkEventKey* gdk_key)
176 {
177         int effopcode;
178         switch (gdk_key->type) {
179                 case GDK_KEY_PRESS:
180                         effopcode = 59; // effEditKeyDown
181                         break;
182                 case GDK_KEY_RELEASE:
183                         effopcode = 60; // effEditKeyUp
184                         break;
185                 default:
186                         return false;
187         }
188
189         /* see https://github.com/DISTRHO/DPF/blob/master/distrho/src/DistrhoPluginVST.cpp */
190         int special_key = 0;
191         int ascii_key = 0;
192
193         switch (gdk_key->keyval) {
194                 case GDK_BackSpace:
195                         special_key = 1;
196                         break;
197                 case GDK_Tab:
198                 case GDK_KP_Tab:
199                         special_key = 2;
200                         break;
201                 case GDK_Return:
202                 case GDK_KP_Enter:
203                         special_key = 4;
204                         break;
205                 case GDK_Escape:
206                         special_key = 6;
207                         break;
208                 case GDK_KP_Space:
209                         special_key = 7;
210                         break;
211
212                 case GDK_End:
213                 case GDK_KP_End:
214                         special_key = 9;
215                         break;
216                 case GDK_Home:
217                 case GDK_KP_Home:
218                         special_key = 10;
219                         break;
220                 case GDK_Left:
221                         special_key = 11;
222                         break;
223                 case GDK_Up:
224                         special_key = 12;
225                         break;
226                 case GDK_Right:
227                         special_key = 13;
228                         break;
229                 case GDK_Down:
230                         special_key = 14;
231                         break;
232                 case GDK_Page_Up:
233                 case GDK_KP_Page_Up:
234                         special_key = 15;
235                         break;
236                 case GDK_Page_Down:
237                 case GDK_KP_Page_Down:
238                         special_key = 16;
239                 case GDK_Insert:
240                         special_key = 21;
241                         break;
242                 case GDK_Delete:
243                 case GDK_KP_Delete:
244                         special_key = 22;
245                         break;
246
247                 case GDK_Shift_L:
248                 case GDK_Shift_R:
249                         special_key = 54;
250                         break;
251                 case GDK_Control_L:
252                 case GDK_Control_R:
253                         special_key = 55;
254                         break;
255                 case GDK_Alt_L:
256                 case GDK_Alt_R:
257                         special_key = 56;
258                         break;
259
260                 case GDK_F1:  special_key = 40; break;
261                 case GDK_F2:  special_key = 41; break;
262                 case GDK_F3:  special_key = 42; break;
263                 case GDK_F4:  special_key = 43; break;
264                 case GDK_F5:  special_key = 44; break;
265                 case GDK_F6:  special_key = 45; break;
266                 case GDK_F7:  special_key = 46; break;
267                 case GDK_F8:  special_key = 47; break;
268                 case GDK_F9:  special_key = 48; break;
269                 case GDK_F10: special_key = 49; break;
270                 case GDK_F11: special_key = 50; break;
271                 case GDK_F12: special_key = 51; break;
272
273                 default:
274                         ascii_key = gdk_key->keyval;
275                         break;
276         }
277
278         if (special_key > 0 || ascii_key > 0) {
279                 VSTState* vstfx = _vst->state();
280                 /* expect non-zero return if key was handled */
281                 return 0 != vstfx->plugin->dispatcher (vstfx->plugin, effopcode, (int)ascii_key, (intptr_t)special_key, NULL, 0);
282         }
283         return false;
284 }