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