Update export format when 'upload to soundcloud' changes.
[ardour.git] / gtk2_ardour / export_format_dialog.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 "ardour/session.h"
22 #include "ardour/export_format_specification.h"
23
24 #include "export_format_dialog.h"
25 #include "gui_thread.h"
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29
30 ExportFormatDialog::ExportFormatDialog (FormatPtr format, bool new_dialog) :
31   ArdourDialog (new_dialog ? _("New Export Format Profile") : _("Edit Export Format Profile")),
32   format (format),
33   manager (format),
34   original_state (format->get_state()),
35
36   applying_changes_from_engine (0),
37
38   name_label (_("Label: "), Gtk::ALIGN_LEFT),
39   name_generated_part ("", Gtk::ALIGN_LEFT),
40
41   normalize_checkbox (_("Normalize to:")),
42   normalize_adjustment (0.00, -90.00, 0.00, 0.1, 0.2),
43   normalize_db_label (_("dBFS"), Gtk::ALIGN_LEFT),
44
45   silence_table (2, 4),
46   trim_start_checkbox (_("Trim silence at start")),
47   silence_start_checkbox (_("Add silence at start:")),
48   silence_start_clock ("silence_start", true, "", true, false, true),
49
50   trim_end_checkbox (_("Trim silence at end")),
51   silence_end_checkbox (_("Add silence at end:")),
52   silence_end_clock ("silence_end", true, "", true, false, true),
53
54   upload_checkbox(_("Upload to Soundcloud")),
55
56   format_table (3, 4),
57   compatibility_label (_("Compatibility"), Gtk::ALIGN_LEFT),
58   quality_label (_("Quality"), Gtk::ALIGN_LEFT),
59   format_label (_("File format"), Gtk::ALIGN_LEFT),
60   sample_rate_label (_("Sample rate"), Gtk::ALIGN_LEFT),
61   src_quality_label (_("Sample rate conversion quality:"), Gtk::ALIGN_RIGHT),
62
63   encoding_options_label ("", Gtk::ALIGN_LEFT),
64
65   /* Changing encoding options from here on */
66
67   sample_format_label (_("Sample Format"), Gtk::ALIGN_LEFT),
68   dither_label (_("Dithering"), Gtk::ALIGN_LEFT),
69
70   with_cue (_("Create CUE file for disk-at-once CD/DVD creation")),
71   with_toc (_("Create TOC file for disk-at-once CD/DVD creation")),
72
73   tag_checkbox (_("Tag file with session's metadata"))
74 {
75
76         /* Pack containers in dialog */
77
78         get_vbox()->pack_start (silence_table, false, false, 6);
79         get_vbox()->pack_start (format_table, false, false, 6);
80         get_vbox()->pack_start (encoding_options_vbox, false, false, 0);
81         get_vbox()->pack_start (cue_toc_vbox, false, false, 0);
82         get_vbox()->pack_start (name_hbox, false, false, 6);
83
84         /* Name, new and remove */
85
86         name_hbox.pack_start (name_label, false, false, 0);
87         name_hbox.pack_start (name_entry, false, false, 0);
88         name_hbox.pack_start (name_generated_part, true, true, 0);
89         name_entry.set_width_chars(20);
90         update_description();
91         manager.DescriptionChanged.connect(
92                 *this, invalidator (*this),
93                 boost::bind (&ExportFormatDialog::update_description, this), gui_context());
94
95         /* Normalize */
96
97         normalize_hbox.pack_start (normalize_checkbox, false, false, 0);
98         normalize_hbox.pack_start (normalize_spinbutton, false, false, 6);
99         normalize_hbox.pack_start (normalize_db_label, false, false, 0);
100
101         normalize_spinbutton.configure (normalize_adjustment, 0.1, 2);
102
103         /* Silence  */
104
105         silence_table.set_row_spacings (6);
106         silence_table.set_col_spacings (12);
107
108         silence_table.attach (normalize_hbox, 0, 3, 0, 1);
109
110         silence_table.attach (trim_start_checkbox, 0, 1, 1, 2);
111         silence_table.attach (silence_start_checkbox, 1, 2, 1, 2);
112         silence_table.attach (silence_start_clock, 2, 3, 1, 2);
113
114         silence_table.attach (trim_end_checkbox, 0, 1, 2, 3);
115         silence_table.attach (silence_end_checkbox, 1, 2, 2, 3);
116         silence_table.attach (silence_end_clock, 2, 3, 2, 3);
117
118         get_vbox()->pack_start (upload_checkbox, false, false);
119         /* Format table */
120
121         init_format_table();
122
123         /* Encoding options */
124
125         init_encoding_option_widgets();
126
127         encoding_options_table.set_spacings (1);
128
129         encoding_options_vbox.pack_start (encoding_options_label, false, false, 0);
130         encoding_options_vbox.pack_start (encoding_options_table, false, false, 12);
131
132         Pango::AttrList bold;
133         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
134         bold.insert (b);
135         encoding_options_label.set_attributes (bold);
136
137         /* Buttons */
138
139         revert_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
140         revert_button->signal_clicked().connect (sigc::mem_fun(*this, &ExportFormatDialog::revert));
141         close_button = add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_APPLY);
142         close_button->set_sensitive (false);
143         close_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportFormatDialog::end_dialog));
144         manager.CompleteChanged.connect (*this, invalidator (*this), boost::bind (&Gtk::Button::set_sensitive, close_button, _1), gui_context());
145
146         with_cue.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_cue));
147         with_toc.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_toc));
148         upload_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_upload));
149
150         cue_toc_vbox.pack_start (with_cue, false, false);
151         cue_toc_vbox.pack_start (with_toc, false, false);
152
153         /* Load state before hooking up the rest of the signals */
154
155         load_state (format);
156
157         /* Name entry */
158
159         name_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_name));
160
161         /* Normalize, silence and src_quality signals */
162
163         trim_start_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_trim_start_selection));
164         trim_end_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_trim_end_selection));
165
166         normalize_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
167         normalize_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
168
169         silence_start_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_start_selection));
170         silence_start_clock.ValueChanged.connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_start_selection));
171
172         silence_end_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_end_selection));
173         silence_end_clock.ValueChanged.connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_end_selection));
174
175         src_quality_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_src_quality_selection));
176
177         /* Format table signals */
178
179         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(compatibility_view.get_column_cell_renderer (0));
180         toggle->signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_compatibility_selection));
181         compatibility_select_connection = compatibility_view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::prohibit_compatibility_selection));
182
183         quality_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_quality_selection));
184         format_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_format_selection));
185         sample_rate_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_sample_rate_selection));
186
187         /* Encoding option signals */
188
189         sample_format_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_sample_format_selection));
190         dither_type_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_dither_type_selection));
191
192         tag_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_tagging_selection));
193
194         /* Finalize */
195
196         show_all_children();
197 }
198
199 ExportFormatDialog::~ExportFormatDialog ()
200 {
201
202 }
203
204 void
205 ExportFormatDialog::revert ()
206 {
207         ++applying_changes_from_engine;
208
209         format->set_state (original_state);
210         load_state (format);
211
212         --applying_changes_from_engine;
213 }
214
215 void
216 ExportFormatDialog::set_session (ARDOUR::Session* s)
217 {
218         SessionHandlePtr::set_session (s);
219         silence_start_clock.set_session (s);
220         silence_end_clock.set_session (s);
221
222         if (!_session) {
223                 return;
224         }
225
226         update_clock (silence_start_clock, silence_start);
227         update_clock (silence_end_clock, silence_end);
228
229         /* Select native samplerate if no selection is yet made */
230
231         if (sample_rate_view.get_selection()->count_selected_rows() == 0) {
232                 Gtk::ListStore::Children::iterator it;
233                 for (it = sample_rate_list->children().begin(); it != sample_rate_list->children().end(); ++it) {
234                         if ((framecnt_t) (*it)->get_value (sample_rate_cols.ptr)->rate == _session->nominal_frame_rate()) {
235                                 sample_rate_view.get_selection()->select (it);
236                                 break;
237                         }
238                 }
239         }
240 }
241
242 void
243 ExportFormatDialog::load_state (FormatPtr spec)
244 {
245         name_entry.set_text (spec->name());
246
247         normalize_checkbox.set_active (spec->normalize());
248         normalize_spinbutton.set_value (spec->normalize_target());
249
250         trim_start_checkbox.set_active (spec->trim_beginning());
251         silence_start = spec->silence_beginning_time();
252         silence_start_checkbox.set_active (spec->silence_beginning_time().not_zero());
253
254         trim_end_checkbox.set_active (spec->trim_end());
255         silence_end = spec->silence_end_time();
256         silence_end_checkbox.set_active (spec->silence_end_time().not_zero());
257
258         with_cue.set_active (spec->with_cue());
259         with_toc.set_active (spec->with_toc());
260
261         for (Gtk::ListStore::Children::iterator it = src_quality_list->children().begin(); it != src_quality_list->children().end(); ++it) {
262                 if (it->get_value (src_quality_cols.id) == spec->src_quality()) {
263                         src_quality_combo.set_active (it);
264                         break;
265                 }
266         }
267
268         for (Gtk::ListStore::Children::iterator it = format_list->children().begin(); it != format_list->children().end(); ++it) {
269                 boost::shared_ptr<ARDOUR::ExportFormat> format_in_list = it->get_value (format_cols.ptr);
270                 if (format_in_list->get_format_id() == spec->format_id() &&
271                     // BWF has the same format id with wav, so we need to check this.
272                     format_in_list->has_broadcast_info() == spec->has_broadcast_info()) {
273
274                         format_in_list->set_selected (true);
275                         break;
276                 }
277         }
278
279         for (Gtk::ListStore::Children::iterator it = sample_rate_list->children().begin(); it != sample_rate_list->children().end(); ++it) {
280                 if (it->get_value (sample_rate_cols.ptr)->rate == spec->sample_rate()) {
281                         it->get_value (sample_rate_cols.ptr)->set_selected (true);
282                         break;
283                 }
284         }
285
286         if (spec->sample_format()) {
287                 for (Gtk::ListStore::Children::iterator it = sample_format_list->children().begin(); it != sample_format_list->children().end(); ++it) {
288                         if (it->get_value (sample_format_cols.ptr)->format == spec->sample_format()) {
289                                 it->get_value (sample_format_cols.ptr)->set_selected (true);
290                                 break;
291                         }
292                 }
293
294                 for (Gtk::ListStore::Children::iterator it = dither_type_list->children().begin(); it != dither_type_list->children().end(); ++it) {
295                         if (it->get_value (dither_type_cols.ptr)->type == spec->dither_type()) {
296                                 it->get_value (dither_type_cols.ptr)->set_selected (true);
297                                 break;
298                         }
299                 }
300         }
301
302         tag_checkbox.set_active (spec->tag());
303         upload_checkbox.set_active (spec->upload());
304 }
305
306 void
307 ExportFormatDialog::init_format_table ()
308 {
309
310         format_table.set_spacings (1);
311
312         format_table.attach (compatibility_label, 0, 1, 0, 1);
313         format_table.attach (quality_label, 1, 2, 0, 1);
314         format_table.attach (format_label, 2, 3, 0, 1);
315         format_table.attach (sample_rate_label, 3, 4, 0, 1);
316
317         format_table.attach (compatibility_view, 0, 1, 1, 2);
318         format_table.attach (quality_view, 1, 2, 1, 2);
319         format_table.attach (format_view, 2, 3, 1, 2);
320         format_table.attach (sample_rate_view, 3, 4, 1, 2);
321
322         format_table.attach (src_quality_label, 0, 3, 2, 3);
323         format_table.attach (src_quality_combo, 3, 4, 2, 3);
324
325         compatibility_view.set_headers_visible (false);
326         quality_view.set_headers_visible (false);
327         format_view.set_headers_visible (false);
328         sample_rate_view.set_headers_visible (false);
329
330         /*** Table entries ***/
331
332         Gtk::TreeModel::iterator iter;
333         Gtk::TreeModel::Row row;
334
335         /* Compatibilities */
336
337         compatibility_list = Gtk::ListStore::create (compatibility_cols);
338         compatibility_view.set_model (compatibility_list);
339
340         ExportFormatManager::CompatList const & compat_list = manager.get_compatibilities();
341
342         for (ExportFormatManager::CompatList::const_iterator it = compat_list.begin(); it != compat_list.end(); ++it) {
343                 iter = compatibility_list->append();
344                 row = *iter;
345
346                 row[compatibility_cols.ptr] = *it;
347                 row[compatibility_cols.selected] = false;
348                 row[compatibility_cols.label] = (*it)->name();
349
350                 WeakCompatPtr ptr (*it);
351                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_compatibility_selection, this, _1, ptr), gui_context());
352         }
353
354         compatibility_view.append_column_editable ("", compatibility_cols.selected);
355
356         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
357         text_renderer->property_editable() = false;
358
359         Gtk::TreeView::Column* column = compatibility_view.get_column (0);
360         column->pack_start (*text_renderer);
361         column->add_attribute (text_renderer->property_text(), compatibility_cols.label);
362
363         /* Qualities */
364
365         quality_list = Gtk::ListStore::create (quality_cols);
366         quality_view.set_model (quality_list);
367
368         ExportFormatManager::QualityList const & qualities = manager.get_qualities ();
369
370         for (ExportFormatManager::QualityList::const_iterator it = qualities.begin(); it != qualities.end(); ++it) {
371                 iter = quality_list->append();
372                 row = *iter;
373
374                 row[quality_cols.ptr] = *it;
375                 row[quality_cols.color] = "white";
376                 row[quality_cols.label] = (*it)->name();
377
378                 WeakQualityPtr ptr (*it);
379                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_quality_selection, this, _1, ptr), gui_context());
380                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_quality_compatibility, this, _1, ptr), gui_context());
381         }
382
383         quality_view.append_column ("", quality_cols.label);
384
385         /* Formats */
386
387         format_list = Gtk::ListStore::create (format_cols);
388         format_view.set_model (format_list);
389
390         ExportFormatManager::FormatList const & formats = manager.get_formats ();
391
392         for (ExportFormatManager::FormatList::const_iterator it = formats.begin(); it != formats.end(); ++it) {
393                 iter = format_list->append();
394                 row = *iter;
395
396                 row[format_cols.ptr] = *it;
397                 row[format_cols.color] = "white";
398                 row[format_cols.label] = (*it)->name();
399
400                 WeakFormatPtr ptr (*it);
401                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_format_selection, this, _1, ptr), gui_context());
402                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_format_compatibility, this, _1, ptr), gui_context());
403
404                 /* Encoding options */
405
406                 boost::shared_ptr<HasSampleFormat> hsf;
407
408                 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (*it))) {
409                         hsf->SampleFormatSelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_format_selection, this, _1, _2), gui_context());
410                         hsf->SampleFormatCompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_format_compatibility, this, _1, _2), gui_context());
411
412                         hsf->DitherTypeSelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_dither_type_selection, this, _1, _2), gui_context());
413                         hsf->DitherTypeCompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_dither_type_compatibility, this, _1, _2), gui_context());
414                 }
415         }
416
417         format_view.append_column ("", format_cols.label);
418
419         /* Sample Rates */
420
421         sample_rate_list = Gtk::ListStore::create (sample_rate_cols);
422         sample_rate_view.set_model (sample_rate_list);
423
424         ExportFormatManager::SampleRateList const & rates = manager.get_sample_rates ();
425
426         for (ExportFormatManager::SampleRateList::const_iterator it = rates.begin(); it != rates.end(); ++it) {
427                 iter = sample_rate_list->append();
428                 row = *iter;
429
430                 row[sample_rate_cols.ptr] = *it;
431                 row[sample_rate_cols.color] = "white";
432                 row[sample_rate_cols.label] = (*it)->name();
433
434                 WeakSampleRatePtr ptr (*it);
435                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_rate_selection, this, _1, ptr), gui_context());
436                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_rate_compatibility, this, _1, ptr), gui_context());
437         }
438
439         sample_rate_view.append_column ("", sample_rate_cols.label);
440
441         /* Color rendering */
442
443         Gtk::TreeViewColumn * label_col;
444         Gtk::CellRendererText * renderer;
445
446         label_col = quality_view.get_column(0);
447         renderer = dynamic_cast<Gtk::CellRendererText*> (quality_view.get_column_cell_renderer (0));
448         label_col->add_attribute(renderer->property_foreground(), quality_cols.color);
449
450         label_col = format_view.get_column(0);
451         renderer = dynamic_cast<Gtk::CellRendererText*> (format_view.get_column_cell_renderer (0));
452         label_col->add_attribute(renderer->property_foreground(), format_cols.color);
453
454         label_col = sample_rate_view.get_column(0);
455         renderer = dynamic_cast<Gtk::CellRendererText*> (sample_rate_view.get_column_cell_renderer (0));
456         label_col->add_attribute(renderer->property_foreground(), sample_rate_cols.color);
457
458         /* SRC Qualities */
459
460         src_quality_list = Gtk::ListStore::create (src_quality_cols);
461         src_quality_combo.set_model (src_quality_list);
462
463         iter = src_quality_list->append();
464         row = *iter;
465         row[src_quality_cols.id] = ExportFormatBase::SRC_SincBest;
466         row[src_quality_cols.label] = _("Best (sinc)");
467
468         iter = src_quality_list->append();
469         row = *iter;
470         row[src_quality_cols.id] = ExportFormatBase::SRC_SincMedium;
471         row[src_quality_cols.label] = _("Medium (sinc)");
472
473         iter = src_quality_list->append();
474         row = *iter;
475         row[src_quality_cols.id] = ExportFormatBase::SRC_SincFast;
476         row[src_quality_cols.label] = _("Fast (sinc)");
477
478         iter = src_quality_list->append();
479         row = *iter;
480         row[src_quality_cols.id] = ExportFormatBase::SRC_Linear;
481         row[src_quality_cols.label] = _("Linear");
482
483         iter = src_quality_list->append();
484         row = *iter;
485         row[src_quality_cols.id] = ExportFormatBase::SRC_ZeroOrderHold;
486         row[src_quality_cols.label] = _("Zero order hold");
487
488         src_quality_combo.pack_start (src_quality_cols.label);
489         src_quality_combo.set_active (0);
490 }
491
492 void
493 ExportFormatDialog::init_encoding_option_widgets ()
494 {
495         Gtk::TreeViewColumn * label_col;
496         Gtk::CellRendererText * renderer;
497
498         sample_format_list = Gtk::ListStore::create (sample_format_cols);
499         sample_format_view.set_model (sample_format_list);
500         sample_format_view.set_headers_visible (false);
501         sample_format_view.append_column ("", sample_format_cols.label);
502         label_col = sample_format_view.get_column(0);
503         renderer = dynamic_cast<Gtk::CellRendererText*> (sample_format_view.get_column_cell_renderer (0));
504         label_col->add_attribute(renderer->property_foreground(), sample_format_cols.color);
505
506         dither_type_list = Gtk::ListStore::create (dither_type_cols);
507         dither_type_view.set_model (dither_type_list);
508         dither_type_view.set_headers_visible (false);
509         dither_type_view.append_column ("", dither_type_cols.label);
510         label_col = dither_type_view.get_column(0);
511         renderer = dynamic_cast<Gtk::CellRendererText*> (dither_type_view.get_column_cell_renderer (0));
512         label_col->add_attribute(renderer->property_foreground(), dither_type_cols.color);
513
514 }
515
516 void
517 ExportFormatDialog::update_compatibility_selection (std::string const & path)
518 {
519
520         Gtk::TreeModel::iterator iter = compatibility_view.get_model ()->get_iter (path);
521         ExportFormatCompatibilityPtr ptr = iter->get_value (compatibility_cols.ptr);
522         bool state = iter->get_value (compatibility_cols.selected);
523
524         iter->set_value (compatibility_cols.selected, state);
525         ptr->set_selected (state);
526
527 }
528
529 void
530 ExportFormatDialog::update_quality_selection ()
531 {
532         update_selection<QualityCols> (quality_list, quality_view, quality_cols);
533 }
534
535 void
536 ExportFormatDialog::update_format_selection ()
537 {
538         update_selection<FormatCols> (format_list, format_view, format_cols);
539 }
540
541 void
542 ExportFormatDialog::update_sample_rate_selection ()
543 {
544         update_selection<SampleRateCols> (sample_rate_list, sample_rate_view, sample_rate_cols);
545 }
546
547 void
548 ExportFormatDialog::update_sample_format_selection ()
549 {
550         update_selection<SampleFormatCols> (sample_format_list, sample_format_view, sample_format_cols);
551 }
552
553 void
554 ExportFormatDialog::update_dither_type_selection ()
555 {
556         update_selection<DitherTypeCols> (dither_type_list, dither_type_view, dither_type_cols);
557 }
558
559 template<typename ColsT>
560 void
561 ExportFormatDialog::update_selection (Glib::RefPtr<Gtk::ListStore> & list, Gtk::TreeView & view, ColsT & cols)
562 {
563         if (applying_changes_from_engine) {
564                 return;
565         }
566
567         Gtk::ListStore::Children::iterator it;
568         Glib::RefPtr<Gtk::TreeSelection> selection = view.get_selection();
569
570         for (it = list->children().begin(); it != list->children().end(); ++it) {
571                 bool selected = selection->is_selected (it);
572                 it->get_value (cols.ptr)->set_selected (selected);
573         }
574 }
575
576 void
577 ExportFormatDialog::change_compatibility_selection (bool select, WeakCompatPtr compat)
578 {
579         ++applying_changes_from_engine;
580
581         ExportFormatCompatibilityPtr ptr = compat.lock();
582
583         for (Gtk::ListStore::Children::iterator it = compatibility_list->children().begin(); it != compatibility_list->children().end(); ++it) {
584                 if (it->get_value (compatibility_cols.ptr) == ptr) {
585                         it->set_value (compatibility_cols.selected, select);
586                         break;
587                 }
588         }
589
590         --applying_changes_from_engine;
591 }
592
593 void
594 ExportFormatDialog::change_quality_selection (bool select, WeakQualityPtr quality)
595 {
596         change_selection<ExportFormatManager::QualityState, QualityCols> (select, quality, quality_list, quality_view, quality_cols);
597 }
598
599 void
600 ExportFormatDialog::change_format_selection (bool select, WeakFormatPtr format)
601 {
602         change_selection<ExportFormat, FormatCols> (select, format, format_list, format_view, format_cols);
603
604         ExportFormatPtr ptr = format.lock();
605
606         if (select && ptr) {
607                 change_encoding_options (ptr);
608         }
609 }
610
611 void
612 ExportFormatDialog::change_sample_rate_selection (bool select, WeakSampleRatePtr rate)
613 {
614         change_selection<ExportFormatManager::SampleRateState, SampleRateCols> (select, rate, sample_rate_list, sample_rate_view, sample_rate_cols);
615
616         if (select) {
617                 ExportFormatManager::SampleRatePtr ptr = rate.lock();
618                 if (ptr && _session) {
619                         src_quality_combo.set_sensitive ((uint32_t) ptr->rate != _session->frame_rate());
620                 }
621         }
622 }
623
624 void
625 ExportFormatDialog::change_sample_format_selection (bool select, WeakSampleFormatPtr format)
626 {
627         change_selection<HasSampleFormat::SampleFormatState, SampleFormatCols> (select, format, sample_format_list, sample_format_view, sample_format_cols);
628 }
629
630 void
631 ExportFormatDialog::change_dither_type_selection (bool select, WeakDitherTypePtr type)
632 {
633         change_selection<HasSampleFormat::DitherTypeState, DitherTypeCols> (select, type, dither_type_list, dither_type_view, dither_type_cols);
634 }
635
636 template<typename T, typename ColsT>
637 void
638 ExportFormatDialog::change_selection (bool select, boost::weak_ptr<T> w_ptr, Glib::RefPtr<Gtk::ListStore> & list, Gtk::TreeView & view, ColsT & cols)
639 {
640         ++applying_changes_from_engine;
641
642         boost::shared_ptr<T> ptr = w_ptr.lock();
643
644         Gtk::ListStore::Children::iterator it;
645         Glib::RefPtr<Gtk::TreeSelection> selection;
646
647         selection = view.get_selection();
648
649         if (!ptr) {
650                 selection->unselect_all();
651         } else {
652                 for (it = list->children().begin(); it != list->children().end(); ++it) {
653                         if (it->get_value (cols.ptr) == ptr) {
654                                 if (select) {
655                                         selection->select (it);
656                                 } else {
657                                         selection->unselect (it);
658                                 }
659                                 break;
660                         }
661                 }
662         }
663
664         --applying_changes_from_engine;
665 }
666
667 void
668 ExportFormatDialog::change_quality_compatibility (bool compatibility, WeakQualityPtr quality)
669 {
670         change_compatibility<ExportFormatManager::QualityState, QualityCols> (compatibility, quality, quality_list, quality_cols);
671 }
672
673 void
674 ExportFormatDialog::change_format_compatibility (bool compatibility, WeakFormatPtr format)
675 {
676         change_compatibility<ExportFormat, FormatCols> (compatibility, format, format_list, format_cols);
677 }
678
679 void
680 ExportFormatDialog::change_sample_rate_compatibility (bool compatibility, WeakSampleRatePtr rate)
681 {
682         change_compatibility<ExportFormatManager::SampleRateState, SampleRateCols> (compatibility, rate, sample_rate_list, sample_rate_cols);
683 }
684
685 void
686 ExportFormatDialog::change_sample_format_compatibility (bool compatibility, WeakSampleFormatPtr format)
687 {
688         change_compatibility<HasSampleFormat::SampleFormatState, SampleFormatCols> (compatibility, format, sample_format_list, sample_format_cols);
689 }
690
691 void
692 ExportFormatDialog::change_dither_type_compatibility (bool compatibility, WeakDitherTypePtr type)
693 {
694         change_compatibility<HasSampleFormat::DitherTypeState, DitherTypeCols> (compatibility, type, dither_type_list, dither_type_cols, "red");
695 }
696
697 template<typename T, typename ColsT>
698 void
699 ExportFormatDialog::change_compatibility (bool compatibility, boost::weak_ptr<T> w_ptr, Glib::RefPtr<Gtk::ListStore> & list, ColsT & cols,
700                                           std::string const & c_incompatible, std::string const & c_compatible)
701 {
702         boost::shared_ptr<T> ptr = w_ptr.lock();
703
704         Gtk::ListStore::Children::iterator it;
705         for (it = list->children().begin(); it != list->children().end(); ++it) {
706                 if (it->get_value (cols.ptr) == ptr) {
707                         it->set_value (cols.color, compatibility ? c_compatible : c_incompatible);
708                         break;
709                 }
710         }
711 }
712
713 void
714 ExportFormatDialog::update_with_cue ()
715 {
716         manager.select_with_cue (with_cue.get_active());
717 }
718
719 void
720 ExportFormatDialog::update_with_toc ()
721 {
722         manager.select_with_toc (with_toc.get_active());
723 }
724
725 void
726 ExportFormatDialog::update_upload ()
727 {
728         manager.select_upload (upload_checkbox.get_active());
729 }
730
731 ExportFormatDialog::update_description()
732 {
733         std::string text = ": " + format->description(false);
734         name_generated_part.set_text(text);
735 }
736
737 void
738 ExportFormatDialog::update_name ()
739 {
740         manager.set_name (name_entry.get_text());
741 }
742
743 void
744 ExportFormatDialog::update_trim_start_selection ()
745 {
746         manager.select_trim_beginning (trim_start_checkbox.get_active());
747 }
748
749 void
750 ExportFormatDialog::update_trim_end_selection ()
751 {
752         manager.select_trim_end (trim_end_checkbox.get_active());
753 }
754
755 void
756 ExportFormatDialog::update_normalize_selection ()
757 {
758         manager.select_normalize (normalize_checkbox.get_active());
759         manager.select_normalize_target (normalize_spinbutton.get_value ());
760 }
761
762 void
763 ExportFormatDialog::update_silence_start_selection ()
764 {
765         update_time (silence_start, silence_start_clock);
766         AnyTime zero;
767         zero.type = AnyTime::Timecode;
768         manager.select_silence_beginning (silence_start_checkbox.get_active() ? silence_start : zero);
769 }
770
771 void
772 ExportFormatDialog::update_silence_end_selection ()
773 {
774         update_time (silence_end, silence_end_clock);
775         AnyTime zero;
776         zero.type = AnyTime::Timecode;
777         manager.select_silence_end (silence_end_checkbox.get_active() ? silence_end : zero);
778 }
779
780 void
781 ExportFormatDialog::update_clock (AudioClock & clock, ARDOUR::AnyTime const & time)
782 {
783         // TODO position
784         clock.set (_session->convert_to_frames (time), true);
785
786         AudioClock::Mode mode(AudioClock::Timecode);
787
788         switch (time.type) {
789           case AnyTime::Timecode:
790                 mode = AudioClock::Timecode;
791                 break;
792           case AnyTime::BBT:
793                 mode = AudioClock::BBT;
794                 break;
795           case AnyTime::Frames:
796                 mode = AudioClock::Frames;
797                 break;
798           case AnyTime::Seconds:
799                 mode = AudioClock::MinSec;
800                 break;
801         }
802
803         clock.set_mode (mode);
804 }
805
806 void
807 ExportFormatDialog::update_time (AnyTime & time, AudioClock const & clock)
808 {
809         if (!_session) {
810                 return;
811         }
812
813         framecnt_t frames = clock.current_duration();
814
815         switch (clock.mode()) {
816           case AudioClock::Timecode:
817                 time.type = AnyTime::Timecode;
818                 _session->timecode_time (frames, time.timecode);
819                 break;
820           case AudioClock::BBT:
821                 time.type = AnyTime::BBT;
822                 _session->bbt_time (frames, time.bbt);
823                 break;
824           case AudioClock::MinSec:
825                 time.type = AnyTime::Seconds;
826                 time.seconds = (double) frames / _session->frame_rate();
827                 break;
828           case AudioClock::Frames:
829                 time.type = AnyTime::Frames;
830                 time.frames = frames;
831                 break;
832         }
833 }
834
835 void
836 ExportFormatDialog::update_src_quality_selection ()
837 {
838         Gtk::TreeModel::const_iterator iter = src_quality_combo.get_active();
839         ExportFormatBase::SRCQuality quality = iter->get_value (src_quality_cols.id);
840         manager.select_src_quality (quality);
841 }
842
843 void
844 ExportFormatDialog::update_tagging_selection ()
845 {
846         manager.select_tagging (tag_checkbox.get_active());
847 }
848
849 void
850 ExportFormatDialog::change_encoding_options (ExportFormatPtr ptr)
851 {
852         empty_encoding_option_table ();
853
854         boost::shared_ptr<ARDOUR::ExportFormatLinear> linear_ptr;
855         boost::shared_ptr<ARDOUR::ExportFormatOggVorbis> ogg_ptr;
856         boost::shared_ptr<ARDOUR::ExportFormatFLAC> flac_ptr;
857         boost::shared_ptr<ARDOUR::ExportFormatBWF> bwf_ptr;
858
859         if ((linear_ptr = boost::dynamic_pointer_cast<ExportFormatLinear> (ptr))) {
860                 show_linear_enconding_options (linear_ptr);
861         } else if ((ogg_ptr = boost::dynamic_pointer_cast<ExportFormatOggVorbis> (ptr))) {
862                 show_ogg_enconding_options (ogg_ptr);
863         } else if ((flac_ptr = boost::dynamic_pointer_cast<ExportFormatFLAC> (ptr))) {
864                 show_flac_enconding_options (flac_ptr);
865         } else if ((bwf_ptr = boost::dynamic_pointer_cast<ExportFormatBWF> (ptr))) {
866                 show_bwf_enconding_options (bwf_ptr);
867         } else {
868                 std::cout << "Unrecognized format!" << std::endl;
869         }
870 }
871
872 void
873 ExportFormatDialog::empty_encoding_option_table ()
874 {
875         encoding_options_table.foreach (sigc::bind (sigc::mem_fun (*this, &ExportFormatDialog::remove_widget), &encoding_options_table));
876 }
877
878 void
879 ExportFormatDialog::remove_widget (Gtk::Widget & to_remove, Gtk::Container * remove_from)
880 {
881         remove_from->remove (to_remove);
882 }
883
884 void
885 ExportFormatDialog::show_linear_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatLinear> ptr)
886 {
887         /* Set label and pack table */
888
889         encoding_options_label.set_label (_("Linear encoding options"));
890
891         encoding_options_table.resize (2, 2);
892         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
893         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
894         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
895         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
896
897         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
898
899         show_all_children ();
900 }
901
902 void
903 ExportFormatDialog::show_ogg_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatOggVorbis> /*ptr*/)
904 {
905         encoding_options_label.set_label (_("Ogg Vorbis options"));
906
907         encoding_options_table.resize (1, 1);
908         encoding_options_table.attach (tag_checkbox, 0, 1, 0, 1);
909
910         update_tagging_selection ();
911
912         show_all_children ();
913 }
914
915 void
916 ExportFormatDialog::show_flac_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatFLAC> ptr)
917 {
918         encoding_options_label.set_label (_("FLAC options"));
919
920         encoding_options_table.resize (3, 2);
921         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
922         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
923         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
924         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
925         encoding_options_table.attach (tag_checkbox, 0, 2, 2, 3);
926
927         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
928
929         show_all_children ();
930 }
931
932 void
933 ExportFormatDialog::show_bwf_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatBWF> ptr)
934 {
935         encoding_options_label.set_label (_("Broadcast Wave options"));
936
937         encoding_options_table.resize (2, 2);
938         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
939         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
940         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
941         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
942
943         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
944
945         show_all_children ();
946 }
947
948 void
949 ExportFormatDialog::fill_sample_format_lists (boost::shared_ptr<ARDOUR::HasSampleFormat> ptr)
950 {
951         /* Fill lists */
952
953         Gtk::TreeModel::iterator iter;
954         Gtk::TreeModel::Row row;
955
956         sample_format_list->clear ();
957
958         HasSampleFormat::SampleFormatList const & formats = ptr->get_sample_formats ();
959
960         for (HasSampleFormat::SampleFormatList::const_iterator it = formats.begin(); it != formats.end(); ++it) {
961                 iter = sample_format_list->append();
962                 row = *iter;
963
964                 row[sample_format_cols.ptr] = *it;
965                 row[sample_format_cols.color] = (*it)->compatible() ? "white" : "red";
966                 row[sample_format_cols.label] = (*it)->name();
967
968                 if ((*it)->selected()) {
969                         sample_format_view.get_selection()->select (iter);
970                 }
971         }
972
973         dither_type_list->clear ();
974
975         HasSampleFormat::DitherTypeList const & types = ptr->get_dither_types ();
976
977         for (HasSampleFormat::DitherTypeList::const_iterator it = types.begin(); it != types.end(); ++it) {
978                 iter = dither_type_list->append();
979                 row = *iter;
980
981                 row[dither_type_cols.ptr] = *it;
982                 row[dither_type_cols.color] = "white";
983                 row[dither_type_cols.label] = (*it)->name();
984
985                 if ((*it)->selected()) {
986                         dither_type_view.get_selection()->select (iter);
987                 }
988         }
989 }
990
991 void
992 ExportFormatDialog::end_dialog ()
993 {
994         hide_all ();
995 }
996
997 void
998 ExportFormatDialog::prohibit_compatibility_selection ()
999 {
1000         compatibility_select_connection.block (true);
1001         compatibility_view.get_selection()->unselect_all ();
1002         compatibility_select_connection.block (false);
1003 }