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