Only allow LV2 touch events for control inputs
[ardour.git] / gtk2_ardour / mac_vst_plugin_ui.mm
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 <gdk/gdkquartz.h>
23
24 #include "gui_thread.h"
25 #include "ardour/plugin_insert.h"
26 #include "ardour/mac_vst_plugin.h"
27 #include "ardour/vst_types.h"
28 #include "mac_vst_plugin_ui.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace Gtk;
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 struct ERect{
37     short top;
38     short left;
39     short bottom;
40     short right;
41 };
42
43
44 @implementation ResizeNotificationObject
45
46 - (ResizeNotificationObject*) initWithPluginUI: (MacVSTPluginUI*) vstui
47 {
48         self = [ super init ];
49         plugin_ui = vstui;
50         return self;
51 }
52
53 - (void)viewResized:(NSNotification *)notification
54 {
55         (void) notification; // unused
56         plugin_ui->view_resized();
57 }
58
59 @end
60
61 VSTPluginUI*
62 create_mac_vst_gui (boost::shared_ptr<PluginInsert> insert)
63 {
64         /* PluginUIWindow::create_mac_vst_editor assures this cast works */
65         boost::shared_ptr<MacVSTPlugin> mvst =  boost::dynamic_pointer_cast<MacVSTPlugin> (insert->plugin());
66         return new MacVSTPluginUI (insert, mvst);
67 }
68
69
70 MacVSTPluginUI::MacVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vst)
71         : VSTPluginUI (pi, vst)
72         , _ns_view (0)
73 {
74         low_box.add_events (Gdk::VISIBILITY_NOTIFY_MASK | Gdk::EXPOSURE_MASK);
75         low_box.signal_realize().connect (mem_fun (this, &MacVSTPluginUI::lower_box_realized));
76         low_box.signal_visibility_notify_event ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_visibility_notify));
77         low_box.signal_size_request ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_size_request));
78         low_box.signal_size_allocate ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_size_allocate));
79         low_box.signal_map ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_map));
80         low_box.signal_unmap ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_unmap));
81
82         pack_start (low_box, true, true);
83         low_box.show ();
84
85         vst->LoadPresetProgram.connect (_program_connection, invalidator (*this), boost::bind (&MacVSTPluginUI::set_program, this), gui_context());
86
87         _ns_view = [[NSView new] retain];
88
89         AEffect* plugin = _vst->state()->plugin;
90         plugin->dispatcher (plugin, effEditOpen, 0, 0, _ns_view, 0.0f);
91         _idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &MacVSTPluginUI::idle));
92
93         _resize_notifier = [[ResizeNotificationObject alloc] initWithPluginUI:this];
94         [[NSNotificationCenter defaultCenter] addObserver:_resize_notifier
95                 selector:@selector(viewResized:) name:NSViewFrameDidChangeNotification
96                 object:_ns_view];
97
98         NSArray* subviews = [_ns_view subviews];
99         assert ([subviews count] < 2);
100         for (unsigned long i = 0; i < [subviews count]; ++i) {
101                 NSView* subview = [subviews objectAtIndex:i];
102                 [[NSNotificationCenter defaultCenter] addObserver:_resize_notifier
103                         selector:@selector(viewResized:) name:NSViewFrameDidChangeNotification
104                         object:subview];
105         }
106 }
107
108 MacVSTPluginUI::~MacVSTPluginUI ()
109 {
110         [[NSNotificationCenter defaultCenter] removeObserver:_resize_notifier];
111         [_resize_notifier release];
112
113         [_ns_view removeFromSuperview];
114         [_ns_view release];
115
116         AEffect* plugin = _vst->state()->plugin;
117         plugin->dispatcher (plugin, effEditClose, 0, 0, 0, 0.0f);
118         _idle_connection.disconnect();
119 }
120
121 NSWindow*
122 MacVSTPluginUI::get_nswindow ()
123 {
124         Gtk::Container* toplevel = get_toplevel();
125         if (!toplevel || !toplevel->is_toplevel()) {
126                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
127                 return 0;
128         }
129         NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
130
131         if (!true_parent) {
132                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
133                 return 0;
134         }
135
136         return true_parent;
137 }
138
139 int
140 MacVSTPluginUI::package (Gtk::Window& win)
141 {
142         VSTPluginUI::package (win);
143         return 0;
144 }
145
146 void
147 MacVSTPluginUI::forward_key_event (GdkEventKey* ev)
148 {
149         NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
150
151         if (_ns_view && nsevent) {
152                 /* filter on nsevent type here because GDK massages FlagsChanged
153                  * messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
154                  * handle a FlagsChanged message as a keyDown or keyUp
155                  */
156                 if ([nsevent type] == NSKeyDown) {
157                         [[[_ns_view window] firstResponder] keyDown:nsevent];
158                 } else if ([nsevent type] == NSKeyUp) {
159                         [[[_ns_view window] firstResponder] keyUp:nsevent];
160                 } else if ([nsevent type] == NSFlagsChanged) {
161                         [[[_ns_view window] firstResponder] flagsChanged:nsevent];
162                 }
163         }
164 }
165
166 void
167 MacVSTPluginUI::lower_box_realized ()
168 {
169         NSWindow* win = get_nswindow ();
170         if (!win) {
171                 return;
172         }
173
174         [win setAutodisplay:1]; // turn off GTK stuff for this window
175
176         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
177         [view addSubview:_ns_view];
178         low_box.queue_resize ();
179 }
180
181 bool
182 MacVSTPluginUI::lower_box_visibility_notify (GdkEventVisibility* ev)
183 {
184         return false;
185 }
186 void
187 MacVSTPluginUI::lower_box_map ()
188 {
189         [_ns_view setHidden:0];
190 }
191
192 void
193 MacVSTPluginUI::lower_box_unmap ()
194 {
195         [_ns_view setHidden:1];
196 }
197
198 void
199 MacVSTPluginUI::lower_box_size_request (GtkRequisition* requisition)
200 {
201         struct ERect* er = NULL;
202         AEffect* plugin = _vst->state()->plugin;
203         plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
204         if (er) {
205                 requisition->width  = er->right - er->left;
206                 requisition->height = er->bottom - er->top;
207         } else {
208                 requisition->width  = 600;
209                 requisition->height = 400;
210         }
211 }
212
213 void
214 MacVSTPluginUI::lower_box_size_allocate (Gtk::Allocation& allocation)
215 {
216         gint xx, yy;
217         gtk_widget_translate_coordinates(
218                         GTK_WIDGET(low_box.gobj()),
219                         GTK_WIDGET(low_box.get_parent()->gobj()),
220                         8, 6, &xx, &yy);
221         [_ns_view setFrame:NSMakeRect (xx, yy, allocation.get_width (), allocation.get_height ())];
222         NSArray* subviews = [_ns_view subviews];
223         for (unsigned long i = 0; i < [subviews count]; ++i) {
224                 NSView* subview = [subviews objectAtIndex:i];
225                 [subview setFrame:NSMakeRect (0, 0, allocation.get_width (), allocation.get_height ())];
226         }
227 }
228
229 void
230 MacVSTPluginUI::view_resized ()
231 {
232         low_box.queue_resize ();
233 }
234
235 int
236 MacVSTPluginUI::get_XID ()
237 {
238         return _vst->state()->xid; // unused
239 }
240
241 bool
242 MacVSTPluginUI::idle ()
243 {
244         AEffect* plugin = _vst->state()->plugin;
245         _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
246         return true; // _vst->state()->wantIdle;
247 }
248
249 void
250 MacVSTPluginUI::set_program ()
251 {
252         vststate_maybe_set_program (_vst->state());
253 }