add a plugin-death connection
[ardour.git] / gtk2_ardour / processor_box.h
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 #ifndef __ardour_gtk_processor_box__
21 #define __ardour_gtk_processor_box__
22
23 #include <cmath>
24 #include <vector>
25
26 #include <boost/function.hpp>
27
28 #include <gtkmm/box.h>
29 #include <gtkmm/eventbox.h>
30 #include <gtkmm/menu.h>
31 #include <gtkmm/scrolledwindow.h>
32
33 #include "gtkmm2ext/auto_spin.h"
34 #include "gtkmm2ext/bindings.h"
35 #include "gtkmm2ext/click_box.h"
36 #include "gtkmm2ext/dndtreeview.h"
37 #include "gtkmm2ext/dndvbox.h"
38 #include "gtkmm2ext/pixfader.h"
39 #include "gtkmm2ext/persistent_tooltip.h"
40
41 #include "pbd/stateful.h"
42 #include "pbd/signals.h"
43
44 #include "ardour/types.h"
45 #include "ardour/ardour.h"
46 #include "ardour/plugin_insert.h"
47 #include "ardour/luaproc.h"
48 #include "ardour/port_insert.h"
49 #include "ardour/processor.h"
50 #include "ardour/route.h"
51 #include "ardour/session_handle.h"
52
53 #include "pbd/fastlog.h"
54
55 #include "plugin_interest.h"
56 #include "io_selector.h"
57 #include "send_ui.h"
58 #include "enums.h"
59 #include "ardour_button.h"
60 #include "window_manager.h"
61
62 class MotionController;
63 class PluginSelector;
64 class PluginUIWindow;
65 class RouteProcessorSelection;
66 class MixerStrip;
67
68 namespace ARDOUR {
69         class Connection;
70         class IO;
71         class Insert;
72         class Plugin;
73         class PluginInsert;
74         class PortInsert;
75         class Route;
76         class Session;
77 }
78
79 class ProcessorBox;
80
81 class ProcessorWindowProxy : public WM::ProxyBase
82 {
83   public:
84         ProcessorWindowProxy (std::string const &, ProcessorBox *, boost::weak_ptr<ARDOUR::Processor>);
85         ~ProcessorWindowProxy();
86
87         Gtk::Window* get (bool create = false);
88
89         boost::weak_ptr<ARDOUR::Processor> processor () const {
90                 return _processor;
91         }
92
93         ARDOUR::SessionHandlePtr* session_handle();
94         void show_the_right_window ();
95         void set_custom_ui_mode(bool use_custom) { want_custom = use_custom; }
96
97         int set_state (const XMLNode&, int);
98         XMLNode& get_state ();
99
100   private:
101         ProcessorBox* _processor_box;
102         boost::weak_ptr<ARDOUR::Processor> _processor;
103         bool is_custom;
104         bool want_custom;
105
106         void processor_going_away ();
107         PBD::ScopedConnection going_away_connection;
108 };
109
110
111 class PluginPinWindowProxy : public WM::ProxyBase
112 {
113   public:
114         PluginPinWindowProxy (std::string const &, boost::weak_ptr<ARDOUR::Processor>);
115         ~PluginPinWindowProxy();
116
117         Gtk::Window* get (bool create = false);
118         ARDOUR::SessionHandlePtr* session_handle();
119
120   private:
121         ProcessorBox* _processor_box;
122         boost::weak_ptr<ARDOUR::Processor> _processor;
123
124         void processor_going_away ();
125         PBD::ScopedConnection going_away_connection;
126 };
127
128
129
130 class ProcessorEntry : public Gtkmm2ext::DnDVBoxChild, public sigc::trackable
131 {
132 public:
133         ProcessorEntry (ProcessorBox *, boost::shared_ptr<ARDOUR::Processor>, Width);
134         ~ProcessorEntry ();
135
136         Gtk::EventBox& action_widget ();
137         Gtk::Widget& widget ();
138         std::string drag_text () const;
139         void set_visual_state (Gtkmm2ext::VisualState, bool);
140
141         bool is_selectable() const {return _selectable;}
142         void set_selectable(bool s) { _selectable = s; }
143
144         bool drag_data_get (Glib::RefPtr<Gdk::DragContext> const, Gtk::SelectionData &);
145         bool can_copy_state (Gtkmm2ext::DnDVBoxChild*) const;
146
147         enum Position {
148                 PreFader,
149                 Fader,
150                 PostFader
151         };
152
153         void set_position (Position, uint32_t);
154         bool unknown_processor () const { return _unknown_processor; } ;
155         boost::shared_ptr<ARDOUR::Processor> processor () const;
156         void set_enum_width (Width);
157
158         /** Hide any widgets that should be hidden */
159         virtual void hide_things ();
160
161         void show_all_controls ();
162         void hide_all_controls ();
163         void add_control_state (XMLNode *) const;
164         void set_control_state (XMLNode const *);
165         std::string state_id () const;
166         Gtk::Menu* build_controls_menu ();
167         Gtk::Menu* build_send_options_menu ();
168
169 protected:
170         ArdourButton _button;
171         Gtk::VBox _vbox;
172         Position _position;
173         uint32_t _position_num;
174         ProcessorBox* _parent;
175
176         virtual void setup_visuals ();
177
178 private:
179         bool _selectable;
180         bool _unknown_processor;
181         void led_clicked(GdkEventButton *);
182         void processor_active_changed ();
183         void processor_property_changed (const PBD::PropertyChange&);
184         void processor_configuration_changed (const ARDOUR::ChanCount in, const ARDOUR::ChanCount out);
185         std::string name (Width) const;
186         void setup_tooltip ();
187
188         boost::shared_ptr<ARDOUR::Processor> _processor;
189         Width _width;
190         PBD::ScopedConnection active_connection;
191         PBD::ScopedConnection name_connection;
192         PBD::ScopedConnection config_connection;
193         ARDOUR::PluginPresetPtr _plugin_preset_pointer;
194
195         class Control : public sigc::trackable {
196         public:
197                 Control (boost::shared_ptr<ARDOUR::AutomationControl>, std::string const &);
198                 ~Control ();
199
200                 void set_visible (bool);
201                 void add_state (XMLNode *) const;
202                 void set_state (XMLNode const *);
203                 void hide_things ();
204
205                 bool visible () const {
206                         return _visible;
207                 }
208
209                 std::string name () const {
210                         return _name;
211                 }
212
213                 Gtk::Alignment box;
214
215         private:
216                 void slider_adjusted ();
217                 void button_clicked ();
218                 void button_clicked_event (GdkEventButton *);
219                 void control_changed ();
220                 std::string state_id () const;
221                 void set_tooltip ();
222
223                 boost::weak_ptr<ARDOUR::AutomationControl> _control;
224                 /* things for a slider */
225                 Gtk::Adjustment _adjustment;
226                 Gtkmm2ext::HSliderController _slider;
227                 Gtkmm2ext::PersistentTooltip _slider_persistant_tooltip;
228                 /* things for a button */
229                 ArdourButton _button;
230                 bool _ignore_ui_adjustment;
231                 PBD::ScopedConnection _connection;
232                 bool _visible;
233                 std::string _name;
234                 sigc::connection timer_connection;
235         };
236
237         std::list<Control*> _controls;
238
239         void toggle_inline_display_visibility ();
240         void toggle_control_visibility (Control *);
241         void toggle_panner_link ();
242
243         class PluginDisplay : public Gtk::DrawingArea {
244         public:
245                 PluginDisplay(ProcessorEntry&, boost::shared_ptr<ARDOUR::Plugin>, uint32_t max_height = 80);
246                 virtual ~PluginDisplay();
247         protected:
248                 bool on_expose_event (GdkEventExpose *);
249                 void on_size_request (Gtk::Requisition* req);
250                 bool on_button_press_event (GdkEventButton *ev);
251                 bool on_button_release_event (GdkEventButton *ev);
252
253                 void plugin_going_away () {
254                         _qdraw_connection.disconnect ();
255                 }
256
257                 void update_height_alloc (uint32_t inline_height);
258                 virtual uint32_t render_inline (cairo_t *, uint32_t width);
259
260                 ProcessorEntry& _entry;
261                 boost::shared_ptr<ARDOUR::Plugin> _plug;
262                 PBD::ScopedConnection _qdraw_connection;
263                 PBD::ScopedConnection _death_connection;
264                 cairo_surface_t* _surf;
265                 uint32_t _max_height;
266                 uint32_t _cur_height;
267                 bool _scroll;
268         };
269
270         class LuaPluginDisplay : public PluginDisplay {
271         public:
272                 LuaPluginDisplay(ProcessorEntry&, boost::shared_ptr<ARDOUR::LuaProc>, uint32_t max_height = 80);
273                 ~LuaPluginDisplay();
274         protected:
275                 virtual uint32_t render_inline (cairo_t *, uint32_t width);
276         private:
277                 boost::shared_ptr<ARDOUR::LuaProc> _luaproc;
278                 LuaState lua_gui;
279                 luabridge::LuaRef * _lua_render_inline;
280         };
281
282         class PortIcon : public Gtk::DrawingArea {
283         public:
284                 PortIcon(bool input);
285                 void set_ports(ARDOUR::ChanCount const ports) { _ports = ports; }
286         private:
287                 bool on_expose_event (GdkEventExpose *);
288                 bool _input;
289                 ARDOUR::ChanCount _ports;
290         };
291
292         class RoutingIcon : public Gtk::DrawingArea {
293         public:
294                 RoutingIcon(bool inputrouting = true);
295                 void set (
296                                 const ARDOUR::ChanCount&,
297                                 const ARDOUR::ChanCount&,
298                                 const ARDOUR::ChanCount&,
299                                 const ARDOUR::ChanCount&,
300                                 const ARDOUR::ChanMapping&,
301                                 const ARDOUR::ChanMapping&,
302                                 const ARDOUR::ChanMapping&);
303                 void set_fed_by (
304                                 const ARDOUR::ChanCount&,
305                                 const ARDOUR::ChanCount&,
306                                 const ARDOUR::ChanMapping&,
307                                 const ARDOUR::ChanMapping&);
308
309                 void set_feeding (
310                                 const ARDOUR::ChanCount&,
311                                 const ARDOUR::ChanCount&,
312                                 const ARDOUR::ChanMapping&,
313                                 const ARDOUR::ChanMapping&);
314
315                 void set_terminal (bool b);
316
317                 void copy_state (const RoutingIcon& other) {
318                         _in         = other._in;
319                         _out        = other._out;
320                         _sources    = other._sources;
321                         _sinks      = other._sinks;
322                         _in_map     = other._in_map;
323                         _out_map    = other._out_map;
324                         _thru_map   = other._thru_map;
325                         _f_out      = other._f_out;
326                         _f_out_map  = other._f_out_map;
327                         _f_thru_map = other._f_thru_map;
328                         _f_sources  = other._f_sources;
329                         _i_in       = other._i_in;
330                         _i_in_map   = other._i_in_map;
331                         _i_thru_map = other._i_thru_map;
332                         _i_sinks    = other._i_sinks;
333                         _fed_by     = other._fed_by;
334                         _feeding    = other._feeding;
335                 }
336
337                 void unset_fed_by () { _fed_by  = false ; }
338                 void unset_feeding () { _feeding  = false ; }
339                 bool in_identity () const;
340                 bool out_identity () const;
341                 bool can_coalesce () const;
342
343                 static double pin_x_pos (uint32_t, double, uint32_t, uint32_t, bool);
344                 static void draw_connection (cairo_t*, double, double, double, double, bool, bool dashed = false);
345                 static void draw_gnd (cairo_t*, double, double, double, bool);
346                 static void draw_sidechain (cairo_t*, double, double, double, bool);
347                 static void draw_thru_src (cairo_t*, double, double, double, bool);
348                 static void draw_thru_sink (cairo_t*, double, double, double, bool);
349
350         private:
351                 bool on_expose_event (GdkEventExpose *);
352                 void expose_input_map (cairo_t*, const double, const double);
353                 void expose_coalesced_input_map (cairo_t*, const double, const double);
354                 void expose_output_map (cairo_t*, const double, const double);
355
356                 ARDOUR::ChanCount   _in;
357                 ARDOUR::ChanCount   _out;
358                 ARDOUR::ChanCount   _sources;
359                 ARDOUR::ChanCount   _sinks;
360                 ARDOUR::ChanMapping _in_map;
361                 ARDOUR::ChanMapping _out_map;
362                 ARDOUR::ChanMapping _thru_map;
363                 ARDOUR::ChanCount   _f_out;
364                 ARDOUR::ChanMapping _f_out_map;
365                 ARDOUR::ChanMapping _f_thru_map;
366                 ARDOUR::ChanCount   _f_sources;
367                 ARDOUR::ChanCount   _i_in;
368                 ARDOUR::ChanMapping _i_in_map;
369                 ARDOUR::ChanMapping _i_thru_map;
370                 ARDOUR::ChanCount   _i_sinks;
371                 bool _fed_by;
372                 bool _feeding;
373                 bool _input;
374                 bool _terminal;
375         };
376
377 public:
378         PortIcon input_icon;
379         PortIcon output_icon;
380         RoutingIcon routing_icon; // sits on top of every processor (input routing)
381         RoutingIcon output_routing_icon; // only used by last processor in the chain
382
383 protected:
384         PluginDisplay *_plugin_display ;
385 };
386
387 class PluginInsertProcessorEntry : public ProcessorEntry
388 {
389 public:
390         PluginInsertProcessorEntry (ProcessorBox *, boost::shared_ptr<ARDOUR::PluginInsert>, Width);
391
392         void hide_things ();
393
394 private:
395         void iomap_changed ();
396         boost::shared_ptr<ARDOUR::PluginInsert> _plugin_insert;
397
398         PBD::ScopedConnectionList _iomap_connection;
399 };
400
401 class ProcessorBox : public Gtk::HBox, public PluginInterestedObject, public ARDOUR::SessionHandlePtr
402 {
403   public:
404         enum ProcessorOperation {
405                 ProcessorsCut,
406                 ProcessorsCopy,
407                 ProcessorsPaste,
408                 ProcessorsDelete,
409                 ProcessorsSelectAll,
410                 ProcessorsSelectNone,
411                 ProcessorsToggleActive,
412                 ProcessorsAB,
413         };
414
415         ProcessorBox (ARDOUR::Session*, boost::function<PluginSelector*()> get_plugin_selector,
416                       RouteProcessorSelection&, MixerStrip* parent, bool owner_is_mixer = false);
417         ~ProcessorBox ();
418
419         void set_route (boost::shared_ptr<ARDOUR::Route>);
420         void set_width (Width);
421
422         bool processor_operation (ProcessorOperation);
423
424         void select_all_processors ();
425         void deselect_all_processors ();
426         void select_all_plugins ();
427         void select_all_inserts ();
428         void select_all_sends ();
429
430         void all_visible_processors_active(bool state);
431         void setup_routing_feeds ();
432
433         void hide_things ();
434
435         bool edit_aux_send(boost::shared_ptr<ARDOUR::Processor>);
436
437         /* Everything except a WindowProxy object should use this to get the window */
438         Gtk::Window* get_processor_ui (boost::shared_ptr<ARDOUR::Processor>) const;
439
440         /* a WindowProxy object can use this */
441         Gtk::Window* get_editor_window (boost::shared_ptr<ARDOUR::Processor>, bool);
442         Gtk::Window* get_generic_editor_window (boost::shared_ptr<ARDOUR::Processor>);
443
444         void manage_pins (boost::shared_ptr<ARDOUR::Processor>);
445         void edit_processor (boost::shared_ptr<ARDOUR::Processor>);
446         void generic_edit_processor (boost::shared_ptr<ARDOUR::Processor>);
447
448         void update_gui_object_state (ProcessorEntry *);
449
450         sigc::signal<void,boost::shared_ptr<ARDOUR::Processor> > ProcessorSelected;
451         sigc::signal<void,boost::shared_ptr<ARDOUR::Processor> > ProcessorUnselected;
452
453         static Glib::RefPtr<Gtk::ActionGroup> processor_box_actions;
454         static Gtkmm2ext::Bindings* bindings;
455         static void register_actions();
456
457 #ifndef NDEBUG
458         static bool show_all_processors;
459 #endif
460
461   private:
462         /* prevent copy construction */
463         ProcessorBox (ProcessorBox const &);
464
465         boost::shared_ptr<ARDOUR::Route>  _route;
466         MixerStrip*         _parent_strip; // null if in RouteParamsUI
467         bool                _owner_is_mixer;
468         bool                 ab_direction;
469         PBD::ScopedConnectionList _mixer_strip_connections;
470         PBD::ScopedConnectionList _route_connections;
471
472         boost::function<PluginSelector*()> _get_plugin_selector;
473
474         boost::shared_ptr<ARDOUR::Processor> _processor_being_created;
475
476         /** Index at which to place a new plugin (based on where the menu was opened), or -1 to
477          *  put at the end of the plugin list.
478          */
479         int _placement;
480         uint32_t                  _visible_prefader_processors;
481
482         RouteProcessorSelection& _rr_selection;
483         static Gtkmm2ext::ActionMap myactions;
484
485         static void load_bindings ();
486
487         void route_going_away ();
488
489         bool is_editor_mixer_strip() const;
490
491         Gtkmm2ext::DnDVBox<ProcessorEntry> processor_display;
492         Gtk::ScrolledWindow    processor_scroller;
493
494         boost::shared_ptr<ARDOUR::Processor> find_drop_position (ProcessorEntry* position);
495
496         void _drop_plugin_preset (Gtk::SelectionData const &, ARDOUR::Route::ProcessorList &);
497         void _drop_plugin (Gtk::SelectionData const &, ARDOUR::Route::ProcessorList &);
498
499         void plugin_drop (Gtk::SelectionData const &, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context);
500         void object_drop (Gtkmm2ext::DnDVBox<ProcessorEntry> *, ProcessorEntry *, Glib::RefPtr<Gdk::DragContext> const &);
501
502         Width _width;
503         bool  _redisplay_pending;
504
505         Gtk::Menu *processor_menu;
506         gint processor_menu_map_handler (GdkEventAny *ev);
507         Gtk::Menu * build_processor_menu ();
508         void show_processor_menu (int);
509         Gtk::Menu* build_possible_aux_menu();
510
511         void choose_aux (boost::weak_ptr<ARDOUR::Route>);
512         void choose_send ();
513         void send_io_finished (IOSelector::Result, boost::weak_ptr<ARDOUR::Processor>, IOSelectorWindow*);
514         void return_io_finished (IOSelector::Result, boost::weak_ptr<ARDOUR::Processor>, IOSelectorWindow*);
515         void choose_insert ();
516         void choose_plugin ();
517         bool use_plugins (const SelectedPlugins&);
518
519         bool no_processor_redisplay;
520
521         bool enter_notify (GdkEventCrossing *ev);
522         bool leave_notify (GdkEventCrossing *ev);
523         bool processor_button_press_event (GdkEventButton *, ProcessorEntry *);
524         bool processor_button_release_event (GdkEventButton *, ProcessorEntry *);
525         void redisplay_processors ();
526         void add_processor_to_display (boost::weak_ptr<ARDOUR::Processor>);
527         void help_count_visible_prefader_processors (boost::weak_ptr<ARDOUR::Processor>, uint32_t*, bool*);
528         void reordered ();
529         void report_failed_reorder ();
530         void route_processors_changed (ARDOUR::RouteProcessorChange);
531         void processor_menu_unmapped ();
532
533         void processors_reordered (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator&, int*);
534         void compute_processor_sort_keys ();
535
536         void ab_plugins ();
537
538         typedef std::vector<boost::shared_ptr<ARDOUR::Processor> > ProcSelection;
539
540         void cut_processors (const ProcSelection&);
541         void copy_processors (const ProcSelection&);
542         void delete_processors (const ProcSelection&);
543         void paste_processors ();
544         void paste_processors (boost::shared_ptr<ARDOUR::Processor> before);
545
546         void delete_dragged_processors (const std::list<boost::shared_ptr<ARDOUR::Processor> >&);
547         void clear_processors ();
548         void clear_processors (ARDOUR::Placement);
549         void rename_processors ();
550
551         void for_selected_processors (void (ProcessorBox::*pmf)(boost::shared_ptr<ARDOUR::Processor>));
552         void get_selected_processors (ProcSelection&) const;
553
554         bool can_cut() const;
555         bool stub_processor_selected() const;
556
557         static Glib::RefPtr<Gtk::Action> cut_action;
558         static Glib::RefPtr<Gtk::Action> copy_action;
559         static Glib::RefPtr<Gtk::Action> paste_action;
560         static Glib::RefPtr<Gtk::Action> rename_action;
561         static Glib::RefPtr<Gtk::Action> delete_action;
562         static Glib::RefPtr<Gtk::Action> backspace_action;
563         static Glib::RefPtr<Gtk::Action> manage_pins_action;
564         static Glib::RefPtr<Gtk::Action> edit_action;
565         static Glib::RefPtr<Gtk::Action> edit_generic_action;
566         void paste_processor_state (const XMLNodeList&, boost::shared_ptr<ARDOUR::Processor>);
567
568         void hide_processor_editor (boost::shared_ptr<ARDOUR::Processor>);
569         void rename_processor (boost::shared_ptr<ARDOUR::Processor>);
570
571         gint idle_delete_processor (boost::weak_ptr<ARDOUR::Processor>);
572
573         void weird_plugin_dialog (ARDOUR::Plugin& p, ARDOUR::Route::ProcessorStreams streams);
574
575         void setup_entry_positions ();
576
577         static ProcessorBox* _current_processor_box;
578
579         static void rb_choose_aux (boost::weak_ptr<ARDOUR::Route>);
580         static void rb_choose_plugin ();
581         static void rb_choose_insert ();
582         static void rb_choose_send ();
583         static void rb_clear ();
584         static void rb_clear_pre ();
585         static void rb_clear_post ();
586         static void rb_cut ();
587         static void rb_copy ();
588         static void rb_paste ();
589         static void rb_delete ();
590         static void rb_rename ();
591         static void rb_select_all ();
592         static void rb_deselect_all ();
593         static void rb_activate_all ();
594         static void rb_deactivate_all ();
595         static void rb_ab_plugins ();
596         static void rb_manage_pins ();
597         static void rb_edit ();
598         static void rb_edit_generic ();
599
600         void route_property_changed (const PBD::PropertyChange&);
601         std::string generate_processor_title (boost::shared_ptr<ARDOUR::PluginInsert> pi);
602
603         //typedef std::list<ProcessorWindowProxy*> ProcessorWindowProxies;
604         //ProcessorWindowProxies _processor_window_info;
605
606         ProcessorWindowProxy* find_window_proxy (boost::shared_ptr<ARDOUR::Processor>) const;
607
608         void set_processor_ui (boost::shared_ptr<ARDOUR::Processor>, Gtk::Window *);
609         void maybe_add_processor_to_ui_list (boost::weak_ptr<ARDOUR::Processor>);
610         void maybe_add_processor_pin_mgr (boost::weak_ptr<ARDOUR::Processor>);
611
612         bool one_processor_can_be_edited ();
613         bool processor_can_be_edited (boost::shared_ptr<ARDOUR::Processor>);
614
615         void mixer_strip_delivery_changed (boost::weak_ptr<ARDOUR::Delivery>);
616
617         XMLNode* entry_gui_object_state (ProcessorEntry *);
618         PBD::ScopedConnection amp_config_connection;
619 };
620
621 #endif /* __ardour_gtk_processor_box__ */