file checking logic and minor gui fixups to export dialog
[ardour.git] / gtk2_ardour / export_dialog.cc
1 /*
2     Copyright (C) 1999-2005 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <unistd.h>
21 #include <utility>
22 #include <sys/stat.h>
23
24 #include <fstream>
25
26 #include <samplerate.h>
27 #include <pbd/pthread_utils.h>
28 #include <pbd/xml++.h>
29 #include <pbd/dirname.h>
30
31 #include <gtkmm2ext/utils.h>
32 #include <ardour/export.h>
33 #include <ardour/sndfile_helpers.h>
34 #include <ardour/audio_track.h>
35 #include <ardour/audioregion.h>
36 #include <ardour/audioengine.h>
37 #include <ardour/gdither.h>
38 #include <ardour/utils.h>
39
40 #include "export_dialog.h"
41 #include "ardour_ui.h"
42 #include "public_editor.h"
43 #include "keyboard.h"
44 #include "ardour_message.h"
45
46 #include "i18n.h"
47
48 #define FRAME_NAME "BaseFrame"
49
50 using namespace std;
51
52 using namespace ARDOUR;
53 using namespace sigc;
54 using namespace Gtk;
55
56 static const gchar *sample_rates[] = {
57         N_("22.05kHz"),
58         N_("44.1kHz"),
59         N_("48kHz"),
60         N_("88.2kHz"),
61         N_("96kHz"),
62         N_("192kHz"),
63         0
64 };
65
66 static const gchar *src_quality[] = {
67         N_("best"),
68         N_("fastest"),
69         N_("linear"),
70         N_("better"),
71         N_("intermediate"),
72         0
73 };
74
75 static const gchar *dither_types[] = {
76         N_("None"),
77         N_("Rectangular"),
78         N_("Shaped Noise"),
79         N_("Triangular"),
80         0
81 };
82
83 static const gchar* channel_strings[] = {
84         N_("stereo"), 
85         N_("mono"), 
86         0
87 };
88
89 static const gchar* cue_file_types[] = {
90         N_("None"), 
91         N_("CUE"),
92         N_("TOC"),
93         0
94 };
95
96 ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
97         : ArdourDialog ("export dialog"),
98           editor (e),
99           format_table (9, 2),
100           format_frame (_("FORMAT")),
101           channel_count_label (_("CHANNELS")),
102           channel_count_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
103           header_format_label (_("FILE TYPE")),
104           header_format_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
105           bitdepth_format_label (_("SAMPLE FORMAT")),
106           bitdepth_format_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
107           endian_format_label (_("SAMPLE ENDIANNESS")),
108           endian_format_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
109           sample_rate_label (_("SAMPLE RATE")),
110           sample_rate_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
111           src_quality_label (_("CONVERSION QUALITY")),
112           src_quality_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
113           dither_type_label (_("DITHER TYPE")),
114           dither_type_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
115           cue_file_label (_("CD MARKER FILE TYPE")),
116           cue_file_align(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, 0.0, 0.0),
117           cuefile_only_checkbox (_("EXPORT CD MARKER FILE ONLY")),
118           file_frame (_("EXPORT TO FILE")),
119           file_browse_button (_("Browse")),
120           ok_button (_("Export")),
121           track_selector_button (_("Specific tracks ..."))
122 {
123         guint32 n;
124         guint32 len;
125         guint32 maxlen;
126
127         audio_region = r;
128
129         session = 0;
130         
131         set_title (_("ardour: export"));
132         set_wmclass (_("ardour_export"), "Ardour");
133         set_name ("ExportWindow");
134         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
135
136         file_selector = 0;
137         spec.running = false;
138
139         file_entry.set_name ("ExportFileNameEntry");
140
141         master_list = ListStore::create (exp_cols);
142         master_selector.set_model (master_list);
143
144         master_selector.set_name ("ExportTrackSelector");
145         master_selector.set_size_request (-1, 100);
146         master_selector.append_column(_("Output"), exp_cols.output);
147         master_selector.append_column_editable(_("Left"), exp_cols.left);
148         master_selector.append_column_editable(_("Right"), exp_cols.right);
149         master_selector.get_column(0)->set_min_width(100);
150         
151         master_selector.get_column(1)->set_min_width(40);
152         master_selector.get_column(1)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
153         master_selector.get_column(2)->set_min_width(40);
154         master_selector.get_column(2)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
155         master_selector.get_selection()->set_mode (Gtk::SELECTION_NONE);
156
157         track_list = ListStore::create (exp_cols);
158         track_selector.set_model (track_list);
159
160         track_selector.set_name ("ExportTrackSelector");
161         track_selector.set_size_request (-1, 130);
162         track_selector.append_column(_("Output"), exp_cols.output);
163         track_selector.append_column_editable(_("Left"), exp_cols.left);
164         track_selector.append_column_editable(_("Right"), exp_cols.right);
165
166         track_selector.get_column(0)->set_min_width(100);
167         track_selector.get_column(1)->set_min_width(40);
168         track_selector.get_column(1)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
169         track_selector.get_column(2)->set_min_width(40);
170         track_selector.get_column(2)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
171         track_selector.get_selection()->set_mode (Gtk::SELECTION_NONE);
172
173         progress_bar.set_name ("ExportProgress");
174
175         format_frame.add (format_table);
176         format_frame.set_name (FRAME_NAME);
177
178         track_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
179         master_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
180
181         get_vbox()->pack_start (file_frame, false, false);
182
183         hpacker.set_spacing (5);
184         hpacker.set_border_width (5);
185         hpacker.pack_start (format_frame, false, false);
186
187         if (!audio_region) {
188
189                 master_scroll.add (master_selector);
190                 track_scroll.add (track_selector);
191
192                 master_scroll.set_size_request (220, 100);
193                 track_scroll.set_size_request (220, 100);
194
195                 
196                 
197                 /* we may hide some of these later */
198                 track_vpacker.pack_start (master_scroll);
199                 track_vpacker.pack_start (track_scroll);
200                 track_vpacker.pack_start (track_selector_button, Gtk::PACK_EXPAND_PADDING);
201
202                 hpacker.pack_start (track_vpacker);
203         }
204
205         get_vbox()->pack_start (hpacker);
206         
207         track_selector_button.set_name ("EditorGTKButton");
208         track_selector_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::track_selector_button_click));
209
210         get_vbox()->pack_start (button_box, false, false);
211         get_vbox()->pack_start (progress_bar, false, false);
212
213         Gtkmm2ext::set_size_request_to_display_given_text (file_entry, X_("Kg/quite/a/reasonable/size/for/files/i/think"), 5, 8);
214
215         file_hbox.set_spacing (5);
216         file_hbox.set_border_width (5);
217         file_hbox.pack_start (file_entry, true, true);
218         file_hbox.pack_start (file_browse_button, false, false);
219
220         file_frame.add (file_hbox);
221         file_frame.set_border_width (5);
222         file_frame.set_name (FRAME_NAME);
223
224         /* pop_strings needs to be created on the stack because set_popdown_strings()
225          * takes a reference. */
226         vector<string> pop_strings = internationalize(sample_rates);
227         Gtkmm2ext::set_popdown_strings (sample_rate_combo, pop_strings);
228         pop_strings = internationalize(sample_rates);
229         Gtkmm2ext::set_popdown_strings (src_quality_combo, pop_strings);
230         pop_strings = internationalize(dither_types);
231         Gtkmm2ext::set_popdown_strings (dither_type_combo, pop_strings);
232         pop_strings = internationalize(channel_strings);
233         Gtkmm2ext::set_popdown_strings (channel_count_combo, pop_strings);
234         pop_strings = internationalize((const char **) sndfile_header_formats_strings);
235         Gtkmm2ext::set_popdown_strings (header_format_combo, pop_strings);
236         pop_strings = internationalize((const char **) sndfile_bitdepth_formats_strings);
237         Gtkmm2ext::set_popdown_strings (bitdepth_format_combo, pop_strings);
238         pop_strings = internationalize((const char **) sndfile_endian_formats_strings);
239         Gtkmm2ext::set_popdown_strings (endian_format_combo, pop_strings);
240         pop_strings = internationalize(cue_file_types);
241         Gtkmm2ext::set_popdown_strings (cue_file_combo, pop_strings);
242
243         /* this will re-sensitized as soon as a non RIFF/WAV
244            header format is chosen.
245         */
246
247         endian_format_combo.set_sensitive (false);
248
249         /* determine longest strings at runtime */
250
251         const guint32 FUDGE = 10; // Combo's are stupid - they steal space from the entry for the button
252
253         maxlen = 0;
254         const char *longest = "gl";
255         string longest_str;
256
257         for (n = 0; n < SNDFILE_HEADER_FORMATS; ++n) {
258                 if ((len = strlen (sndfile_header_formats_strings[n])) > maxlen) {
259                         maxlen = len;
260                         longest = sndfile_header_formats_strings[n];
261                 }
262         }
263
264         for (n = 0; n < SNDFILE_BITDEPTH_FORMATS; ++n) {
265                 if ((len = strlen (sndfile_bitdepth_formats_strings[n])) > maxlen) {
266                         maxlen = len;
267                         longest = sndfile_bitdepth_formats_strings[n];
268                 }
269         }
270
271         for (n = 0; n < SNDFILE_ENDIAN_FORMATS; ++n) {
272                 if ((len = strlen (sndfile_endian_formats_strings[n])) > maxlen) {
273                         maxlen = len;
274                         longest = sndfile_endian_formats_strings[n];
275                 }
276         }
277
278         longest_str = longest;
279
280         /* force ascender + descender */
281
282         longest_str[0] = 'g';
283         longest_str[1] = 'l';
284
285         //Gtkmm2ext::set_size_request_to_display_given_text (header_format_combo, longest_str.c_str(), 5+FUDGE, 5);
286
287         // TRANSLATORS: "slereg" is "stereo" with ascender and descender substituted
288         //Gtkmm2ext::set_size_request_to_display_given_text (channel_count_combo, _("slereg"), 5+FUDGE, 5);
289
290 /*      header_format_combo.set_focus_on_click (true);
291         bitdepth_format_combo.set_focus_on_click (true);
292         endian_format_combo.set_focus_on_click (true);
293         channel_count_combo.set_focus_on_click (true);
294         src_quality_combo.set_focus_on_click (true);
295         dither_type_combo.set_focus_on_click (true);
296         sample_rate_combo.set_focus_on_click (true);
297         cue_file_combo.set_focus_on_click (true);
298 */
299         dither_type_label.set_name ("ExportFormatLabel");
300         sample_rate_label.set_name ("ExportFormatLabel");
301         src_quality_label.set_name ("ExportFormatLabel");
302         channel_count_label.set_name ("ExportFormatLabel");
303         header_format_label.set_name ("ExportFormatLabel");
304         bitdepth_format_label.set_name ("ExportFormatLabel");
305         endian_format_label.set_name ("ExportFormatLabel");
306         cue_file_label.set_name ("ExportFormatLabel");
307
308         header_format_combo.set_name ("ExportFormatDisplay");
309         bitdepth_format_combo.set_name ("ExportFormatDisplay");
310         endian_format_combo.set_name ("ExportFormatDisplay");
311         channel_count_combo.set_name ("ExportFormatDisplay");
312         dither_type_combo.set_name ("ExportFormatDisplay");
313         src_quality_combo.set_name ("ExportFormatDisplay");
314         sample_rate_combo.set_name ("ExportFormatDisplay");
315         cue_file_combo.set_name ("ExportFormatDisplay");
316
317         cuefile_only_checkbox.set_name ("ExportCheckbox");
318
319         format_table.set_homogeneous (false);
320         format_table.set_border_width (5);
321         format_table.set_col_spacings (5);
322         format_table.set_row_spacings (5);
323
324         if (!audio_region) {
325                 channel_count_align.add(channel_count_label);
326                 format_table.attach (channel_count_align, 0, 1, 0, 1);
327                 format_table.attach (channel_count_combo, 1, 2, 0, 1);
328         }
329
330         header_format_align.add(header_format_label);
331         format_table.attach (header_format_align, 0, 1, 1, 2);
332         format_table.attach (header_format_combo, 1, 2, 1, 2);
333
334         bitdepth_format_align.add(bitdepth_format_label);
335         format_table.attach (bitdepth_format_align, 0, 1, 2, 3);
336         format_table.attach (bitdepth_format_combo, 1, 2, 2, 3);
337
338         endian_format_align.add(endian_format_label);
339         format_table.attach (endian_format_align, 0, 1, 3, 4);
340         format_table.attach (endian_format_combo, 1, 2, 3, 4);
341
342         sample_rate_align.add(sample_rate_label);
343         format_table.attach (sample_rate_align, 0, 1, 4, 5);
344         format_table.attach (sample_rate_combo, 1, 2, 4, 5);
345
346         src_quality_align.add(src_quality_label);
347         format_table.attach (src_quality_align, 0, 1, 5, 6);
348         format_table.attach (src_quality_combo, 1, 2, 5, 6);
349
350         dither_type_align.add(dither_type_label);
351         format_table.attach (dither_type_align, 0, 1, 6, 7);
352         format_table.attach (dither_type_combo, 1, 2, 6, 7);
353
354         cue_file_align.add(cue_file_label);
355         format_table.attach (cue_file_align, 0, 1, 7, 8);
356         format_table.attach (cue_file_combo, 1, 2, 7, 8);
357         format_table.attach (cuefile_only_checkbox, 0, 2, 8, 9);
358
359
360         button_box.set_border_width (6);
361         button_box.set_spacing (20);
362         button_box.set_homogeneous (true);
363
364         cancel_button.add (cancel_label);
365
366         button_box.pack_start (ok_button, false, true);
367         button_box.pack_start (cancel_button, false, true);
368         
369         ok_button.set_name ("EditorGTKButton");
370         cancel_button.set_name ("EditorGTKButton");
371         file_entry.set_name ("ExportFileDisplay");
372
373         signal_delete_event().connect (mem_fun(*this, &ExportDialog::window_closed));
374         ok_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::do_export));
375         cancel_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::end_dialog));
376         
377         file_browse_button.set_name ("EditorGTKButton");
378         file_browse_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::initiate_browse));
379
380         channel_count_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::channels_chosen));
381         bitdepth_format_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::bitdepth_chosen));
382         header_format_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::header_chosen));
383         sample_rate_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::sample_rate_chosen));
384         cue_file_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::cue_file_type_chosen));
385 }
386
387 ExportDialog::~ExportDialog()
388 {
389         if (file_selector) {
390                 delete file_selector;
391         }
392 }
393
394 void
395 ExportDialog::connect_to_session (Session *s)
396 {
397         session = s;
398         session->going_away.connect (mem_fun(*this, &Window::hide_all));
399
400         switch (session->frame_rate()) {
401         case 22050:
402                 sample_rate_combo.set_active_text (N_("22.05kHz"));
403                 break;
404         case 44100:
405                 sample_rate_combo.set_active_text (N_("44.1kHz"));
406                 break;
407         case 48000:
408                 sample_rate_combo.set_active_text (N_("48kHz"));
409                 break;
410         case 88200:
411                 sample_rate_combo.set_active_text (N_("88.2kHz"));
412                 break;
413         case 96000:
414                 sample_rate_combo.set_active_text (N_("96kHz"));
415                 break;
416         case 192000:
417                 sample_rate_combo.set_active_text (N_("192kHz"));
418                 break;
419         default:
420                 sample_rate_combo.set_active_text (N_("44.1kHz"));
421                 break;
422         }
423
424         src_quality_combo.set_sensitive (false);
425
426         set_state();
427 }
428
429 void
430 ExportDialog::set_state()
431 {
432         XMLNode* node = session->instant_xml(X_("ExportDialog"), session->path());
433         XMLProperty* prop;
434
435         if (node) {
436
437                 if ((prop = node->property (X_("sample_rate"))) != 0) {
438                         sample_rate_combo.set_active_text(prop->value());
439                 }
440                 if ((prop = node->property (X_("src_quality"))) != 0) {
441                         src_quality_combo.set_active_text(prop->value());
442                 }
443                 if ((prop = node->property (X_("dither_type"))) != 0) {
444                         dither_type_combo.set_active_text(prop->value());
445                 }
446                 if ((prop = node->property (X_("channel_count"))) != 0) {
447                         channel_count_combo.set_active_text(prop->value());
448                 }
449                 if ((prop = node->property (X_("header_format"))) != 0) {
450                         header_format_combo.set_active_text(prop->value());
451                 }
452                 if ((prop = node->property (X_("bitdepth_format"))) != 0) {
453                         bitdepth_format_combo.set_active_text(prop->value());
454                 }
455                 if ((prop = node->property (X_("endian_format"))) != 0) {
456                         endian_format_combo.set_active_text(prop->value());
457                 }
458                 if ((prop = node->property (X_("filename"))) != 0) {
459                         file_entry.set_text(prop->value());
460                 }
461                 if ((prop = node->property (X_("cue_file_type"))) != 0) {
462                         cue_file_combo.set_active_text(prop->value());
463                 }
464         }
465
466         header_chosen ();
467         bitdepth_chosen();
468         channels_chosen();
469         sample_rate_chosen();
470
471         if (session->master_out()) {
472                 track_scroll.hide ();
473         } else {
474                 master_scroll.hide ();
475                 track_selector_button.hide ();
476         }
477
478         if (!node) {
479                 return;
480         }
481
482         if (session->master_out()) {
483                 XMLNode* master = find_named_node(*node, (X_("Master")));
484                 int nchns;
485
486                 if (!master) {
487                         
488                         /* default is to use all */
489                         if (channel_count_combo.get_active_text() == _("mono")) {
490                                 nchns = 1;
491                         } else {
492                                 nchns = 2;
493                         }
494
495                         TreeModel::Children rows = master_selector.get_model()->children();
496                         for (uint32_t r = 0; r < session->master_out()->n_outputs(); ++r) {
497                                 if (nchns == 2) {
498                                         if (r % 2) {
499                                                 rows[r][exp_cols.right] = true;
500                                         } else {
501                                                 rows[r][exp_cols.left] = true;
502                                         }
503                                 } else {
504                                         rows[r][exp_cols.left] = true;
505                                 }
506                         }
507
508                 } else {
509                         /* XXX use XML state */
510                 }
511         }
512
513         XMLNode* tracks = find_named_node(*node, (X_("Tracks")));
514         if (!tracks) {
515                 return;
516         }
517         
518         XMLNodeList track_list = tracks->children(X_("Track"));
519         TreeModel::Children rows = track_selector.get_model()->children();
520         TreeModel::Children::iterator ri = rows.begin();
521         TreeModel::Row row;
522
523         for (XMLNodeIterator it = track_list.begin(); it != track_list.end(); ++it, ++ri) {
524                 if (ri == rows.end()){
525                         break;
526                 }
527
528                 XMLNode* track = *it;
529                 row = *ri;
530
531                 if ((prop = track->property(X_("channel1"))) != 0) {
532                         if (prop->value() == X_("on")) {
533                                 row[exp_cols.left] = true;
534                         } else {
535                                 row[exp_cols.left] = false;
536                         }
537                 }
538
539                 if ((prop = track->property(X_("channel2"))) != 0) {
540                         if (prop->value() == X_("on")) {
541                                 row[exp_cols.right] = true;
542                         } else {
543                                 row[exp_cols.right] = false;
544                         }
545                 }
546         }
547 }
548
549 void
550 ExportDialog::save_state()
551 {
552         if (!session) {
553                 return;
554         }
555
556         XMLNode* node = new XMLNode(X_("ExportDialog"));
557
558         node->add_property(X_("sample_rate"), sample_rate_combo.get_active_text());
559         node->add_property(X_("src_quality"), src_quality_combo.get_active_text());
560         node->add_property(X_("dither_type"), dither_type_combo.get_active_text());
561         node->add_property(X_("channel_count"), channel_count_combo.get_active_text());
562         node->add_property(X_("header_format"), header_format_combo.get_active_text());
563         node->add_property(X_("bitdepth_format"), bitdepth_format_combo.get_active_text());
564         node->add_property(X_("endian_format"), endian_format_combo.get_active_text());
565         node->add_property(X_("filename"), file_entry.get_text());
566         node->add_property(X_("cue_file_type"), cue_file_combo.get_active_text());
567
568         XMLNode* tracks = new XMLNode(X_("Tracks"));
569
570         TreeModel::Children rows = track_selector.get_model()->children();
571         TreeModel::Row row;
572         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri) {
573                 XMLNode* track = new XMLNode(X_("Track"));
574
575                 row = *ri;
576                 track->add_property(X_("channel1"), row[exp_cols.left] ? X_("on") : X_("off"));
577                 track->add_property(X_("channel1"), row[exp_cols.right] ? X_("on") : X_("off"));
578
579                 tracks->add_child_nocopy(*track);
580         }
581         node->add_child_nocopy(*tracks);
582         
583         session->add_instant_xml(*node, session->path());
584 }
585
586 void
587 ExportDialog::set_range (jack_nframes_t start, jack_nframes_t end)
588 {
589         spec.start_frame = start;
590         spec.end_frame = end;
591
592         if (!audio_region) {
593                 // XXX: this is a hack until we figure out what is really wrong
594                 session->request_locate (spec.start_frame, false);
595         }
596 }
597
598 gint
599 ExportDialog::progress_timeout ()
600 {
601         progress_bar.set_fraction (spec.progress/100);
602         return TRUE;
603 }
604
605 void*
606 ExportDialog::_export_region_thread (void *arg)
607 {
608         PBD::ThreadCreated (pthread_self(), X_("Export Region"));
609
610         static_cast<ExportDialog*>(arg)->export_region ();
611         return 0;
612 }
613
614 void
615 ExportDialog::export_region ()
616 {
617         audio_region->exportme (*session, spec);
618 }
619
620 void
621 frames_to_cd_frames_string (char* buf, jack_nframes_t when, jack_nframes_t fr)
622 {
623
624   long unsigned int remainder;
625   int mins, secs, frames;
626
627         mins = when / (60 * fr);
628         remainder = when - (mins * 60 * fr);
629         secs = remainder / fr;
630         remainder -= secs * fr;
631         frames = remainder / (fr / 75);
632         sprintf (buf, " %02d:%02d:%02d", mins, secs, frames);
633
634 }
635
636 struct LocationSortByStart {
637     bool operator() (Location *a, Location *b) {
638             return a->start() < b->start();
639     }
640 };
641
642 void
643 ExportDialog::export_toc_file (Locations::LocationList& locations, const string& path)
644 {
645         
646         string filepath = path + ".toc";
647         ofstream out (filepath.c_str());
648         long unsigned int last_end_time = spec.start_frame, last_start_time = spec.start_frame;
649         int numtracks = 0;
650         gchar buf[18];
651
652         if (!out) {
653                 error << string_compose(_("Editor: cannot open \"%1\" as export file for CD toc file"), filepath) << endmsg;
654                 return;
655         }
656         out << "CD_DA" << endl;
657         out << "CD_TEXT {" << endl << "  LANGUAGE_MAP {" << endl << "    0 : EN" << endl << "  }" << endl;
658         out << "  LANGUAGE 0 {" << endl << "    TITLE \"" << session->name() << "\"" << endl << "  }" << endl << "}" << endl;
659
660         Locations::LocationList::iterator i;
661         Locations::LocationList temp;
662
663         for (i = locations.begin(); i != locations.end(); ++i) {
664           if ((*i)->start() >= spec.start_frame && (*i)->end() <= spec.end_frame && (*i)->is_cd_marker() && !(*i)->is_end()) {
665             temp.push_back (*i);
666             if (!(*i)->is_mark()) {
667               numtracks ++;
668             }
669           }
670         }
671
672         if (numtracks == 0 ) {
673                     /* the user supplied no track markers.
674                        we now treat the session as one track.*/
675
676                     out << endl << "TRACK AUDIO" << endl;
677                    
678                     out << "COPY" << endl;
679
680                     out << "NO PRE_EMPHASIS" << endl;
681    
682                     /* XXX add session properties for catalog etc.
683                        (so far only the session name is used) */
684                     
685                     out << "CD_TEXT {" << endl << "  LANGUAGE 0 {" << endl << "     TITLE \"" << session->name() << "\"" << endl;
686                     out << "  }" << endl << "}" << endl;
687
688                     out << "FILE \"" << path << "\" ";
689                     out << "00:00:00 " ;
690                     frames_to_cd_frames_string (buf, spec.end_frame - spec.start_frame, session->frame_rate());
691                     out << buf << endl;
692                     out << "START 00:00:00" << endl;
693
694                     last_start_time = spec.start_frame;
695                     last_end_time = spec.end_frame;
696         } 
697
698         if (temp.size()) {
699                 LocationSortByStart cmp;
700                 temp.sort (cmp);
701
702                 for (i = temp.begin(); i != temp.end(); ++i) {
703         
704                       if (!(*i)->is_mark()) {
705                         /*this is a track */
706                         out << endl << "TRACK AUDIO" << endl;
707
708                         if ((*i)->cd_info.find("scms") != (*i)->cd_info.end())  {
709                           out << "NO ";
710                         }
711                         out << "COPY" << endl;
712
713                         if ((*i)->cd_info.find("preemph") != (*i)->cd_info.end())  {
714                           out << "PRE_EMPHASIS" << endl;
715                         } else {
716                           out << "NO PRE_EMPHASIS" << endl;
717                         }
718
719                         if ((*i)->cd_info.find("isrc") != (*i)->cd_info.end())  {
720                           out << "ISRC \"" << (*i)->cd_info["isrc"] << "\"" << endl;
721                         }
722
723                         out << "CD_TEXT {" << endl << "  LANGUAGE 0 {" << endl << "     TITLE \"" << (*i)->name() << "\"" << endl;
724                         if ((*i)->cd_info.find("performer") != (*i)->cd_info.end()) {
725                           out << "     PERFORMER \"" << (*i)->cd_info["performer"]  << "\"" << endl;
726                         }
727                         if ((*i)->cd_info.find("string_composer") != (*i)->cd_info.end()) {
728                           out  << "     COMPOSER \"" << (*i)->cd_info["string_composer"] << "\"" << endl;
729                         }
730
731                         if ((*i)->cd_info.find("isrc") != (*i)->cd_info.end()) {                          
732                           out  << "     ISRC \"";
733                           out << (*i)->cd_info["isrc"].substr(0,2) << "-";
734                           out << (*i)->cd_info["isrc"].substr(2,3) << "-";
735                           out << (*i)->cd_info["isrc"].substr(5,2) << "-";
736                           out << (*i)->cd_info["isrc"].substr(7,5) << "\"" << endl;
737                         }
738
739                         out << "  }" << endl << "}" << endl;
740
741                         frames_to_cd_frames_string (buf, last_end_time - spec.start_frame, session->frame_rate());
742                         out << "FILE \"" << path << "\" " << buf;
743
744                         frames_to_cd_frames_string (buf, (*i)->end() - last_end_time, session->frame_rate());
745                         out << buf << endl;
746
747                         frames_to_cd_frames_string (buf, (*i)->start() - last_end_time, session->frame_rate());
748                         out << "START" << buf << endl;
749                         
750                         last_start_time = (*i)->start();
751                         last_end_time = (*i)->end();
752                  
753
754                       } else  if ((*i)->start() < last_end_time) {
755                         /* this is an index within a track */
756                         
757                         frames_to_cd_frames_string (buf, (*i)->start() - last_start_time, session->frame_rate());
758                         out << "INDEX" << buf << endl;
759                       }
760                 }
761         }
762         
763 }
764
765 void
766 ExportDialog::export_cue_file (Locations::LocationList& locations, const string& path)
767 {
768         string filepath = path + ".cue";
769         ofstream out (filepath.c_str());
770         gchar buf[18];
771         long unsigned int last_track_end = spec.start_frame;
772         int numtracks = 0, tracknum = 0, indexnum = 0;
773
774         if (!out) {
775                 error << string_compose(_("Editor: cannot open \"%1\" as export file for CD cue file"), filepath) << endmsg;
776                 return;
777         }
778
779         Locations::LocationList::iterator i;
780         Locations::LocationList temp;
781
782         for (i = locations.begin(); i != locations.end(); ++i) {
783                 if ((*i)->start() >= spec.start_frame && (*i)->end() <= spec.end_frame && (*i)->is_cd_marker() && !(*i)->is_end()) {
784                         temp.push_back (*i);
785                         if (!(*i)->is_mark()) {
786                                 numtracks++;
787                         }
788                 }
789         }
790         
791         out << "REM Cue file generated by Ardour" << endl;
792         out << "TITLE \"" << session->name() << "\"" << endl;
793
794         if ((header_format_combo.get_active_text() == N_("WAV"))) {
795                   out << "FILE " << path  << " WAVE" << endl;
796         } else {
797                   out << "FILE " << path  << ' ' << (header_format_combo.get_active_text()) << endl;
798         }
799
800         if (numtracks == 0) {
801                     /* the user has supplied no track markers.
802                        the entire export is treated as one track. 
803                     */
804
805                   numtracks++;
806                   tracknum++;
807                   indexnum = 0;
808                   out << endl << "TRACK " << tracknum << " AUDIO" << endl;
809                   out << "FLAGS " ;
810                   
811                   out << "DCP " << endl;                   
812                   
813                   /* use the session name*/
814                   
815                   if (session->name() != "") {
816                     out << "TITLE \"" << session->name() << "\"" << endl;
817                   }           
818                   
819                   /* no pregap in this case */
820
821                   out << "INDEX 00 00:00:00" << endl;
822                   indexnum++;
823                   out << "INDEX 01 00:00:00" << endl;
824                   indexnum++;
825                   last_track_end = spec.end_frame;
826         }
827
828         if (temp.size()) {
829                 LocationSortByStart cmp;
830                 temp.sort (cmp);
831
832                 for ( i = temp.begin(); i != temp.end(); ++i) {
833
834                     if (!(*i)->is_mark() && ((*i)->start() >= last_track_end)) {
835                       /* this is a track and it doesn't start inside another one*/
836                       
837                       tracknum++;
838                       indexnum = 0;
839                       out << endl << "TRACK " << tracknum << " AUDIO" << endl;
840                       out << "FLAGS " ;
841                       
842                       if ((*i)->cd_info.find("scms") != (*i)->cd_info.end())  {
843                         out << "SCMS ";
844                       } else {
845                         out << "DCP ";
846                       }
847                       
848                       if ((*i)->cd_info.find("preemph") != (*i)->cd_info.end())  {
849                         out << "PRE";
850                       }
851                       out << endl;
852                       
853                       if ((*i)->cd_info.find("isrc") != (*i)->cd_info.end())  {
854                         out << "ISRC " << (*i)->cd_info["isrc"] << endl;
855                         
856                       }
857                       if ((*i)->name() != "") {
858                         out << "TITLE \"" << (*i)->name() << "\"" << endl;
859                       }       
860                       
861                       if ((*i)->cd_info.find("performer") != (*i)->cd_info.end()) {
862                         out << "PERFORMER \"" <<  (*i)->cd_info["performer"] << "\"" << endl;
863                       }
864                       
865                       if ((*i)->cd_info.find("string_composer") != (*i)->cd_info.end()) {
866                         out << "SONGWRITER \"" << (*i)->cd_info["string_composer"]  << "\"" << endl;
867                       }
868                         snprintf (buf, sizeof(buf), "INDEX %02d", indexnum);
869                         out << buf;
870                         frames_to_cd_frames_string (buf, last_track_end - spec.start_frame, session->frame_rate());
871                         out << buf << endl;
872                         indexnum++;
873                         last_track_end = (*i)->end();
874                     } 
875                     if ((tracknum > 0) && ((*i)->start() < last_track_end)) {
876                       /*this is an index and it lies within a track*/
877                       snprintf (buf, sizeof(buf), "INDEX %02d", indexnum);
878                       out << buf;
879                       frames_to_cd_frames_string (buf,(*i)->start() - spec.start_frame, session->frame_rate());
880                       out << buf << endl;
881                       indexnum++;
882                     }
883                 }
884         }
885         
886 }
887
888 void
889 ExportDialog::do_export_cd_markers (const string& path,const string& cuefile_type)
890 {
891         if (cuefile_type == "TOC") {
892                 session->locations()->apply (*this, &ExportDialog::export_toc_file, path);      
893         } else {
894                 session->locations()->apply (*this, &ExportDialog::export_cue_file, path);
895         }
896 }
897
898
899 void
900 ExportDialog::do_export ()
901 {
902         // sanity check file name first
903         string filepath = file_entry.get_text();
904         struct stat statbuf;
905   
906         if (filepath.empty()) {
907                 // warning dialog
908                 string txt = _("Please enter a valid filename.");
909                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
910                 msg.run();
911                 return;
912         }
913         
914         // check if file exists already and warn
915         if (stat (filepath.c_str(), &statbuf) == 0) {
916                 if (S_ISDIR (statbuf.st_mode)) {
917                         string txt = _("Please specify a complete filename for the audio file.");
918                         MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
919                         msg.run();
920                         return;
921                 }
922                 else {
923                         string txt = _("File already exists, do you want to overwrite it?");
924                         MessageDialog msg (*this, txt, false, MESSAGE_QUESTION, BUTTONS_YES_NO, true);
925                         //ArdourMessage msg (this, X_("exportoverwrite"), txt, true, false, Gtk::BUTTONS_YES_NO);
926                         if ((ResponseType) msg.run() == Gtk::RESPONSE_NO) {
927                                 return;
928                         }
929                 }
930         }
931         
932         // directory needs to exist and be writable
933         string dirpath = PBD::dirname (filepath);
934         if (::access (dirpath.c_str(), W_OK) != 0) {
935                 string txt = _("Cannot write file in: ") + dirpath;
936                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
937                 msg.run();
938                 return;
939         }
940         
941         if (cue_file_combo.get_active_text () != _("None")) {
942                 do_export_cd_markers (file_entry.get_text(), cue_file_combo.get_active_text ());
943         }
944
945         if (cuefile_only_checkbox.get_active()) {
946                 end_dialog ();
947                 return;
948         }
949
950         ok_button.set_sensitive(false);
951         save_state();
952         
953         set_modal (true);
954         
955         spec.path = filepath;
956         spec.progress = 0;
957         spec.running = true;
958         spec.stop = false;
959         spec.port_map.clear();
960         
961         if (channel_count_combo.get_active_text() == _("mono")) {
962                 spec.channels = 1;
963         } else {
964                 spec.channels = 2;
965         }
966
967         spec.format = 0;
968
969         spec.format |= sndfile_header_format_from_string (header_format_combo.get_active_text ());
970         
971         if ((spec.format & SF_FORMAT_WAV) == 0) {
972                 /* RIFF/WAV specifies endianess */
973                 spec.format |= sndfile_endian_format_from_string (endian_format_combo.get_active_text ());
974         }
975
976         spec.format |= sndfile_bitdepth_format_from_string (bitdepth_format_combo.get_active_text ());
977
978         string sr_str = sample_rate_combo.get_active_text();
979         if (sr_str == N_("22.05kHz")) {
980                 spec.sample_rate = 22050;
981         } else if (sr_str == N_("44.1kHz")) {
982                 spec.sample_rate = 44100;
983         } else if (sr_str == N_("48kHz")) {
984                 spec.sample_rate = 48000;
985         } else if (sr_str == N_("88.2kHz")) {
986                 spec.sample_rate = 88200;
987         } else if (sr_str == N_("96kHz")) {
988                 spec.sample_rate = 96000;
989         } else if (sr_str == N_("192kHz")) {
990                 spec.sample_rate = 192000;
991         } else {
992                 spec.sample_rate = session->frame_rate();
993         }
994         
995         string src_str = src_quality_combo.get_active_text();
996         if (src_str == _("fastest")) {
997                 spec.src_quality = SRC_ZERO_ORDER_HOLD;
998         } else if (src_str == _("linear")) {
999                 spec.src_quality = SRC_LINEAR;
1000         } else if (src_str == _("better")) {
1001                 spec.src_quality = SRC_SINC_FASTEST;
1002         } else if (src_str == _("intermediate")) {
1003                 spec.src_quality = SRC_SINC_MEDIUM_QUALITY;
1004         } else {
1005                 spec.src_quality = SRC_SINC_BEST_QUALITY;
1006         }
1007
1008         string dither_str = dither_type_combo.get_active_text();
1009         if (dither_str == _("None")) {
1010                 spec.dither_type = GDitherNone;
1011         } else if (dither_str == _("Rectangular")) {
1012                 spec.dither_type = GDitherRect;
1013         } else if (dither_str == _("Triangular")) {
1014                 spec.dither_type = GDitherTri;
1015         } else {
1016                 spec.dither_type = GDitherShaped;
1017         } 
1018
1019         if (!audio_region) {
1020
1021                 uint32_t chan=0;
1022                 Port *last_port = 0;
1023                 
1024                 TreeModel::Children rows = master_selector.get_model()->children();
1025                 TreeModel::Children::iterator ri;
1026                 TreeModel::Row row;
1027                 for (ri = rows.begin(); ri != rows.end(); ++ri) {
1028                         row = *ri;
1029                         Port* port = row[exp_cols.port];
1030                         
1031                         if (last_port != port) {
1032                                 chan = 0;
1033                         }
1034                         
1035                         if (row[exp_cols.left]) {
1036                                 spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
1037                         } 
1038                         
1039                         if (spec.channels == 2) {
1040                                 if (row[exp_cols.right]) {
1041                                         spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
1042                                 }
1043                         }
1044                 }
1045
1046                 chan = 0;
1047
1048                 rows = track_selector.get_model()->children();
1049                 for (ri = rows.begin(); ri != rows.end(); ++ri) {
1050                         row = *ri;
1051                         
1052                         Port* port = row[exp_cols.port];
1053                         
1054                         if (last_port != port) {
1055                                 chan = 0;
1056                         }
1057                         
1058                         if (row[exp_cols.left]) {
1059                                 spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
1060                         } 
1061                         
1062                         if (spec.channels == 2) {
1063                                 if (row[exp_cols.right]) {
1064                                         spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
1065                                 }
1066                                 
1067                         }
1068                         
1069                         last_port = port;
1070                         ++chan;
1071                 }
1072         }
1073
1074         progress_connection = Glib::signal_timeout().connect (mem_fun(*this, &ExportDialog::progress_timeout), 100);
1075         cancel_label.set_text (_("Stop Export"));
1076
1077         if (!audio_region) {
1078                 if (session->start_audio_export (spec)) {
1079                         goto out;
1080                 }
1081         } else {
1082                 pthread_t thr;
1083                 pthread_create_and_store ("region export", &thr, 0, ExportDialog::_export_region_thread, this);
1084         }
1085
1086         gtk_main_iteration ();
1087         while (spec.running) {
1088                 if (gtk_events_pending()) {
1089                         gtk_main_iteration ();
1090                 } else {
1091                         usleep (10000);
1092                 }
1093         }
1094         
1095   out:
1096         progress_connection.disconnect ();
1097         end_dialog ();
1098 }
1099         
1100
1101 void
1102 ExportDialog::end_dialog ()
1103 {
1104
1105         if (spec.running) {
1106                 spec.stop = true;
1107
1108                 while (spec.running) {
1109                         if (gtk_events_pending()) {
1110                                 gtk_main_iteration ();
1111                         } else {
1112                                 usleep (10000);
1113                         }
1114                 }
1115         }
1116
1117         session->engine().freewheel (false);
1118
1119         hide_all ();
1120
1121         if (file_selector) {
1122                 file_selector->hide_all ();
1123         }
1124
1125         set_modal (false);
1126         ok_button.set_sensitive(true);
1127 }
1128
1129 void
1130 ExportDialog::start_export ()
1131 {
1132         if (session == 0) {
1133                 return;
1134         }
1135
1136         /* If it the filename hasn't been set before, use the
1137            directory above the current session as a default
1138            location for the export.  
1139         */
1140         
1141         if (file_entry.get_text().length() == 0) {
1142                 string dir = session->path();
1143                 string::size_type last_slash;
1144                 
1145                 if ((last_slash = dir.find_last_of ('/')) != string::npos) {
1146                         dir = dir.substr (0, last_slash+1);
1147                 }
1148
1149                 dir = dir + "export.wav";
1150                 
1151                 file_entry.set_text (dir);
1152         }
1153         
1154         progress_bar.set_fraction (0);
1155         cancel_label.set_text (_("Cancel"));
1156
1157         show_all ();
1158
1159         if (session->master_out()) {
1160                 track_scroll.hide ();
1161         } else {
1162                 master_scroll.hide ();
1163                 track_selector_button.hide ();
1164         }
1165 }
1166
1167 void
1168 ExportDialog::header_chosen ()
1169 {
1170         if (sndfile_header_format_from_string (header_format_combo.get_active_text ()) == SF_FORMAT_WAV) {
1171                 endian_format_combo.set_sensitive (false);
1172         } else {
1173                 endian_format_combo.set_sensitive (true);
1174         }
1175 }
1176
1177 void
1178 ExportDialog::bitdepth_chosen ()
1179 {
1180         int format = sndfile_bitdepth_format_from_string (bitdepth_format_combo.get_active_text ());    
1181         switch (format) {
1182         case SF_FORMAT_PCM_24:
1183         case SF_FORMAT_PCM_32:
1184         case SF_FORMAT_FLOAT:
1185                 dither_type_combo.set_sensitive (false);
1186                 break;
1187
1188         default:
1189                 dither_type_combo.set_sensitive (true);
1190                 break;
1191         }
1192 }
1193
1194 void
1195 ExportDialog::cue_file_type_chosen ()
1196 {
1197         if (cue_file_combo.get_active_text () != "None") {
1198                 cuefile_only_checkbox.set_sensitive (true);
1199         } else {
1200                 cuefile_only_checkbox.set_active (false);
1201                 cuefile_only_checkbox.set_sensitive (false);
1202         }
1203 }
1204
1205 void
1206 ExportDialog::sample_rate_chosen ()
1207 {
1208         string sr_str = sample_rate_combo.get_active_text();
1209         jack_nframes_t rate;
1210
1211         if (sr_str == N_("22.05kHz")) {
1212                 rate = 22050;
1213         } else if (sr_str == N_("44.1kHz")) {
1214                 rate = 44100;
1215         } else if (sr_str == N_("48kHz")) {
1216                 rate = 48000;
1217         } else if (sr_str == N_("88.2kHz")) {
1218                 rate = 88200;
1219         } else if (sr_str == N_("96kHz")) {
1220                 rate = 96000;
1221         } else if (sr_str == N_("192kHz")) {
1222                 rate = 192000;
1223         } else {
1224                 rate = session->frame_rate();
1225         }
1226                 
1227         if (rate != session->frame_rate()) {
1228                 src_quality_combo.set_sensitive (true);
1229         } else {
1230                 src_quality_combo.set_sensitive (false);
1231         }
1232 }
1233
1234 void
1235 ExportDialog::channels_chosen ()
1236 {
1237         bool mono;
1238
1239         mono = (channel_count_combo.get_active_text() == _("mono"));
1240
1241         if (mono) {
1242                 track_selector.get_column(2)->set_visible(false);
1243                 track_selector.get_column(1)->set_title(_("Export"));
1244
1245                 if (session->master_out()) {
1246                         master_selector.get_column(2)->set_visible(false);
1247                         master_selector.get_column(1)->set_title(_("Export"));
1248                 }
1249
1250         } else {
1251                 track_selector.get_column(2)->set_visible(true);
1252                 track_selector.get_column(1)->set_title(_("Left"));
1253
1254                 if (session->master_out()) {
1255                         master_selector.get_column(2)->set_visible(true);
1256                         master_selector.get_column(1)->set_title(_("Left"));
1257                 }
1258         }
1259
1260         fill_lists();
1261 }
1262
1263 void
1264 ExportDialog::fill_lists ()
1265 {
1266         track_list->clear();
1267         master_list->clear();
1268         
1269         Session::RouteList routes = session->get_routes ();
1270
1271         for (Session::RouteList::iterator ri = routes.begin(); ri != routes.end(); ++ri) {
1272
1273                 Route* route = (*ri);
1274                 
1275                 if (route->hidden()) {
1276                         continue;
1277                 }
1278
1279                 for (uint32_t i=0; i < route->n_outputs(); ++i) {
1280                         string name;
1281                         if (route->n_outputs() == 1) {
1282                                 name = route->name();
1283                         } else {
1284                                 name = string_compose("%1: out-%2", route->name(), i+1);
1285                         }
1286
1287                         if (route == session->master_out()) {
1288                                 TreeModel::iterator iter = master_list->append();
1289                                 TreeModel::Row row = *iter;
1290                                 row[exp_cols.output] = name;
1291                                 row[exp_cols.left] = false;
1292                                 row[exp_cols.right] = false;
1293                                 row[exp_cols.port] = route->output (i);
1294                         } else {
1295                                 TreeModel::iterator iter = track_list->append();
1296                                 TreeModel::Row row = *iter;
1297                                 row[exp_cols.output] = name;
1298                                 row[exp_cols.left] = false;
1299                                 row[exp_cols.right] = false;
1300                                 row[exp_cols.port] = route->output (i);
1301                         }
1302                 }
1303         }
1304 }
1305
1306 gint
1307 ExportDialog::window_closed (GdkEventAny *ignored)
1308 {
1309         end_dialog ();
1310         return TRUE;
1311 }
1312 void
1313 ExportDialog::initiate_browse ()
1314 {
1315         if (file_selector == 0) {
1316                 file_selector = new FileSelection;
1317                 file_selector->set_modal (true);
1318
1319                 file_selector->get_cancel_button()->signal_clicked().connect (bind (mem_fun(*this, &ExportDialog::finish_browse), -1));
1320                 file_selector->get_ok_button()->signal_clicked().connect (bind (mem_fun(*this, &ExportDialog::finish_browse), 1));
1321         }
1322         file_selector->set_filename (file_entry.get_text());
1323         file_selector->show_all ();
1324 }
1325
1326 void
1327 ExportDialog::finish_browse (int status)
1328 {
1329         if (file_selector) {
1330                 if (status > 0) {
1331                         string result = file_selector->get_filename();
1332                         
1333                         if (result.length()) {
1334                                 file_entry.set_text (result);
1335                         }
1336                 }
1337                 file_selector->hide_all();
1338         }
1339 }
1340
1341 void
1342 ExportDialog::track_selector_button_click ()
1343 {
1344         if (track_scroll.is_visible ()) {
1345                 track_scroll.hide ();
1346         } else {
1347                 track_scroll.show_all ();
1348         }
1349 }