fix embedded plugin UI keyboard handling
[ardour.git] / gtk2_ardour / plugin_ui.h
1 /*
2     Copyright (C) 2000-2006 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 #ifndef __ardour_plugin_ui_h__
21 #define __ardour_plugin_ui_h__
22
23 #ifdef WAF_BUILD
24 #include "gtk2ardour-config.h"
25 #endif
26
27 #include <vector>
28 #include <map>
29 #include <list>
30
31 #include <sigc++/signal.h>
32
33 #include <gtkmm/button.h>
34 #include <gtkmm/box.h>
35 #include <gtkmm/table.h>
36 #include <gtkmm/eventbox.h>
37 #include <gtkmm/viewport.h>
38 #include <gtkmm/scrolledwindow.h>
39 #include <gtkmm/label.h>
40 #include <gtkmm/menu.h>
41 #include <gtkmm/image.h>
42 #include <gtkmm/adjustment.h>
43 #include <gtkmm/togglebutton.h>
44 #include <gtkmm/socket.h>
45 #include <gtkmm/comboboxtext.h>
46 #include <gtkmm/socket.h>
47
48 #include "ardour/types.h"
49 #include "ardour/plugin.h"
50 #include "ardour/variant.h"
51
52 #include "automation_controller.h"
53 #include "ardour_button.h"
54 #include "ardour_window.h"
55
56 namespace ARDOUR {
57         class PluginInsert;
58         class Plugin;
59         class WindowsVSTPlugin;
60         class LXVSTPlugin;
61         class IOProcessor;
62         class AUPlugin;
63         class Processor;
64 }
65
66 namespace PBD {
67         class Controllable;
68 }
69
70 namespace Gtkmm2ext {
71         class HSliderController;
72         class BarController;
73         class ClickBox;
74         class FastMeter;
75         class PixmapButton;
76 }
77
78 class LatencyGUI;
79 class ArdourWindow;
80 class PluginEqGui;
81
82 class PlugUIBase : public virtual sigc::trackable, public PBD::ScopedConnectionList
83 {
84   public:
85         PlugUIBase (boost::shared_ptr<ARDOUR::PluginInsert>);
86         virtual ~PlugUIBase();
87
88         virtual gint get_preferred_height () = 0;
89         virtual gint get_preferred_width () = 0;
90         virtual bool resizable () { return true; }
91         virtual bool start_updating(GdkEventAny*) = 0;
92         virtual bool stop_updating(GdkEventAny*) = 0;
93
94         virtual void activate () {}
95         virtual void deactivate () {}
96
97         void update_preset_list ();
98         void update_preset ();
99
100         void latency_button_clicked ();
101
102         virtual bool on_window_show(const std::string& /*title*/) { return true; }
103         virtual void on_window_hide() {}
104
105         virtual void forward_key_event (GdkEventKey*) {}
106         virtual void grab_focus () {}
107   virtual bool non_gtk_gui() const { return false; }
108
109         sigc::signal<void,bool> KeyboardFocused;
110
111   protected:
112         boost::shared_ptr<ARDOUR::PluginInsert> insert;
113         boost::shared_ptr<ARDOUR::Plugin> plugin;
114
115         /* UI elements that can subclasses can add to their widgets */
116
117         /** a ComboBoxText which lists presets and manages their selection */
118         Gtk::ComboBoxText _preset_combo;
119         /** a label which has a * in if the current settings are different from the preset being shown */
120         Gtk::Label _preset_modified;
121         /** a button to add a preset */
122         Gtk::Button add_button;
123         /** a button to save the current settings as a new user preset */
124         Gtk::Button save_button;
125         /** a button to delete the current preset (if it is a user one) */
126         Gtk::Button delete_button;
127         /** a button to bypass the plugin */
128         ArdourButton bypass_button;
129         /** a button to acquire keyboard focus */
130         Gtk::EventBox focus_button;
131         /** an expander containing the plugin description */
132         Gtk::Expander description_expander;
133         /** an expander containing the plugin analysis graph */
134         Gtk::Expander plugin_analysis_expander;
135         /** a label indicating the plugin latency */
136         Gtk::Label latency_label;
137         /** a button which, when clicked, opens the latency GUI */
138         Gtk::Button latency_button;
139         /** a button which sets all controls' automation setting to Manual */
140         ArdourButton automation_manual_all_button;
141         /** a button which sets all controls' automation setting to Play */
142         ArdourButton automation_play_all_button;
143     /** a button which sets all controls' automation setting to Write */
144         ArdourButton automation_write_all_button;
145         /** a button which sets all controls' automation setting to Touch */
146         ArdourButton automation_touch_all_button;
147         
148         void set_latency_label ();
149
150         LatencyGUI* latency_gui;
151         ArdourWindow* latency_dialog;
152
153         PluginEqGui* eqgui;
154         Gtk::Requisition pre_eq_size;
155
156         Gtk::Image* focus_out_image;
157         Gtk::Image* focus_in_image;
158         int _no_load_preset;
159
160         virtual void preset_selected ();
161         void add_plugin_setting ();
162         void save_plugin_setting ();
163         void delete_plugin_setting ();
164         bool focus_toggled(GdkEventButton*);
165         bool bypass_button_release(GdkEventButton*);
166         void toggle_description ();
167         void toggle_plugin_analysis ();
168         void processor_active_changed (boost::weak_ptr<ARDOUR::Processor> p);
169         void plugin_going_away ();
170         virtual void parameter_changed (uint32_t, float);
171         void preset_added_or_removed ();
172         void update_preset_modified ();
173
174         PBD::ScopedConnection death_connection;
175         PBD::ScopedConnection active_connection;
176         PBD::ScopedConnection preset_added_connection;
177         PBD::ScopedConnection preset_removed_connection;
178         PBD::ScopedConnectionList control_connections;
179 };
180
181 class GenericPluginUI : public PlugUIBase, public Gtk::VBox
182 {
183   public:
184         GenericPluginUI (boost::shared_ptr<ARDOUR::PluginInsert> plug, bool scrollable=false);
185         ~GenericPluginUI ();
186
187         gint get_preferred_height () { return prefheight; }
188         gint get_preferred_width () { return -1; }
189
190         bool start_updating(GdkEventAny*);
191         bool stop_updating(GdkEventAny*);
192
193   private:
194         Gtk::VBox main_contents;
195
196         Gtk::HBox settings_box;
197         Gtk::HBox hpacker;
198
199         Gtk::Table button_table;
200         Gtk::Table output_table;
201
202         Gtk::ScrolledWindow scroller;
203         Gtk::Adjustment hAdjustment;
204         Gtk::Adjustment vAdjustment;
205         Gtk::Viewport scroller_view;
206         Gtk::Menu* automation_menu;
207
208         gint prefheight;
209         bool is_scrollable;
210
211         struct MeterInfo {
212                 Gtkmm2ext::FastMeter *meter;
213
214                 float           min;
215                 float           max;
216                 bool            min_unbound;
217                 bool            max_unbound;
218                 bool packed;
219
220                 MeterInfo () {
221                         meter = 0;
222                         packed = false;
223                         min = 1.0e10;
224                         max = -1.0e10;
225                         min_unbound = false;
226                         max_unbound = false;
227                 }
228         };
229
230         static const int32_t initial_button_rows = 12;
231         static const int32_t initial_button_cols = 1;
232         static const int32_t initial_output_rows = 1;
233         static const int32_t initial_output_cols = 4;
234
235         /* FIXME: Unify with AutomationController */
236         struct ControlUI : public Gtk::HBox {
237
238                 const Evoral::Parameter parameter() const { return param; }
239
240                 Evoral::Parameter                            param;
241                 boost::shared_ptr<ARDOUR::AutomationControl> control;
242
243                 /* input */
244
245                 Gtk::ComboBoxText*                      combo;
246                 boost::shared_ptr<ARDOUR::ScalePoints>  scale_points;
247                 Gtk::ToggleButton*                      button;
248                 boost::shared_ptr<AutomationController> controller;
249                 Gtkmm2ext::ClickBox*                    clickbox;
250                 Gtk::Label                              label;
251                 bool                                    update_pending;
252                 char                                    ignore_change;
253                 Gtk::Button                             automate_button;
254                 Gtk::FileChooserButton*                 file_button;
255
256                 /* output */
257
258                 Gtk::EventBox* display;
259                 Gtk::Label*    display_label;
260
261                 Gtk::HBox*     hbox;
262                 Gtk::VBox*     vbox;
263                 MeterInfo*     meterinfo;
264
265                 ControlUI (const Evoral::Parameter& param);
266                 ~ControlUI ();
267         };
268
269         std::vector<ControlUI*>   input_controls;
270         std::vector<ControlUI*>   input_controls_with_automation;
271         std::vector<ControlUI*>   output_controls;
272         sigc::connection screen_update_connection;
273         void output_update();
274
275         void build ();
276         ControlUI* build_control_ui (const Evoral::Parameter&                     param,
277                                      const ARDOUR::ParameterDescriptor&           desc,
278                                      boost::shared_ptr<ARDOUR::AutomationControl> mcontrol,
279                                      float                                        value,
280                                      bool                                         is_input);
281
282         void ui_parameter_changed (ControlUI* cui);
283         void toggle_parameter_changed (ControlUI* cui);
284         void update_control_display (ControlUI* cui);
285         void control_port_toggled (ControlUI* cui);
286         void control_combo_changed (ControlUI* cui);
287
288         void astate_clicked (ControlUI*);
289         void automation_state_changed (ControlUI*);
290         void set_automation_state (ARDOUR::AutoState state, ControlUI* cui);
291         void set_all_automation (ARDOUR::AutoState state);
292
293         /* XXX: remove */
294         void print_parameter (char *buf, uint32_t len, uint32_t param);
295         bool integer_printer (char* buf, Gtk::Adjustment &, ControlUI *);
296         bool midinote_printer(char* buf, Gtk::Adjustment &, ControlUI *);
297
298         void set_property (const ARDOUR::ParameterDescriptor& desc,
299                            Gtk::FileChooserButton*            widget);
300         void property_changed (uint32_t key, const ARDOUR::Variant& value);
301
302         typedef std::map<uint32_t, Gtk::FileChooserButton*> PropertyControls;
303         PropertyControls _property_controls;
304 };
305
306 class PluginUIWindow : public ArdourWindow
307 {
308   public:
309         PluginUIWindow (boost::shared_ptr<ARDOUR::PluginInsert> insert,
310                         bool scrollable=false,
311                         bool editor=true);
312         ~PluginUIWindow ();
313
314         PlugUIBase& pluginui() { return *_pluginui; }
315
316         void resize_preferred();
317         void set_parent (Gtk::Window*);
318         void set_title(const std::string& title);
319
320
321         bool on_key_press_event (GdkEventKey*);
322         bool on_key_release_event (GdkEventKey*);
323         void on_show ();
324         void on_hide ();
325
326   private:
327         std::string _title;
328         PlugUIBase* _pluginui;
329         PBD::ScopedConnection death_connection;
330         Gtk::Window* parent;
331         Gtk::VBox vbox;
332         bool was_visible;
333         bool _keyboard_focused;
334 #ifdef AUDIOUNIT_SUPPORT
335         int pre_deactivate_x;
336         int pre_deactivate_y;
337 #endif
338
339         void keyboard_focused (bool yn);
340
341         void app_activated (bool);
342         void plugin_going_away ();
343
344         bool create_windows_vst_editor (boost::shared_ptr<ARDOUR::PluginInsert>);
345         bool create_lxvst_editor(boost::shared_ptr<ARDOUR::PluginInsert>);
346         bool create_audiounit_editor (boost::shared_ptr<ARDOUR::PluginInsert>);
347         bool create_lv2_editor (boost::shared_ptr<ARDOUR::PluginInsert>);
348 };
349
350 #ifdef AUDIOUNIT_SUPPORT
351 /* this function has to be in a .mm file */
352 extern PlugUIBase* create_au_gui (boost::shared_ptr<ARDOUR::PluginInsert>, Gtk::VBox**);
353 #endif
354
355 #endif /* __ardour_plugin_ui_h__ */