version update, msg txt, tweak mixeralign locks/leaks, disable libwebp,libaom in...
[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_track()
650 {
651         if( edl->tracks->last )
652                 delete_track(edl->tracks->last);
653 }
654
655 void MWindow::delete_tracks()
656 {
657         undo_before();
658         edl->tracks->delete_tracks();
659         undo_after(_("delete tracks"), LOAD_ALL);
660         save_backup();
661
662         restart_brender();
663         update_plugin_states();
664
665         gui->update(1, NORMAL_DRAW, 1, 0, 1, 0, 0);
666         cwindow->refresh_frame(CHANGE_EDL);
667 }
668
669 void MWindow::delete_track(Track *track)
670 {
671         undo_before();
672         edl->tracks->delete_track(track);
673         undo_after(_("delete track"), LOAD_ALL);
674
675         restart_brender();
676         update_plugin_states();
677
678         gui->update(1, NORMAL_DRAW, 1, 0, 1, 0, 0);
679         cwindow->refresh_frame(CHANGE_EDL);
680         save_backup();
681 }
682
683
684 // Insert data from clipboard
685 void MWindow::insert(double position, FileXML *file,
686         int edit_labels, int edit_plugins, int edit_autos,
687         EDL *parent_edl, Track *first_track, int overwrite)
688 {
689 // For clipboard pasting make the new edl use a separate session
690 // from the master EDL.  Then it can be resampled to the master rates.
691 // For splice, overwrite, and dragging need same session to get the assets.
692         EDL *edl = new EDL(parent_edl);
693         ArrayList<EDL*> new_edls;
694         uint32_t load_flags = LOAD_ALL;
695
696
697         new_edls.append(edl);
698         edl->create_objects();
699
700
701
702
703         if( parent_edl ) load_flags &= ~LOAD_SESSION;
704         if( !edl->session->autos_follow_edits ) load_flags &= ~LOAD_AUTOMATION;
705         if( !edl->session->labels_follow_edits ) load_flags &= ~LOAD_TIMEBAR;
706
707         edl->load_xml(file, load_flags);
708
709
710 //printf("MWindow::insert %f\n", edl->local_session->clipboard_length);
711
712
713
714         paste_edls(&new_edls, LOADMODE_PASTE, first_track, position,
715                 edit_labels, edit_plugins, edit_autos, overwrite);
716 // if( vwindow->edl )
717 // printf("MWindow::insert 5 %f %f\n",
718 // vwindow->edl->local_session->in_point,
719 // vwindow->edl->local_session->out_point);
720         new_edls.remove_all();
721         edl->Garbage::remove_user();
722 //printf("MWindow::insert 6 %p\n", vwindow->get_edl());
723 }
724
725 void MWindow::insert_effects_canvas(double start,
726         double length)
727 {
728         Track *dest_track = session->track_highlighted;
729         if( !dest_track ) return;
730
731         undo_before();
732
733         for( int i=0; i<session->drag_pluginservers->total; ++i ) {
734                 PluginServer *plugin = session->drag_pluginservers->values[i];
735                 insert_effect(plugin->title, 0, dest_track,
736                         i == 0 ? session->pluginset_highlighted : 0,
737                         start, length, PLUGIN_STANDALONE);
738         }
739
740         save_backup();
741         undo_after(_("insert effect"), LOAD_EDITS | LOAD_PATCHES);
742         restart_brender();
743         sync_parameters(CHANGE_EDL);
744 // GUI updated in TrackCanvas, after current_operations are reset
745 }
746
747 void MWindow::insert_effects_cwindow(Track *dest_track)
748 {
749         if( !dest_track ) return;
750
751         undo_before();
752
753         double start = 0;
754         double length = dest_track->get_length();
755
756         if( edl->local_session->get_selectionend() >
757                 edl->local_session->get_selectionstart() ) {
758                 start = edl->local_session->get_selectionstart();
759                 length = edl->local_session->get_selectionend() -
760                         edl->local_session->get_selectionstart();
761         }
762
763         for( int i=0; i<session->drag_pluginservers->total; ++i ) {
764                 PluginServer *plugin = session->drag_pluginservers->values[i];
765                 insert_effect(plugin->title, 0, dest_track, 0,
766                         start, length, PLUGIN_STANDALONE);
767         }
768
769         save_backup();
770         undo_after(_("insert effect"), LOAD_EDITS | LOAD_PATCHES);
771         restart_brender();
772         sync_parameters(CHANGE_EDL);
773         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
774 }
775
776 void MWindow::insert_effect(char *title,
777         SharedLocation *shared_location,
778         int data_type,
779         int plugin_type,
780         int single_standalone)
781 {
782         Track *current = edl->tracks->first;
783         SharedLocation shared_location_local;
784         shared_location_local.copy_from(shared_location);
785         int first_track = 1;
786         for( ; current; current=NEXT ) {
787                 if( current->data_type == data_type &&
788                         current->record ) {
789                         insert_effect(title, &shared_location_local,
790                                 current, 0, 0, 0, plugin_type);
791
792                         if( first_track ) {
793                                 if( plugin_type == PLUGIN_STANDALONE && single_standalone ) {
794                                         plugin_type = PLUGIN_SHAREDPLUGIN;
795                                         shared_location_local.module = edl->tracks->number_of(current);
796                                         shared_location_local.plugin = current->plugin_set.total - 1;
797                                 }
798                                 first_track = 0;
799                         }
800                 }
801         }
802 }
803
804
805 void MWindow::insert_effect(char *title,
806         SharedLocation *shared_location,
807         Track *track,
808         PluginSet *plugin_set,
809         double start,
810         double length,
811         int plugin_type)
812 {
813         KeyFrame *default_keyframe = 0;
814         PluginServer *server = 0;
815 // Get default keyframe
816         if( plugin_type == PLUGIN_STANDALONE ) {
817                 default_keyframe = new KeyFrame;
818                 server = new PluginServer(*scan_plugindb(title, track->data_type));
819
820                 server->open_plugin(0, preferences, edl, 0);
821                 server->save_data(default_keyframe);
822         }
823 // Insert plugin object
824         track->insert_effect(title, shared_location,
825                 default_keyframe, plugin_set,
826                 start, length, plugin_type);
827         track->optimize();
828
829         if( plugin_type == PLUGIN_STANDALONE ) {
830                 server->close_plugin();
831                 delete server;
832                 delete default_keyframe;
833         }
834 }
835
836 int MWindow::modify_edithandles()
837 {
838         undo_before();
839         int handle_mode = edl->session->edit_handle_mode[session->drag_button];
840         edl->modify_edithandles(session->drag_start,
841                 session->drag_position, session->drag_handle, handle_mode,
842                 edl->session->labels_follow_edits,
843                 edl->session->plugins_follow_edits,
844                 edl->session->autos_follow_edits,
845                 session->drag_edit->group_id);
846         finish_modify_handles();
847 //printf("MWindow::modify_handles 1\n");
848         return 0;
849 }
850
851 int MWindow::modify_pluginhandles()
852 {
853         undo_before();
854
855         edl->modify_pluginhandles(session->drag_start,
856                 session->drag_position,
857                 session->drag_handle,
858                 edl->session->edit_handle_mode[session->drag_button],
859                 edl->session->labels_follow_edits,
860                 edl->session->autos_follow_edits,
861                 session->trim_edits);
862
863         finish_modify_handles();
864
865         return 0;
866 }
867
868
869 // Common to edithandles and plugin handles
870 void MWindow::finish_modify_handles()
871 {
872         int edit_mode = edl->session->edit_handle_mode[session->drag_button];
873         double position = -1;
874         switch( edit_mode ) {
875         case MOVE_RIPPLE:
876                 position = !session->drag_handle ?
877                         session->drag_start : session->drag_position;
878                 break;
879         case MOVE_ROLL:
880         case MOVE_SLIDE:
881                 position = session->drag_position;
882                 break;
883         case MOVE_SLIP:
884         case MOVE_EDGE:
885                 position = session->drag_start;
886                 break;
887         }
888         if( position >= 0 ) {
889                 edl->local_session->set_selectionstart(position);
890                 edl->local_session->set_selectionend(position);
891         }
892         undo_after(_("drag handle"), LOAD_EDITS | LOAD_TIMEBAR);
893
894         save_backup();
895         restart_brender();
896         sync_parameters(CHANGE_EDL);
897         update_plugin_guis();
898         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
899 // label list can be modified
900         awindow->gui->async_update_assets();
901         cwindow->update(1, 0, 0, 0, 1);
902 }
903
904 void MWindow::match_output_size(Track *track)
905 {
906         undo_before();
907         track->track_w = edl->session->output_w;
908         track->track_h = edl->session->output_h;
909         save_backup();
910         undo_after(_("match output size"), LOAD_ALL);
911
912         restart_brender();
913         sync_parameters(CHANGE_EDL);
914 }
915
916
917 void MWindow::delete_edit(Edit *edit, const char *msg, int collapse)
918 {
919         ArrayList<Edit*> edits;
920         edits.append(edit);
921         delete_edits(&edits, msg, collapse);
922 }
923
924 void MWindow::delete_edits(ArrayList<Edit*> *edits, const char *msg, int collapse)
925 {
926         if( !edits->size() ) return;
927         undo_before();
928         if( edl->session->labels_follow_edits )
929                 edl->delete_edit_labels(edits, collapse);
930         edl->delete_edits(edits, collapse);
931         edl->optimize();
932         save_backup();
933         undo_after(msg, LOAD_EDITS);
934
935         restart_brender();
936         cwindow->refresh_frame(CHANGE_EDL);
937         update_plugin_guis();
938         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
939 }
940
941 void MWindow::delete_edits(int collapse)
942 {
943         ArrayList<Edit*> edits;
944         edl->tracks->get_selected_edits(&edits);
945         delete_edits(&edits,_("del edit"), collapse);
946 }
947
948 // collapse - delete from timeline, not collapse replace with silence
949 // packed - omit unselected from selection, unpacked - replace unselected with silence
950 void MWindow::cut_selected_edits(int collapse, int packed)
951 {
952         selected_edits_to_clipboard(packed);
953         ArrayList<Edit*> edits;
954         edl->tracks->get_selected_edits(&edits);
955         delete_edits(&edits, _("cut edit"), collapse);
956 }
957
958
959 void MWindow::move_edits(ArrayList<Edit*> *edits,
960                 Track *track, double position, int mode)
961 {
962         undo_before();
963 // lockout timebar labels update
964 //  labels can be deleted with tooltip repeater running
965         cwindow->gui->lock_window("Tracks::move_edits");
966         edl->tracks->move_edits(edits, track, position,
967                 edl->session->labels_follow_edits,
968                 edl->session->plugins_follow_edits,
969                 edl->session->autos_follow_edits, mode);
970         cwindow->gui->timebar->update(1);
971         cwindow->gui->unlock_window();
972
973         save_backup();
974         undo_after(_("move edit"), LOAD_ALL);
975
976         restart_brender();
977         cwindow->refresh_frame(CHANGE_EDL);
978
979         update_plugin_guis();
980         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
981 }
982
983 void MWindow::selected_edits_to_clipboard(int packed)
984 {
985         EDL *new_edl = edl->selected_edits_to_clip(packed, 0, 0,
986                 edl->session->labels_follow_edits,
987                 edl->session->autos_follow_edits,
988                 edl->session->plugins_follow_edits);
989         if( !new_edl ) return;
990         FileXML file;
991         new_edl->copy(COPY_EDL, &file, "", 1);
992         const char *file_string = file.string();
993         long file_length = strlen(file_string);
994         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
995         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
996         new_edl->remove_user();
997 }
998
999 void MWindow::paste_clipboard(Track *first_track, double position, int overwrite,
1000                 int edit_edits, int edit_labels, int edit_autos, int edit_plugins)
1001 {
1002         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1003         if( !len ) return;
1004         char *string = new char[len];
1005         gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1006         FileXML file;
1007         file.read_from_string(string);
1008         delete [] string;
1009         EDL *clip = new EDL();
1010         clip->create_objects();
1011         if( !clip->load_xml(&file, LOAD_ALL) ) {
1012                 undo_before();
1013                 edl->paste_edits(clip, first_track, position, overwrite,
1014                         edit_edits, edit_labels, edit_autos, edit_plugins);
1015                 save_backup();
1016                 undo_after(_("paste clip"), LOAD_ALL);
1017                 restart_brender();
1018                 cwindow->refresh_frame(CHANGE_EDL);
1019
1020                 update_plugin_guis();
1021                 gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1022         }
1023         clip->remove_user();
1024 }
1025
1026 void MWindow::move_group(EDL *group, Track *first_track, double position, int overwrite)
1027 {
1028         undo_before();
1029 // lockout timebar labels update
1030 //  labels can be deleted with tooltip repeater running
1031         cwindow->gui->lock_window("Tracks::move_group");
1032
1033         ArrayList<Edit *>edits;
1034         edl->tracks->get_selected_edits(&edits);
1035         if( edl->session->labels_follow_edits )
1036                 edl->delete_edit_labels(&edits, 0);
1037         edl->delete_edits(&edits, 0);
1038         edl->paste_edits(group, first_track, position, overwrite, 1,
1039                 edl->session->labels_follow_edits,
1040                 edl->session->autos_follow_edits,
1041                 edl->session->plugins_follow_edits);
1042         cwindow->gui->timebar->update(1);
1043         cwindow->gui->unlock_window();
1044 // big debate over whether to do this, must either clear selected, or no tweaking
1045 //      edl->tracks->clear_selected_edits();
1046
1047         save_backup();
1048         undo_after(_("move group"), LOAD_ALL);
1049         restart_brender();
1050         cwindow->refresh_frame(CHANGE_EDL);
1051
1052         update_plugin_guis();
1053         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1054 }
1055
1056 void MWindow::move_effect(Plugin *plugin, Track *track, int64_t position)
1057 {
1058         undo_before();
1059         edl->tracks->move_effect(plugin, track, position);
1060         save_backup();
1061         undo_after(_("paste effect"), LOAD_ALL);
1062
1063         restart_brender();
1064         cwindow->refresh_frame(CHANGE_EDL);
1065         update_plugin_guis();
1066         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1067 }
1068
1069 void MWindow::move_effect(Plugin *plugin, PluginSet *plugin_set, int64_t position)
1070 {
1071         undo_before();
1072         edl->tracks->move_effect(plugin, plugin_set, position);
1073         save_backup();
1074         undo_after(_("move effect"), LOAD_ALL);
1075
1076         restart_brender();
1077         cwindow->refresh_frame(CHANGE_EDL);
1078         update_plugin_guis();
1079         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1080 }
1081
1082 void MWindow::move_plugins_up(PluginSet *plugin_set)
1083 {
1084
1085         undo_before();
1086         plugin_set->track->move_plugins_up(plugin_set);
1087
1088         save_backup();
1089         undo_after(_("move effect up"), LOAD_ALL);
1090         restart_brender();
1091         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1092         sync_parameters(CHANGE_EDL);
1093 }
1094
1095 void MWindow::move_plugins_down(PluginSet *plugin_set)
1096 {
1097         undo_before();
1098
1099         plugin_set->track->move_plugins_down(plugin_set);
1100
1101         save_backup();
1102         undo_after(_("move effect down"), LOAD_ALL);
1103         restart_brender();
1104         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1105         sync_parameters(CHANGE_EDL);
1106 }
1107
1108 void MWindow::move_track_down(Track *track)
1109 {
1110         undo_before();
1111         edl->tracks->move_track_down(track);
1112         save_backup();
1113         undo_after(_("move track down"), LOAD_ALL);
1114
1115         restart_brender();
1116         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1117         sync_parameters(CHANGE_EDL);
1118         save_backup();
1119 }
1120
1121 void MWindow::move_tracks_down()
1122 {
1123         undo_before();
1124         edl->tracks->move_tracks_down();
1125         save_backup();
1126         undo_after(_("move tracks down"), LOAD_ALL);
1127
1128         restart_brender();
1129         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1130         sync_parameters(CHANGE_EDL);
1131         save_backup();
1132 }
1133
1134 void MWindow::move_track_up(Track *track)
1135 {
1136         undo_before();
1137         edl->tracks->move_track_up(track);
1138         save_backup();
1139         undo_after(_("move track up"), LOAD_ALL);
1140         restart_brender();
1141         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1142         sync_parameters(CHANGE_EDL);
1143         save_backup();
1144 }
1145
1146 void MWindow::move_tracks_up()
1147 {
1148         undo_before();
1149         edl->tracks->move_tracks_up();
1150         save_backup();
1151         undo_after(_("move tracks up"), LOAD_ALL);
1152         restart_brender();
1153         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1154         sync_parameters(CHANGE_EDL);
1155 }
1156
1157
1158 void MWindow::mute_selection()
1159 {
1160         double start = edl->local_session->get_selectionstart();
1161         double end = edl->local_session->get_selectionend();
1162         if( start != end ) {
1163                 undo_before();
1164                 edl->clear(start, end, 0,
1165                         edl->session->plugins_follow_edits,
1166                         edl->session->autos_follow_edits);
1167                 edl->local_session->set_selectionend(end);
1168                 edl->local_session->set_selectionstart(start);
1169                 edl->paste_silence(start, end, 0,
1170                         edl->session->plugins_follow_edits,
1171                         edl->session->autos_follow_edits);
1172
1173                 save_backup();
1174                 undo_after(_("mute"), LOAD_EDITS);
1175
1176                 restart_brender();
1177                 update_plugin_guis();
1178                 gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
1179                 cwindow->refresh_frame(CHANGE_EDL);
1180         }
1181 }
1182
1183
1184 void MWindow::overwrite(EDL *source, int all)
1185 {
1186         FileXML file;
1187
1188         LocalSession *src = source->local_session;
1189         double src_start = all ? 0 :
1190                 src->inpoint_valid() ? src->get_inpoint() :
1191                 src->outpoint_valid() ? 0 :
1192                         src->get_selectionstart();
1193         double src_end = all ? source->tracks->total_length() :
1194                 src->outpoint_valid() ? src->get_outpoint() :
1195                 src->inpoint_valid() ? source->tracks->total_length() :
1196                         src->get_selectionend();
1197         double overwrite_len = src_end - src_start;
1198         double dst_start = edl->local_session->get_selectionstart();
1199         double dst_len = edl->local_session->get_selectionend() - dst_start;
1200
1201         undo_before();
1202         if( !EQUIV(dst_len, 0) && (dst_len < overwrite_len) ) {
1203 // in/out points or selection present and shorter than overwrite range
1204 // shorten the copy range
1205                 overwrite_len = dst_len;
1206         }
1207
1208         source->copy(copy_flags(), src_start, src_start + overwrite_len, &file, "", 1);
1209
1210 // HACK around paste_edl get_start/endselection on its own
1211 // so we need to clear only when not using both io points
1212 // FIXME: need to write simple overwrite_edl to be used for overwrite function
1213         if( edl->local_session->get_inpoint() < 0 ||
1214                 edl->local_session->get_outpoint() < 0 )
1215                 edl->clear(dst_start, dst_start + overwrite_len, 0, 0, 0);
1216
1217         paste(dst_start, dst_start + overwrite_len, &file, 0, 0, 0, 0, 0);
1218
1219         edl->local_session->set_selectionstart(dst_start + overwrite_len);
1220         edl->local_session->set_selectionend(dst_start + overwrite_len);
1221
1222         save_backup();
1223         undo_after(_("overwrite"), LOAD_EDITS);
1224
1225         restart_brender();
1226         update_plugin_guis();
1227         gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
1228         sync_parameters(CHANGE_EDL);
1229 }
1230
1231 // For splice and overwrite
1232 int MWindow::paste(double start, double end, FileXML *file,
1233         int edit_labels, int edit_plugins, int edit_autos,
1234         Track *first_track, int overwrite)
1235 {
1236         clear(0);
1237
1238 // Want to insert with assets shared with the master EDL.
1239         insert(start, file,
1240                 edit_labels, edit_plugins, edit_autos,
1241                 edl, first_track, overwrite);
1242
1243         return 0;
1244 }
1245
1246 // For editing using insertion point
1247 void MWindow::paste()
1248 {
1249         paste(edl->local_session->get_selectionstart(), 0, 1, 0);
1250 }
1251
1252 void MWindow::paste(double start, Track *first_track, int clear_selection, int overwrite)
1253 {
1254         //double end = edl->local_session->get_selectionend();
1255         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1256
1257         if( len ) {
1258                 char *string = new char[len];
1259                 undo_before();
1260                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1261                 FileXML file;
1262                 file.read_from_string(string);
1263                 if( clear_selection ) clear(0);
1264
1265                 insert(start, &file,
1266                         edl->session->labels_follow_edits,
1267                         edl->session->plugins_follow_edits,
1268                         edl->session->autos_follow_edits,
1269                         0, first_track, overwrite);
1270
1271                 edl->optimize();
1272                 delete [] string;
1273
1274                 save_backup();
1275
1276                 undo_after(_("paste"), LOAD_EDITS | LOAD_TIMEBAR);
1277                 restart_brender();
1278                 update_plugin_guis();
1279                 gui->update(1, FORCE_REDRAW, 1, 1, 0, 1, 0);
1280                 awindow->gui->async_update_assets();
1281                 sync_parameters(CHANGE_EDL);
1282         }
1283
1284 }
1285
1286 int MWindow::paste_assets(double position, Track *dest_track, int overwrite)
1287 {
1288         int result = 0;
1289         undo_before();
1290
1291         if( session->drag_assets->total ) {
1292                 load_assets(session->drag_assets,
1293                         position, LOADMODE_PASTE, dest_track, 0,
1294                         edl->session->labels_follow_edits,
1295                         edl->session->plugins_follow_edits,
1296                         edl->session->autos_follow_edits,
1297                         overwrite);
1298                 result = 1;
1299         }
1300
1301         if( session->drag_clips->total ) {
1302                 paste_edls(session->drag_clips,
1303                         LOADMODE_PASTE, dest_track, position,
1304                         edl->session->labels_follow_edits,
1305                         edl->session->plugins_follow_edits,
1306                         edl->session->autos_follow_edits,
1307                         overwrite);
1308                 result = 1;
1309         }
1310
1311         save_backup();
1312
1313         undo_after(_("paste assets"), LOAD_EDITS);
1314         restart_brender();
1315         gui->update(1, FORCE_REDRAW, 1, 0, 0, 1, 0);
1316         sync_parameters(CHANGE_EDL);
1317         return result;
1318 }
1319
1320 void MWindow::load_assets(ArrayList<Indexable*> *new_assets,
1321         double position, int load_mode, Track *first_track, RecordLabels *labels,
1322         int edit_labels, int edit_plugins, int edit_autos, int overwrite)
1323 {
1324         if( load_mode == LOADMODE_RESOURCESONLY )
1325                 load_mode = LOADMODE_ASSETSONLY;
1326 const int debug = 0;
1327 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1328         if( position < 0 ) position = edl->local_session->get_selectionstart();
1329
1330         ArrayList<EDL*> new_edls;
1331         for( int i=0; i<new_assets->total; ++i ) {
1332                 Indexable *indexable = new_assets->get(i);
1333                 if( indexable->is_asset ) {
1334                         remove_asset_from_caches((Asset*)indexable);
1335                 }
1336                 EDL *new_edl = new EDL;
1337                 new_edl->create_objects();
1338                 new_edl->copy_session(edl);
1339                 if( !indexable->is_asset ) {
1340                         EDL *nested_edl = (EDL*)indexable;
1341                         new_edl->create_nested(nested_edl);
1342                         new_edl->set_path(indexable->path);
1343                 }
1344                 else {
1345                         Asset *asset = (Asset*)indexable;
1346                         asset_to_edl(new_edl, asset);
1347                 }
1348                 new_edls.append(new_edl);
1349
1350                 if( labels ) {
1351                         for( RecordLabel *label=labels->first; label; label=label->next ) {
1352                                 new_edl->labels->toggle_label(label->position, label->position);
1353                         }
1354                 }
1355         }
1356 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1357
1358         paste_edls(&new_edls, load_mode, first_track, position,
1359                 edit_labels, edit_plugins, edit_autos, overwrite);
1360 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1361
1362         save_backup();
1363         for( int i=0; i<new_edls.size(); ++i )
1364                 new_edls.get(i)->Garbage::remove_user();
1365
1366 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1367 }
1368
1369 int MWindow::paste_automation()
1370 {
1371         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1372
1373         if( len ) {
1374                 undo_before();
1375                 speed_before();
1376                 char *string = new char[len];
1377                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1378                 FileXML file;
1379                 file.read_from_string(string);
1380                 delete [] string;
1381                 double start = edl->local_session->get_selectionstart();
1382                 double end = edl->local_session->get_selectionend();
1383                 edl->tracks->clear_automation(start, end);
1384                 edl->tracks->paste_automation(start, &file, 0, 1,
1385                         edl->session->typeless_keyframes);
1386                 int changed_edl = speed_after(1);
1387                 save_backup();
1388                 undo_after(_("paste keyframes"),
1389                         !changed_edl ? LOAD_AUTOMATION :
1390                                 LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
1391                 update_gui(changed_edl);
1392         }
1393
1394         return 0;
1395 }
1396
1397 int MWindow::paste_default_keyframe()
1398 {
1399         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1400
1401         if( len ) {
1402                 undo_before();
1403                 speed_before();
1404                 char *string = new char[len];
1405                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1406                 FileXML file;
1407                 file.read_from_string(string);
1408                 delete [] string;
1409                 double start = edl->local_session->get_selectionstart();
1410                 edl->tracks->paste_automation(start, &file, 1, 0,
1411                         edl->session->typeless_keyframes);
1412 //              edl->tracks->paste_default_keyframe(&file);
1413                 int changed_edl = speed_after(1);
1414                 undo_after(_("paste default keyframe"),
1415                         !changed_edl ? LOAD_AUTOMATION :
1416                                 LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
1417                 save_backup();
1418                 update_gui(changed_edl);
1419         }
1420
1421         return 0;
1422 }
1423
1424
1425 // Insert edls with project deletion and index file generation.
1426 int MWindow::paste_edls(ArrayList<EDL*> *new_edls, int load_mode,
1427         Track *first_track, double current_position,
1428         int edit_labels, int edit_plugins, int edit_autos,
1429         int overwrite)
1430 {
1431
1432         ArrayList<Track*> destination_tracks;
1433         int need_new_tracks = 0;
1434
1435 //PRINT_TRACE
1436         if( !new_edls->total ) return 0;
1437
1438 //PRINT_TRACE
1439 //      double original_length = edl->tracks->total_length();
1440 //      double original_preview_end = edl->local_session->preview_end;
1441 //PRINT_TRACE
1442
1443 // Delete current project
1444         if( load_mode == LOADMODE_REPLACE ||
1445             load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1446                 reset_caches();
1447                 edl->save_defaults(defaults);
1448                 hide_plugins();
1449                 edl->Garbage::remove_user();
1450                 edl = new EDL;
1451                 edl->create_objects();
1452                 edl->copy_session(new_edls->values[0]);
1453                 edl->copy_mixers(new_edls->values[0]);
1454                 gui->mainmenu->update_toggles(0);
1455                 gui->unlock_window();
1456                 gwindow->gui->update_toggles(1);
1457                 gui->lock_window("MWindow::paste_edls");
1458
1459 // Insert labels for certain modes constitutively
1460                 edit_labels = 1;
1461                 edit_plugins = 1;
1462                 edit_autos = 1;
1463 // Force reset of preview
1464 //              original_length = 0;
1465 //              original_preview_end = -1;
1466         }
1467
1468
1469 //PRINT_TRACE
1470
1471 // Create new tracks in master EDL
1472         if( load_mode == LOADMODE_REPLACE ||
1473             load_mode == LOADMODE_REPLACE_CONCATENATE ||
1474             load_mode == LOADMODE_NEW_TRACKS ) {
1475
1476                 need_new_tracks = 1;
1477                 for( int i=0; i<new_edls->total; ++i ) {
1478                         EDL *new_edl = new_edls->values[i];
1479                         for( Track *current=new_edl->tracks->first; current; current=NEXT ) {
1480                                 switch( current->data_type ) {
1481                                 case TRACK_VIDEO:
1482                                         edl->tracks->add_video_track(0, 0);
1483                                         if( current->draw ) edl->tracks->last->draw = 1;
1484                                         break;
1485                                 case TRACK_AUDIO:
1486                                         edl->tracks->add_audio_track(0, 0);
1487                                         break;
1488                                 case TRACK_SUBTITLE:
1489                                         edl->tracks->add_subttl_track(0, 0);
1490                                         break;
1491                                 default:
1492                                         continue;
1493                                 }
1494                                 destination_tracks.append(edl->tracks->last);
1495                         }
1496
1497 // Base track count on first EDL only for concatenation
1498                         if( load_mode == LOADMODE_REPLACE_CONCATENATE ) break;
1499                 }
1500                 edl->session->highlighted_track = edl->tracks->total() - 1;
1501         }
1502         else
1503 // Recycle existing tracks of master EDL
1504         if( load_mode == LOADMODE_CONCATENATE ||
1505             load_mode == LOADMODE_PASTE ||
1506             load_mode == LOADMODE_NESTED ) {
1507                 Track *current = first_track ? first_track : edl->tracks->first;
1508                 for( ; current; current=NEXT ) {
1509                         if( current->record ) {
1510                                 destination_tracks.append(current);
1511                         }
1512                 }
1513 //PRINT_TRACE
1514         }
1515 //PRINT_TRACE
1516         int destination_track = 0;
1517         double *paste_position = new double[destination_tracks.total];
1518
1519 // Iterate through the edls
1520         for( int i=0; i<new_edls->total; ++i ) {
1521
1522                 EDL *new_edl = new_edls->values[i];
1523                 double edl_length = new_edl->local_session->clipboard_length ?
1524                         new_edl->local_session->clipboard_length :
1525                         new_edl->tracks->total_length();
1526 // printf("MWindow::paste_edls 2 %f %f\n",
1527 // new_edl->local_session->clipboard_length,
1528 // new_edl->tracks->total_length());
1529 // new_edl->dump();
1530 //PRINT_TRACE
1531
1532 // Convert EDL to master rates
1533                 new_edl->resample(new_edl->session->sample_rate,
1534                         edl->session->sample_rate,
1535                         TRACK_AUDIO);
1536                 new_edl->resample(new_edl->session->frame_rate,
1537                         edl->session->frame_rate,
1538                         TRACK_VIDEO);
1539 //PRINT_TRACE
1540 // Add assets and prepare index files
1541                 for( Asset *new_asset=new_edl->assets->first;
1542                      new_asset; new_asset=new_asset->next ) {
1543                         mainindexes->add_next_asset(0, new_asset);
1544                 }
1545 // Capture index file status from mainindex test
1546                 edl->update_assets(new_edl);
1547 //PRINT_TRACE
1548 // Get starting point of insertion.  Need this to paste labels.
1549                 switch( load_mode ) {
1550                 case LOADMODE_REPLACE:
1551                         current_position = 0;
1552                 case LOADMODE_NEW_TRACKS:
1553                         break;
1554
1555                 case LOADMODE_CONCATENATE:
1556                 case LOADMODE_REPLACE_CONCATENATE:
1557                         destination_track = 0;
1558                         if( destination_tracks.total )
1559                                 current_position = destination_tracks.values[0]->get_length();
1560                         else
1561                                 current_position = 0;
1562                         break;
1563
1564                 case LOADMODE_PASTE:
1565                 case LOADMODE_NESTED:
1566                         destination_track = 0;
1567                         if( i == 0 ) {
1568                                 for( int j=0; j<destination_tracks.total; ++j ) {
1569                                         paste_position[j] = (current_position >= 0) ?
1570                                                 current_position :
1571                                                 edl->local_session->get_selectionstart();
1572                                 }
1573                         }
1574                         break;
1575
1576                 case LOADMODE_RESOURCESONLY:
1577                         edl->add_clip(new_edl);
1578                         break;
1579                 }
1580 // Insert edl
1581                 if( load_mode != LOADMODE_RESOURCESONLY &&
1582                     load_mode != LOADMODE_ASSETSONLY ) {
1583 // Insert labels
1584                         if( edit_labels ) {
1585                                 if( load_mode == LOADMODE_PASTE || load_mode == LOADMODE_NESTED )
1586                                         edl->labels->insert_labels(new_edl->labels,
1587                                                 destination_tracks.total ? paste_position[0] : 0.0,
1588                                                 edl_length, 1);
1589                                 else
1590                                         edl->labels->insert_labels(new_edl->labels, current_position,
1591                                                 edl_length, 1);
1592                         }
1593                         double total_length = new_edl->tracks->total_length();
1594                         for( Track *new_track=new_edl->tracks->first;
1595                              new_track; new_track=new_track->next ) {
1596 // Get destination track of same type as new_track
1597                                 for( int k = 0;
1598                                      k < destination_tracks.total &&
1599                                      destination_tracks.values[destination_track]->data_type != new_track->data_type;
1600                                      ++k, ++destination_track ) {
1601                                         if( destination_track >= destination_tracks.total - 1 )
1602                                                 destination_track = 0;
1603                                 }
1604
1605 // Insert data into destination track
1606                                 if( destination_track < destination_tracks.total &&
1607                                     destination_tracks.values[destination_track]->data_type == new_track->data_type ) {
1608                                         Track *track = destination_tracks.values[destination_track];
1609
1610 // Replace default keyframes if first EDL and new tracks were created.
1611 // This means data copied from one track and pasted to another won't retain
1612 // the camera position unless it's a keyframe.  If it did, previous data in the
1613 // track might get unknowingly corrupted.  Ideally we would detect when differing
1614 // default keyframes existed and create discrete keyframes for both.
1615                                         int replace_default = (i == 0) && need_new_tracks;
1616
1617 //printf("MWindow::paste_edls 1 %d\n", replace_default);
1618 // Insert new track at current position
1619                                         switch( load_mode ) {
1620                                         case LOADMODE_REPLACE_CONCATENATE:
1621                                         case LOADMODE_CONCATENATE:
1622                                                 current_position = track->get_length();
1623                                                 break;
1624
1625                                         case LOADMODE_PASTE:
1626                                         case LOADMODE_NESTED:
1627                                                 current_position = paste_position[destination_track];
1628                                                 paste_position[destination_track] += new_track->get_length();
1629                                                 break;
1630                                         }
1631                                         if( overwrite ) {
1632                                                 double length = overwrite >= 0 ?
1633                                                         new_track->get_length() : total_length;
1634                                                 track->clear(current_position,
1635                                                         current_position + length,
1636                                                         1, // edit edits
1637                                                         edit_labels, edit_plugins, edit_autos,
1638                                                         0); // trim edits
1639                                         }
1640 //PRINT_TRACE
1641                                         track->insert_track(new_track, current_position, replace_default,
1642                                                 edit_plugins, edit_autos, edl_length);
1643 //PRINT_TRACE
1644                                 }
1645
1646 // Get next destination track
1647                                 destination_track++;
1648                                 if( destination_track >= destination_tracks.total )
1649                                         destination_track = 0;
1650                         }
1651                 }
1652
1653                 if( load_mode == LOADMODE_PASTE ||
1654                     load_mode == LOADMODE_NESTED )
1655                         current_position += edl_length;
1656         }
1657
1658
1659 // Move loading of clips and vwindow to the end - this fixes some
1660 // strange issue, for index not being shown
1661 // Assume any paste operation from the same EDL won't contain any clips.
1662 // If it did it would duplicate every clip here.
1663         for( int i=0; i<new_edls->total; ++i ) {
1664                 EDL *new_edl = new_edls->get(i);
1665
1666                 for( int j=0; j<new_edl->clips.size(); ++j ) {
1667                         edl->add_clip(new_edl->clips[j]);
1668                 }
1669                 for( int j=0; j<new_edl->nested_edls.size(); ++j ) {
1670                         edl->nested_edls.get_nested(new_edl->nested_edls[j]);
1671                 }
1672
1673                 if( new_edl->total_vwindow_edls() ) {
1674 //                      if( edl->vwindow_edl )
1675 //                              edl->vwindow_edl->Garbage::remove_user();
1676 //                      edl->vwindow_edl = new EDL(edl);
1677 //                      edl->vwindow_edl->create_objects();
1678 //                      edl->vwindow_edl->copy_all(new_edl->vwindow_edl);
1679
1680                         for( int j=0; j<new_edl->total_vwindow_edls(); ++j ) {
1681                                 EDL *vwindow_edl = new EDL(edl);
1682                                 vwindow_edl->create_objects();
1683                                 vwindow_edl->copy_all(new_edl->get_vwindow_edl(j));
1684                                 edl->append_vwindow_edl(vwindow_edl, 0);
1685                         }
1686                 }
1687         }
1688
1689         if( paste_position ) delete [] paste_position;
1690
1691 // This is already done in load_filenames and everything else that uses paste_edls
1692 //      update_project(load_mode);
1693
1694 // Fix preview range
1695 //      if( EQUIV(original_length, original_preview_end) )
1696 //      {
1697 //              edl->local_session->preview_end = edl->tracks->total_length();
1698 //      }
1699
1700 // Start examining next batch of index files
1701         mainindexes->start_build();
1702
1703 // Don't save a backup after loading since the loaded file is on disk already.
1704 //PRINT_TRACE
1705         return 0;
1706 }
1707
1708 void MWindow::paste_silence()
1709 {
1710         double start = edl->local_session->get_selectionstart();
1711         double end = edl->local_session->get_selectionend();
1712         if( EQUIV(start, end) ) {
1713                 if( edl->session->frame_rate > 0 )
1714                         end += 1./edl->session->frame_rate;
1715         }
1716         undo_before(_("silence"), this);
1717         edl->paste_silence(start, end,
1718                 edl->session->labels_follow_edits,
1719                 edl->session->plugins_follow_edits,
1720                 edl->session->autos_follow_edits);
1721         edl->optimize();
1722         save_backup();
1723         undo_after(_("silence"), LOAD_EDITS | LOAD_TIMEBAR);
1724
1725         update_plugin_guis();
1726         restart_brender();
1727         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
1728         cwindow->update(1, 0, 0, 0, 1);
1729         cwindow->refresh_frame(CHANGE_EDL);
1730 }
1731
1732 void MWindow::detach_transition(Transition *transition)
1733 {
1734         undo_before();
1735         hide_plugin(transition, 1);
1736         int is_video = (transition->edit->track->data_type == TRACK_VIDEO);
1737         transition->edit->detach_transition();
1738         save_backup();
1739         undo_after(_("detach transition"), LOAD_ALL);
1740
1741         if( is_video ) restart_brender();
1742         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1743         sync_parameters(CHANGE_EDL);
1744 }
1745
1746 void MWindow::detach_transitions()
1747 {
1748         gui->lock_window("MWindow::detach_transitions 1");
1749
1750         undo_before();
1751         double start = edl->local_session->get_selectionstart();
1752         double end = edl->local_session->get_selectionend();
1753         edl->tracks->clear_transitions(start, end);
1754
1755         save_backup();
1756         undo_after(_("detach transitions"), LOAD_EDITS);
1757
1758         sync_parameters(CHANGE_EDL);
1759         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1760         gui->unlock_window();
1761 }
1762
1763 void MWindow::paste_transition()
1764 {
1765 // Only the first transition gets dropped.
1766         PluginServer *server = session->drag_pluginservers->values[0];
1767
1768         undo_before();
1769         edl->tracks->paste_transition(server, session->edit_highlighted);
1770         save_backup();
1771         undo_after(_("transition"), LOAD_EDITS);
1772
1773         if( server->video ) restart_brender();
1774         sync_parameters(CHANGE_ALL);
1775 }
1776
1777 void MWindow::paste_transitions(int track_type, char *title)
1778 {
1779         gui->lock_window("MWindow::detach_transitions 1");
1780
1781         undo_before();
1782         double start = edl->local_session->get_selectionstart();
1783         double end = edl->local_session->get_selectionend();
1784         edl->tracks->paste_transitions(start, end, track_type, title);
1785         save_backup();
1786         undo_after(_("attach transitions"), LOAD_EDITS);
1787
1788         sync_parameters(CHANGE_EDL);
1789         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1790         gui->unlock_window();
1791 }
1792
1793 void MWindow::paste_transition_cwindow(Track *dest_track)
1794 {
1795         PluginServer *server = session->drag_pluginservers->values[0];
1796         undo_before();
1797         edl->tracks->paste_video_transition(server, 1);
1798         save_backup();
1799         undo_after(_("transition"), LOAD_EDITS);
1800         restart_brender();
1801         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1802         sync_parameters(CHANGE_ALL);
1803 }
1804
1805 void MWindow::paste_audio_transition()
1806 {
1807         PluginServer *server = scan_plugindb(edl->session->default_atransition,
1808                 TRACK_AUDIO);
1809         if( !server ) {
1810                 char string[BCTEXTLEN];
1811                 sprintf(string, _("No default transition %s found."), edl->session->default_atransition);
1812                 gui->show_message(string);
1813                 return;
1814         }
1815
1816         undo_before();
1817         edl->tracks->paste_audio_transition(server);
1818         save_backup();
1819         undo_after(_("transition"), LOAD_EDITS);
1820
1821         sync_parameters(CHANGE_EDL);
1822         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1823 }
1824
1825 void MWindow::paste_video_transition()
1826 {
1827         PluginServer *server = scan_plugindb(edl->session->default_vtransition,
1828                 TRACK_VIDEO);
1829         if( !server ) {
1830                 char string[BCTEXTLEN];
1831                 sprintf(string, _("No default transition %s found."), edl->session->default_vtransition);
1832                 gui->show_message(string);
1833                 return;
1834         }
1835
1836         undo_before();
1837
1838         edl->tracks->paste_video_transition(server);
1839         save_backup();
1840         undo_after(_("transition"), LOAD_EDITS);
1841
1842         sync_parameters(CHANGE_EDL);
1843         restart_brender();
1844         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1845 }
1846
1847 void MWindow::shuffle_edits()
1848 {
1849         gui->lock_window("MWindow::shuffle_edits 1");
1850
1851         undo_before();
1852         double start = edl->local_session->get_selectionstart();
1853         double end = edl->local_session->get_selectionend();
1854
1855         edl->tracks->shuffle_edits(start, end);
1856
1857         save_backup();
1858         undo_after(_("shuffle edits"), LOAD_EDITS | LOAD_TIMEBAR);
1859
1860         sync_parameters(CHANGE_EDL);
1861         restart_brender();
1862         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
1863         gui->unlock_window();
1864 }
1865
1866 void MWindow::reverse_edits()
1867 {
1868         gui->lock_window("MWindow::reverse_edits 1");
1869
1870         undo_before();
1871         double start = edl->local_session->get_selectionstart();
1872         double end = edl->local_session->get_selectionend();
1873
1874         edl->tracks->reverse_edits(start, end);
1875
1876         save_backup();
1877         undo_after(_("reverse edits"), LOAD_EDITS | LOAD_TIMEBAR);
1878
1879         sync_parameters(CHANGE_EDL);
1880         restart_brender();
1881         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
1882         gui->unlock_window();
1883 }
1884
1885 void MWindow::align_edits()
1886 {
1887         gui->lock_window("MWindow::align_edits 1");
1888
1889         undo_before();
1890         double start = edl->local_session->get_selectionstart();
1891         double end = edl->local_session->get_selectionend();
1892
1893         edl->tracks->align_edits(start, end);
1894
1895         save_backup();
1896         undo_after(_("align edits"), LOAD_EDITS | LOAD_TIMEBAR);
1897
1898         sync_parameters(CHANGE_EDL);
1899         restart_brender();
1900         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
1901         gui->unlock_window();
1902 }
1903
1904 void MWindow::set_edit_length(double length)
1905 {
1906         gui->lock_window("MWindow::set_edit_length 1");
1907
1908         undo_before();
1909         double start = edl->local_session->get_selectionstart();
1910         double end = edl->local_session->get_selectionend();
1911
1912         edl->tracks->set_edit_length(start, end, length);
1913
1914         save_backup();
1915         undo_after(_("edit length"), LOAD_EDITS | LOAD_TIMEBAR);
1916
1917         sync_parameters(CHANGE_EDL);
1918         restart_brender();
1919         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
1920         gui->unlock_window();
1921 }
1922
1923
1924 void MWindow::set_transition_length(Transition *transition, double length)
1925 {
1926         gui->lock_window("MWindow::set_transition_length 1");
1927
1928         undo_before();
1929         //double start = edl->local_session->get_selectionstart();
1930         //double end = edl->local_session->get_selectionend();
1931
1932         edl->tracks->set_transition_length(transition, length);
1933
1934         save_backup();
1935         undo_after(_("transition length"), LOAD_EDITS);
1936
1937         edl->session->default_transition_length = length;
1938         sync_parameters(CHANGE_PARAMS);
1939         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1940         gui->unlock_window();
1941 }
1942
1943 void MWindow::set_transition_length(double length)
1944 {
1945         gui->lock_window("MWindow::set_transition_length 2");
1946
1947         undo_before();
1948         double start = edl->local_session->get_selectionstart();
1949         double end = edl->local_session->get_selectionend();
1950
1951         edl->tracks->set_transition_length(start, end, length);
1952
1953         save_backup();
1954         undo_after(_("transition length"), LOAD_EDITS);
1955
1956         edl->session->default_transition_length = length;
1957         sync_parameters(CHANGE_PARAMS);
1958         restart_brender();
1959         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1960         gui->unlock_window();
1961 }
1962
1963
1964 void MWindow::redo_entry(BC_WindowBase *calling_window_gui)
1965 {
1966         calling_window_gui->unlock_window();
1967         stop_playback(0);
1968         if( undo->redo_load_flags() & LOAD_SESSION )
1969                 close_mixers();
1970
1971         cwindow->gui->lock_window("MWindow::redo_entry 1");
1972         for( int i=0; i<vwindows.size(); ++i ) {
1973                 if( vwindows.get(i)->is_running() ) {
1974                         if( calling_window_gui != vwindows.get(i)->gui ) {
1975                                 vwindows.get(i)->gui->lock_window("MWindow::redo_entry 2");
1976                         }
1977                 }
1978         }
1979         gui->lock_window("MWindow::redo_entry 3");
1980
1981         undo->redo();
1982
1983         save_backup();
1984         restart_brender();
1985         update_plugin_states();
1986         update_plugin_guis();
1987
1988         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 1);
1989         gui->update_proxy_toggle();
1990         gui->unlock_window();
1991         cwindow->update(1, 1, 1, 1, 1);
1992         cwindow->gui->unlock_window();
1993
1994         for( int i=0; i < vwindows.size(); ++i ) {
1995                 if( vwindows.get(i)->is_running() ) {
1996                         if( calling_window_gui != vwindows.get(i)->gui ) {
1997                                 vwindows.get(i)->gui->unlock_window();
1998                         }
1999                 }
2000         }
2001
2002         awindow->gui->async_update_assets();
2003
2004         cwindow->refresh_frame(CHANGE_ALL);
2005         calling_window_gui->lock_window("MWindow::redo_entry 4");
2006 }
2007
2008
2009 void MWindow::resize_track(Track *track, int w, int h)
2010 {
2011         undo_before();
2012 // We have to move all maskpoints so they do not move in relation to image areas
2013         ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->translate_masks(
2014                 (w - track->track_w) / 2,
2015                 (h - track->track_h) / 2);
2016         track->track_w = w;
2017         track->track_h = h;
2018         undo_after(_("resize track"), LOAD_ALL);
2019         save_backup();
2020
2021         restart_brender();
2022         sync_parameters(CHANGE_EDL);
2023 }
2024
2025
2026 void MWindow::set_inpoint()
2027 {
2028         undo_before();
2029         edl->set_inpoint(edl->local_session->get_selectionstart(1));
2030         save_backup();
2031         undo_after(_("in point"), LOAD_TIMEBAR);
2032         gui->update_timebar(1);
2033
2034         cwindow->gui->lock_window("MWindow::set_inpoint 2");
2035         cwindow->gui->timebar->update(1);
2036         cwindow->gui->unlock_window();
2037 }
2038
2039 void MWindow::set_outpoint()
2040 {
2041         undo_before();
2042         edl->set_outpoint(edl->local_session->get_selectionend(1));
2043         save_backup();
2044         undo_after(_("out point"), LOAD_TIMEBAR);
2045
2046         gui->update_timebar(1);
2047         cwindow->gui->lock_window("MWindow::set_outpoint 2");
2048         cwindow->gui->timebar->update(1);
2049         cwindow->gui->unlock_window();
2050 }
2051
2052 void MWindow::unset_inoutpoint()
2053 {
2054         undo_before();
2055         edl->unset_inoutpoint();
2056         save_backup();
2057         undo_after(_("clear in/out"), LOAD_TIMEBAR);
2058
2059         gui->update_timebar(1);
2060         cwindow->gui->lock_window("MWindow::unset_inoutpoint 2");
2061         cwindow->gui->timebar->update(1);
2062         cwindow->gui->unlock_window();
2063 }
2064
2065 void MWindow::splice(EDL *source, int all)
2066 {
2067         FileXML file;
2068         LocalSession *src = source->local_session;
2069
2070         undo_before();
2071         double source_start = all ? 0 :
2072                 src->inpoint_valid() ? src->get_inpoint() :
2073                 src->outpoint_valid() ? 0 : src->get_selectionstart();
2074         double source_end = all ? source->tracks->total_length() :
2075                 src->outpoint_valid() ? src->get_outpoint() :
2076                 src->inpoint_valid() ? source->tracks->total_length() :
2077                         src->get_selectionend();
2078         source->copy(COPY_EDL, source_start, source_end, &file, "", 1);
2079 //file.dump();
2080         double start = edl->local_session->get_selectionstart();
2081         //double end = edl->local_session->get_selectionend();
2082
2083         paste(start, start, &file,
2084                 edl->session->labels_follow_edits,
2085                 edl->session->plugins_follow_edits,
2086                 edl->session->autos_follow_edits,
2087                 0, 0);
2088
2089 // Position at end of clip
2090         edl->local_session->set_selectionstart(start + source_end - source_start);
2091         edl->local_session->set_selectionend(start + source_end - source_start);
2092
2093         save_backup();
2094         undo_after(_("splice"), LOAD_EDITS | LOAD_TIMEBAR);
2095         update_plugin_guis();
2096         restart_brender();
2097         gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
2098         sync_parameters(CHANGE_EDL);
2099 }
2100
2101 void MWindow::save_clip(EDL *new_edl, const char *txt)
2102 {
2103         new_edl->local_session->set_selectionstart(0);
2104         new_edl->local_session->set_selectionend(0);
2105         sprintf(new_edl->local_session->clip_title, _("Clip %d"),
2106                 session->clip_number++);
2107         char duration[BCTEXTLEN];
2108         Units::totext(duration, new_edl->tracks->total_length(),
2109                 new_edl->session->time_format,
2110                 new_edl->session->sample_rate,
2111                 new_edl->session->frame_rate,
2112                 new_edl->session->frames_per_foot);
2113
2114         Track *track = new_edl->tracks->first;
2115         const char *path = edl->path;
2116         for( ; (!path || !*path) && track; track=track->next ) {
2117                 if( !track->record ) continue;
2118                 Edit *edit = track->edits->first;
2119                 if( !edit ) continue;
2120                 Indexable *indexable = edit->get_source();
2121                 if( !indexable ) continue;
2122                 path = indexable->path;
2123         }
2124
2125         time_t now;  time(&now);
2126         struct tm dtm;   localtime_r(&now, &dtm);
2127         char *cp = new_edl->local_session->clip_notes;
2128         int n, sz = sizeof(new_edl->local_session->clip_notes)-1;
2129         if( txt && *txt ) {
2130                 n = snprintf(cp, sz, "%s", txt);
2131                 cp += n;  sz -= n;
2132         }
2133         n = snprintf(cp, sz,
2134                 "%02d/%02d/%02d %02d:%02d:%02d,  +%s\n",
2135                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
2136                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec, duration);
2137         cp += n;  sz -= n;
2138         if( path && *path ) {
2139                 FileSystem fs;
2140                 char title[BCTEXTLEN];
2141                 fs.extract_name(title, path);
2142                 n = snprintf(cp, sz, "%s", title);
2143                 cp += n;  sz -= n;
2144         }
2145         cp[n] = 0;
2146         sprintf(new_edl->local_session->clip_icon,
2147                 "clip_%02d%02d%02d-%02d%02d%02d.png",
2148                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
2149                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
2150         new_edl->folder_no = AW_CLIP_FOLDER;
2151         edl->update_assets(new_edl);
2152         int cur_x, cur_y;
2153         gui->get_abs_cursor(cur_x, cur_y, 0);
2154         gui->unlock_window();
2155
2156         awindow->clip_edit->create_clip(new_edl, cur_x, cur_y);
2157         new_edl->remove_user();
2158
2159         gui->lock_window("MWindow::save_clip");
2160         save_backup();
2161 }
2162
2163 void MWindow::to_clip(EDL *edl, const char *txt, int all)
2164 {
2165         FileXML file;
2166         LocalSession *src = edl->local_session;
2167
2168         gui->lock_window("MWindow::to_clip 1");
2169         double start = all ? 0 :
2170                 src->inpoint_valid() ? src->get_inpoint() :
2171                 src->outpoint_valid() ? 0 : src->get_selectionstart();
2172         double end = all ? edl->tracks->total_length() :
2173                 src->outpoint_valid() ? src->get_outpoint() :
2174                 src->inpoint_valid() ? edl->tracks->total_length() :
2175                         src->get_selectionend();
2176         if( EQUIV(end, start) ) {
2177                 start = 0;
2178                 end = edl->tracks->total_length();
2179         }
2180
2181 // Don't copy all since we don't want the clips twice.
2182         edl->copy(copy_flags(), start, end, &file, "", 1);
2183
2184         EDL *new_edl = new EDL(edl);
2185         new_edl->create_objects();
2186         new_edl->load_xml(&file, LOAD_ALL);
2187         save_clip(new_edl, txt);
2188         gui->unlock_window();
2189 }
2190
2191 int MWindow::toggle_label()
2192 {
2193         double position1, position2;
2194         undo_before();
2195
2196         position1 = edl->local_session->get_selectionstart(1);
2197         position2 = edl->local_session->get_selectionend(1);
2198         position1 = edl->align_to_frame(position1, 0);
2199         position2 = edl->align_to_frame(position2, 0);
2200
2201         edl->labels->toggle_label(position1, position2);
2202         save_backup();
2203
2204         gui->update_timebar(0);
2205         gui->activate_timeline();
2206         gui->flush();
2207
2208         cwindow->gui->lock_window("MWindow::toggle_label 2");
2209         cwindow->gui->timebar->update(1);
2210         cwindow->gui->unlock_window();
2211         awindow->gui->async_update_assets();
2212
2213         undo_after(_("label"), LOAD_TIMEBAR);
2214         return 0;
2215 }
2216
2217 void MWindow::trim_selection()
2218 {
2219         undo_before();
2220
2221
2222         edl->trim_selection(edl->local_session->get_selectionstart(),
2223                 edl->local_session->get_selectionend(),
2224                 edl->session->labels_follow_edits,
2225                 edl->session->plugins_follow_edits,
2226                 edl->session->autos_follow_edits);
2227
2228         save_backup();
2229         undo_after(_("trim selection"), LOAD_EDITS | LOAD_TIMEBAR);
2230         update_plugin_guis();
2231         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2232         cwindow->update(1, 0, 0, 0, 1);
2233         awindow->gui->async_update_assets();
2234         restart_brender();
2235         cwindow->refresh_frame(CHANGE_EDL);
2236 }
2237
2238
2239 void MWindow::undo_entry(BC_WindowBase *calling_window_gui)
2240 {
2241         calling_window_gui->unlock_window();
2242         stop_playback(0);
2243         if( undo->undo_load_flags() & LOAD_SESSION )
2244                 close_mixers();
2245
2246         cwindow->gui->lock_window("MWindow::undo_entry 1");
2247         for( int i=0; i<vwindows.size(); ++i ) {
2248                 if( vwindows.get(i)->is_running() ) {
2249                         if( calling_window_gui != vwindows.get(i)->gui ) {
2250                                 vwindows.get(i)->gui->lock_window("MWindow::undo_entry 4");
2251                         }
2252                 }
2253         }
2254         gui->lock_window("MWindow::undo_entry 2");
2255
2256         undo->undo();
2257
2258         save_backup();
2259         restart_brender();
2260         update_plugin_states();
2261         update_plugin_guis();
2262
2263         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 1);
2264         gui->update_proxy_toggle();
2265         gui->unlock_window();
2266         cwindow->update(1, 1, 1, 1, 1);
2267         cwindow->gui->unlock_window();
2268
2269         for( int i=0; i<vwindows.size(); ++i ) {
2270                 if( vwindows.get(i)->is_running() ) {
2271                         if( calling_window_gui != vwindows.get(i)->gui ) {
2272                                 vwindows.get(i)->gui->unlock_window();
2273                         }
2274                 }
2275         }
2276
2277         awindow->gui->async_update_assets();
2278
2279         cwindow->refresh_frame(CHANGE_ALL);
2280         calling_window_gui->lock_window("MWindow::undo_entry 4");
2281 }
2282
2283
2284 void MWindow::new_folder(const char *new_folder, int is_clips)
2285 {
2286         undo_before();
2287         if( edl->new_folder(new_folder, is_clips) ) {
2288                 MainError::show_error(_("create new folder failed"));
2289         }
2290         undo_after(_("new folder"), LOAD_ALL);
2291         awindow->gui->async_update_assets();
2292 }
2293
2294 void MWindow::delete_folder(char *folder)
2295 {
2296         undo_before();
2297         if( edl->delete_folder(folder) < 0 ) {
2298                 MainError::show_error(_("delete folder failed"));
2299         }
2300         undo_after(_("del folder"), LOAD_ALL);
2301         awindow->gui->async_update_assets();
2302 }
2303
2304 void MWindow::select_point(double position)
2305 {
2306         gui->unlock_window();
2307         gui->stop_drawing();
2308         cwindow->stop_playback(0);
2309         gui->lock_window("MWindow::select_point");
2310
2311         edl->local_session->set_selectionstart(position);
2312         edl->local_session->set_selectionend(position);
2313
2314 // Que the CWindow
2315         cwindow->update(1, 0, 0, 0, 1);
2316
2317         update_plugin_guis();
2318         gui->update_patchbay();
2319         gui->hide_cursor(0);
2320         gui->draw_cursor(0);
2321         gui->mainclock->update(edl->local_session->get_selectionstart(1));
2322         gui->zoombar->update();
2323         gui->update_timebar(0);
2324         gui->flash_canvas(0);
2325         gui->flush();
2326 }
2327
2328
2329
2330
2331 void MWindow::map_audio(int pattern)
2332 {
2333         undo_before();
2334         remap_audio(pattern);
2335         undo_after(
2336                 pattern == MWindow::AUDIO_1_TO_1 ? _("map 1:1") : _("map 5.1:2"),
2337                 LOAD_AUTOMATION);
2338         sync_parameters(CHANGE_PARAMS);
2339         gui->update(0, NORMAL_DRAW, 0, 0, 1, 0, 0);
2340 }
2341
2342 void MWindow::remap_audio(int pattern)
2343 {
2344         int current_channel = 0;
2345         int current_track = 0;
2346         for( Track *current=edl->tracks->first; current; current=NEXT ) {
2347                 if( current->data_type == TRACK_AUDIO &&
2348                         current->record ) {
2349                         Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
2350                         PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
2351
2352                         for( int i=0; i < MAXCHANNELS; ++i ) {
2353                                 pan_auto->values[i] = 0.0;
2354                         }
2355
2356                         if( pattern == MWindow::AUDIO_1_TO_1 ) {
2357                                 pan_auto->values[current_channel] = 1.0;
2358                         }
2359                         else
2360                         if( pattern == MWindow::AUDIO_5_1_TO_2 ) {
2361                                 switch( current_track ) {
2362                                 case 0:
2363                                 case 4:
2364                                         pan_auto->values[0] = 1;
2365                                         break;
2366                                 case 1:
2367                                 case 5:
2368                                         pan_auto->values[1] = 1;
2369                                         break;
2370                                 case 2:
2371                                 case 3:
2372                                         pan_auto->values[0] = 0.5;
2373                                         pan_auto->values[1] = 0.5;
2374                                         break;
2375                                 }
2376                         }
2377
2378                         BC_Pan::calculate_stick_position(edl->session->audio_channels,
2379                                 edl->session->achannel_positions, pan_auto->values,
2380                                 MAX_PAN, PAN_RADIUS, pan_auto->handle_x, pan_auto->handle_y);
2381
2382                         current_channel++;
2383                         current_track++;
2384                         if( current_channel >= edl->session->audio_channels )
2385                                 current_channel = 0;
2386                 }
2387         }
2388 }
2389
2390
2391 void MWindow::rescale_proxy(EDL *clip, int orig_scale, int new_scale)
2392 {
2393         edl->rescale_proxy(orig_scale, new_scale);
2394 }
2395
2396 void MWindow::add_proxy(int use_scaler,
2397         ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
2398 {
2399         edl->add_proxy(use_scaler, orig_assets, proxy_assets);
2400 }
2401
2402 void MWindow::cut_commercials()
2403 {
2404 #ifdef HAVE_COMMERCIAL
2405         undo_before();
2406         commercials->scan_media();
2407         edl->optimize();
2408         save_backup();
2409         undo_after(_("cut ads"), LOAD_EDITS | LOAD_TIMEBAR);
2410
2411         restart_brender();
2412         update_plugin_guis();
2413         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2414         cwindow->update(1, 0, 0, 0, 1);
2415         cwindow->refresh_frame(CHANGE_EDL);
2416 #endif
2417 }
2418
2419 int MWindow::normalize_speed(EDL *old_edl, EDL *new_edl)
2420 {
2421         int result = 0;
2422         Track *old_track = old_edl->tracks->first;
2423         Track *new_track = new_edl->tracks->first;
2424         for( ; old_track && new_track; old_track=old_track->next, new_track=new_track->next ) {
2425                 if( old_track->data_type != new_track->data_type ) continue;
2426                 FloatAutos *old_speeds = (FloatAutos *)old_track->automation->autos[AUTOMATION_SPEED];
2427                 FloatAutos *new_speeds = (FloatAutos *)new_track->automation->autos[AUTOMATION_SPEED];
2428                 if( !old_speeds || !new_speeds ) continue;
2429                 FloatAuto *old_speed = (FloatAuto *)old_speeds->first;
2430                 FloatAuto *new_speed = (FloatAuto *)new_speeds->first;
2431                 while( old_speed && new_speed && old_speed->equals(new_speed) ) {
2432                         old_speed = (FloatAuto *)old_speed->next;
2433                         new_speed = (FloatAuto *)new_speed->next;
2434                 }
2435                 Edit *old_edit = old_track->edits->first;
2436                 Edit *new_edit = new_track->edits->first;
2437                 for( ; old_edit && new_edit; old_edit=old_edit->next, new_edit=new_edit->next ) {
2438                         int64_t edit_start = old_edit->startproject, edit_end = edit_start + old_edit->length;
2439                         if( old_speed || new_speed ) {
2440                                 double orig_start = old_speeds->automation_integral(0, edit_start, PLAY_FORWARD);
2441                                 double orig_end   = old_speeds->automation_integral(0, edit_end, PLAY_FORWARD);
2442                                 edit_start = new_speeds->speed_position(orig_start);
2443                                 edit_end = new_speeds->speed_position(orig_end);
2444                                 result = 1;
2445                         }
2446                         new_edit->startproject = edit_start;
2447                         new_edit->length = edit_end - edit_start;
2448                 }
2449         }
2450         return result;
2451 }
2452
2453 void MWindow::speed_before()
2454 {
2455         if( !speed_edl ) {
2456                 speed_edl = new EDL;
2457                 speed_edl->create_objects();
2458         }
2459         speed_edl->copy_all(edl);
2460 }
2461
2462 int MWindow::speed_after(int done)
2463 {
2464         int result = 0;
2465         if( speed_edl ) {
2466                 if( done >= 0 )
2467                         result = normalize_speed(speed_edl, edl);
2468                 if( done != 0 ) {
2469                         speed_edl->remove_user();
2470                         speed_edl = 0;
2471                 }
2472         }
2473         return result;
2474 }
2475