Fix menu missing fit-tracks.
[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         int const ports = _port_group.ports.size();
73         int const rows = _port_matrix.n_rows ();
74
75         if (rows == 0 || ports == 0) {
76                 return;
77         }
78
79         /* Sort out the table and the checkbuttons inside it */
80         
81         _table.resize (rows, ports);
82         _port_checkbuttons.resize (rows);
83         for (int i = 0; i < rows; ++i) {
84                 _port_checkbuttons[i].resize (ports);
85         }
86
87
88         for (int i = 0; i < rows; ++i) {
89                 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
90                         CheckButton* b = new CheckButton;
91                         
92                         b->signal_toggled().connect (
93                                 sigc::bind (sigc::mem_fun (*this, &PortGroupUI::port_checkbutton_toggled), b, i, j));
94                         
95                         b->signal_button_release_event().connect (
96                                 sigc::bind (sigc::mem_fun (*this, &PortGroupUI::port_checkbutton_release), b, i, j), false);
97
98                         _port_checkbuttons[i][j] = b;
99
100                         cerr << this << " bind to " << &_port_checkbuttons << " via " << b 
101                              << endl;
102
103                         _table.attach (*b, j, j + 1, i, i + 1);
104                 }
105         }
106         
107         _table_box.add (_table);
108
109         _ignore_check_button_toggle = true;
110
111         /* Set the state of the check boxes according to current connections */
112         for (int i = 0; i < rows; ++i) {
113                 for (uint32_t j = 0; j < _port_group.ports.size(); ++j) {
114                         std::string const t = _port_group.prefix + _port_group.ports[j];
115                         bool const s = _port_matrix.get_state (i, t);
116                         _port_checkbuttons[i][j]->set_active (s);
117                         if (s) {
118                                 _port_group.visible = true;
119                         }
120                 }
121         }
122
123         _ignore_check_button_toggle = false;
124
125         _visibility_checkbutton.signal_toggled().connect (sigc::mem_fun (*this, &PortGroupUI::visibility_checkbutton_toggled));
126 }
127
128 /** The visibility of a PortGroupUI has been toggled */
129 void
130 PortGroupUI::visibility_checkbutton_toggled ()
131 {
132         _port_group.visible = _visibility_checkbutton.get_active ();
133 }
134
135 /** @return Width and height of a single checkbutton in a port group table */
136 std::pair<int, int>
137 PortGroupUI::unit_size () const
138 {
139         if (_port_checkbuttons.empty() || _port_checkbuttons[0].empty())
140         {
141                 return std::pair<int, int> (0, 0);
142         }
143
144         int r = 0;
145         /* We can't ask for row spacing unless there >1 rows, otherwise we get a warning */
146         if (_table.property_n_rows() > 1) {
147                 r = _table.get_row_spacing (0);
148         }
149
150         return std::make_pair (
151                 _port_checkbuttons[0][0]->get_width() + _table.get_col_spacing (0),
152                 _port_checkbuttons[0][0]->get_height() + r
153                 );
154 }
155
156 /** @return Table widget containing the port checkbuttons */
157 Widget&
158 PortGroupUI::get_table ()
159 {
160         return _table_box;
161 }
162
163 /** @return Checkbutton used to toggle visibility */
164 Widget&
165 PortGroupUI::get_visibility_checkbutton ()
166 {
167         return _visibility_checkbutton;
168 }
169
170
171 /** Handle a toggle of a port check button */
172 void
173 PortGroupUI::port_checkbutton_toggled (CheckButton* b, int r, int c)
174 {
175         if (_ignore_check_button_toggle == false) {
176                 _port_matrix.set_state (r, _port_group.prefix + _port_group.ports[c], b->get_active(), 0);
177         }
178 }
179
180 bool
181 PortGroupUI::port_checkbutton_release (GdkEventButton* ev, CheckButton* b, int r, int c)
182 {
183         cerr << this << " RELEASE on " << b << " state = " << ev->state << endl;
184
185         if (ev->state == 0) {
186                 /* let usual toggle handler take care of it */
187                 return false;
188         }
189
190         /* The fun starts here 
191          */
192
193         const size_t ports = _port_group.ports.size();
194         const size_t rows = _port_matrix.n_rows ();
195
196         if (rows == 0 || ports == 0) {
197                 return true;
198         }
199
200         /* For each port in the group, change the state of
201            the connection for the corresponding "connector" port.
202         */
203         
204         for (size_t j = c; j < ports; ++j) {
205
206                 /* we've got a port to connect, now lets find the thing to 
207                    connect it too ... (search "down" the rows)
208                 */
209
210                 cerr << "we're going to connect port " << j << " of " << ports << endl;
211
212                 for (size_t i = r; i < rows; ++i) {
213
214                         cerr << this << " going to connect to row " << i << " of " << rows << endl;
215                         cerr << "access [" << i << "][" << j << "]\n";
216                         cerr << " @ " << &_port_checkbuttons << endl;
217
218                         _port_checkbuttons[i][j]->set_active (!_port_checkbuttons[i][j]->get_active());
219
220                         /* next time, get at least as far as this port before looking
221                            for more.
222                         */
223
224                         r = i + 1;
225
226                         /* changed connection state, stop looking for more */
227
228                         break;
229                 }
230         }
231
232         return true;
233 }
234
235 /** Set up visibility of the port group according to PortGroup::visible */
236 void
237 PortGroupUI::setup_visibility ()
238 {
239         if (!_port_group.ports.empty() && _port_group.visible) {
240                 _table_box.show ();
241         } else {
242                 _table_box.hide ();
243         }
244
245         if (_visibility_checkbutton.get_active () != _port_group.visible) {
246
247                 _visibility_checkbutton.set_active (_port_group.visible);
248         }
249 }
250
251 RotatedLabelSet::RotatedLabelSet (PortGroupList& g)
252         : Glib::ObjectBase ("RotatedLabelSet"), Widget (), _port_group_list (g), _base_width (128)
253 {
254         set_flags (NO_WINDOW);
255         if (getenv ("AD_ANGLE") != 0) {
256                 set_angle (atoi (getenv ("AD_ANGLE")));
257         } else {
258                 set_angle (45);
259         }
260 }
261
262 RotatedLabelSet::~RotatedLabelSet ()
263 {
264         
265 }
266
267
268 /** Set the angle that the labels are drawn at.
269  * @param degrees New angle in degrees.
270  */
271
272 void
273 RotatedLabelSet::set_angle (int degrees)
274 {
275         _angle_degrees = degrees;
276         _angle_radians = M_PI * _angle_degrees / 180;
277
278         queue_resize ();
279 }
280
281 void
282 RotatedLabelSet::on_size_request (Requisition* requisition)
283 {
284         *requisition = Requisition ();
285
286         if (_pango_layout == 0) {
287                 return;
288         }
289
290         /* Our height is the highest label */
291         requisition->height = 0;
292         for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
293                 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
294                         std::pair<int, int> const d = setup_layout (*j);
295                         if (d.second > requisition->height) {
296                                 requisition->height = d.second;
297                         }
298                 }
299         }
300
301         /* And our width is the base plus the width of the last label */
302         requisition->width = _base_width;
303         int const n = _port_group_list.n_visible_ports ();
304         if (n > 0) {
305                 std::pair<int, int> const d = setup_layout (_port_group_list.get_port_by_index (n - 1, false));
306                 requisition->width += d.first;
307         }
308
309         cerr << "Labels will be " << requisition->width << " x " << requisition->height << endl;
310 }
311
312 void
313 RotatedLabelSet::on_size_allocate (Allocation& allocation)
314 {
315         set_allocation (allocation);
316
317         if (_gdk_window) {
318                 _gdk_window->move_resize (allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height());
319         }
320 }
321
322 void
323 RotatedLabelSet::on_realize ()
324 {
325         Widget::on_realize ();
326
327         Glib::RefPtr<Style> style = get_style ();
328
329         if (!_gdk_window) {
330                 GdkWindowAttr attributes;
331                 memset (&attributes, 0, sizeof (attributes));
332
333                 Allocation allocation = get_allocation ();
334                 attributes.x = allocation.get_x ();
335                 attributes.y = allocation.get_y ();
336                 attributes.width = allocation.get_width ();
337                 attributes.height = allocation.get_height ();
338
339                 attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK; 
340                 attributes.window_type = GDK_WINDOW_CHILD;
341                 attributes.wclass = GDK_INPUT_OUTPUT;
342
343                 _gdk_window = Gdk::Window::create (get_window (), &attributes, GDK_WA_X | GDK_WA_Y);
344                 unset_flags (NO_WINDOW);
345                 set_window (_gdk_window);
346
347                 _bg_colour = style->get_bg (STATE_NORMAL );
348                 modify_bg (STATE_NORMAL, _bg_colour);
349                 _fg_colour = style->get_fg (STATE_NORMAL);
350
351                 _gdk_window->set_user_data (gobj ());
352
353                 /* Set up Pango stuff */
354                 _pango_context = create_pango_context ();
355
356                 Pango::Matrix matrix = PANGO_MATRIX_INIT;
357                 pango_matrix_rotate (&matrix, _angle_degrees);
358                 _pango_context->set_matrix (matrix);
359
360                 _pango_layout = Pango::Layout::create (_pango_context);
361                 _gc = Gdk::GC::create (get_window ());
362         }
363 }
364
365 void
366 RotatedLabelSet::on_unrealize()
367 {
368         _gdk_window.clear ();
369
370         Widget::on_unrealize ();
371 }
372
373
374 /** Set up our Pango layout to plot a given string, and compute its dimensions once
375  *  it has been rotated.
376  *  @param s String to use.
377  *  @return width and height of the rotated string, in pixels.
378  */
379
380 std::pair<int, int>
381 RotatedLabelSet::setup_layout (std::string const & s)
382 {
383         _pango_layout->set_text (s);
384
385         /* Here's the unrotated size */
386         int w;
387         int h;
388         _pango_layout->get_pixel_size (w, h);
389
390         /* Rotate the width and height as appropriate.  I thought Pango might be able
391            to do this for us, but I can't find out how... 
392         */
393
394         std::pair<int, int> d;
395
396         // cerr << "\"" << s << "\" was " << w << " x " << h << endl;
397
398         d.first = int (fabs (w * cos (_angle_radians) + h * sin (_angle_radians)));
399         d.second = int (fabs (w * sin (_angle_radians) + h * cos (_angle_radians)));
400
401         // cerr << "\trotated by " << _angle_degrees << " = " << d.first << " x " << d.second << endl;
402
403         return d;
404 }
405
406 bool
407 RotatedLabelSet::on_expose_event (GdkEventExpose* event)
408 {
409         if (!_gdk_window) {
410                 return true;
411         }
412
413         int const height = get_allocation().get_height ();
414         double const spacing = double (_base_width) / _port_group_list.n_visible_ports();
415
416         /* Plot all the visible labels; really we should clip for efficiency */
417         int n = 0;
418         for (PortGroupList::const_iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
419                 if ((*i)->visible) {
420                         for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
421                                 std::pair<int, int> const d = setup_layout ((*i)->ports[j]);
422                                 int x, y;
423                                 if (getenv ("AD_X_SHIFT") != 0) {
424                                         x = atoi (getenv ("AD_X_SHIFT"));
425                                 } else {
426                                         x = 0;
427                                 }
428                                 if (getenv ("AD_Y_SHIFT") != 0) {
429                                         y = atoi (getenv ("AD_Y_SHIFT"));
430                                 } else {
431                                         y = 0;
432                                 }
433                                 get_window()->draw_layout (_gc, int ((n + 0.25) * spacing) + x, height - d.second + y, _pango_layout, _fg_colour, _bg_colour);
434                                 ++n;
435                         }
436                 }
437         }
438
439         return true;
440 }
441
442 /** Set the `base width'.  This is the width of the base of the label set, ie:
443  *
444  *     L L L L
445  *    E E E E
446  *   B B B B
447  *  A A A A
448  * L L L L
449  * <--w-->
450  */
451     
452 void
453 RotatedLabelSet::set_base_width (int w)
454 {
455         _base_width = w;
456         queue_resize ();
457 }
458
459
460 PortMatrix::PortMatrix (ARDOUR::Session& session, ARDOUR::DataType type, bool offer_inputs, PortGroupList::Mask mask)
461         : _offer_inputs (offer_inputs), _port_group_list (session, type, offer_inputs, mask), _type (type),
462           _column_labels (_port_group_list)
463 {
464         _row_labels_vbox = 0;
465         _side_vbox_pad = 0;
466
467         _visibility_checkbutton_box.pack_start (*(manage (new Label (_("Connections displayed: ")))), false, false, 10);
468         pack_start (_visibility_checkbutton_box, false, false);
469         
470         _scrolled_window.set_policy (POLICY_ALWAYS, POLICY_AUTOMATIC);
471         _scrolled_window.set_shadow_type (SHADOW_NONE);
472
473         VBox* b = manage (new VBox);
474
475         if (offer_inputs) {
476                 b->pack_start (_port_group_hbox, false, false);
477                 b->pack_start (_column_labels, false, false);
478         } else {
479                 b->pack_start (_column_labels, false, false);
480                 b->pack_start (_port_group_hbox, false, false);
481         }
482
483         Alignment* a; 
484
485         if (offer_inputs) {
486                 a = manage (new Alignment (1, 0, 0, 0));
487         } else {
488                 a = manage (new Alignment (0, 1, 0, 0));
489         }
490
491         a->add (*b);
492
493         _scrolled_window.add (*a);
494
495         if (offer_inputs) {
496                 _overall_hbox.pack_start (_side_vbox, false, false, 6);
497                 _overall_hbox.pack_start (_scrolled_window, true, true);
498         } else {
499                 _overall_hbox.pack_start (_scrolled_window, true, true, 6);
500                 _overall_hbox.pack_start (_side_vbox, false, false);
501         }
502
503         pack_start (_overall_hbox);
504
505         _port_group_hbox.signal_size_allocate().connect (sigc::hide (sigc::mem_fun (*this, &IOSelector::setup_dimensions)));
506 }
507
508 PortMatrix::~PortMatrix ()
509 {
510         clear ();
511 }
512
513 /** Clear out the things that change when the number of source or destination ports changes */
514 void
515 PortMatrix::clear ()
516 {
517         for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
518                 delete *j;
519         }
520         _row_labels.clear ();
521                 
522         if (_row_labels_vbox) {
523                 _side_vbox.remove (*_row_labels_vbox);
524                 delete _row_labels_vbox;
525                 _row_labels_vbox = 0;
526         }
527         
528         /* remove lurking, invisible label and padding */
529         
530         _side_vbox.children().clear ();
531
532         if (_side_vbox_pad) {
533                 delete _side_vbox_pad;
534                 _side_vbox_pad = 0;
535         }
536
537         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
538                 _port_group_hbox.remove ((*i)->get_table());
539                 _visibility_checkbutton_box.remove ((*i)->get_visibility_checkbutton());
540                 delete *i;
541         }
542
543         _port_group_ui.clear ();
544 }
545
546
547 /** Set up dimensions of some of our widgets which depend on other dimensions
548  *  within the dialogue.
549  */
550 void
551 PortMatrix::setup_dimensions ()
552 {
553         /* Get some dimensions from various places */
554         int const scrollbar_height = _scrolled_window.get_hscrollbar()->get_height();
555
556         std::pair<int, int> unit_size (0, 0);
557         int port_group_tables_height = 0;
558         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
559                 std::pair<int, int> const u = (*i)->unit_size ();
560                 unit_size.first = std::max (unit_size.first, u.first);
561                 unit_size.second = std::max (unit_size.second, u.second);
562                 port_group_tables_height = std::max (
563                         port_group_tables_height, (*i)->get_table().get_height()
564                         );
565         }
566
567         /* Column labels */
568         _column_labels.set_base_width (_port_group_list.n_visible_ports () * unit_size.first);
569
570         /* Scrolled window */
571         /* XXX: really shouldn't set a minimum horizontal size here, but if we don't
572            the window starts up very small.
573            The constant value in the set_size_request() computation will control
574            how big the scrolled window will be if we fill the port matrix will a gajillion
575            ports.
576         */
577
578         _scrolled_window.set_size_request (
579                 std::min (_column_labels.get_width(), 400),
580                 _column_labels.get_height() + port_group_tables_height + scrollbar_height + 16
581                 );
582         
583         /* Row labels */
584         for (std::vector<EventBox*>::iterator j = _row_labels.begin(); j != _row_labels.end(); ++j) {
585                 (*j)->get_child()->set_size_request (-1, unit_size.second);
586         }
587         
588         if (_side_vbox_pad) {
589                 if (_offer_inputs) {
590                         _side_vbox_pad->set_size_request (-1, unit_size.second / 4);
591                 } else {
592                         _side_vbox_pad->set_size_request (-1, scrollbar_height + unit_size.second / 4);
593                 }
594         } 
595 }
596
597
598 /** Set up the dialogue */
599 void
600 PortMatrix::setup ()
601 {
602         clear ();
603
604         int const rows = n_rows ();
605         
606         /* Row labels */
607
608         _row_labels_vbox = new VBox;
609         int const run_rows = std::max (1, rows);
610
611         for (int j = 0; j < run_rows; ++j) {
612                 
613                 /* embolden the port/channel name */
614                 
615                 string s = "<b>";
616                 s += row_name (j);
617                 s += "</b>";
618                 
619                 Label* label = manage (new Label (s));
620                 EventBox* b = manage (new EventBox);
621                 
622                 label->set_use_markup (true);
623                 
624                 b->set_events (Gdk::BUTTON_PRESS_MASK);
625                 b->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &IOSelector::row_label_button_pressed), j));
626                 b->add (*label);
627                 
628                 _row_labels.push_back (b);
629                 _row_labels_vbox->pack_start (*b, false, false);
630         }
631
632         _side_vbox_pad = new Label (""); /* unmanaged, explicitly deleted */
633
634         if (_offer_inputs) {
635                 _side_vbox.pack_start (*_side_vbox_pad, false, false);
636                 _side_vbox.pack_start (*_row_labels_vbox, false, false);
637                 _side_vbox.pack_start (*manage (new Label ("")));
638         } else {
639                 _side_vbox.pack_start (*manage (new Label ("")));
640                 _side_vbox.pack_start (*_row_labels_vbox, false, false);
641                 _side_vbox.pack_start (*_side_vbox_pad, false, false);
642         }
643
644         /* Checkbutton tables and visibility checkbuttons */
645         for (PortGroupList::iterator i = _port_group_list.begin(); i != _port_group_list.end(); ++i) {
646
647                 PortGroupUI* t = new PortGroupUI (*this, **i);
648                 
649                 _port_group_ui.push_back (t);
650                 _port_group_hbox.pack_start (t->get_table(), false, false);
651                 
652                 _visibility_checkbutton_box.pack_start (t->get_visibility_checkbutton(), false, false);
653
654                 CheckButton* chk = dynamic_cast<CheckButton*>(&t->get_visibility_checkbutton());
655
656                 if (chk) { 
657                         chk->signal_toggled().connect (sigc::mem_fun (*this, &PortMatrix::reset_visibility));
658                 }
659         }
660
661         show_all ();
662
663         reset_visibility ();
664 }
665
666 void
667 PortMatrix::reset_visibility ()
668 {
669         /* now adjust visibility and coloring */
670
671         bool even = true;
672         Gdk::Color odd_bg (color_from_style ("OddPortGroups", STATE_NORMAL, "fg"));
673         Gdk::Color even_bg (color_from_style ("EvenPortGroups", STATE_NORMAL, "fg"));
674
675         for (std::vector<PortGroupUI*>::iterator i = _port_group_ui.begin(); i != _port_group_ui.end(); ++i) {
676
677                 (*i)->setup_visibility ();
678                 
679                 if ((*i)->port_group().visible) {
680                         if (even) {
681                                 (*i)->get_table().modify_bg (STATE_NORMAL, even_bg);
682                         } else {
683                                 (*i)->get_table().modify_bg (STATE_NORMAL, odd_bg);
684                         }
685                         even = !even;
686                 }
687         }
688 }
689
690 void
691 PortMatrix::redisplay ()
692 {
693         _port_group_list.refresh ();
694         setup ();
695 }
696
697
698 /** Handle a button press on a row label */
699 bool
700 PortMatrix::row_label_button_pressed (GdkEventButton* e, int r)
701 {
702         if (e->type != GDK_BUTTON_PRESS || e->button != 3) {
703                 return false;
704         }
705
706         Menu* menu = manage (new Menu);
707         Menu_Helpers::MenuList& items = menu->items ();
708         menu->set_name ("ArdourContextMenu");
709
710         bool const can_add = maximum_rows () > n_rows ();
711         bool const can_remove = minimum_rows () < n_rows ();
712         std::string const name = row_name (r);
713         
714         items.push_back (
715                 Menu_Helpers::MenuElem (string_compose(_("Add %1"), row_descriptor()), sigc::mem_fun (*this, &PortMatrix::add_row))
716                 );
717
718         items.back().set_sensitive (can_add);
719
720         items.push_back (
721                 Menu_Helpers::MenuElem (string_compose(_("Remove %1 \"%2\""), row_descriptor(), name), sigc::bind (sigc::mem_fun (*this, &PortMatrix::remove_row), r))
722                 );
723
724         items.back().set_sensitive (can_remove);
725
726         menu->popup (e->button, e->time);
727         
728         return true;
729 }
730
731 void
732 PortMatrix::set_type (ARDOUR::DataType t)
733 {
734         _type = t;
735         _port_group_list.set_type (t);
736         redisplay ();
737 }
738
739 void
740 PortMatrix::set_offer_inputs (bool i)
741 {
742         _offer_inputs = i;
743         _port_group_list.set_offer_inputs (i);
744         redisplay ();
745 }
746
747 /** PortGroupList constructor.
748  *  @param session Session to get ports from.
749  *  @param type Type of ports to offer (audio or MIDI)
750  *  @param offer_inputs true to offer output ports, otherwise false.
751  *  @param mask Mask of groups to make visible by default.
752  */
753
754 PortGroupList::PortGroupList (ARDOUR::Session & session, ARDOUR::DataType type, bool offer_inputs, Mask mask)
755         : _session (session), _type (type), _offer_inputs (offer_inputs),
756           buss (_("Bus"), "ardour:", mask & BUSS),
757           track (_("Track"), "ardour:", mask & TRACK),
758           system (_("System"), "system:", mask & SYSTEM),
759           other (_("Other"), "", mask & OTHER)
760 {
761         refresh ();
762 }
763
764 void
765 PortGroupList::refresh ()
766 {
767         clear ();
768         
769         buss.ports.clear ();
770         track.ports.clear ();
771         system.ports.clear ();
772         other.ports.clear ();
773
774         /* Find the ports provided by ardour; we can't derive their type just from their
775            names, so we'll have to be more devious. 
776         */
777
778         boost::shared_ptr<ARDOUR::Session::RouteList> routes = _session.get_routes ();
779
780         for (ARDOUR::Session::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
781
782                 PortGroup* g = 0;
783
784                 if (_type == ARDOUR::DataType::AUDIO) {
785
786                         if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
787                                 g = &track;
788                         } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
789                                 g = &buss;
790                         } 
791
792
793                 } else if (_type == ARDOUR::DataType::MIDI) {
794
795                         if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
796                                 g = &track;
797                         }
798
799                         /* No MIDI busses yet */
800                 } 
801                         
802                 if (g) {
803                         ARDOUR::PortSet const & p = _offer_inputs ? ((*i)->inputs()) : ((*i)->outputs());
804                         for (uint32_t j = 0; j < p.num_ports(); ++j) {
805                                 g->add (p.port(j)->name ());
806                         }
807
808                         std::sort (g->ports.begin(), g->ports.end());
809                 }
810         }
811         
812         /* XXX: inserts, sends, plugin inserts? */
813         
814         /* Now we need to find the non-ardour ports; we do this by first
815            finding all the ports that we can connect to. 
816         */
817
818         const char **ports = _session.engine().get_ports ("", _type.to_jack_type(), _offer_inputs ? 
819                                                           JackPortIsInput : JackPortIsOutput);
820         if (ports) {
821
822                 int n = 0;
823                 string client_matching_string;
824
825                 client_matching_string = _session.engine().client_name();
826                 client_matching_string += ':';
827
828                 while (ports[n]) {
829                         std::string const p = ports[n];
830
831                         if (p.substr(0, strlen ("system:")) == "system:") {
832                                 /* system: prefix */
833                                 system.add (p);
834                         } else {
835                                 if (p.substr(0, client_matching_string.length()) != client_matching_string) {
836                                         /* other (non-ardour) prefix */
837                                         other.add (p);
838                                 }
839                         }
840
841                         ++n;
842                 }
843
844                 free (ports);
845         }
846
847         push_back (&system);
848         push_back (&buss);
849         push_back (&track);
850         push_back (&other);
851 }
852
853 int
854 PortGroupList::n_visible_ports () const
855 {
856         int n = 0;
857         
858         for (const_iterator i = begin(); i != end(); ++i) {
859                 if ((*i)->visible) {
860                         n += (*i)->ports.size();
861                 }
862         }
863
864         return n;
865 }
866
867 std::string
868 PortGroupList::get_port_by_index (int n, bool with_prefix) const
869 {
870         /* XXX: slightly inefficient algorithm */
871
872         for (const_iterator i = begin(); i != end(); ++i) {
873                 for (std::vector<std::string>::const_iterator j = (*i)->ports.begin(); j != (*i)->ports.end(); ++j) {
874                         if (n == 0) {
875                                 if (with_prefix) {
876                                         return (*i)->prefix + *j;
877                                 } else {
878                                         return *j;
879                                 }
880                         }
881                         --n;
882                 }
883         }
884
885         return "";
886 }
887
888 void
889 PortGroupList::set_type (ARDOUR::DataType t)
890 {
891         _type = t;
892 }
893
894 void
895 PortGroupList::set_offer_inputs (bool i)
896 {
897         _offer_inputs = i;
898 }
899