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