mp4chaps Lua script: don't clutter global environment
[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 }
150
151 void
152 MacVSTPluginUI::lower_box_realized ()
153 {
154         NSWindow* win = get_nswindow ();
155         if (!win) {
156                 return;
157         }
158
159         [win setAutodisplay:1]; // turn off GTK stuff for this window
160
161         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
162         [view addSubview:_ns_view];
163         low_box.queue_resize ();
164 }
165
166 bool
167 MacVSTPluginUI::lower_box_visibility_notify (GdkEventVisibility* ev)
168 {
169         return false;
170 }
171 void
172 MacVSTPluginUI::lower_box_map ()
173 {
174         [_ns_view setHidden:0];
175 }
176
177 void
178 MacVSTPluginUI::lower_box_unmap ()
179 {
180         [_ns_view setHidden:1];
181 }
182
183 void
184 MacVSTPluginUI::lower_box_size_request (GtkRequisition* requisition)
185 {
186         struct ERect* er = NULL;
187         AEffect* plugin = _vst->state()->plugin;
188         plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
189         if (er) {
190                 requisition->width  = er->right - er->left;
191                 requisition->height = er->bottom - er->top;
192         } else {
193                 requisition->width  = 600;
194                 requisition->height = 400;
195         }
196 }
197
198 void
199 MacVSTPluginUI::lower_box_size_allocate (Gtk::Allocation& allocation)
200 {
201         gint xx, yy;
202         gtk_widget_translate_coordinates(
203                         GTK_WIDGET(low_box.gobj()),
204                         GTK_WIDGET(low_box.get_parent()->gobj()),
205                         8, 6, &xx, &yy);
206         [_ns_view setFrame:NSMakeRect (xx, yy, allocation.get_width (), allocation.get_height ())];
207         NSArray* subviews = [_ns_view subviews];
208         for (unsigned long i = 0; i < [subviews count]; ++i) {
209                 NSView* subview = [subviews objectAtIndex:i];
210                 [subview setFrame:NSMakeRect (0, 0, allocation.get_width (), allocation.get_height ())];
211         }
212 }
213
214 void
215 MacVSTPluginUI::view_resized ()
216 {
217         low_box.queue_resize ();
218 }
219
220 int
221 MacVSTPluginUI::get_XID ()
222 {
223         return _vst->state()->xid; // unused
224 }
225
226 bool
227 MacVSTPluginUI::idle ()
228 {
229         AEffect* plugin = _vst->state()->plugin;
230         _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
231         return true; // _vst->state()->wantIdle;
232 }
233
234 void
235 MacVSTPluginUI::set_program ()
236 {
237         vststate_maybe_set_program (_vst->state());
238 }