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