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