bump required version of GTK, SLV2; basic fixups so that the new port-matrix io-selec...
[ardour.git] / gtk2_ardour / port_matrix.cc
1 /*
2     Copyright (C) 2002-2007 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 <gtkmm/label.h>
21 #include <gtkmm/enums.h>
22 #include <gtkmm/image.h>
23 #include <gtkmm/stock.h>
24 #include <gtkmm/messagedialog.h>
25 #include <gtkmm/menu.h>
26 #include <gtkmm/menu_elems.h>
27 #include <gtkmm/menuitem.h>
28 #include <gtkmm/menushell.h>
29 #include <glibmm/objectbase.h>
30 #include <gtkmm2ext/doi.h>
31 #include <ardour/port_insert.h>
32 #include "ardour/session.h"
33 #include "ardour/io.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/track.h"
36 #include "ardour/audio_track.h"
37 #include "ardour/midi_track.h"
38 #include "ardour/data_type.h"
39 #include "io_selector.h"
40 #include "keyboard.h"
41 #include "utils.h"
42 #include "gui_thread.h"
43 #include "i18n.h"
44
45 using namespace Gtk;
46
47 /** Add a port to a group.
48  *  @param p Port name, with or without prefix.
49  */
50
51 void
52 PortGroup::add (std::string const & p)
53 {
54         if (prefix.empty() == false && p.substr (0, prefix.length()) == prefix) {
55                 ports.push_back (p.substr (prefix.length()));
56         } else {
57                 ports.push_back (p);
58         }
59 }
60
61 /** PortGroupUI constructor.
62  *  @param m PortMatrix to work for.
63  *  @Param g PortGroup to represent.
64  */
65
66 PortGroupUI::PortGroupUI (PortMatrix& m, PortGroup& g)
67         : _port_matrix (m)
68         , _port_group (g)
69         , _ignore_check_button_toggle (false)
70         , _visibility_checkbutton (g.name)
71 {
72         _port_group.visible = true;
73         _ignore_check_button_toggle = false;
74         _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
75 }
76
77 /** The visibility of a PortGroupUI has been toggled */
78 void
79 PortGroupUI::visibility_checkbutton_toggled ()
80 {
81         _port_group.visible = _visibility_checkbutton.get_active ();
82 }
83
84 /** @return Checkbutton used to toggle visibility */
85 Widget&
86 PortGroupUI::get_visibility_checkbutton ()
87 {
88         return _visibility_checkbutton;
89 }
90
91
92 /** Handle a toggle of a port check button */
93 void
94 PortGroupUI::port_checkbutton_toggled (CheckButton* b, int r, int c)
95 {
96         if (_ignore_check_button_toggle == false) {
97                 // _port_matrix.hide_group (_port_group);
98         }
99 }
100
101 /** Set up visibility of the port group according to PortGroup::visible */
102 void
103 PortGroupUI::setup_visibility ()
104 {
105         if (_visibility_checkbutton.get_active () != _port_group.visible) {
106                 _visibility_checkbutton.set_active (_port_group.visible);
107         }
108 }
109
110 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
111         : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type)
112 {
113         _row_labels_vbox = 0;
114         _side_vbox_pad = 0;
115
116         _visibility_checkbutton_box.pack_start (*(manage (new Label (_("Connections displayed: ")))), false, false, 10);
117         pack_start (_visibility_checkbutton_box, false, false);
118
119         _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_AUTOMATIC);
120         _scrolled_window.set_shadow_type (SHADOW_NONE);
121
122         VBox* b = manage (new VBox);
123
124         b->pack_start (_port_group_hbox, false, false);
125         b->pack_start (_port_group_hbox, false, false);
126
127         _scrolled_window.add (matrix);
128
129         if (offer_inputs) {
130                 _overall_hbox.pack_start (_side_vbox, false, false, 6);
131                 _overall_hbox.pack_start (_scrolled_window, true, true);
132         } else {
133                 _overall_hbox.pack_start (_scrolled_window, true, true, 6);
134                 _overall_hbox.pack_start (_side_vbox, false, false);
135         }
136
137         pack_start (_overall_hbox);
138
139         _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
140 }
141
142 PortMatrix::~PortMatrix ()
143 {
144         clear ();
145 }
146
147 void 
148 PortMatrix::set_ports (const std::list<std::string>& ports)
149 {
150         matrix.set_ports (ports);
151 }
152
153 /** Clear out the things that change when the number of source or destination ports changes */
154 void
155 PortMatrix::clear ()
156 {
157         for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
158                 delete *j;
159         }
160         _row_labels.clear ();
161                 
162         if (_row_labels_vbox) {
163                 _side_vbox.remove (*_row_labels_vbox);
164                 delete _row_labels_vbox;
165                 _row_labels_vbox = 0;
166         }
167         
168         /* remove lurking, invisible label and padding */
169         
170         _side_vbox.children().clear ();
171
172         if (_side_vbox_pad) {
173                 delete _side_vbox_pad;
174                 _side_vbox_pad = 0;
175         }
176
177         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
178                 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
179                 delete *i;
180         }
181
182         _port_group_ui.clear ();
183 }
184
185
186 /** Set up dimensions of some of our widgets which depend on other dimensions
187  *  within the dialogue.
188  */
189 void
190 PortMatrix::setup_dimensions ()
191 {
192         /* Row labels */
193         for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
194                 (*j)->get_child()->set_size_request (-1, matrix.row_spacing());
195         }
196         
197         if (_side_vbox_pad) {
198                 if (_offer_inputs) {
199                         _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
200                 } else {
201                         _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
202                 }
203         } 
204 }
205
206
207 /** Set up the dialogue */
208 void
209 PortMatrix::setup ()
210 {
211         clear ();
212
213         int const rows = n_rows ();
214         
215         /* Row labels */
216
217         _row_labels_vbox = new VBox;
218         int const run_rows = std::max (1, rows);
219
220         for (int j = 0; j < run_rows; ++j) {
221                 
222                 /* embolden the port/channel name */
223                 
224                 string s = "<b>";
225                 s += row_name (j);
226                 s += "</b>";
227                 
228                 Label* label = manage (new Label (s));
229                 EventBox* b = manage (new EventBox);
230                 
231                 label->set_use_markup (true);
232                 
233                 b->set_events (Gdk::BUTTON_PRESS_MASK);
234                 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
235                 b->add (*label);
236                 
237                 _row_labels.push_back (b);
238                 _row_labels_vbox->pack_start (*b, false, false);
239         }
240
241         _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
242
243         if (_offer_inputs) {
244                 _side_vbox.pack_start (*_side_vbox_pad, false, false);
245                 _side_vbox.pack_start (*_row_labels_vbox, false, false);
246                 _side_vbox.pack_start (*manage (new Label ("")));
247         } else {
248                 _side_vbox.pack_start (*manage (new Label ("")));
249                 _side_vbox.pack_start (*_row_labels_vbox, false, false);
250                 _side_vbox.pack_start (*_side_vbox_pad, false, false);
251         }
252
253         matrix.clear ();
254
255         /* Checkbutton tables and visibility checkbuttons */
256         for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
257
258                 PortGroupUI* t = new PortGroupUI (*this, **i);
259                 
260                 _port_group_ui.push_back (t);
261
262                 matrix.add_group (**i);
263
264                 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
265
266                 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
267
268                 if (chk) { 
269                         chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
270                 }
271         }
272
273         show_all ();
274
275         reset_visibility ();
276 }
277
278 void
279 PortMatrix::reset_visibility ()
280 {
281         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
282
283                 (*i)->setup_visibility ();
284                 
285                 if ((*i)->port_group().visible) {
286                         matrix.show_group ((*i)->port_group());
287                 } else {
288                         matrix.hide_group ((*i)->port_group());
289                 }
290         }
291 }
292
293 void
294 PortMatrix::redisplay ()
295 {
296         _port_group_list.refresh ();
297         setup ();
298 }
299
300
301 /** Handle a button press on a row label */
302 bool
303 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
304 {
305         if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
306                 return false;
307         }
308
309         Menu* menu = manage (new Menu);
310         Menu_Helpers::MenuList& items = menu->items ();
311         menu->set_name ("ArdourContextMenu");
312
313         bool const can_add = maximum_rows () > n_rows ();
314         bool const can_remove = minimum_rows () < n_rows ();
315         std::string const name = row_name (r);
316         
317         items.push_back (
318                 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
319                 );
320
321         items.back().set_sensitive (can_add);
322
323         items.push_back (
324                 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
325                 );
326
327         items.back().set_sensitive (can_remove);
328
329         menu->popup (e->button, e->time);
330         
331         return true;
332 }
333
334 void
335 PortMatrix::set_type (ARDOUR::DataType t)
336 {
337         _type = t;
338         _port_group_list.set_type (t);
339         redisplay ();
340 }
341
342 void
343 PortMatrix::set_offer_inputs (bool i)
344 {
345         _offer_inputs = i;
346         _port_group_list.set_offer_inputs (i);
347         redisplay ();
348 }
349
350 /** PortGroupList constructor.
351  *  @param session Session to get ports from.
352  *  @param type Type of ports to offer (audio or MIDI)
353  *  @param offer_inputs true to offer output ports, otherwise false.
354  *  @param mask Mask of groups to make visible by default.
355  */
356
357 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
358         : _session (session), _type (type), _offer_inputs (offer_inputs),
359           buss (_("Bus"), "ardour:", mask & BUSS),
360           track (_("Track"), "ardour:", mask & TRACK),
361           system (_("System"), "system:", mask & SYSTEM),
362           other (_("Other"), "", mask & OTHER)
363 {
364         refresh ();
365 }
366
367 void
368 PortGroupList::refresh ()
369 {
370         clear ();
371         
372         buss.ports.clear ();
373         track.ports.clear ();
374         system.ports.clear ();
375         other.ports.clear ();
376
377         /* Find the ports provided by ardour; we can't derive their type just from their
378            names, so we'll have to be more devious. 
379         */
380
381         boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
382
383         cerr << "Looking for arour routes\n";
384
385         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
386
387                 PortGroup* g = 0;
388
389                 if (_type == ARDOUR::DataType::AUDIO) {
390
391                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
392                                 g = &track;
393                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
394                                 g = &buss;
395                         } 
396
397
398                 } else if (_type == ARDOUR::DataType::MIDI) {
399
400                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
401                                 g = &track;
402                         }
403
404                         /* No MIDI busses yet */
405                 } 
406                         
407                 if (g) {
408                         ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
409                         for (uint32_t j = 0; j < p.num_ports(); ++j) {
410                                 g->add (p.port(j)->name ());
411                         }
412
413                         std::sort (g->ports.begin(), g->ports.end());
414                 }
415         }
416         
417         /* XXX: inserts, sends, plugin inserts? */
418         
419         /* Now we need to find the non-ardour ports; we do this by first
420            finding all the ports that we can connect to. 
421         */
422
423         cerr << "Looking for non-ardour ports\n";
424
425         const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ? 
426                                                           JackPortIsInput : JackPortIsOutput);
427         if (ports) {
428
429                 int n = 0;
430                 string client_matching_string;
431
432                 cerr << "Got some\n";
433
434                 client_matching_string = _session.engine().client_name();
435                 client_matching_string += ':';
436
437                 while (ports[n]) {
438                         std::string const p = ports[n];
439
440                         if (p.substr(0, strlen ("system:")) == "system:") {
441                                 /* system: prefix */
442                                 system.add (p);
443                         } else {
444                                 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
445                                         /* other (non-ardour) prefix */
446                                         other.add (p);
447                                 }
448                         }
449
450                         ++n;
451                 }
452
453                 free (ports);
454         }
455
456         cerr << "at end of refresh, we have " << buss.ports.size () << " buss\n";
457         cerr << "at end of refresh, we have " << track.ports.size () << " track\n";
458         cerr << "at end of refresh, we have " << system.ports.size () << " system\n";
459         cerr << "at end of refresh, we have " << other.ports.size () << " other\n";
460
461         push_back (&system);
462         push_back (&buss);
463         push_back (&track);
464         push_back (&other);
465 }
466
467 int
468 PortGroupList::n_visible_ports () const
469 {
470         int n = 0;
471         
472         for (const_iterator i = begin(); i != end(); ++i) {
473                 if ((*i)->visible) {
474                         n += (*i)->ports.size();
475                 }
476         }
477
478         return n;
479 }
480
481 std::string
482 PortGroupList::get_port_by_index (int n, bool with_prefix) const
483 {
484         /* XXX: slightly inefficient algorithm */
485
486         for (const_iterator i = begin(); i != end(); ++i) {
487                 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
488                         if (n == 0) {
489                                 if (with_prefix) {
490                                         return (*i)->prefix + *j;
491                                 } else {
492                                         return *j;
493                                 }
494                         }
495                         --n;
496                 }
497         }
498
499         return "";
500 }
501
502 void
503 PortGroupList::set_type (ARDOUR::DataType t)
504 {
505         _type = t;
506 }
507
508 void
509 PortGroupList::set_offer_inputs (bool i)
510 {
511         _offer_inputs = i;
512 }
513