fix MacVST UI idle calls
[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 "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 VSTPluginUI*
43 create_mac_vst_gui (boost::shared_ptr<PluginInsert> insert)
44 {
45         /* PluginUIWindow::create_mac_vst_editor assures this cast works */
46         boost::shared_ptr<MacVSTPlugin> mvst =  boost::dynamic_pointer_cast<MacVSTPlugin> (insert->plugin());
47         return new MacVSTPluginUI (insert, mvst);
48 }
49
50
51 MacVSTPluginUI::MacVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vst)
52         : VSTPluginUI (pi, vst)
53         , _ns_view (0)
54 {
55         low_box.add_events (Gdk::VISIBILITY_NOTIFY_MASK | Gdk::EXPOSURE_MASK);
56         low_box.signal_realize().connect (mem_fun (this, &MacVSTPluginUI::lower_box_realized));
57         pack_start (low_box, true, true);
58         low_box.show ();
59 }
60
61 MacVSTPluginUI::~MacVSTPluginUI ()
62 {
63         if (_ns_view) {
64                 [_ns_view removeFromSuperview];
65                 [_ns_view release];
66         }
67
68         AEffect* plugin = _vst->state()->plugin;
69         plugin->dispatcher (plugin, effEditClose, 0, 0, 0, 0.0f);
70         _idle_connection.disconnect();
71 }
72
73 NSWindow*
74 MacVSTPluginUI::get_nswindow ()
75 {
76         Gtk::Container* toplevel = get_toplevel();
77         if (!toplevel || !toplevel->is_toplevel()) {
78                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
79                 return 0;
80         }
81         NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
82
83         if (!true_parent) {
84                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
85                 return 0;
86         }
87
88         return true_parent;
89 }
90
91 int
92 MacVSTPluginUI::package (Gtk::Window& win)
93 {
94         VSTPluginUI::package (win);
95         return 0;
96 }
97
98 void
99 MacVSTPluginUI::forward_key_event (GdkEventKey* ev)
100 {
101 }
102
103 void
104 MacVSTPluginUI::lower_box_realized ()
105 {
106         NSWindow* win = get_nswindow ();
107         if (!win) {
108                 return;
109         }
110
111         [win setAutodisplay:1]; // turn off GTK stuff for this window
112
113         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
114         _ns_view = [[NSView new] retain];
115         [view addSubview:_ns_view];
116
117         AEffect* plugin = _vst->state()->plugin;
118         plugin->dispatcher (plugin, effEditOpen, 0, 0, _ns_view, 0.0f);
119
120         struct ERect* er = NULL;
121         plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
122         if (er) {
123                 int req_width = er->right - er->left;
124                 int req_height = er->bottom - er->top;
125
126                 low_box.set_size_request (req_width, req_height);
127
128                 gint xx, yy;
129                 gtk_widget_translate_coordinates(
130                                 GTK_WIDGET(low_box.gobj()),
131                                 GTK_WIDGET(low_box.get_parent()->gobj()),
132                                 8, 6, &xx, &yy);
133                 [_ns_view setFrame:NSMakeRect(xx, yy, req_width, req_height)];
134         }
135
136         _idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &MacVSTPluginUI::idle));
137 }
138
139 int
140 MacVSTPluginUI::get_XID ()
141 {
142         return _vst->state()->xid;
143 }
144
145 bool
146 MacVSTPluginUI::idle ()
147 {
148         AEffect* plugin = _vst->state()->plugin;
149         _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
150         return true; // _vst->state()->wantIdle;
151 }