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