2 Copyright (C) 2004 Paul Davis
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
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gdk/gdkquartz.h>
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"
33 using namespace ARDOUR;
44 @implementation ResizeNotificationObject
46 - (ResizeNotificationObject*) initWithPluginUI: (MacVSTPluginUI*) vstui
48 self = [ super init ];
53 - (void)viewResized:(NSNotification *)notification
55 (void) notification; // unused
56 plugin_ui->view_resized();
62 create_mac_vst_gui (boost::shared_ptr<PluginInsert> insert)
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);
70 MacVSTPluginUI::MacVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vst)
71 : VSTPluginUI (pi, vst)
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));
82 pack_start (low_box, true, true);
85 vst->LoadPresetProgram.connect (_program_connection, invalidator (*this), boost::bind (&MacVSTPluginUI::set_program, this), gui_context());
87 _ns_view = [[NSView new] retain];
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));
93 _resize_notifier = [[ResizeNotificationObject alloc] initWithPluginUI:this];
94 [[NSNotificationCenter defaultCenter] addObserver:_resize_notifier
95 selector:@selector(viewResized:) name:NSViewFrameDidChangeNotification
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
108 MacVSTPluginUI::~MacVSTPluginUI ()
110 [[NSNotificationCenter defaultCenter] removeObserver:_resize_notifier];
111 [_resize_notifier release];
113 [_ns_view removeFromSuperview];
116 AEffect* plugin = _vst->state()->plugin;
117 plugin->dispatcher (plugin, effEditClose, 0, 0, 0, 0.0f);
118 _idle_connection.disconnect();
122 MacVSTPluginUI::get_nswindow ()
124 Gtk::Container* toplevel = get_toplevel();
125 if (!toplevel || !toplevel->is_toplevel()) {
126 error << _("MacVSTPluginUI: no top level window!") << endmsg;
129 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
132 error << _("MacVSTPluginUI: no top level window!") << endmsg;
140 MacVSTPluginUI::package (Gtk::Window& win)
142 VSTPluginUI::package (win);
147 MacVSTPluginUI::forward_key_event (GdkEventKey* ev)
149 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
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
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];
167 MacVSTPluginUI::lower_box_realized ()
169 NSWindow* win = get_nswindow ();
174 [win setAutodisplay:1]; // turn off GTK stuff for this window
176 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
177 [view addSubview:_ns_view];
178 low_box.queue_resize ();
182 MacVSTPluginUI::lower_box_visibility_notify (GdkEventVisibility* ev)
187 MacVSTPluginUI::lower_box_map ()
189 [_ns_view setHidden:0];
193 MacVSTPluginUI::lower_box_unmap ()
195 [_ns_view setHidden:1];
199 MacVSTPluginUI::lower_box_size_request (GtkRequisition* requisition)
201 struct ERect* er = NULL;
202 AEffect* plugin = _vst->state()->plugin;
203 plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
205 requisition->width = er->right - er->left;
206 requisition->height = er->bottom - er->top;
208 requisition->width = 600;
209 requisition->height = 400;
214 MacVSTPluginUI::lower_box_size_allocate (Gtk::Allocation& allocation)
217 gtk_widget_translate_coordinates(
218 GTK_WIDGET(low_box.gobj()),
219 GTK_WIDGET(low_box.get_parent()->gobj()),
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 ())];
230 MacVSTPluginUI::view_resized ()
232 low_box.queue_resize ();
236 MacVSTPluginUI::get_XID ()
238 return _vst->state()->xid; // unused
242 MacVSTPluginUI::idle ()
244 AEffect* plugin = _vst->state()->plugin;
245 _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
246 return true; // _vst->state()->wantIdle;
250 MacVSTPluginUI::set_program ()
252 vststate_maybe_set_program (_vst->state());