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