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