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