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