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