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