2 * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
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.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <gdk/gdkquartz.h>
23 #include "gui_thread.h"
24 #include "ardour/plugin_insert.h"
25 #include "ardour/mac_vst_plugin.h"
26 #include "ardour/vst_types.h"
27 #include "mac_vst_plugin_ui.h"
32 using namespace ARDOUR;
43 @implementation ResizeNotificationObject
45 - (ResizeNotificationObject*) initWithPluginUI: (MacVSTPluginUI*) vstui
47 self = [ super init ];
52 - (void)viewResized:(NSNotification *)notification
54 (void) notification; // unused
55 plugin_ui->view_resized();
61 create_mac_vst_gui (boost::shared_ptr<PluginInsert> insert)
63 /* PluginUIWindow::create_mac_vst_editor assures this cast works */
64 boost::shared_ptr<MacVSTPlugin> mvst = boost::dynamic_pointer_cast<MacVSTPlugin> (insert->plugin());
65 return new MacVSTPluginUI (insert, mvst);
69 MacVSTPluginUI::MacVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vst)
70 : VSTPluginUI (pi, vst)
73 low_box.add_events (Gdk::VISIBILITY_NOTIFY_MASK | Gdk::EXPOSURE_MASK);
74 low_box.signal_realize().connect (mem_fun (this, &MacVSTPluginUI::lower_box_realized));
75 low_box.signal_visibility_notify_event ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_visibility_notify));
76 low_box.signal_size_request ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_size_request));
77 low_box.signal_size_allocate ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_size_allocate));
78 low_box.signal_map ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_map));
79 low_box.signal_unmap ().connect (mem_fun (this, &MacVSTPluginUI::lower_box_unmap));
81 pack_start (low_box, true, true);
84 vst->LoadPresetProgram.connect (_program_connection, invalidator (*this), boost::bind (&MacVSTPluginUI::set_program, this), gui_context());
86 _ns_view = [[NSView new] retain];
88 AEffect* plugin = _vst->state()->plugin;
89 plugin->dispatcher (plugin, effEditOpen, 0, 0, _ns_view, 0.0f);
90 _idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &MacVSTPluginUI::idle));
92 _resize_notifier = [[ResizeNotificationObject alloc] initWithPluginUI:this];
93 [[NSNotificationCenter defaultCenter] addObserver:_resize_notifier
94 selector:@selector(viewResized:) name:NSViewFrameDidChangeNotification
97 NSArray* subviews = [_ns_view subviews];
98 assert ([subviews count] < 2);
99 for (unsigned long i = 0; i < [subviews count]; ++i) {
100 NSView* subview = [subviews objectAtIndex:i];
101 [[NSNotificationCenter defaultCenter] addObserver:_resize_notifier
102 selector:@selector(viewResized:) name:NSViewFrameDidChangeNotification
107 MacVSTPluginUI::~MacVSTPluginUI ()
109 [[NSNotificationCenter defaultCenter] removeObserver:_resize_notifier];
110 [_resize_notifier release];
112 [_ns_view removeFromSuperview];
115 AEffect* plugin = _vst->state()->plugin;
116 plugin->dispatcher (plugin, effEditClose, 0, 0, 0, 0.0f);
117 _idle_connection.disconnect();
121 MacVSTPluginUI::get_nswindow ()
123 Gtk::Container* toplevel = get_toplevel();
124 if (!toplevel || !toplevel->is_toplevel()) {
125 error << _("MacVSTPluginUI: no top level window!") << endmsg;
128 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
131 error << _("MacVSTPluginUI: no top level window!") << endmsg;
139 MacVSTPluginUI::package (Gtk::Window& win)
141 VSTPluginUI::package (win);
146 MacVSTPluginUI::forward_key_event (GdkEventKey* ev)
148 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
150 if (_ns_view && nsevent) {
151 /* filter on nsevent type here because GDK massages FlagsChanged
152 * messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
153 * handle a FlagsChanged message as a keyDown or keyUp
155 if ([nsevent type] == NSKeyDown) {
156 [[[_ns_view window] firstResponder] keyDown:nsevent];
157 } else if ([nsevent type] == NSKeyUp) {
158 [[[_ns_view window] firstResponder] keyUp:nsevent];
159 } else if ([nsevent type] == NSFlagsChanged) {
160 [[[_ns_view window] firstResponder] flagsChanged:nsevent];
166 MacVSTPluginUI::lower_box_realized ()
168 NSWindow* win = get_nswindow ();
173 [win setAutodisplay:1]; // turn off GTK stuff for this window
175 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
176 [view addSubview:_ns_view];
177 low_box.queue_resize ();
181 MacVSTPluginUI::lower_box_visibility_notify (GdkEventVisibility* ev)
186 MacVSTPluginUI::lower_box_map ()
188 [_ns_view setHidden:0];
192 MacVSTPluginUI::lower_box_unmap ()
194 [_ns_view setHidden:1];
198 MacVSTPluginUI::lower_box_size_request (GtkRequisition* requisition)
200 struct ERect* er = NULL;
201 AEffect* plugin = _vst->state()->plugin;
202 plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
204 requisition->width = er->right - er->left;
205 requisition->height = er->bottom - er->top;
207 requisition->width = 600;
208 requisition->height = 400;
213 MacVSTPluginUI::lower_box_size_allocate (Gtk::Allocation& allocation)
216 gtk_widget_translate_coordinates(
217 GTK_WIDGET(low_box.gobj()),
218 GTK_WIDGET(low_box.get_parent()->gobj()),
220 [_ns_view setFrame:NSMakeRect (xx, yy, allocation.get_width (), allocation.get_height ())];
221 NSArray* subviews = [_ns_view subviews];
222 for (unsigned long i = 0; i < [subviews count]; ++i) {
223 NSView* subview = [subviews objectAtIndex:i];
224 [subview setFrame:NSMakeRect (0, 0, allocation.get_width (), allocation.get_height ())];
229 MacVSTPluginUI::view_resized ()
231 low_box.queue_resize ();
235 MacVSTPluginUI::get_XID ()
237 return _vst->state()->xid; // unused
241 MacVSTPluginUI::idle ()
243 AEffect* plugin = _vst->state()->plugin;
244 _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
245 return true; // _vst->state()->wantIdle;
249 MacVSTPluginUI::set_program ()
251 vststate_maybe_set_program (_vst->state());