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