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