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