Another hacky fix for deadlocks when updating the port matrix.
[ardour.git] / gtk2_ardour / port_matrix.h
1 /*
2     Copyright (C) 2002-2009 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 __gtk_ardour_port_matrix_h__
21 #define __gtk_ardour_port_matrix_h__
22
23 #include <list>
24 #include <gtkmm/box.h>
25 #include <gtkmm/scrollbar.h>
26 #include <gtkmm/table.h>
27 #include <gtkmm/label.h>
28 #include <gtkmm/checkbutton.h>
29 #include <gtkmm/notebook.h>
30 #include <boost/shared_ptr.hpp>
31
32 #include "ardour/bundle.h"
33 #include "ardour/types.h"
34 #include "ardour/session_handle.h"
35
36 #include "port_group.h"
37 #include "port_matrix_types.h"
38
39 /** The `port matrix' UI.  This is a widget which lets the user alter
40  *  associations between one set of ports and another.  e.g. to connect
41  *  things together.
42  *
43  *  It is made up of a body, PortMatrixBody, which is rendered using cairo,
44  *  and some scrollbars and other stuff.  All of this is arranged inside the
45  *  Table that we inherit from.
46  */
47
48 namespace ARDOUR {
49         class Bundle;
50 }
51
52 namespace Gtk {
53         namespace Menu_Helpers {
54                 class MenuList;
55         }
56 }
57
58 class PortMatrixBody;
59
60 class PortMatrix : public Gtk::Table, public ARDOUR::SessionHandlePtr
61 {
62 public:
63         PortMatrix (Gtk::Window*, ARDOUR::Session *, ARDOUR::DataType);
64         ~PortMatrix ();
65
66         void set_type (ARDOUR::DataType);
67
68         ARDOUR::DataType type () const {
69                 return _type;
70         }
71
72         void disassociate_all ();
73         void setup_scrollbars ();
74         void popup_menu (ARDOUR::BundleChannel, ARDOUR::BundleChannel, uint32_t);
75
76         int min_height_divisor () const {
77                 return _min_height_divisor;
78         }
79         void set_min_height_divisor (int f) {
80                 _min_height_divisor = f;
81         }
82
83         enum Arrangement {
84                 TOP_TO_RIGHT,  ///< column labels on top, row labels to the right
85                 LEFT_TO_BOTTOM ///< row labels to the left, column labels on the bottom
86         };
87
88         
89         /** @return Arrangement in use */
90         Arrangement arrangement () const {
91                 return _arrangement;
92         }
93
94         bool show_only_bundles () const {
95                 return _show_only_bundles;
96         }
97
98         PortGroupList const * columns () const;
99         boost::shared_ptr<const PortGroup> visible_columns () const;
100
101         /** @return index into the _ports array for the list which is displayed as columns */
102         int column_index () const {
103                 return _column_index;
104         }
105
106         PortGroupList const * rows () const;
107         boost::shared_ptr<const PortGroup> visible_rows () const;
108
109         /** @return index into the _ports array for the list which is displayed as rows */
110         int row_index () const {
111                 return _row_index;
112         }
113
114         PortGroupList const * ports (int d) const {
115                 return &_ports[d];
116         }
117
118         boost::shared_ptr<const PortGroup> visible_ports (int d) const;
119         
120         void init ();
121         void setup ();
122         virtual void setup_ports (int) = 0;
123         void setup_all_ports ();
124
125         std::pair<uint32_t, uint32_t> max_size () const;
126
127         bool should_show (ARDOUR::DataType) const;
128         uint32_t count_of_our_type (ARDOUR::ChanCount) const;
129
130         /** @param c Channels; where c[0] is from _ports[0] and c[1] is from _ports[1].
131          *  @param s New state.
132          */
133         virtual void set_state (ARDOUR::BundleChannel c[2], bool s) = 0;
134
135         /** @param c Channels; where c[0] is from _ports[0] and c[1] is from _ports[1].
136          *  @return state
137          */
138         virtual PortMatrixNode::State get_state (ARDOUR::BundleChannel c[2]) const = 0;
139         virtual bool list_is_global (int) const = 0;
140
141         virtual bool can_add_channel (boost::shared_ptr<ARDOUR::Bundle>) const;
142         virtual void add_channel (boost::shared_ptr<ARDOUR::Bundle>, ARDOUR::DataType);
143         virtual bool can_remove_channels (boost::shared_ptr<ARDOUR::Bundle>) const;
144         virtual void remove_channel (ARDOUR::BundleChannel);
145         virtual void remove_all_channels (boost::weak_ptr<ARDOUR::Bundle>);
146         virtual bool can_rename_channels (boost::shared_ptr<ARDOUR::Bundle>) const {
147                 return false;
148         }
149         virtual void rename_channel (ARDOUR::BundleChannel) {}
150         virtual std::string disassociation_verb () const = 0;
151         virtual std::string channel_noun () const;
152
153         enum Result {
154                 Cancelled,
155                 Accepted
156         };
157
158         sigc::signal<void, Result> Finished;
159
160 protected:
161
162         /** We have two port group lists.  One will be presented on the rows of the matrix,
163             the other on the columns.  The PortMatrix chooses the arrangement based on which has
164             more ports in it.  Subclasses must fill these two lists with the port groups that they
165             wish to present.  The PortMatrix will arrange its layout such that signal flow is vaguely
166             from left to right as you go from list 0 to list 1.  Hence subclasses which deal with
167             inputs and outputs should put outputs in list 0 and inputs in list 1. */
168         PortGroupList _ports[2];
169
170 private:
171
172         void hscroll_changed ();
173         void vscroll_changed ();
174         void routes_changed ();
175         void reconnect_to_routes ();
176         void select_arrangement ();
177         void add_channel_proxy (boost::weak_ptr<ARDOUR::Bundle>, ARDOUR::DataType);
178         void remove_channel_proxy (boost::weak_ptr<ARDOUR::Bundle>, uint32_t);
179         void rename_channel_proxy (boost::weak_ptr<ARDOUR::Bundle>, uint32_t);
180         void disassociate_all_on_channel (boost::weak_ptr<ARDOUR::Bundle>, uint32_t, int);
181         void disassociate_all_on_bundle (boost::weak_ptr<ARDOUR::Bundle>, int);
182         void setup_global_ports ();
183         void setup_global_ports_proxy ();
184         void toggle_show_only_bundles ();
185         bool on_scroll_event (GdkEventScroll *);
186         boost::shared_ptr<ARDOUR::IO> io_from_bundle (boost::shared_ptr<ARDOUR::Bundle>) const;
187         void setup_notebooks ();
188         void remove_notebook_pages (Gtk::Notebook &);
189         void notebook_page_selected (GtkNotebookPage *, guint);
190         void route_processors_changed (ARDOUR::RouteProcessorChange);
191         void body_dimensions_changed ();
192         void session_going_away ();
193         void add_remove_option (Gtk::Menu_Helpers::MenuList &, boost::weak_ptr<ARDOUR::Bundle>, int);
194         void add_disassociate_option (Gtk::Menu_Helpers::MenuList &, boost::weak_ptr<ARDOUR::Bundle>, int, int);
195         void port_connected_or_disconnected ();
196
197         Gtk::Window* _parent;
198
199         /// port type that we are working with, or NIL if we are working with all of them
200         ARDOUR::DataType _type;
201         PBD::ScopedConnectionList _route_connections;
202         PBD::ScopedConnectionList _changed_connections;
203         PBD::ScopedConnectionList _bundle_changed_connections;
204
205         PortMatrixBody* _body;
206         Gtk::HScrollbar _hscroll;
207         Gtk::VScrollbar _vscroll;
208         Gtk::Notebook _vnotebook;
209         Gtk::Notebook _hnotebook;
210         Gtk::Label _vlabel;
211         Gtk::Label _hlabel;
212         Gtk::VBox _vbox;
213         Gtk::HBox _hbox;
214         Gtk::Label _hspacer;
215         Gtk::Label _vspacer;
216         Gtk::Menu* _menu;
217         Arrangement _arrangement;
218         int _row_index;
219         int _column_index;
220         int _min_height_divisor;
221         bool _show_only_bundles;
222         bool _inhibit_toggle_show_only_bundles;
223         bool _ignore_notebook_page_selected;
224 };
225
226 #endif