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