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