Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / mac_vst_plugin_ui.mm
1 /*
2  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
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 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.
17  */
18
19 #include <gtkmm.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkquartz.h>
22
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"
28
29 #include "pbd/i18n.h"
30
31 using namespace Gtk;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 struct ERect{
36     short top;
37     short left;
38     short bottom;
39     short right;
40 };
41
42
43 @implementation ResizeNotificationObject
44
45 - (ResizeNotificationObject*) initWithPluginUI: (MacVSTPluginUI*) vstui
46 {
47         self = [ super init ];
48         plugin_ui = vstui;
49         return self;
50 }
51
52 - (void)viewResized:(NSNotification *)notification
53 {
54         (void) notification; // unused
55         plugin_ui->view_resized();
56 }
57
58 @end
59
60 VSTPluginUI*
61 create_mac_vst_gui (boost::shared_ptr<PluginInsert> insert)
62 {
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);
66 }
67
68
69 MacVSTPluginUI::MacVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vst)
70         : VSTPluginUI (pi, vst)
71         , _ns_view (0)
72 {
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));
80
81         pack_start (low_box, true, true);
82         low_box.show ();
83
84         vst->LoadPresetProgram.connect (_program_connection, invalidator (*this), boost::bind (&MacVSTPluginUI::set_program, this), gui_context());
85
86         _ns_view = [[NSView new] retain];
87
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));
91
92         _resize_notifier = [[ResizeNotificationObject alloc] initWithPluginUI:this];
93         [[NSNotificationCenter defaultCenter] addObserver:_resize_notifier
94                 selector:@selector(viewResized:) name:NSViewFrameDidChangeNotification
95                 object:_ns_view];
96
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
103                         object:subview];
104         }
105 }
106
107 MacVSTPluginUI::~MacVSTPluginUI ()
108 {
109         [[NSNotificationCenter defaultCenter] removeObserver:_resize_notifier];
110         [_resize_notifier release];
111
112         [_ns_view removeFromSuperview];
113         [_ns_view release];
114
115         AEffect* plugin = _vst->state()->plugin;
116         plugin->dispatcher (plugin, effEditClose, 0, 0, 0, 0.0f);
117         _idle_connection.disconnect();
118 }
119
120 NSWindow*
121 MacVSTPluginUI::get_nswindow ()
122 {
123         Gtk::Container* toplevel = get_toplevel();
124         if (!toplevel || !toplevel->is_toplevel()) {
125                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
126                 return 0;
127         }
128         NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
129
130         if (!true_parent) {
131                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
132                 return 0;
133         }
134
135         return true_parent;
136 }
137
138 int
139 MacVSTPluginUI::package (Gtk::Window& win)
140 {
141         VSTPluginUI::package (win);
142         return 0;
143 }
144
145 void
146 MacVSTPluginUI::forward_key_event (GdkEventKey* ev)
147 {
148         NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
149
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
154                  */
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];
161                 }
162         }
163 }
164
165 void
166 MacVSTPluginUI::lower_box_realized ()
167 {
168         NSWindow* win = get_nswindow ();
169         if (!win) {
170                 return;
171         }
172
173         [win setAutodisplay:1]; // turn off GTK stuff for this window
174
175         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
176         [view addSubview:_ns_view];
177         low_box.queue_resize ();
178 }
179
180 bool
181 MacVSTPluginUI::lower_box_visibility_notify (GdkEventVisibility* ev)
182 {
183         return false;
184 }
185 void
186 MacVSTPluginUI::lower_box_map ()
187 {
188         [_ns_view setHidden:0];
189 }
190
191 void
192 MacVSTPluginUI::lower_box_unmap ()
193 {
194         [_ns_view setHidden:1];
195 }
196
197 void
198 MacVSTPluginUI::lower_box_size_request (GtkRequisition* requisition)
199 {
200         struct ERect* er = NULL;
201         AEffect* plugin = _vst->state()->plugin;
202         plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
203         if (er) {
204                 requisition->width  = er->right - er->left;
205                 requisition->height = er->bottom - er->top;
206         } else {
207                 requisition->width  = 600;
208                 requisition->height = 400;
209         }
210 }
211
212 void
213 MacVSTPluginUI::lower_box_size_allocate (Gtk::Allocation& allocation)
214 {
215         gint xx, yy;
216         gtk_widget_translate_coordinates(
217                         GTK_WIDGET(low_box.gobj()),
218                         GTK_WIDGET(low_box.get_parent()->gobj()),
219                         8, 6, &xx, &yy);
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 ())];
225         }
226 }
227
228 void
229 MacVSTPluginUI::view_resized ()
230 {
231         low_box.queue_resize ();
232 }
233
234 int
235 MacVSTPluginUI::get_XID ()
236 {
237         return _vst->state()->xid; // unused
238 }
239
240 bool
241 MacVSTPluginUI::idle ()
242 {
243         AEffect* plugin = _vst->state()->plugin;
244         _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
245         return true; // _vst->state()->wantIdle;
246 }
247
248 void
249 MacVSTPluginUI::set_program ()
250 {
251         vststate_maybe_set_program (_vst->state());
252 }