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