More port matrix re-working. Global matrix now has separate visibility buttons
[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)
27         : _matrix (p),
28           _column_labels (p, this),
29           _row_labels (p, this),
30           _grid (p, this),
31           _xoffset (0),
32           _yoffset (0),
33           _pointer_inside (false)
34 {
35         modify_bg (Gtk::STATE_NORMAL, Gdk::Color ("#00000"));
36         add_events (Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK | Gdk::POINTER_MOTION_MASK);
37 }
38
39
40 bool
41 PortMatrixBody::on_expose_event (GdkEventExpose* event)
42 {
43         Gdk::Rectangle const exposure (
44                 event->area.x, event->area.y, event->area.width, event->area.height
45                 );
46
47         bool intersects;
48         Gdk::Rectangle r = exposure;
49         r.intersect (_column_labels.parent_rectangle(), intersects);
50
51         if (intersects) {
52                 gdk_draw_drawable (
53                         get_window()->gobj(),
54                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
55                         _column_labels.get_pixmap (get_window()->gobj()),
56                         _column_labels.parent_to_component_x (r.get_x()),
57                         _column_labels.parent_to_component_y (r.get_y()),
58                         r.get_x(),
59                         r.get_y(),
60                         r.get_width(),
61                         r.get_height()
62                         );
63         }
64
65         r = exposure;
66         r.intersect (_row_labels.parent_rectangle(), intersects);
67
68         if (intersects) {
69                 gdk_draw_drawable (
70                         get_window()->gobj(),
71                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
72                         _row_labels.get_pixmap (get_window()->gobj()),
73                         _row_labels.parent_to_component_x (r.get_x()),
74                         _row_labels.parent_to_component_y (r.get_y()),
75                         r.get_x(),
76                         r.get_y(),
77                         r.get_width(),
78                         r.get_height()
79                         );
80         }
81
82         r = exposure;
83         r.intersect (_grid.parent_rectangle(), intersects);
84
85         if (intersects) {
86                 gdk_draw_drawable (
87                         get_window()->gobj(),
88                         get_style()->get_fg_gc (Gtk::STATE_NORMAL)->gobj(),
89                         _grid.get_pixmap (get_window()->gobj()),
90                         _grid.parent_to_component_x (r.get_x()),
91                         _grid.parent_to_component_y (r.get_y()),
92                         r.get_x(),
93                         r.get_y(),
94                         r.get_width(),
95                         r.get_height()
96                         );
97         }
98
99         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
100         _grid.draw_extra (cr);
101         _row_labels.draw_extra (cr);
102         _column_labels.draw_extra (cr);
103         cairo_destroy (cr);
104
105         return true;
106 }
107
108 void
109 PortMatrixBody::on_size_request (Gtk::Requisition *req)
110 {
111         std::pair<int, int> const col = _column_labels.dimensions ();
112         std::pair<int, int> const row = _row_labels.dimensions ();
113         std::pair<int, int> const grid = _grid.dimensions ();
114
115         /* don't ask for the maximum size of our contents, otherwise GTK won't
116            let the containing window shrink below this size */
117
118         req->width = std::min (512, std::max (col.first, grid.first + row.first));
119         req->height = std::min (512, col.second + grid.second);
120 }
121
122 void
123 PortMatrixBody::on_size_allocate (Gtk::Allocation& alloc)
124 {
125         Gtk::EventBox::on_size_allocate (alloc);
126
127         _alloc_width = alloc.get_width ();
128         _alloc_height = alloc.get_height ();
129
130         compute_rectangles ();
131         _matrix->setup_scrollbars ();
132 }
133
134 void
135 PortMatrixBody::compute_rectangles ()
136 {
137         /* full sizes of components */
138         std::pair<uint32_t, uint32_t> const col = _column_labels.dimensions ();
139         std::pair<uint32_t, uint32_t> const row = _row_labels.dimensions ();
140         std::pair<uint32_t, uint32_t> const grid = _grid.dimensions ();
141
142         Gdk::Rectangle col_rect;
143         Gdk::Rectangle row_rect;
144         Gdk::Rectangle grid_rect;
145
146         if (_matrix->arrangement() == PortMatrix::TOP_TO_RIGHT) {
147
148                 /* build from top left */
149
150                 col_rect.set_x (0);
151                 col_rect.set_y (0);
152                 grid_rect.set_x (0);
153
154                 if (_alloc_width > col.first) {
155                         col_rect.set_width (col.first);
156                 } else {
157                         col_rect.set_width (_alloc_width);
158                 }
159
160                 /* move down to y division */
161                 
162                 uint32_t y = 0;
163                 if (_alloc_height > col.second) {
164                         y = col.second;
165                 } else {
166                         y = _alloc_height;
167                 }
168
169                 col_rect.set_height (y);
170                 row_rect.set_y (y);
171                 row_rect.set_height (_alloc_height - y);
172                 grid_rect.set_y (y);
173                 grid_rect.set_height (_alloc_height - y);
174
175                 /* move right to x division */
176
177                 uint32_t x = 0;
178                 if (_alloc_width > (grid.first + row.first)) {
179                         x = grid.first;
180                 } else if (_alloc_width > row.first) {
181                         x = _alloc_width - row.first;
182                 }
183
184                 grid_rect.set_width (x);
185                 row_rect.set_x (x);
186                 row_rect.set_width (_alloc_width - x);
187                         
188
189         } else if (_matrix->arrangement() == PortMatrix::LEFT_TO_BOTTOM) {
190
191                 /* build from bottom right */
192
193                 /* move left to x division */
194
195                 uint32_t x = 0;
196                 if (_alloc_width > (grid.first + row.first)) {
197                         x = grid.first;
198                 } else if (_alloc_width > row.first) {
199                         x = _alloc_width - row.first;
200                 }
201
202                 grid_rect.set_x (_alloc_width - x);
203                 grid_rect.set_width (x);
204                 col_rect.set_width (col.first - grid.first + x);
205                 col_rect.set_x (_alloc_width - col_rect.get_width());
206
207                 row_rect.set_width (std::min (_alloc_width - x, row.first));
208                 row_rect.set_x (_alloc_width - x - row_rect.get_width());
209
210                 /* move up to the y division */
211                 
212                 uint32_t y = 0;
213                 if (_alloc_height > col.second) {
214                         y = col.second;
215                 } else {
216                         y = _alloc_height;
217                 }
218
219                 col_rect.set_y (_alloc_height - y);
220                 col_rect.set_height (y);
221
222                 grid_rect.set_height (std::min (grid.second, _alloc_height - y));
223                 grid_rect.set_y (_alloc_height - y - grid_rect.get_height());
224
225                 row_rect.set_height (grid_rect.get_height());
226                 row_rect.set_y (grid_rect.get_y());
227         }
228
229         _row_labels.set_parent_rectangle (row_rect);
230         _column_labels.set_parent_rectangle (col_rect);
231         _grid.set_parent_rectangle (grid_rect);
232 }
233
234 void
235 PortMatrixBody::setup ()
236 {
237         /* Discard any old connections to bundles */
238         
239         for (std::list<sigc::connection>::iterator i = _bundle_connections.begin(); i != _bundle_connections.end(); ++i) {
240                 i->disconnect ();
241         }
242         _bundle_connections.clear ();
243
244         /* Connect to bundles so that we find out when their names change */
245         
246         ARDOUR::BundleList r = _matrix->rows()->bundles ();
247         for (ARDOUR::BundleList::iterator i = r.begin(); i != r.end(); ++i) {
248                 
249                 _bundle_connections.push_back (
250                         (*i)->NameChanged.connect (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_row_labels))
251                         );
252                 
253         }
254
255         ARDOUR::BundleList c = _matrix->columns()->bundles ();
256         for (ARDOUR::BundleList::iterator i = c.begin(); i != c.end(); ++i) {
257                 _bundle_connections.push_back (
258                         (*i)->NameChanged.connect (sigc::mem_fun (*this, &PortMatrixBody::rebuild_and_draw_column_labels))
259                         );
260         }
261         
262         _column_labels.setup ();
263         _row_labels.setup ();
264         _grid.setup ();
265
266         set_mouseover (
267                 PortMatrixNode (
268                         ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0),
269                         ARDOUR::BundleChannel (boost::shared_ptr<ARDOUR::Bundle> (), 0)
270                         )
271                 );
272
273         compute_rectangles ();
274 }
275
276 uint32_t
277 PortMatrixBody::full_scroll_width ()
278 {
279         return _grid.dimensions().first;
280
281 }
282
283 uint32_t
284 PortMatrixBody::alloc_scroll_width ()
285 {
286         return _grid.parent_rectangle().get_width();
287 }
288
289 uint32_t
290 PortMatrixBody::full_scroll_height ()
291 {
292         return _grid.dimensions().second;
293 }
294
295 uint32_t
296 PortMatrixBody::alloc_scroll_height ()
297 {
298         return _grid.parent_rectangle().get_height();
299 }
300
301 void
302 PortMatrixBody::set_xoffset (uint32_t xo)
303 {
304         _xoffset = xo;
305         queue_draw ();
306 }
307
308 void
309 PortMatrixBody::set_yoffset (uint32_t yo)
310 {
311         _yoffset = yo;
312         queue_draw ();
313 }
314
315 bool
316 PortMatrixBody::on_button_press_event (GdkEventButton* ev)
317 {
318         if (Gdk::Region (_grid.parent_rectangle()).point_in (ev->x, ev->y)) {
319
320                 _grid.button_press (
321                         _grid.parent_to_component_x (ev->x),
322                         _grid.parent_to_component_y (ev->y),
323                         ev->button
324                         );
325
326         } else if (Gdk::Region (_row_labels.parent_rectangle()).point_in (ev->x, ev->y)) {
327
328                 _row_labels.button_press (
329                         _row_labels.parent_to_component_x (ev->x),
330                         _row_labels.parent_to_component_y (ev->y),
331                         ev->button, ev->time
332                         );
333         
334         } else if (Gdk::Region (_column_labels.parent_rectangle()).point_in (ev->x, ev->y)) {
335
336                 _column_labels.button_press (
337                         _column_labels.parent_to_component_x (ev->x),
338                         _column_labels.parent_to_component_y (ev->y),
339                         ev->button, ev->time
340                         );
341         }
342
343         return true;
344 }
345
346 void
347 PortMatrixBody::rebuild_and_draw_grid ()
348 {
349         _grid.require_rebuild ();
350         queue_draw ();
351 }
352
353 void
354 PortMatrixBody::rebuild_and_draw_column_labels ()
355 {
356         _column_labels.require_rebuild ();
357         queue_draw ();
358 }
359
360 void
361 PortMatrixBody::rebuild_and_draw_row_labels ()
362 {
363         _row_labels.require_rebuild ();
364         queue_draw ();
365 }
366
367 bool
368 PortMatrixBody::on_enter_notify_event (GdkEventCrossing* ev)
369 {
370         if (ev->type == GDK_ENTER_NOTIFY) {
371                 _pointer_inside = true;
372         } else if (ev->type == GDK_LEAVE_NOTIFY) {
373                 _pointer_inside = false;
374         }
375
376         return true;
377 }
378
379 bool
380 PortMatrixBody::on_motion_notify_event (GdkEventMotion* ev)
381 {
382         if (_pointer_inside && Gdk::Region (_grid.parent_rectangle()).point_in (ev->x, ev->y)) {
383                 _grid.mouseover_event (
384                         _grid.parent_to_component_x (ev->x),
385                         _grid.parent_to_component_y (ev->y)
386                         );
387         }
388
389         return true;
390 }
391
392 void
393 PortMatrixBody::set_mouseover (PortMatrixNode const & n)
394 {
395         if (n == _mouseover) {
396                 return;
397         }
398
399         PortMatrixNode old = _mouseover;
400         _mouseover = n;
401         
402         _grid.mouseover_changed (old);
403         _row_labels.mouseover_changed (old);
404         _column_labels.mouseover_changed (old);
405 }