First cut at mouseovers for the port matrix.
[ardour.git] / gtk2_ardour / port_matrix_row_labels.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 <boost/weak_ptr.hpp>
22 #include <gtkmm/menu.h>
23 #include <gtkmm/menushell.h>
24 #include <gtkmm/menu_elems.h>
25 #include <cairo/cairo.h>
26 #include "ardour/bundle.h"
27 #include "port_matrix_row_labels.h"
28 #include "port_matrix.h"
29 #include "i18n.h"
30
31 PortMatrixRowLabels::PortMatrixRowLabels (PortMatrix* p, PortMatrixBody* b, Location l)
32         : PortMatrixComponent (b), _port_matrix (p), _menu (0), _location (l)
33 {
34         
35 }
36
37 PortMatrixRowLabels::~PortMatrixRowLabels ()
38 {
39         delete _menu;
40 }
41
42 void
43 PortMatrixRowLabels::compute_dimensions ()
44 {
45         GdkPixmap* pm = gdk_pixmap_new (NULL, 1, 1, 24);
46         gdk_drawable_set_colormap (pm, gdk_colormap_get_system());
47         cairo_t* cr = gdk_cairo_create (pm);
48         
49         _longest_port_name = 0;
50         _longest_bundle_name = 0;
51         _height = 0;
52         ARDOUR::BundleList const r = _body->row_ports().bundles();
53         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
54                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
55                         cairo_text_extents_t ext;
56                         cairo_text_extents (cr, (*i)->channel_name(j).c_str(), &ext);
57                         if (ext.width > _longest_port_name) {
58                                 _longest_port_name = ext.width;
59                         }
60                 }
61
62                 cairo_text_extents_t ext;
63                 cairo_text_extents (cr, (*i)->name().c_str(), &ext);
64                 if (ext.width > _longest_bundle_name) {
65                         _longest_bundle_name = ext.width;
66                 }
67
68                 _height += (*i)->nchannels() * row_height();
69         }
70
71         _highest_group_name = 0;
72         for (PortGroupList::const_iterator i = _body->row_ports().begin(); i != _body->row_ports().end(); ++i) {
73                 if ((*i)->visible()) {
74                         cairo_text_extents_t ext;
75                         cairo_text_extents (cr, (*i)->name.c_str(), &ext);
76                         if (ext.height > _highest_group_name) {
77                                 _highest_group_name = ext.height;
78                         }
79                 }
80         }
81                         
82         cairo_destroy (cr);
83         gdk_pixmap_unref (pm);
84
85         _width = _highest_group_name +
86                 _longest_port_name +
87                 _longest_bundle_name +
88                 name_pad() * 6;
89 }
90
91
92 void
93 PortMatrixRowLabels::render (cairo_t* cr)
94 {
95         /* BACKGROUND */
96         
97         set_source_rgb (cr, background_colour());
98         cairo_rectangle (cr, 0, 0, _width, _height);
99         cairo_fill (cr);
100
101         /* PORT GROUP NAMES */
102
103         double x = 0;
104         if (_location == LEFT) {
105                 x = 0;
106         } else if (_location == RIGHT) {
107                 x = _width - _highest_group_name - 2 * name_pad();
108         }
109
110         double y = 0;
111         int g = 0;
112         for (PortGroupList::const_iterator i = _body->row_ports().begin(); i != _body->row_ports().end(); ++i) {
113
114                 if (!(*i)->visible() || ((*i)->bundles().empty() && (*i)->ports.empty()) ) {
115                         continue;
116                 }
117                         
118                 /* compute height of this group */
119                 double h = 0;
120                 for (ARDOUR::BundleList::const_iterator j = (*i)->bundles().begin(); j != (*i)->bundles().end(); ++j) {
121                         h += (*j)->nchannels() * row_height();
122                 }
123                 h += (*i)->ports.size() * row_height();
124
125                 /* rectangle */
126                 set_source_rgb (cr, get_a_group_colour (g));
127                 double const rw = _highest_group_name + 2 * name_pad();
128                 cairo_rectangle (cr, x, y, rw, h);
129                 cairo_fill (cr);
130                     
131                 /* hence what abbreviation (or not) we need for the group name */
132                 std::pair<std::string, double> display = display_port_name (cr, (*i)->name, h);
133
134                 /* plot it */
135                 set_source_rgb (cr, text_colour());
136                 cairo_move_to (cr, x + rw - name_pad(), y + (h + display.second) / 2);
137                 cairo_save (cr);
138                 cairo_rotate (cr, - M_PI / 2);
139                 cairo_show_text (cr, display.first.c_str());
140                 cairo_restore (cr);
141
142                 y += h;
143                 ++g;
144         }
145
146         /* BUNDLE NAMES */
147
148         x = 0;
149         if (_location == LEFT) {
150                 x = _highest_group_name + 2 * name_pad();
151         } else if (_location == RIGHT) {
152                 x = _longest_port_name + name_pad() * 2;
153         }
154
155         y = 0;
156         ARDOUR::BundleList const r = _body->row_ports().bundles();
157         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
158
159                 Gdk::Color const colour = get_a_bundle_colour (i - r.begin ());
160                 set_source_rgb (cr, colour);
161                 cairo_rectangle (cr, x, y, _longest_bundle_name + name_pad() * 2, row_height() * (*i)->nchannels());
162                 cairo_fill_preserve (cr);
163                 set_source_rgb (cr, background_colour());
164                 cairo_set_line_width (cr, label_border_width ());
165                 cairo_stroke (cr);
166
167                 double off = 0;
168                 if ((*i)->nchannels () > 0) {
169                         /* use the extent of our first channel name so that the bundle name is vertically aligned with it */
170                         cairo_text_extents_t ext;
171                         cairo_text_extents (cr, (*i)->channel_name(0).c_str(), &ext);
172                         off = (row_height() - ext.height) / 2;
173                 } else {
174                         off = row_height() / 2;
175                 }
176
177                 set_source_rgb (cr, text_colour());
178                 cairo_move_to (cr, x + name_pad(), y + name_pad() + off);
179                 cairo_show_text (cr, (*i)->name().c_str());
180                 
181                 y += row_height() * (*i)->nchannels ();
182         }
183         
184
185         /* PORT NAMES */
186
187         y = 0;
188         for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
189                 for (uint32_t j = 0; j < (*i)->nchannels(); ++j) {
190                         render_port_name (cr, get_a_bundle_colour (i - r.begin()), port_name_x(), y, PortMatrixBundleChannel (*i, j));
191                         y += row_height();
192                 }
193         }
194 }
195
196 void
197 PortMatrixRowLabels::button_press (double x, double y, int b, uint32_t t)
198 {
199         if (b != 3) {
200                 return;
201         }
202
203         if ( (_location ==  LEFT && x > (_longest_bundle_name + name_pad() * 2)) ||
204              (_location == RIGHT && x < (_longest_port_name + name_pad() * 2))
205                 ) {
206
207                 delete _menu;
208                 
209                 _menu = new Gtk::Menu;
210                 _menu->set_name ("ArdourContextMenu");
211                 
212                 Gtk::Menu_Helpers::MenuList& items = _menu->items ();
213                 
214                 uint32_t row = y / row_height ();
215
216                 boost::shared_ptr<ARDOUR::Bundle> bundle;
217                 uint32_t channel = 0;
218
219                 ARDOUR::BundleList const r = _body->row_ports().bundles();
220                 for (ARDOUR::BundleList::const_iterator i = r.begin(); i != r.end(); ++i) {
221                         if (row < (*i)->nchannels ()) {
222                                 bundle = *i;
223                                 channel = row;
224                                 break;
225                         } else {
226                                 row -= (*i)->nchannels ();
227                         }
228                 }
229
230                 if (bundle) {
231                         char buf [64];
232
233                         if (_port_matrix->can_rename_channels ()) {
234                                 snprintf (buf, sizeof (buf), _("Rename '%s'..."), bundle->channel_name (channel).c_str());
235                                 items.push_back (
236                                         Gtk::Menu_Helpers::MenuElem (
237                                                 buf,
238                                                 sigc::bind (sigc::mem_fun (*this, &PortMatrixRowLabels::rename_channel_proxy), bundle, channel)
239                                                 )
240                                         );
241                         }
242                         
243                         snprintf (buf, sizeof (buf), _("Remove '%s'"), bundle->channel_name (channel).c_str());
244                         items.push_back (
245                                 Gtk::Menu_Helpers::MenuElem (
246                                         buf,
247                                         sigc::bind (sigc::mem_fun (*this, &PortMatrixRowLabels::remove_channel_proxy), bundle, channel)
248                                         )
249                                 );
250
251                         _menu->popup (1, t);
252                 }
253         }
254 }
255
256
257 void
258 PortMatrixRowLabels::remove_channel_proxy (boost::weak_ptr<ARDOUR::Bundle> b, uint32_t c)
259 {
260         boost::shared_ptr<ARDOUR::Bundle> sb = b.lock ();
261         if (!sb) {
262                 return;
263         }
264
265         _port_matrix->remove_channel (sb, c);
266
267 }
268
269 void
270 PortMatrixRowLabels::rename_channel_proxy (boost::weak_ptr<ARDOUR::Bundle> b, uint32_t c)
271 {
272         boost::shared_ptr<ARDOUR::Bundle> sb = b.lock ();
273         if (!sb) {
274                 return;
275         }
276
277         _port_matrix->rename_channel (sb, c);
278 }
279
280
281 double
282 PortMatrixRowLabels::component_to_parent_x (double x) const
283 {
284         return x + _parent_rectangle.get_x();
285 }
286
287 double
288 PortMatrixRowLabels::parent_to_component_x (double x) const
289 {
290         return x - _parent_rectangle.get_x();
291 }
292
293 double
294 PortMatrixRowLabels::component_to_parent_y (double y) const
295 {
296         return y - _body->yoffset() + _parent_rectangle.get_y();
297 }
298
299 double
300 PortMatrixRowLabels::parent_to_component_y (double y) const
301 {
302         return y + _body->yoffset() - _parent_rectangle.get_y();
303 }
304
305 double
306 PortMatrixRowLabels::port_name_x () const
307 {
308         if (_location == LEFT) {
309                 return _longest_bundle_name + _highest_group_name + name_pad() * 4;
310         } else if (_location == RIGHT) {
311                 return 0;
312         }
313
314         return 0;
315 }
316
317 void
318 PortMatrixRowLabels::render_port_name (
319         cairo_t* cr, Gdk::Color colour, double x, double y, PortMatrixBundleChannel const& bc
320         )
321 {
322         set_source_rgb (cr, colour);
323         cairo_rectangle (cr, x, y, _longest_port_name + name_pad() * 2, row_height());
324         cairo_fill_preserve (cr);
325         set_source_rgb (cr, background_colour());
326         cairo_set_line_width (cr, label_border_width ());
327         cairo_stroke (cr);
328         
329         cairo_text_extents_t ext;
330         cairo_text_extents (cr, bc.bundle->channel_name(bc.channel).c_str(), &ext);
331         double const off = (row_height() - ext.height) / 2;
332         
333         set_source_rgb (cr, text_colour());
334         cairo_move_to (cr, x + name_pad(), y + name_pad() + off);
335         cairo_show_text (cr, bc.bundle->channel_name(bc.channel).c_str());
336 }
337
338 double
339 PortMatrixRowLabels::channel_y (PortMatrixBundleChannel const& bc) const
340 {
341         double y = 0;
342
343         ARDOUR::BundleList::const_iterator i = _body->row_ports().bundles().begin();
344         while (i != _body->row_ports().bundles().end() && *i != bc.bundle) {
345                 y += row_height() * (*i)->nchannels();
346                 ++i;
347         }
348
349         y += row_height() * bc.channel;
350
351         return y;
352 }
353
354 void
355 PortMatrixRowLabels::queue_draw_for (PortMatrixNode const& n)
356 {
357         if (n.row.bundle) {
358
359                 _body->queue_draw_area (
360                         component_to_parent_x (port_name_x()),
361                         component_to_parent_y (channel_y (n.row)),
362                         _longest_port_name + name_pad() * 2,
363                         row_height()
364                         );
365         }
366
367 }
368
369 void
370 PortMatrixRowLabels::mouseover_changed (PortMatrixNode const& old)
371 {
372         queue_draw_for (old);
373         queue_draw_for (_body->mouseover());
374 }
375
376 void
377 PortMatrixRowLabels::draw_extra (cairo_t* cr)
378 {
379         if (_body->mouseover().row.bundle) {
380                 render_port_name (
381                         cr,
382                         mouseover_port_colour (),
383                         component_to_parent_x (port_name_x()),
384                         component_to_parent_y (channel_y (_body->mouseover().row)),
385                         _body->mouseover().row
386                         );
387         }
388 }