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