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