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