Make MIDI selection actually show up.
[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         delete _side_vbox_pad;
173         _side_vbox_pad = 0;
174
175         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
176                 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
177                 delete *i;
178         }
179
180         _port_group_ui.clear ();
181 }
182
183
184 /** Set up dimensions of some of our widgets which depend on other dimensions
185  *  within the dialogue.
186  */
187 void
188 PortMatrix::setup_dimensions ()
189 {
190         /* Row labels */
191         for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
192                 (*j)->get_child()->set_size_request (-1, matrix.row_spacing());
193         }
194         
195         if (_side_vbox_pad) {
196                 if (_offer_inputs) {
197                         _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
198                 } else {
199                         _side_vbox_pad->set_size_request (-1, matrix.row_spacing() / 4);
200                 }
201         } 
202 }
203
204
205 /** Set up the dialogue */
206 void
207 PortMatrix::setup ()
208 {
209         clear ();
210
211         int const rows = n_rows ();
212         
213         /* Row labels */
214
215         _row_labels_vbox = new VBox;
216         int const run_rows = std::max (1, rows);
217
218         for (int j = 0; j < run_rows; ++j) {
219                 
220                 /* embolden the port/channel name */
221                 
222                 string s = "<b>";
223                 s += row_name (j);
224                 s += "</b>";
225                 
226                 Label* label = manage (new Label (s));
227                 EventBox* b = manage (new EventBox);
228                 
229                 label->set_use_markup (true);
230                 
231                 b->set_events (Gdk::BUTTON_PRESS_MASK);
232                 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
233                 b->add (*label);
234                 
235                 _row_labels.push_back (b);
236                 _row_labels_vbox->pack_start (*b, false, false);
237         }
238
239         _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
240
241         if (_offer_inputs) {
242                 _side_vbox.pack_start (*_side_vbox_pad, false, false);
243                 _side_vbox.pack_start (*_row_labels_vbox, false, false);
244                 _side_vbox.pack_start (*manage (new Label ("")));
245         } else {
246                 _side_vbox.pack_start (*manage (new Label ("")));
247                 _side_vbox.pack_start (*_row_labels_vbox, false, false);
248                 _side_vbox.pack_start (*_side_vbox_pad, false, false);
249         }
250
251         matrix.clear ();
252
253         /* Checkbutton tables and visibility checkbuttons */
254         for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
255
256                 PortGroupUI* t = new PortGroupUI (*this, **i);
257                 
258                 _port_group_ui.push_back (t);
259
260                 matrix.add_group (**i);
261
262                 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
263
264                 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
265
266                 if (chk) { 
267                         chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
268                 }
269         }
270
271         show_all ();
272
273         reset_visibility ();
274 }
275
276 void
277 PortMatrix::reset_visibility ()
278 {
279         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
280
281                 (*i)->setup_visibility ();
282                 
283                 if ((*i)->port_group().visible) {
284                         matrix.show_group ((*i)->port_group());
285                 } else {
286                         matrix.hide_group ((*i)->port_group());
287                 }
288         }
289 }
290
291 void
292 PortMatrix::redisplay ()
293 {
294         _port_group_list.refresh ();
295         setup ();
296 }
297
298
299 /** Handle a button press on a row label */
300 bool
301 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
302 {
303         if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
304                 return false;
305         }
306
307         Menu* menu = manage (new Menu);
308         Menu_Helpers::MenuList& items = menu->items ();
309         menu->set_name ("ArdourContextMenu");
310
311         bool const can_add = maximum_rows () > n_rows ();
312         bool const can_remove = minimum_rows () < n_rows ();
313         std::string const name = row_name (r);
314         
315         items.push_back (
316                 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
317                 );
318
319         items.back().set_sensitive (can_add);
320
321         items.push_back (
322                 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
323                 );
324
325         items.back().set_sensitive (can_remove);
326
327         menu->popup (e->button, e->time);
328         
329         return true;
330 }
331
332 void
333 PortMatrix::set_type (ARDOUR::DataType t)
334 {
335         _type = t;
336         _port_group_list.set_type (t);
337         redisplay ();
338 }
339
340 void
341 PortMatrix::set_offer_inputs (bool i)
342 {
343         _offer_inputs = i;
344         _port_group_list.set_offer_inputs (i);
345         redisplay ();
346 }
347
348 /** PortGroupList constructor.
349  *  @param session Session to get ports from.
350  *  @param type Type of ports to offer (audio or MIDI)
351  *  @param offer_inputs true to offer output ports, otherwise false.
352  *  @param mask Mask of groups to make visible by default.
353  */
354
355 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
356         : _session (session), _type (type), _offer_inputs (offer_inputs),
357           buss (_("Bus"), "ardour:", mask & BUSS),
358           track (_("Track"), "ardour:", mask & TRACK),
359           system (_("System"), "system:", mask & SYSTEM),
360           other (_("Other"), "", mask & OTHER)
361 {
362         refresh ();
363 }
364
365 void
366 PortGroupList::refresh ()
367 {
368         clear ();
369         
370         buss.ports.clear ();
371         track.ports.clear ();
372         system.ports.clear ();
373         other.ports.clear ();
374
375         /* Find the ports provided by ardour; we can't derive their type just from their
376            names, so we'll have to be more devious. 
377         */
378
379         boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
380
381         cerr << "Looking for arour routes\n";
382
383         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
384
385                 PortGroup* g = 0;
386
387                 if (_type == ARDOUR::DataType::AUDIO) {
388
389                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
390                                 g = &track;
391                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
392                                 g = &buss;
393                         } 
394
395
396                 } else if (_type == ARDOUR::DataType::MIDI) {
397
398                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
399                                 g = &track;
400                         }
401
402                         /* No MIDI busses yet */
403                 } 
404                         
405                 if (g) {
406                         ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
407                         for (uint32_t j = 0; j < p.num_ports(); ++j) {
408                                 g->add (p.port(j)->name ());
409                         }
410
411                         std::sort (g->ports.begin(), g->ports.end());
412                 }
413         }
414         
415         /* XXX: inserts, sends, plugin inserts? */
416         
417         /* Now we need to find the non-ardour ports; we do this by first
418            finding all the ports that we can connect to. 
419         */
420
421         cerr << "Looking for non-ardour ports\n";
422
423         const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ? 
424                                                           JackPortIsInput : JackPortIsOutput);
425         if (ports) {
426
427                 int n = 0;
428                 string client_matching_string;
429
430                 cerr << "Got some\n";
431
432                 client_matching_string = _session.engine().client_name();
433                 client_matching_string += ':';
434
435                 while (ports[n]) {
436                         std::string const p = ports[n];
437
438                         if (p.substr(0, strlen ("system:")) == "system:") {
439                                 /* system: prefix */
440                                 system.add (p);
441                         } else {
442                                 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
443                                         /* other (non-ardour) prefix */
444                                         other.add (p);
445                                 }
446                         }
447
448                         ++n;
449                 }
450
451                 free (ports);
452         }
453
454         cerr << "at end of refresh, we have " << buss.ports.size () << " buss\n";
455         cerr << "at end of refresh, we have " << track.ports.size () << " track\n";
456         cerr << "at end of refresh, we have " << system.ports.size () << " system\n";
457         cerr << "at end of refresh, we have " << other.ports.size () << " other\n";
458
459         push_back (&system);
460         push_back (&buss);
461         push_back (&track);
462         push_back (&other);
463 }
464
465 int
466 PortGroupList::n_visible_ports () const
467 {
468         int n = 0;
469         
470         for (const_iterator i = begin(); i != end(); ++i) {
471                 if ((*i)->visible) {
472                         n += (*i)->ports.size();
473                 }
474         }
475
476         return n;
477 }
478
479 std::string
480 PortGroupList::get_port_by_index (int n, bool with_prefix) const
481 {
482         /* XXX: slightly inefficient algorithm */
483
484         for (const_iterator i = begin(); i != end(); ++i) {
485                 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
486                         if (n == 0) {
487                                 if (with_prefix) {
488                                         return (*i)->prefix + *j;
489                                 } else {
490                                         return *j;
491                                 }
492                         }
493                         --n;
494                 }
495         }
496
497         return "";
498 }
499
500 void
501 PortGroupList::set_type (ARDOUR::DataType t)
502 {
503         _type = t;
504 }
505
506 void
507 PortGroupList::set_offer_inputs (bool i)
508 {
509         _offer_inputs = i;
510 }
511