Some refactoring. Add port group headers to the port matrix.
[ardour.git] / gtk2_ardour / port_matrix_body.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 "ardour/bundle.h"
22 #include "ardour/types.h"
23 #include "port_matrix_body.h"
24 #include "port_matrix.h"
25
26 PortMatrixBody::PortMatrixBody (PortMatrix* p, Arrangement a)
27         : _port_matrix (p),
28           _column_labels (this, a == TOP_AND_RIGHT ? PortMatrixColumnLabels::TOP : PortMatrixColumnLabels::BOTTOM),
29           _row_labels (p, this, a == BOTTOM_AND_LEFT ? PortMatrixRowLabels::LEFT : PortMatrixRowLabels::RIGHT),
30           _grid (p, this),
31           _arrangement (a),
32           _xoffset (0),
33           _yoffset (0),
34           _column_ports (_port_matrix->type(), _port_matrix->offering_input()),
35           _row_ports (_port_matrix->type(), !_port_matrix->offering_input())
36 {
37         modify_bg (Gtk::STATE_NORMAL, Gdk::Color ("#00000"));
38 }
39
40
41 bool
42 PortMatrixBody::on_expose_event (GdkEventExpose* event)
43 {
44         Gdk::Rectangle const exposure (
45                 event->area.x, event->area.y, event->area.width, event->area.height
46                 );
47
48         bool intersects;
49         Gdk::Rectangle r = exposure;
50         r.intersect (_column_labels_rect, intersects);
51
52         if (intersects) {
53                 gdk_draw_drawable (
54                         get_window()->gobj(),
55                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
56                         _column_labels.get_pixmap (get_window()->gobj()),
57                         r.get_x() - _column_labels_rect.get_x() + _xoffset,
58                         r.get_y() - _column_labels_rect.get_y(),
59                         r.get_x(),
60                         r.get_y(),
61                         r.get_width(),
62                         r.get_height()
63                         );
64         }
65
66         r = exposure;
67         r.intersect (_row_labels_rect, intersects);
68
69         if (intersects) {
70                 gdk_draw_drawable (
71                         get_window()->gobj(),
72                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
73                         _row_labels.get_pixmap (get_window()->gobj()),
74                         r.get_x() - _row_labels_rect.get_x(),
75                         r.get_y() - _row_labels_rect.get_y() + _yoffset,
76                         r.get_x(),
77                         r.get_y(),
78                         r.get_width(),
79                         r.get_height()
80                         );
81         }
82
83         r = exposure;
84         r.intersect (_grid_rect, intersects);
85
86         if (intersects) {
87                 gdk_draw_drawable (
88                         get_window()->gobj(),
89                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
90                         _grid.get_pixmap (get_window()->gobj()),
91                         r.get_x() - _grid_rect.get_x() + _xoffset,
92                         r.get_y() - _grid_rect.get_y() + _yoffset,
93                         r.get_x(),
94                         r.get_y(),
95                         r.get_width(),
96                         r.get_height()
97                         );
98         }
99
100         return true;
101 }
102
103 void
104 PortMatrixBody::on_size_request (Gtk::Requisition *req)
105 {
106         std::pair<int, int> const col = _column_labels.dimensions ();
107         std::pair<int, int> const row = _row_labels.dimensions ();
108         std::pair<int, int> const grid = _grid.dimensions ();
109
110         req->width = std::max (col.first, grid.first + row.first);
111         req->height = col.second + grid.second;
112 }
113
114 void
115 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
116 {
117         Gtk::EventBox::on_size_allocate (alloc);
118         set_allocation (alloc);
119
120         _alloc_width = alloc.get_width ();
121         _alloc_height = alloc.get_height ();
122
123         compute_rectangles ();
124         _port_matrix->setup_scrollbars ();
125 }
126
127 void
128 PortMatrixBody::compute_rectangles ()
129 {
130         /* full sizes of components */
131         std::pair<uint32_t, uint32_t> const col = _column_labels.dimensions ();
132         std::pair<uint32_t, uint32_t> const row = _row_labels.dimensions ();
133         std::pair<uint32_t, uint32_t> const grid = _grid.dimensions ();
134
135         if (_arrangement == TOP_AND_RIGHT) {
136
137                 /* build from top left */
138
139                 _column_labels_rect.set_x (0);
140                 _column_labels_rect.set_y (0);
141                 _grid_rect.set_x (0);
142
143                 if (_alloc_width > col.first) {
144                         _column_labels_rect.set_width (col.first);
145                 } else {
146                         _column_labels_rect.set_width (_alloc_width);
147                 }
148
149                 /* move down to y division */
150                 
151                 uint32_t y = 0;
152                 if (_alloc_height > col.second) {
153                         y = col.second;
154                 } else {
155                         y = _alloc_height;
156                 }
157
158                 _column_labels_rect.set_height (y);
159                 _row_labels_rect.set_y (y);
160                 _row_labels_rect.set_height (_alloc_height - y);
161                 _grid_rect.set_y (y);
162                 _grid_rect.set_height (_alloc_height - y);
163
164                 /* move right to x division */
165
166                 uint32_t x = 0;
167                 if (_alloc_width > (grid.first + row.first)) {
168                         x = grid.first;
169                 } else if (_alloc_width > row.first) {
170                         x = _alloc_width - row.first;
171                 }
172
173                 _grid_rect.set_width (x);
174                 _row_labels_rect.set_x (x);
175                 _row_labels_rect.set_width (_alloc_width - x);
176                         
177
178         } else if (_arrangement == BOTTOM_AND_LEFT) {
179
180                 /* build from bottom right */
181
182                 /* move left to x division */
183
184                 uint32_t x = 0;
185                 if (_alloc_width > (grid.first + row.first)) {
186                         x = grid.first;
187                 } else if (_alloc_width > row.first) {
188                         x = _alloc_width - row.first;
189                 }
190
191                 _grid_rect.set_x (_alloc_width - x);
192                 _grid_rect.set_width (x);
193                 _column_labels_rect.set_width (col.first - grid.first + x);
194                 _column_labels_rect.set_x (_alloc_width - _column_labels_rect.get_width());
195
196                 _row_labels_rect.set_width (std::min (_alloc_width - x, row.first));
197                 _row_labels_rect.set_x (_alloc_width - x - _row_labels_rect.get_width());
198
199                 /* move up to the y division */
200                 
201                 uint32_t y = 0;
202                 if (_alloc_height > col.second) {
203                         y = col.second;
204                 } else {
205                         y = _alloc_height;
206                 }
207
208                 _column_labels_rect.set_y (_alloc_height - y);
209                 _column_labels_rect.set_height (y);
210
211                 _grid_rect.set_height (std::min (grid.second, _alloc_height - y));
212                 _grid_rect.set_y (_alloc_height - y - _grid_rect.get_height());
213
214                 _row_labels_rect.set_height (_grid_rect.get_height());
215                 _row_labels_rect.set_y (_grid_rect.get_y());
216
217         }
218 }
219
220 void
221 PortMatrixBody::setup (PortGroupList const& row, PortGroupList const& column)
222 {
223         for (std::list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
224                 i->disconnect ();
225         }
226
227         _bundle_connections.clear ();
228         
229         _row_ports = row;
230         _column_ports = column;
231
232         ARDOUR::BundleList r = _row_ports.bundles ();
233         for (ARDOUR::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
234                 
235                 _bundle_connections.push_back (
236                         (*i)->NameChanged.connect (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels))
237                         );
238                 
239         }
240
241         ARDOUR::BundleList c = _column_ports.bundles ();
242         for (ARDOUR::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
243                 _bundle_connections.push_back (
244                         (*i)->NameChanged.connect (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels))
245                         );
246         }
247         
248         _column_labels.setup ();
249         _row_labels.setup ();
250         _grid.setup ();
251
252         compute_rectangles ();
253 }
254
255 uint32_t
256 PortMatrixBody::full_scroll_width ()
257 {
258         return _grid.dimensions().first;
259
260 }
261
262 uint32_t
263 PortMatrixBody::alloc_scroll_width ()
264 {
265         return _grid_rect.get_width();
266 }
267
268 uint32_t
269 PortMatrixBody::full_scroll_height ()
270 {
271         return _grid.dimensions().second;
272 }
273
274 uint32_t
275 PortMatrixBody::alloc_scroll_height ()
276 {
277         return _grid_rect.get_height();
278 }
279
280 void
281 PortMatrixBody::set_xoffset (uint32_t xo)
282 {
283         _xoffset = xo;
284         queue_draw ();
285 }
286
287 void
288 PortMatrixBody::set_yoffset (uint32_t yo)
289 {
290         _yoffset = yo;
291         queue_draw ();
292 }
293
294 bool
295 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
296 {
297         if (Gdk::Region (_grid_rect).point_in (ev->x, ev->y)) {
298
299                 _grid.button_press (
300                         ev->x - _grid_rect.get_x() + _xoffset,
301                         ev->y - _grid_rect.get_y() + _yoffset,
302                         ev->button
303                         );
304
305         } else if (Gdk::Region (_row_labels_rect).point_in (ev->x, ev->y)) {
306
307                 _row_labels.button_press (
308                         ev->x - _row_labels_rect.get_x(),
309                         ev->y - _row_labels_rect.get_y() + _yoffset,
310                         ev->button, ev->time
311                         );
312         
313         } else {
314         
315                 return false;
316                 
317         }
318
319         return true;
320 }
321
322 void
323 PortMatrixBody::rebuild_and_draw_grid ()
324 {
325         _grid.require_rebuild ();
326         queue_draw ();
327 }
328
329 void
330 PortMatrixBody::rebuild_and_draw_column_labels ()
331 {
332         _column_labels.require_rebuild ();
333         queue_draw ();
334 }
335
336 void
337 PortMatrixBody::rebuild_and_draw_row_labels ()
338 {
339         _row_labels.require_rebuild ();
340         queue_draw ();
341 }