Fix some typo.
[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         vst->LoadPresetProgram.connect (_program_connection, invalidator (*this), boost::bind (&MacVSTPluginUI::set_program, this), gui_context());
60
61 }
62
63 MacVSTPluginUI::~MacVSTPluginUI ()
64 {
65         if (_ns_view) {
66                 [_ns_view removeFromSuperview];
67                 [_ns_view release];
68         }
69
70         AEffect* plugin = _vst->state()->plugin;
71         plugin->dispatcher (plugin, effEditClose, 0, 0, 0, 0.0f);
72         _idle_connection.disconnect();
73 }
74
75 NSWindow*
76 MacVSTPluginUI::get_nswindow ()
77 {
78         Gtk::Container* toplevel = get_toplevel();
79         if (!toplevel || !toplevel->is_toplevel()) {
80                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
81                 return 0;
82         }
83         NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
84
85         if (!true_parent) {
86                 error << _("MacVSTPluginUI: no top level window!") << endmsg;
87                 return 0;
88         }
89
90         return true_parent;
91 }
92
93 int
94 MacVSTPluginUI::package (Gtk::Window& win)
95 {
96         VSTPluginUI::package (win);
97         return 0;
98 }
99
100 void
101 MacVSTPluginUI::forward_key_event (GdkEventKey* ev)
102 {
103 }
104
105 void
106 MacVSTPluginUI::lower_box_realized ()
107 {
108         NSWindow* win = get_nswindow ();
109         if (!win) {
110                 return;
111         }
112
113         [win setAutodisplay:1]; // turn off GTK stuff for this window
114
115         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
116         _ns_view = [[NSView new] retain];
117         [view addSubview:_ns_view];
118
119         AEffect* plugin = _vst->state()->plugin;
120         plugin->dispatcher (plugin, effEditOpen, 0, 0, _ns_view, 0.0f);
121
122         struct ERect* er = NULL;
123         plugin->dispatcher (plugin, effEditGetRect, 0, 0, &er, 0 );
124         if (er) {
125                 int req_width = er->right - er->left;
126                 int req_height = er->bottom - er->top;
127
128                 low_box.set_size_request (req_width, req_height);
129
130                 gint xx, yy;
131                 gtk_widget_translate_coordinates(
132                                 GTK_WIDGET(low_box.gobj()),
133                                 GTK_WIDGET(low_box.get_parent()->gobj()),
134                                 8, 6, &xx, &yy);
135                 [_ns_view setFrame:NSMakeRect(xx, yy, req_width, req_height)];
136         }
137
138         _idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &MacVSTPluginUI::idle));
139 }
140
141 int
142 MacVSTPluginUI::get_XID ()
143 {
144         return _vst->state()->xid;
145 }
146
147 bool
148 MacVSTPluginUI::idle ()
149 {
150         AEffect* plugin = _vst->state()->plugin;
151         _vst->state()->wantIdle = plugin->dispatcher (plugin, effEditIdle, 0, 0, NULL, 0);
152         return true; // _vst->state()->wantIdle;
153 }
154
155 void
156 MacVSTPluginUI::set_program ()
157 {
158         VSTState* vstfx = _vst->state();
159
160         if (vstfx->want_program != -1) {
161                 if (vstfx->vst_version >= 2) {
162                         vstfx->plugin->dispatcher (vstfx->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
163                 }
164
165                 vstfx->plugin->dispatcher (vstfx->plugin, effSetProgram, 0, vstfx->want_program, NULL, 0);
166
167                 if (vstfx->vst_version >= 2) {
168                         vstfx->plugin->dispatcher (vstfx->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
169                 }
170
171                 vstfx->want_program = -1;
172         }
173
174         if (vstfx->want_chunk == 1) {
175                 pthread_mutex_lock (&vstfx->state_lock);
176                 vstfx->plugin->dispatcher (vstfx->plugin, 24 /* effSetChunk */, 1, vstfx->wanted_chunk_size, vstfx->wanted_chunk, 0);
177                 vstfx->want_chunk = 0;
178                 pthread_mutex_unlock (&vstfx->state_lock);
179         }
180 }