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