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