awindowgui / mwindowedit / label rework
[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,
1157         int load_mode,
1158         Track *first_track,
1159         RecordLabels *labels,
1160         int edit_labels,
1161         int edit_plugins,
1162         int edit_autos,
1163         int overwrite)
1164 {
1165 const int debug = 0;
1166 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1167         if( position < 0 ) position = edl->local_session->get_selectionstart();
1168
1169         ArrayList<EDL*> new_edls;
1170         for( int i = 0; i < new_assets->total; i++ ) {
1171                 Indexable *indexable = new_assets->get(i);
1172                 if( indexable->is_asset ) {
1173                         remove_asset_from_caches((Asset*)indexable);
1174                 }
1175                 EDL *new_edl = new EDL;
1176                 new_edl->create_objects();
1177                 new_edl->copy_session(edl);
1178                 new_edls.append(new_edl);
1179
1180
1181                 if( indexable->is_asset ) {
1182 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1183 if( debug ) ((Asset*)indexable)->dump();
1184                         asset_to_edl(new_edl, (Asset*)indexable);
1185                 }
1186                 else
1187                         edl_to_nested(new_edl, (EDL*)indexable);
1188 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1189
1190
1191                 if( labels ) {
1192                         for( RecordLabel *label = labels->first; label; label = label->next ) {
1193                                 new_edl->labels->toggle_label(label->position, label->position);
1194                         }
1195                 }
1196         }
1197 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1198
1199         paste_edls(&new_edls, load_mode, first_track, position,
1200                 edit_labels, edit_plugins, edit_autos, overwrite);
1201 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1202
1203         save_backup();
1204         for( int i = 0; i < new_edls.size(); i++ )
1205                 new_edls.get(i)->Garbage::remove_user();
1206
1207 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1208 }
1209
1210 int MWindow::paste_automation()
1211 {
1212         int64_t len = gui->get_clipboard()->clipboard_len(SECONDARY_SELECTION);
1213
1214         if( len ) {
1215                 undo->update_undo_before();
1216                 char *string = new char[len + 1];
1217                 gui->get_clipboard()->from_clipboard(string,
1218                         len,
1219                         BC_PRIMARY_SELECTION);
1220                 FileXML file;
1221                 file.read_from_string(string);
1222
1223                 double start = edl->local_session->get_selectionstart();
1224                 double end = edl->local_session->get_selectionend();
1225                 edl->tracks->clear_automation(start, end);
1226                 edl->tracks->paste_automation(start, &file, 0, 1,
1227                         edl->session->typeless_keyframes);
1228                 save_backup();
1229                 undo->update_undo_after(_("paste keyframes"), LOAD_AUTOMATION);
1230                 delete [] string;
1231
1232                 restart_brender();
1233                 update_plugin_guis();
1234                 gui->draw_overlays(1);
1235                 sync_parameters(CHANGE_PARAMS);
1236                 gui->update_patchbay();
1237                 cwindow->update(1, 0, 0);
1238         }
1239
1240         return 0;
1241 }
1242
1243 int MWindow::paste_default_keyframe()
1244 {
1245         int64_t len = gui->get_clipboard()->clipboard_len(SECONDARY_SELECTION);
1246
1247         if( len ) {
1248                 undo->update_undo_before();
1249                 char *string = new char[len + 1];
1250                 gui->get_clipboard()->from_clipboard(string,
1251                         len,
1252                         BC_PRIMARY_SELECTION);
1253                 FileXML file;
1254                 file.read_from_string(string);
1255                 double start = edl->local_session->get_selectionstart();
1256                 edl->tracks->paste_automation(start, &file, 1, 0,
1257                         edl->session->typeless_keyframes);
1258 //              edl->tracks->paste_default_keyframe(&file);
1259                 undo->update_undo_after(_("paste default keyframe"), LOAD_AUTOMATION);
1260
1261                 restart_brender();
1262                 update_plugin_guis();
1263                 gui->draw_overlays(1);
1264                 sync_parameters(CHANGE_PARAMS);
1265                 gui->update_patchbay();
1266                 cwindow->update(1, 0, 0);
1267                 delete [] string;
1268                 save_backup();
1269         }
1270
1271         return 0;
1272 }
1273
1274
1275 // Insert edls with project deletion and index file generation.
1276 int MWindow::paste_edls(ArrayList<EDL*> *new_edls, int load_mode,
1277         Track *first_track, double current_position,
1278         int edit_labels, int edit_plugins, int edit_autos,
1279         int overwrite)
1280 {
1281
1282         ArrayList<Track*> destination_tracks;
1283         int need_new_tracks = 0;
1284
1285 //PRINT_TRACE
1286         if( !new_edls->total ) return 0;
1287
1288 //PRINT_TRACE
1289 //      double original_length = edl->tracks->total_playable_length();
1290 //      double original_preview_end = edl->local_session->preview_end;
1291 //PRINT_TRACE
1292
1293 // Delete current project
1294         if( load_mode == LOADMODE_REPLACE ||
1295             load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1296                 reset_caches();
1297                 edl->save_defaults(defaults);
1298                 hide_plugins();
1299                 edl->Garbage::remove_user();
1300                 edl = new EDL;
1301                 edl->create_objects();
1302                 edl->copy_session(new_edls->values[0]);
1303                 gui->mainmenu->update_toggles(0);
1304                 gui->unlock_window();
1305                 gwindow->gui->update_toggles(1);
1306                 gui->lock_window("MWindow::paste_edls");
1307
1308 // Insert labels for certain modes constitutively
1309                 edit_labels = 1;
1310                 edit_plugins = 1;
1311                 edit_autos = 1;
1312 // Force reset of preview
1313 //              original_length = 0;
1314 //              original_preview_end = -1;
1315         }
1316
1317
1318 //PRINT_TRACE
1319
1320 // Create new tracks in master EDL
1321         if( load_mode == LOADMODE_REPLACE ||
1322             load_mode == LOADMODE_REPLACE_CONCATENATE ||
1323             load_mode == LOADMODE_NEW_TRACKS ) {
1324
1325                 need_new_tracks = 1;
1326                 for( int i = 0; i < new_edls->total; i++ ) {
1327                         EDL *new_edl = new_edls->values[i];
1328                         for( Track *current = new_edl->tracks->first;
1329                                 current;
1330                                 current = NEXT ) {
1331                                 if( current->data_type == TRACK_VIDEO ) {
1332                                         edl->tracks->add_video_track(0, 0);
1333                                         if( current->draw ) edl->tracks->last->draw = 1;
1334                                         destination_tracks.append(edl->tracks->last);
1335                                 }
1336                                 else
1337                                 if( current->data_type == TRACK_AUDIO ) {
1338                                         edl->tracks->add_audio_track(0, 0);
1339                                         destination_tracks.append(edl->tracks->last);
1340                                 }
1341                                 else
1342                                 if( current->data_type == TRACK_SUBTITLE ) {
1343                                         edl->tracks->add_subttl_track(0, 0);
1344                                         destination_tracks.append(edl->tracks->last);
1345                                 }
1346                                 edl->session->highlighted_track = edl->tracks->total() - 1;
1347                         }
1348
1349 // Base track count on first EDL only for concatenation
1350                         if( load_mode == LOADMODE_REPLACE_CONCATENATE ) break;
1351                 }
1352
1353         }
1354         else
1355 // Recycle existing tracks of master EDL
1356         if( load_mode == LOADMODE_CONCATENATE ||
1357             load_mode == LOADMODE_PASTE ||
1358             load_mode == LOADMODE_NESTED ) {
1359 //PRINT_TRACE
1360
1361 // The point of this is to shift forward labels after the selection so they can
1362 // then be shifted back to their original locations without recursively
1363 // shifting back every paste.
1364                 if( (load_mode == LOADMODE_PASTE || load_mode == LOADMODE_NESTED) &&
1365                         edl->session->labels_follow_edits )
1366                         edl->labels->clear(edl->local_session->get_selectionstart(),
1367                                                 edl->local_session->get_selectionend(),
1368                                                 1);
1369
1370                 Track *current = first_track ? first_track : edl->tracks->first;
1371                 for( ; current; current = NEXT) {
1372                         if( current->record ) {
1373                                 destination_tracks.append(current);
1374                         }
1375                 }
1376 //PRINT_TRACE
1377
1378         }
1379 //PRINT_TRACE
1380         int destination_track = 0;
1381         double *paste_position = new double[destination_tracks.total];
1382
1383 // Iterate through the edls
1384         for( int i = 0; i < new_edls->total; i++ ) {
1385
1386                 EDL *new_edl = new_edls->values[i];
1387                 double edl_length = new_edl->local_session->clipboard_length ?
1388                         new_edl->local_session->clipboard_length :
1389                         new_edl->tracks->total_length();
1390 // printf("MWindow::paste_edls 2 %f %f\n",
1391 // new_edl->local_session->clipboard_length,
1392 // new_edl->tracks->total_length());
1393 // new_edl->dump();
1394 //PRINT_TRACE
1395
1396 // Convert EDL to master rates
1397                 new_edl->resample(new_edl->session->sample_rate,
1398                         edl->session->sample_rate,
1399                         TRACK_AUDIO);
1400                 new_edl->resample(new_edl->session->frame_rate,
1401                         edl->session->frame_rate,
1402                         TRACK_VIDEO);
1403 //PRINT_TRACE
1404 // Add assets and prepare index files
1405                 for( Asset *new_asset = new_edl->assets->first;
1406                      new_asset;
1407                      new_asset = new_asset->next ) {
1408                         mainindexes->add_next_asset(0, new_asset);
1409                 }
1410 // Capture index file status from mainindex test
1411                 edl->update_assets(new_edl);
1412 //PRINT_TRACE
1413 // Get starting point of insertion.  Need this to paste labels.
1414                 switch( load_mode ) {
1415                 case LOADMODE_REPLACE:
1416                 case LOADMODE_NEW_TRACKS:
1417                         current_position = 0;
1418                         break;
1419
1420                 case LOADMODE_CONCATENATE:
1421                 case LOADMODE_REPLACE_CONCATENATE:
1422                         destination_track = 0;
1423                         if( destination_tracks.total )
1424                                 current_position = destination_tracks.values[0]->get_length();
1425                         else
1426                                 current_position = 0;
1427                         break;
1428
1429                 case LOADMODE_PASTE:
1430                 case LOADMODE_NESTED:
1431                         destination_track = 0;
1432                         if( i == 0 ) {
1433                                 for( int j = 0; j < destination_tracks.total; j++ ) {
1434                                         paste_position[j] = (current_position >= 0) ?
1435                                                 current_position :
1436                                                 edl->local_session->get_selectionstart();
1437                                 }
1438                         }
1439                         break;
1440
1441                 case LOADMODE_RESOURCESONLY:
1442                         edl->add_clip(new_edl);
1443                         break;
1444                 }
1445 //PRINT_TRACE
1446 // Insert edl
1447                 if( load_mode != LOADMODE_RESOURCESONLY ) {
1448 // Insert labels
1449 //printf("MWindow::paste_edls %f %f\n", current_position, edl_length);
1450                         if( load_mode == LOADMODE_PASTE ||
1451                             load_mode == LOADMODE_NESTED )
1452                                 edl->labels->insert_labels(new_edl->labels,
1453                                         destination_tracks.total ? paste_position[0] : 0.0,
1454                                         edl_length,
1455                                         edit_labels);
1456                         else
1457                                 edl->labels->insert_labels(new_edl->labels,
1458                                         current_position,
1459                                         edl_length,
1460                                         edit_labels);
1461 //PRINT_TRACE
1462
1463                         for( Track *new_track = new_edl->tracks->first;
1464                              new_track;
1465                              new_track = new_track->next ) {
1466 // Get destination track of same type as new_track
1467                                 for( int k = 0;
1468                                         k < destination_tracks.total &&
1469                                         destination_tracks.values[destination_track]->data_type != new_track->data_type;
1470                                         k++, destination_track++ ) {
1471                                         if( destination_track >= destination_tracks.total - 1 )
1472                                                 destination_track = 0;
1473                                 }
1474
1475 // Insert data into destination track
1476                                 if( destination_track < destination_tracks.total &&
1477                                     destination_tracks.values[destination_track]->data_type == new_track->data_type ) {
1478                                         Track *track = destination_tracks.values[destination_track];
1479
1480 // Replace default keyframes if first EDL and new tracks were created.
1481 // This means data copied from one track and pasted to another won't retain
1482 // the camera position unless it's a keyframe.  If it did, previous data in the
1483 // track might get unknowingly corrupted.  Ideally we would detect when differing
1484 // default keyframes existed and create discrete keyframes for both.
1485                                         int replace_default = (i == 0) && need_new_tracks;
1486
1487 //printf("MWindow::paste_edls 1 %d\n", replace_default);
1488 // Insert new track at current position
1489                                         switch( load_mode ) {
1490                                         case LOADMODE_REPLACE_CONCATENATE:
1491                                         case LOADMODE_CONCATENATE:
1492                                                 current_position = track->get_length();
1493                                                 break;
1494
1495                                         case LOADMODE_PASTE:
1496                                         case LOADMODE_NESTED:
1497                                                 current_position = paste_position[destination_track];
1498                                                 paste_position[destination_track] += new_track->get_length();
1499                                                 break;
1500                                         }
1501                                         if( overwrite ) {
1502                                                 track->clear(current_position,
1503                                                         current_position + new_track->get_length(),
1504                                                         1, // edit edits
1505                                                         edit_labels, edit_plugins, edit_autos,
1506                                                         1, // convert units
1507                                                         0); // trim edits
1508                                         }
1509 //PRINT_TRACE
1510                                         track->insert_track(new_track, current_position, replace_default,
1511                                                 edit_plugins, edit_autos, edl_length);
1512 //PRINT_TRACE
1513                                 }
1514
1515 // Get next destination track
1516                                 destination_track++;
1517                                 if( destination_track >= destination_tracks.total )
1518                                         destination_track = 0;
1519                         }
1520                 }
1521
1522                 if( load_mode == LOADMODE_PASTE ||
1523                     load_mode == LOADMODE_NESTED )
1524                         current_position += edl_length;
1525         }
1526
1527
1528 // Move loading of clips and vwindow to the end - this fixes some
1529 // strange issue, for index not being shown
1530 // Assume any paste operation from the same EDL won't contain any clips.
1531 // If it did it would duplicate every clip here.
1532         for( int i = 0; i < new_edls->total; i++ ) {
1533                 EDL *new_edl = new_edls->values[i];
1534
1535                 for( int j = 0; j < new_edl->clips.total; j++ ) {
1536                         edl->add_clip(new_edl->clips.values[j]);
1537                 }
1538
1539                 if( new_edl->total_vwindow_edls() ) {
1540 //                      if( edl->vwindow_edl )
1541 //                              edl->vwindow_edl->Garbage::remove_user();
1542 //                      edl->vwindow_edl = new EDL(edl);
1543 //                      edl->vwindow_edl->create_objects();
1544 //                      edl->vwindow_edl->copy_all(new_edl->vwindow_edl);
1545
1546                         for( int j = 0; j < new_edl->total_vwindow_edls(); j++ ) {
1547                                 EDL *vwindow_edl = new EDL(edl);
1548                                 vwindow_edl->create_objects();
1549                                 vwindow_edl->copy_all(new_edl->get_vwindow_edl(j));
1550                                 edl->append_vwindow_edl(vwindow_edl, 0);
1551                         }
1552                 }
1553         }
1554
1555         if( paste_position ) delete [] paste_position;
1556
1557 // This is already done in load_filenames and everything else that uses paste_edls
1558 //      update_project(load_mode);
1559
1560 // Fix preview range
1561 //      if( EQUIV(original_length, original_preview_end) )
1562 //      {
1563 //              edl->local_session->preview_end = edl->tracks->total_playable_length();
1564 //      }
1565
1566 // Start examining next batch of index files
1567         mainindexes->start_build();
1568
1569 // Don't save a backup after loading since the loaded file is on disk already.
1570 //PRINT_TRACE
1571         return 0;
1572 }
1573
1574 void MWindow::paste_silence()
1575 {
1576         double start = edl->local_session->get_selectionstart();
1577         double end = edl->local_session->get_selectionend();
1578         undo->update_undo_before();
1579         edl->paste_silence(start, end,
1580                 edl->session->labels_follow_edits,
1581                 edl->session->plugins_follow_edits,
1582                 edl->session->autos_follow_edits);
1583         edl->optimize();
1584         save_backup();
1585         undo->update_undo_after(_("silence"), LOAD_EDITS | LOAD_TIMEBAR);
1586
1587         update_plugin_guis();
1588         restart_brender();
1589         gui->update(1, 2, 1, 1, 1, 1, 0);
1590         cwindow->update(1, 0, 0, 0, 1);
1591         cwindow->playback_engine->que->
1592                 send_command(CURRENT_FRAME, CHANGE_EDL, edl, 1);
1593 }
1594
1595 void MWindow::detach_transition(Transition *transition)
1596 {
1597         undo->update_undo_before();
1598         hide_plugin(transition, 1);
1599         int is_video = (transition->edit->track->data_type == TRACK_VIDEO);
1600         transition->edit->detach_transition();
1601         save_backup();
1602         undo->update_undo_after(_("detach transition"), LOAD_ALL);
1603
1604         if( is_video ) restart_brender();
1605         gui->update(0, 1, 0, 0, 0, 0, 0);
1606         sync_parameters(CHANGE_EDL);
1607 }
1608
1609 void MWindow::detach_transitions()
1610 {
1611         gui->lock_window("MWindow::detach_transitions 1");
1612
1613         undo->update_undo_before();
1614         double start = edl->local_session->get_selectionstart();
1615         double end = edl->local_session->get_selectionend();
1616         edl->tracks->clear_transitions(start, end);
1617
1618         save_backup();
1619         undo->update_undo_after(_("detach transitions"), LOAD_EDITS);
1620
1621         sync_parameters(CHANGE_EDL);
1622         gui->update(0, 1, 0, 0, 0, 0, 0);
1623         gui->unlock_window();
1624 }
1625
1626 void MWindow::paste_transition()
1627 {
1628 // Only the first transition gets dropped.
1629         PluginServer *server = session->drag_pluginservers->values[0];
1630
1631         undo->update_undo_before();
1632         if( server->audio )
1633                 strcpy(edl->session->default_atransition, server->title);
1634         else
1635                 strcpy(edl->session->default_vtransition, server->title);
1636
1637         edl->tracks->paste_transition(server, session->edit_highlighted);
1638         save_backup();
1639         undo->update_undo_after(_("transition"), LOAD_EDITS);
1640
1641         if( server->video ) restart_brender();
1642         sync_parameters(CHANGE_ALL);
1643 }
1644
1645 void MWindow::paste_transitions(int track_type, char *title)
1646 {
1647         gui->lock_window("MWindow::detach_transitions 1");
1648
1649         undo->update_undo_before();
1650         double start = edl->local_session->get_selectionstart();
1651         double end = edl->local_session->get_selectionend();
1652         edl->tracks->paste_transitions(start, end, track_type, title);
1653
1654         save_backup();
1655         undo->update_undo_after(_("attach transitions"), LOAD_EDITS);
1656
1657         sync_parameters(CHANGE_EDL);
1658         gui->update(0, 1, 0, 0, 0, 0, 0);
1659         gui->unlock_window();
1660 }
1661
1662 void MWindow::paste_transition_cwindow(Track *dest_track)
1663 {
1664         PluginServer *server = session->drag_pluginservers->values[0];
1665         undo->update_undo_before();
1666         edl->tracks->paste_video_transition(server, 1);
1667         save_backup();
1668         undo->update_undo_after(_("transition"), LOAD_EDITS);
1669         restart_brender();
1670         gui->update(0, 1, 0, 0, 0, 0, 0);
1671         sync_parameters(CHANGE_ALL);
1672 }
1673
1674 void MWindow::paste_audio_transition()
1675 {
1676         PluginServer *server = scan_plugindb(edl->session->default_atransition,
1677                 TRACK_AUDIO);
1678         if( !server ) {
1679                 char string[BCTEXTLEN];
1680                 sprintf(string, _("No default transition %s found."), edl->session->default_atransition);
1681                 gui->show_message(string);
1682                 return;
1683         }
1684
1685         undo->update_undo_before();
1686         edl->tracks->paste_audio_transition(server);
1687         save_backup();
1688         undo->update_undo_after(_("transition"), LOAD_EDITS);
1689
1690         sync_parameters(CHANGE_EDL);
1691         gui->update(0, 1, 0, 0, 0, 0, 0);
1692 }
1693
1694 void MWindow::paste_video_transition()
1695 {
1696         PluginServer *server = scan_plugindb(edl->session->default_vtransition,
1697                 TRACK_VIDEO);
1698         if( !server ) {
1699                 char string[BCTEXTLEN];
1700                 sprintf(string, _("No default transition %s found."), edl->session->default_vtransition);
1701                 gui->show_message(string);
1702                 return;
1703         }
1704
1705         undo->update_undo_before();
1706
1707         edl->tracks->paste_video_transition(server);
1708         save_backup();
1709         undo->update_undo_after(_("transition"), LOAD_EDITS);
1710
1711         sync_parameters(CHANGE_EDL);
1712         restart_brender();
1713         gui->update(0, 1, 0, 0, 0, 0, 0);
1714 }
1715
1716 void MWindow::shuffle_edits()
1717 {
1718         gui->lock_window("MWindow::shuffle_edits 1");
1719
1720         undo->update_undo_before();
1721         double start = edl->local_session->get_selectionstart();
1722         double end = edl->local_session->get_selectionend();
1723
1724         edl->tracks->shuffle_edits(start, end);
1725
1726         save_backup();
1727         undo->update_undo_after(_("shuffle edits"), LOAD_EDITS | LOAD_TIMEBAR);
1728
1729         sync_parameters(CHANGE_EDL);
1730         restart_brender();
1731         gui->update(0, 1, 1, 0, 0, 0, 0);
1732         gui->unlock_window();
1733 }
1734
1735 void MWindow::reverse_edits()
1736 {
1737         gui->lock_window("MWindow::reverse_edits 1");
1738
1739         undo->update_undo_before();
1740         double start = edl->local_session->get_selectionstart();
1741         double end = edl->local_session->get_selectionend();
1742
1743         edl->tracks->reverse_edits(start, end);
1744
1745         save_backup();
1746         undo->update_undo_after(_("reverse edits"), LOAD_EDITS | LOAD_TIMEBAR);
1747
1748         sync_parameters(CHANGE_EDL);
1749         restart_brender();
1750         gui->update(0, 1, 1, 0, 0, 0, 0);
1751         gui->unlock_window();
1752 }
1753
1754 void MWindow::align_edits()
1755 {
1756         gui->lock_window("MWindow::align_edits 1");
1757
1758         undo->update_undo_before();
1759         double start = edl->local_session->get_selectionstart();
1760         double end = edl->local_session->get_selectionend();
1761
1762         edl->tracks->align_edits(start, end);
1763
1764         save_backup();
1765         undo->update_undo_after(_("align edits"), LOAD_EDITS | LOAD_TIMEBAR);
1766
1767         sync_parameters(CHANGE_EDL);
1768         restart_brender();
1769         gui->update(0, 1, 1, 0, 0, 0, 0);
1770         gui->unlock_window();
1771 }
1772
1773 void MWindow::set_edit_length(double length)
1774 {
1775         gui->lock_window("MWindow::detach_transitions 1");
1776
1777         undo->update_undo_before();
1778         double start = edl->local_session->get_selectionstart();
1779         double end = edl->local_session->get_selectionend();
1780
1781         edl->tracks->set_edit_length(start, end, length);
1782
1783         save_backup();
1784         undo->update_undo_after(_("edit length"), LOAD_EDITS | LOAD_TIMEBAR);
1785
1786         sync_parameters(CHANGE_EDL);
1787         restart_brender();
1788         gui->update(0, 1, 1, 0, 0, 0, 0);
1789         gui->unlock_window();
1790 }
1791
1792
1793 void MWindow::set_transition_length(Transition *transition, double length)
1794 {
1795         gui->lock_window("MWindow::detach_transitions 1");
1796
1797         undo->update_undo_before();
1798         //double start = edl->local_session->get_selectionstart();
1799         //double end = edl->local_session->get_selectionend();
1800
1801         edl->tracks->set_transition_length(transition, length);
1802
1803         save_backup();
1804         undo->update_undo_after(_("transition length"), LOAD_EDITS);
1805
1806         edl->session->default_transition_length = length;
1807         sync_parameters(CHANGE_PARAMS);
1808         gui->update(0, 1, 0, 0, 0, 0, 0);
1809         gui->unlock_window();
1810 }
1811
1812 void MWindow::set_transition_length(double length)
1813 {
1814         gui->lock_window("MWindow::detach_transitions 1");
1815
1816         undo->update_undo_before();
1817         double start = edl->local_session->get_selectionstart();
1818         double end = edl->local_session->get_selectionend();
1819
1820         edl->tracks->set_transition_length(start, end, length);
1821
1822         save_backup();
1823         undo->update_undo_after(_("transition length"), LOAD_EDITS);
1824
1825         edl->session->default_transition_length = length;
1826         sync_parameters(CHANGE_PARAMS);
1827         restart_brender();
1828         gui->update(0, 1, 0, 0, 0, 0, 0);
1829         gui->unlock_window();
1830 }
1831
1832
1833 void MWindow::redo_entry(BC_WindowBase *calling_window_gui)
1834 {
1835
1836         calling_window_gui->unlock_window();
1837
1838         cwindow->playback_engine->que->
1839                 send_command(STOP, CHANGE_NONE, 0, 0);
1840         cwindow->playback_engine->interrupt_playback(0);
1841
1842         for( int i = 0; i < vwindows.size(); i++ ) {
1843                 if( vwindows.get(i)->is_running() ) {
1844                         vwindows.get(i)->playback_engine->que->
1845                                 send_command(STOP, CHANGE_NONE, 0, 0);
1846                         vwindows.get(i)->playback_engine->interrupt_playback(0);
1847                 }
1848         }
1849
1850         cwindow->gui->lock_window("MWindow::redo_entry");
1851         for( int i = 0; i < vwindows.size(); i++ ) {
1852                 if( vwindows.get(i)->is_running() ) {
1853                         if( calling_window_gui != vwindows.get(i)->gui ) {
1854                                 vwindows.get(i)->gui->lock_window("MWindow::redo_entry 2");
1855                         }
1856                 }
1857         }
1858         gui->lock_window();
1859
1860         undo->redo();
1861
1862         save_backup();
1863         update_plugin_states();
1864         update_plugin_guis();
1865         restart_brender();
1866         gui->update(1, 2, 1, 1, 1, 1, 1);
1867         cwindow->update(1, 1, 1, 1, 1);
1868
1869         if( calling_window_gui != cwindow->gui )
1870                 cwindow->gui->unlock_window();
1871         if( calling_window_gui != gui )
1872                 gui->unlock_window();
1873
1874
1875         for( int i = 0; i < vwindows.size(); i++ ) {
1876                 if( vwindows.get(i)->is_running() ) {
1877                         if( calling_window_gui != vwindows.get(i)->gui ) {
1878                                 vwindows.get(i)->gui->unlock_window();
1879                         }
1880                 }
1881         }
1882
1883         cwindow->playback_engine->que->
1884                 send_command(CURRENT_FRAME, CHANGE_ALL, edl, 1);
1885 }
1886
1887
1888 void MWindow::resize_track(Track *track, int w, int h)
1889 {
1890         undo->update_undo_before();
1891 // We have to move all maskpoints so they do not move in relation to image areas
1892         ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->translate_masks(
1893                 (w - track->track_w) / 2,
1894                 (h - track->track_h) / 2);
1895         track->track_w = w;
1896         track->track_h = h;
1897         undo->update_undo_after(_("resize track"), LOAD_ALL);
1898         save_backup();
1899
1900         restart_brender();
1901         sync_parameters(CHANGE_EDL);
1902 }
1903
1904
1905 void MWindow::set_inpoint(int is_mwindow)
1906 {
1907         undo->update_undo_before();
1908         edl->set_inpoint(edl->local_session->get_selectionstart(1));
1909         save_backup();
1910         undo->update_undo_after(_("in point"), LOAD_TIMEBAR);
1911
1912         if( !is_mwindow ) {
1913                 gui->lock_window("MWindow::set_inpoint 1");
1914         }
1915         gui->update_timebar(1);
1916         if( !is_mwindow ) {
1917                 gui->unlock_window();
1918         }
1919
1920         if( is_mwindow ) {
1921                 cwindow->gui->lock_window("MWindow::set_inpoint 2");
1922         }
1923         cwindow->gui->timebar->update(1);
1924         if( is_mwindow ) {
1925                 cwindow->gui->unlock_window();
1926         }
1927 }
1928
1929 void MWindow::set_outpoint(int is_mwindow)
1930 {
1931         undo->update_undo_before();
1932         edl->set_outpoint(edl->local_session->get_selectionend(1));
1933         save_backup();
1934         undo->update_undo_after(_("out point"), LOAD_TIMEBAR);
1935
1936         if( !is_mwindow ) {
1937                 gui->lock_window("MWindow::set_outpoint 1");
1938         }
1939         gui->update_timebar(1);
1940         if( !is_mwindow ) {
1941                 gui->unlock_window();
1942         }
1943
1944         if( is_mwindow ) {
1945                 cwindow->gui->lock_window("MWindow::set_outpoint 2");
1946         }
1947         cwindow->gui->timebar->update(1);
1948         if( is_mwindow ) {
1949                 cwindow->gui->unlock_window();
1950         }
1951 }
1952
1953 void MWindow::splice(EDL *source)
1954 {
1955         FileXML file;
1956
1957         undo->update_undo_before();
1958         double source_start = source->local_session->get_selectionstart();
1959         double source_end = source->local_session->get_selectionend();
1960         source->copy(source_start, source_end, 1, 0, 0, &file, "", 1);
1961 //file.dump();
1962         double start = edl->local_session->get_selectionstart();
1963         //double end = edl->local_session->get_selectionend();
1964
1965         paste(start, start, &file,
1966                 edl->session->labels_follow_edits,
1967                 edl->session->plugins_follow_edits,
1968                 edl->session->autos_follow_edits);
1969
1970 // Position at end of clip
1971         edl->local_session->set_selectionstart(start + source_end - source_start);
1972         edl->local_session->set_selectionend(start + source_end - source_start);
1973
1974         save_backup();
1975         undo->update_undo_after(_("splice"), LOAD_EDITS | LOAD_TIMEBAR);
1976         update_plugin_guis();
1977         restart_brender();
1978         gui->update(1, 1, 1, 1, 0, 1, 0);
1979         sync_parameters(CHANGE_EDL);
1980 }
1981
1982 void MWindow::save_clip(EDL *new_edl, const char *txt)
1983 {
1984         new_edl->local_session->set_selectionstart(0);
1985         new_edl->local_session->set_selectionend(0);
1986         sprintf(new_edl->local_session->clip_title, _("Clip %d"),
1987                 session->clip_number++);
1988         char duration[BCTEXTLEN];
1989         Units::totext(duration, new_edl->tracks->total_length(),
1990                 new_edl->session->time_format,
1991                 new_edl->session->sample_rate,
1992                 new_edl->session->frame_rate,
1993                 new_edl->session->frames_per_foot);
1994
1995         Track *track = new_edl->tracks->first;
1996         const char *path = edl->path;
1997         for( ; (!path || !*path) && track; track=track->next ) {
1998                 if( !track->record ) continue;
1999                 Edit *edit = track->edits->first;
2000                 if( !edit ) continue;
2001                 Indexable *indexable = edit->get_source();
2002                 if( !indexable ) continue;
2003                 path = indexable->path;
2004         }
2005
2006         time_t now;  time(&now);
2007         struct tm *tm = localtime(&now);
2008         char *cp = new_edl->local_session->clip_notes;
2009         int n, sz = sizeof(new_edl->local_session->clip_notes)-1;
2010         if( txt && *txt ) {
2011                 n = snprintf(cp, sz, "%s", txt);
2012                 cp += n;  sz -= n;
2013         }
2014         n = snprintf(cp, sz, 
2015                 "%02d/%02d/%02d %02d:%02d:%02d,  +%s\n",
2016                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, 
2017                 tm->tm_hour, tm->tm_min, tm->tm_sec, duration);
2018         cp += n;  sz -= n;
2019         if( path && *path ) {
2020                 FileSystem fs;
2021                 char title[BCTEXTLEN];
2022                 fs.extract_name(title, path);
2023                 n = snprintf(cp, sz, "%s", title);
2024                 cp += n;  sz -= n;
2025         }
2026         cp[n] = 0;
2027
2028         edl->update_assets(new_edl);
2029         gui->unlock_window();
2030
2031         awindow->clip_edit->create_clip(new_edl);
2032
2033         gui->lock_window("MWindow::save_clip");
2034         save_backup();
2035 }
2036
2037 void MWindow::to_clip(EDL *edl, const char *txt)
2038 {
2039         FileXML file;
2040         double start, end;
2041
2042         gui->lock_window("MWindow::to_clip 1");
2043         start = edl->local_session->get_selectionstart();
2044         end = edl->local_session->get_selectionend();
2045
2046         if( EQUIV(end, start) ) {
2047                 start = 0;
2048                 end = edl->tracks->total_length();
2049         }
2050
2051 // Don't copy all since we don't want the clips twice.
2052         edl->copy(start, end, 0, 0, 0, &file, "", 1);
2053
2054         EDL *new_edl = new EDL(edl);
2055         new_edl->create_objects();
2056         new_edl->load_xml(&file, LOAD_ALL);
2057         save_clip(new_edl, txt);
2058         gui->unlock_window();
2059 }
2060
2061 int MWindow::toggle_label(int is_mwindow)
2062 {
2063         double position1, position2;
2064         undo->update_undo_before();
2065
2066         if( cwindow->playback_engine->is_playing_back ) {
2067                 position1 = position2 =
2068                         cwindow->playback_engine->get_tracking_position();
2069         }
2070         else {
2071                 position1 = edl->local_session->get_selectionstart(1);
2072                 position2 = edl->local_session->get_selectionend(1);
2073         }
2074
2075         position1 = edl->align_to_frame(position1, 0);
2076         position2 = edl->align_to_frame(position2, 0);
2077
2078 //printf("MWindow::toggle_label 1\n");
2079
2080         edl->labels->toggle_label(position1, position2);
2081         save_backup();
2082
2083         if( !is_mwindow ) {
2084                 gui->lock_window("MWindow::toggle_label 1");
2085         }
2086         gui->update_timebar(0);
2087         gui->activate_timeline();
2088         gui->flush();
2089         if( !is_mwindow ) {
2090                 gui->unlock_window();
2091         }
2092
2093         if( is_mwindow ) {
2094                 cwindow->gui->lock_window("MWindow::toggle_label 2");
2095         }
2096         cwindow->gui->timebar->update(1);
2097         if( is_mwindow ) {
2098                 cwindow->gui->unlock_window();
2099         }
2100
2101         awindow->gui->async_update_assets();
2102
2103         undo->update_undo_after(_("label"), LOAD_TIMEBAR);
2104         return 0;
2105 }
2106
2107 void MWindow::trim_selection()
2108 {
2109         undo->update_undo_before();
2110
2111
2112         edl->trim_selection(edl->local_session->get_selectionstart(),
2113                 edl->local_session->get_selectionend(),
2114                 edl->session->labels_follow_edits,
2115                 edl->session->plugins_follow_edits,
2116                 edl->session->autos_follow_edits);
2117
2118         save_backup();
2119         undo->update_undo_after(_("trim selection"), LOAD_EDITS | LOAD_TIMEBAR);
2120         update_plugin_guis();
2121         gui->update(1, 2, 1, 1, 1, 1, 0);
2122         cwindow->update(1, 0, 0, 0, 1);
2123         restart_brender();
2124         cwindow->playback_engine->que->
2125                 send_command(CURRENT_FRAME, CHANGE_EDL, edl, 1);
2126 }
2127
2128
2129 void MWindow::undo_entry(BC_WindowBase *calling_window_gui)
2130 {
2131         calling_window_gui->unlock_window();
2132
2133         cwindow->playback_engine->que->
2134                 send_command(STOP, CHANGE_NONE, 0, 0);
2135         cwindow->playback_engine->interrupt_playback(0);
2136
2137 //printf("MWindow::undo_entry %d %d\n", __LINE__, vwindows.size());
2138         for( int i = 0; i < vwindows.size(); i++ ) {
2139                 if( vwindows.get(i)->is_running() ) {
2140                         vwindows.get(i)->playback_engine->que->
2141                                 send_command(STOP, CHANGE_NONE, 0, 0);
2142                         vwindows.get(i)->playback_engine->interrupt_playback(0);
2143                 }
2144         }
2145
2146         cwindow->gui->lock_window("MWindow::undo_entry 1");
2147         for( int i = 0; i < vwindows.size(); i++ ) {
2148                 if( vwindows.get(i)->is_running() ) {
2149                         if( calling_window_gui != vwindows.get(i)->gui ) {
2150                                 vwindows.get(i)->gui->lock_window("MWindow::undo_entry 4");
2151                         }
2152                 }
2153         }
2154         gui->lock_window("MWindow::undo_entry 2");
2155
2156         undo->undo();
2157
2158         save_backup();
2159         restart_brender();
2160         update_plugin_states();
2161         update_plugin_guis();
2162
2163         gui->update(1, 2, 1, 1, 1, 1, 1);
2164         gui->unlock_window();
2165         cwindow->update(1, 1, 1, 1, 1);
2166         cwindow->gui->unlock_window();
2167
2168         for( int i = 0; i < vwindows.size(); i++ ) {
2169                 if( vwindows.get(i)->is_running() ) {
2170                         if( calling_window_gui != vwindows.get(i)->gui ) {
2171                                 vwindows.get(i)->gui->unlock_window();
2172                         }
2173                 }
2174         }
2175
2176         if( calling_window_gui != gui )
2177                 gui->unlock_window();
2178
2179         awindow->gui->async_update_assets();
2180
2181         cwindow->playback_engine->que->
2182                 send_command(CURRENT_FRAME, CHANGE_ALL, edl, 1);
2183         calling_window_gui->lock_window("MWindow::undo_entry 4");
2184 }
2185
2186
2187 void MWindow::new_folder(const char *new_folder)
2188 {
2189         undo->update_undo_before();
2190         edl->new_folder(new_folder);
2191         undo->update_undo_after(_("new folder"), LOAD_ALL);
2192         awindow->gui->async_update_assets();
2193 }
2194
2195 void MWindow::delete_folder(char *folder)
2196 {
2197 //      undo->update_undo_after(_("delete folder"), LOAD_ALL);
2198 }
2199
2200 void MWindow::select_point(double position)
2201 {
2202         edl->local_session->set_selectionstart(position);
2203         edl->local_session->set_selectionend(position);
2204
2205 // Que the CWindow
2206         cwindow->update(1, 0, 0, 0, 1);
2207
2208         update_plugin_guis();
2209         gui->update_patchbay();
2210         gui->hide_cursor(0);
2211         gui->draw_cursor(0);
2212         gui->mainclock->update(edl->local_session->get_selectionstart(1));
2213         gui->zoombar->update();
2214         gui->update_timebar(0);
2215         gui->flash_canvas(0);
2216         gui->flush();
2217 }
2218
2219
2220
2221
2222 void MWindow::map_audio(int pattern)
2223 {
2224         undo->update_undo_before();
2225         remap_audio(pattern);
2226         undo->update_undo_after(
2227                 pattern == MWindow::AUDIO_1_TO_1 ? _("map 1:1") : _("map 5.1:2"),
2228                 LOAD_AUTOMATION);
2229         sync_parameters(CHANGE_PARAMS);
2230         gui->update(0, 1, 0, 0, 1, 0, 0);
2231 }
2232
2233 void MWindow::remap_audio(int pattern)
2234 {
2235         int current_channel = 0;
2236         int current_track = 0;
2237         for( Track *current = edl->tracks->first; current; current = NEXT ) {
2238                 if( current->data_type == TRACK_AUDIO &&
2239                         current->record ) {
2240                         Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
2241                         PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
2242
2243                         for( int i = 0; i < MAXCHANNELS; i++ ) {
2244                                 pan_auto->values[i] = 0.0;
2245                         }
2246
2247                         if( pattern == MWindow::AUDIO_1_TO_1 ) {
2248                                 pan_auto->values[current_channel] = 1.0;
2249                         }
2250                         else
2251                         if( pattern == MWindow::AUDIO_5_1_TO_2 ) {
2252                                 switch( current_track ) {
2253                                 case 0:
2254                                         pan_auto->values[0] = 0.5;
2255                                         pan_auto->values[1] = 0.5;
2256                                         break;
2257                                 case 1:
2258                                         pan_auto->values[0] = 1;
2259                                         break;
2260                                 case 2:
2261                                         pan_auto->values[1] = 1;
2262                                         break;
2263                                 case 3:
2264                                         pan_auto->values[0] = 1;
2265                                         break;
2266                                 case 4:
2267                                         pan_auto->values[1] = 1;
2268                                         break;
2269                                 case 5:
2270                                         pan_auto->values[0] = 0.5;
2271                                         pan_auto->values[1] = 0.5;
2272                                         break;
2273                                 }
2274                         }
2275
2276                         BC_Pan::calculate_stick_position(edl->session->audio_channels,
2277                                 edl->session->achannel_positions, pan_auto->values,
2278                                 MAX_PAN, PAN_RADIUS, pan_auto->handle_x, pan_auto->handle_y);
2279
2280                         current_channel++;
2281                         current_track++;
2282                         if( current_channel >= edl->session->audio_channels )
2283                                 current_channel = 0;
2284                 }
2285         }
2286 }
2287
2288 void MWindow::cut_commercials()
2289 {
2290         undo->update_undo_before();
2291         commercials->scan_media();
2292         edl->optimize();
2293         save_backup();
2294         undo->update_undo_after(_("cut ads"), LOAD_EDITS | LOAD_TIMEBAR);
2295
2296         restart_brender();
2297         update_plugin_guis();
2298         gui->update(1, 2, 1, 1, 1, 1, 0);
2299         cwindow->update(1, 0, 0, 0, 1);
2300         cwindow->playback_engine->que->
2301                 send_command(CURRENT_FRAME, CHANGE_EDL, edl, 1);
2302 }
2303
2304