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