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