Update comments & icon of rubberband example Lua script
[ardour.git] / gtk2_ardour / export_channel_selector.cc
1 /*
2  * Copyright (C) 2008-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
3  * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
6  * Copyright (C) 2013-2015 Colin Fletcher <colin.m.fletcher@googlemail.com>
7  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
8  * Copyright (C) 2015 Ben Loftis <ben@harrisonconsoles.com>
9  * Copyright (C) 2015 Nick Mainsbridge <mainsbridge@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <algorithm>
27 #include <sstream>
28
29 #include <gtkmm/menu.h>
30
31 #include "pbd/convert.h"
32
33 #include "ardour/audio_track.h"
34 #include "ardour/audioregion.h"
35 #include "ardour/export_channel_configuration.h"
36 #include "ardour/io.h"
37 #include "ardour/route.h"
38 #include "ardour/session.h"
39 #include "ardour/selection.h"
40
41 #include "export_channel_selector.h"
42 #include "route_sorter.h"
43
44 #include "pbd/i18n.h"
45
46 using namespace std;
47 using namespace Glib;
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 PortExportChannelSelector::PortExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager) :
52   ExportChannelSelector (session, manager),
53   channels_label (_("Channels:"), Gtk::ALIGN_LEFT),
54   split_checkbox (_("Split to mono files")),
55   max_channels (20),
56   channel_view (max_channels)
57 {
58         channels_hbox.pack_start (channels_label, false, false, 0);
59         channels_hbox.pack_end (channels_spinbutton, false, false, 0);
60
61         channels_vbox.pack_start (channels_hbox, false, false, 0);
62         channels_vbox.pack_start (split_checkbox, false, false, 6);
63
64         channel_alignment.add (channel_scroller);
65         channel_alignment.set_padding (0, 0, 12, 0);
66         channel_scroller.add (channel_view);
67         channel_scroller.set_size_request (-1, 130);
68         channel_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
69
70         pack_start (channels_vbox, false, false, 0);
71         pack_start (channel_alignment, true, true, 0);
72
73         /* Channels spinbutton */
74
75         channels_spinbutton.set_digits (0);
76         channels_spinbutton.set_increments (1, 2);
77         channels_spinbutton.set_range (1, max_channels);
78         channels_spinbutton.set_value (2);
79
80         channels_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_channel_count));
81
82         /* Other signals */
83
84         split_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::update_split_state));
85         channel_view.CriticalSelectionChanged.connect (CriticalSelectionChanged.make_slot());
86
87         /* Finalize */
88
89         sync_with_manager();
90         show_all_children ();
91
92 }
93
94 PortExportChannelSelector::~PortExportChannelSelector ()
95 {
96 }
97
98 void
99 PortExportChannelSelector::sync_with_manager ()
100 {
101         state = manager->get_channel_configs().front();
102
103         split_checkbox.set_active (state->config->get_split());
104         channels_spinbutton.set_value (state->config->get_n_chans());
105
106         /* when loading presets, config is ready set here (shared ptr)
107          * fill_route_list () -> update_channel_count () -> set_channel_count () -> update_config()
108          * will call config->clear_channels(); and clear the config
109          */
110         channel_view.set_config (ChannelConfigPtr ());
111         fill_route_list ();
112         channel_view.set_config (state->config);
113 }
114
115 void
116 PortExportChannelSelector::fill_route_list ()
117 {
118         channel_view.clear_routes ();
119         RouteList routes = _session->get_routelist();
120
121         /* Add master bus and then everything else */
122
123         if (_session->master_out()) {
124                 ARDOUR::IO* master = _session->master_out()->output().get();
125                 channel_view.add_route (master);
126         }
127
128         routes.sort (Stripable::Sorter ());
129
130         for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
131                 if ((*it)->is_master () || (*it)->is_monitor ()) {
132                         continue;
133                 }
134                 channel_view.add_route ((*it)->output().get());
135         }
136
137         update_channel_count ();
138 }
139
140 void
141 PortExportChannelSelector::update_channel_count ()
142 {
143         uint32_t chans = static_cast<uint32_t> (channels_spinbutton.get_value());
144         channel_view.set_channel_count (chans);
145         CriticalSelectionChanged();
146 }
147
148 void
149 PortExportChannelSelector::update_split_state ()
150 {
151         state->config->set_split (split_checkbox.get_active());
152         CriticalSelectionChanged();
153 }
154
155 void
156 PortExportChannelSelector::RouteCols::add_channels (uint32_t chans)
157 {
158         while (chans > 0) {
159                 channels.push_back (Channel (*this));
160                 ++n_channels;
161                 --chans;
162         }
163 }
164
165 PortExportChannelSelector::RouteCols::Channel &
166 PortExportChannelSelector::RouteCols::get_channel (uint32_t channel)
167 {
168         if (channel > n_channels) {
169                 std::cout << "Invalid channel count for get_channel!" << std::endl;
170         }
171
172         std::list<Channel>::iterator it = channels.begin();
173
174         while (channel > 1) { // Channel count starts from one!
175                 ++it;
176                 --channel;
177         }
178
179         return *it;
180 }
181
182 PortExportChannelSelector::ChannelTreeView::ChannelTreeView (uint32_t max_channels) :
183   n_channels (0)
184 {
185         /* Main columns */
186
187         route_cols.add_channels (max_channels);
188
189         route_list = Gtk::ListStore::create(route_cols);
190         set_model (route_list);
191
192         /* Add column with toggle and text */
193
194         append_column_editable (_("Export"), route_cols.selected);
195
196         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
197         text_renderer->property_editable() = false;
198         text_renderer->set_alignment (0.0, 0.5);
199
200         Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
201         column->set_title (_("Bus or Track"));
202         column->pack_start (*text_renderer);
203         column->set_expand (true);
204         column->add_attribute (text_renderer->property_text(), route_cols.name);
205         append_column  (*column);
206
207         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(get_column_cell_renderer (0));
208         toggle->set_alignment (0.0, 0.5);
209         toggle->signal_toggled().connect (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_toggle_selection));
210
211         static_columns = get_columns().size();
212 }
213
214 void
215 PortExportChannelSelector::ChannelTreeView::set_config (ChannelConfigPtr c)
216 {
217         /* TODO Without the following line, the state might get reset.
218          * Pointing to the same address does not mean the state of the configuration hasn't changed.
219          * In the current code this is safe, but the actual cause of the problem would be good to fix
220          */
221
222         if (config == c) { return; }
223         config = c;
224         if (!config) { return; }
225
226         uint32_t i = 1;
227         ExportChannelConfiguration::ChannelList chan_list = config->get_channels();
228         for (ExportChannelConfiguration::ChannelList::iterator c_it = chan_list.begin(); c_it != chan_list.end(); ++c_it) {
229
230                 for (Gtk::ListStore::Children::iterator r_it = route_list->children().begin(); r_it != route_list->children().end(); ++r_it) {
231
232                         ARDOUR::PortExportChannel * pec;
233                         if (!(pec = dynamic_cast<ARDOUR::PortExportChannel *> (c_it->get()))) {
234                                 continue;
235                         }
236
237                         Glib::RefPtr<Gtk::ListStore> port_list = r_it->get_value (route_cols.port_list_col);
238                         std::set<boost::weak_ptr<AudioPort> > route_ports;
239                         std::set<boost::weak_ptr<AudioPort> > intersection;
240                         std::map<boost::weak_ptr<AudioPort>, string> port_labels;
241
242                         for (Gtk::ListStore::Children::const_iterator p_it = port_list->children().begin(); p_it != port_list->children().end(); ++p_it) {
243                                 route_ports.insert ((*p_it)->get_value (route_cols.port_cols.port));
244                                 port_labels.insert (make_pair ((*p_it)->get_value (route_cols.port_cols.port),
245                                                                (*p_it)->get_value (route_cols.port_cols.label)));
246                         }
247
248                         std::set_intersection (pec->get_ports().begin(), pec->get_ports().end(),
249                                                route_ports.begin(), route_ports.end(),
250                                                std::insert_iterator<std::set<boost::weak_ptr<AudioPort> > > (intersection, intersection.begin()));
251
252                         intersection.erase (boost::weak_ptr<AudioPort> ()); // Remove "none" selection
253
254                         if (intersection.empty()) {
255                                 continue;
256                         }
257
258                         if (!r_it->get_value (route_cols.selected)) {
259                                 r_it->set_value (route_cols.selected, true);
260
261                                 /* Set previous channels (if any) to none */
262
263                                 for (uint32_t chn = 1; chn < i; ++chn) {
264                                         r_it->set_value (route_cols.get_channel (chn).port, boost::weak_ptr<AudioPort> ());
265                                         r_it->set_value (route_cols.get_channel (chn).label, string ("(none)"));
266                                 }
267                         }
268
269                         boost::weak_ptr<AudioPort> port = *intersection.begin();
270                         std::map<boost::weak_ptr<AudioPort>, string>::iterator label_it = port_labels.find (port);
271                         string label = label_it != port_labels.end() ? label_it->second : "error";
272
273                         r_it->set_value (route_cols.get_channel (i).port, port);
274                         r_it->set_value (route_cols.get_channel (i).label, label);
275                 }
276
277                 ++i;
278         }
279 }
280
281 void
282 PortExportChannelSelector::ChannelTreeView::add_route (ARDOUR::IO * io)
283 {
284         Gtk::TreeModel::iterator iter = route_list->append();
285         Gtk::TreeModel::Row row = *iter;
286
287         row[route_cols.selected] = false;
288         row[route_cols.name] = io->name();
289         row[route_cols.io] = io;
290
291         /* Initialize port list */
292
293         Glib::RefPtr<Gtk::ListStore> port_list = Gtk::ListStore::create (route_cols.port_cols);
294         row[route_cols.port_list_col] = port_list;
295
296         uint32_t outs = io->n_ports().n_audio();
297         for (uint32_t i = 0; i < outs; ++i) {
298                 iter = port_list->append();
299                 row = *iter;
300
301                 row[route_cols.port_cols.selected] = false;
302                 row[route_cols.port_cols.port] = io->audio (i);
303
304                 std::ostringstream oss;
305                 oss << "Out-" << (i + 1);
306
307                 row[route_cols.port_cols.label] = oss.str();
308         }
309
310         iter = port_list->append();
311         row = *iter;
312
313         row[route_cols.port_cols.selected] = false;
314         row[route_cols.port_cols.port] = boost::weak_ptr<AudioPort> ();
315         row[route_cols.port_cols.label] = "(none)";
316
317 }
318
319 void
320 PortExportChannelSelector::ChannelTreeView::set_channel_count (uint32_t channels)
321 {
322         int offset = channels - n_channels;
323
324         while (offset > 0) {
325                 ++n_channels;
326
327                 std::ostringstream oss;
328                 oss << n_channels;
329
330                 /* New column */
331
332                 Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column (oss.str()));
333
334                 Gtk::CellRendererCombo* combo_renderer = Gtk::manage (new Gtk::CellRendererCombo);
335                 combo_renderer->property_text_column() = 2;
336                 combo_renderer->property_has_entry() = false;
337                 column->pack_start (*combo_renderer);
338
339                 append_column (*column);
340
341                 column->add_attribute (combo_renderer->property_text(), route_cols.get_channel(n_channels).label);
342                 column->add_attribute (combo_renderer->property_model(), route_cols.port_list_col);
343                 column->add_attribute (combo_renderer->property_editable(), route_cols.selected);
344
345                 combo_renderer->signal_edited().connect (sigc::bind (sigc::mem_fun (*this, &PortExportChannelSelector::ChannelTreeView::update_selection_text), n_channels));
346
347                 /* put data into view */
348
349                 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
350                         std::string label = it->get_value(route_cols.selected) ? "(none)" : "";
351                         it->set_value (route_cols.get_channel (n_channels).label, label);
352                         it->set_value (route_cols.get_channel (n_channels).port, boost::weak_ptr<AudioPort> ());
353                 }
354
355                 /* set column width */
356
357                 get_column (static_columns + n_channels - 1)->set_min_width (80);
358
359                 --offset;
360         }
361
362         while (offset < 0) {
363                 --n_channels;
364
365                 remove_column (*get_column (n_channels + static_columns));
366
367                 ++offset;
368         }
369
370         update_config ();
371 }
372
373 void
374 PortExportChannelSelector::ChannelTreeView::update_config ()
375 {
376         if (!config) { return; }
377
378         config->clear_channels();
379
380         for (uint32_t i = 1; i <= n_channels; ++i) {
381
382                 ExportChannelPtr channel (new PortExportChannel ());
383                 PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
384
385                 for (Gtk::ListStore::Children::iterator it = route_list->children().begin(); it != route_list->children().end(); ++it) {
386                         Gtk::TreeModel::Row row = *it;
387
388                         if (!row[route_cols.selected]) {
389                                 continue;
390                         }
391
392                         boost::weak_ptr<AudioPort> weak_port = row[route_cols.get_channel (i).port];
393                         boost::shared_ptr<AudioPort> port = weak_port.lock ();
394                         if (port) {
395                                 pec->add_port (port);
396                         }
397                 }
398
399                 config->register_channel (channel);
400         }
401
402         CriticalSelectionChanged ();
403 }
404
405 void
406 PortExportChannelSelector::ChannelTreeView::update_toggle_selection (std::string const & path)
407 {
408         Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
409         bool selected = iter->get_value (route_cols.selected);
410
411         for (uint32_t i = 1; i <= n_channels; ++i) {
412
413                 if (!selected) {
414                         iter->set_value (route_cols.get_channel (i).label, std::string (""));
415                         continue;
416                 }
417
418                 iter->set_value (route_cols.get_channel (i).label, std::string("(none)"));
419                 iter->set_value (route_cols.get_channel (i).port, boost::weak_ptr<AudioPort> ());
420
421                 Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
422                 Gtk::ListStore::Children::iterator port_it;
423                 uint32_t port_number = 1;
424
425                 for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
426                         if (port_number == i) {
427                                 iter->set_value (route_cols.get_channel (i).label, (std::string) (*port_it)->get_value (route_cols.port_cols.label));
428                                 iter->set_value (route_cols.get_channel (i).port, (*port_it)->get_value (route_cols.port_cols.port));
429                         }
430
431                         ++port_number;
432                 }
433         }
434
435         update_config ();
436 }
437
438 void
439 PortExportChannelSelector::ChannelTreeView::update_selection_text (std::string const & path, std::string const & new_text, uint32_t channel)
440 {
441         Gtk::TreeModel::iterator iter = get_model ()->get_iter (path);
442         iter->set_value (route_cols.get_channel (channel).label, new_text);
443
444         Glib::RefPtr<Gtk::ListStore> port_list = iter->get_value (route_cols.port_list_col);
445         Gtk::ListStore::Children::iterator port_it;
446
447         for (port_it = port_list->children().begin(); port_it != port_list->children().end(); ++port_it) {
448                 std::string label = port_it->get_value (route_cols.port_cols.label);
449                 if (label == new_text) {
450                         boost::weak_ptr<AudioPort> w = (*port_it)[route_cols.port_cols.port];
451                         iter->set_value (route_cols.get_channel (channel).port, w);
452                 }
453         }
454
455         update_config ();
456 }
457
458 RegionExportChannelSelector::RegionExportChannelSelector (ARDOUR::Session * _session,
459                                                           ProfileManagerPtr manager,
460                                                           ARDOUR::AudioRegion const & region,
461                                                           ARDOUR::AudioTrack & track) :
462         ExportChannelSelector (_session, manager),
463         region (region),
464         track (track),
465         region_chans (region.n_channels()),
466         track_chans (track.n_outputs().n_audio()),
467
468         raw_button (type_group),
469         fades_button (type_group),
470         processed_button (type_group)
471 {
472         pack_start (vbox);
473
474         /* make fades+region gain be the default */
475
476         fades_button.set_active ();
477
478         raw_button.set_label (string_compose (_("Region contents without fades nor region gain (channels: %1)"), region_chans));
479         raw_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
480         vbox.pack_start (raw_button, false, false);
481
482         fades_button.set_label (string_compose (_("Region contents with fades and region gain (channels: %1)"), region_chans));
483         fades_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
484         vbox.pack_start (fades_button, false, false);
485
486         processed_button.set_label (string_compose (_("Track output (channels: %1)"), track_chans));
487         processed_button.signal_toggled ().connect (sigc::mem_fun (*this, &RegionExportChannelSelector::handle_selection));
488         vbox.pack_start (processed_button, false, false);
489
490         sync_with_manager();
491         vbox.show_all_children ();
492         show_all_children ();
493 }
494
495 void
496 RegionExportChannelSelector::sync_with_manager ()
497 {
498         state = manager->get_channel_configs().front();
499
500         if (!state) { return; }
501
502         switch (state->config->region_processing_type()) {
503         case RegionExportChannelFactory::None:
504                 // Do nothing
505                 break;
506         case RegionExportChannelFactory::Raw:
507                 raw_button.set_active (true);
508                 break;
509         case RegionExportChannelFactory::Fades:
510                 fades_button.set_active (true);
511                 break;
512         case RegionExportChannelFactory::Processed:
513                 processed_button.set_active (true);
514                 break;
515         }
516
517         handle_selection ();
518 }
519
520 void
521 RegionExportChannelSelector::handle_selection ()
522 {
523         if (!state) {
524                 return;
525         }
526
527         state->config->clear_channels ();
528
529         RegionExportChannelFactory::Type type = RegionExportChannelFactory::None;
530         if (raw_button.get_active ()) {
531                 type = RegionExportChannelFactory::Raw;
532         } else if (fades_button.get_active ()) {
533                 type = RegionExportChannelFactory::Fades;
534         } else if (processed_button.get_active ()) {
535                 type = RegionExportChannelFactory::Processed;
536         } else {
537                 CriticalSelectionChanged ();
538                 return;
539         }
540
541         factory.reset (new RegionExportChannelFactory (_session, region, track, type));
542         state->config->set_region_processing_type (type);
543
544         const size_t cc = type == RegionExportChannelFactory::Processed ? track_chans : region_chans;
545         for (size_t chan = 0; chan < cc; ++chan) {
546                 state->config->register_channel (factory->create (chan));
547         }
548
549         CriticalSelectionChanged ();
550 }
551
552 /* Track export channel selector */
553
554 TrackExportChannelSelector::TrackExportChannelSelector (ARDOUR::Session * session, ProfileManagerPtr manager)
555   : ExportChannelSelector(session, manager)
556   , track_output_button(_("Apply track/bus processing"))
557 {
558         pack_start(main_layout);
559
560         // Populate Selection Menu
561         {
562                 using namespace Gtk::Menu_Helpers;
563
564                 select_menu.set_text (_("Selection Actions"));
565                 select_menu.disable_scrolling ();
566
567                 select_menu.AddMenuElem (MenuElem (_("Select tracks"), sigc::mem_fun (*this, &TrackExportChannelSelector::select_tracks)));
568                 select_menu.AddMenuElem (MenuElem (_("Select busses"), sigc::mem_fun (*this, &TrackExportChannelSelector::select_busses)));
569                 select_menu.AddMenuElem (MenuElem (_("Deselect all"), sigc::mem_fun (*this, &TrackExportChannelSelector::select_none)));
570                 select_menu.AddMenuElem (SeparatorElem ());
571
572                 exclude_hidden = new Gtk::CheckMenuItem (_("Exclude Hidden"));
573                 exclude_hidden->set_active (false);
574                 exclude_hidden->show();
575                 select_menu.AddMenuElem (*exclude_hidden);
576
577                 exclude_muted = new Gtk::CheckMenuItem (_("Exclude Muted"));
578                 exclude_muted->set_active (true);
579                 exclude_muted->show();
580                 select_menu.AddMenuElem (*exclude_muted);
581         }
582
583         // Options
584         options_box.set_spacing (8);
585         options_box.pack_start (track_output_button, false, false);
586         options_box.pack_start (select_menu, false, false);
587         main_layout.pack_start (options_box, false, false);
588
589         // Track scroller
590         track_scroller.add (track_view);
591         track_scroller.set_size_request (-1, 130);
592         track_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
593         main_layout.pack_start(track_scroller);
594
595         // Track list
596         track_list = Gtk::ListStore::create (track_cols);
597         track_list->set_sort_column (track_cols.order_key, Gtk::SORT_ASCENDING);
598         track_view.set_model (track_list);
599         track_view.set_headers_visible (true);
600
601         track_view.append_column_editable (_("Export"), track_cols.selected);
602         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(track_view.get_column_cell_renderer (0));
603         toggle->set_alignment (0.0, 0.5);
604
605         toggle->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &TrackExportChannelSelector::update_config)));
606
607         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
608         text_renderer->property_editable() = false;
609         text_renderer->set_alignment (0.0, 0.5);
610
611         Gtk::TreeView::Column* column = Gtk::manage (new Gtk::TreeView::Column);
612         column->set_title (_("Track name"));
613
614         track_view.append_column  (*column);
615         column->pack_start (*text_renderer, false);
616         column->add_attribute (text_renderer->property_text(), track_cols.label);
617
618         track_output_button.signal_clicked().connect (sigc::mem_fun (*this, &TrackExportChannelSelector::track_outputs_selected));
619
620         fill_list();
621
622         show_all_children ();
623 }
624
625 TrackExportChannelSelector::~TrackExportChannelSelector ()
626 {
627         delete exclude_hidden;
628         delete exclude_muted;
629 }
630
631 void
632 TrackExportChannelSelector::sync_with_manager ()
633 {
634         // TODO implement properly
635         update_config();
636 }
637
638 void
639 TrackExportChannelSelector::select_tracks ()
640 {
641         bool excl_hidden = exclude_hidden->get_active ();
642         bool excl_muted  = exclude_muted->get_active ();
643
644         for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
645                 Gtk::TreeModel::Row row = *it;
646                 boost::shared_ptr<Route> route = row[track_cols.route];
647                 if (boost::dynamic_pointer_cast<Track> (route)) {
648                         if (excl_muted && route->muted ()) {
649                                 continue;
650                         }
651                         if (excl_hidden && route->is_hidden ()) {
652                                 continue;
653                         }
654                         row[track_cols.selected] = true;
655                 }
656         }
657         update_config();
658 }
659
660 void
661 TrackExportChannelSelector::select_busses ()
662 {
663         bool excl_hidden = exclude_hidden->get_active ();
664         bool excl_muted  = exclude_muted->get_active ();
665
666         for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
667                 Gtk::TreeModel::Row row = *it;
668                 boost::shared_ptr<Route> route = row[track_cols.route];
669                 if (!boost::dynamic_pointer_cast<Track> (route)) {
670                         if (excl_muted && route->muted ()) {
671                                 continue;
672                         }
673                         if (excl_hidden && route->is_hidden ()) {
674                                 continue;
675                         }
676                         row[track_cols.selected] = true;
677                 }
678         }
679         update_config();
680 }
681
682 void
683 TrackExportChannelSelector::select_none ()
684 {
685         for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
686                 Gtk::TreeModel::Row row = *it;
687                 row[track_cols.selected] = false;
688         }
689         update_config();
690 }
691
692 void
693 TrackExportChannelSelector::track_outputs_selected ()
694 {
695         update_config();
696 }
697
698 void
699 TrackExportChannelSelector::fill_list()
700 {
701         track_list->clear();
702         RouteList routes = _session->get_routelist();
703
704         CoreSelection const& cs (_session->selection());
705
706         for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
707                 if (!boost::dynamic_pointer_cast<Track>(*it)) {
708                         // not a track, must be a bus
709                         if ((*it)->is_master () || (*it)->is_monitor ()) {
710                                 continue;
711                         }
712                         if (!(*it)->active ()) {
713                                 // don't include inactive busses
714                                 continue;
715                         }
716
717                         // not monitor or master bus
718                         add_track (*it, cs.selected (*it));
719                 }
720         }
721         for (RouteList::iterator it = routes.begin(); it != routes.end(); ++it) {
722                 if (boost::dynamic_pointer_cast<AudioTrack>(*it)) {
723                         if (!(*it)->active ()) {
724                                 // don't include inactive tracks
725                                 continue;
726                         }
727                         add_track (*it, cs.selected (*it));
728                 }
729         }
730 }
731
732 void
733 TrackExportChannelSelector::add_track (boost::shared_ptr<Route> route, bool selected)
734 {
735         Gtk::TreeModel::iterator iter = track_list->append();
736         Gtk::TreeModel::Row row = *iter;
737
738         row[track_cols.selected] = selected;
739         row[track_cols.label] = route->name();
740         row[track_cols.route] = route;
741         row[track_cols.order_key] = route->presentation_info().order();
742 }
743
744 void
745 TrackExportChannelSelector::update_config()
746 {
747         manager->clear_channel_configs();
748
749         for (Gtk::ListStore::Children::iterator it = track_list->children().begin(); it != track_list->children().end(); ++it) {
750                 Gtk::TreeModel::Row row = *it;
751
752                 if (!row[track_cols.selected]) {
753                         continue;
754                 }
755
756                 ExportProfileManager::ChannelConfigStatePtr state;
757
758                 boost::shared_ptr<Route> route = row[track_cols.route];
759
760                 if (track_output_button.get_active()) {
761                         uint32_t outs = route->n_outputs().n_audio();
762                         for (uint32_t i = 0; i < outs; ++i) {
763                                 boost::shared_ptr<AudioPort> port = route->output()->audio (i);
764                                 if (port) {
765                                         ExportChannelPtr channel (new PortExportChannel ());
766                                         PortExportChannel * pec = static_cast<PortExportChannel *> (channel.get());
767                                         pec->add_port(port);
768                                         if (!state) {
769                                                 state = manager->add_channel_config();
770                                         }
771                                         state->config->register_channel(channel);
772                                 }
773                         }
774                 } else {
775                         std::list<ExportChannelPtr> list;
776                         RouteExportChannel::create_from_route (list, route);
777                         if (list.size () == 0) {
778                                 continue;
779                         }
780                         state = manager->add_channel_config();
781                         state->config->register_channels (list);
782                 }
783
784                 if (state) {
785                         if (_session->config.get_track_name_number() && route->track_number() > 0) {
786                                 state->config->set_name (string_compose ("%1-%2", route->track_number(), route->name()));
787                         } else {
788                                 state->config->set_name (route->name());
789                         }
790                 }
791
792         }
793
794         CriticalSelectionChanged ();
795 }