yuv colorspace/range + prefs, ffmpeg colorrange probe, x11 direct force colormodel...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / mwindowedit.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "asset.h"
22 #include "assets.h"
23 #include "awindowgui.h"
24 #include "awindow.h"
25 #include "bcsignals.h"
26 #include "cache.h"
27 #include "clip.h"
28 #include "clipedit.h"
29 #include "commercials.h"
30 #include "cplayback.h"
31 #include "ctimebar.h"
32 #include "cwindow.h"
33 #include "cwindowgui.h"
34 #include "bchash.h"
35 #include "edl.h"
36 #include "edlsession.h"
37 #include "filesystem.h"
38 #include "filexml.h"
39 #include "floatauto.h"
40 #include "floatautos.h"
41 #include "gwindow.h"
42 #include "gwindowgui.h"
43 #include "keyframe.h"
44 #include "language.h"
45 #include "labels.h"
46 #include "levelwindow.h"
47 #include "localsession.h"
48 #include "mainclock.h"
49 #include "maincursor.h"
50 #include "mainerror.h"
51 #include "mainindexes.h"
52 #include "mainmenu.h"
53 #include "mainsession.h"
54 #include "mainundo.h"
55 #include "maskautos.h"
56 #include "mtimebar.h"
57 #include "mwindowgui.h"
58 #include "mwindow.h"
59 #include "panauto.h"
60 #include "patchbay.h"
61 #include "playbackengine.h"
62 #include "pluginset.h"
63 #include "recordlabel.h"
64 #include "samplescroll.h"
65 #include "trackcanvas.h"
66 #include "track.h"
67 #include "trackscroll.h"
68 #include "tracks.h"
69 #include "transition.h"
70 #include "transportque.h"
71 #include "units.h"
72 #include "undostackitem.h"
73 #include "vplayback.h"
74 #include "vwindow.h"
75 #include "vwindowgui.h"
76 #include "zoombar.h"
77 #include "zwindow.h"
78 #include "automation.h"
79 #include "maskautos.h"
80
81 #include <string.h>
82
83 void MWindow::add_audio_track_entry(int above, Track *dst)
84 {
85         undo->update_undo_before();
86         add_audio_track(above, dst);
87         save_backup();
88         undo->update_undo_after(_("add track"), LOAD_ALL);
89
90         restart_brender();
91         gui->update(1, 1, 0, 0, 1, 0, 0);
92         gui->activate_timeline();
93
94 //      gui->get_scrollbars(0);
95 //      gui->canvas->draw();
96 //      gui->patchbay->update();
97 //      gui->cursor->draw(1);
98 //      gui->canvas->flash();
99 //      gui->canvas->activate();
100         cwindow->refresh_frame(CHANGE_EDL);
101 }
102
103 void MWindow::add_video_track_entry(Track *dst)
104 {
105         undo->update_undo_before();
106         add_video_track(1, dst);
107         undo->update_undo_after(_("add track"), LOAD_ALL);
108
109         restart_brender();
110
111         gui->update(1, 1, 0, 0, 1, 0, 0);
112         gui->activate_timeline();
113 //      gui->get_scrollbars(0);
114 //      gui->canvas->draw();
115 //      gui->patchbay->update();
116 //      gui->cursor->draw(1);
117 //      gui->canvas->flash();
118 //      gui->canvas->activate();
119         cwindow->refresh_frame(CHANGE_EDL);
120         save_backup();
121 }
122
123 void MWindow::add_subttl_track_entry(Track *dst)
124 {
125         undo->update_undo_before();
126         add_subttl_track(1, dst);
127         undo->update_undo_after(_("add track"), LOAD_ALL);
128
129         restart_brender();
130
131         gui->update(1, 1, 0, 0, 1, 0, 0);
132         gui->activate_timeline();
133 //      gui->get_scrollbars(0);
134 //      gui->canvas->draw();
135 //      gui->patchbay->update();
136 //      gui->cursor->draw(1);
137 //      gui->canvas->flash();
138 //      gui->canvas->activate();
139         cwindow->refresh_frame(CHANGE_EDL);
140         save_backup();
141 }
142
143
144 int MWindow::add_audio_track(int above, Track *dst)
145 {
146         edl->tracks->add_audio_track(above, dst);
147         edl->tracks->update_y_pixels(theme);
148         save_backup();
149         return 0;
150 }
151
152 int MWindow::add_video_track(int above, Track *dst)
153 {
154         edl->tracks->add_video_track(above, dst);
155         edl->tracks->update_y_pixels(theme);
156         save_backup();
157         return 0;
158 }
159
160 int MWindow::add_subttl_track(int above, Track *dst)
161 {
162         edl->tracks->add_subttl_track(above, dst);
163         edl->tracks->update_y_pixels(theme);
164         save_backup();
165         return 0;
166 }
167
168 void MWindow::asset_to_all()
169 {
170         if( !session->drag_assets->size() ) return;
171         Indexable *indexable = session->drag_assets->get(0);
172
173 //      if( indexable->have_video() )
174         {
175                 int w, h;
176
177                 undo->update_undo_before();
178
179 // Get w and h
180                 w = indexable->get_w();
181                 h = indexable->get_h();
182                 double new_framerate = session->drag_assets->get(0)->get_frame_rate();
183                 double old_framerate = edl->session->frame_rate;
184                 int old_samplerate = edl->session->sample_rate;
185                 int new_samplerate = session->drag_assets->get(0)->get_sample_rate();
186
187
188                 if( indexable->have_video() ) {
189                         edl->session->output_w = w;
190                         edl->session->output_h = h;
191                         edl->session->frame_rate = new_framerate;
192                         create_aspect_ratio(
193                                 edl->session->aspect_w,
194                                 edl->session->aspect_h,
195                                 w, h);
196
197                         for( Track *current = edl->tracks->first; current; current = NEXT ) {
198                                 if( current->data_type == TRACK_VIDEO /* &&
199                                         current->record */  ) {
200                                         current->track_w = w;
201                                         current->track_h = h;
202                                 }
203                         }
204
205
206                         if( ((edl->session->output_w % 4) ||
207                                 (edl->session->output_h % 4)) &&
208                                 edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL ) {
209                                 MainError::show_error(
210                                         _("This project's dimensions are not multiples of 4 so\n"
211                                         "it can't be rendered by OpenGL."));
212                         }
213
214 // Get aspect ratio
215                         if( defaults->get("AUTOASPECT", 0) ) {
216                                 create_aspect_ratio(
217                                         edl->session->aspect_w,
218                                         edl->session->aspect_h,
219                                         w, h);
220                         }
221                 }
222
223                 if( indexable->have_audio() ) {
224                         edl->session->sample_rate = new_samplerate;
225                         edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
226                         edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
227                 }
228
229                 save_backup();
230
231                 undo->update_undo_after(_("asset to all"), LOAD_ALL);
232                 restart_brender();
233                 gui->update(1, 2, 1, 1, 1, 1, 0);
234                 sync_parameters(CHANGE_ALL);
235         }
236 }
237
238 void MWindow::asset_to_size()
239 {
240         if( !session->drag_assets->size() ) return;
241         Indexable *indexable = session->drag_assets->get(0);
242
243         if( indexable->have_video() ) {
244                 int w, h;
245                 undo->update_undo_before();
246
247 // Get w and h
248                 w = indexable->get_w();
249                 h = indexable->get_h();
250                 edl->session->output_w = w;
251                 edl->session->output_h = h;
252
253                 if( ((edl->session->output_w % 4) ||
254                         (edl->session->output_h % 4)) &&
255                         edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL ) {
256                         MainError::show_error(
257                                 _("This project's dimensions are not multiples of 4 so\n"
258                                 "it can't be rendered by OpenGL."));
259                 }
260
261 // Get aspect ratio
262                 if( defaults->get("AUTOASPECT", 0) ) {
263                         create_aspect_ratio(edl->session->aspect_w,
264                                 edl->session->aspect_h,
265                                 w,
266                                 h);
267                 }
268
269                 save_backup();
270
271                 undo->update_undo_after(_("asset to size"), LOAD_ALL);
272                 restart_brender();
273                 sync_parameters(CHANGE_ALL);
274         }
275 }
276
277
278 void MWindow::asset_to_rate()
279 {
280         if( session->drag_assets->size() &&
281                 session->drag_assets->get(0)->have_video() ) {
282                 double new_framerate = session->drag_assets->get(0)->get_frame_rate();
283                 double old_framerate = edl->session->frame_rate;
284                 undo->update_undo_before();
285
286                 edl->session->frame_rate = new_framerate;
287                 edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
288
289                 save_backup();
290
291                 undo->update_undo_after(_("asset to rate"), LOAD_ALL);
292                 restart_brender();
293                 gui->update(1, 2, 1, 1, 1, 1, 0);
294                 sync_parameters(CHANGE_ALL);
295         }
296 }
297
298
299 void MWindow::clear_entry()
300 {
301         undo->update_undo_before();
302         clear(1);
303
304         edl->optimize();
305         save_backup();
306         undo->update_undo_after(_("clear"), LOAD_EDITS | LOAD_TIMEBAR);
307
308         restart_brender();
309         update_plugin_guis();
310         gui->update(1, 2, 1, 1, 1, 1, 0);
311         cwindow->update(1, 0, 0, 0, 1);
312         cwindow->refresh_frame(CHANGE_EDL);
313 }
314
315 void MWindow::clear(int clear_handle)
316 {
317         double start = edl->local_session->get_selectionstart();
318         double end = edl->local_session->get_selectionend();
319         if( clear_handle || !EQUIV(start, end) ) {
320                 edl->clear(start,
321                         end,
322                         edl->session->labels_follow_edits,
323                         edl->session->plugins_follow_edits,
324                         edl->session->autos_follow_edits);
325         }
326 }
327
328 void MWindow::set_automation_mode(int mode)
329 {
330         undo->update_undo_before();
331         edl->tracks->set_automation_mode(
332                 edl->local_session->get_selectionstart(),
333                 edl->local_session->get_selectionend(),
334                 mode);
335         save_backup();
336         char string[BCSTRLEN];
337         sprintf(string,"set %s", FloatAuto::curve_name(mode));
338         undo->update_undo_after(string, LOAD_AUTOMATION);
339
340         restart_brender();
341         update_plugin_guis();
342         gui->draw_overlays(1);
343         sync_parameters(CHANGE_PARAMS);
344         gui->update_patchbay();
345         cwindow->update(1, 0, 0);
346 }
347
348 void MWindow::clear_automation()
349 {
350         undo->update_undo_before();
351         edl->tracks->clear_automation(edl->local_session->get_selectionstart(),
352                 edl->local_session->get_selectionend());
353         save_backup();
354         undo->update_undo_after(_("clear keyframes"), LOAD_AUTOMATION);
355
356         restart_brender();
357         update_plugin_guis();
358         gui->draw_overlays(1);
359         sync_parameters(CHANGE_PARAMS);
360         gui->update_patchbay();
361         cwindow->update(1, 0, 0);
362 }
363
364 int MWindow::clear_default_keyframe()
365 {
366         undo->update_undo_before();
367         edl->tracks->clear_default_keyframe();
368         save_backup();
369         undo->update_undo_after(_("clear default keyframe"), LOAD_AUTOMATION);
370
371         restart_brender();
372         gui->draw_overlays(1);
373         sync_parameters(CHANGE_PARAMS);
374         gui->update_patchbay();
375         cwindow->update(1, 0, 0);
376
377         return 0;
378 }
379
380 void MWindow::clear_labels()
381 {
382         undo->update_undo_before();
383         clear_labels(edl->local_session->get_selectionstart(),
384                 edl->local_session->get_selectionend());
385         undo->update_undo_after(_("clear labels"), LOAD_TIMEBAR);
386
387         gui->update_timebar(1);
388         cwindow->update(0, 0, 0, 0, 1);
389         save_backup();
390 }
391
392 int MWindow::clear_labels(double start, double end)
393 {
394         edl->labels->clear(start, end, 0);
395         return 0;
396 }
397
398 void MWindow::concatenate_tracks()
399 {
400         undo->update_undo_before();
401         edl->tracks->concatenate_tracks(edl->session->plugins_follow_edits,
402                 edl->session->autos_follow_edits);
403         save_backup();
404         undo->update_undo_after(_("concatenate tracks"), LOAD_EDITS);
405
406         restart_brender();
407         gui->update(1, 1, 0, 0, 1, 0, 0);
408         cwindow->refresh_frame(CHANGE_EDL);
409 }
410
411
412 void MWindow::copy()
413 {
414         copy(edl->local_session->get_selectionstart(),
415                 edl->local_session->get_selectionend());
416 }
417
418 int MWindow::copy(double start, double end)
419 {
420         if( start == end ) return 1;
421
422         FileXML file;
423         edl->copy(start, end, 0, 0, 0, &file, "", 1);
424         const char *file_string = file.string();
425         long file_length = strlen(file_string);
426         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
427         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
428         save_backup();
429         return 0;
430 }
431
432 int MWindow::copy_automation()
433 {
434         FileXML file;
435         double start = edl->local_session->get_selectionstart();
436         double end = edl->local_session->get_selectionend();
437         edl->tracks->copy_automation(start, end, &file, 0, 1);
438         const char *file_string = file.string();
439         long file_length = strlen(file_string);
440         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
441         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
442         return 0;
443 }
444
445 int MWindow::copy_default_keyframe()
446 {
447         FileXML file;
448         double start = edl->local_session->get_selectionstart();
449         double end = edl->local_session->get_selectionend();
450         edl->tracks->copy_automation(start, end, &file, 1, 0);
451         const char *file_string = file.string();
452         long file_length = strlen(file_string);
453         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
454         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
455         return 0;
456 }
457
458
459 // Uses cropping coordinates in edl session to crop and translate video.
460 // We modify the projector since camera automation depends on the track size.
461 void MWindow::crop_video()
462 {
463
464         undo->update_undo_before();
465 // Clamp EDL crop region
466         if( edl->session->crop_x1 > edl->session->crop_x2 ) {
467                 edl->session->crop_x1 ^= edl->session->crop_x2;
468                 edl->session->crop_x2 ^= edl->session->crop_x1;
469                 edl->session->crop_x1 ^= edl->session->crop_x2;
470         }
471         if( edl->session->crop_y1 > edl->session->crop_y2 ) {
472                 edl->session->crop_y1 ^= edl->session->crop_y2;
473                 edl->session->crop_y2 ^= edl->session->crop_y1;
474                 edl->session->crop_y1 ^= edl->session->crop_y2;
475         }
476
477         float old_projector_x = (float)edl->session->output_w / 2;
478         float old_projector_y = (float)edl->session->output_h / 2;
479         float new_projector_x = (float)(edl->session->crop_x1 + edl->session->crop_x2) / 2;
480         float new_projector_y = (float)(edl->session->crop_y1 + edl->session->crop_y2) / 2;
481         float projector_offset_x = -(new_projector_x - old_projector_x);
482         float projector_offset_y = -(new_projector_y - old_projector_y);
483
484         edl->tracks->translate_projector(projector_offset_x, projector_offset_y);
485
486         edl->session->output_w = edl->session->crop_x2 - edl->session->crop_x1;
487         edl->session->output_h = edl->session->crop_y2 - edl->session->crop_y1;
488         edl->session->crop_x1 = 0;
489         edl->session->crop_y1 = 0;
490         edl->session->crop_x2 = edl->session->output_w;
491         edl->session->crop_y2 = edl->session->output_h;
492
493 // Recalculate aspect ratio
494         if( defaults->get("AUTOASPECT", 0) ) {
495                 create_aspect_ratio(edl->session->aspect_w,
496                         edl->session->aspect_h,
497                         edl->session->output_w,
498                         edl->session->output_h);
499         }
500
501         undo->update_undo_after(_("crop"), LOAD_ALL);
502
503         restart_brender();
504         cwindow->refresh_frame(CHANGE_ALL);
505         save_backup();
506 }
507
508 void MWindow::cut()
509 {
510         double start = edl->local_session->get_selectionstart();
511         double end = edl->local_session->get_selectionend();
512         if( EQUIV(start,end) )
513                 blade(start);
514         else
515                 cut(start, end);
516 }
517
518 void MWindow::blade(double position)
519 {
520         undo->update_undo_before();
521         edl->blade(position);
522         edl->optimize();
523         save_backup();
524         undo->update_undo_after(_("blade"), LOAD_EDITS | LOAD_TIMEBAR);
525         restart_brender();
526         update_plugin_guis();
527         gui->update(1, 2, 1, 1, 1, 1, 0);
528         cwindow->update(1, 0, 0, 0, 1);
529         awindow->gui->async_update_assets();
530         cwindow->refresh_frame(CHANGE_EDL);
531 }
532
533 void MWindow::cut(double start, double end, double new_position)
534 {
535         undo->update_undo_before();
536         copy(start, end);
537         edl->clear(start, end,
538                 edl->session->labels_follow_edits,
539                 edl->session->plugins_follow_edits,
540                 edl->session->autos_follow_edits);
541
542         edl->optimize();
543         save_backup();
544         undo->update_undo_after(_("cut"), LOAD_EDITS | LOAD_TIMEBAR);
545         if( new_position >= 0 ) {
546                 edl->local_session->set_selectionstart(new_position);
547                 edl->local_session->set_selectionend(new_position);
548         }
549         restart_brender();
550         update_plugin_guis();
551         gui->update(1, 2, 1, 1, 1, 1, 0);
552         cwindow->update(1, 0, 0, 0, 1);
553         awindow->gui->async_update_assets();
554         cwindow->refresh_frame(CHANGE_EDL);
555 }
556
557 void MWindow::snap_left_edit()
558 {
559         double start_pos = edl->local_session->get_selectionstart(1);
560         double position = edl->prev_edit(start_pos);
561         if( position < start_pos )
562                 cut(position, start_pos, position);
563 }
564
565 void MWindow::snap_right_edit()
566 {
567         double end_pos = edl->local_session->get_selectionend(1);
568         double position = edl->next_edit(end_pos);
569         if( end_pos < position )
570                 cut(end_pos, position, end_pos);
571 }
572
573 void MWindow::snap_left_label()
574 {
575         double start_pos = edl->local_session->get_selectionstart(1);
576         Label *left_label = edl->labels->prev_label(start_pos);
577         if( !left_label ) return;
578         double position = left_label->position;
579         if( position < start_pos )
580                 cut(position, start_pos, position);
581 }
582
583 void MWindow::snap_right_label()
584 {
585         double end_pos = edl->local_session->get_selectionend(1);
586         Label *right_label = edl->labels->next_label(end_pos);
587         if( !right_label ) return;
588         double position = right_label->position;
589         if( end_pos < position )
590                 cut(end_pos, position, end_pos);
591 }
592
593 int MWindow::cut_automation()
594 {
595         undo->update_undo_before();
596
597         copy_automation();
598
599         edl->tracks->clear_automation(edl->local_session->get_selectionstart(),
600                 edl->local_session->get_selectionend());
601         save_backup();
602         undo->update_undo_after(_("cut keyframes"), LOAD_AUTOMATION);
603
604
605         restart_brender();
606         update_plugin_guis();
607         gui->draw_overlays(1);
608         sync_parameters(CHANGE_PARAMS);
609         gui->update_patchbay();
610         cwindow->update(1, 0, 0);
611         return 0;
612 }
613
614 int MWindow::cut_default_keyframe()
615 {
616
617         undo->update_undo_before();
618         copy_default_keyframe();
619         edl->tracks->clear_default_keyframe();
620         undo->update_undo_after(_("cut default keyframe"), LOAD_AUTOMATION);
621
622         restart_brender();
623         gui->draw_overlays(1);
624         sync_parameters(CHANGE_PARAMS);
625         gui->update_patchbay();
626         cwindow->update(1, 0, 0);
627         save_backup();
628
629
630         return 0;
631 }
632
633
634 void MWindow::delete_track()
635 {
636         if( edl->tracks->last )
637                 delete_track(edl->tracks->last);
638 }
639
640 void MWindow::delete_tracks()
641 {
642         undo->update_undo_before();
643         edl->tracks->delete_tracks();
644         undo->update_undo_after(_("delete tracks"), LOAD_ALL);
645         save_backup();
646
647         restart_brender();
648         update_plugin_states();
649
650         gui->update(1, 1, 1, 0, 1, 0, 0);
651         cwindow->refresh_frame(CHANGE_EDL);
652 }
653
654 void MWindow::delete_track(Track *track)
655 {
656         undo->update_undo_before();
657         edl->tracks->delete_track(track);
658         undo->update_undo_after(_("delete track"), LOAD_ALL);
659
660         restart_brender();
661         update_plugin_states();
662
663         gui->update(1, 1, 1, 0, 1, 0, 0);
664         cwindow->refresh_frame(CHANGE_EDL);
665         save_backup();
666 }
667
668
669 // Insert data from clipboard
670 void MWindow::insert(double position, FileXML *file,
671         int edit_labels, int edit_plugins, int edit_autos,
672         EDL *parent_edl)
673 {
674 // For clipboard pasting make the new edl use a separate session
675 // from the master EDL.  Then it can be resampled to the master rates.
676 // For splice, overwrite, and dragging need same session to get the assets.
677         EDL *edl = new EDL(parent_edl);
678         ArrayList<EDL*> new_edls;
679         uint32_t load_flags = LOAD_ALL;
680
681
682         new_edls.append(edl);
683         edl->create_objects();
684
685
686
687
688         if( parent_edl ) load_flags &= ~LOAD_SESSION;
689         if( !edl->session->autos_follow_edits ) load_flags &= ~LOAD_AUTOMATION;
690         if( !edl->session->labels_follow_edits ) load_flags &= ~LOAD_TIMEBAR;
691
692         edl->load_xml(file, load_flags);
693
694
695 //printf("MWindow::insert %f\n", edl->local_session->clipboard_length);
696
697
698
699         paste_edls(&new_edls, LOADMODE_PASTE, 0, position,
700                 edit_labels, edit_plugins, edit_autos, 0); // overwrite
701 // if( vwindow->edl )
702 // printf("MWindow::insert 5 %f %f\n",
703 // vwindow->edl->local_session->in_point,
704 // vwindow->edl->local_session->out_point);
705         new_edls.remove_all();
706         edl->Garbage::remove_user();
707 //printf("MWindow::insert 6 %p\n", vwindow->get_edl());
708 }
709
710 void MWindow::insert_effects_canvas(double start,
711         double length)
712 {
713         Track *dest_track = session->track_highlighted;
714         if( !dest_track ) return;
715
716         undo->update_undo_before();
717
718         for( int i = 0; i < session->drag_pluginservers->total; i++ ) {
719                 PluginServer *plugin = session->drag_pluginservers->values[i];
720                 insert_effect(plugin->title, 0, dest_track,
721                         i == 0 ? session->pluginset_highlighted : 0,
722                         start, length, PLUGIN_STANDALONE);
723         }
724
725         save_backup();
726         undo->update_undo_after(_("insert effect"), LOAD_EDITS | LOAD_PATCHES);
727         restart_brender();
728         sync_parameters(CHANGE_EDL);
729 // GUI updated in TrackCanvas, after current_operations are reset
730 }
731
732 void MWindow::insert_effects_cwindow(Track *dest_track)
733 {
734         if( !dest_track ) return;
735
736         undo->update_undo_before();
737
738         double start = 0;
739         double length = dest_track->get_length();
740
741         if( edl->local_session->get_selectionend() >
742                 edl->local_session->get_selectionstart() ) {
743                 start = edl->local_session->get_selectionstart();
744                 length = edl->local_session->get_selectionend() -
745                         edl->local_session->get_selectionstart();
746         }
747
748         for( int i = 0; i < session->drag_pluginservers->total; i++ ) {
749                 PluginServer *plugin = session->drag_pluginservers->values[i];
750                 insert_effect(plugin->title, 0, dest_track, 0,
751                         start, length, PLUGIN_STANDALONE);
752         }
753
754         save_backup();
755         undo->update_undo_after(_("insert effect"), LOAD_EDITS | LOAD_PATCHES);
756         restart_brender();
757         sync_parameters(CHANGE_EDL);
758         gui->update(1, 1, 0, 0, 1, 0, 0);
759 }
760
761 void MWindow::insert_effect(char *title,
762         SharedLocation *shared_location,
763         int data_type,
764         int plugin_type,
765         int single_standalone)
766 {
767         Track *current = edl->tracks->first;
768         SharedLocation shared_location_local;
769         shared_location_local.copy_from(shared_location);
770         int first_track = 1;
771         for( ; current; current = NEXT) {
772                 if( current->data_type == data_type &&
773                         current->record ) {
774                         insert_effect(title, &shared_location_local,
775                                 current, 0, 0, 0, plugin_type);
776
777                         if( first_track ) {
778                                 if( plugin_type == PLUGIN_STANDALONE && single_standalone ) {
779                                         plugin_type = PLUGIN_SHAREDPLUGIN;
780                                         shared_location_local.module = edl->tracks->number_of(current);
781                                         shared_location_local.plugin = current->plugin_set.total - 1;
782                                 }
783                                 first_track = 0;
784                         }
785                 }
786         }
787 }
788
789
790 void MWindow::insert_effect(char *title,
791         SharedLocation *shared_location,
792         Track *track,
793         PluginSet *plugin_set,
794         double start,
795         double length,
796         int plugin_type)
797 {
798         KeyFrame *default_keyframe = 0;
799         PluginServer *server = 0;
800 // Get default keyframe
801         if( plugin_type == PLUGIN_STANDALONE ) {
802                 default_keyframe = new KeyFrame;
803                 server = new PluginServer(*scan_plugindb(title, track->data_type));
804
805                 server->open_plugin(0, preferences, edl, 0);
806                 server->save_data(default_keyframe);
807         }
808 // Insert plugin object
809         track->insert_effect(title, shared_location,
810                 default_keyframe, plugin_set,
811                 start, length, plugin_type);
812         track->optimize();
813
814         if( plugin_type == PLUGIN_STANDALONE ) {
815                 server->close_plugin();
816                 delete server;
817                 delete default_keyframe;
818         }
819 }
820
821 int MWindow::modify_edithandles()
822 {
823         undo->update_undo_before();
824         edl->modify_edithandles(session->drag_start,
825                 session->drag_position,
826                 session->drag_handle,
827                 edl->session->edit_handle_mode[session->drag_button],
828                 edl->session->labels_follow_edits,
829                 edl->session->plugins_follow_edits,
830                 edl->session->autos_follow_edits);
831
832         finish_modify_handles();
833 //printf("MWindow::modify_handles 1\n");
834         return 0;
835 }
836
837 int MWindow::modify_pluginhandles()
838 {
839         undo->update_undo_before();
840
841         edl->modify_pluginhandles(session->drag_start,
842                 session->drag_position,
843                 session->drag_handle,
844                 edl->session->edit_handle_mode[session->drag_button],
845                 edl->session->labels_follow_edits,
846                 edl->session->autos_follow_edits,
847                 session->trim_edits);
848
849         finish_modify_handles();
850
851         return 0;
852 }
853
854
855 // Common to edithandles and plugin handles
856 void MWindow::finish_modify_handles()
857 {
858         int edit_mode = edl->session->edit_handle_mode[session->drag_button];
859
860         if( (session->drag_handle == 1 && edit_mode != MOVE_NO_EDITS) ||
861                 (session->drag_handle == 0 && edit_mode == MOVE_ONE_EDIT) ) {
862 //printf("MWindow::finish_modify_handles %d\n", __LINE__);
863                 edl->local_session->set_selectionstart(session->drag_position);
864                 edl->local_session->set_selectionend(session->drag_position);
865         }
866         else
867         if( edit_mode != MOVE_NO_EDITS ) {
868 //printf("MWindow::finish_modify_handles %d\n", __LINE__);
869                 edl->local_session->set_selectionstart(session->drag_start);
870                 edl->local_session->set_selectionend(session->drag_start);
871         }
872
873 // clamp the selection to 0
874         if( edl->local_session->get_selectionstart(1) < 0 ) {
875                 edl->local_session->set_selectionstart(0);
876                 edl->local_session->set_selectionend(0);
877         }
878         undo->update_undo_after(_("drag handle"), LOAD_EDITS | LOAD_TIMEBAR);
879
880         save_backup();
881         restart_brender();
882         sync_parameters(CHANGE_EDL);
883         update_plugin_guis();
884         gui->update(1, 2, 1, 1, 1, 1, 0);
885 // label list can be modified
886         awindow->gui->async_update_assets();
887         cwindow->update(1, 0, 0, 0, 1);
888 }
889
890 void MWindow::match_output_size(Track *track)
891 {
892         undo->update_undo_before();
893         track->track_w = edl->session->output_w;
894         track->track_h = edl->session->output_h;
895         save_backup();
896         undo->update_undo_after(_("match output size"), LOAD_ALL);
897
898         restart_brender();
899         sync_parameters(CHANGE_EDL);
900 }
901
902
903 void MWindow::move_edits(ArrayList<Edit*> *edits,
904                 Track *track,
905                 double position,
906                 int behaviour)
907 {
908         undo->update_undo_before();
909
910         edl->tracks->move_edits(edits,
911                 track,
912                 position,
913                 edl->session->labels_follow_edits,
914                 edl->session->plugins_follow_edits,
915                 edl->session->autos_follow_edits,
916                 behaviour);
917
918         save_backup();
919         undo->update_undo_after(_("move edit"), LOAD_ALL);
920
921         restart_brender();
922         cwindow->refresh_frame(CHANGE_EDL);
923
924         update_plugin_guis();
925         gui->update(1, 1,      // 1 for incremental drawing.  2 for full refresh
926                 1, 0, 0, 0, 0);
927 }
928
929 void MWindow::move_effect(Plugin *plugin, Track *track, int64_t position)
930 {
931         undo->update_undo_before();
932         edl->tracks->move_effect(plugin, track, position);
933         save_backup();
934         undo->update_undo_after(_("paste effect"), LOAD_ALL);
935
936         restart_brender();
937         cwindow->refresh_frame(CHANGE_EDL);
938         update_plugin_guis();
939         gui->update(1, 1,      // 1 for incremental drawing.  2 for full refresh
940                 0, 0, 0, 0, 0);
941 }
942
943 void MWindow::move_effect(Plugin *plugin, PluginSet *plugin_set, int64_t position)
944 {
945         undo->update_undo_before();
946         edl->tracks->move_effect(plugin, plugin_set, position);
947         save_backup();
948         undo->update_undo_after(_("move effect"), LOAD_ALL);
949
950         restart_brender();
951         cwindow->refresh_frame(CHANGE_EDL);
952         update_plugin_guis();
953         gui->update(1, 1,      // 1 for incremental drawing.  2 for full refresh
954                 0, 0, 0, 0, 0);
955 }
956
957 void MWindow::move_plugins_up(PluginSet *plugin_set)
958 {
959
960         undo->update_undo_before();
961         plugin_set->track->move_plugins_up(plugin_set);
962
963         save_backup();
964         undo->update_undo_after(_("move effect up"), LOAD_ALL);
965         restart_brender();
966         gui->update(1, 1,      // 1 for incremental drawing.  2 for full refresh
967                 0, 0, 0, 0, 0);
968         sync_parameters(CHANGE_EDL);
969 }
970
971 void MWindow::move_plugins_down(PluginSet *plugin_set)
972 {
973         undo->update_undo_before();
974
975         plugin_set->track->move_plugins_down(plugin_set);
976
977         save_backup();
978         undo->update_undo_after(_("move effect down"), LOAD_ALL);
979         restart_brender();
980         gui->update(1, 1,      // 1 for incremental drawing.  2 for full refresh
981                 0, 0, 0, 0, 0);
982         sync_parameters(CHANGE_EDL);
983 }
984
985 void MWindow::move_track_down(Track *track)
986 {
987         undo->update_undo_before();
988         edl->tracks->move_track_down(track);
989         save_backup();
990         undo->update_undo_after(_("move track down"), LOAD_ALL);
991
992         restart_brender();
993         gui->update(1, 1, 0, 0, 1, 0, 0);
994         sync_parameters(CHANGE_EDL);
995         save_backup();
996 }
997
998 void MWindow::move_tracks_down()
999 {
1000         undo->update_undo_before();
1001         edl->tracks->move_tracks_down();
1002         save_backup();
1003         undo->update_undo_after(_("move tracks down"), LOAD_ALL);
1004
1005         restart_brender();
1006         gui->update(1, 1, 0, 0, 1, 0, 0);
1007         sync_parameters(CHANGE_EDL);
1008         save_backup();
1009 }
1010
1011 void MWindow::move_track_up(Track *track)
1012 {
1013         undo->update_undo_before();
1014         edl->tracks->move_track_up(track);
1015         save_backup();
1016         undo->update_undo_after(_("move track up"), LOAD_ALL);
1017         restart_brender();
1018         gui->update(1, 1, 0, 0, 1, 0, 0);
1019         sync_parameters(CHANGE_EDL);
1020         save_backup();
1021 }
1022
1023 void MWindow::move_tracks_up()
1024 {
1025         undo->update_undo_before();
1026         edl->tracks->move_tracks_up();
1027         save_backup();
1028         undo->update_undo_after(_("move tracks up"), LOAD_ALL);
1029         restart_brender();
1030         gui->update(1, 1, 0, 0, 1, 0, 0);
1031         sync_parameters(CHANGE_EDL);
1032 }
1033
1034
1035 void MWindow::mute_selection()
1036 {
1037         double start = edl->local_session->get_selectionstart();
1038         double end = edl->local_session->get_selectionend();
1039         if( start != end ) {
1040                 undo->update_undo_before();
1041                 edl->clear(start, end, 0,
1042                         edl->session->plugins_follow_edits,
1043                         edl->session->autos_follow_edits);
1044                 edl->local_session->set_selectionend(end);
1045                 edl->local_session->set_selectionstart(start);
1046                 edl->paste_silence(start, end, 0,
1047                         edl->session->plugins_follow_edits,
1048                         edl->session->autos_follow_edits);
1049
1050                 save_backup();
1051                 undo->update_undo_after(_("mute"), LOAD_EDITS);
1052
1053                 restart_brender();
1054                 update_plugin_guis();
1055                 gui->update(1, 2, 1, 1, 1, 1, 0);
1056                 cwindow->refresh_frame(CHANGE_EDL);
1057         }
1058 }
1059
1060
1061 void MWindow::overwrite(EDL *source)
1062 {
1063         FileXML file;
1064
1065         double src_start = source->local_session->get_selectionstart();
1066         double overwrite_len = source->local_session->get_selectionend() - src_start;
1067         double dst_start = edl->local_session->get_selectionstart();
1068         double dst_len = edl->local_session->get_selectionend() - dst_start;
1069
1070         undo->update_undo_before();
1071         if( !EQUIV(dst_len, 0) && (dst_len < overwrite_len) ) {
1072 // in/out points or selection present and shorter than overwrite range
1073 // shorten the copy range
1074                 overwrite_len = dst_len;
1075         }
1076
1077         source->copy(src_start, src_start + overwrite_len,
1078                 0, 0, 0, &file, "", 1);
1079
1080 // HACK around paste_edl get_start/endselection on its own
1081 // so we need to clear only when not using both io points
1082 // FIXME: need to write simple overwrite_edl to be used for overwrite function
1083         if( edl->local_session->get_inpoint() < 0 ||
1084                 edl->local_session->get_outpoint() < 0 )
1085                 edl->clear(dst_start, dst_start + overwrite_len, 0, 0, 0);
1086
1087         paste(dst_start, dst_start + overwrite_len,
1088                 &file, 0, 0, 0);
1089
1090         edl->local_session->set_selectionstart(dst_start + overwrite_len);
1091         edl->local_session->set_selectionend(dst_start + overwrite_len);
1092
1093         save_backup();
1094         undo->update_undo_after(_("overwrite"), LOAD_EDITS);
1095
1096         restart_brender();
1097         update_plugin_guis();
1098         gui->update(1, 1, 1, 1, 0, 1, 0);
1099         sync_parameters(CHANGE_EDL);
1100 }
1101
1102 // For splice and overwrite
1103 int MWindow::paste(double start,
1104         double end,
1105         FileXML *file,
1106         int edit_labels,
1107         int edit_plugins,
1108         int edit_autos)
1109 {
1110         clear(0);
1111
1112 // Want to insert with assets shared with the master EDL.
1113         insert(start, file,
1114                 edit_labels, edit_plugins, edit_autos,
1115                 edl);
1116
1117         return 0;
1118 }
1119
1120 // For editing using insertion point
1121 void MWindow::paste()
1122 {
1123         double start = edl->local_session->get_selectionstart();
1124         //double end = edl->local_session->get_selectionend();
1125         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1126
1127         if( len ) {
1128                 char *string = new char[len];
1129                 undo->update_undo_before();
1130                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1131                 FileXML file;
1132                 file.read_from_string(string);
1133                 clear(0);
1134
1135                 insert(start, &file,
1136                         edl->session->labels_follow_edits,
1137                         edl->session->plugins_follow_edits,
1138                         edl->session->autos_follow_edits,
1139                         0);
1140
1141                 edl->optimize();
1142                 delete [] string;
1143
1144                 save_backup();
1145
1146                 undo->update_undo_after(_("paste"), LOAD_EDITS | LOAD_TIMEBAR);
1147                 restart_brender();
1148                 update_plugin_guis();
1149                 gui->update(1, 2, 1, 1, 0, 1, 0);
1150                 awindow->gui->async_update_assets();
1151                 sync_parameters(CHANGE_EDL);
1152         }
1153
1154 }
1155
1156 int MWindow::paste_assets(double position, Track *dest_track, int overwrite)
1157 {
1158         int result = 0;
1159         undo->update_undo_before();
1160
1161         if( session->drag_assets->total ) {
1162                 load_assets(session->drag_assets,
1163                         position, LOADMODE_PASTE, dest_track, 0,
1164                         edl->session->labels_follow_edits,
1165                         edl->session->plugins_follow_edits,
1166                         edl->session->autos_follow_edits,
1167                         overwrite);
1168                 result = 1;
1169         }
1170
1171         if( session->drag_clips->total ) {
1172                 paste_edls(session->drag_clips,
1173                         LOADMODE_PASTE, dest_track, position,
1174                         edl->session->labels_follow_edits,
1175                         edl->session->plugins_follow_edits,
1176                         edl->session->autos_follow_edits,
1177                         overwrite);
1178                 result = 1;
1179         }
1180
1181         save_backup();
1182
1183         undo->update_undo_after(_("paste assets"), LOAD_EDITS);
1184         restart_brender();
1185         gui->update(1, 2, 1, 0, 0, 1, 0);
1186         sync_parameters(CHANGE_EDL);
1187         return result;
1188 }
1189
1190 void MWindow::load_assets(ArrayList<Indexable*> *new_assets,
1191         double position, int load_mode, Track *first_track, RecordLabels *labels,
1192         int edit_labels, int edit_plugins, int edit_autos, int overwrite)
1193 {
1194         if( load_mode == LOADMODE_RESOURCESONLY )
1195                 load_mode = LOADMODE_ASSETSONLY;
1196 const int debug = 0;
1197 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1198         if( position < 0 ) position = edl->local_session->get_selectionstart();
1199
1200         ArrayList<EDL*> new_edls;
1201         for( int i = 0; i < new_assets->total; i++ ) {
1202                 Indexable *indexable = new_assets->get(i);
1203                 if( indexable->is_asset ) {
1204                         remove_asset_from_caches((Asset*)indexable);
1205                 }
1206                 EDL *new_edl = new EDL;
1207                 new_edl->create_objects();
1208                 new_edl->copy_session(edl);
1209                 new_edls.append(new_edl);
1210
1211
1212                 if( indexable->is_asset ) {
1213 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1214 if( debug ) ((Asset*)indexable)->dump();
1215                         asset_to_edl(new_edl, (Asset*)indexable);
1216                 }
1217                 else
1218                         edl_to_nested(new_edl, (EDL*)indexable);
1219 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1220
1221
1222                 if( labels ) {
1223                         for( RecordLabel *label = labels->first; label; label = label->next ) {
1224                                 new_edl->labels->toggle_label(label->position, label->position);
1225                         }
1226                 }
1227         }
1228 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1229
1230         paste_edls(&new_edls, load_mode, first_track, position,
1231                 edit_labels, edit_plugins, edit_autos, overwrite);
1232 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1233
1234         save_backup();
1235         for( int i = 0; i < new_edls.size(); i++ )
1236                 new_edls.get(i)->Garbage::remove_user();
1237
1238 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1239 }
1240
1241 int MWindow::paste_automation()
1242 {
1243         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1244
1245         if( len ) {
1246                 undo->update_undo_before();
1247                 char *string = new char[len];
1248                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1249                 FileXML file;
1250                 file.read_from_string(string);
1251
1252                 double start = edl->local_session->get_selectionstart();
1253                 double end = edl->local_session->get_selectionend();
1254                 edl->tracks->clear_automation(start, end);
1255                 edl->tracks->paste_automation(start, &file, 0, 1,
1256                         edl->session->typeless_keyframes);
1257                 save_backup();
1258                 undo->update_undo_after(_("paste keyframes"), LOAD_AUTOMATION);
1259                 delete [] string;
1260
1261                 restart_brender();
1262                 update_plugin_guis();
1263                 gui->draw_overlays(1);
1264                 sync_parameters(CHANGE_PARAMS);
1265                 gui->update_patchbay();
1266                 cwindow->update(1, 0, 0);
1267         }
1268
1269         return 0;
1270 }
1271
1272 int MWindow::paste_default_keyframe()
1273 {
1274         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1275
1276         if( len ) {
1277                 undo->update_undo_before();
1278                 char *string = new char[len];
1279                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1280                 FileXML file;
1281                 file.read_from_string(string);
1282                 double start = edl->local_session->get_selectionstart();
1283                 edl->tracks->paste_automation(start, &file, 1, 0,
1284                         edl->session->typeless_keyframes);
1285 //              edl->tracks->paste_default_keyframe(&file);
1286                 undo->update_undo_after(_("paste default keyframe"), LOAD_AUTOMATION);
1287
1288                 restart_brender();
1289                 update_plugin_guis();
1290                 gui->draw_overlays(1);
1291                 sync_parameters(CHANGE_PARAMS);
1292                 gui->update_patchbay();
1293                 cwindow->update(1, 0, 0);
1294                 delete [] string;
1295                 save_backup();
1296         }
1297
1298         return 0;
1299 }
1300
1301
1302 // Insert edls with project deletion and index file generation.
1303 int MWindow::paste_edls(ArrayList<EDL*> *new_edls, int load_mode,
1304         Track *first_track, double current_position,
1305         int edit_labels, int edit_plugins, int edit_autos,
1306         int overwrite)
1307 {
1308
1309         ArrayList<Track*> destination_tracks;
1310         int need_new_tracks = 0;
1311
1312 //PRINT_TRACE
1313         if( !new_edls->total ) return 0;
1314
1315 //PRINT_TRACE
1316 //      double original_length = edl->tracks->total_length();
1317 //      double original_preview_end = edl->local_session->preview_end;
1318 //PRINT_TRACE
1319
1320 // Delete current project
1321         if( load_mode == LOADMODE_REPLACE ||
1322             load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1323                 reset_caches();
1324                 edl->save_defaults(defaults);
1325                 hide_plugins();
1326                 edl->Garbage::remove_user();
1327                 edl = new EDL;
1328                 edl->create_objects();
1329                 edl->copy_session(new_edls->values[0]);
1330                 edl->copy_mixers(new_edls->values[0]);
1331                 gui->mainmenu->update_toggles(0);
1332                 gui->unlock_window();
1333                 gwindow->gui->update_toggles(1);
1334                 gui->lock_window("MWindow::paste_edls");
1335
1336 // Insert labels for certain modes constitutively
1337                 edit_labels = 1;
1338                 edit_plugins = 1;
1339                 edit_autos = 1;
1340 // Force reset of preview
1341 //              original_length = 0;
1342 //              original_preview_end = -1;
1343         }
1344
1345
1346 //PRINT_TRACE
1347
1348 // Create new tracks in master EDL
1349         if( load_mode == LOADMODE_REPLACE ||
1350             load_mode == LOADMODE_REPLACE_CONCATENATE ||
1351             load_mode == LOADMODE_NEW_TRACKS ) {
1352
1353                 need_new_tracks = 1;
1354                 for( int i = 0; i < new_edls->total; i++ ) {
1355                         EDL *new_edl = new_edls->values[i];
1356                         for( Track *current = new_edl->tracks->first;
1357                                 current;
1358                                 current = NEXT ) {
1359                                 if( current->data_type == TRACK_VIDEO ) {
1360                                         edl->tracks->add_video_track(0, 0);
1361                                         if( current->draw ) edl->tracks->last->draw = 1;
1362                                         destination_tracks.append(edl->tracks->last);
1363                                 }
1364                                 else
1365                                 if( current->data_type == TRACK_AUDIO ) {
1366                                         edl->tracks->add_audio_track(0, 0);
1367                                         destination_tracks.append(edl->tracks->last);
1368                                 }
1369                                 else
1370                                 if( current->data_type == TRACK_SUBTITLE ) {
1371                                         edl->tracks->add_subttl_track(0, 0);
1372                                         destination_tracks.append(edl->tracks->last);
1373                                 }
1374                                 edl->session->highlighted_track = edl->tracks->total() - 1;
1375                         }
1376
1377 // Base track count on first EDL only for concatenation
1378                         if( load_mode == LOADMODE_REPLACE_CONCATENATE ) break;
1379                 }
1380
1381         }
1382         else
1383 // Recycle existing tracks of master EDL
1384         if( load_mode == LOADMODE_CONCATENATE ||
1385             load_mode == LOADMODE_PASTE ||
1386             load_mode == LOADMODE_NESTED ) {
1387 //PRINT_TRACE
1388
1389 // The point of this is to shift forward labels after the selection so they can
1390 // then be shifted back to their original locations without recursively
1391 // shifting back every paste.
1392                 if( (load_mode == LOADMODE_PASTE || load_mode == LOADMODE_NESTED) &&
1393                         edl->session->labels_follow_edits )
1394                         edl->labels->clear(edl->local_session->get_selectionstart(),
1395                                                 edl->local_session->get_selectionend(),
1396                                                 1);
1397
1398                 Track *current = first_track ? first_track : edl->tracks->first;
1399                 for( ; current; current = NEXT) {
1400                         if( current->record ) {
1401                                 destination_tracks.append(current);
1402                         }
1403                 }
1404 //PRINT_TRACE
1405
1406         }
1407 //PRINT_TRACE
1408         int destination_track = 0;
1409         double *paste_position = new double[destination_tracks.total];
1410
1411 // Iterate through the edls
1412         for( int i = 0; i < new_edls->total; i++ ) {
1413
1414                 EDL *new_edl = new_edls->values[i];
1415                 double edl_length = new_edl->local_session->clipboard_length ?
1416                         new_edl->local_session->clipboard_length :
1417                         new_edl->tracks->total_length();
1418 // printf("MWindow::paste_edls 2 %f %f\n",
1419 // new_edl->local_session->clipboard_length,
1420 // new_edl->tracks->total_length());
1421 // new_edl->dump();
1422 //PRINT_TRACE
1423
1424 // Convert EDL to master rates
1425                 new_edl->resample(new_edl->session->sample_rate,
1426                         edl->session->sample_rate,
1427                         TRACK_AUDIO);
1428                 new_edl->resample(new_edl->session->frame_rate,
1429                         edl->session->frame_rate,
1430                         TRACK_VIDEO);
1431 //PRINT_TRACE
1432 // Add assets and prepare index files
1433                 for( Asset *new_asset = new_edl->assets->first;
1434                      new_asset;
1435                      new_asset = new_asset->next ) {
1436                         mainindexes->add_next_asset(0, new_asset);
1437                 }
1438 // Capture index file status from mainindex test
1439                 edl->update_assets(new_edl);
1440 //PRINT_TRACE
1441 // Get starting point of insertion.  Need this to paste labels.
1442                 switch( load_mode ) {
1443                 case LOADMODE_REPLACE:
1444                 case LOADMODE_NEW_TRACKS:
1445                         current_position = 0;
1446                         break;
1447
1448                 case LOADMODE_CONCATENATE:
1449                 case LOADMODE_REPLACE_CONCATENATE:
1450                         destination_track = 0;
1451                         if( destination_tracks.total )
1452                                 current_position = destination_tracks.values[0]->get_length();
1453                         else
1454                                 current_position = 0;
1455                         break;
1456
1457                 case LOADMODE_PASTE:
1458                 case LOADMODE_NESTED:
1459                         destination_track = 0;
1460                         if( i == 0 ) {
1461                                 for( int j = 0; j < destination_tracks.total; j++ ) {
1462                                         paste_position[j] = (current_position >= 0) ?
1463                                                 current_position :
1464                                                 edl->local_session->get_selectionstart();
1465                                 }
1466                         }
1467                         break;
1468
1469                 case LOADMODE_RESOURCESONLY:
1470                         edl->add_clip(new_edl);
1471                         break;
1472                 }
1473 //PRINT_TRACE
1474 // Insert edl
1475                 if( load_mode != LOADMODE_RESOURCESONLY &&
1476                     load_mode != LOADMODE_ASSETSONLY ) {
1477 // Insert labels
1478 //printf("MWindow::paste_edls %f %f\n", current_position, edl_length);
1479                         if( load_mode == LOADMODE_PASTE ||
1480                             load_mode == LOADMODE_NESTED )
1481                                 edl->labels->insert_labels(new_edl->labels,
1482                                         destination_tracks.total ? paste_position[0] : 0.0,
1483                                         edl_length,
1484                                         edit_labels);
1485                         else
1486                                 edl->labels->insert_labels(new_edl->labels,
1487                                         current_position,
1488                                         edl_length,
1489                                         edit_labels);
1490 //PRINT_TRACE
1491
1492                         for( Track *new_track = new_edl->tracks->first;
1493                              new_track;
1494                              new_track = new_track->next ) {
1495 // Get destination track of same type as new_track
1496                                 for( int k = 0;
1497                                         k < destination_tracks.total &&
1498                                         destination_tracks.values[destination_track]->data_type != new_track->data_type;
1499                                         k++, destination_track++ ) {
1500                                         if( destination_track >= destination_tracks.total - 1 )
1501                                                 destination_track = 0;
1502                                 }
1503
1504 // Insert data into destination track
1505                                 if( destination_track < destination_tracks.total &&
1506                                     destination_tracks.values[destination_track]->data_type == new_track->data_type ) {
1507                                         Track *track = destination_tracks.values[destination_track];
1508
1509 // Replace default keyframes if first EDL and new tracks were created.
1510 // This means data copied from one track and pasted to another won't retain
1511 // the camera position unless it's a keyframe.  If it did, previous data in the
1512 // track might get unknowingly corrupted.  Ideally we would detect when differing
1513 // default keyframes existed and create discrete keyframes for both.
1514                                         int replace_default = (i == 0) && need_new_tracks;
1515
1516 //printf("MWindow::paste_edls 1 %d\n", replace_default);
1517 // Insert new track at current position
1518                                         switch( load_mode ) {
1519                                         case LOADMODE_REPLACE_CONCATENATE:
1520                                         case LOADMODE_CONCATENATE:
1521                                                 current_position = track->get_length();
1522                                                 break;
1523
1524                                         case LOADMODE_PASTE:
1525                                         case LOADMODE_NESTED:
1526                                                 current_position = paste_position[destination_track];
1527                                                 paste_position[destination_track] += new_track->get_length();
1528                                                 break;
1529                                         }
1530                                         if( overwrite ) {
1531                                                 track->clear(current_position,
1532                                                         current_position + new_track->get_length(),
1533                                                         1, // edit edits
1534                                                         edit_labels, edit_plugins, edit_autos,
1535                                                         1, // convert units
1536                                                         0); // trim edits
1537                                         }
1538 //PRINT_TRACE
1539                                         track->insert_track(new_track, current_position, replace_default,
1540                                                 edit_plugins, edit_autos, edl_length);
1541 //PRINT_TRACE
1542                                 }
1543
1544 // Get next destination track
1545                                 destination_track++;
1546                                 if( destination_track >= destination_tracks.total )
1547                                         destination_track = 0;
1548                         }
1549                 }
1550
1551                 if( load_mode == LOADMODE_PASTE ||
1552                     load_mode == LOADMODE_NESTED )
1553                         current_position += edl_length;
1554         }
1555
1556
1557 // Move loading of clips and vwindow to the end - this fixes some
1558 // strange issue, for index not being shown
1559 // Assume any paste operation from the same EDL won't contain any clips.
1560 // If it did it would duplicate every clip here.
1561         for( int i = 0; i < new_edls->total; i++ ) {
1562                 EDL *new_edl = new_edls->values[i];
1563
1564                 for( int j = 0; j < new_edl->clips.total; j++ ) {
1565                         edl->add_clip(new_edl->clips.values[j]);
1566                 }
1567
1568                 if( new_edl->total_vwindow_edls() ) {
1569 //                      if( edl->vwindow_edl )
1570 //                              edl->vwindow_edl->Garbage::remove_user();
1571 //                      edl->vwindow_edl = new EDL(edl);
1572 //                      edl->vwindow_edl->create_objects();
1573 //                      edl->vwindow_edl->copy_all(new_edl->vwindow_edl);
1574
1575                         for( int j = 0; j < new_edl->total_vwindow_edls(); j++ ) {
1576                                 EDL *vwindow_edl = new EDL(edl);
1577                                 vwindow_edl->create_objects();
1578                                 vwindow_edl->copy_all(new_edl->get_vwindow_edl(j));
1579                                 edl->append_vwindow_edl(vwindow_edl, 0);
1580                         }
1581                 }
1582         }
1583
1584         if( paste_position ) delete [] paste_position;
1585
1586 // This is already done in load_filenames and everything else that uses paste_edls
1587 //      update_project(load_mode);
1588
1589 // Fix preview range
1590 //      if( EQUIV(original_length, original_preview_end) )
1591 //      {
1592 //              edl->local_session->preview_end = edl->tracks->total_length();
1593 //      }
1594
1595 // Start examining next batch of index files
1596         mainindexes->start_build();
1597
1598 // Don't save a backup after loading since the loaded file is on disk already.
1599 //PRINT_TRACE
1600         return 0;
1601 }
1602
1603 void MWindow::paste_silence()
1604 {
1605         double start = edl->local_session->get_selectionstart();
1606         double end = edl->local_session->get_selectionend();
1607         undo->update_undo_before();
1608         edl->paste_silence(start, end,
1609                 edl->session->labels_follow_edits,
1610                 edl->session->plugins_follow_edits,
1611                 edl->session->autos_follow_edits);
1612         edl->optimize();
1613         save_backup();
1614         undo->update_undo_after(_("silence"), LOAD_EDITS | LOAD_TIMEBAR);
1615
1616         update_plugin_guis();
1617         restart_brender();
1618         gui->update(1, 2, 1, 1, 1, 1, 0);
1619         cwindow->update(1, 0, 0, 0, 1);
1620         cwindow->refresh_frame(CHANGE_EDL);
1621 }
1622
1623 void MWindow::detach_transition(Transition *transition)
1624 {
1625         undo->update_undo_before();
1626         hide_plugin(transition, 1);
1627         int is_video = (transition->edit->track->data_type == TRACK_VIDEO);
1628         transition->edit->detach_transition();
1629         save_backup();
1630         undo->update_undo_after(_("detach transition"), LOAD_ALL);
1631
1632         if( is_video ) restart_brender();
1633         gui->update(0, 1, 0, 0, 0, 0, 0);
1634         sync_parameters(CHANGE_EDL);
1635 }
1636
1637 void MWindow::detach_transitions()
1638 {
1639         gui->lock_window("MWindow::detach_transitions 1");
1640
1641         undo->update_undo_before();
1642         double start = edl->local_session->get_selectionstart();
1643         double end = edl->local_session->get_selectionend();
1644         edl->tracks->clear_transitions(start, end);
1645
1646         save_backup();
1647         undo->update_undo_after(_("detach transitions"), LOAD_EDITS);
1648
1649         sync_parameters(CHANGE_EDL);
1650         gui->update(0, 1, 0, 0, 0, 0, 0);
1651         gui->unlock_window();
1652 }
1653
1654 void MWindow::paste_transition()
1655 {
1656 // Only the first transition gets dropped.
1657         PluginServer *server = session->drag_pluginservers->values[0];
1658
1659         undo->update_undo_before();
1660         if( server->audio )
1661                 strcpy(edl->session->default_atransition, server->title);
1662         else
1663                 strcpy(edl->session->default_vtransition, server->title);
1664
1665         edl->tracks->paste_transition(server, session->edit_highlighted);
1666         save_backup();
1667         undo->update_undo_after(_("transition"), LOAD_EDITS);
1668
1669         if( server->video ) restart_brender();
1670         sync_parameters(CHANGE_ALL);
1671 }
1672
1673 void MWindow::paste_transitions(int track_type, char *title)
1674 {
1675         gui->lock_window("MWindow::detach_transitions 1");
1676
1677         undo->update_undo_before();
1678         double start = edl->local_session->get_selectionstart();
1679         double end = edl->local_session->get_selectionend();
1680         edl->tracks->paste_transitions(start, end, track_type, title);
1681
1682         save_backup();
1683         undo->update_undo_after(_("attach transitions"), LOAD_EDITS);
1684
1685         sync_parameters(CHANGE_EDL);
1686         gui->update(0, 1, 0, 0, 0, 0, 0);
1687         gui->unlock_window();
1688 }
1689
1690 void MWindow::paste_transition_cwindow(Track *dest_track)
1691 {
1692         PluginServer *server = session->drag_pluginservers->values[0];
1693         undo->update_undo_before();
1694         edl->tracks->paste_video_transition(server, 1);
1695         save_backup();
1696         undo->update_undo_after(_("transition"), LOAD_EDITS);
1697         restart_brender();
1698         gui->update(0, 1, 0, 0, 0, 0, 0);
1699         sync_parameters(CHANGE_ALL);
1700 }
1701
1702 void MWindow::paste_audio_transition()
1703 {
1704         PluginServer *server = scan_plugindb(edl->session->default_atransition,
1705                 TRACK_AUDIO);
1706         if( !server ) {
1707                 char string[BCTEXTLEN];
1708                 sprintf(string, _("No default transition %s found."), edl->session->default_atransition);
1709                 gui->show_message(string);
1710                 return;
1711         }
1712
1713         undo->update_undo_before();
1714         edl->tracks->paste_audio_transition(server);
1715         save_backup();
1716         undo->update_undo_after(_("transition"), LOAD_EDITS);
1717
1718         sync_parameters(CHANGE_EDL);
1719         gui->update(0, 1, 0, 0, 0, 0, 0);
1720 }
1721
1722 void MWindow::paste_video_transition()
1723 {
1724         PluginServer *server = scan_plugindb(edl->session->default_vtransition,
1725                 TRACK_VIDEO);
1726         if( !server ) {
1727                 char string[BCTEXTLEN];
1728                 sprintf(string, _("No default transition %s found."), edl->session->default_vtransition);
1729                 gui->show_message(string);
1730                 return;
1731         }
1732
1733         undo->update_undo_before();
1734
1735         edl->tracks->paste_video_transition(server);
1736         save_backup();
1737         undo->update_undo_after(_("transition"), LOAD_EDITS);
1738
1739         sync_parameters(CHANGE_EDL);
1740         restart_brender();
1741         gui->update(0, 1, 0, 0, 0, 0, 0);
1742 }
1743
1744 void MWindow::shuffle_edits()
1745 {
1746         gui->lock_window("MWindow::shuffle_edits 1");
1747
1748         undo->update_undo_before();
1749         double start = edl->local_session->get_selectionstart();
1750         double end = edl->local_session->get_selectionend();
1751
1752         edl->tracks->shuffle_edits(start, end);
1753
1754         save_backup();
1755         undo->update_undo_after(_("shuffle edits"), LOAD_EDITS | LOAD_TIMEBAR);
1756
1757         sync_parameters(CHANGE_EDL);
1758         restart_brender();
1759         gui->update(0, 1, 1, 0, 0, 0, 0);
1760         gui->unlock_window();
1761 }
1762
1763 void MWindow::reverse_edits()
1764 {
1765         gui->lock_window("MWindow::reverse_edits 1");
1766
1767         undo->update_undo_before();
1768         double start = edl->local_session->get_selectionstart();
1769         double end = edl->local_session->get_selectionend();
1770
1771         edl->tracks->reverse_edits(start, end);
1772
1773         save_backup();
1774         undo->update_undo_after(_("reverse edits"), LOAD_EDITS | LOAD_TIMEBAR);
1775
1776         sync_parameters(CHANGE_EDL);
1777         restart_brender();
1778         gui->update(0, 1, 1, 0, 0, 0, 0);
1779         gui->unlock_window();
1780 }
1781
1782 void MWindow::align_edits()
1783 {
1784         gui->lock_window("MWindow::align_edits 1");
1785
1786         undo->update_undo_before();
1787         double start = edl->local_session->get_selectionstart();
1788         double end = edl->local_session->get_selectionend();
1789
1790         edl->tracks->align_edits(start, end);
1791
1792         save_backup();
1793         undo->update_undo_after(_("align edits"), LOAD_EDITS | LOAD_TIMEBAR);
1794
1795         sync_parameters(CHANGE_EDL);
1796         restart_brender();
1797         gui->update(0, 1, 1, 0, 0, 0, 0);
1798         gui->unlock_window();
1799 }
1800
1801 void MWindow::set_edit_length(double length)
1802 {
1803         gui->lock_window("MWindow::detach_transitions 1");
1804
1805         undo->update_undo_before();
1806         double start = edl->local_session->get_selectionstart();
1807         double end = edl->local_session->get_selectionend();
1808
1809         edl->tracks->set_edit_length(start, end, length);
1810
1811         save_backup();
1812         undo->update_undo_after(_("edit length"), LOAD_EDITS | LOAD_TIMEBAR);
1813
1814         sync_parameters(CHANGE_EDL);
1815         restart_brender();
1816         gui->update(0, 1, 1, 0, 0, 0, 0);
1817         gui->unlock_window();
1818 }
1819
1820
1821 void MWindow::set_transition_length(Transition *transition, double length)
1822 {
1823         gui->lock_window("MWindow::detach_transitions 1");
1824
1825         undo->update_undo_before();
1826         //double start = edl->local_session->get_selectionstart();
1827         //double end = edl->local_session->get_selectionend();
1828
1829         edl->tracks->set_transition_length(transition, length);
1830
1831         save_backup();
1832         undo->update_undo_after(_("transition length"), LOAD_EDITS);
1833
1834         edl->session->default_transition_length = length;
1835         sync_parameters(CHANGE_PARAMS);
1836         gui->update(0, 1, 0, 0, 0, 0, 0);
1837         gui->unlock_window();
1838 }
1839
1840 void MWindow::set_transition_length(double length)
1841 {
1842         gui->lock_window("MWindow::detach_transitions 1");
1843
1844         undo->update_undo_before();
1845         double start = edl->local_session->get_selectionstart();
1846         double end = edl->local_session->get_selectionend();
1847
1848         edl->tracks->set_transition_length(start, end, length);
1849
1850         save_backup();
1851         undo->update_undo_after(_("transition length"), LOAD_EDITS);
1852
1853         edl->session->default_transition_length = length;
1854         sync_parameters(CHANGE_PARAMS);
1855         restart_brender();
1856         gui->update(0, 1, 0, 0, 0, 0, 0);
1857         gui->unlock_window();
1858 }
1859
1860
1861 void MWindow::redo_entry(BC_WindowBase *calling_window_gui)
1862 {
1863         calling_window_gui->unlock_window();
1864         stop_playback(0);
1865         close_mixers();
1866
1867         cwindow->gui->lock_window("MWindow::redo_entry 1");
1868         for( int i = 0; i < vwindows.size(); i++ ) {
1869                 if( vwindows.get(i)->is_running() ) {
1870                         if( calling_window_gui != vwindows.get(i)->gui ) {
1871                                 vwindows.get(i)->gui->lock_window("MWindow::redo_entry 2");
1872                         }
1873                 }
1874         }
1875         gui->lock_window("MWindow::redo_entry 3");
1876
1877         undo->redo();
1878
1879         save_backup();
1880         restart_brender();
1881         update_plugin_states();
1882         update_plugin_guis();
1883
1884         gui->update(1, 2, 1, 1, 1, 1, 1);
1885         gui->unlock_window();
1886         cwindow->update(1, 1, 1, 1, 1);
1887         cwindow->gui->unlock_window();
1888
1889         for( int i = 0; i < vwindows.size(); i++ ) {
1890                 if( vwindows.get(i)->is_running() ) {
1891                         if( calling_window_gui != vwindows.get(i)->gui ) {
1892                                 vwindows.get(i)->gui->unlock_window();
1893                         }
1894                 }
1895         }
1896
1897         if( calling_window_gui != gui )
1898                 gui->unlock_window();
1899
1900         open_mixers();
1901         awindow->gui->async_update_assets();
1902
1903         cwindow->refresh_frame(CHANGE_ALL);
1904         calling_window_gui->lock_window("MWindow::redo_entry 4");
1905 }
1906
1907
1908 void MWindow::resize_track(Track *track, int w, int h)
1909 {
1910         undo->update_undo_before();
1911 // We have to move all maskpoints so they do not move in relation to image areas
1912         ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->translate_masks(
1913                 (w - track->track_w) / 2,
1914                 (h - track->track_h) / 2);
1915         track->track_w = w;
1916         track->track_h = h;
1917         undo->update_undo_after(_("resize track"), LOAD_ALL);
1918         save_backup();
1919
1920         restart_brender();
1921         sync_parameters(CHANGE_EDL);
1922 }
1923
1924
1925 void MWindow::set_inpoint(int is_mwindow)
1926 {
1927         undo->update_undo_before();
1928         edl->set_inpoint(edl->local_session->get_selectionstart(1));
1929         save_backup();
1930         undo->update_undo_after(_("in point"), LOAD_TIMEBAR);
1931
1932         if( !is_mwindow ) {
1933                 gui->lock_window("MWindow::set_inpoint 1");
1934         }
1935         gui->update_timebar(1);
1936         if( !is_mwindow ) {
1937                 gui->unlock_window();
1938         }
1939
1940         if( is_mwindow ) {
1941                 cwindow->gui->lock_window("MWindow::set_inpoint 2");
1942         }
1943         cwindow->gui->timebar->update(1);
1944         if( is_mwindow ) {
1945                 cwindow->gui->unlock_window();
1946         }
1947 }
1948
1949 void MWindow::set_outpoint(int is_mwindow)
1950 {
1951         undo->update_undo_before();
1952         edl->set_outpoint(edl->local_session->get_selectionend(1));
1953         save_backup();
1954         undo->update_undo_after(_("out point"), LOAD_TIMEBAR);
1955
1956         if( !is_mwindow ) {
1957                 gui->lock_window("MWindow::set_outpoint 1");
1958         }
1959         gui->update_timebar(1);
1960         if( !is_mwindow ) {
1961                 gui->unlock_window();
1962         }
1963
1964         if( is_mwindow ) {
1965                 cwindow->gui->lock_window("MWindow::set_outpoint 2");
1966         }
1967         cwindow->gui->timebar->update(1);
1968         if( is_mwindow ) {
1969                 cwindow->gui->unlock_window();
1970         }
1971 }
1972
1973 void MWindow::unset_inoutpoint(int is_mwindow)
1974 {
1975         undo->update_undo_before();
1976         edl->unset_inoutpoint();
1977         save_backup();
1978         undo->update_undo_after(_("clear in/out"), LOAD_TIMEBAR);
1979
1980         if( !is_mwindow ) {
1981                 gui->lock_window("MWindow::unset_inoutpoint 1");
1982         }
1983         gui->update_timebar(1);
1984         if( !is_mwindow ) {
1985                 gui->unlock_window();
1986         }
1987
1988         if( is_mwindow ) {
1989                 cwindow->gui->lock_window("MWindow::unset_inoutpoint 2");
1990         }
1991         cwindow->gui->timebar->update(1);
1992         if( is_mwindow ) {
1993                 cwindow->gui->unlock_window();
1994         }
1995 }
1996
1997 void MWindow::splice(EDL *source)
1998 {
1999         FileXML file;
2000
2001         undo->update_undo_before();
2002         double source_start = source->local_session->get_selectionstart();
2003         double source_end = source->local_session->get_selectionend();
2004         source->copy(source_start, source_end, 1, 0, 0, &file, "", 1);
2005 //file.dump();
2006         double start = edl->local_session->get_selectionstart();
2007         //double end = edl->local_session->get_selectionend();
2008
2009         paste(start, start, &file,
2010                 edl->session->labels_follow_edits,
2011                 edl->session->plugins_follow_edits,
2012                 edl->session->autos_follow_edits);
2013
2014 // Position at end of clip
2015         edl->local_session->set_selectionstart(start + source_end - source_start);
2016         edl->local_session->set_selectionend(start + source_end - source_start);
2017
2018         save_backup();
2019         undo->update_undo_after(_("splice"), LOAD_EDITS | LOAD_TIMEBAR);
2020         update_plugin_guis();
2021         restart_brender();
2022         gui->update(1, 1, 1, 1, 0, 1, 0);
2023         sync_parameters(CHANGE_EDL);
2024 }
2025
2026 void MWindow::save_clip(EDL *new_edl, const char *txt)
2027 {
2028         new_edl->local_session->set_selectionstart(0);
2029         new_edl->local_session->set_selectionend(0);
2030         sprintf(new_edl->local_session->clip_title, _("Clip %d"),
2031                 session->clip_number++);
2032         char duration[BCTEXTLEN];
2033         Units::totext(duration, new_edl->tracks->total_length(),
2034                 new_edl->session->time_format,
2035                 new_edl->session->sample_rate,
2036                 new_edl->session->frame_rate,
2037                 new_edl->session->frames_per_foot);
2038
2039         Track *track = new_edl->tracks->first;
2040         const char *path = edl->path;
2041         for( ; (!path || !*path) && track; track=track->next ) {
2042                 if( !track->record ) continue;
2043                 Edit *edit = track->edits->first;
2044                 if( !edit ) continue;
2045                 Indexable *indexable = edit->get_source();
2046                 if( !indexable ) continue;
2047                 path = indexable->path;
2048         }
2049
2050         time_t now;  time(&now);
2051         struct tm *tm = localtime(&now);
2052         char *cp = new_edl->local_session->clip_notes;
2053         int n, sz = sizeof(new_edl->local_session->clip_notes)-1;
2054         if( txt && *txt ) {
2055                 n = snprintf(cp, sz, "%s", txt);
2056                 cp += n;  sz -= n;
2057         }
2058         n = snprintf(cp, sz, 
2059                 "%02d/%02d/%02d %02d:%02d:%02d,  +%s\n",
2060                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, 
2061                 tm->tm_hour, tm->tm_min, tm->tm_sec, duration);
2062         cp += n;  sz -= n;
2063         if( path && *path ) {
2064                 FileSystem fs;
2065                 char title[BCTEXTLEN];
2066                 fs.extract_name(title, path);
2067                 n = snprintf(cp, sz, "%s", title);
2068                 cp += n;  sz -= n;
2069         }
2070         cp[n] = 0;
2071
2072         edl->update_assets(new_edl);
2073         int cur_x, cur_y;
2074         gui->get_abs_cursor(cur_x, cur_y, 0);
2075         gui->unlock_window();
2076
2077         awindow->clip_edit->create_clip(new_edl, cur_x, cur_y);
2078
2079         gui->lock_window("MWindow::save_clip");
2080         save_backup();
2081 }
2082
2083 void MWindow::to_clip(EDL *edl, const char *txt)
2084 {
2085         FileXML file;
2086         double start, end;
2087
2088         gui->lock_window("MWindow::to_clip 1");
2089         start = edl->local_session->get_selectionstart();
2090         end = edl->local_session->get_selectionend();
2091
2092         if( EQUIV(end, start) ) {
2093                 start = 0;
2094                 end = edl->tracks->total_length();
2095         }
2096
2097 // Don't copy all since we don't want the clips twice.
2098         edl->copy(start, end, 0, 0, 0, &file, "", 1);
2099
2100         EDL *new_edl = new EDL(edl);
2101         new_edl->create_objects();
2102         new_edl->load_xml(&file, LOAD_ALL);
2103         save_clip(new_edl, txt);
2104         gui->unlock_window();
2105 }
2106
2107 int MWindow::toggle_label(int is_mwindow)
2108 {
2109         double position1, position2;
2110         undo->update_undo_before();
2111
2112         if( cwindow->playback_engine->is_playing_back ) {
2113                 position1 = position2 =
2114                         cwindow->playback_engine->get_tracking_position();
2115         }
2116         else {
2117                 position1 = edl->local_session->get_selectionstart(1);
2118                 position2 = edl->local_session->get_selectionend(1);
2119         }
2120
2121         position1 = edl->align_to_frame(position1, 0);
2122         position2 = edl->align_to_frame(position2, 0);
2123
2124 //printf("MWindow::toggle_label 1\n");
2125
2126         edl->labels->toggle_label(position1, position2);
2127         save_backup();
2128
2129         if( !is_mwindow ) {
2130                 gui->lock_window("MWindow::toggle_label 1");
2131         }
2132         gui->update_timebar(0);
2133         gui->activate_timeline();
2134         gui->flush();
2135         if( !is_mwindow ) {
2136                 gui->unlock_window();
2137         }
2138
2139         if( is_mwindow ) {
2140                 cwindow->gui->lock_window("MWindow::toggle_label 2");
2141         }
2142         cwindow->gui->timebar->update(1);
2143         if( is_mwindow ) {
2144                 cwindow->gui->unlock_window();
2145         }
2146
2147         awindow->gui->async_update_assets();
2148
2149         undo->update_undo_after(_("label"), LOAD_TIMEBAR);
2150         return 0;
2151 }
2152
2153 void MWindow::trim_selection()
2154 {
2155         undo->update_undo_before();
2156
2157
2158         edl->trim_selection(edl->local_session->get_selectionstart(),
2159                 edl->local_session->get_selectionend(),
2160                 edl->session->labels_follow_edits,
2161                 edl->session->plugins_follow_edits,
2162                 edl->session->autos_follow_edits);
2163
2164         save_backup();
2165         undo->update_undo_after(_("trim selection"), LOAD_EDITS | LOAD_TIMEBAR);
2166         update_plugin_guis();
2167         gui->update(1, 2, 1, 1, 1, 1, 0);
2168         cwindow->update(1, 0, 0, 0, 1);
2169         awindow->gui->async_update_assets();
2170         restart_brender();
2171         cwindow->refresh_frame(CHANGE_EDL);
2172 }
2173
2174
2175 void MWindow::undo_entry(BC_WindowBase *calling_window_gui)
2176 {
2177         calling_window_gui->unlock_window();
2178         stop_playback(0);
2179         close_mixers();
2180
2181         cwindow->gui->lock_window("MWindow::undo_entry 1");
2182         for( int i = 0; i < vwindows.size(); i++ ) {
2183                 if( vwindows.get(i)->is_running() ) {
2184                         if( calling_window_gui != vwindows.get(i)->gui ) {
2185                                 vwindows.get(i)->gui->lock_window("MWindow::undo_entry 4");
2186                         }
2187                 }
2188         }
2189         gui->lock_window("MWindow::undo_entry 2");
2190
2191         undo->undo();
2192
2193         save_backup();
2194         restart_brender();
2195         update_plugin_states();
2196         update_plugin_guis();
2197
2198         gui->update(1, 2, 1, 1, 1, 1, 1);
2199         gui->unlock_window();
2200         cwindow->update(1, 1, 1, 1, 1);
2201         cwindow->gui->unlock_window();
2202
2203         for( int i = 0; i < vwindows.size(); i++ ) {
2204                 if( vwindows.get(i)->is_running() ) {
2205                         if( calling_window_gui != vwindows.get(i)->gui ) {
2206                                 vwindows.get(i)->gui->unlock_window();
2207                         }
2208                 }
2209         }
2210
2211         if( calling_window_gui != gui )
2212                 gui->unlock_window();
2213
2214         awindow->gui->async_update_assets();
2215
2216         open_mixers();
2217         cwindow->refresh_frame(CHANGE_ALL);
2218         calling_window_gui->lock_window("MWindow::undo_entry 4");
2219 }
2220
2221
2222 void MWindow::new_folder(const char *new_folder)
2223 {
2224         undo->update_undo_before();
2225         edl->new_folder(new_folder);
2226         undo->update_undo_after(_("new folder"), LOAD_ALL);
2227         awindow->gui->async_update_assets();
2228 }
2229
2230 void MWindow::delete_folder(char *folder)
2231 {
2232 //      undo->update_undo_after(_("delete folder"), LOAD_ALL);
2233 }
2234
2235 void MWindow::select_point(double position)
2236 {
2237         edl->local_session->set_selectionstart(position);
2238         edl->local_session->set_selectionend(position);
2239
2240 // Que the CWindow
2241         cwindow->update(1, 0, 0, 0, 1);
2242
2243         update_plugin_guis();
2244         gui->update_patchbay();
2245         gui->hide_cursor(0);
2246         gui->draw_cursor(0);
2247         gui->mainclock->update(edl->local_session->get_selectionstart(1));
2248         gui->zoombar->update();
2249         gui->update_timebar(0);
2250         gui->flash_canvas(0);
2251         gui->flush();
2252 }
2253
2254
2255
2256
2257 void MWindow::map_audio(int pattern)
2258 {
2259         undo->update_undo_before();
2260         remap_audio(pattern);
2261         undo->update_undo_after(
2262                 pattern == MWindow::AUDIO_1_TO_1 ? _("map 1:1") : _("map 5.1:2"),
2263                 LOAD_AUTOMATION);
2264         sync_parameters(CHANGE_PARAMS);
2265         gui->update(0, 1, 0, 0, 1, 0, 0);
2266 }
2267
2268 void MWindow::remap_audio(int pattern)
2269 {
2270         int current_channel = 0;
2271         int current_track = 0;
2272         for( Track *current = edl->tracks->first; current; current = NEXT ) {
2273                 if( current->data_type == TRACK_AUDIO &&
2274                         current->record ) {
2275                         Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
2276                         PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
2277
2278                         for( int i = 0; i < MAXCHANNELS; i++ ) {
2279                                 pan_auto->values[i] = 0.0;
2280                         }
2281
2282                         if( pattern == MWindow::AUDIO_1_TO_1 ) {
2283                                 pan_auto->values[current_channel] = 1.0;
2284                         }
2285                         else
2286                         if( pattern == MWindow::AUDIO_5_1_TO_2 ) {
2287                                 switch( current_track ) {
2288                                 case 0:
2289                                         pan_auto->values[0] = 0.5;
2290                                         pan_auto->values[1] = 0.5;
2291                                         break;
2292                                 case 1:
2293                                         pan_auto->values[0] = 1;
2294                                         break;
2295                                 case 2:
2296                                         pan_auto->values[1] = 1;
2297                                         break;
2298                                 case 3:
2299                                         pan_auto->values[0] = 1;
2300                                         break;
2301                                 case 4:
2302                                         pan_auto->values[1] = 1;
2303                                         break;
2304                                 case 5:
2305                                         pan_auto->values[0] = 0.5;
2306                                         pan_auto->values[1] = 0.5;
2307                                         break;
2308                                 }
2309                         }
2310
2311                         BC_Pan::calculate_stick_position(edl->session->audio_channels,
2312                                 edl->session->achannel_positions, pan_auto->values,
2313                                 MAX_PAN, PAN_RADIUS, pan_auto->handle_x, pan_auto->handle_y);
2314
2315                         current_channel++;
2316                         current_track++;
2317                         if( current_channel >= edl->session->audio_channels )
2318                                 current_channel = 0;
2319                 }
2320         }
2321 }
2322
2323 void MWindow::set_proxy(int use_scaler, int new_scale, int auto_scale,
2324                 ArrayList<Indexable*> *orig_assets,
2325                 ArrayList<Indexable*> *proxy_assets)
2326 {
2327         int orig_use_scaler = edl->session->proxy_use_scaler;
2328         int orig_scale = edl->session->proxy_scale;
2329 // rescale to full size asset in read_frame
2330         edl->session->proxy_use_scaler = use_scaler;
2331         edl->session->proxy_scale = new_scale;
2332         edl->session->proxy_auto_scale = auto_scale;
2333
2334         if( use_scaler ) {
2335                 for( int i=0; i<proxy_assets->size(); ++i ) {
2336                         Asset *proxy_asset = (Asset *)proxy_assets->get(i);
2337                         proxy_asset->width = orig_assets->get(i)->get_w();
2338                         proxy_asset->height = orig_assets->get(i)->get_h();
2339                 }
2340                 new_scale = 1;
2341         }
2342
2343         if( !orig_use_scaler && new_scale != orig_scale ) {
2344 // project size
2345                 float orig_w = (float)edl->session->output_w * orig_scale;
2346                 float orig_h = (float)edl->session->output_h * orig_scale;
2347                 edl->session->output_w = Units::round(orig_w / new_scale);
2348                 edl->session->output_h = Units::round(orig_h / new_scale);
2349
2350 // track sizes
2351                 for( Track *track=edl->tracks->first; track; track=track->next ) {
2352                         if( track->data_type != TRACK_VIDEO ) continue;
2353                         orig_w = (float)track->track_w * orig_scale;
2354                         orig_h = (float)track->track_h * orig_scale;
2355                         track->track_w = Units::round(orig_w / new_scale);
2356                         track->track_h = Units::round(orig_h / new_scale);
2357                         ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->
2358                                 set_proxy(orig_scale, new_scale);
2359                         ((FloatAutos*)track->automation->autos[AUTOMATION_CAMERA_X])->
2360                                 set_proxy(orig_scale, new_scale);
2361                         ((FloatAutos*)track->automation->autos[AUTOMATION_CAMERA_Y])->
2362                                 set_proxy(orig_scale, new_scale);
2363                         ((FloatAutos*)track->automation->autos[AUTOMATION_PROJECTOR_X])->
2364                                 set_proxy(orig_scale, new_scale);
2365                         ((FloatAutos*)track->automation->autos[AUTOMATION_PROJECTOR_Y])->
2366                                 set_proxy(orig_scale, new_scale);
2367                 }
2368         }
2369
2370 // change original assets to proxy assets
2371         int awindow_folder = use_scaler || new_scale != 1 ? AW_PROXY_FOLDER : AW_MEDIA_FOLDER;
2372         for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
2373                 Asset *proxy_asset = edl->assets->update((Asset *)proxy_assets->get(i));
2374                 proxy_asset->awindow_folder = awindow_folder;
2375 // replace track contents
2376                 for( Track *track = edl->tracks->first; track; track = track->next ) {
2377                         if( track->data_type != TRACK_VIDEO ) continue;
2378                         for( Edit *edit = track->edits->first; edit; edit = edit->next ) {
2379                                 if( !edit->asset ) continue;
2380                                 if( !strcmp(edit->asset->path, orig_assets->get(i)->path) ) {
2381                                         edit->asset = proxy_asset;
2382                                 }
2383                         }
2384                 }
2385         }
2386 }
2387
2388 void MWindow::add_proxy(int use_scaler,
2389                 ArrayList<Indexable*> *orig_assets,
2390                 ArrayList<Indexable*> *proxy_assets)
2391 {
2392         if( use_scaler ) {
2393                 for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
2394                         Asset *proxy_asset = (Asset *)proxy_assets->get(i);
2395                         proxy_asset->width = orig_assets->get(i)->get_w();
2396                         proxy_asset->height = orig_assets->get(i)->get_h();
2397                 }
2398         }
2399
2400 // change original assets to proxy assets
2401         for( int i=0,n=proxy_assets->size(); i<n; ++i ) {
2402                 Asset *proxy_asset = edl->assets->update((Asset *)proxy_assets->get(i));
2403                 proxy_asset->awindow_folder = AW_PROXY_FOLDER;
2404 // replace track contents
2405                 for( Track *track = edl->tracks->first; track; track = track->next ) {
2406                         if( track->data_type != TRACK_VIDEO ) continue;
2407                         for( Edit *edit = track->edits->first; edit; edit = edit->next ) {
2408                                 if( !edit->asset ) continue;
2409                                 if( !strcmp(edit->asset->path, orig_assets->get(i)->path) ) {
2410                                         edit->asset = proxy_asset;
2411                                 }
2412                         }
2413                 }
2414         }
2415 }
2416
2417 void MWindow::cut_commercials()
2418 {
2419         undo->update_undo_before();
2420         commercials->scan_media();
2421         edl->optimize();
2422         save_backup();
2423         undo->update_undo_after(_("cut ads"), LOAD_EDITS | LOAD_TIMEBAR);
2424
2425         restart_brender();
2426         update_plugin_guis();
2427         gui->update(1, 2, 1, 1, 1, 1, 0);
2428         cwindow->update(1, 0, 0, 0, 1);
2429         cwindow->refresh_frame(CHANGE_EDL);
2430 }
2431
2432