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