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