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