Some refactoring. Add port group headers to the port matrix.
[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 "port_matrix.h"
27 #include "i18n.h"
28
29 /** PortMatrix constructor.
30  *  @param session Our session.
31  *  @param type Port type that we are handling.
32  *  @param offer_inputs true to offer inputs, otherwise false.
33  */
34 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs)
35         : _row_ports (type, !offer_inputs),
36           _column_ports (type, offer_inputs),
37           _session (session),
38           _offer_inputs (offer_inputs),
39           _type (type),
40           _body (this, offer_inputs ? PortMatrixBody::BOTTOM_AND_LEFT : PortMatrixBody::TOP_AND_RIGHT)
41 {
42         setup ();
43         
44         /* checkbuttons for visibility of groups */
45         Gtk::HBox* visibility_buttons = Gtk::manage (new Gtk::HBox);
46
47         visibility_buttons->pack_start (*Gtk::manage (new Gtk::Label (_("Show:"))), Gtk::PACK_SHRINK);
48
49         for (std::list<PortGroup*>::iterator i = _column_ports.begin(); i != _column_ports.end(); ++i) {
50                 _port_group_uis.push_back (new PortGroupUI (this, *i));
51         }
52
53         for (std::list<PortGroupUI*>::iterator i = _port_group_uis.begin(); i != _port_group_uis.end(); ++i) {
54                 visibility_buttons->pack_start ((*i)->visibility_checkbutton(), Gtk::PACK_SHRINK);
55         }
56
57         pack_start (*visibility_buttons, Gtk::PACK_SHRINK);
58         pack_start (_hscroll, Gtk::PACK_SHRINK);
59         Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox);
60         hbox->pack_start (_body);
61         hbox->pack_start (_vscroll, Gtk::PACK_SHRINK);
62         pack_start (*hbox);
63
64         _hscroll.signal_value_changed().connect (sigc::mem_fun (*this, &PortMatrix::hscroll_changed));
65         _vscroll.signal_value_changed().connect (sigc::mem_fun (*this, &PortMatrix::vscroll_changed));
66         setup_scrollbars ();
67
68         /* XXX hard-coded initial size suggestion */
69         set_size_request (400, 200);
70         show_all ();
71 }
72
73 PortMatrix::~PortMatrix ()
74 {
75         for (std::list<PortGroupUI*>::iterator i = _port_group_uis.begin(); i != _port_group_uis.end(); ++i) {
76                 delete *i;
77         }
78 }
79
80 void
81 PortMatrix::setup ()
82 {
83         _column_ports.gather (_session);
84         _body.setup (_row_ports, _column_ports);
85         setup_scrollbars ();
86         queue_draw ();
87 }
88
89 void
90 PortMatrix::set_offer_inputs (bool s)
91 {
92         _offer_inputs = s;
93         _column_ports.set_offer_inputs (s);
94         _row_ports.set_offer_inputs (!s);
95         setup ();
96 }
97
98 void
99 PortMatrix::set_type (ARDOUR::DataType t)
100 {
101         _type = t;
102         _column_ports.set_type (t);
103         _row_ports.set_type (t);
104         setup ();
105 }
106
107 void
108 PortMatrix::hscroll_changed ()
109 {
110         _body.set_xoffset (_hscroll.get_adjustment()->get_value());
111 }
112
113 void
114 PortMatrix::vscroll_changed ()
115 {
116         _body.set_yoffset (_vscroll.get_adjustment()->get_value());
117 }
118
119 void
120 PortMatrix::setup_scrollbars ()
121 {
122         Gtk::Adjustment* a = _hscroll.get_adjustment ();
123         a->set_lower (0);
124         a->set_upper (_body.full_scroll_width());
125         a->set_page_size (_body.alloc_scroll_width());
126         a->set_step_increment (32);
127         a->set_page_increment (128);
128
129         a = _vscroll.get_adjustment ();
130         a->set_lower (0);
131         a->set_upper (_body.full_scroll_height());
132         a->set_page_size (_body.alloc_scroll_height());
133         a->set_step_increment (32);
134         a->set_page_increment (128);
135 }
136
137 void
138 PortMatrix::disassociate_all ()
139 {
140         ARDOUR::BundleList c = _column_ports.bundles ();
141         ARDOUR::BundleList r = _row_ports.bundles ();
142         
143         for (ARDOUR::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
144                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
145                         for (uint32_t k = 0; k < r.front()->nchannels(); ++k) {
146
147                                 set_state (
148                                         r.front(), k, *i, j, false, 0
149                                         );
150                                 
151                         }
152                 }
153         }
154
155         _body.rebuild_and_draw_grid ();
156 }