a1e3d438a3af191c79754874cb2758bcd5d66ffc
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mwindowedit.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "asset.h"
22 #include "assets.h"
23 #include "awindowgui.h"
24 #include "awindow.h"
25 #include "bcsignals.h"
26 #include "cache.h"
27 #include "clip.h"
28 #include "clipedit.h"
29 #include "commercials.h"
30 #include "cplayback.h"
31 #include "ctimebar.h"
32 #include "cwindow.h"
33 #include "cwindowgui.h"
34 #include "bchash.h"
35 #include "edl.h"
36 #include "edlsession.h"
37 #include "filesystem.h"
38 #include "filexml.h"
39 #include "floatauto.h"
40 #include "floatautos.h"
41 #include "gwindow.h"
42 #include "gwindowgui.h"
43 #include "keyframe.h"
44 #include "keyframes.h"
45 #include "language.h"
46 #include "labels.h"
47 #include "levelwindow.h"
48 #include "localsession.h"
49 #include "mainclock.h"
50 #include "maincursor.h"
51 #include "mainerror.h"
52 #include "mainindexes.h"
53 #include "mainmenu.h"
54 #include "mainsession.h"
55 #include "mainundo.h"
56 #include "maskautos.h"
57 #include "mtimebar.h"
58 #include "mwindowgui.h"
59 #include "mwindow.h"
60 #include "panauto.h"
61 #include "patchbay.h"
62 #include "playbackengine.h"
63 #include "pluginset.h"
64 #include "recordlabel.h"
65 #include "samplescroll.h"
66 #include "trackcanvas.h"
67 #include "track.h"
68 #include "trackscroll.h"
69 #include "tracks.h"
70 #include "transition.h"
71 #include "transportque.h"
72 #include "units.h"
73 #include "undostackitem.h"
74 #include "vplayback.h"
75 #include "vwindow.h"
76 #include "vwindowgui.h"
77 #include "zoombar.h"
78 #include "zwindow.h"
79 #include "automation.h"
80 #include "maskautos.h"
81
82 #include <string.h>
83
84 void MWindow::add_audio_track_entry(int above, Track *dst)
85 {
86         undo->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
836         finish_modify_handles();
837 //printf("MWindow::modify_handles 1\n");
838         return 0;
839 }
840
841 int MWindow::modify_pluginhandles()
842 {
843         undo->update_undo_before();
844
845         edl->modify_pluginhandles(session->drag_start,
846                 session->drag_position,
847                 session->drag_handle,
848                 edl->session->edit_handle_mode[session->drag_button],
849                 edl->session->labels_follow_edits,
850                 edl->session->autos_follow_edits,
851                 session->trim_edits);
852
853         finish_modify_handles();
854
855         return 0;
856 }
857
858
859 // Common to edithandles and plugin handles
860 void MWindow::finish_modify_handles()
861 {
862         int edit_mode = edl->session->edit_handle_mode[session->drag_button];
863
864         if( (session->drag_handle == 1 && edit_mode != MOVE_NO_EDITS) ||
865                 (session->drag_handle == 0 && edit_mode == MOVE_ONE_EDIT) ) {
866 //printf("MWindow::finish_modify_handles %d\n", __LINE__);
867                 edl->local_session->set_selectionstart(session->drag_position);
868                 edl->local_session->set_selectionend(session->drag_position);
869         }
870         else
871         if( edit_mode != MOVE_NO_EDITS ) {
872 //printf("MWindow::finish_modify_handles %d\n", __LINE__);
873                 edl->local_session->set_selectionstart(session->drag_start);
874                 edl->local_session->set_selectionend(session->drag_start);
875         }
876
877 // clamp the selection to 0
878         if( edl->local_session->get_selectionstart(1) < 0 ) {
879                 edl->local_session->set_selectionstart(0);
880                 edl->local_session->set_selectionend(0);
881         }
882         undo->update_undo_after(_("drag handle"), LOAD_EDITS | LOAD_TIMEBAR);
883
884         save_backup();
885         restart_brender();
886         sync_parameters(CHANGE_EDL);
887         update_plugin_guis();
888         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
889 // label list can be modified
890         awindow->gui->async_update_assets();
891         cwindow->update(1, 0, 0, 0, 1);
892 }
893
894 void MWindow::match_output_size(Track *track)
895 {
896         undo->update_undo_before();
897         track->track_w = edl->session->output_w;
898         track->track_h = edl->session->output_h;
899         save_backup();
900         undo->update_undo_after(_("match output size"), LOAD_ALL);
901
902         restart_brender();
903         sync_parameters(CHANGE_EDL);
904 }
905
906
907 EDL *MWindow::selected_edits_to_clip(int packed,
908                 double *start_position, Track **start_track,
909                 int edit_labels, int edit_autos, int edit_plugins)
910 {
911         double start = DBL_MAX, end = DBL_MIN;
912         Track *first_track=0, *last_track = 0;
913         for( Track *track=edl->tracks->first; track; track=track->next ) {
914                 if( !track->record ) continue;
915                 int empty = 1;
916                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
917                         if( !edit->is_selected || edit->silence() ) continue;
918                         double edit_pos = track->from_units(edit->startproject);
919                         if( start > edit_pos ) start = edit_pos;
920                         if( end < (edit_pos+=edit->length) ) end = edit_pos;
921                         empty = 0;
922                 }
923                 if( empty ) continue;
924                 if( !first_track ) first_track = track;
925                 last_track = track;
926         }
927         if( start_position ) *start_position = start;
928         if( start_track ) *start_track = first_track;
929         if( !first_track ) return 0;
930         EDL *new_edl = new EDL();
931         new_edl->create_objects();
932         new_edl->copy_session(edl);
933         const char *text = _("new_edl edit");
934         new_edl->set_path(text);
935         strcpy(new_edl->local_session->clip_title, text);
936         strcpy(new_edl->local_session->clip_notes, text);
937         new_edl->session->video_tracks = 0;
938         new_edl->session->audio_tracks = 0;
939         for( Track *track=edl->tracks->first; track; track=track->next ) {
940                 if( !track->record ) continue;
941                 if( first_track ) {
942                         if( first_track != track ) continue;
943                         first_track = 0;
944                 }
945                 Track *new_track = 0;
946                 if( !packed )
947                         new_track = new_edl->add_new_track(track->data_type);
948                 int64_t start_pos = track->to_units(start, 0);
949                 int64_t end_pos = track->to_units(end, 0);
950                 int64_t startproject = 0;
951                 Edit *edit = track->edits->first;
952                 for( ; edit; edit=edit->next ) {
953                         if( !edit->is_selected || edit->silence() ) continue;
954                         if( edit->startproject < start_pos ) continue;
955                         if( edit->startproject >= end_pos ) break;
956                         int64_t edit_start_pos = edit->startproject;
957                         int64_t edit_end_pos = edit->startproject + edit->length;
958                         if( !new_track )
959                                 new_track = new_edl->add_new_track(track->data_type);
960                         int64_t edit_pos = edit_start_pos - start_pos;
961                         if( !packed ) {
962                                 if( 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                         }
970                         int64_t clip_start_pos = startproject;
971                         Edit *clip_edit = new Edit(new_edl, new_track);
972                         clip_edit->copy_from(edit);
973                         clip_edit->startproject = startproject;
974                         startproject += clip_edit->length;
975                         new_track->edits->append(clip_edit);
976                         if( edit_labels ) {
977                                 double edit_start = track->from_units(edit_start_pos);
978                                 double edit_end = track->from_units(edit_end_pos);
979                                 double clip_start = new_track->from_units(clip_start_pos);
980                                 Label *label = edl->labels->first;
981                                 for( ; label; label=label->next ) {
982                                         if( label->position < edit_start ) continue;
983                                         if( label->position >= edit_end ) break;
984                                         double clip_position = label->position - edit_start + clip_start;
985                                         Label *clip_label = new_edl->labels->first;
986                                         while( clip_label && clip_label->position<clip_position )
987                                                 clip_label = clip_label->next;
988                                         if( clip_label && clip_label->position == clip_position ) continue;
989                                         Label *new_label = new Label(new_edl,
990                                                 new_edl->labels, clip_position, label->textstr);
991                                         new_edl->labels->insert_before(clip_label, new_label);
992                                 }
993                         }
994                         if( edit_autos ) {
995                                 Automation *automation = track->automation;
996                                 Automation *new_automation = new_track->automation;
997                                 for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
998                                         Autos *autos = automation->autos[i];
999                                         if( !autos ) continue;
1000                                         Autos *new_autos = new_automation->autos[i];
1001                                         new_autos->default_auto->copy_from(autos->default_auto);
1002                                         Auto *aut0 = autos->first;
1003                                         for( ; aut0; aut0=aut0->next ) {
1004                                                 if( aut0->position < edit_start_pos ) continue;
1005                                                 if( aut0->position >= edit_end_pos ) break;
1006                                                 Auto *new_auto = new_autos->new_auto();
1007                                                 new_auto->copy_from(aut0);
1008                                                 int64_t clip_position = aut0->position - edit_start_pos + clip_start_pos;
1009                                                 new_auto->position = clip_position;
1010                                                 new_autos->append(new_auto);
1011                                         }
1012                                 }
1013                         }
1014                         if( edit_plugins ) {
1015                                 while( new_track->plugin_set.size() < track->plugin_set.size() )
1016                                         new_track->plugin_set.append(0);
1017                                 for( int i=0; i<track->plugin_set.total; ++i ) {
1018                                         PluginSet *plugin_set = track->plugin_set[i];
1019                                         if( !plugin_set ) continue;
1020                                         PluginSet *new_plugin_set = new_track->plugin_set[i];
1021                                         if( !new_plugin_set ) {
1022                                                 new_plugin_set = new PluginSet(new_edl, new_track);
1023                                                 new_track->plugin_set[i] = new_plugin_set;
1024                                         }
1025                                         Plugin *plugin = (Plugin*)plugin_set->first;
1026                                         int64_t startplugin = 0;
1027                                         for( ; plugin ; plugin=(Plugin*)plugin->next ) {
1028                                                 if( plugin->silence() ) continue;
1029                                                 int64_t plugin_start_pos = plugin->startproject;
1030                                                 int64_t plugin_end_pos = plugin_start_pos + plugin->length;
1031                                                 if( plugin_end_pos < start_pos ) continue;
1032                                                 if( plugin_start_pos > end_pos ) break;
1033                                                 if( plugin_start_pos < edit_start_pos )
1034                                                         plugin_start_pos = edit_start_pos;
1035                                                 if( plugin_end_pos > edit_end_pos )
1036                                                         plugin_end_pos = edit_end_pos;
1037                                                 if( plugin_start_pos >= plugin_end_pos ) continue;
1038                                                 int64_t plugin_pos = plugin_start_pos - start_pos;
1039                                                 if( plugin_pos > startplugin ) {
1040                                                         Plugin *silence = new Plugin(new_edl, new_track, "");
1041                                                         silence->startproject = startplugin;
1042                                                         silence->length = plugin_pos - startplugin;
1043                                                         new_plugin_set->append(silence);
1044                                                         startplugin = plugin_pos;
1045                                                 }
1046                                                 Plugin *new_plugin = new Plugin(new_edl, new_track, plugin->title);
1047                                                 new_plugin->copy_base(plugin);
1048                                                 new_plugin->startproject = startplugin;
1049                                                 new_plugin->length = plugin_end_pos - plugin_start_pos;
1050                                                 startplugin += new_plugin->length;
1051                                                 new_plugin_set->append(new_plugin);
1052                                                 KeyFrames *keyframes = plugin->keyframes;
1053                                                 KeyFrames *new_keyframes = new_plugin->keyframes;
1054                                                 new_keyframes->default_auto->copy_from(keyframes->default_auto);
1055                                                 new_keyframes->default_auto->position = startplugin;
1056                                                 KeyFrame *keyframe = (KeyFrame*)keyframes->first;
1057                                                 for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
1058                                                         if( keyframe->position < edit_start_pos ) continue;
1059                                                         if( keyframe->position >= edit_end_pos ) break;
1060                                                         KeyFrame *clip_keyframe = new KeyFrame(new_edl, new_keyframes);
1061                                                         clip_keyframe->copy_from(keyframe);
1062                                                         int64_t clip_position = keyframe->position - start_pos;
1063                                                         clip_keyframe->position = clip_position;
1064                                                         new_keyframes->append(clip_keyframe);
1065                                                 }
1066                                         }
1067                                 }
1068                         }
1069                 }
1070                 if( last_track == track ) break;
1071         }
1072         return new_edl;
1073 }
1074
1075 void MWindow::selected_edits_to_clipboard(int packed)
1076 {
1077         EDL *new_edl = selected_edits_to_clip(packed, 0, 0,
1078                 edl->session->labels_follow_edits,
1079                 edl->session->autos_follow_edits,
1080                 edl->session->plugins_follow_edits);
1081         if( !new_edl ) return;
1082         double length = new_edl->tracks->total_length();
1083         FileXML file;
1084         new_edl->copy(0, length, 1, &file, "", 1);
1085         const char *file_string = file.string();
1086         long file_length = strlen(file_string);
1087         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
1088         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
1089         new_edl->remove_user();
1090 }
1091
1092 void MWindow::delete_edit(Edit *edit, const char *msg, int collapse)
1093 {
1094         ArrayList<Edit*> edits;
1095         edits.append(edit);
1096         delete_edits(&edits, msg, collapse);
1097 }
1098
1099 void MWindow::delete_edits(ArrayList<Edit*> *edits, const char *msg, int collapse)
1100 {
1101         if( !edits->size() ) return;
1102         undo->update_undo_before();
1103         edl->delete_edits(edits, collapse);
1104         save_backup();
1105         undo->update_undo_after(msg, LOAD_EDITS);
1106
1107         restart_brender();
1108         cwindow->refresh_frame(CHANGE_EDL);
1109         update_plugin_guis();
1110         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1111 }
1112
1113 void MWindow::delete_edits(int collapse)
1114 {
1115         ArrayList<Edit*> edits;
1116         edl->tracks->get_selected_edits(&edits);
1117         delete_edits(&edits,_("del edit"), collapse);
1118 }
1119
1120 // collapse - delete from timeline, not collapse replace with silence
1121 // packed - omit unselected from selection, unpacked - replace unselected with silence
1122 void MWindow::cut_selected_edits(int collapse, int packed)
1123 {
1124         selected_edits_to_clipboard(packed);
1125         ArrayList<Edit*> edits;
1126         edl->tracks->get_selected_edits(&edits);
1127         delete_edits(&edits, _("cut edit"), collapse);
1128 }
1129
1130
1131 void MWindow::move_edits(ArrayList<Edit*> *edits,
1132                 Track *track,
1133                 double position,
1134                 int behaviour)
1135 {
1136         undo->update_undo_before();
1137
1138         edl->tracks->move_edits(edits,
1139                 track,
1140                 position,
1141                 edl->session->labels_follow_edits,
1142                 edl->session->plugins_follow_edits,
1143                 edl->session->autos_follow_edits,
1144                 behaviour);
1145
1146         save_backup();
1147         undo->update_undo_after(_("move edit"), LOAD_ALL);
1148
1149         restart_brender();
1150         cwindow->refresh_frame(CHANGE_EDL);
1151
1152         update_plugin_guis();
1153         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1154 }
1155
1156 void MWindow::move_group(EDL *group, Track *first_track, double position)
1157 {
1158         undo->update_undo_before();
1159         if( edl->session->labels_follow_edits ) {
1160                 ArrayList<Edit *>edits;
1161                 edl->tracks->get_selected_edits(&edits);
1162                 double dist = position - session->drag_group_position;
1163                 edl->move_edit_labels(&edits, dist);
1164         }
1165         for( Track *track=edl->tracks->first; track; track=track->next ) {
1166                 if( !track->record ) continue;
1167                 Edit *edit = track->edits->first;
1168                 for( Edit *next=0; edit; edit=next ) {
1169                         next = edit->next;
1170                         if( !edit->is_selected ) continue;
1171                         edit->is_selected = 0;
1172                         edit->group_id = 0;
1173                         int64_t start = edit->startproject;
1174                         int64_t end = start + edit->length;
1175                         track->clear(start, end, 1, 0,
1176                                 edl->session->plugins_follow_edits,
1177                                 edl->session->autos_follow_edits, 0);
1178                         track->paste_silence(start, end,
1179                                 edl->session->plugins_follow_edits,
1180                                         edl->session->autos_follow_edits);
1181                         next = track->edits->first;
1182                 }
1183         }
1184         Track *src = group->tracks->first;
1185         for( Track *track=first_track; track && src; track=track->next ) {
1186                 if( !track->record ) continue;
1187                 int64_t pos = track->to_units(position, 0);
1188                 for( Edit *edit=src->edits->first; edit; edit=edit->next ) {
1189                         if( edit->silence() ) continue;
1190                         int64_t start = pos + edit->startproject;
1191                         int64_t end = start + edit->length;
1192                         track->edits->clear(start, end);
1193                         Edit *dst = track->edits->insert_new_edit(start);
1194                         dst->copy_from(edit);
1195                         dst->startproject = start;
1196                         dst->is_selected = 1;
1197                         while( (dst=dst->next) != 0 )
1198                                 dst->startproject += edit->length;
1199                 }
1200                 if( edl->session->autos_follow_edits ) {
1201                         for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
1202                                 Autos *src_autos = src->automation->autos[i];
1203                                 if( !src_autos ) continue;
1204                                 Autos *autos = track->automation->autos[i];
1205                                 for( Auto *aut0=src_autos->first; aut0; aut0=aut0->next ) {
1206                                         int64_t position = pos + aut0->position;
1207                                         autos->insert_auto(position, aut0);
1208                                 }
1209                         }
1210                 }
1211                 if( edl->session->plugins_follow_edits ) {
1212                         for( int i=0; i<src->plugin_set.size(); ++i ) {
1213                                 PluginSet *plugin_set = src->plugin_set[i];
1214                                 if( !plugin_set ) continue;
1215                                 while( track->plugin_set.size() < i )
1216                                         track->plugin_set.append(0);
1217                                 PluginSet *dst_plugin_set = track->plugin_set[i];
1218                                 if( !dst_plugin_set ) {
1219                                         dst_plugin_set = new PluginSet(edl, track);
1220                                         track->plugin_set[i] = dst_plugin_set;
1221                                 }
1222                                 Plugin *plugin = (Plugin *)plugin_set->first;
1223                                 for( ; plugin; plugin=(Plugin *)plugin->next ) {
1224                                         int64_t start = pos + plugin->startproject;
1225                                         int64_t end = start + plugin->length;
1226                                         dst_plugin_set->clear(start, end, 1);
1227                                         Plugin *dst = dst_plugin_set->insert_plugin(
1228                                                 plugin->title, start, end-start,
1229                                                 plugin->plugin_type, &plugin->shared_location,
1230                                                 (KeyFrame*)plugin->keyframes->default_auto, 0);
1231                                         KeyFrame *keyframe = (KeyFrame*)plugin->keyframes->first;
1232                                         for( ; keyframe; keyframe=(KeyFrame*)keyframe->next ) {
1233                                                 int64_t position = pos + keyframe->position;
1234                                                 dst->keyframes->insert_auto(position, keyframe);
1235                                         }
1236                                 }
1237                         }
1238                 }
1239                 track->optimize();
1240                 src = src->next;
1241         }
1242         edl->tracks->clear_selected_edits();
1243
1244         save_backup();
1245         undo->update_undo_after(_("move group"), LOAD_ALL);
1246         restart_brender();
1247         cwindow->refresh_frame(CHANGE_EDL);
1248
1249         update_plugin_guis();
1250         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1251 }
1252
1253 void MWindow::move_effect(Plugin *plugin, Track *track, int64_t position)
1254 {
1255         undo->update_undo_before();
1256         edl->tracks->move_effect(plugin, track, position);
1257         save_backup();
1258         undo->update_undo_after(_("paste effect"), LOAD_ALL);
1259
1260         restart_brender();
1261         cwindow->refresh_frame(CHANGE_EDL);
1262         update_plugin_guis();
1263         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1264 }
1265
1266 void MWindow::move_effect(Plugin *plugin, PluginSet *plugin_set, int64_t position)
1267 {
1268         undo->update_undo_before();
1269         edl->tracks->move_effect(plugin, plugin_set, position);
1270         save_backup();
1271         undo->update_undo_after(_("move effect"), LOAD_ALL);
1272
1273         restart_brender();
1274         cwindow->refresh_frame(CHANGE_EDL);
1275         update_plugin_guis();
1276         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1277 }
1278
1279 void MWindow::move_plugins_up(PluginSet *plugin_set)
1280 {
1281
1282         undo->update_undo_before();
1283         plugin_set->track->move_plugins_up(plugin_set);
1284
1285         save_backup();
1286         undo->update_undo_after(_("move effect up"), LOAD_ALL);
1287         restart_brender();
1288         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1289         sync_parameters(CHANGE_EDL);
1290 }
1291
1292 void MWindow::move_plugins_down(PluginSet *plugin_set)
1293 {
1294         undo->update_undo_before();
1295
1296         plugin_set->track->move_plugins_down(plugin_set);
1297
1298         save_backup();
1299         undo->update_undo_after(_("move effect down"), LOAD_ALL);
1300         restart_brender();
1301         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1302         sync_parameters(CHANGE_EDL);
1303 }
1304
1305 void MWindow::move_track_down(Track *track)
1306 {
1307         undo->update_undo_before();
1308         edl->tracks->move_track_down(track);
1309         save_backup();
1310         undo->update_undo_after(_("move track down"), LOAD_ALL);
1311
1312         restart_brender();
1313         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1314         sync_parameters(CHANGE_EDL);
1315         save_backup();
1316 }
1317
1318 void MWindow::move_tracks_down()
1319 {
1320         undo->update_undo_before();
1321         edl->tracks->move_tracks_down();
1322         save_backup();
1323         undo->update_undo_after(_("move tracks down"), LOAD_ALL);
1324
1325         restart_brender();
1326         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1327         sync_parameters(CHANGE_EDL);
1328         save_backup();
1329 }
1330
1331 void MWindow::move_track_up(Track *track)
1332 {
1333         undo->update_undo_before();
1334         edl->tracks->move_track_up(track);
1335         save_backup();
1336         undo->update_undo_after(_("move track up"), LOAD_ALL);
1337         restart_brender();
1338         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1339         sync_parameters(CHANGE_EDL);
1340         save_backup();
1341 }
1342
1343 void MWindow::move_tracks_up()
1344 {
1345         undo->update_undo_before();
1346         edl->tracks->move_tracks_up();
1347         save_backup();
1348         undo->update_undo_after(_("move tracks up"), LOAD_ALL);
1349         restart_brender();
1350         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1351         sync_parameters(CHANGE_EDL);
1352 }
1353
1354
1355 void MWindow::mute_selection()
1356 {
1357         double start = edl->local_session->get_selectionstart();
1358         double end = edl->local_session->get_selectionend();
1359         if( start != end ) {
1360                 undo->update_undo_before();
1361                 edl->clear(start, end, 0,
1362                         edl->session->plugins_follow_edits,
1363                         edl->session->autos_follow_edits);
1364                 edl->local_session->set_selectionend(end);
1365                 edl->local_session->set_selectionstart(start);
1366                 edl->paste_silence(start, end, 0,
1367                         edl->session->plugins_follow_edits,
1368                         edl->session->autos_follow_edits);
1369
1370                 save_backup();
1371                 undo->update_undo_after(_("mute"), LOAD_EDITS);
1372
1373                 restart_brender();
1374                 update_plugin_guis();
1375                 gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
1376                 cwindow->refresh_frame(CHANGE_EDL);
1377         }
1378 }
1379
1380
1381 void MWindow::overwrite(EDL *source, int all)
1382 {
1383         FileXML file;
1384
1385         LocalSession *src = source->local_session;
1386         double src_start = all ? 0 :
1387                 src->inpoint_valid() ? src->get_inpoint() :
1388                 src->outpoint_valid() ? 0 :
1389                         src->get_selectionstart();
1390         double src_end = all ? source->tracks->total_length() :
1391                 src->outpoint_valid() ? src->get_outpoint() :
1392                 src->inpoint_valid() ? source->tracks->total_length() :
1393                         src->get_selectionend();
1394         double overwrite_len = src_end - src_start;
1395         double dst_start = edl->local_session->get_selectionstart();
1396         double dst_len = edl->local_session->get_selectionend() - dst_start;
1397
1398         undo->update_undo_before();
1399         if( !EQUIV(dst_len, 0) && (dst_len < overwrite_len) ) {
1400 // in/out points or selection present and shorter than overwrite range
1401 // shorten the copy range
1402                 overwrite_len = dst_len;
1403         }
1404
1405         source->copy(src_start, src_start + overwrite_len, 0, &file, "", 1);
1406
1407 // HACK around paste_edl get_start/endselection on its own
1408 // so we need to clear only when not using both io points
1409 // FIXME: need to write simple overwrite_edl to be used for overwrite function
1410         if( edl->local_session->get_inpoint() < 0 ||
1411                 edl->local_session->get_outpoint() < 0 )
1412                 edl->clear(dst_start, dst_start + overwrite_len, 0, 0, 0);
1413
1414         paste(dst_start, dst_start + overwrite_len, &file, 0, 0, 0, 0, 0);
1415
1416         edl->local_session->set_selectionstart(dst_start + overwrite_len);
1417         edl->local_session->set_selectionend(dst_start + overwrite_len);
1418
1419         save_backup();
1420         undo->update_undo_after(_("overwrite"), LOAD_EDITS);
1421
1422         restart_brender();
1423         update_plugin_guis();
1424         gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
1425         sync_parameters(CHANGE_EDL);
1426 }
1427
1428 // For splice and overwrite
1429 int MWindow::paste(double start, double end, FileXML *file,
1430         int edit_labels, int edit_plugins, int edit_autos,
1431         Track *first_track, int overwrite)
1432 {
1433         clear(0);
1434
1435 // Want to insert with assets shared with the master EDL.
1436         insert(start, file,
1437                 edit_labels, edit_plugins, edit_autos,
1438                 edl, first_track, overwrite);
1439
1440         return 0;
1441 }
1442
1443 // For editing using insertion point
1444 void MWindow::paste()
1445 {
1446         paste(edl->local_session->get_selectionstart(), 0, 1, 0);
1447 }
1448
1449 void MWindow::paste(double start, Track *first_track, int clear_selection, int overwrite)
1450 {
1451         //double end = edl->local_session->get_selectionend();
1452         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1453
1454         if( len ) {
1455                 char *string = new char[len];
1456                 undo->update_undo_before();
1457                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1458                 FileXML file;
1459                 file.read_from_string(string);
1460                 if( clear_selection ) clear(0);
1461
1462                 insert(start, &file,
1463                         edl->session->labels_follow_edits,
1464                         edl->session->plugins_follow_edits,
1465                         edl->session->autos_follow_edits,
1466                         0, first_track, overwrite);
1467
1468                 edl->optimize();
1469                 delete [] string;
1470
1471                 save_backup();
1472
1473                 undo->update_undo_after(_("paste"), LOAD_EDITS | LOAD_TIMEBAR);
1474                 restart_brender();
1475                 update_plugin_guis();
1476                 gui->update(1, FORCE_REDRAW, 1, 1, 0, 1, 0);
1477                 awindow->gui->async_update_assets();
1478                 sync_parameters(CHANGE_EDL);
1479         }
1480
1481 }
1482
1483 int MWindow::paste_assets(double position, Track *dest_track, int overwrite)
1484 {
1485         int result = 0;
1486         undo->update_undo_before();
1487
1488         if( session->drag_assets->total ) {
1489                 load_assets(session->drag_assets,
1490                         position, LOADMODE_PASTE, dest_track, 0,
1491                         edl->session->labels_follow_edits,
1492                         edl->session->plugins_follow_edits,
1493                         edl->session->autos_follow_edits,
1494                         overwrite);
1495                 result = 1;
1496         }
1497
1498         if( session->drag_clips->total ) {
1499                 paste_edls(session->drag_clips,
1500                         LOADMODE_PASTE, dest_track, position,
1501                         edl->session->labels_follow_edits,
1502                         edl->session->plugins_follow_edits,
1503                         edl->session->autos_follow_edits,
1504                         overwrite);
1505                 result = 1;
1506         }
1507
1508         save_backup();
1509
1510         undo->update_undo_after(_("paste assets"), LOAD_EDITS);
1511         restart_brender();
1512         gui->update(1, FORCE_REDRAW, 1, 0, 0, 1, 0);
1513         sync_parameters(CHANGE_EDL);
1514         return result;
1515 }
1516
1517 void MWindow::load_assets(ArrayList<Indexable*> *new_assets,
1518         double position, int load_mode, Track *first_track, RecordLabels *labels,
1519         int edit_labels, int edit_plugins, int edit_autos, int overwrite)
1520 {
1521         if( load_mode == LOADMODE_RESOURCESONLY )
1522                 load_mode = LOADMODE_ASSETSONLY;
1523 const int debug = 0;
1524 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1525         if( position < 0 ) position = edl->local_session->get_selectionstart();
1526
1527         ArrayList<EDL*> new_edls;
1528         for( int i=0; i<new_assets->total; ++i ) {
1529                 Indexable *indexable = new_assets->get(i);
1530                 if( indexable->is_asset ) {
1531                         remove_asset_from_caches((Asset*)indexable);
1532                 }
1533                 EDL *new_edl = new EDL;
1534                 new_edl->create_objects();
1535                 new_edl->copy_session(edl);
1536                 if( !indexable->is_asset ) {
1537                         EDL *nested_edl = (EDL*)indexable;
1538                         new_edl->create_nested(nested_edl);
1539                         new_edl->set_path(indexable->path);
1540                 }
1541                 else {
1542                         Asset *asset = (Asset*)indexable;
1543                         asset_to_edl(new_edl, asset);
1544                 }
1545                 new_edls.append(new_edl);
1546
1547                 if( labels ) {
1548                         for( RecordLabel *label=labels->first; label; label=label->next ) {
1549                                 new_edl->labels->toggle_label(label->position, label->position);
1550                         }
1551                 }
1552         }
1553 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1554
1555         paste_edls(&new_edls, load_mode, first_track, position,
1556                 edit_labels, edit_plugins, edit_autos, overwrite);
1557 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1558
1559         save_backup();
1560         for( int i=0; i<new_edls.size(); ++i )
1561                 new_edls.get(i)->Garbage::remove_user();
1562
1563 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1564 }
1565
1566 int MWindow::paste_automation()
1567 {
1568         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1569
1570         if( len ) {
1571                 undo->update_undo_before();
1572                 speed_before();
1573                 char *string = new char[len];
1574                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1575                 FileXML file;
1576                 file.read_from_string(string);
1577                 delete [] string;
1578                 double start = edl->local_session->get_selectionstart();
1579                 double end = edl->local_session->get_selectionend();
1580                 edl->tracks->clear_automation(start, end);
1581                 edl->tracks->paste_automation(start, &file, 0, 1,
1582                         edl->session->typeless_keyframes);
1583                 int changed_edl = speed_after(1);
1584                 save_backup();
1585                 undo->update_undo_after(_("paste keyframes"),
1586                         !changed_edl ? LOAD_AUTOMATION :
1587                                 LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
1588                 update_gui(changed_edl);
1589         }
1590
1591         return 0;
1592 }
1593
1594 int MWindow::paste_default_keyframe()
1595 {
1596         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1597
1598         if( len ) {
1599                 undo->update_undo_before();
1600                 speed_before();
1601                 char *string = new char[len];
1602                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1603                 FileXML file;
1604                 file.read_from_string(string);
1605                 delete [] string;
1606                 double start = edl->local_session->get_selectionstart();
1607                 edl->tracks->paste_automation(start, &file, 1, 0,
1608                         edl->session->typeless_keyframes);
1609 //              edl->tracks->paste_default_keyframe(&file);
1610                 int changed_edl = speed_after(1);
1611                 undo->update_undo_after(_("paste default keyframe"),
1612                         !changed_edl ? LOAD_AUTOMATION :
1613                                 LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
1614                 save_backup();
1615                 update_gui(changed_edl);
1616         }
1617
1618         return 0;
1619 }
1620
1621
1622 // Insert edls with project deletion and index file generation.
1623 int MWindow::paste_edls(ArrayList<EDL*> *new_edls, int load_mode,
1624         Track *first_track, double current_position,
1625         int edit_labels, int edit_plugins, int edit_autos,
1626         int overwrite)
1627 {
1628
1629         ArrayList<Track*> destination_tracks;
1630         int need_new_tracks = 0;
1631
1632 //PRINT_TRACE
1633         if( !new_edls->total ) return 0;
1634
1635 //PRINT_TRACE
1636 //      double original_length = edl->tracks->total_length();
1637 //      double original_preview_end = edl->local_session->preview_end;
1638 //PRINT_TRACE
1639
1640 // Delete current project
1641         if( load_mode == LOADMODE_REPLACE ||
1642             load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1643                 reset_caches();
1644                 edl->save_defaults(defaults);
1645                 hide_plugins();
1646                 edl->Garbage::remove_user();
1647                 edl = new EDL;
1648                 edl->create_objects();
1649                 edl->copy_session(new_edls->values[0]);
1650                 edl->copy_mixers(new_edls->values[0]);
1651                 gui->mainmenu->update_toggles(0);
1652                 gui->unlock_window();
1653                 gwindow->gui->update_toggles(1);
1654                 gui->lock_window("MWindow::paste_edls");
1655
1656 // Insert labels for certain modes constitutively
1657                 edit_labels = 1;
1658                 edit_plugins = 1;
1659                 edit_autos = 1;
1660 // Force reset of preview
1661 //              original_length = 0;
1662 //              original_preview_end = -1;
1663         }
1664
1665
1666 //PRINT_TRACE
1667
1668 // Create new tracks in master EDL
1669         if( load_mode == LOADMODE_REPLACE ||
1670             load_mode == LOADMODE_REPLACE_CONCATENATE ||
1671             load_mode == LOADMODE_NEW_TRACKS ) {
1672
1673                 need_new_tracks = 1;
1674                 for( int i=0; i<new_edls->total; ++i ) {
1675                         EDL *new_edl = new_edls->values[i];
1676                         for( Track *current=new_edl->tracks->first; current; current=NEXT ) {
1677                                 switch( current->data_type ) {
1678                                 case TRACK_VIDEO:
1679                                         edl->tracks->add_video_track(0, 0);
1680                                         if( current->draw ) edl->tracks->last->draw = 1;
1681                                         break;
1682                                 case TRACK_AUDIO:
1683                                         edl->tracks->add_audio_track(0, 0);
1684                                         break;
1685                                 case TRACK_SUBTITLE:
1686                                         edl->tracks->add_subttl_track(0, 0);
1687                                         break;
1688                                 default:
1689                                         continue;
1690                                 }
1691                                 destination_tracks.append(edl->tracks->last);
1692                         }
1693
1694 // Base track count on first EDL only for concatenation
1695                         if( load_mode == LOADMODE_REPLACE_CONCATENATE ) break;
1696                 }
1697                 edl->session->highlighted_track = edl->tracks->total() - 1;
1698         }
1699         else
1700 // Recycle existing tracks of master EDL
1701         if( load_mode == LOADMODE_CONCATENATE ||
1702             load_mode == LOADMODE_PASTE ||
1703             load_mode == LOADMODE_NESTED ) {
1704 //PRINT_TRACE
1705
1706 // The point of this is to shift forward labels after the selection so they can
1707 // then be shifted back to their original locations without recursively
1708 // shifting back every paste.
1709                 if( (load_mode == LOADMODE_PASTE || load_mode == LOADMODE_NESTED) &&
1710                     edl->session->labels_follow_edits )
1711                         edl->labels->clear(edl->local_session->get_selectionstart(),
1712                                            edl->local_session->get_selectionend(), 1);
1713
1714                 Track *current = first_track ? first_track : edl->tracks->first;
1715                 for( ; current; current=NEXT ) {
1716                         if( current->record ) {
1717                                 destination_tracks.append(current);
1718                         }
1719                 }
1720 //PRINT_TRACE
1721
1722         }
1723 //PRINT_TRACE
1724         int destination_track = 0;
1725         double *paste_position = new double[destination_tracks.total];
1726
1727 // Iterate through the edls
1728         for( int i=0; i<new_edls->total; ++i ) {
1729
1730                 EDL *new_edl = new_edls->values[i];
1731                 double edl_length = new_edl->local_session->clipboard_length ?
1732                         new_edl->local_session->clipboard_length :
1733                         new_edl->tracks->total_length();
1734 // printf("MWindow::paste_edls 2 %f %f\n",
1735 // new_edl->local_session->clipboard_length,
1736 // new_edl->tracks->total_length());
1737 // new_edl->dump();
1738 //PRINT_TRACE
1739
1740 // Convert EDL to master rates
1741                 new_edl->resample(new_edl->session->sample_rate,
1742                         edl->session->sample_rate,
1743                         TRACK_AUDIO);
1744                 new_edl->resample(new_edl->session->frame_rate,
1745                         edl->session->frame_rate,
1746                         TRACK_VIDEO);
1747 //PRINT_TRACE
1748 // Add assets and prepare index files
1749                 for( Asset *new_asset=new_edl->assets->first;
1750                      new_asset; new_asset=new_asset->next ) {
1751                         mainindexes->add_next_asset(0, new_asset);
1752                 }
1753 // Capture index file status from mainindex test
1754                 edl->update_assets(new_edl);
1755 //PRINT_TRACE
1756 // Get starting point of insertion.  Need this to paste labels.
1757                 switch( load_mode ) {
1758                 case LOADMODE_REPLACE:
1759                 case LOADMODE_NEW_TRACKS:
1760                         current_position = 0;
1761                         break;
1762
1763                 case LOADMODE_CONCATENATE:
1764                 case LOADMODE_REPLACE_CONCATENATE:
1765                         destination_track = 0;
1766                         if( destination_tracks.total )
1767                                 current_position = destination_tracks.values[0]->get_length();
1768                         else
1769                                 current_position = 0;
1770                         break;
1771
1772                 case LOADMODE_PASTE:
1773                 case LOADMODE_NESTED:
1774                         destination_track = 0;
1775                         if( i == 0 ) {
1776                                 for( int j=0; j<destination_tracks.total; ++j ) {
1777                                         paste_position[j] = (current_position >= 0) ?
1778                                                 current_position :
1779                                                 edl->local_session->get_selectionstart();
1780                                 }
1781                         }
1782                         break;
1783
1784                 case LOADMODE_RESOURCESONLY:
1785                         edl->add_clip(new_edl);
1786                         break;
1787                 }
1788 //PRINT_TRACE
1789 // Insert edl
1790                 if( load_mode != LOADMODE_RESOURCESONLY &&
1791                     load_mode != LOADMODE_ASSETSONLY ) {
1792 // Insert labels
1793 //printf("MWindow::paste_edls %f %f\n", current_position, edl_length);
1794                         if( load_mode == LOADMODE_PASTE ||
1795                             load_mode == LOADMODE_NESTED )
1796                                 edl->labels->insert_labels(new_edl->labels,
1797                                         destination_tracks.total ? paste_position[0] : 0.0,
1798                                         edl_length,
1799                                         edit_labels);
1800                         else
1801                                 edl->labels->insert_labels(new_edl->labels,
1802                                         current_position,
1803                                         edl_length,
1804                                         edit_labels);
1805 //PRINT_TRACE
1806                         double total_length = new_edl->tracks->total_length();
1807                         for( Track *new_track=new_edl->tracks->first;
1808                              new_track; new_track=new_track->next ) {
1809 // Get destination track of same type as new_track
1810                                 for( int k = 0;
1811                                      k < destination_tracks.total &&
1812                                      destination_tracks.values[destination_track]->data_type != new_track->data_type;
1813                                      ++k, ++destination_track ) {
1814                                         if( destination_track >= destination_tracks.total - 1 )
1815                                                 destination_track = 0;
1816                                 }
1817
1818 // Insert data into destination track
1819                                 if( destination_track < destination_tracks.total &&
1820                                     destination_tracks.values[destination_track]->data_type == new_track->data_type ) {
1821                                         Track *track = destination_tracks.values[destination_track];
1822
1823 // Replace default keyframes if first EDL and new tracks were created.
1824 // This means data copied from one track and pasted to another won't retain
1825 // the camera position unless it's a keyframe.  If it did, previous data in the
1826 // track might get unknowingly corrupted.  Ideally we would detect when differing
1827 // default keyframes existed and create discrete keyframes for both.
1828                                         int replace_default = (i == 0) && need_new_tracks;
1829
1830 //printf("MWindow::paste_edls 1 %d\n", replace_default);
1831 // Insert new track at current position
1832                                         switch( load_mode ) {
1833                                         case LOADMODE_REPLACE_CONCATENATE:
1834                                         case LOADMODE_CONCATENATE:
1835                                                 current_position = track->get_length();
1836                                                 break;
1837
1838                                         case LOADMODE_PASTE:
1839                                         case LOADMODE_NESTED:
1840                                                 current_position = paste_position[destination_track];
1841                                                 paste_position[destination_track] += new_track->get_length();
1842                                                 break;
1843                                         }
1844                                         if( overwrite ) {
1845                                                 double length = overwrite >= 0 ?
1846                                                         new_track->get_length() : total_length;
1847                                                 track->clear(current_position,
1848                                                         current_position + length,
1849                                                         1, // edit edits
1850                                                         edit_labels, edit_plugins, edit_autos,
1851                                                         0); // trim edits
1852                                         }
1853 //PRINT_TRACE
1854                                         track->insert_track(new_track, current_position, replace_default,
1855                                                 edit_plugins, edit_autos, edl_length);
1856 //PRINT_TRACE
1857                                 }
1858
1859 // Get next destination track
1860                                 destination_track++;
1861                                 if( destination_track >= destination_tracks.total )
1862                                         destination_track = 0;
1863                         }
1864                 }
1865
1866                 if( load_mode == LOADMODE_PASTE ||
1867                     load_mode == LOADMODE_NESTED )
1868                         current_position += edl_length;
1869         }
1870
1871
1872 // Move loading of clips and vwindow to the end - this fixes some
1873 // strange issue, for index not being shown
1874 // Assume any paste operation from the same EDL won't contain any clips.
1875 // If it did it would duplicate every clip here.
1876         for( int i=0; i<new_edls->total; ++i ) {
1877                 EDL *new_edl = new_edls->get(i);
1878
1879                 for( int j=0; j<new_edl->clips.size(); ++j ) {
1880                         edl->add_clip(new_edl->clips[j]);
1881                 }
1882                 for( int j=0; j<new_edl->nested_edls.size(); ++j ) {
1883                         edl->nested_edls.get_nested(new_edl->nested_edls[j]);
1884                 }
1885
1886                 if( new_edl->total_vwindow_edls() ) {
1887 //                      if( edl->vwindow_edl )
1888 //                              edl->vwindow_edl->Garbage::remove_user();
1889 //                      edl->vwindow_edl = new EDL(edl);
1890 //                      edl->vwindow_edl->create_objects();
1891 //                      edl->vwindow_edl->copy_all(new_edl->vwindow_edl);
1892
1893                         for( int j=0; j<new_edl->total_vwindow_edls(); ++j ) {
1894                                 EDL *vwindow_edl = new EDL(edl);
1895                                 vwindow_edl->create_objects();
1896                                 vwindow_edl->copy_all(new_edl->get_vwindow_edl(j));
1897                                 edl->append_vwindow_edl(vwindow_edl, 0);
1898                         }
1899                 }
1900         }
1901
1902         if( paste_position ) delete [] paste_position;
1903
1904 // This is already done in load_filenames and everything else that uses paste_edls
1905 //      update_project(load_mode);
1906
1907 // Fix preview range
1908 //      if( EQUIV(original_length, original_preview_end) )
1909 //      {
1910 //              edl->local_session->preview_end = edl->tracks->total_length();
1911 //      }
1912
1913 // Start examining next batch of index files
1914         mainindexes->start_build();
1915
1916 // Don't save a backup after loading since the loaded file is on disk already.
1917 //PRINT_TRACE
1918         return 0;
1919 }
1920
1921 void MWindow::paste_silence()
1922 {
1923         double start = edl->local_session->get_selectionstart();
1924         double end = edl->local_session->get_selectionend();
1925         if( EQUIV(start, end) ) {
1926                 if( edl->session->frame_rate > 0 )
1927                         end += 1./edl->session->frame_rate;
1928         }
1929         undo->update_undo_before(_("silence"), this);
1930         edl->paste_silence(start, end,
1931                 edl->session->labels_follow_edits,
1932                 edl->session->plugins_follow_edits,
1933                 edl->session->autos_follow_edits);
1934         edl->optimize();
1935         save_backup();
1936         undo->update_undo_after(_("silence"), LOAD_EDITS | LOAD_TIMEBAR);
1937
1938         update_plugin_guis();
1939         restart_brender();
1940         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
1941         cwindow->update(1, 0, 0, 0, 1);
1942         cwindow->refresh_frame(CHANGE_EDL);
1943 }
1944
1945 void MWindow::detach_transition(Transition *transition)
1946 {
1947         undo->update_undo_before();
1948         hide_plugin(transition, 1);
1949         int is_video = (transition->edit->track->data_type == TRACK_VIDEO);
1950         transition->edit->detach_transition();
1951         save_backup();
1952         undo->update_undo_after(_("detach transition"), LOAD_ALL);
1953
1954         if( is_video ) restart_brender();
1955         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1956         sync_parameters(CHANGE_EDL);
1957 }
1958
1959 void MWindow::detach_transitions()
1960 {
1961         gui->lock_window("MWindow::detach_transitions 1");
1962
1963         undo->update_undo_before();
1964         double start = edl->local_session->get_selectionstart();
1965         double end = edl->local_session->get_selectionend();
1966         edl->tracks->clear_transitions(start, end);
1967
1968         save_backup();
1969         undo->update_undo_after(_("detach transitions"), LOAD_EDITS);
1970
1971         sync_parameters(CHANGE_EDL);
1972         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1973         gui->unlock_window();
1974 }
1975
1976 void MWindow::paste_transition()
1977 {
1978 // Only the first transition gets dropped.
1979         PluginServer *server = session->drag_pluginservers->values[0];
1980
1981         undo->update_undo_before();
1982         edl->tracks->paste_transition(server, session->edit_highlighted);
1983         save_backup();
1984         undo->update_undo_after(_("transition"), LOAD_EDITS);
1985
1986         if( server->video ) restart_brender();
1987         sync_parameters(CHANGE_ALL);
1988 }
1989
1990 void MWindow::paste_transitions(int track_type, char *title)
1991 {
1992         gui->lock_window("MWindow::detach_transitions 1");
1993
1994         undo->update_undo_before();
1995         double start = edl->local_session->get_selectionstart();
1996         double end = edl->local_session->get_selectionend();
1997         edl->tracks->paste_transitions(start, end, track_type, title);
1998         save_backup();
1999         undo->update_undo_after(_("attach transitions"), LOAD_EDITS);
2000
2001         sync_parameters(CHANGE_EDL);
2002         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2003         gui->unlock_window();
2004 }
2005
2006 void MWindow::paste_transition_cwindow(Track *dest_track)
2007 {
2008         PluginServer *server = session->drag_pluginservers->values[0];
2009         undo->update_undo_before();
2010         edl->tracks->paste_video_transition(server, 1);
2011         save_backup();
2012         undo->update_undo_after(_("transition"), LOAD_EDITS);
2013         restart_brender();
2014         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2015         sync_parameters(CHANGE_ALL);
2016 }
2017
2018 void MWindow::paste_audio_transition()
2019 {
2020         PluginServer *server = scan_plugindb(edl->session->default_atransition,
2021                 TRACK_AUDIO);
2022         if( !server ) {
2023                 char string[BCTEXTLEN];
2024                 sprintf(string, _("No default transition %s found."), edl->session->default_atransition);
2025                 gui->show_message(string);
2026                 return;
2027         }
2028
2029         undo->update_undo_before();
2030         edl->tracks->paste_audio_transition(server);
2031         save_backup();
2032         undo->update_undo_after(_("transition"), LOAD_EDITS);
2033
2034         sync_parameters(CHANGE_EDL);
2035         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2036 }
2037
2038 void MWindow::paste_video_transition()
2039 {
2040         PluginServer *server = scan_plugindb(edl->session->default_vtransition,
2041                 TRACK_VIDEO);
2042         if( !server ) {
2043                 char string[BCTEXTLEN];
2044                 sprintf(string, _("No default transition %s found."), edl->session->default_vtransition);
2045                 gui->show_message(string);
2046                 return;
2047         }
2048
2049         undo->update_undo_before();
2050
2051         edl->tracks->paste_video_transition(server);
2052         save_backup();
2053         undo->update_undo_after(_("transition"), LOAD_EDITS);
2054
2055         sync_parameters(CHANGE_EDL);
2056         restart_brender();
2057         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2058 }
2059
2060 void MWindow::shuffle_edits()
2061 {
2062         gui->lock_window("MWindow::shuffle_edits 1");
2063
2064         undo->update_undo_before();
2065         double start = edl->local_session->get_selectionstart();
2066         double end = edl->local_session->get_selectionend();
2067
2068         edl->tracks->shuffle_edits(start, end);
2069
2070         save_backup();
2071         undo->update_undo_after(_("shuffle edits"), LOAD_EDITS | LOAD_TIMEBAR);
2072
2073         sync_parameters(CHANGE_EDL);
2074         restart_brender();
2075         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
2076         gui->unlock_window();
2077 }
2078
2079 void MWindow::reverse_edits()
2080 {
2081         gui->lock_window("MWindow::reverse_edits 1");
2082
2083         undo->update_undo_before();
2084         double start = edl->local_session->get_selectionstart();
2085         double end = edl->local_session->get_selectionend();
2086
2087         edl->tracks->reverse_edits(start, end);
2088
2089         save_backup();
2090         undo->update_undo_after(_("reverse edits"), LOAD_EDITS | LOAD_TIMEBAR);
2091
2092         sync_parameters(CHANGE_EDL);
2093         restart_brender();
2094         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
2095         gui->unlock_window();
2096 }
2097
2098 void MWindow::align_edits()
2099 {
2100         gui->lock_window("MWindow::align_edits 1");
2101
2102         undo->update_undo_before();
2103         double start = edl->local_session->get_selectionstart();
2104         double end = edl->local_session->get_selectionend();
2105
2106         edl->tracks->align_edits(start, end);
2107
2108         save_backup();
2109         undo->update_undo_after(_("align edits"), LOAD_EDITS | LOAD_TIMEBAR);
2110
2111         sync_parameters(CHANGE_EDL);
2112         restart_brender();
2113         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
2114         gui->unlock_window();
2115 }
2116
2117 void MWindow::set_edit_length(double length)
2118 {
2119         gui->lock_window("MWindow::set_edit_length 1");
2120
2121         undo->update_undo_before();
2122         double start = edl->local_session->get_selectionstart();
2123         double end = edl->local_session->get_selectionend();
2124
2125         edl->tracks->set_edit_length(start, end, length);
2126
2127         save_backup();
2128         undo->update_undo_after(_("edit length"), LOAD_EDITS | LOAD_TIMEBAR);
2129
2130         sync_parameters(CHANGE_EDL);
2131         restart_brender();
2132         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
2133         gui->unlock_window();
2134 }
2135
2136
2137 void MWindow::set_transition_length(Transition *transition, double length)
2138 {
2139         gui->lock_window("MWindow::set_transition_length 1");
2140
2141         undo->update_undo_before();
2142         //double start = edl->local_session->get_selectionstart();
2143         //double end = edl->local_session->get_selectionend();
2144
2145         edl->tracks->set_transition_length(transition, length);
2146
2147         save_backup();
2148         undo->update_undo_after(_("transition length"), LOAD_EDITS);
2149
2150         edl->session->default_transition_length = length;
2151         sync_parameters(CHANGE_PARAMS);
2152         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2153         gui->unlock_window();
2154 }
2155
2156 void MWindow::set_transition_length(double length)
2157 {
2158         gui->lock_window("MWindow::set_transition_length 2");
2159
2160         undo->update_undo_before();
2161         double start = edl->local_session->get_selectionstart();
2162         double end = edl->local_session->get_selectionend();
2163
2164         edl->tracks->set_transition_length(start, end, length);
2165
2166         save_backup();
2167         undo->update_undo_after(_("transition length"), LOAD_EDITS);
2168
2169         edl->session->default_transition_length = length;
2170         sync_parameters(CHANGE_PARAMS);
2171         restart_brender();
2172         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2173         gui->unlock_window();
2174 }
2175
2176
2177 void MWindow::redo_entry(BC_WindowBase *calling_window_gui)
2178 {
2179         calling_window_gui->unlock_window();
2180         stop_playback(0);
2181         if( undo->redo_load_flags() & LOAD_SESSION )
2182                 close_mixers();
2183
2184         cwindow->gui->lock_window("MWindow::redo_entry 1");
2185         for( int i=0; i<vwindows.size(); ++i ) {
2186                 if( vwindows.get(i)->is_running() ) {
2187                         if( calling_window_gui != vwindows.get(i)->gui ) {
2188                                 vwindows.get(i)->gui->lock_window("MWindow::redo_entry 2");
2189                         }
2190                 }
2191         }
2192         gui->lock_window("MWindow::redo_entry 3");
2193
2194         undo->redo();
2195
2196         save_backup();
2197         restart_brender();
2198         update_plugin_states();
2199         update_plugin_guis();
2200
2201         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 1);
2202         gui->update_proxy_toggle();
2203         gui->unlock_window();
2204         cwindow->update(1, 1, 1, 1, 1);
2205         cwindow->gui->unlock_window();
2206
2207         for( int i=0; i < vwindows.size(); ++i ) {
2208                 if( vwindows.get(i)->is_running() ) {
2209                         if( calling_window_gui != vwindows.get(i)->gui ) {
2210                                 vwindows.get(i)->gui->unlock_window();
2211                         }
2212                 }
2213         }
2214
2215         awindow->gui->async_update_assets();
2216
2217         cwindow->refresh_frame(CHANGE_ALL);
2218         calling_window_gui->lock_window("MWindow::redo_entry 4");
2219 }
2220
2221
2222 void MWindow::resize_track(Track *track, int w, int h)
2223 {
2224         undo->update_undo_before();
2225 // We have to move all maskpoints so they do not move in relation to image areas
2226         ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->translate_masks(
2227                 (w - track->track_w) / 2,
2228                 (h - track->track_h) / 2);
2229         track->track_w = w;
2230         track->track_h = h;
2231         undo->update_undo_after(_("resize track"), LOAD_ALL);
2232         save_backup();
2233
2234         restart_brender();
2235         sync_parameters(CHANGE_EDL);
2236 }
2237
2238
2239 void MWindow::set_inpoint(int is_mwindow)
2240 {
2241         undo->update_undo_before();
2242         edl->set_inpoint(edl->local_session->get_selectionstart(1));
2243         save_backup();
2244         undo->update_undo_after(_("in point"), LOAD_TIMEBAR);
2245
2246         if( !is_mwindow ) {
2247                 gui->lock_window("MWindow::set_inpoint 1");
2248         }
2249         gui->update_timebar(1);
2250         if( !is_mwindow ) {
2251                 gui->unlock_window();
2252         }
2253
2254         if( is_mwindow ) {
2255                 cwindow->gui->lock_window("MWindow::set_inpoint 2");
2256         }
2257         cwindow->gui->timebar->update(1);
2258         if( is_mwindow ) {
2259                 cwindow->gui->unlock_window();
2260         }
2261 }
2262
2263 void MWindow::set_outpoint(int is_mwindow)
2264 {
2265         undo->update_undo_before();
2266         edl->set_outpoint(edl->local_session->get_selectionend(1));
2267         save_backup();
2268         undo->update_undo_after(_("out point"), LOAD_TIMEBAR);
2269
2270         if( !is_mwindow ) {
2271                 gui->lock_window("MWindow::set_outpoint 1");
2272         }
2273         gui->update_timebar(1);
2274         if( !is_mwindow ) {
2275                 gui->unlock_window();
2276         }
2277
2278         if( is_mwindow ) {
2279                 cwindow->gui->lock_window("MWindow::set_outpoint 2");
2280         }
2281         cwindow->gui->timebar->update(1);
2282         if( is_mwindow ) {
2283                 cwindow->gui->unlock_window();
2284         }
2285 }
2286
2287 void MWindow::unset_inoutpoint(int is_mwindow)
2288 {
2289         undo->update_undo_before();
2290         edl->unset_inoutpoint();
2291         save_backup();
2292         undo->update_undo_after(_("clear in/out"), LOAD_TIMEBAR);
2293
2294         if( !is_mwindow ) {
2295                 gui->lock_window("MWindow::unset_inoutpoint 1");
2296         }
2297         gui->update_timebar(1);
2298         if( !is_mwindow ) {
2299                 gui->unlock_window();
2300         }
2301
2302         if( is_mwindow ) {
2303                 cwindow->gui->lock_window("MWindow::unset_inoutpoint 2");
2304         }
2305         cwindow->gui->timebar->update(1);
2306         if( is_mwindow ) {
2307                 cwindow->gui->unlock_window();
2308         }
2309 }
2310
2311 void MWindow::splice(EDL *source, int all)
2312 {
2313         FileXML file;
2314         LocalSession *src = source->local_session;
2315
2316         undo->update_undo_before();
2317         double source_start = all ? 0 :
2318                 src->inpoint_valid() ? src->get_inpoint() :
2319                 src->outpoint_valid() ? 0 : src->get_selectionstart();
2320         double source_end = all ? source->tracks->total_length() :
2321                 src->outpoint_valid() ? src->get_outpoint() :
2322                 src->inpoint_valid() ? source->tracks->total_length() :
2323                         src->get_selectionend();
2324         source->copy(source_start, source_end, 1, &file, "", 1);
2325 //file.dump();
2326         double start = edl->local_session->get_selectionstart();
2327         //double end = edl->local_session->get_selectionend();
2328
2329         paste(start, start, &file,
2330                 edl->session->labels_follow_edits,
2331                 edl->session->plugins_follow_edits,
2332                 edl->session->autos_follow_edits,
2333                 0, 0);
2334
2335 // Position at end of clip
2336         edl->local_session->set_selectionstart(start + source_end - source_start);
2337         edl->local_session->set_selectionend(start + source_end - source_start);
2338
2339         save_backup();
2340         undo->update_undo_after(_("splice"), LOAD_EDITS | LOAD_TIMEBAR);
2341         update_plugin_guis();
2342         restart_brender();
2343         gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
2344         sync_parameters(CHANGE_EDL);
2345 }
2346
2347 void MWindow::save_clip(EDL *new_edl, const char *txt)
2348 {
2349         new_edl->local_session->set_selectionstart(0);
2350         new_edl->local_session->set_selectionend(0);
2351         sprintf(new_edl->local_session->clip_title, _("Clip %d"),
2352                 session->clip_number++);
2353         char duration[BCTEXTLEN];
2354         Units::totext(duration, new_edl->tracks->total_length(),
2355                 new_edl->session->time_format,
2356                 new_edl->session->sample_rate,
2357                 new_edl->session->frame_rate,
2358                 new_edl->session->frames_per_foot);
2359
2360         Track *track = new_edl->tracks->first;
2361         const char *path = edl->path;
2362         for( ; (!path || !*path) && track; track=track->next ) {
2363                 if( !track->record ) continue;
2364                 Edit *edit = track->edits->first;
2365                 if( !edit ) continue;
2366                 Indexable *indexable = edit->get_source();
2367                 if( !indexable ) continue;
2368                 path = indexable->path;
2369         }
2370
2371         time_t now;  time(&now);
2372         struct tm dtm;   localtime_r(&now, &dtm);
2373         char *cp = new_edl->local_session->clip_notes;
2374         int n, sz = sizeof(new_edl->local_session->clip_notes)-1;
2375         if( txt && *txt ) {
2376                 n = snprintf(cp, sz, "%s", txt);
2377                 cp += n;  sz -= n;
2378         }
2379         n = snprintf(cp, sz,
2380                 "%02d/%02d/%02d %02d:%02d:%02d,  +%s\n",
2381                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
2382                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec, duration);
2383         cp += n;  sz -= n;
2384         if( path && *path ) {
2385                 FileSystem fs;
2386                 char title[BCTEXTLEN];
2387                 fs.extract_name(title, path);
2388                 n = snprintf(cp, sz, "%s", title);
2389                 cp += n;  sz -= n;
2390         }
2391         cp[n] = 0;
2392         sprintf(new_edl->local_session->clip_icon,
2393                 "clip_%02d%02d%02d-%02d%02d%02d.png",
2394                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
2395                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
2396         new_edl->folder_no = AW_CLIP_FOLDER;
2397         edl->update_assets(new_edl);
2398         int cur_x, cur_y;
2399         gui->get_abs_cursor(cur_x, cur_y, 0);
2400         gui->unlock_window();
2401
2402         awindow->clip_edit->create_clip(new_edl, cur_x, cur_y);
2403         new_edl->remove_user();
2404
2405         gui->lock_window("MWindow::save_clip");
2406         save_backup();
2407 }
2408
2409 void MWindow::to_clip(EDL *edl, const char *txt, int all)
2410 {
2411         FileXML file;
2412         LocalSession *src = edl->local_session;
2413
2414         gui->lock_window("MWindow::to_clip 1");
2415         double start = all ? 0 :
2416                 src->inpoint_valid() ? src->get_inpoint() :
2417                 src->outpoint_valid() ? 0 : src->get_selectionstart();
2418         double end = all ? edl->tracks->total_length() :
2419                 src->outpoint_valid() ? src->get_outpoint() :
2420                 src->inpoint_valid() ? edl->tracks->total_length() :
2421                         src->get_selectionend();
2422         if( EQUIV(end, start) ) {
2423                 start = 0;
2424                 end = edl->tracks->total_length();
2425         }
2426
2427 // Don't copy all since we don't want the clips twice.
2428         edl->copy(start, end, 0, &file, "", 1);
2429
2430         EDL *new_edl = new EDL(edl);
2431         new_edl->create_objects();
2432         new_edl->load_xml(&file, LOAD_ALL);
2433         save_clip(new_edl, txt);
2434         gui->unlock_window();
2435 }
2436
2437 int MWindow::toggle_label(int is_mwindow)
2438 {
2439         double position1, position2;
2440         undo->update_undo_before();
2441
2442         if( cwindow->playback_engine->is_playing_back ) {
2443                 position1 = position2 =
2444                         cwindow->playback_engine->get_tracking_position();
2445         }
2446         else {
2447                 position1 = edl->local_session->get_selectionstart(1);
2448                 position2 = edl->local_session->get_selectionend(1);
2449         }
2450
2451         position1 = edl->align_to_frame(position1, 0);
2452         position2 = edl->align_to_frame(position2, 0);
2453
2454 //printf("MWindow::toggle_label 1\n");
2455
2456         edl->labels->toggle_label(position1, position2);
2457         save_backup();
2458
2459         if( !is_mwindow ) {
2460                 gui->lock_window("MWindow::toggle_label 1");
2461         }
2462         gui->update_timebar(0);
2463         gui->activate_timeline();
2464         gui->flush();
2465         if( !is_mwindow ) {
2466                 gui->unlock_window();
2467         }
2468
2469         if( is_mwindow ) {
2470                 cwindow->gui->lock_window("MWindow::toggle_label 2");
2471         }
2472         cwindow->gui->timebar->update(1);
2473         if( is_mwindow ) {
2474                 cwindow->gui->unlock_window();
2475         }
2476
2477         awindow->gui->async_update_assets();
2478
2479         undo->update_undo_after(_("label"), LOAD_TIMEBAR);
2480         return 0;
2481 }
2482
2483 void MWindow::trim_selection()
2484 {
2485         undo->update_undo_before();
2486
2487
2488         edl->trim_selection(edl->local_session->get_selectionstart(),
2489                 edl->local_session->get_selectionend(),
2490                 edl->session->labels_follow_edits,
2491                 edl->session->plugins_follow_edits,
2492                 edl->session->autos_follow_edits);
2493
2494         save_backup();
2495         undo->update_undo_after(_("trim selection"), LOAD_EDITS | LOAD_TIMEBAR);
2496         update_plugin_guis();
2497         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2498         cwindow->update(1, 0, 0, 0, 1);
2499         awindow->gui->async_update_assets();
2500         restart_brender();
2501         cwindow->refresh_frame(CHANGE_EDL);
2502 }
2503
2504
2505 void MWindow::undo_entry(BC_WindowBase *calling_window_gui)
2506 {
2507         calling_window_gui->unlock_window();
2508         stop_playback(0);
2509         if( undo->undo_load_flags() & LOAD_SESSION )
2510                 close_mixers();
2511
2512         cwindow->gui->lock_window("MWindow::undo_entry 1");
2513         for( int i=0; i<vwindows.size(); ++i ) {
2514                 if( vwindows.get(i)->is_running() ) {
2515                         if( calling_window_gui != vwindows.get(i)->gui ) {
2516                                 vwindows.get(i)->gui->lock_window("MWindow::undo_entry 4");
2517                         }
2518                 }
2519         }
2520         gui->lock_window("MWindow::undo_entry 2");
2521
2522         undo->undo();
2523
2524         save_backup();
2525         restart_brender();
2526         update_plugin_states();
2527         update_plugin_guis();
2528
2529         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 1);
2530         gui->update_proxy_toggle();
2531         gui->unlock_window();
2532         cwindow->update(1, 1, 1, 1, 1);
2533         cwindow->gui->unlock_window();
2534
2535         for( int i=0; i<vwindows.size(); ++i ) {
2536                 if( vwindows.get(i)->is_running() ) {
2537                         if( calling_window_gui != vwindows.get(i)->gui ) {
2538                                 vwindows.get(i)->gui->unlock_window();
2539                         }
2540                 }
2541         }
2542
2543         awindow->gui->async_update_assets();
2544
2545         cwindow->refresh_frame(CHANGE_ALL);
2546         calling_window_gui->lock_window("MWindow::undo_entry 4");
2547 }
2548
2549
2550 void MWindow::new_folder(const char *new_folder, int is_clips)
2551 {
2552         undo->update_undo_before();
2553         if( edl->new_folder(new_folder, is_clips) ) {
2554                 MainError::show_error(_("create new folder failed"));
2555         }
2556         undo->update_undo_after(_("new folder"), LOAD_ALL);
2557         awindow->gui->async_update_assets();
2558 }
2559
2560 void MWindow::delete_folder(char *folder)
2561 {
2562         undo->update_undo_before();
2563         if( edl->delete_folder(folder) < 0 ) {
2564                 MainError::show_error(_("delete folder failed"));
2565         }
2566         undo->update_undo_after(_("del folder"), LOAD_ALL);
2567         awindow->gui->async_update_assets();
2568 }
2569
2570 void MWindow::select_point(double position)
2571 {
2572         gui->unlock_window();
2573         gui->stop_drawing();
2574         cwindow->stop_playback(0);
2575         gui->lock_window("MWindow::select_point");
2576
2577         edl->local_session->set_selectionstart(position);
2578         edl->local_session->set_selectionend(position);
2579
2580 // Que the CWindow
2581         cwindow->update(1, 0, 0, 0, 1);
2582
2583         update_plugin_guis();
2584         gui->update_patchbay();
2585         gui->hide_cursor(0);
2586         gui->draw_cursor(0);
2587         gui->mainclock->update(edl->local_session->get_selectionstart(1));
2588         gui->zoombar->update();
2589         gui->update_timebar(0);
2590         gui->flash_canvas(0);
2591         gui->flush();
2592 }
2593
2594
2595
2596
2597 void MWindow::map_audio(int pattern)
2598 {
2599         undo->update_undo_before();
2600         remap_audio(pattern);
2601         undo->update_undo_after(
2602                 pattern == MWindow::AUDIO_1_TO_1 ? _("map 1:1") : _("map 5.1:2"),
2603                 LOAD_AUTOMATION);
2604         sync_parameters(CHANGE_PARAMS);
2605         gui->update(0, NORMAL_DRAW, 0, 0, 1, 0, 0);
2606 }
2607
2608 void MWindow::remap_audio(int pattern)
2609 {
2610         int current_channel = 0;
2611         int current_track = 0;
2612         for( Track *current=edl->tracks->first; current; current=NEXT ) {
2613                 if( current->data_type == TRACK_AUDIO &&
2614                         current->record ) {
2615                         Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
2616                         PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
2617
2618                         for( int i=0; i < MAXCHANNELS; ++i ) {
2619                                 pan_auto->values[i] = 0.0;
2620                         }
2621
2622                         if( pattern == MWindow::AUDIO_1_TO_1 ) {
2623                                 pan_auto->values[current_channel] = 1.0;
2624                         }
2625                         else
2626                         if( pattern == MWindow::AUDIO_5_1_TO_2 ) {
2627                                 switch( current_track ) {
2628                                 case 0:
2629                                 case 4:
2630                                         pan_auto->values[0] = 1;
2631                                         break;
2632                                 case 1:
2633                                 case 5:
2634                                         pan_auto->values[1] = 1;
2635                                         break;
2636                                 case 2:
2637                                 case 3:
2638                                         pan_auto->values[0] = 0.5;
2639                                         pan_auto->values[1] = 0.5;
2640                                         break;
2641                                 }
2642                         }
2643
2644                         BC_Pan::calculate_stick_position(edl->session->audio_channels,
2645                                 edl->session->achannel_positions, pan_auto->values,
2646                                 MAX_PAN, PAN_RADIUS, pan_auto->handle_x, pan_auto->handle_y);
2647
2648                         current_channel++;
2649                         current_track++;
2650                         if( current_channel >= edl->session->audio_channels )
2651                                 current_channel = 0;
2652                 }
2653         }
2654 }
2655
2656
2657 void MWindow::rescale_proxy(EDL *clip, int orig_scale, int new_scale)
2658 {
2659         edl->rescale_proxy(orig_scale, new_scale);
2660 }
2661
2662 void MWindow::add_proxy(int use_scaler,
2663         ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
2664 {
2665         edl->add_proxy(use_scaler, orig_assets, proxy_assets);
2666 }
2667
2668 void MWindow::cut_commercials()
2669 {
2670 #ifdef HAVE_COMMERCIAL
2671         undo->update_undo_before();
2672         commercials->scan_media();
2673         edl->optimize();
2674         save_backup();
2675         undo->update_undo_after(_("cut ads"), LOAD_EDITS | LOAD_TIMEBAR);
2676
2677         restart_brender();
2678         update_plugin_guis();
2679         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2680         cwindow->update(1, 0, 0, 0, 1);
2681         cwindow->refresh_frame(CHANGE_EDL);
2682 #endif
2683 }
2684
2685 int MWindow::normalize_speed(EDL *old_edl, EDL *new_edl)
2686 {
2687         int result = 0;
2688         Track *old_track = old_edl->tracks->first;
2689         Track *new_track = new_edl->tracks->first;
2690         for( ; old_track && new_track; old_track=old_track->next, new_track=new_track->next ) {
2691                 if( old_track->data_type != new_track->data_type ) continue;
2692                 FloatAutos *old_speeds = (FloatAutos *)old_track->automation->autos[AUTOMATION_SPEED];
2693                 FloatAutos *new_speeds = (FloatAutos *)new_track->automation->autos[AUTOMATION_SPEED];
2694                 if( !old_speeds || !new_speeds ) continue;
2695                 FloatAuto *old_speed = (FloatAuto *)old_speeds->first;
2696                 FloatAuto *new_speed = (FloatAuto *)new_speeds->first;
2697                 while( old_speed && new_speed && old_speed->equals(new_speed) ) {
2698                         old_speed = (FloatAuto *)old_speed->next;
2699                         new_speed = (FloatAuto *)new_speed->next;
2700                 }
2701                 Edit *old_edit = old_track->edits->first;
2702                 Edit *new_edit = new_track->edits->first;
2703                 for( ; old_edit && new_edit; old_edit=old_edit->next, new_edit=new_edit->next ) {
2704                         int64_t edit_start = old_edit->startproject, edit_end = edit_start + old_edit->length;
2705                         if( old_speed || new_speed ) {
2706                                 double orig_start = old_speeds->automation_integral(0, edit_start, PLAY_FORWARD);
2707                                 double orig_end   = old_speeds->automation_integral(0, edit_end, PLAY_FORWARD);
2708                                 edit_start = new_speeds->speed_position(orig_start);
2709                                 edit_end = new_speeds->speed_position(orig_end);
2710                                 result = 1;
2711                         }
2712                         new_edit->startproject = edit_start;
2713                         new_edit->length = edit_end - edit_start;
2714                 }
2715         }
2716         return result;
2717 }
2718
2719 void MWindow::speed_before()
2720 {
2721         if( !speed_edl ) {
2722                 speed_edl = new EDL;
2723                 speed_edl->create_objects();
2724         }
2725         speed_edl->copy_all(edl);
2726 }
2727
2728 int MWindow::speed_after(int done)
2729 {
2730         int result = 0;
2731         if( speed_edl ) {
2732                 if( done >= 0 )
2733                         result = normalize_speed(speed_edl, edl);
2734                 if( done != 0 ) {
2735                         speed_edl->remove_user();
2736                         speed_edl = 0;
2737                 }
2738         }
2739         return result;
2740 }
2741