Gracefully handle templates that lack contents in their description or created_with...
[ardour.git] / gtk2_ardour / vst_plugin_ui.cc
1 /*
2     Copyright (C) 2000-2010 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 "ardour/vst_plugin.h"
22 #include "ardour/vst_types.h"
23 #include "ardour/plugin_insert.h"
24 #include "vst_plugin_ui.h"
25
26 #ifdef PLATFORM_WINDOWS
27 #include <gdk/gdkwin32.h>
28 #elif defined __APPLE__
29 // TODO
30 #else
31 #include <gdk/gdkx.h>
32 #endif
33
34 VSTPluginUI::VSTPluginUI (boost::shared_ptr<ARDOUR::PluginInsert> insert, boost::shared_ptr<ARDOUR::VSTPlugin> plugin)
35         : PlugUIBase (insert)
36         , _vst (plugin)
37 {
38         Gtk::HBox* box = manage (new Gtk::HBox);
39         box->set_spacing (6);
40         box->set_border_width (6);
41         box->pack_end (focus_button, false, false);
42         box->pack_end (bypass_button, false, false, 4);
43         box->pack_end (reset_button, false, false, 4);
44         box->pack_end (delete_button, false, false);
45         box->pack_end (save_button, false, false);
46         box->pack_end (add_button, false, false);
47         box->pack_end (_preset_combo, false, false);
48         box->pack_end (_preset_modified, false, false);
49         box->pack_end (pin_management_button, false, false);
50
51         bypass_button.set_active (!insert->active ());
52
53         pack_start (*box, false, false);
54         box->signal_size_allocate().connect (sigc::mem_fun (*this, &VSTPluginUI::top_box_allocated));
55 #ifdef GDK_WINDOWING_X11
56         pack_start (_socket, true, true);
57         _socket.set_border_width (0);
58 #endif
59 }
60
61 VSTPluginUI::~VSTPluginUI ()
62 {
63
64 }
65
66 void
67 VSTPluginUI::preset_selected (ARDOUR::Plugin::PresetRecord preset)
68 {
69 #ifdef GDK_WINDOWING_X11
70         _socket.grab_focus ();
71 #endif
72         PlugUIBase::preset_selected (preset);
73 }
74
75 int
76 VSTPluginUI::get_preferred_height ()
77 {
78         return _vst->state()->height + _vst->state()->voffset;
79 }
80
81 int
82 VSTPluginUI::get_preferred_width ()
83 {
84         return _vst->state()->width + _vst->state()->hoffset;
85 }
86
87 int
88 VSTPluginUI::package (Gtk::Window& win)
89 {
90 #ifdef GDK_WINDOWING_X11
91         /* Forward configure events to plugin window */
92         win.signal_configure_event().connect (sigc::mem_fun (*this, &VSTPluginUI::configure_handler), false);
93
94         /* This assumes that the window's owner understands the XEmbed protocol */
95         _socket.add_id (get_XID ());
96         _socket.set_size_request(
97                         _vst->state()->width + _vst->state()->hoffset,
98                         _vst->state()->height + _vst->state()->voffset);
99 #endif
100
101         return 0;
102 }
103
104 bool
105 VSTPluginUI::on_window_show(const std::string& title)
106 {
107         _vst->state()->gui_shown = 1;
108         return PlugUIBase::on_window_show(title);
109 }
110
111 void
112 VSTPluginUI::on_window_hide()
113 {
114         _vst->state()->gui_shown = 0;
115         PlugUIBase::on_window_hide();
116 }
117
118
119 bool
120 VSTPluginUI::configure_handler (GdkEventConfigure*)
121 {
122 #ifdef GDK_WINDOWING_X11
123         XEvent event;
124         gint x, y;
125         GdkWindow* w;
126         Window xw = _vst->state()->linux_plugin_ui_window;
127
128         if ((w = _socket.gobj()->plug_window) == 0) {
129                 return false;
130         }
131
132         event.xconfigure.type = ConfigureNotify;
133         event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
134         event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
135
136         /* The ICCCM says that synthetic events should have root relative
137          * coordinates. We still aren't really ICCCM compliant, since
138          * we don't send events when the real toplevel is moved.
139          */
140         gdk_error_trap_push ();
141         gdk_window_get_origin (w, &x, &y);
142         gdk_error_trap_pop ();
143
144         event.xconfigure.x = x;
145         event.xconfigure.y = y;
146         event.xconfigure.width = GTK_WIDGET (_socket.gobj())->allocation.width;
147         event.xconfigure.height = GTK_WIDGET (_socket.gobj())->allocation.height;
148
149         event.xconfigure.border_width = 0;
150         event.xconfigure.above = None;
151         event.xconfigure.override_redirect = False;
152
153         gdk_error_trap_push ();
154         XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
155         /* if the plugin does adds itself to the parent,
156          * but ardour re-parents it, we have a pointer to
157          * the socket's child and need to resize the
158          * child window (e.g. JUCE, u-he)
159          */
160         if (xw) {
161                 XMoveResizeWindow (GDK_WINDOW_XDISPLAY (w), xw,
162                                 0, 0, _vst->state()->width, _vst->state()->height);
163                 XMapRaised (GDK_WINDOW_XDISPLAY (w), xw);
164                 XFlush (GDK_WINDOW_XDISPLAY (w));
165         }
166         gdk_error_trap_pop ();
167 #endif
168
169         return false;
170 }