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