fix dragging of region gain line, affected by coordinate system change caused by...
[ardour.git] / gtk2_ardour / export_dialog.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "export_dialog.h"
22
23 #include <sigc++/signal.h>
24
25 #include <pbd/filesystem.h>
26
27 #include <ardour/export_status.h>
28 #include <ardour/export_handler.h>
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 ExportDialog::ExportDialog (PublicEditor & editor, Glib::ustring title) :
34   ArdourDialog (title),
35   editor (editor),
36
37   warn_label ("", Gtk::ALIGN_LEFT),
38   list_files_label (_("<span color=\"#ffa755\">Some already existing files will be overwritten.</span>"), Gtk::ALIGN_RIGHT),
39   list_files_button (_("List files"))
40 { }
41
42 ExportDialog::~ExportDialog ()
43 { }
44
45 void
46 ExportDialog::set_session (ARDOUR::Session* s)
47 {
48         session = s;
49         
50         /* Init handler and profile manager */
51         
52         handler = session->get_export_handler ();
53         status = session->get_export_status ();
54         profile_manager.reset (new ExportProfileManager (*session));
55         
56         /* Possibly init stuff in derived classes */
57         
58         init ();
59         
60         /* Rest of session related initialization */
61         
62         preset_selector->set_manager (profile_manager);
63         file_notebook->set_session_and_manager (session, profile_manager);
64         
65         /* Hand on selection range to profile manager  */
66         
67         TimeSelection const & time (editor.get_selection().time);
68         if (!time.empty()) {
69                 profile_manager->set_selection_range (time.front().start, time.front().end);
70         } else {
71                 profile_manager->set_selection_range ();
72         }
73         
74         /* Load states */
75         
76         profile_manager->load_profile ();
77         sync_with_manager ();
78         
79         /* Warnings */
80         
81         preset_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::sync_with_manager));
82         timespan_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
83         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
84         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings));
85         status->Aborting.connect (sigc::mem_fun (*this, &ExportDialog::notify_errors));
86         
87         update_warnings ();
88 }
89
90 void
91 ExportDialog::init ()
92 {
93         init_components ();
94         init_gui ();
95
96         /* warnings */
97         
98         warning_widget.pack_start (warn_hbox, true, true, 6);
99         warning_widget.pack_end (list_files_hbox, false, false, 0);
100         
101         warn_hbox.pack_start (warn_label, true, true, 16);
102         warn_label.set_use_markup (true);
103         
104         list_files_hbox.pack_end (list_files_button, false, false, 6);
105         list_files_hbox.pack_end (list_files_label, false, false, 6);
106         list_files_label.set_use_markup (true);
107         
108         list_files_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::show_conflicting_files));
109         
110         /* Progress indicators */
111         
112         progress_widget.pack_start (progress_label, false, false, 6);
113         progress_widget.pack_start (progress_bar, false, false, 6);
114         
115         /* Buttons */
116         
117         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
118         rt_export_button = add_button (_("Realtime export"), RESPONSE_RT);
119         fast_export_button = add_button (_("Fast Export"), RESPONSE_FAST);
120         
121         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
122         rt_export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::export_rt));
123         fast_export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::export_fw));
124         
125         /* Done! */
126         
127         show_all_children ();
128         progress_widget.hide_all();
129 }
130
131 void
132 ExportDialog::init_gui ()
133 {
134         Gtk::Alignment * preset_align = Gtk::manage (new Gtk::Alignment());
135         preset_align->add (*preset_selector);
136         preset_align->set_padding (0, 12, 0, 0);
137         get_vbox()->pack_start (*preset_align, false, false, 0);
138         
139         Gtk::Alignment * timespan_align = Gtk::manage (new Gtk::Alignment());
140         Gtk::Label * timespan_label = Gtk::manage (new Gtk::Label (_("Time Span"), Gtk::ALIGN_LEFT));
141         timespan_align->add (*timespan_selector);
142         timespan_align->set_padding (0, 12, 18, 0);
143         get_vbox()->pack_start (*timespan_label, false, false, 0);
144         get_vbox()->pack_start (*timespan_align, false, false, 0);
145         
146         Gtk::Alignment * channels_align = Gtk::manage (new Gtk::Alignment());
147         Gtk::Label * channels_label = Gtk::manage (new Gtk::Label (_("Channels"), Gtk::ALIGN_LEFT));
148         channels_align->add (*channel_selector);
149         channels_align->set_padding (0, 12, 18, 0);
150         get_vbox()->pack_start (*channels_label, false, false, 0);
151         get_vbox()->pack_start (*channels_align, false, false, 0);
152         
153         get_vbox()->pack_start (*file_notebook, false, false, 0);
154         get_vbox()->pack_start (warning_widget, true, true, 0);
155         get_vbox()->pack_start (progress_widget, true, true, 0);
156         
157         Pango::AttrList bold;
158         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
159         bold.insert (b);
160         
161         timespan_label->set_attributes (bold);
162         channels_label->set_attributes (bold);
163 }
164
165 void
166 ExportDialog::init_components ()
167 {
168         preset_selector.reset (new ExportPresetSelector ());
169         timespan_selector.reset (new ExportTimespanSelectorMultiple ());
170         channel_selector.reset (new PortExportChannelSelector ());
171         file_notebook.reset (new ExportFileNotebook ());
172 }
173
174 void
175 ExportDialog::notify_errors ()
176 {
177         if (status->errors()) {
178                 Glib::ustring txt = _("Export has been aborted due to an error!\nSee the Log for details.");
179                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
180                 msg.run();
181         }
182 }
183
184 void
185 ExportDialog::close_dialog ()
186 {
187         if (status->running) {
188                 status->abort();
189         }
190         
191         hide_all ();
192         set_modal (false);
193         
194 }
195
196 void
197 ExportDialog::sync_with_manager ()
198 {
199         timespan_selector->set_state (profile_manager->get_timespans().front(), session);
200         channel_selector->set_state (profile_manager->get_channel_configs().front(), session);
201         file_notebook->sync_with_manager ();
202
203         update_warnings ();
204 }
205
206 void
207 ExportDialog::update_warnings ()
208 {
209         /* Reset state */
210
211         warn_string = "";
212         warn_label.set_markup (warn_string);
213
214         list_files_hbox.hide ();
215         list_files_string = "";
216         
217         fast_export_button->set_sensitive (true);
218         rt_export_button->set_sensitive (true);
219
220         /* Add new warnings */
221
222         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
223
224         for (std::list<Glib::ustring>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
225                 add_error (*it);
226         }
227
228         for (std::list<Glib::ustring>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
229                 add_warning (*it);
230         }
231
232         if (!warnings->conflicting_filenames.empty()) {
233                 list_files_hbox.show ();
234                 for (std::list<Glib::ustring>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
235                         ustring::size_type pos = it->find_last_of ("/");
236                         list_files_string += "\n" + it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>";
237                 }
238         }
239 }
240
241 void
242 ExportDialog::show_conflicting_files ()
243 {
244         ArdourDialog dialog (_("Files that will be overwritten"), true);
245         
246         Gtk::Label label ("", Gtk::ALIGN_LEFT);
247         label.set_use_markup (true);
248         label.set_markup (list_files_string);
249         
250         dialog.get_vbox()->pack_start (label);
251         dialog.add_button (Gtk::Stock::OK, 0);
252         dialog.show_all_children ();
253         
254         dialog.run();
255 }
256
257 void
258 ExportDialog::export_rt ()
259 {
260         profile_manager->prepare_for_export ();
261         handler->do_export (true);
262         show_progress ();
263 }
264
265 void
266 ExportDialog::export_fw ()
267 {
268         profile_manager->prepare_for_export ();
269         handler->do_export (false);
270         show_progress ();
271 }
272
273 void
274 ExportDialog::show_progress ()
275 {
276         status->running = true;
277
278         cancel_button->set_label (_("Stop Export"));
279         rt_export_button->set_sensitive (false);
280         fast_export_button->set_sensitive (false);
281
282         progress_bar.set_fraction (0.0);
283         warning_widget.hide_all();
284         progress_widget.show ();
285         progress_widget.show_all_children ();
286         progress_connection = Glib::signal_timeout().connect (mem_fun(*this, &ExportDialog::progress_timeout), 100);
287         
288         gtk_main_iteration ();
289         while (status->running) {
290                 if (gtk_events_pending()) {
291                         gtk_main_iteration ();
292                 } else {
293                         usleep (10000);
294                 }
295         }
296 }
297
298 gint
299 ExportDialog::progress_timeout ()
300 {
301         switch (status->stage) {
302           case export_None:
303                 progress_label.set_text ("");
304                 break;
305           case export_ReadTimespan:
306                 progress_label.set_text (string_compose (_("Reading timespan %1 of %2"), status->timespan, status->total_timespans));
307                 break;
308           case export_PostProcess:
309                 progress_label.set_text (string_compose (_("Processing file %2 of %3 (%1) from timespan %4 of %5"),
310                                                          file_notebook->get_nth_format_name (status->format),
311                                                          status->format, status->total_formats,
312                                                          status->timespan, status->total_timespans));
313                 break;
314           case export_Write:
315                 progress_label.set_text (string_compose (_("Encoding file %2 of %3 (%1) from timespan %4 of %5"),
316                                                          file_notebook->get_nth_format_name (status->format),
317                                                          status->format, status->total_formats,
318                                                          status->timespan, status->total_timespans));
319                 break;
320         }
321
322         progress_bar.set_fraction (status->progress);
323         return TRUE;
324 }
325
326 void
327 ExportDialog::add_error (Glib::ustring const & text)
328 {
329         fast_export_button->set_sensitive (false);
330         rt_export_button->set_sensitive (false);
331         
332         if (warn_string.empty()) {
333                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
334         } else {
335                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
336         }
337         
338         warn_label.set_markup (warn_string);
339 }
340
341 void
342 ExportDialog::add_warning (Glib::ustring const & text)
343 {
344         if (warn_string.empty()) {
345                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
346         } else {
347                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
348         }
349         
350         warn_label.set_markup (warn_string);
351 }
352
353 /*** Dialog specializations ***/
354
355 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, Glib::ustring range_id) :
356   ExportDialog (editor, _("Export Range")),
357   range_id (range_id)
358 {}
359
360 void
361 ExportRangeDialog::init_components ()
362 {
363         preset_selector.reset (new ExportPresetSelector ());
364         timespan_selector.reset (new ExportTimespanSelectorSingle (range_id));
365         channel_selector.reset (new PortExportChannelSelector ());
366         file_notebook.reset (new ExportFileNotebook ());
367 }
368
369 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
370   ExportDialog (editor, _("Export Selection"))
371 {}
372
373 void
374 ExportSelectionDialog::init_components ()
375 {
376         preset_selector.reset (new ExportPresetSelector ());
377         timespan_selector.reset (new ExportTimespanSelectorSingle (X_("selection")));
378         channel_selector.reset (new PortExportChannelSelector ());
379         file_notebook.reset (new ExportFileNotebook ());
380 }
381
382 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
383   ExportDialog (editor, _("Export Region")),
384   region (region),
385   track (track)
386 {}
387
388 void
389 ExportRegionDialog::init_components ()
390 {
391         Glib::ustring loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
392
393         preset_selector.reset (new ExportPresetSelector ());
394         timespan_selector.reset (new ExportTimespanSelectorSingle (loc_id));
395         channel_selector.reset (new RegionExportChannelSelector (region, track));
396         file_notebook.reset (new ExportFileNotebook ());
397 }