rework intl strategy, titler upgrades, fonts fixes, igor ru.po
[goodguy/history.git] / cinelerra-5.1 / cinelerra / exportedl.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2006 Andraz Tori
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 "bchash.h"
24 #include "condition.h"
25 #include "confirmsave.h"
26 #include "edits.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "errorbox.h"
30 #include "file.h"
31 #include "filesystem.h"
32 #include "filexml.h"
33 #include "language.h"
34 #include "localsession.h"
35 #include "mainsession.h"
36 #include "mutex.h"
37 #include "mwindowgui.h"
38 #include "mwindow.h"
39 #include "exportedl.h"
40 #include "tracks.h"
41 #include "transition.h"
42
43 #include <ctype.h>
44 #include <string.h>
45
46 ExportEDLAsset::ExportEDLAsset(MWindow *mwindow, EDL *edl)
47 {
48         this->mwindow = mwindow;
49         this->edl = edl;
50
51         path[0] = 0;
52         edl_type = EDLTYPE_CMX3600;
53         track_number = -1;
54 }
55
56 ExportEDLAsset::~ExportEDLAsset()
57 {
58 }
59
60 void ExportEDLAsset::double_to_CMX3600(double seconds, double frame_rate, char *str)
61 {
62         char tmp[20];
63         Units::totext(tmp,
64                         seconds,
65                         TIME_HMSF,
66                         0, // sample_rate ... unnecessary
67                         frame_rate,
68                         0);    // frames per foot
69         if ((int)(seconds / 3600) <= 9)
70         {
71                 str[0]='0';
72                 strcpy(str+1, tmp);
73         } else
74         {
75                 strcpy(str, tmp);
76         }
77
78 //      str[8]='.';
79
80         //sprintf(str, "%02d:%02d:%02d:%02d", hour, minute, second, hundredths);
81 }
82
83 int ExportEDLAsset::edit_to_timecodes(Edit *edit, char *sourceinpoint, char *sourceoutpoint, char *destinpoint, char *destoutpoint, char *reel_name)
84 {
85         Asset *asset = edit->asset;
86         Track *track = edit->track;
87         double frame_rate = edit->track->edl->session->frame_rate;
88
89         double edit_sourcestart;
90         double edit_sourceend;
91         double edit_deststart;
92         double edit_destend;
93
94         if (asset)
95         {
96                 // reelname should be 8 chars long
97
98                 strncpy(reel_name, asset->reel_name, 9);
99                 if (strlen(asset->reel_name) > 8)
100                 {
101                         printf(_("Warning: chopping the reel name to eight characters!\n"));
102                 };
103                 reel_name[8] = 0;
104                 for (int i = strlen(reel_name); i<8; i++)
105                         reel_name[i] = ' ';
106
107                 edit_sourcestart = (double)asset->tcstart / asset->frame_rate
108                         + track->from_units(edit->startsource);
109                 edit_sourceend = (double)asset->tcstart / asset->frame_rate
110                         + track->from_units(edit->startsource + edit->length);
111
112         } else
113         {
114                 strcpy(reel_name, "   BL   ");
115                 edit_sourcestart = 0;
116                 edit_sourceend = track->from_units(edit->length);
117         }
118
119         edit_deststart = track->from_units(edit->startproject);
120         edit_destend = track->from_units(edit->startproject + edit->length);
121
122         double_to_CMX3600(edit_sourcestart, frame_rate, sourceinpoint);
123         double_to_CMX3600(edit_sourceend, frame_rate, sourceoutpoint);
124         double_to_CMX3600(edit_deststart, frame_rate, destinpoint);
125         double_to_CMX3600(edit_destend, frame_rate, destoutpoint);
126
127         return 0;
128 }
129
130
131 void ExportEDLAsset::export_it()
132 {
133         FILE *fh;
134         fh = fopen(path, "w+");
135
136 // We currently only support exporting one track at a time
137 // Find the track...
138         int serial = 0;
139         Track *track;
140         for(track = edl->tracks->first;
141                 track;
142                 track = track->next)
143         {
144                 if (serial == track_number)
145                         break;
146                 serial ++;
147         }
148
149
150         int last_dissolve = 1;
151
152         if (edl_type == EDLTYPE_CMX3600)
153         {
154
155                 // TODO: Find docs about exact header for CMX3600
156                 fprintf(fh, "TITLE: Cinproj   FORMAT: CMX 3600 4-Ch\n");
157
158                 int colnum = 1;
159
160
161                 for (Edit *edit = track->edits->first;
162                         edit;
163                         edit = edit->next)
164                 {
165                         char reel_name[BCTEXTLEN];
166                         char avselect[5];
167                         char edittype[5] = "C   ";
168                         char cutinfo[4] = "   ";
169                         char sourceinpoint[12];
170                         char sourceoutpoint[12];
171                         char destinpoint[12];
172                         char destoutpoint[12];
173                         if (track->data_type == TRACK_AUDIO)
174                                 strcpy(avselect, "A   ");
175                         else
176                                 strcpy(avselect, "V   ");
177
178                         //if (edit->transition)
179                         //      printf("title: %s, length: %i\n", edit->transition->title, edit->transition->length);
180                         if (edit->transition && !strcmp(edit->transition->title, "Dissolve"))
181                         {
182                                 char last_sourceout[12];
183                                 edit_to_timecodes(edit->previous, sourceinpoint, last_sourceout, destinpoint, destoutpoint, reel_name);
184                                 edit_to_timecodes(edit, sourceinpoint, sourceoutpoint, destinpoint, destoutpoint, reel_name);
185
186                                 if (last_dissolve)
187                                 {
188                                         fprintf(fh, "%03d %8s %s %4s %3s", colnum, reel_name, avselect, edittype, cutinfo);
189                                         fprintf(fh, " %s %s", last_sourceout, last_sourceout);
190                                         fprintf(fh, " %s %s", destinpoint, destinpoint);
191                                         fprintf(fh,"\n");
192                                 } else
193                                 {
194                                         colnum --;
195                                 }
196                                 edittype[0] = 'D';
197                                 fprintf(fh, "%03d %8s %s %4s %03jd", colnum, reel_name, avselect, edittype, edit->transition->length);
198                                 fprintf(fh, " %s %s", sourceinpoint, sourceoutpoint);
199                                 fprintf(fh, " %s %s", destinpoint, destoutpoint);
200                                 fprintf(fh,"\n");
201                                 last_dissolve = 1;
202                         } else
203                         {
204                                                         edit_to_timecodes(edit, sourceinpoint, sourceoutpoint, destinpoint, destoutpoint, reel_name);
205                                 fprintf(fh, "%03d %8s %s %4s %3s", colnum, reel_name, avselect, edittype, cutinfo);
206                                 fprintf(fh, " %s %s", sourceinpoint, sourceoutpoint);
207                                 fprintf(fh, " %s %s", destinpoint, destoutpoint);
208                                 fprintf(fh,"\n");
209                                 last_dissolve = 0;
210                         }
211
212                         colnum ++;
213
214                 }
215
216         }
217
218         fclose(fh);
219
220
221 }
222
223
224
225 int ExportEDLAsset::load_defaults()
226 {
227         mwindow->defaults->get("EDLEXPORT_PATH", path);
228         mwindow->defaults->get("EDLEXPORT_TYPE", edl_type);
229         mwindow->defaults->get("EDLEXPORT_TRACKNUMBER", track_number);
230         //load_mode = mwindow->defaults->get("RENDER_LOADMODE", LOADMODE_NEW_TRACKS);
231
232
233         return 0;
234 }
235
236 int ExportEDLAsset::save_defaults()
237 {
238         mwindow->defaults->update("EDLEXPORT_PATH", path);
239         mwindow->defaults->update("EDLEXPORT_TYPE", edl_type);
240         mwindow->defaults->update("EDLEXPORT_TRACKNUMBER", track_number);
241         return 0;
242 }
243
244
245
246
247 ExportEDLItem::ExportEDLItem(MWindow *mwindow)
248  : BC_MenuItem(_("Export EDL..."), "Shift-E", 'E')
249 {
250         this->mwindow = mwindow;
251         set_shift(1);
252 }
253
254 int ExportEDLItem::handle_event()
255 {
256         mwindow->exportedl->start_interactive();
257         return 1;
258 }
259
260
261
262
263
264 ExportEDL::ExportEDL(MWindow *mwindow)
265  : Thread(0, 0, 0)
266 {
267         this->mwindow = mwindow;
268 //      package_lock = new Mutex("ExportEDL::package_lock");
269 //      counter_lock = new Mutex("ExportEDL::counter_lock");
270 //      completion = new Condition(0, "ExportEDL::completion");
271 //      progress_timer = new Timer;
272 }
273
274 ExportEDL::~ExportEDL()
275 {
276 //      delete package_lock;
277 //      delete counter_lock;
278 //      delete completion;
279 ///     if(preferences) delete preferences;
280 //      delete progress_timer;
281 }
282
283 void ExportEDL::start_interactive()
284 {
285         if(!Thread::running())
286         {
287                 Thread::start();
288         }
289 }
290
291 void ExportEDL::run()
292 {
293         int result = 0;
294         exportasset = new ExportEDLAsset(mwindow, mwindow->edl);
295
296         exportasset->load_defaults();
297
298 // Get format from user
299                 result = 0;
300                 int filesok;
301
302                 do {
303                 // FIX
304                         filesok = 0;
305                         exportedl_window = new ExportEDLWindow(mwindow, this, exportasset);
306                         exportedl_window->create_objects();
307                         result = exportedl_window->run_window();
308                         if (! result) {
309                                 // add to recentlist only on OK
310                                 // Fix "EDL"!
311                                 exportedl_window->path_recent->add_item("EDLPATH", exportasset->path);
312                         }
313                         exportasset->track_number = exportedl_window->track_list->get_selection_number(0, 0);
314
315                         delete exportedl_window;
316                         exportedl_window = 0;
317                         if (!result)
318                         {
319                                 ArrayList<char*> paths;
320
321                                 paths.append(exportasset->path);
322                                 filesok = ConfirmSave::test_files(mwindow, &paths);
323                         }
324
325                 } while (!result && filesok);
326         mwindow->save_defaults();
327         exportasset->save_defaults();
328
329 // FIX
330         if(!result) exportasset->export_it();
331
332
333         delete exportasset;
334
335 }
336
337
338
339
340
341
342
343
344 #define WIDTH 410
345 #define HEIGHT 400
346
347 static const char *list_titles[] =
348 {
349         N_("No."),
350         N_("Track name")
351 };
352
353
354 static int list_widths[] =
355 {
356         40,
357         200
358 };
359
360 ExportEDLWindow::ExportEDLWindow(MWindow *mwindow, ExportEDL *exportedl, ExportEDLAsset *exportasset)
361  : BC_Window(_(PROGRAM_NAME ": Export EDL"),
362         mwindow->gui->get_screen_w(1, 0) / 2 - WIDTH / 2,
363         mwindow->gui->get_root_h(1) / 2 - HEIGHT / 2,
364         WIDTH,
365         HEIGHT,
366         (int)BC_INFINITY,
367         (int)BC_INFINITY,
368         0,
369         0,
370         1)
371 {
372         this->mwindow = mwindow;
373         this->exportasset = exportasset;
374 }
375
376 ExportEDLWindow::~ExportEDLWindow()
377 {
378 //      delete format_tools;
379 //      delete loadmode;
380 }
381
382
383
384 void ExportEDLWindow::create_objects()
385 {
386         int x = 5, y = 5;
387         add_subwindow(new BC_Title(x,
388                 y,
389                         _("Select a file to export to:")));
390         y += 25;
391
392         add_subwindow(path_textbox = new ExportEDLPathText(x, y, this));
393         x += 300;
394         path_recent = new BC_RecentList("EDLPATH", mwindow->defaults,
395                                         path_textbox, 10, x, y, 300, 100);
396         add_subwindow(path_recent);
397 // FIX
398         path_recent->load_items("EDLPATH");
399
400         x += 24;
401         add_subwindow(path_button = new BrowseButton(
402                 mwindow->theme, this, path_textbox, x, y - 4, exportasset->path,
403                 _("Output to file"), _("Select a file to write to:"), 0));
404
405         y += 34;
406         x = 5;
407         add_subwindow(new BC_Title(x, y, _("Select track to be exported:")));
408         y += 25;
409
410
411         items_tracks[0].remove_all_objects();
412         items_tracks[1].remove_all_objects();
413         int serial = 0;
414         if (exportasset->track_number == -1)
415                 exportasset->track_number = 0;
416         for(Track *track = mwindow->edl->tracks->first;
417                 track;
418                 track = track->next)
419         {
420
421                 char tmp[10];
422                 sprintf(tmp, "%i\n", serial+1);
423
424                 BC_ListBoxItem *listitem = new BC_ListBoxItem(tmp);
425                 if (serial == exportasset->track_number)
426                         listitem->set_selected(1);
427                 items_tracks[0].append(listitem);
428                 items_tracks[1].append(new BC_ListBoxItem(track->title));
429                 serial ++;
430
431         }
432
433
434         add_subwindow(track_list = new ExportEDLWindowTrackList(this, x, y, 400, 200, items_tracks));
435
436         y += 5 + track_list->get_h();
437         add_subwindow(new BC_Title(x, y, _("Currently only CMX 3600 format is supported")));
438
439
440         add_subwindow(new BC_OKButton(this));
441         add_subwindow(new BC_CancelButton(this));
442         show_window();
443 }
444
445
446 ExportEDLPathText::ExportEDLPathText(int x, int y, ExportEDLWindow *window)
447  : BC_TextBox(x, y, 300, 1, window->exportasset->path)
448 {
449         this->window = window;
450 }
451 ExportEDLPathText::~ExportEDLPathText()
452 {
453 }
454 int ExportEDLPathText::handle_event()
455 {
456         strcpy(window->exportasset->path, get_text());
457 //      window->handle_event();
458         return 1;
459 }
460
461 ExportEDLWindowTrackList::ExportEDLWindowTrackList(ExportEDLWindow *window,
462         int x,
463         int y,
464         int w,
465         int h,
466         ArrayList<BC_ListBoxItem*> *track_list)
467  : BC_ListBox(x,
468                 y,
469                 w,
470                 h,
471                 LISTBOX_TEXT,
472                 track_list,
473                 list_titles,
474                 list_widths,
475                 2)
476 {
477         this->window = window;
478 }
479
480 int ExportEDLWindowTrackList::handle_event()
481 {
482 //      window->exportasset->track_number = get_selection_number(0, 0);
483 //      printf("aaaaa %i\n", window->exportasset->track_number );
484 //      window->set_done(0);
485         return 1;
486 }
487
488
489