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