merge hv v6, rework trace methods
[goodguy/history.git] / cinelerra-5.1 / cinelerra / batchrender.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "batchrender.h"
24 #include "bcdisplayinfo.h"
25 #include "bcsignals.h"
26 #include "confirmsave.h"
27 #include "cstrdup.h"
28 #include "bchash.h"
29 #include "edits.h"
30 #include "edit.h"
31 #include "edl.h"
32 #include "edlsession.h"
33 #include "errorbox.h"
34 #include "file.h"
35 #include "filesystem.h"
36 #include "filexml.h"
37 #include "keyframe.h"
38 #include "keys.h"
39 #include "labels.h"
40 #include "language.h"
41 #include "mainerror.h"
42 #include "mainundo.h"
43 #include "mainsession.h"
44 #include "mutex.h"
45 #include "mwindow.h"
46 #include "mwindowgui.h"
47 #include "packagedispatcher.h"
48 #include "packagerenderer.h"
49 #include "plugin.h"
50 #include "pluginset.h"
51 #include "preferences.h"
52 #include "render.h"
53 #include "theme.h"
54 #include "tracks.h"
55 #include "transportque.h"
56 #include "vframe.h"
57
58
59 static const char *list_titles[] =
60 {
61         _("Enabled"),
62         _("Output"),
63         _("EDL"),
64         _("Elapsed")
65 };
66
67 static int list_widths[] =
68 {
69         50,
70         100,
71         200,
72         100
73 };
74
75 BatchRenderMenuItem::BatchRenderMenuItem(MWindow *mwindow)
76  : BC_MenuItem(_("Batch Render..."), _("Shift-B"), 'B')
77 {
78         set_shift(1);
79         this->mwindow = mwindow;
80 }
81
82 int BatchRenderMenuItem::handle_event()
83 {
84         mwindow->batch_render->start();
85         return 1;
86 }
87
88
89
90
91
92
93
94
95 BatchRenderJob::BatchRenderJob(Preferences *preferences)
96 {
97         this->preferences = preferences;
98         asset = new Asset;
99         edl_path[0] = 0;
100         strategy = 0;
101         enabled = 1;
102         elapsed = 0;
103 }
104
105 BatchRenderJob::~BatchRenderJob()
106 {
107         asset->Garbage::remove_user();
108 }
109
110 void BatchRenderJob::copy_from(BatchRenderJob *src)
111 {
112         asset->copy_from(src->asset, 0);
113         strcpy(edl_path, src->edl_path);
114         strategy = src->strategy;
115         enabled = src->enabled;
116         elapsed = 0;
117 }
118
119 void BatchRenderJob::load(FileXML *file)
120 {
121         int result = 0;
122
123         edl_path[0] = 0;
124         file->tag.get_property("EDL_PATH", edl_path);
125         strategy = file->tag.get_property("STRATEGY", strategy);
126         enabled = file->tag.get_property("ENABLED", enabled);
127         elapsed = file->tag.get_property("ELAPSED", elapsed);
128         fix_strategy();
129
130         result = file->read_tag();
131         if(!result)
132         {
133                 if(file->tag.title_is("ASSET"))
134                 {
135                         file->tag.get_property("SRC", asset->path);
136                         asset->read(file, 0);
137 // The compression parameters are stored in the defaults to reduce
138 // coding maintenance.  The defaults must now be stuffed into the XML for
139 // unique storage.
140                         BC_Hash defaults;
141                         defaults.load_string(file->read_text());
142                         asset->load_defaults(&defaults,
143                                 "",
144                                 0,
145                                 1,
146                                 0,
147                                 0,
148                                 0);
149                 }
150         }
151 }
152
153 void BatchRenderJob::save(FileXML *file)
154 {
155         file->tag.set_property("EDL_PATH", edl_path);
156         file->tag.set_property("STRATEGY", strategy);
157         file->tag.set_property("ENABLED", enabled);
158         file->tag.set_property("ELAPSED", elapsed);
159         file->append_tag();
160         file->append_newline();
161         asset->write(file,
162                 0,
163                 "");
164
165 // The compression parameters are stored in the defaults to reduce
166 // coding maintenance.  The defaults must now be stuffed into the XML for
167 // unique storage.
168         BC_Hash defaults;
169         asset->save_defaults(&defaults,
170                 "",
171                 0,
172                 1,
173                 0,
174                 0,
175                 0);
176         char *string;
177         defaults.save_string(string);
178         file->append_text(string);
179         free(string);
180         file->tag.set_title("/JOB");
181         file->append_tag();
182         file->append_newline();
183 }
184
185 void BatchRenderJob::fix_strategy()
186 {
187         strategy = Render::fix_strategy(strategy, preferences->use_renderfarm);
188 }
189
190
191
192
193
194
195
196
197
198
199 BatchRenderThread::BatchRenderThread(MWindow *mwindow)
200  : BC_DialogThread()
201 {
202         this->mwindow = mwindow;
203         current_job = 0;
204         rendering_job = -1;
205         is_rendering = 0;
206         default_job = 0;
207         boot_defaults = 0;
208         preferences = 0;
209         warn = 1;
210         render = 0;
211         file_entries = 0;
212 }
213
214 BatchRenderThread::BatchRenderThread()
215  : BC_DialogThread()
216 {
217         mwindow = 0;
218         current_job = 0;
219         rendering_job = -1;
220         is_rendering = 0;
221         default_job = 0;
222         boot_defaults = 0;
223         preferences = 0;
224         warn = 1;
225         render = 0;
226         file_entries = 0;
227 }
228
229 BatchRenderThread::~BatchRenderThread()
230 {
231         close_window();
232         delete boot_defaults;
233         delete preferences;
234         delete render;
235         if( file_entries ) {
236                 file_entries->remove_all_objects();
237                 delete file_entries;
238         }
239 }
240
241 void BatchRenderThread::reset(int warn)
242 {
243         if( warn ) this->warn = 1;
244         current_job = 0;
245         rendering_job = -1;
246         delete default_job;  default_job = 0;
247         jobs.remove_all_objects();
248         if(file_entries) {
249                 file_entries->remove_all_objects();
250                 delete file_entries;  file_entries = 0;
251         }
252 }
253
254 void BatchRenderThread::handle_close_event(int result)
255 {
256 // Save settings
257         char path[BCTEXTLEN];
258         path[0] = 0;
259         save_jobs(path);
260         save_defaults(mwindow->defaults);
261         reset();
262 }
263
264 BC_Window* BatchRenderThread::new_gui()
265 {
266         current_start = 0.0;
267         current_end = 0.0;
268         default_job = new BatchRenderJob(mwindow->preferences);
269
270
271         if(!file_entries)
272         {
273                 file_entries = new ArrayList<BC_ListBoxItem*>;
274                 FileSystem fs;
275                 char string[BCTEXTLEN];
276         // Load current directory
277                 fs.update(getcwd(string, BCTEXTLEN));
278                 for(int i = 0; i < fs.total_files(); i++)
279                 {
280                         file_entries->append(
281                                 new BC_ListBoxItem(
282                                         fs.get_entry(i)->get_name()));
283                 }
284         }
285
286         char path[BCTEXTLEN];
287         path[0] = 0;
288         load_jobs(path, mwindow->preferences);
289         load_defaults(mwindow->defaults);
290         this->gui = new BatchRenderGUI(mwindow,
291                 this,
292                 mwindow->session->batchrender_x,
293                 mwindow->session->batchrender_y,
294                 mwindow->session->batchrender_w,
295                 mwindow->session->batchrender_h);
296         this->gui->create_objects();
297         return this->gui;
298 }
299
300
301 void BatchRenderThread::load_jobs(char *path, Preferences *preferences)
302 {
303         FileXML file;
304         int result = 0;
305
306         jobs.remove_all_objects();
307         if(path[0])
308                 file.read_from_file(path);
309         else
310                 file.read_from_file(create_path(path));
311
312         while(!result)
313         {
314                 if(!(result = file.read_tag()))
315                 {
316                         if(file.tag.title_is("JOBS"))
317                         {
318                                 warn = file.tag.get_property("WARN", 1);
319                         }
320                         else if(file.tag.title_is("JOB"))
321                         {
322                                 BatchRenderJob *job;
323                                 jobs.append(job = new BatchRenderJob(preferences));
324                                 job->load(&file);
325                         }
326                 }
327         }
328 }
329
330 void BatchRenderThread::save_jobs(char *path)
331 {
332         FileXML file;
333         file.tag.set_title("JOBS");
334         file.tag.set_property("WARN", warn);
335         file.append_tag();
336         file.append_newline();
337
338         for(int i = 0; i < jobs.total; i++)
339         {
340                 file.tag.set_title("JOB");
341                 jobs.values[i]->save(&file);
342         }
343         file.tag.set_title("/JOBS");
344         file.append_tag();
345         file.append_newline();
346
347         if(path[0])
348                 file.write_to_file(path);
349         else
350                 file.write_to_file(create_path(path));
351 }
352
353 void BatchRenderThread::load_defaults(BC_Hash *defaults)
354 {
355         if(default_job)
356         {
357                 default_job->asset->load_defaults(defaults,
358                         "BATCHRENDER_", 1, 1, 1, 1, 1);
359                 default_job->fix_strategy();
360         }
361
362         for(int i = 0; i < BATCHRENDER_COLUMNS; i++)
363         {
364                 char string[BCTEXTLEN];
365                 sprintf(string, "BATCHRENDER_COLUMN%d", i);
366                 column_width[i] = defaults->get(string, list_widths[i]);
367         }
368 }
369
370 void BatchRenderThread::save_defaults(BC_Hash *defaults)
371 {
372         if(default_job)
373         {
374                 default_job->asset->save_defaults(defaults,
375                         "BATCHRENDER_", 1, 1, 1, 1, 1);
376                 defaults->update("BATCHRENDER_STRATEGY", default_job->strategy);
377         }
378         for(int i = 0; i < BATCHRENDER_COLUMNS; i++)
379         {
380                 char string[BCTEXTLEN];
381                 sprintf(string, "BATCHRENDER_COLUMN%d", i);
382                 defaults->update(string, column_width[i]);
383         }
384 //      defaults->update("BATCHRENDER_JOB", current_job);
385         if(mwindow)
386                 mwindow->save_defaults();
387         else
388                 defaults->save();
389 }
390
391 char* BatchRenderThread::create_path(char *string)
392 {
393         FileSystem fs;
394         sprintf(string, "%s/", File::get_config_path());
395         fs.complete_path(string);
396         strcat(string, BATCH_PATH);
397         return string;
398 }
399
400 void BatchRenderThread::new_job()
401 {
402         BatchRenderJob *result = new BatchRenderJob(mwindow->preferences);
403         result->copy_from(get_current_job());
404         jobs.append(result);
405         current_job = jobs.total - 1;
406         gui->create_list(1);
407         gui->change_job();
408 }
409
410 void BatchRenderThread::delete_job()
411 {
412         if(current_job < jobs.total && current_job >= 0)
413         {
414                 jobs.remove_object_number(current_job);
415                 if(current_job > 0) current_job--;
416                 gui->create_list(1);
417                 gui->change_job();
418         }
419 }
420
421 void BatchRenderThread::use_current_edl()
422 {
423 // printf("BatchRenderThread::use_current_edl %d %p %s\n",
424 // __LINE__,
425 // mwindow->edl->path,
426 // mwindow->edl->path);
427
428         strcpy(get_current_edl(), mwindow->edl->path);
429         gui->create_list(1);
430         gui->edl_path_text->update(get_current_edl());
431 }
432
433 void BatchRenderThread::update_selected_edl()
434 {
435         FileXML xml_file;
436         char *path = get_current_edl();
437         EDL *edl = mwindow->edl;
438         edl->save_xml(&xml_file, path, 0, 0);
439         xml_file.terminate_string();
440         if( xml_file.write_to_file(path) ) {
441                 char msg[BCTEXTLEN];
442                 sprintf(msg, _("Unable to save: %s"), path);
443                 MainError::show_error(msg);
444         }
445 }
446
447 BatchRenderJob* BatchRenderThread::get_current_job()
448 {
449         BatchRenderJob *result;
450         if(current_job >= jobs.total || current_job < 0)
451         {
452                 result = default_job;
453         }
454         else
455         {
456                 result = jobs.values[current_job];
457         }
458         return result;
459 }
460
461
462 Asset* BatchRenderThread::get_current_asset()
463 {
464         return get_current_job()->asset;
465 }
466
467 char* BatchRenderThread::get_current_edl()
468 {
469         return get_current_job()->edl_path;
470 }
471
472
473 // Test EDL files for existence
474 int BatchRenderThread::test_edl_files()
475 {
476         int not_equiv = 0, ret = 0;
477         const char *path = 0;
478
479         for( int i=0; !ret && i<jobs.size(); ++i ) {
480                 if( !jobs.values[i]->enabled ) continue;
481                 const char *path = jobs.values[i]->edl_path;
482                 int is_script = *path == '@' ? 1 : 0;
483                 if( is_script ) ++path;
484                 FILE *fp = fopen(path, "r");
485                 if( fp ) {
486                         if( warn && mwindow && !is_script ) {
487                                 fseek(fp, 0, SEEK_END);
488                                 int64_t sz = ftell(fp);
489                                 fseek(fp, 0, SEEK_SET);
490                                 char *bfr = new char[sz+1];
491                                 int64_t len = fread(bfr, 1, sz+1, fp);
492                                 if( len == sz ) {
493                                         FileXML file;  file.set_shared_input(bfr, len);
494                                         EDL *edl = new EDL; edl->create_objects();
495                                         edl->load_xml(&file, LOAD_ALL);
496                                         double pos = edl->equivalent_output(mwindow->edl);
497                                         if( pos >= 0 ) ++not_equiv;
498                                         edl->remove_user();
499                                 }
500                                 else
501                                         ret = 1;
502                                 delete [] bfr;
503                         }
504                         fclose(fp);
505                 }
506                 else
507                         ret = 1;
508         }
509
510         if( ret ) {
511                 char string[BCTEXTLEN];
512                 sprintf(string, _("EDL %s not found.\n"), path);
513                 if( mwindow ) {
514                         ErrorBox error_box(_(PROGRAM_NAME ": Error"),
515                                 mwindow->gui->get_abs_cursor_x(1),
516                                 mwindow->gui->get_abs_cursor_y(1));
517                         error_box.create_objects(string);
518                         error_box.run_window();
519                         gui->button_enable();
520                 }
521                 else {
522                         fprintf(stderr, "%s", string);
523                 }
524                 is_rendering = 0;
525         }
526         else if( warn && mwindow && not_equiv > 0 ) {
527                 fprintf(stderr, _("%d job EDLs do not match session edl\n"), not_equiv);
528                 char string[BCTEXTLEN], *sp = string;
529                 sp += sprintf(sp, _("%d job EDLs do not match session edl\n"),not_equiv);
530                 sp += sprintf(sp, _("press cancel to abandon batch render"));
531                 mwindow->show_warning(&warn, string);
532                 if( mwindow->wait_warning() ) {
533                         gui->button_enable();
534                         is_rendering = 0;
535                         ret = 1;
536                 }
537                 gui->warning->update(warn);
538         }
539
540         return ret;
541 }
542
543 void BatchRenderThread::calculate_dest_paths(ArrayList<char*> *paths,
544         Preferences *preferences)
545 {
546         for(int i = 0; i < jobs.total; i++)
547         {
548                 BatchRenderJob *job = jobs.values[i];
549                 if(job->enabled && *job->edl_path != '@')
550                 {
551                         PackageDispatcher *packages = new PackageDispatcher;
552
553 // Load EDL
554                         TransportCommand *command = new TransportCommand;
555                         FileXML *file = new FileXML;
556                         file->read_from_file(job->edl_path);
557
558 // Use command to calculate range.
559                         command->command = NORMAL_FWD;
560                         command->get_edl()->load_xml(file,
561                                 LOAD_ALL);
562                         command->change_type = CHANGE_ALL;
563                         command->set_playback_range();
564                         command->playback_range_adjust_inout();
565
566 // Create test packages
567                         packages->create_packages(mwindow,
568                                 command->get_edl(),
569                                 preferences,
570                                 job->strategy,
571                                 job->asset,
572                                 command->start_position,
573                                 command->end_position,
574                                 0);
575
576 // Append output paths allocated to total
577                         packages->get_package_paths(paths);
578
579 // Delete package harness
580                         delete packages;
581                         delete command;
582                         delete file;
583                 }
584         }
585 }
586
587
588 void BatchRenderThread::start_rendering(char *config_path,
589         char *batch_path)
590 {
591         BC_Hash *boot_defaults;
592         Preferences *preferences;
593         Render *render;
594         BC_Signals *signals = new BC_Signals;
595         // XXX the above stuff is leaked,
596 //PRINT_TRACE
597 // Initialize stuff which MWindow does.
598         signals->initialize();
599         MWindow::init_defaults(boot_defaults, config_path);
600         load_defaults(boot_defaults);
601         preferences = new Preferences;
602         preferences->load_defaults(boot_defaults);
603         MWindow::init_plugins(0, preferences);
604         char font_path[BCTEXTLEN];
605         strcpy(font_path, preferences->plugin_dir);
606         strcat(font_path, "/" FONT_SEARCHPATH);
607         BC_Resources::init_fontconfig(font_path);
608         BC_WindowBase::get_resources()->vframe_shm = 1;
609
610 //PRINT_TRACE
611         load_jobs(batch_path, preferences);
612         save_jobs(batch_path);
613         save_defaults(boot_defaults);
614
615 //PRINT_TRACE
616 // Test EDL files for existence
617         if(test_edl_files()) return;
618
619 //PRINT_TRACE
620
621 // Predict all destination paths
622         ArrayList<char*> paths;
623         paths.set_array_delete();
624         calculate_dest_paths(&paths, preferences);
625
626 //PRINT_TRACE
627         int result = ConfirmSave::test_files(0, &paths);
628         paths.remove_all_objects();
629 // Abort on any existing file because it's so hard to set this up.
630         if(result) return;
631
632 //PRINT_TRACE
633         render = new Render(0);
634 //PRINT_TRACE
635         render->start_batches(&jobs,
636                 boot_defaults,
637                 preferences);
638 //PRINT_TRACE
639 }
640
641 void BatchRenderThread::start_rendering()
642 {
643         if(is_rendering) return;
644
645         is_rendering = 1;
646         char path[BCTEXTLEN];
647         path[0] = 0;
648         save_jobs(path);
649         save_defaults(mwindow->defaults);
650         gui->button_disable();
651
652 // Test EDL files for existence
653         if(test_edl_files()) return;
654
655 // Predict all destination paths
656         ArrayList<char*> paths;
657         calculate_dest_paths(&paths,
658                 mwindow->preferences);
659
660 // Test destination files for overwrite
661         int result = ConfirmSave::test_files(mwindow, &paths);
662         paths.remove_all_objects();
663
664 // User cancelled
665         if(result)
666         {
667                 is_rendering = 0;
668                 gui->button_enable();
669                 return;
670         }
671
672         mwindow->render->start_batches(&jobs);
673 }
674
675 void BatchRenderThread::stop_rendering()
676 {
677         if(!is_rendering) return;
678         mwindow->render->stop_operation();
679         is_rendering = 0;
680 }
681
682 void BatchRenderThread::update_active(int number)
683 {
684         gui->lock_window("BatchRenderThread::update_active");
685         if(number >= 0)
686         {
687                 current_job = number;
688                 rendering_job = number;
689         }
690         else
691         {
692                 rendering_job = -1;
693                 is_rendering = 0;
694         }
695         gui->create_list(1);
696         gui->unlock_window();
697 }
698
699 void BatchRenderThread::update_done(int number,
700         int create_list,
701         double elapsed_time)
702 {
703         gui->lock_window("BatchRenderThread::update_done");
704         if(number < 0)
705         {
706                 gui->button_enable();
707         }
708         else
709         {
710                 jobs.values[number]->enabled = 0;
711                 jobs.values[number]->elapsed = elapsed_time;
712                 if(create_list) gui->create_list(1);
713         }
714         gui->unlock_window();
715 }
716
717 void BatchRenderThread::move_batch(int src, int dst)
718 {
719         BatchRenderJob *src_job = jobs.values[src];
720         if(dst < 0) dst = jobs.total - 1;
721
722         if(dst != src)
723         {
724                 for(int i = src; i < jobs.total - 1; i++)
725                         jobs.values[i] = jobs.values[i + 1];
726 //              if(dst > src) dst--;
727                 for(int i = jobs.total - 1; i > dst; i--)
728                         jobs.values[i] = jobs.values[i - 1];
729                 jobs.values[dst] = src_job;
730                 gui->create_list(1);
731         }
732 }
733
734
735
736
737
738
739
740 BatchRenderGUI::BatchRenderGUI(MWindow *mwindow,
741         BatchRenderThread *thread, int x, int y, int w, int h)
742  : BC_Window(_(PROGRAM_NAME ": Batch Render"),
743         x, y, w, h, 50, 50, 1, 0, 1)
744 {
745         this->mwindow = mwindow;
746         this->thread = thread;
747 }
748
749 BatchRenderGUI::~BatchRenderGUI()
750 {
751         lock_window("BatchRenderGUI::~BatchRenderGUI");
752         delete format_tools;
753         unlock_window();
754 }
755
756
757 void BatchRenderGUI::create_objects()
758 {
759         lock_window("BatchRenderGUI::create_objects");
760         mwindow->theme->get_batchrender_sizes(this, get_w(), get_h());
761         create_list(0);
762
763         int x = mwindow->theme->batchrender_x1;
764         int y = 5;
765         int x1 = x, x2 = get_w()/2 + 10; // mwindow->theme->batchrender_x2;
766         int y1 = 5, y2 = 5;
767
768 // output file
769         add_subwindow(output_path_title = new BC_Title(x1, y1, _("Output path:")));
770         y1 += output_path_title->get_h() + mwindow->theme->widget_border;
771
772         format_tools = new BatchFormat(mwindow, this, thread->get_current_asset());
773         format_tools->set_w(get_w() / 2);
774         format_tools->create_objects(x1, y1, 1, 1, 1, 1, 0, 1, 0, 0,
775                         &thread->get_current_job()->strategy, 0); 
776
777 // input EDL
778         add_subwindow(edl_path_title = new BC_Title(x2, y2, _("EDL Path:")));
779         y2 += edl_path_title->get_h() + mwindow->theme->widget_border;
780         
781         x = x2;  y = y2;
782         add_subwindow(edl_path_text = new BatchRenderEDLPath( thread,
783                 x, y, get_w()-x - 40, thread->get_current_edl())); 
784         x =  x2 + edl_path_text->get_w();
785         add_subwindow(edl_path_browse = new BrowseButton(
786                 mwindow->theme, this, edl_path_text, x, y, thread->get_current_edl(),
787                 _("Input EDL"), _("Select an EDL to load:"), 0));
788         y2 = y + edl_path_browse->get_h() + mwindow->theme->widget_border;
789
790         x = x2;  y = y2;
791         add_subwindow(update_selected_edl = new BatchRenderUpdateEDL(thread, x, y));
792         y += update_selected_edl->get_h() + mwindow->theme->widget_border;
793         add_subwindow(use_current_edl = new BatchRenderCurrentEDL(thread, x, y));
794         y += use_current_edl->get_h() + mwindow->theme->widget_border;
795         if( !mwindow->edl || !mwindow->edl->path[0] ) use_current_edl->disable();
796         add_subwindow(new_batch = new BatchRenderNew(thread, x, y));
797         x += new_batch->get_w() + mwindow->theme->widget_border;
798         add_subwindow(delete_batch = new BatchRenderDelete(thread, x, y));
799         x = x2;  y += delete_batch->get_h() + mwindow->theme->widget_border;
800         add_subwindow(savelist_batch = new BatchRenderSaveList(thread, x, y));
801         x += savelist_batch->get_w() + mwindow->theme->widget_border;
802         add_subwindow(loadlist_batch = new BatchRenderLoadList(thread, x, y));
803         y += loadlist_batch->get_h() + mwindow->theme->widget_border;
804         add_subwindow(warning = new BatchRenderWarning(thread, x2, y));
805         y2 = y + warning->get_h() + mwindow->theme->widget_border;
806         if( y2 > y1 ) y1 = y2;
807         x = mwindow->theme->batchrender_x1, y = y1;
808
809         add_subwindow(list_title = new BC_Title(x, y, _("Batches to render:")));
810         y += list_title->get_h() + mwindow->theme->widget_border;
811         y1 = get_h();
812         y1 -= 15 + BC_GenericButton::calculate_h() + mwindow->theme->widget_border;
813         add_subwindow(batch_list = new BatchRenderList(thread, x, y,
814                 get_w() - x - 10, y1 - y));
815         y += batch_list->get_h() + mwindow->theme->widget_border;
816
817         add_subwindow(start_button = new BatchRenderStart(thread, x, y));
818         x = get_w() / 2 - BC_GenericButton::calculate_w(this, _("Stop")) / 2;
819         add_subwindow(stop_button = new BatchRenderStop(thread, x, y));
820         x = get_w() - BC_GenericButton::calculate_w(this, _("Close")) - 10;
821         add_subwindow(cancel_button = new BatchRenderCancel(thread, x, y));
822
823         show_window(1);
824         unlock_window();
825 }
826
827 void BatchRenderGUI::button_disable()
828 {
829         new_batch->disable();
830         delete_batch->disable();
831         use_current_edl->disable();
832         update_selected_edl->disable();
833 }
834
835 void BatchRenderGUI::button_enable()
836 {
837         new_batch->enable();
838         delete_batch->enable();
839         if( mwindow->edl && mwindow->edl->path[0] )
840                 use_current_edl->enable();
841         update_selected_edl->enable();
842 }
843
844 int BatchRenderGUI::resize_event(int w, int h)
845 {
846         mwindow->session->batchrender_w = w;
847         mwindow->session->batchrender_h = h;
848         mwindow->theme->get_batchrender_sizes(this, w, h);
849
850         int x = mwindow->theme->batchrender_x1;
851         int y = 5;
852         int x1 = x, x2 = get_w()/2 + 10; // mwindow->theme->batchrender_x2;
853         int y1 = 5, y2 = 5;
854
855 // output file
856         output_path_title->reposition_window(x1, y1);
857         y1 += output_path_title->get_h() + mwindow->theme->widget_border;
858         format_tools->reposition_window(x1, y1);
859
860 // input EDL
861         x = x2, y = y2;
862         edl_path_title->reposition_window(x, y);
863         y += edl_path_title->get_h() + mwindow->theme->widget_border;
864         edl_path_text->reposition_window(x, y, w - x - 40);
865         x += edl_path_text->get_w();
866         edl_path_browse->reposition_window(x, y);
867         y2 = y + edl_path_browse->get_h() + mwindow->theme->widget_border;
868
869         x = x2;  y = y2;
870         update_selected_edl->reposition_window(x, y);
871         y += update_selected_edl->get_h() + mwindow->theme->widget_border;
872         use_current_edl->reposition_window(x, y);
873         y += use_current_edl->get_h() + mwindow->theme->widget_border;
874         new_batch->reposition_window(x, y);
875         x += new_batch->get_w() + mwindow->theme->widget_border;
876         delete_batch->reposition_window(x, y);
877
878         x = x2;  y += delete_batch->get_h() + mwindow->theme->widget_border;
879         savelist_batch->reposition_window(x, y);
880         x += savelist_batch->get_w() + mwindow->theme->widget_border;
881         loadlist_batch->reposition_window(x, y);
882         y += loadlist_batch->get_h() + mwindow->theme->widget_border;
883
884         y1 = 15 + BC_GenericButton::calculate_h() + mwindow->theme->widget_border;
885         y2 = get_h() - y1 - batch_list->get_h();
886         y2 -= list_title->get_h() + mwindow->theme->widget_border;
887
888         x = mwindow->theme->batchrender_x1;  y = y2;
889         list_title->reposition_window(x, y);
890         y += list_title->get_h() + mwindow->theme->widget_border;
891         batch_list->reposition_window(x, y, w - x - 10, h - y - y1);
892         y += batch_list->get_h() + mwindow->theme->widget_border;
893
894         start_button->reposition_window(x, y);
895         x = w / 2 - stop_button->get_w() / 2;
896         stop_button->reposition_window(x, y);
897         x = w - cancel_button->get_w() - 10;
898         cancel_button->reposition_window(x, y);
899         return 1;
900 }
901
902 int BatchRenderGUI::translation_event()
903 {
904         mwindow->session->batchrender_x = get_x();
905         mwindow->session->batchrender_y = get_y();
906         return 1;
907 }
908
909 int BatchRenderGUI::close_event()
910 {
911 // Stop batch rendering
912         unlock_window();
913         thread->stop_rendering();
914         lock_window("BatchRenderGUI::close_event");
915         set_done(1);
916         return 1;
917 }
918
919 void BatchRenderGUI::create_list(int update_widget)
920 {
921         for(int i = 0; i < BATCHRENDER_COLUMNS; i++)
922         {
923                 list_columns[i].remove_all_objects();
924         }
925
926         for(int i = 0; i < thread->jobs.total; i++)
927         {
928                 BatchRenderJob *job = thread->jobs.values[i];
929                 char string[BCTEXTLEN];
930                 BC_ListBoxItem *enabled = new BC_ListBoxItem(job->enabled ?
931                         (char*)"X" :
932                         (char*)" ");
933                 BC_ListBoxItem *item1 = new BC_ListBoxItem(job->asset->path);
934                 BC_ListBoxItem *item2 = new BC_ListBoxItem(job->edl_path);
935                 BC_ListBoxItem *item3;
936                 if(job->elapsed)
937                         item3 = new BC_ListBoxItem(
938                                 Units::totext(string,
939                                         job->elapsed,
940                                         TIME_HMS2));
941                 else
942                         item3 = new BC_ListBoxItem(_("Unknown"));
943                 list_columns[0].append(enabled);
944                 list_columns[1].append(item1);
945                 list_columns[2].append(item2);
946                 list_columns[3].append(item3);
947                 if(i == thread->current_job)
948                 {
949                         enabled->set_selected(1);
950                         item1->set_selected(1);
951                         item2->set_selected(1);
952                         item3->set_selected(1);
953                 }
954                 if(i == thread->rendering_job)
955                 {
956                         enabled->set_color(RED);
957                         item1->set_color(RED);
958                         item2->set_color(RED);
959                         item3->set_color(RED);
960                 }
961         }
962
963         if(update_widget)
964         {
965                 batch_list->update(list_columns,
966                                                 list_titles,
967                                                 thread->column_width,
968                                                 BATCHRENDER_COLUMNS,
969                                                 batch_list->get_xposition(),
970                                                 batch_list->get_yposition(),
971                                                 batch_list->get_highlighted_item(),  // Flat index of item cursor is over
972                                                 1,     // set all autoplace flags to 1
973                                                 1);
974         }
975 }
976
977 void BatchRenderGUI::change_job()
978 {
979         BatchRenderJob *job = thread->get_current_job();
980         format_tools->update(job->asset, &job->strategy);
981         edl_path_text->update(job->edl_path);
982 }
983
984
985
986
987
988
989
990
991 BatchFormat::BatchFormat(MWindow *mwindow,
992                         BatchRenderGUI *gui,
993                         Asset *asset)
994  : FormatTools(mwindow, gui, asset)
995 {
996         this->gui = gui;
997         this->mwindow = mwindow;
998 }
999
1000 BatchFormat::~BatchFormat()
1001 {
1002 }
1003
1004
1005 int BatchFormat::handle_event()
1006 {
1007         gui->create_list(1);
1008         return 1;
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 BatchRenderEDLPath::BatchRenderEDLPath(BatchRenderThread *thread,
1022         int x,
1023         int y,
1024         int w,
1025         char *text)
1026  : BC_TextBox(x,
1027                 y,
1028                 w,
1029                 1,
1030                 text)
1031 {
1032         this->thread = thread;
1033 }
1034
1035
1036 int BatchRenderEDLPath::handle_event()
1037 {
1038 // Suggestions
1039         calculate_suggestions(thread->file_entries);
1040
1041         strcpy(thread->get_current_edl(), get_text());
1042         thread->gui->create_list(1);
1043         return 1;
1044 }
1045
1046
1047
1048
1049
1050
1051 BatchRenderNew::BatchRenderNew(BatchRenderThread *thread,
1052         int x,
1053         int y)
1054  : BC_GenericButton(x, y, _("New"))
1055 {
1056         this->thread = thread;
1057 }
1058
1059 int BatchRenderNew::handle_event()
1060 {
1061         thread->new_job();
1062         return 1;
1063 }
1064
1065 BatchRenderDelete::BatchRenderDelete(BatchRenderThread *thread,
1066         int x,
1067         int y)
1068  : BC_GenericButton(x, y, _("Delete"))
1069 {
1070         this->thread = thread;
1071 }
1072
1073 int BatchRenderDelete::handle_event()
1074 {
1075         thread->delete_job();
1076         return 1;
1077 }
1078
1079
1080
1081 BatchRenderSaveList::BatchRenderSaveList(BatchRenderThread *thread,
1082         int x,
1083         int y)
1084  : BC_GenericButton(x, y, _("Save List"))
1085 {
1086         this->thread = thread;
1087         set_tooltip(_("Save a Batch Render List"));
1088         gui = 0;
1089         startup_lock = new Mutex("BatchRenderSaveList::startup_lock");
1090 }
1091
1092 BatchRenderSaveList::~BatchRenderSaveList()
1093 {
1094         startup_lock->lock("BatchRenderSaveList::~BrowseButton");
1095         if(gui)
1096         {
1097                 gui->lock_window();
1098                 gui->set_done(1);
1099                 gui->unlock_window();
1100         }
1101         startup_lock->unlock();
1102         Thread::join();
1103         delete startup_lock;
1104 }
1105
1106 int BatchRenderSaveList::handle_event()
1107 {
1108         if(Thread::running())
1109         {
1110                 if(gui)
1111                 {
1112                         gui->lock_window();
1113                         gui->raise_window();
1114                         gui->unlock_window();
1115                 }
1116                 return 1;
1117         }
1118         startup_lock->lock("BatchRenderSaveList::handle_event 1");
1119         Thread::start();
1120         startup_lock->lock("BatchRenderSaveList::handle_event 2");
1121         startup_lock->unlock();
1122         return 1;
1123 }
1124
1125 void BatchRenderSaveList::run()
1126 {
1127         char default_path[BCTEXTLEN];
1128         sprintf(default_path, "~");
1129         BC_FileBox filewindow(100,
1130                               100,
1131                               this->thread->mwindow->defaults->get("DEFAULT_BATCHLOADPATH", default_path),
1132                               _("Save Batch Render List"),
1133                               _("Enter a Batch Render filename to save as:"),
1134                               0,
1135                               0,
1136                               0,
1137                               0);
1138
1139         gui = &filewindow;
1140
1141         startup_lock->unlock();
1142         filewindow.create_objects();
1143
1144         int result2 = filewindow.run_window();
1145
1146         if(!result2)
1147         {
1148                 this->thread->save_jobs(filewindow.get_submitted_path());
1149                 this->thread->mwindow->defaults->update("DEFAULT_BATCHLOADPATH", filewindow.get_submitted_path());
1150         }
1151
1152         this->thread->gui->flush();
1153         startup_lock->lock("BatchRenderLoadList::run");
1154         gui = 0;
1155         startup_lock->unlock();
1156 }
1157
1158 int BatchRenderSaveList::keypress_event() {
1159         if (get_keypress() == 's' ||
1160             get_keypress() == 'S') return handle_event();
1161         return 0;
1162 }
1163
1164
1165
1166
1167 BatchRenderLoadList::BatchRenderLoadList(BatchRenderThread *thread,
1168         int x,
1169         int y)
1170   : BC_GenericButton(x, y, _("Load List")),
1171     Thread()
1172 {
1173         this->thread = thread;
1174         set_tooltip(_("Load a previously saved Batch Render List"));
1175         gui = 0;
1176         startup_lock = new Mutex("BatchRenderLoadList::startup_lock");
1177 }
1178
1179 BatchRenderLoadList::~BatchRenderLoadList()
1180 {
1181         startup_lock->lock("BatchRenderLoadList::~BrowseButton");
1182         if(gui)
1183         {
1184                 gui->lock_window();
1185                 gui->set_done(1);
1186                 gui->unlock_window();
1187         }
1188         startup_lock->unlock();
1189         Thread::join();
1190         delete startup_lock;
1191 }
1192
1193 int BatchRenderLoadList::handle_event()
1194 {
1195         if(Thread::running())
1196         {
1197                 if(gui)
1198                 {
1199                         gui->lock_window();
1200                         gui->raise_window();
1201                         gui->unlock_window();
1202                 }
1203                 return 1;
1204         }
1205         startup_lock->lock("BatchRenderLoadList::handle_event 1");
1206         Thread::start();
1207         startup_lock->lock("BatchRenderLoadList::handle_event 2");
1208         startup_lock->unlock();
1209         return 1;
1210 }
1211
1212 void BatchRenderLoadList::run()
1213 {
1214         char default_path[BCTEXTLEN];
1215         sprintf(default_path, "~");
1216         BC_FileBox filewindow(100,
1217                               100,
1218                               this->thread->mwindow->defaults->get("DEFAULT_BATCHLOADPATH", default_path),
1219                               _("Load Batch Render List"),
1220                               _("Enter a Batch Render filename to load from:"),
1221                               0,
1222                               0,
1223                               0,
1224                               0);
1225
1226         gui = &filewindow;
1227
1228         startup_lock->unlock();
1229         filewindow.create_objects();
1230
1231         int result2 = filewindow.run_window();
1232
1233         if(!result2)
1234         {
1235                 this->thread->load_jobs(filewindow.get_submitted_path(),this->thread->mwindow->preferences);
1236                 this->thread->gui->create_list(1);
1237                 this->thread->mwindow->defaults->update("DEFAULT_BATCHLOADPATH", filewindow.get_submitted_path());
1238         }
1239
1240         this->thread->gui->flush();
1241         startup_lock->lock("BatchRenderLoadList::run");
1242         gui = 0;
1243         startup_lock->unlock();
1244 }
1245
1246 int BatchRenderLoadList::keypress_event() {
1247         if (get_keypress() == 'o' ||
1248             get_keypress() == 'O') return handle_event();
1249         return 0;
1250 }
1251
1252 BatchRenderCurrentEDL::BatchRenderCurrentEDL(BatchRenderThread *thread,
1253         int x,
1254         int y)
1255  : BC_GenericButton(x, y, _("Use Current EDL"))
1256 {
1257         this->thread = thread;
1258 }
1259
1260 int BatchRenderCurrentEDL::handle_event()
1261 {
1262         thread->use_current_edl();
1263         return 1;
1264 }
1265
1266 BatchRenderUpdateEDL::BatchRenderUpdateEDL(BatchRenderThread *thread,
1267         int x,
1268         int y)
1269  : BC_GenericButton(x, y, _("Save to EDL Path"))
1270 {
1271         this->thread = thread;
1272 }
1273
1274 int BatchRenderUpdateEDL::handle_event()
1275 {
1276         thread->update_selected_edl();
1277         return 1;
1278 }
1279
1280
1281
1282
1283 BatchRenderList::BatchRenderList(BatchRenderThread *thread,
1284         int x,
1285         int y,
1286         int w,
1287         int h)
1288  : BC_ListBox(x,
1289         y,
1290         w,
1291         h,
1292         LISTBOX_TEXT,
1293         thread->gui->list_columns,
1294         list_titles,
1295         thread->column_width,
1296         BATCHRENDER_COLUMNS,
1297         0,
1298         0,
1299         LISTBOX_SINGLE,
1300         ICON_LEFT,
1301         1)
1302 {
1303         this->thread = thread;
1304         dragging_item = 0;
1305         set_process_drag(0);
1306 }
1307
1308 int BatchRenderList::handle_event()
1309 {
1310         return 1;
1311 }
1312
1313 int BatchRenderList::selection_changed()
1314 {
1315         thread->current_job = get_selection_number(0, 0);
1316         thread->gui->change_job();
1317         if(get_cursor_x() < thread->column_width[0])
1318         {
1319                 BatchRenderJob *job = thread->get_current_job();
1320                 job->enabled = !job->enabled;
1321                 thread->gui->create_list(1);
1322         }
1323         return 1;
1324 }
1325
1326 int BatchRenderList::column_resize_event()
1327 {
1328         for(int i = 0; i < BATCHRENDER_COLUMNS; i++)
1329         {
1330                 thread->column_width[i] = get_column_width(i);
1331         }
1332         return 1;
1333 }
1334
1335 int BatchRenderList::drag_start_event()
1336 {
1337         if(BC_ListBox::drag_start_event())
1338         {
1339                 dragging_item = 1;
1340                 return 1;
1341         }
1342
1343         return 0;
1344 }
1345
1346 int BatchRenderList::drag_motion_event()
1347 {
1348         if(BC_ListBox::drag_motion_event())
1349         {
1350                 return 1;
1351         }
1352         return 0;
1353 }
1354
1355 int BatchRenderList::drag_stop_event()
1356 {
1357         if(dragging_item)
1358         {
1359                 int src = get_selection_number(0, 0);
1360                 int dst = get_highlighted_item();
1361                 if(src != dst)
1362                 {
1363                         thread->move_batch(src, dst);
1364                 }
1365                 BC_ListBox::drag_stop_event();
1366         }
1367         return 0;
1368 }
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 BatchRenderStart::BatchRenderStart(BatchRenderThread *thread,
1383         int x,
1384         int y)
1385  : BC_GenericButton(x,
1386         y,
1387         _("Start"))
1388 {
1389         this->thread = thread;
1390 }
1391
1392 int BatchRenderStart::handle_event()
1393 {
1394         thread->start_rendering();
1395         return 1;
1396 }
1397
1398 BatchRenderStop::BatchRenderStop(BatchRenderThread *thread,
1399         int x,
1400         int y)
1401  : BC_GenericButton(x,
1402         y,
1403         _("Stop"))
1404 {
1405         this->thread = thread;
1406 }
1407
1408 int BatchRenderStop::handle_event()
1409 {
1410         unlock_window();
1411         thread->stop_rendering();
1412         lock_window("BatchRenderStop::handle_event");
1413         return 1;
1414 }
1415
1416
1417 BatchRenderWarning::BatchRenderWarning(BatchRenderThread *thread, int x, int y)
1418  : BC_CheckBox(x, y, thread->warn, _("warn if jobs/session mismatched"))
1419 {
1420         this->thread = thread;
1421 }
1422
1423 int BatchRenderWarning::handle_event()
1424 {
1425         thread->warn = get_value();
1426         return 1;
1427 }
1428
1429
1430 BatchRenderCancel::BatchRenderCancel(BatchRenderThread *thread,
1431         int x,
1432         int y)
1433  : BC_GenericButton(x,
1434         y,
1435         _("Close"))
1436 {
1437         this->thread = thread;
1438 }
1439
1440 int BatchRenderCancel::handle_event()
1441 {
1442         unlock_window();
1443         thread->stop_rendering();
1444         lock_window("BatchRenderCancel::handle_event");
1445         thread->gui->set_done(1);
1446         return 1;
1447 }
1448
1449 int BatchRenderCancel::keypress_event()
1450 {
1451         if(get_keypress() == ESC)
1452         {
1453                 unlock_window();
1454                 thread->stop_rendering();
1455                 lock_window("BatchRenderCancel::keypress_event");
1456                 thread->gui->set_done(1);
1457                 return 1;
1458         }
1459         return 0;
1460 }
1461