first pass at internal sends. this is a very tentative work in progress, and it is...
[ardour.git] / gtk2_ardour / port_matrix.cc
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 #include <iostream>
21 #include <gtkmm/scrolledwindow.h>
22 #include <gtkmm/adjustment.h>
23 #include <gtkmm/label.h>
24 #include "ardour/bundle.h"
25 #include "ardour/types.h"
26 #include "ardour/session.h"
27 #include "ardour/route.h"
28 #include "port_matrix.h"
29 #include "i18n.h"
30
31 /** PortMatrix constructor.
32  *  @param session Our session.
33  *  @param type Port type that we are handling.
34  *  @param offer_inputs true to offer inputs, otherwise false.
35  */
36 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs)
37         : _row_ports (type, !offer_inputs),
38           _column_ports (type, offer_inputs),
39           _session (session),
40           _offer_inputs (offer_inputs),
41           _type (type),
42           _body (this, offer_inputs ? PortMatrixBody::BOTTOM_AND_LEFT : PortMatrixBody::TOP_AND_RIGHT)
43 {
44         setup ();
45         
46         /* checkbuttons for visibility of groups */
47         Gtk::HBox* visibility_buttons = Gtk::manage (new Gtk::HBox);
48
49         visibility_buttons->pack_start (*Gtk::manage (new Gtk::Label (_("Show:"))), Gtk::PACK_SHRINK);
50
51         for (std::list<PortGroup*>::iterator i = _column_ports.begin(); i != _column_ports.end(); ++i) {
52                 _port_group_uis.push_back (new PortGroupUI (this, *i));
53         }
54
55         for (std::list<PortGroupUI*>::iterator i = _port_group_uis.begin(); i != _port_group_uis.end(); ++i) {
56                 visibility_buttons->pack_start ((*i)->visibility_checkbutton(), Gtk::PACK_SHRINK);
57         }
58
59         pack_start (*visibility_buttons, Gtk::PACK_SHRINK);
60         pack_start (_hscroll, Gtk::PACK_SHRINK);
61         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
62         hbox->pack_start (_body);
63         hbox->pack_start (_vscroll, Gtk::PACK_SHRINK);
64         pack_start (*hbox);
65
66         _hscroll.signal_value_changed().connect (sigc::mem_fun (*this, &PortMatrix::hscroll_changed));
67         _vscroll.signal_value_changed().connect (sigc::mem_fun (*this, &PortMatrix::vscroll_changed));
68         setup_scrollbars ();
69
70         _session.RouteAdded.connect (sigc::hide (sigc::mem_fun (*this, &PortMatrix::routes_changed)));
71         routes_changed ();
72
73         /* XXX hard-coded initial size suggestion */
74         set_size_request (400, 200);
75         show_all ();
76 }
77
78 PortMatrix::~PortMatrix ()
79 {
80         for (std::list<PortGroupUI*>::iterator i = _port_group_uis.begin(); i != _port_group_uis.end(); ++i) {
81                 delete *i;
82         }
83 }
84
85 void
86 PortMatrix::routes_changed ()
87 {
88         for (std::vector<sigc::connection>::iterator i = _route_connections.begin(); i != _route_connections.end(); ++i) {
89                 i->disconnect ();
90         }
91
92         boost::shared_ptr<ARDOUR::RouteList> routes = _session.get_routes ();
93         for (ARDOUR::RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
94                 _route_connections.push_back (
95                         (*i)->processors_changed.connect (sigc::mem_fun (*this, &PortMatrix::setup))
96                         );
97         }
98
99         setup ();
100 }
101
102 void
103 PortMatrix::setup ()
104 {
105         _column_ports.gather (_session);
106         _body.setup (_row_ports, _column_ports);
107         setup_scrollbars ();
108         queue_draw ();
109 }
110
111 void
112 PortMatrix::set_offer_inputs (bool s)
113 {
114         _offer_inputs = s;
115         _column_ports.set_offer_inputs (s);
116         _row_ports.set_offer_inputs (!s);
117         setup ();
118 }
119
120 void
121 PortMatrix::set_type (ARDOUR::DataType t)
122 {
123         _type = t;
124         _column_ports.set_type (t);
125         _row_ports.set_type (t);
126         setup ();
127 }
128
129 void
130 PortMatrix::hscroll_changed ()
131 {
132         _body.set_xoffset (_hscroll.get_adjustment()->get_value());
133 }
134
135 void
136 PortMatrix::vscroll_changed ()
137 {
138         _body.set_yoffset (_vscroll.get_adjustment()->get_value());
139 }
140
141 void
142 PortMatrix::setup_scrollbars ()
143 {
144         Gtk::Adjustment* a = _hscroll.get_adjustment ();
145         a->set_lower (0);
146         a->set_upper (_body.full_scroll_width());
147         a->set_page_size (_body.alloc_scroll_width());
148         a->set_step_increment (32);
149         a->set_page_increment (128);
150
151         a = _vscroll.get_adjustment ();
152         a->set_lower (0);
153         a->set_upper (_body.full_scroll_height());
154         a->set_page_size (_body.alloc_scroll_height());
155         a->set_step_increment (32);
156         a->set_page_increment (128);
157 }
158
159 void
160 PortMatrix::disassociate_all ()
161 {
162         ARDOUR::BundleList c = _column_ports.bundles ();
163         ARDOUR::BundleList r = _row_ports.bundles ();
164         
165         for (ARDOUR::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
166                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
167                         for (uint32_t k = 0; k < r.front()->nchannels(); ++k) {
168
169                                 set_state (
170                                         r.front(), k, *i, j, false, 0
171                                         );
172                                 
173                         }
174                 }
175         }
176
177         _body.rebuild_and_draw_grid ();
178 }