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