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