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