d424d60879335f2f4dc267f2b57dedb9f88f47dc
[goodguy/cinelerra.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 "convert.h"
31 #include "cplayback.h"
32 #include "ctimebar.h"
33 #include "cwindow.h"
34 #include "cwindowgui.h"
35 #include "bchash.h"
36 #include "edl.h"
37 #include "edlsession.h"
38 #include "filesystem.h"
39 #include "filexml.h"
40 #include "floatauto.h"
41 #include "floatautos.h"
42 #include "gwindow.h"
43 #include "gwindowgui.h"
44 #include "keyframe.h"
45 #include "keyframes.h"
46 #include "language.h"
47 #include "labels.h"
48 #include "levelwindow.h"
49 #include "localsession.h"
50 #include "mainclock.h"
51 #include "maincursor.h"
52 #include "mainerror.h"
53 #include "mainindexes.h"
54 #include "mainmenu.h"
55 #include "mainsession.h"
56 #include "mainundo.h"
57 #include "maskautos.h"
58 #include "mtimebar.h"
59 #include "mwindowgui.h"
60 #include "mwindow.h"
61 #include "panauto.h"
62 #include "patchbay.h"
63 #include "playbackengine.h"
64 #include "pluginset.h"
65 #include "recordlabel.h"
66 #include "samplescroll.h"
67 #include "trackcanvas.h"
68 #include "track.h"
69 #include "trackscroll.h"
70 #include "tracks.h"
71 #include "transition.h"
72 #include "transportque.h"
73 #include "units.h"
74 #include "undostackitem.h"
75 #include "vplayback.h"
76 #include "vwindow.h"
77 #include "vwindowgui.h"
78 #include "zoombar.h"
79 #include "zwindow.h"
80 #include "automation.h"
81 #include "maskautos.h"
82
83 #include <string.h>
84
85 void MWindow::add_audio_track_entry(int above, Track *dst)
86 {
87         undo_before();
88         Track *track = add_audio_track(above, dst);
89         track->master = 1;
90         save_backup();
91         undo_after(_("add track"), LOAD_ALL);
92
93         restart_brender();
94         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
95         gui->activate_timeline();
96         cwindow->refresh_frame(CHANGE_EDL);
97 }
98
99 void MWindow::add_video_track_entry(int above, Track *dst)
100 {
101         undo_before();
102         Track *track = add_video_track(above, dst);
103         track->master = 1;
104         undo_after(_("add track"), LOAD_ALL);
105
106         restart_brender();
107
108         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
109         gui->activate_timeline();
110         cwindow->refresh_frame(CHANGE_EDL);
111         save_backup();
112 }
113
114 void MWindow::add_subttl_track_entry(int above, Track *dst)
115 {
116         undo_before();
117         Track *track = add_subttl_track(above, dst);
118         track->master = 1;
119         undo_after(_("add track"), LOAD_ALL);
120
121         restart_brender();
122
123         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
124         gui->activate_timeline();
125         cwindow->refresh_frame(CHANGE_EDL);
126         save_backup();
127 }
128
129
130 Track *MWindow::add_audio_track(int above, Track *dst)
131 {
132         Track *track = edl->tracks->add_audio_track(above, dst);
133         edl->tracks->update_y_pixels(theme);
134         save_backup();
135         return track;
136 }
137
138 Track *MWindow::add_video_track(int above, Track *dst)
139 {
140         Track *track = edl->tracks->add_video_track(above, dst);
141         edl->tracks->update_y_pixels(theme);
142         save_backup();
143         return track;
144 }
145
146 Track *MWindow::add_subttl_track(int above, Track *dst)
147 {
148         Track *track = edl->tracks->add_subttl_track(above, dst);
149         edl->tracks->update_y_pixels(theme);
150         save_backup();
151         return track;
152 }
153
154 void MWindow::asset_to_all()
155 {
156         if( !session->drag_assets->size() ) return;
157         Indexable *indexable = session->drag_assets->get(0);
158
159 //      if( indexable->have_video() )
160         {
161                 int w, h;
162
163                 undo_before();
164
165 // Get w and h
166                 w = indexable->get_w();
167                 h = indexable->get_h();
168                 double new_framerate = session->drag_assets->get(0)->get_frame_rate();
169                 double old_framerate = edl->session->frame_rate;
170                 int old_samplerate = edl->session->sample_rate;
171                 int new_samplerate = session->drag_assets->get(0)->get_sample_rate();
172
173
174                 if( indexable->have_video() ) {
175                         edl->session->output_w = w;
176                         edl->session->output_h = h;
177                         edl->session->frame_rate = new_framerate;
178                         create_aspect_ratio(
179                                 edl->session->aspect_w,
180                                 edl->session->aspect_h,
181                                 w, h);
182
183                         for( Track *current=edl->tracks->first; current; current=NEXT ) {
184                                 if( current->data_type == TRACK_VIDEO /* &&
185                                         current->is_armed() */  ) {
186                                         current->track_w = w;
187                                         current->track_h = h;
188                                 }
189                         }
190
191 #ifdef GLx4
192                         if( ((edl->session->output_w % 4) ||
193                                 (edl->session->output_h % 4)) &&
194                                 edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL ) {
195                                 MainError::show_error(
196                                         _("This project's dimensions are not multiples of 4 so\n"
197                                         "it can't be rendered by OpenGL."));
198                         }
199 #endif
200 // Get aspect ratio
201                         if( defaults->get("AUTOASPECT", 0) ) {
202                                 create_aspect_ratio(
203                                         edl->session->aspect_w,
204                                         edl->session->aspect_h,
205                                         w, h);
206                         }
207                 }
208
209                 if( indexable->have_audio() ) {
210                         edl->session->sample_rate = new_samplerate;
211                         edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
212                         edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
213                 }
214
215                 save_backup();
216
217                 undo_after(_("asset to all"), LOAD_ALL);
218                 restart_brender();
219                 gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
220                 sync_parameters(CHANGE_ALL);
221         }
222 }
223
224 void MWindow::asset_to_size()
225 {
226         if( !session->drag_assets->size() ) return;
227         Indexable *indexable = session->drag_assets->get(0);
228
229         if( indexable->have_video() ) {
230                 int w, h;
231                 undo_before();
232
233 // Get w and h
234                 w = indexable->get_w();
235                 h = indexable->get_h();
236                 edl->session->output_w = w;
237                 edl->session->output_h = h;
238 #ifdef GLx4
239                 if( ((edl->session->output_w % 4) ||
240                         (edl->session->output_h % 4)) &&
241                         edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL ) {
242                         MainError::show_error(
243                                 _("This project's dimensions are not multiples of 4 so\n"
244                                 "it can't be rendered by OpenGL."));
245                 }
246 #endif
247 // Get aspect ratio
248                 if( defaults->get("AUTOASPECT", 0) ) {
249                         create_aspect_ratio(edl->session->aspect_w,
250                                 edl->session->aspect_h,
251                                 w,
252                                 h);
253                 }
254
255                 save_backup();
256
257                 undo_after(_("asset to size"), LOAD_ALL);
258                 restart_brender();
259                 sync_parameters(CHANGE_ALL);
260         }
261 }
262
263
264 void MWindow::asset_to_rate()
265 {
266         if( session->drag_assets->size() &&
267                 session->drag_assets->get(0)->have_video() ) {
268                 double new_framerate = session->drag_assets->get(0)->get_frame_rate();
269                 double old_framerate = edl->session->frame_rate;
270                 undo_before();
271
272                 edl->session->frame_rate = new_framerate;
273                 edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
274
275                 save_backup();
276
277                 undo_after(_("asset to rate"), LOAD_ALL);
278                 restart_brender();
279                 gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
280                 sync_parameters(CHANGE_ALL);
281         }
282 }
283
284
285 void MWindow::clear_entry()
286 {
287         undo_before();
288         clear(1);
289
290         edl->optimize();
291         save_backup();
292         undo_after(_("clear"), LOAD_EDITS | LOAD_TIMEBAR);
293
294         restart_brender();
295         update_plugin_guis();
296         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
297         cwindow->update(1, 0, 0, 0, 1);
298         cwindow->refresh_frame(CHANGE_EDL);
299 }
300
301 void MWindow::clear(int clear_handle)
302 {
303         double start = edl->local_session->get_selectionstart();
304         double end = edl->local_session->get_selectionend();
305         if( clear_handle || !EQUIV(start, end) ) {
306                 edl->clear(start,
307                         end,
308                         edl->session->labels_follow_edits,
309                         edl->session->plugins_follow_edits,
310                         edl->session->autos_follow_edits);
311         }
312 }
313
314 void MWindow::update_gui(int changed_edl)
315 {
316         restart_brender();
317         update_plugin_guis();
318         if( changed_edl ) {
319                 gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
320                 cwindow->update(1, 0, 0, 0, 1);
321                 cwindow->refresh_frame(CHANGE_EDL);
322         }
323         else {
324                 gui->draw_overlays(1);
325                 sync_parameters(CHANGE_PARAMS);
326                 gui->update_patchbay();
327                 cwindow->update(1, 0, 0);
328         }
329 }
330
331 void MWindow::set_automation_mode(int mode)
332 {
333         undo_before();
334         speed_before();
335         edl->tracks->set_automation_mode(
336                 edl->local_session->get_selectionstart(),
337                 edl->local_session->get_selectionend(),
338                 mode);
339         int changed_edl = speed_after(1);
340         save_backup();
341         char string[BCSTRLEN];
342         sprintf(string,"set %s", FloatAuto::curve_name(mode));
343         undo_after(string,
344                 !changed_edl ? LOAD_AUTOMATION :
345                         LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
346         update_gui(changed_edl);
347 }
348
349 void MWindow::clear_automation()
350 {
351         undo_before();
352         speed_before();
353         edl->tracks->clear_automation(edl->local_session->get_selectionstart(),
354                 edl->local_session->get_selectionend());
355         int changed_edl = speed_after(1);
356         save_backup();
357         undo_after(_("clear keyframes"),
358                 !changed_edl ? LOAD_AUTOMATION :
359                         LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
360         update_gui(changed_edl);
361 }
362
363 int MWindow::clear_default_keyframe()
364 {
365         undo_before();
366         speed_before();
367         edl->tracks->clear_default_keyframe();
368         int changed_edl = speed_after(1);
369         save_backup();
370         undo_after(_("clear default keyframe"),
371                 !changed_edl ? LOAD_AUTOMATION :
372                         LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
373         update_gui(changed_edl);
374         return 0;
375 }
376
377 void MWindow::clear_labels()
378 {
379         undo_before();
380         clear_labels(edl->local_session->get_selectionstart(),
381                 edl->local_session->get_selectionend());
382         undo_after(_("clear labels"), LOAD_TIMEBAR);
383
384         gui->update_timebar(1);
385         cwindow->update(0, 0, 0, 0, 1);
386         save_backup();
387 }
388
389 int MWindow::clear_labels(double start, double end)
390 {
391         if( start == end ) {
392                 start = 0;
393                 end = edl->tracks->total_length();
394         }
395         edl->labels->clear(start, end, 0);
396         return 0;
397 }
398
399 void MWindow::clear_hard_edges()
400 {
401         undo_before();
402         clear_hard_edges(edl->local_session->get_selectionstart(),
403                 edl->local_session->get_selectionend());
404         edl->optimize();
405         save_backup();
406         undo_after(_("clear hard edges"), LOAD_EDITS);
407         restart_brender();
408         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
409         cwindow->refresh_frame(CHANGE_EDL);
410 }
411
412 int MWindow::clear_hard_edges(double start, double end)
413 {
414         if( start == end ) {
415                 start = 0;
416                 end = edl->tracks->total_length();
417         }
418         edl->clear_hard_edges(start, end);
419         return 0;
420 }
421
422 void MWindow::clear_select()
423 {
424         edl->tracks->clear_selected_edits();
425         gui->draw_overlays(1);
426 }
427
428 void MWindow::concatenate_tracks()
429 {
430         undo_before();
431         edl->tracks->concatenate_tracks(edl->session->plugins_follow_edits,
432                 edl->session->autos_follow_edits);
433         save_backup();
434         undo_after(_("concatenate tracks"), LOAD_EDITS);
435
436         restart_brender();
437         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
438         cwindow->refresh_frame(CHANGE_EDL);
439 }
440
441
442 int MWindow::copy_flags(int copy_flags)
443 {
444         if( !edl->session->labels_follow_edits )
445                 copy_flags &= ~COPY_LABELS;
446         if( !edl->session->autos_follow_edits )
447                 copy_flags &= ~COPY_AUTOS;
448         if( !edl->session->plugins_follow_edits )
449                 copy_flags &= ~COPY_PLUGINS;
450         return copy_flags;
451 }
452
453 void MWindow::copy()
454 {
455         copy(edl->local_session->get_selectionstart(),
456                 edl->local_session->get_selectionend());
457 }
458
459 int MWindow::copy(double start, double end)
460 {
461         if( start == end ) return 1;
462
463         FileXML file;
464         edl->copy(copy_flags(), start, end, &file, "", 1);
465         const char *file_string = file.string();
466         long file_length = strlen(file_string);
467         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
468         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
469         save_backup();
470         return 0;
471 }
472
473 int MWindow::copy_automation()
474 {
475         FileXML file;
476         double start = edl->local_session->get_selectionstart();
477         double end = edl->local_session->get_selectionend();
478         edl->tracks->copy_automation(start, end, &file, 0, 1);
479         const char *file_string = file.string();
480         long file_length = strlen(file_string);
481         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
482         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
483         return 0;
484 }
485
486 int MWindow::copy_default_keyframe()
487 {
488         FileXML file;
489         double start = edl->local_session->get_selectionstart();
490         double end = edl->local_session->get_selectionend();
491         edl->tracks->copy_automation(start, end, &file, 1, 0);
492         const char *file_string = file.string();
493         long file_length = strlen(file_string);
494         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
495         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
496         return 0;
497 }
498
499 // Uses cropping coordinates in edl session to crop and translate video.
500 // We modify the projector since camera automation depends on the track size.
501 void MWindow::crop_video(int mode)
502 {
503         undo_before();
504 // Clamp EDL crop region
505         if( edl->session->crop_x1 > edl->session->crop_x2 ) {
506                 edl->session->crop_x1 ^= edl->session->crop_x2;
507                 edl->session->crop_x2 ^= edl->session->crop_x1;
508                 edl->session->crop_x1 ^= edl->session->crop_x2;
509         }
510         if( edl->session->crop_y1 > edl->session->crop_y2 ) {
511                 edl->session->crop_y1 ^= edl->session->crop_y2;
512                 edl->session->crop_y2 ^= edl->session->crop_y1;
513                 edl->session->crop_y1 ^= edl->session->crop_y2;
514         }
515         switch( mode ) {
516         case CROP_REFORMAT: {
517                 float ctr_x = edl->session->output_w / 2.;
518                 float ctr_y = edl->session->output_h / 2.;
519                 float new_x = (edl->session->crop_x1 + edl->session->crop_x2) / 2.;
520                 float new_y = (edl->session->crop_y1 + edl->session->crop_y2) / 2.;
521                 float dx = -(new_x - ctr_x), dy = -(new_y - ctr_y);
522                 edl->tracks->translate_projector(dx, dy, 1);
523
524                 edl->session->output_w = edl->session->crop_x2 - edl->session->crop_x1;
525                 edl->session->output_h = edl->session->crop_y2 - edl->session->crop_y1;
526                 edl->session->crop_x1 = edl->session->crop_y1 = 0;
527                 edl->session->crop_x2 = edl->session->output_w;
528                 edl->session->crop_y2 = edl->session->output_h;
529                 break; }
530         case CROP_RESIZE: {
531                 float old_w = edl->session->output_w;
532                 float old_h = edl->session->output_h;
533                 float new_w = edl->session->crop_x2 - edl->session->crop_x1;
534                 float new_h = edl->session->crop_y2 - edl->session->crop_y1;
535                 if( !new_w ) new_w = 1;
536                 if( !new_h ) new_h = 1;
537                 float xzoom = old_w / new_w, yzoom = old_h / new_h;
538                 float new_z = bmin(xzoom, yzoom);
539                 float new_x = (edl->session->crop_x1 + edl->session->crop_x2) / 2.;
540                 float new_y = (edl->session->crop_y1 + edl->session->crop_y2) / 2.;
541                 edl->tracks->crop_resize(new_x, new_y, new_z);
542
543                 edl->session->crop_x1 = 0;
544                 edl->session->crop_y1 = 0;
545                 edl->session->crop_x2 = edl->session->output_w;
546                 edl->session->crop_y2 = edl->session->output_h;
547                 break; }
548         case CROP_SHRINK: {
549                 float old_w = edl->session->output_w;
550                 float old_h = edl->session->output_h;
551                 float new_w = edl->session->crop_x2 - edl->session->crop_x1;
552                 float new_h = edl->session->crop_y2 - edl->session->crop_y1;
553                 if( !new_w ) new_w = 1;
554                 if( !new_h ) new_h = 1;
555                 float xzoom = old_w / new_w, yzoom = old_h / new_h;
556                 float new_z = bmin(xzoom, yzoom);
557
558                 float new_x = (edl->session->crop_x1 + edl->session->crop_x2) / 2.;
559                 float new_y = (edl->session->crop_y1 + edl->session->crop_y2) / 2.;
560                 edl->tracks->crop_shrink(new_x, new_y, new_z);
561                 break; }
562         }
563
564 // Recalculate aspect ratio
565         if( defaults->get("AUTOASPECT", 0) ) {
566                 create_aspect_ratio(edl->session->aspect_w,
567                         edl->session->aspect_h,
568                         edl->session->output_w,
569                         edl->session->output_h);
570         }
571
572         undo_after(_("crop"), LOAD_ALL);
573
574         restart_brender();
575         cwindow->refresh_frame(CHANGE_ALL);
576         save_backup();
577 }
578
579 void MWindow::cut()
580 {
581         double start = edl->local_session->get_selectionstart();
582         double end = edl->local_session->get_selectionend();
583         if( EQUIV(start,end) )
584                 blade(start);
585         else
586                 cut(start, end);
587 }
588
589 void MWindow::blade(double position)
590 {
591         undo_before();
592         edl->blade(position);
593         edl->optimize();
594         save_backup();
595         undo_after(_("blade"), LOAD_EDITS | LOAD_TIMEBAR);
596         restart_brender();
597         update_plugin_guis();
598         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
599         cwindow->update(1, 0, 0, 0, 1);
600         awindow->gui->async_update_assets();
601         cwindow->refresh_frame(CHANGE_EDL);
602 }
603
604 void MWindow::cut(double start, double end, double new_position)
605 {
606         undo_before();
607         copy(start, end);
608         edl->clear(start, end,
609                 edl->session->labels_follow_edits,
610                 edl->session->plugins_follow_edits,
611                 edl->session->autos_follow_edits);
612
613         edl->optimize();
614         save_backup();
615         undo_after(_("split | cut"), LOAD_EDITS | LOAD_TIMEBAR);
616         if( new_position >= 0 ) {
617                 edl->local_session->set_selectionstart(new_position);
618                 edl->local_session->set_selectionend(new_position);
619         }
620         restart_brender();
621         update_plugin_guis();
622         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
623         cwindow->update(1, 0, 0, 0, 1);
624         awindow->gui->async_update_assets();
625         cwindow->refresh_frame(CHANGE_EDL);
626 }
627
628 void MWindow::cut_left_edit()
629 {
630         double start_pos = edl->local_session->get_selectionstart(1);
631         double position = edl->prev_edit(start_pos);
632         if( position < start_pos )
633                 cut(position, start_pos, position);
634 }
635
636 void MWindow::cut_right_edit()
637 {
638         double end_pos = edl->local_session->get_selectionend(1);
639         double position = edl->next_edit(end_pos);
640         if( end_pos < position )
641                 cut(end_pos, position, end_pos);
642 }
643
644 void MWindow::cut_left_label()
645 {
646         double start_pos = edl->local_session->get_selectionstart(1);
647         Label *left_label = edl->labels->prev_label(start_pos);
648         if( !left_label ) return;
649         double position = left_label->position;
650         if( position < start_pos )
651                 cut(position, start_pos, position);
652 }
653
654 void MWindow::cut_right_label()
655 {
656         double end_pos = edl->local_session->get_selectionend(1);
657         Label *right_label = edl->labels->next_label(end_pos);
658         if( !right_label ) return;
659         double position = right_label->position;
660         if( end_pos < position )
661                 cut(end_pos, position, end_pos);
662 }
663
664 int MWindow::cut_automation()
665 {
666         undo_before();
667         speed_before();
668         copy_automation();
669         edl->tracks->clear_automation(edl->local_session->get_selectionstart(),
670                 edl->local_session->get_selectionend());
671         int changed_edl = speed_after(1);
672         save_backup();
673         undo_after(_("cut keyframes"),
674                 !changed_edl ? LOAD_AUTOMATION :
675                         LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
676         update_gui(changed_edl);
677         return 0;
678 }
679
680 int MWindow::cut_default_keyframe()
681 {
682
683         undo_before();
684         speed_before();
685         copy_default_keyframe();
686         edl->tracks->clear_default_keyframe();
687         int changed_edl = speed_after(1);
688         save_backup();
689         undo_after(_("cut default keyframe"),
690                 !changed_edl ? LOAD_AUTOMATION :
691                         LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
692         update_gui(changed_edl);
693         return 0;
694 }
695
696
697 void MWindow::delete_tracks()
698 {
699         undo_before();
700         edl->tracks->delete_tracks();
701         undo_after(_("delete tracks"), LOAD_ALL);
702         save_backup();
703
704         restart_brender();
705         update_plugin_states();
706
707         gui->update(1, NORMAL_DRAW, 1, 0, 1, 0, 0);
708         cwindow->refresh_frame(CHANGE_EDL);
709 }
710
711 void MWindow::delete_track(Track *track)
712 {
713         undo_before();
714         edl->tracks->delete_track(track);
715         undo_after(_("delete track"), LOAD_ALL);
716
717         restart_brender();
718         update_plugin_states();
719
720         gui->update(1, NORMAL_DRAW, 1, 0, 1, 0, 0);
721         cwindow->refresh_frame(CHANGE_EDL);
722         save_backup();
723 }
724
725
726 // Insert data from clipboard
727 void MWindow::insert(double position, FileXML *file,
728         int edit_labels, int edit_plugins, int edit_autos,
729         EDL *parent_edl, Track *first_track, int overwrite)
730 {
731 // For clipboard pasting make the new edl use a separate session
732 // from the master EDL.  Then it can be resampled to the master rates.
733 // For splice, overwrite, and dragging need same session to get the assets.
734         EDL *edl = new EDL(parent_edl);
735         ArrayList<EDL*> new_edls;
736         uint32_t load_flags = LOAD_ALL;
737
738
739         new_edls.append(edl);
740         edl->create_objects();
741
742
743
744
745         if( parent_edl ) load_flags &= ~LOAD_SESSION;
746         if( !edl->session->autos_follow_edits ) load_flags &= ~LOAD_AUTOMATION;
747         if( !edl->session->labels_follow_edits ) load_flags &= ~LOAD_TIMEBAR;
748
749         edl->load_xml(file, load_flags);
750
751
752 //printf("MWindow::insert %f\n", edl->local_session->clipboard_length);
753
754
755
756         paste_edls(&new_edls, LOADMODE_PASTE, first_track, position,
757                 edit_labels, edit_plugins, edit_autos, overwrite);
758 // if( vwindow->edl )
759 // printf("MWindow::insert 5 %f %f\n",
760 // vwindow->edl->local_session->in_point,
761 // vwindow->edl->local_session->out_point);
762         new_edls.remove_all();
763         edl->Garbage::remove_user();
764 //printf("MWindow::insert 6 %p\n", vwindow->get_edl());
765 }
766
767 void MWindow::insert_effects_canvas(Track *dest_track, double start, double length)
768 {
769         undo_before();
770
771         ArrayList<SharedLocation> shared_locations;
772         PluginSet *pluginset = session->pluginset_highlighted;
773         int gang = edl->session->gang_tracks != GANG_NONE ? 1 : 0;
774         int data_type = dest_track->data_type;
775         int first_track = 1;
776
777         for( Track *track=dest_track; track; track=track->next ) {
778                 if( gang && track->master && !first_track ) break;
779                 if( track->data_type != data_type ) continue;
780                 if( !track->is_armed() ) continue;
781                 int module = edl->tracks->number_of(track);
782                 for( int i=0; i<session->drag_pluginservers->total; ++i ) {
783                         PluginServer *plugin = session->drag_pluginservers->values[i];
784                         int shared = gang; // && plugin->multichannel ? 1 : 0;
785                         int plugin_type = !first_track && shared ?
786                                 PLUGIN_SHAREDPLUGIN : PLUGIN_STANDALONE;
787                         SharedLocation *shared_location = !first_track ?
788                                 &shared_locations[i] : &shared_locations.append();
789                         insert_effect(plugin->title, shared_location, track,
790                                         pluginset, start, length, plugin_type);
791                         if( first_track && shared ) {
792                                 shared_location->module = module;
793                                 shared_location->plugin = pluginset ?
794                                         track->plugin_set.number_of(pluginset) :
795                                         track->plugin_set.total-1 ;
796                         }
797                 }
798                 if( !gang ) break;
799                 first_track = 0;
800                 pluginset = 0;
801         }
802
803         save_backup();
804         undo_after(_("insert effect"), LOAD_EDITS | LOAD_PATCHES);
805         restart_brender();
806         sync_parameters(CHANGE_EDL);
807 // GUI updated in TrackCanvas, after current_operations are reset
808 }
809
810 void MWindow::insert_effects_cwindow(Track *dest_track)
811 {
812         if( !dest_track ) return;
813
814         double start = 0;
815         double length = dest_track->get_length();
816
817         if( edl->local_session->get_selectionend() >
818                 edl->local_session->get_selectionstart() ) {
819                 start = edl->local_session->get_selectionstart();
820                 length = edl->local_session->get_selectionend() -
821                         edl->local_session->get_selectionstart();
822         }
823
824         insert_effects_canvas(dest_track, start, length);
825         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
826 }
827
828 void MWindow::insert_effect(char *title, SharedLocation *shared_location,
829                 int data_type, int plugin_type, int single_standalone)
830 {
831         Track *current = edl->tracks->first;
832         SharedLocation shared_location_local;
833         shared_location_local.copy_from(shared_location);
834         int first_track = 1;
835         double start_pos = edl->local_session->get_selectionstart(1);
836         double end_pos = edl->local_session->get_selectionend(1);
837         for( ; current; current=NEXT ) {
838                 if( current->data_type != data_type ) continue;
839                 if( !current->is_armed() ) continue;
840                 double start = start_pos, end = end_pos;
841                 if( plugin_type == PLUGIN_STANDALONE && start >= end ) {
842                         start = 0;
843                         end = current->get_length();
844                 }
845                 double length = end - start;
846                 insert_effect(title, &shared_location_local,
847                                 current, 0, start, length, plugin_type);
848                 if( first_track ) {
849                         if( plugin_type == PLUGIN_STANDALONE && single_standalone ) {
850                                 plugin_type = PLUGIN_SHAREDPLUGIN;
851                                 shared_location_local.module = edl->tracks->number_of(current);
852                                 shared_location_local.plugin = current->plugin_set.total - 1;
853                                 start_pos = start;  end_pos = end;
854                         }
855                         first_track = 0;
856                 }
857         }
858 }
859
860 void MWindow::insert_effect(char *title,
861                 SharedLocation *shared_location, Track *track, PluginSet *plugin_set,
862                 double start, double length, int plugin_type)
863 {
864         KeyFrame *default_keyframe = 0;
865         PluginServer *server = 0;
866 // Get default keyframe
867         if( plugin_type == PLUGIN_STANDALONE ) {
868                 default_keyframe = new KeyFrame;
869                 server = new PluginServer(*scan_plugindb(title, track->data_type));
870
871                 server->open_plugin(0, preferences, edl, 0);
872                 server->save_data(default_keyframe);
873         }
874 // Insert plugin object
875         track->insert_effect(title, shared_location, default_keyframe,
876                         plugin_set, start, length, plugin_type);
877         track->optimize();
878
879         if( plugin_type == PLUGIN_STANDALONE ) {
880                 server->close_plugin();
881                 delete server;
882                 delete default_keyframe;
883         }
884 }
885
886 int MWindow::modify_edithandles()
887 {
888         undo_before();
889         int handle_mode = edl->session->edit_handle_mode[session->drag_button];
890         edl->modify_edithandles(session->drag_start,
891                 session->drag_position, session->drag_handle, handle_mode,
892                 edl->session->labels_follow_edits,
893                 edl->session->plugins_follow_edits,
894                 edl->session->autos_follow_edits,
895                 session->drag_edit->group_id);
896         finish_modify_handles();
897 //printf("MWindow::modify_handles 1\n");
898         return 0;
899 }
900
901 int MWindow::modify_pluginhandles()
902 {
903         undo_before();
904
905         edl->modify_pluginhandles(session->drag_start,
906                 session->drag_position,
907                 session->drag_handle,
908                 edl->session->edit_handle_mode[session->drag_button],
909                 edl->session->labels_follow_edits,
910                 edl->session->autos_follow_edits,
911                 session->trim_edits);
912
913         finish_modify_handles();
914
915         return 0;
916 }
917
918
919 // Common to edithandles and plugin handles
920 void MWindow::finish_modify_handles()
921 {
922         int edit_mode = edl->session->edit_handle_mode[session->drag_button];
923         double position = -1;
924         switch( edit_mode ) {
925         case MOVE_RIPPLE:
926                 position = !session->drag_handle ?
927                         session->drag_start : session->drag_position;
928                 break;
929         case MOVE_ROLL:
930         case MOVE_SLIDE:
931                 position = session->drag_position;
932                 break;
933         case MOVE_SLIP:
934         case MOVE_EDGE:
935                 position = session->drag_start;
936                 break;
937         }
938         if( position >= 0 ) {
939                 edl->local_session->set_selectionstart(position);
940                 edl->local_session->set_selectionend(position);
941         }
942         undo_after(_("drag handle"), LOAD_EDITS | LOAD_TIMEBAR);
943
944         save_backup();
945         restart_brender();
946         sync_parameters(CHANGE_EDL);
947         update_plugin_guis();
948         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
949 // label list can be modified
950         awindow->gui->async_update_assets();
951         cwindow->update(1, 0, 0, 0, 1);
952 }
953
954 void MWindow::match_output_size(Track *track)
955 {
956         undo_before();
957         track->track_w = edl->session->output_w;
958         track->track_h = edl->session->output_h;
959         save_backup();
960         undo_after(_("match output size"), LOAD_ALL);
961
962         restart_brender();
963         sync_parameters(CHANGE_EDL);
964 }
965
966
967 void MWindow::delete_edit(Edit *edit, const char *msg, int collapse)
968 {
969         ArrayList<Edit*> edits;
970         edits.append(edit);
971         delete_edits(&edits, msg, collapse);
972 }
973
974 void MWindow::delete_edits(ArrayList<Edit*> *edits, const char *msg, int collapse)
975 {
976         if( !edits->size() ) return;
977         undo_before();
978         if( edl->session->labels_follow_edits )
979                 edl->delete_edit_labels(edits, collapse);
980         edl->delete_edits(edits, collapse);
981         edl->optimize();
982         save_backup();
983         undo_after(msg, LOAD_EDITS);
984
985         restart_brender();
986         cwindow->refresh_frame(CHANGE_EDL);
987         update_plugin_guis();
988         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
989 }
990
991 void MWindow::delete_edits(int collapse)
992 {
993         ArrayList<Edit*> edits;
994         edl->tracks->get_selected_edits(&edits);
995         delete_edits(&edits,_("del edit"), collapse);
996 }
997
998 // collapse - delete from timeline, not collapse replace with silence
999 // packed - omit unselected from selection, unpacked - replace unselected with silence
1000 void MWindow::cut_selected_edits(int collapse, int packed)
1001 {
1002         selected_edits_to_clipboard(packed);
1003         ArrayList<Edit*> edits;
1004         edl->tracks->get_selected_edits(&edits);
1005         delete_edits(&edits, _("cut edit"), collapse);
1006 }
1007
1008
1009 void MWindow::move_edits(ArrayList<Edit*> *edits,
1010                 Track *track, double position, int mode)
1011 {
1012         undo_before();
1013 // lockout timebar labels update
1014 //  labels can be deleted with tooltip repeater running
1015         cwindow->gui->lock_window("Tracks::move_edits");
1016         edl->tracks->move_edits(edits, track, position,
1017                 edl->session->labels_follow_edits,
1018                 edl->session->plugins_follow_edits,
1019                 edl->session->autos_follow_edits, mode);
1020         cwindow->gui->timebar->update(1);
1021         cwindow->gui->unlock_window();
1022
1023         save_backup();
1024         undo_after(_("move edit"), LOAD_ALL);
1025
1026         restart_brender();
1027         cwindow->refresh_frame(CHANGE_EDL);
1028
1029         update_plugin_guis();
1030         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1031 }
1032
1033 void MWindow::selected_edits_to_clipboard(int packed)
1034 {
1035         EDL *new_edl = edl->selected_edits_to_clip(packed, 0, 0,
1036                 edl->session->labels_follow_edits,
1037                 edl->session->autos_follow_edits,
1038                 edl->session->plugins_follow_edits);
1039         if( !new_edl ) return;
1040         FileXML file;
1041         new_edl->copy(COPY_EDL, &file, "", 1);
1042         const char *file_string = file.string();
1043         long file_length = strlen(file_string);
1044         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
1045         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
1046         new_edl->remove_user();
1047 }
1048
1049 void MWindow::paste_clipboard(Track *first_track, double position, int overwrite,
1050                 int edit_edits, int edit_labels, int edit_autos, int edit_plugins)
1051 {
1052         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1053         if( !len ) return;
1054         char *string = new char[len];
1055         gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1056         FileXML file;
1057         file.read_from_string(string);
1058         delete [] string;
1059         EDL *clip = new EDL();
1060         clip->create_objects();
1061         if( !clip->load_xml(&file, LOAD_ALL) ) {
1062                 undo_before();
1063                 edl->paste_edits(clip, first_track, position, overwrite,
1064                         edit_edits, edit_labels, edit_autos, edit_plugins);
1065                 save_backup();
1066                 undo_after(_("paste clip"), LOAD_ALL);
1067                 restart_brender();
1068                 cwindow->refresh_frame(CHANGE_EDL);
1069
1070                 update_plugin_guis();
1071                 gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1072         }
1073         clip->remove_user();
1074 }
1075
1076 void MWindow::move_group(EDL *group, Track *first_track, double position, int overwrite)
1077 {
1078         undo_before();
1079 // lockout timebar labels update
1080 //  labels can be deleted with tooltip repeater running
1081         cwindow->gui->lock_window("Tracks::move_group");
1082
1083         ArrayList<Edit *>edits;
1084         edl->tracks->get_selected_edits(&edits);
1085         if( edl->session->labels_follow_edits )
1086                 edl->delete_edit_labels(&edits, 0);
1087         edl->delete_edits(&edits, 0);
1088         edl->paste_edits(group, first_track, position, overwrite, 1,
1089                 edl->session->labels_follow_edits,
1090                 edl->session->autos_follow_edits,
1091                 edl->session->plugins_follow_edits);
1092         cwindow->gui->timebar->update(1);
1093         cwindow->gui->unlock_window();
1094 // big debate over whether to do this, must either clear selected, or no tweaking
1095 //      edl->tracks->clear_selected_edits();
1096
1097         save_backup();
1098         undo_after(_("move group"), LOAD_ALL);
1099         restart_brender();
1100         cwindow->refresh_frame(CHANGE_EDL);
1101
1102         update_plugin_guis();
1103         gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
1104 }
1105
1106 void MWindow::move_effect(Plugin *plugin, Track *track, int64_t position)
1107 {
1108         undo_before();
1109         edl->tracks->move_effect(plugin, track, position);
1110         save_backup();
1111         undo_after(_("paste effect"), LOAD_ALL);
1112
1113         restart_brender();
1114         cwindow->refresh_frame(CHANGE_EDL);
1115         update_plugin_guis();
1116         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1117 }
1118
1119 void MWindow::move_effect(Plugin *plugin, PluginSet *plugin_set, int64_t position)
1120 {
1121         undo_before();
1122         edl->tracks->move_effect(plugin, plugin_set, position);
1123         save_backup();
1124         undo_after(_("move effect"), LOAD_ALL);
1125
1126         restart_brender();
1127         cwindow->refresh_frame(CHANGE_EDL);
1128         update_plugin_guis();
1129         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1130 }
1131
1132 void MWindow::move_plugins_up(PluginSet *plugin_set)
1133 {
1134
1135         undo_before();
1136         plugin_set->track->move_plugins_up(plugin_set);
1137
1138         save_backup();
1139         undo_after(_("move effect up"), LOAD_ALL);
1140         restart_brender();
1141         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1142         sync_parameters(CHANGE_EDL);
1143 }
1144
1145 void MWindow::move_plugins_down(PluginSet *plugin_set)
1146 {
1147         undo_before();
1148
1149         plugin_set->track->move_plugins_down(plugin_set);
1150
1151         save_backup();
1152         undo_after(_("move effect down"), LOAD_ALL);
1153         restart_brender();
1154         gui->update(1, NORMAL_DRAW, 0, 0, 0, 0, 0);
1155         sync_parameters(CHANGE_EDL);
1156 }
1157
1158 void MWindow::move_track_down(Track *track)
1159 {
1160         undo_before();
1161         edl->tracks->move_track_down(track);
1162         save_backup();
1163         undo_after(_("move track down"), LOAD_ALL);
1164
1165         restart_brender();
1166         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1167         sync_parameters(CHANGE_EDL);
1168         save_backup();
1169 }
1170
1171 void MWindow::move_tracks_down()
1172 {
1173         undo_before();
1174         edl->tracks->move_tracks_down();
1175         save_backup();
1176         undo_after(_("move tracks down"), LOAD_ALL);
1177
1178         restart_brender();
1179         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1180         sync_parameters(CHANGE_EDL);
1181         save_backup();
1182 }
1183
1184 void MWindow::move_track_up(Track *track)
1185 {
1186         undo_before();
1187         edl->tracks->move_track_up(track);
1188         save_backup();
1189         undo_after(_("move track up"), LOAD_ALL);
1190         restart_brender();
1191         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1192         sync_parameters(CHANGE_EDL);
1193         save_backup();
1194 }
1195
1196 void MWindow::move_tracks_up()
1197 {
1198         undo_before();
1199         edl->tracks->move_tracks_up();
1200         save_backup();
1201         undo_after(_("move tracks up"), LOAD_ALL);
1202         restart_brender();
1203         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1204         sync_parameters(CHANGE_EDL);
1205 }
1206
1207
1208 void MWindow::swap_track_down(Track *track)
1209 {
1210         undo_before();
1211         edl->tracks->swap_track_down(track);
1212         save_backup();
1213         undo_after(_("swap track down"), LOAD_ALL);
1214
1215         restart_brender();
1216         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1217         sync_parameters(CHANGE_EDL);
1218         save_backup();
1219 }
1220
1221 void MWindow::swap_tracks_down()
1222 {
1223         undo_before();
1224         edl->tracks->swap_tracks_down();
1225         save_backup();
1226         undo_after(_("swap tracks down"), LOAD_ALL);
1227
1228         restart_brender();
1229         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1230         sync_parameters(CHANGE_EDL);
1231         save_backup();
1232 }
1233
1234 void MWindow::swap_track_up(Track *track)
1235 {
1236         undo_before();
1237         edl->tracks->swap_track_up(track);
1238         save_backup();
1239         undo_after(_("swap track up"), LOAD_ALL);
1240         restart_brender();
1241         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1242         sync_parameters(CHANGE_EDL);
1243         save_backup();
1244 }
1245
1246 void MWindow::swap_tracks_up()
1247 {
1248         undo_before();
1249         edl->tracks->swap_tracks_up();
1250         save_backup();
1251         undo_after(_("swap tracks up"), LOAD_ALL);
1252         restart_brender();
1253         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
1254         sync_parameters(CHANGE_EDL);
1255 }
1256
1257
1258 void MWindow::mute_selection()
1259 {
1260         double start = edl->local_session->get_selectionstart();
1261         double end = edl->local_session->get_selectionend();
1262         if( start != end ) {
1263                 undo_before();
1264                 edl->clear(start, end, 0,
1265                         edl->session->plugins_follow_edits,
1266                         edl->session->autos_follow_edits);
1267                 edl->local_session->set_selectionend(end);
1268                 edl->local_session->set_selectionstart(start);
1269                 edl->paste_silence(start, end, 0,
1270                         edl->session->plugins_follow_edits,
1271                         edl->session->autos_follow_edits);
1272
1273                 save_backup();
1274                 undo_after(_("mute"), LOAD_EDITS);
1275
1276                 restart_brender();
1277                 update_plugin_guis();
1278                 gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
1279                 cwindow->refresh_frame(CHANGE_EDL);
1280         }
1281 }
1282
1283
1284 void MWindow::overwrite(EDL *source, int all)
1285 {
1286         FileXML file;
1287
1288         LocalSession *src = source->local_session;
1289         double src_start = all ? 0 :
1290                 src->inpoint_valid() ? src->get_inpoint() :
1291                 src->outpoint_valid() ? 0 :
1292                         src->get_selectionstart();
1293         double src_end = all ? source->tracks->total_length() :
1294                 src->outpoint_valid() ? src->get_outpoint() :
1295                 src->inpoint_valid() ? source->tracks->total_length() :
1296                         src->get_selectionend();
1297         double overwrite_len = src_end - src_start;
1298         double dst_start = edl->local_session->get_selectionstart();
1299         double dst_len = edl->local_session->get_selectionend() - dst_start;
1300
1301         undo_before();
1302         if( !EQUIV(dst_len, 0) && (dst_len < overwrite_len) ) {
1303 // in/out points or selection present and shorter than overwrite range
1304 // shorten the copy range
1305                 overwrite_len = dst_len;
1306         }
1307
1308         source->copy(copy_flags(), src_start, src_start + overwrite_len, &file, "", 1);
1309
1310 // HACK around paste_edl get_start/endselection on its own
1311 // so we need to clear only when not using both io points
1312 // FIXME: need to write simple overwrite_edl to be used for overwrite function
1313         if( edl->local_session->get_inpoint() < 0 ||
1314             edl->local_session->get_outpoint() < 0 )
1315                 edl->clear(dst_start, dst_start + overwrite_len,
1316                                 edl->session->labels_follow_edits,
1317                                 edl->session->plugins_follow_edits,
1318                                 edl->session->autos_follow_edits);
1319
1320         paste(dst_start, dst_start + overwrite_len, &file,
1321                                 edl->session->labels_follow_edits,
1322                                 edl->session->plugins_follow_edits,
1323                                 edl->session->autos_follow_edits, 0, 0);
1324
1325         edl->local_session->set_selectionstart(dst_start + overwrite_len);
1326         edl->local_session->set_selectionend(dst_start + overwrite_len);
1327
1328         save_backup();
1329         undo_after(_("overwrite"), LOAD_EDITS);
1330
1331         restart_brender();
1332         update_plugin_guis();
1333         gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
1334         sync_parameters(CHANGE_EDL);
1335 }
1336
1337 // For splice and overwrite
1338 int MWindow::paste(double start, double end, FileXML *file,
1339         int edit_labels, int edit_plugins, int edit_autos,
1340         Track *first_track, int overwrite)
1341 {
1342         clear(0);
1343
1344 // Want to insert with assets shared with the master EDL.
1345         insert(start, file,
1346                 edit_labels, edit_plugins, edit_autos,
1347                 edl, first_track, overwrite);
1348
1349         return 0;
1350 }
1351
1352 // For editing using insertion point
1353 void MWindow::paste()
1354 {
1355         paste(edl->local_session->get_selectionstart(), 0, 1, 0);
1356 }
1357
1358 void MWindow::paste(double start, Track *first_track, int clear_selection, int overwrite)
1359 {
1360         //double end = edl->local_session->get_selectionend();
1361         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1362
1363         if( len ) {
1364                 char *string = new char[len];
1365                 undo_before();
1366                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1367                 FileXML file;
1368                 file.read_from_string(string);
1369                 if( clear_selection ) clear(0);
1370
1371                 insert(start, &file,
1372                         edl->session->labels_follow_edits,
1373                         edl->session->plugins_follow_edits,
1374                         edl->session->autos_follow_edits,
1375                         0, first_track, overwrite);
1376
1377                 edl->optimize();
1378                 delete [] string;
1379
1380                 save_backup();
1381
1382                 undo_after(_("paste"), LOAD_EDITS | LOAD_TIMEBAR);
1383                 restart_brender();
1384                 update_plugin_guis();
1385                 gui->update(1, FORCE_REDRAW, 1, 1, 0, 1, 0);
1386                 awindow->gui->async_update_assets();
1387                 sync_parameters(CHANGE_EDL);
1388         }
1389
1390 }
1391
1392 int MWindow::paste_assets(double position, Track *dest_track, int overwrite)
1393 {
1394         int result = 0;
1395         undo_before();
1396
1397         if( session->drag_assets->total ) {
1398                 load_assets(session->drag_assets,
1399                         position, LOADMODE_PASTE, dest_track, 0,
1400                         edl->session->labels_follow_edits,
1401                         edl->session->plugins_follow_edits,
1402                         edl->session->autos_follow_edits,
1403                         overwrite);
1404                 result = 1;
1405         }
1406
1407         if( session->drag_clips->total ) {
1408                 paste_edls(session->drag_clips,
1409                         LOADMODE_PASTE, dest_track, position,
1410                         edl->session->labels_follow_edits,
1411                         edl->session->plugins_follow_edits,
1412                         edl->session->autos_follow_edits,
1413                         overwrite);
1414                 result = 1;
1415         }
1416
1417         save_backup();
1418
1419         undo_after(_("paste assets"), LOAD_EDITS);
1420         restart_brender();
1421         gui->update(1, FORCE_REDRAW, 1, 0, 0, 1, 0);
1422         sync_parameters(CHANGE_EDL);
1423         return result;
1424 }
1425
1426 void MWindow::load_assets(ArrayList<Indexable*> *new_assets,
1427         double position, int load_mode, Track *first_track, RecordLabels *labels,
1428         int edit_labels, int edit_plugins, int edit_autos, int overwrite)
1429 {
1430         if( load_mode == LOADMODE_RESOURCESONLY )
1431                 load_mode = LOADMODE_ASSETSONLY;
1432 const int debug = 0;
1433 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1434         if( position < 0 )
1435                 position = edl->local_session->get_selectionstart();
1436
1437         ArrayList<EDL*> new_edls;
1438         for( int i=0; i<new_assets->total; ++i ) {
1439                 Indexable *indexable = new_assets->get(i);
1440                 if( indexable->is_asset ) {
1441                         remove_from_caches(indexable);
1442                 }
1443                 EDL *new_edl = new EDL;
1444                 new_edl->create_objects();
1445                 new_edl->copy_session(edl);
1446                 if( !indexable->is_asset ) {
1447                         EDL *nested_edl = (EDL*)indexable;
1448                         new_edl->create_nested(nested_edl);
1449                         new_edl->set_path(indexable->path);
1450                 }
1451                 else {
1452                         Asset *asset = (Asset*)indexable;
1453                         asset_to_edl(new_edl, asset);
1454                 }
1455                 new_edls.append(new_edl);
1456
1457                 if( labels ) {
1458                         for( RecordLabel *label=labels->first; label; label=label->next ) {
1459                                 new_edl->labels->toggle_label(label->position, label->position);
1460                         }
1461                 }
1462         }
1463 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1464
1465         paste_edls(&new_edls, load_mode, first_track, position,
1466                 edit_labels, edit_plugins, edit_autos, overwrite);
1467 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1468
1469         save_backup();
1470         for( int i=0; i<new_edls.size(); ++i )
1471                 new_edls.get(i)->Garbage::remove_user();
1472
1473 if( debug ) printf("MWindow::load_assets %d\n", __LINE__);
1474 }
1475
1476 int MWindow::paste_automation()
1477 {
1478         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1479
1480         if( len ) {
1481                 undo_before();
1482                 speed_before();
1483                 char *string = new char[len];
1484                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1485                 FileXML file;
1486                 file.read_from_string(string);
1487                 delete [] string;
1488                 double start = edl->local_session->get_selectionstart();
1489                 double end = edl->local_session->get_selectionend();
1490                 edl->tracks->clear_automation(start, end);
1491                 edl->tracks->paste_automation(start, &file, 0, 1,
1492                         edl->session->typeless_keyframes);
1493                 int changed_edl = speed_after(1);
1494                 save_backup();
1495                 undo_after(_("paste keyframes"),
1496                         !changed_edl ? LOAD_AUTOMATION :
1497                                 LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
1498                 update_gui(changed_edl);
1499         }
1500
1501         return 0;
1502 }
1503
1504 int MWindow::paste_default_keyframe()
1505 {
1506         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
1507
1508         if( len ) {
1509                 undo_before();
1510                 speed_before();
1511                 char *string = new char[len];
1512                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
1513                 FileXML file;
1514                 file.read_from_string(string);
1515                 delete [] string;
1516                 double start = edl->local_session->get_selectionstart();
1517                 edl->tracks->paste_automation(start, &file, 1, 0,
1518                         edl->session->typeless_keyframes);
1519 //              edl->tracks->paste_default_keyframe(&file);
1520                 int changed_edl = speed_after(1);
1521                 undo_after(_("paste default keyframe"),
1522                         !changed_edl ? LOAD_AUTOMATION :
1523                                 LOAD_AUTOMATION + LOAD_EDITS + LOAD_TIMEBAR);
1524                 save_backup();
1525                 update_gui(changed_edl);
1526         }
1527
1528         return 0;
1529 }
1530
1531
1532 // Insert edls with project deletion and index file generation.
1533 int MWindow::paste_edls(ArrayList<EDL*> *new_edls, int load_mode,
1534         Track *first_track, double current_position,
1535         int edit_labels, int edit_plugins, int edit_autos,
1536         int overwrite)
1537 {
1538
1539         ArrayList<Track*> destination_tracks;
1540         int need_new_tracks = 0;
1541
1542 //PRINT_TRACE
1543         if( !new_edls->total ) return 0;
1544
1545 //PRINT_TRACE
1546 //      double original_length = edl->tracks->total_length();
1547 //      double original_preview_end = edl->local_session->preview_end;
1548 //PRINT_TRACE
1549
1550 // Delete current project
1551         if( load_mode == LOADMODE_REPLACE ||
1552             load_mode == LOADMODE_REPLACE_CONCATENATE ) {
1553                 reset_caches();
1554                 edl->save_defaults(defaults);
1555                 hide_plugins();
1556                 edl->Garbage::remove_user();
1557                 edl = new EDL;
1558                 edl->create_objects();
1559                 edl->copy_session(new_edls->values[0]);
1560                 edl->copy_mixers(new_edls->values[0]);
1561                 gui->mainmenu->update_toggles(0);
1562                 gui->unlock_window();
1563                 gwindow->gui->update_toggles(1);
1564                 gui->lock_window("MWindow::paste_edls");
1565
1566 // Insert labels for certain modes constitutively
1567                 edit_labels = 1;
1568                 edit_plugins = 1;
1569                 edit_autos = 1;
1570 // Force reset of preview
1571 //              original_length = 0;
1572 //              original_preview_end = -1;
1573         }
1574
1575
1576 //PRINT_TRACE
1577
1578 // Create new tracks in master EDL
1579         if( load_mode == LOADMODE_REPLACE ||
1580             load_mode == LOADMODE_REPLACE_CONCATENATE ||
1581             load_mode == LOADMODE_NEW_TRACKS ) {
1582
1583                 need_new_tracks = 1;
1584                 for( int i=0; i<new_edls->total; ++i ) {
1585                         EDL *new_edl = new_edls->values[i];
1586                         int first_track = 1;
1587                         for( Track *current=new_edl->tracks->first; current; current=NEXT ) {
1588                                 switch( current->data_type ) {
1589                                 case TRACK_VIDEO:
1590                                         edl->tracks->add_video_track(0, 0);
1591                                         if( current->draw ) edl->tracks->last->draw = 1;
1592                                         break;
1593                                 case TRACK_AUDIO:
1594                                         edl->tracks->add_audio_track(0, 0);
1595                                         break;
1596                                 case TRACK_SUBTITLE:
1597                                         edl->tracks->add_subttl_track(0, 0);
1598                                         break;
1599                                 default:
1600                                         continue;
1601                                 }
1602                                 if( first_track ) {
1603                                         edl->tracks->last->master = 1;
1604                                         first_track = 0;
1605                                 }
1606 // re-label only if not already labeled
1607                                 if( new_edl->local_session->asset2edl )
1608                                         strcpy(current->title, edl->tracks->last->title);
1609                                 destination_tracks.append(edl->tracks->last);
1610                         }
1611
1612 // Base track count on first EDL only for concatenation
1613                         if( load_mode == LOADMODE_REPLACE_CONCATENATE ) break;
1614                 }
1615                 edl->session->highlighted_track = edl->tracks->total() - 1;
1616         }
1617         else
1618 // Recycle existing tracks of master EDL
1619         if( load_mode == LOADMODE_CONCATENATE ||
1620             load_mode == LOADMODE_PASTE ) {
1621                 Track *current = first_track ? first_track : edl->tracks->first;
1622                 for( ; current; current=NEXT ) {
1623                         if( current->is_armed() ) {
1624                                 destination_tracks.append(current);
1625                         }
1626                 }
1627 //PRINT_TRACE
1628         }
1629 //PRINT_TRACE
1630         int destination_track = 0;
1631         double *paste_position = new double[destination_tracks.total];
1632
1633 // Iterate through the edls
1634         for( int i=0; i<new_edls->total; ++i ) {
1635
1636                 EDL *new_edl = new_edls->values[i];
1637                 double edl_length = new_edl->local_session->clipboard_length ?
1638                         new_edl->local_session->clipboard_length :
1639                         new_edl->tracks->total_length();
1640 // printf("MWindow::paste_edls 2 %f %f\n",
1641 // new_edl->local_session->clipboard_length,
1642 // new_edl->tracks->total_length());
1643 // new_edl->dump();
1644 //PRINT_TRACE
1645
1646 // Convert EDL to master rates
1647                 new_edl->resample(new_edl->session->sample_rate,
1648                         edl->session->sample_rate,
1649                         TRACK_AUDIO);
1650                 new_edl->resample(new_edl->session->frame_rate,
1651                         edl->session->frame_rate,
1652                         TRACK_VIDEO);
1653 //PRINT_TRACE
1654 // Add assets and prepare index files
1655                 for( Asset *new_asset=new_edl->assets->first;
1656                      new_asset; new_asset=new_asset->next ) {
1657                         mainindexes->add_indexable(new_asset);
1658                 }
1659 // Capture index file status from mainindex test
1660                 edl->update_assets(new_edl);
1661 //PRINT_TRACE
1662 // Get starting point of insertion.  Need this to paste labels.
1663                 switch( load_mode ) {
1664                 case LOADMODE_NOTHING:
1665                         continue;
1666                 case LOADMODE_REPLACE:
1667                         current_position = 0;
1668                         break;
1669                 case LOADMODE_NEW_TRACKS:
1670                         if( !overwrite )
1671                                 current_position = 0;
1672                         break;
1673
1674                 case LOADMODE_CONCATENATE:
1675                 case LOADMODE_REPLACE_CONCATENATE:
1676                         destination_track = 0;
1677                         if( destination_tracks.total )
1678                                 current_position = destination_tracks.values[0]->get_length();
1679                         else
1680                                 current_position = 0;
1681                         break;
1682
1683                 case LOADMODE_PASTE:
1684                         destination_track = 0;
1685                         if( i == 0 ) {
1686                                 for( int j=0; j<destination_tracks.total; ++j ) {
1687                                         paste_position[j] = (current_position >= 0) ?
1688                                                 current_position :
1689                                                 edl->local_session->get_selectionstart();
1690                                 }
1691                         }
1692                         break;
1693
1694                 case LOADMODE_RESOURCESONLY:
1695                         edl->add_clip(new_edl);
1696                         break;
1697                 }
1698 // Insert edl
1699                 if( load_mode != LOADMODE_RESOURCESONLY &&
1700                     load_mode != LOADMODE_ASSETSONLY ) {
1701 // Insert labels
1702                         if( edit_labels ) {
1703                                 if( load_mode == LOADMODE_PASTE )
1704                                         edl->labels->insert_labels(new_edl->labels,
1705                                                 destination_tracks.total ? paste_position[0] : 0.0,
1706                                                 edl_length, 1);
1707                                 else
1708                                         edl->labels->insert_labels(new_edl->labels, current_position,
1709                                                 edl_length, 1);
1710                         }
1711                         double total_length = new_edl->tracks->total_length();
1712                         for( Track *new_track=new_edl->tracks->first;
1713                              new_track; new_track=new_track->next ) {
1714 // Get destination track of same type as new_track
1715                                 for( int k = 0;
1716                                      k < destination_tracks.total &&
1717                                      destination_tracks.values[destination_track]->data_type != new_track->data_type;
1718                                      ++k, ++destination_track ) {
1719                                         if( destination_track >= destination_tracks.total - 1 )
1720                                                 destination_track = 0;
1721                                 }
1722
1723 // Insert data into destination track
1724                                 if( destination_track < destination_tracks.total &&
1725                                     destination_tracks.values[destination_track]->data_type == new_track->data_type ) {
1726                                         Track *track = destination_tracks.values[destination_track];
1727 // Replace default keyframes if first EDL and new tracks were created.
1728 // This means data copied from one track and pasted to another won't retain
1729 // the camera position unless it's a keyframe.  If it did, previous data in the
1730 // track might get unknowingly corrupted.  Ideally we would detect when differing
1731 // default keyframes existed and create discrete keyframes for both.
1732                                         int replace_default = (i == 0) && need_new_tracks;
1733 // master tracks are the first track in each new edl when new tracks are created
1734                                         int master = track->master;
1735 //printf("MWindow::paste_edls 1 %d\n", replace_default);
1736 // Insert new track at current position
1737                                         switch( load_mode ) {
1738                                         case LOADMODE_REPLACE_CONCATENATE:
1739                                         case LOADMODE_CONCATENATE:
1740                                                 current_position = track->get_length();
1741                                                 break;
1742
1743                                         case LOADMODE_PASTE:
1744                                                 current_position = paste_position[destination_track];
1745                                                 paste_position[destination_track] += new_track->get_length();
1746                                                 break;
1747                                         }
1748                                         if( overwrite ) {
1749                                                 double length = overwrite >= 0 ?
1750                                                         new_track->get_length() : total_length;
1751                                                 track->clear(current_position,
1752                                                         current_position + length,
1753                                                         1, // edit edits
1754                                                         edit_labels, edit_plugins, edit_autos,
1755                                                         0); // trim edits
1756                                         }
1757 //PRINT_TRACE
1758                                         track->insert_track(new_track, current_position, replace_default,
1759                                                 edit_plugins, edit_autos, edl_length);
1760                                         if( master ) track->master = 1;
1761 //PRINT_TRACE
1762                                 }
1763
1764 // Get next destination track
1765                                 destination_track++;
1766                                 if( destination_track >= destination_tracks.total )
1767                                         destination_track = 0;
1768                         }
1769                 }
1770
1771                 if( load_mode == LOADMODE_PASTE )
1772                         current_position += edl_length;
1773         }
1774
1775
1776 // Move loading of clips and vwindow to the end - this fixes some
1777 // strange issue, for index not being shown
1778 // Assume any paste operation from the same EDL won't contain any clips.
1779 // If it did it would duplicate every clip here.
1780         for( int i=0; i<new_edls->total; ++i ) {
1781                 EDL *new_edl = new_edls->get(i);
1782
1783                 for( int j=0; j<new_edl->clips.size(); ++j ) {
1784                         edl->add_clip(new_edl->clips[j]);
1785                 }
1786                 for( int j=0; j<new_edl->nested_edls.size(); ++j ) {
1787                         edl->nested_edls.get_nested(new_edl->nested_edls[j]);
1788                 }
1789
1790                 if( new_edl->total_vwindow_edls() ) {
1791 //                      if( edl->vwindow_edl )
1792 //                              edl->vwindow_edl->Garbage::remove_user();
1793 //                      edl->vwindow_edl = new EDL(edl);
1794 //                      edl->vwindow_edl->create_objects();
1795 //                      edl->vwindow_edl->copy_all(new_edl->vwindow_edl);
1796
1797                         for( int j=0; j<new_edl->total_vwindow_edls(); ++j ) {
1798                                 EDL *vwindow_edl = new EDL(edl);
1799                                 vwindow_edl->create_objects();
1800                                 vwindow_edl->copy_all(new_edl->get_vwindow_edl(j));
1801                                 edl->append_vwindow_edl(vwindow_edl, 0);
1802                         }
1803                 }
1804         }
1805
1806         if( paste_position ) delete [] paste_position;
1807
1808 // This is already done in load_filenames and everything else that uses paste_edls
1809 //      update_project(load_mode);
1810
1811 // Fix preview range
1812 //      if( EQUIV(original_length, original_preview_end) )
1813 //      {
1814 //              edl->local_session->preview_end = edl->tracks->total_length();
1815 //      }
1816
1817 // Start examining next batch of index files
1818         mainindexes->start_build();
1819
1820 // Don't save a backup after loading since the loaded file is on disk already.
1821 //PRINT_TRACE
1822         return 0;
1823 }
1824
1825 void MWindow::paste_silence()
1826 {
1827         double start = edl->local_session->get_selectionstart();
1828         double end = edl->local_session->get_selectionend();
1829         if( EQUIV(start, end) ) {
1830                 if( edl->session->frame_rate > 0 )
1831                         end += 1./edl->session->frame_rate;
1832         }
1833         undo_before(_("silence"), this);
1834         edl->paste_silence(start, end,
1835                 edl->session->labels_follow_edits,
1836                 edl->session->plugins_follow_edits,
1837                 edl->session->autos_follow_edits);
1838         edl->optimize();
1839         save_backup();
1840         undo_after(_("silence"), LOAD_EDITS | LOAD_TIMEBAR);
1841
1842         update_plugin_guis();
1843         restart_brender();
1844         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
1845         cwindow->update(1, 0, 0, 0, 1);
1846         cwindow->refresh_frame(CHANGE_EDL);
1847 }
1848
1849 void MWindow::detach_transition(Transition *transition)
1850 {
1851         undo_before();
1852         hide_plugin(transition, 1);
1853         int is_video = (transition->edit->track->data_type == TRACK_VIDEO);
1854         transition->edit->detach_transition();
1855         save_backup();
1856         undo_after(_("detach transition"), LOAD_ALL);
1857
1858         if( is_video ) restart_brender();
1859         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1860         sync_parameters(CHANGE_EDL);
1861 }
1862
1863 void MWindow::detach_transitions()
1864 {
1865         gui->lock_window("MWindow::detach_transitions 1");
1866
1867         undo_before();
1868         double start = edl->local_session->get_selectionstart();
1869         double end = edl->local_session->get_selectionend();
1870         edl->tracks->clear_transitions(start, end);
1871
1872         save_backup();
1873         undo_after(_("detach transitions"), LOAD_EDITS);
1874
1875         sync_parameters(CHANGE_EDL);
1876         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1877         gui->unlock_window();
1878 }
1879
1880 void MWindow::paste_transition()
1881 {
1882 // Only the first transition gets dropped.
1883         PluginServer *server = session->drag_pluginservers->values[0];
1884
1885         undo_before();
1886         edl->tracks->paste_transition(server, session->edit_highlighted);
1887         save_backup();
1888         undo_after(_("transition"), LOAD_EDITS);
1889
1890         if( server->video ) restart_brender();
1891         sync_parameters(CHANGE_ALL);
1892 }
1893
1894 void MWindow::paste_transitions(int track_type, char *title)
1895 {
1896         gui->lock_window("MWindow::detach_transitions 1");
1897
1898         undo_before();
1899         double start = edl->local_session->get_selectionstart();
1900         double end = edl->local_session->get_selectionend();
1901         edl->tracks->paste_transitions(start, end, track_type, title);
1902         save_backup();
1903         undo_after(_("attach transitions"), LOAD_EDITS);
1904
1905         sync_parameters(CHANGE_EDL);
1906         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1907         gui->unlock_window();
1908 }
1909
1910 void MWindow::paste_transition_cwindow(Track *dest_track)
1911 {
1912         PluginServer *server = session->drag_pluginservers->values[0];
1913         undo_before();
1914         edl->tracks->paste_video_transition(server, 1);
1915         save_backup();
1916         undo_after(_("transition"), LOAD_EDITS);
1917         restart_brender();
1918         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1919         sync_parameters(CHANGE_ALL);
1920 }
1921
1922 void MWindow::paste_audio_transition()
1923 {
1924         PluginServer *server = scan_plugindb(edl->session->default_atransition,
1925                 TRACK_AUDIO);
1926         if( !server ) {
1927                 char string[BCTEXTLEN];
1928                 sprintf(string, _("No default transition %s found."), edl->session->default_atransition);
1929                 gui->show_message(string);
1930                 return;
1931         }
1932
1933         undo_before();
1934         edl->tracks->paste_audio_transition(server);
1935         save_backup();
1936         undo_after(_("transition"), LOAD_EDITS);
1937
1938         sync_parameters(CHANGE_EDL);
1939         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1940 }
1941
1942 void MWindow::paste_video_transition()
1943 {
1944         PluginServer *server = scan_plugindb(edl->session->default_vtransition,
1945                 TRACK_VIDEO);
1946         if( !server ) {
1947                 char string[BCTEXTLEN];
1948                 sprintf(string, _("No default transition %s found."), edl->session->default_vtransition);
1949                 gui->show_message(string);
1950                 return;
1951         }
1952
1953         undo_before();
1954
1955         edl->tracks->paste_video_transition(server);
1956         save_backup();
1957         undo_after(_("transition"), LOAD_EDITS);
1958
1959         sync_parameters(CHANGE_EDL);
1960         restart_brender();
1961         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
1962 }
1963
1964 void MWindow::shuffle_edits()
1965 {
1966         gui->lock_window("MWindow::shuffle_edits 1");
1967
1968         undo_before();
1969         double start = edl->local_session->get_selectionstart();
1970         double end = edl->local_session->get_selectionend();
1971
1972         edl->tracks->shuffle_edits(start, end);
1973
1974         save_backup();
1975         undo_after(_("shuffle edits"), LOAD_EDITS | LOAD_TIMEBAR);
1976
1977         sync_parameters(CHANGE_EDL);
1978         restart_brender();
1979         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
1980         gui->unlock_window();
1981 }
1982
1983 void MWindow::reverse_edits()
1984 {
1985         gui->lock_window("MWindow::reverse_edits 1");
1986
1987         undo_before();
1988         double start = edl->local_session->get_selectionstart();
1989         double end = edl->local_session->get_selectionend();
1990
1991         edl->tracks->reverse_edits(start, end);
1992
1993         save_backup();
1994         undo_after(_("reverse edits"), LOAD_EDITS | LOAD_TIMEBAR);
1995
1996         sync_parameters(CHANGE_EDL);
1997         restart_brender();
1998         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
1999         gui->unlock_window();
2000 }
2001
2002 void MWindow::align_edits()
2003 {
2004         gui->lock_window("MWindow::align_edits 1");
2005
2006         undo_before();
2007         double start = edl->local_session->get_selectionstart();
2008         double end = edl->local_session->get_selectionend();
2009
2010         edl->tracks->align_edits(start, end);
2011
2012         save_backup();
2013         undo_after(_("align edits"), LOAD_EDITS | LOAD_TIMEBAR);
2014
2015         sync_parameters(CHANGE_EDL);
2016         restart_brender();
2017         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
2018         gui->unlock_window();
2019 }
2020
2021 void MWindow::set_edit_length(double length)
2022 {
2023         gui->lock_window("MWindow::set_edit_length 1");
2024
2025         undo_before();
2026         double start = edl->local_session->get_selectionstart();
2027         double end = edl->local_session->get_selectionend();
2028
2029         edl->tracks->set_edit_length(start, end, length);
2030
2031         save_backup();
2032         undo_after(_("edit length"), LOAD_EDITS | LOAD_TIMEBAR);
2033
2034         sync_parameters(CHANGE_EDL);
2035         restart_brender();
2036         gui->update(0, NORMAL_DRAW, 1, 0, 0, 0, 0);
2037         gui->unlock_window();
2038 }
2039
2040
2041 void MWindow::set_transition_length(Transition *transition, double length)
2042 {
2043         gui->lock_window("MWindow::set_transition_length 1");
2044
2045         undo_before();
2046         //double start = edl->local_session->get_selectionstart();
2047         //double end = edl->local_session->get_selectionend();
2048
2049         edl->tracks->set_transition_length(transition, length);
2050
2051         save_backup();
2052         undo_after(_("transition length"), LOAD_EDITS);
2053
2054         edl->session->default_transition_length = length;
2055         sync_parameters(CHANGE_PARAMS);
2056         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2057         gui->unlock_window();
2058 }
2059
2060 void MWindow::set_transition_length(double length)
2061 {
2062         gui->lock_window("MWindow::set_transition_length 2");
2063
2064         undo_before();
2065         double start = edl->local_session->get_selectionstart();
2066         double end = edl->local_session->get_selectionend();
2067
2068         edl->tracks->set_transition_length(start, end, length);
2069
2070         save_backup();
2071         undo_after(_("transition length"), LOAD_EDITS);
2072
2073         edl->session->default_transition_length = length;
2074         sync_parameters(CHANGE_PARAMS);
2075         restart_brender();
2076         gui->update(0, NORMAL_DRAW, 0, 0, 0, 0, 0);
2077         gui->unlock_window();
2078 }
2079
2080
2081 void MWindow::redo_entry(BC_WindowBase *calling_window_gui)
2082 {
2083         calling_window_gui->unlock_window();
2084         stop_playback(0);
2085         if( undo->redo_load_flags() & LOAD_SESSION )
2086                 close_mixers();
2087
2088         cwindow->gui->lock_window("MWindow::redo_entry 1");
2089         for( int i=0; i<vwindows.size(); ++i ) {
2090                 if( vwindows.get(i)->is_running() ) {
2091                         if( calling_window_gui != vwindows.get(i)->gui ) {
2092                                 vwindows.get(i)->gui->lock_window("MWindow::redo_entry 2");
2093                         }
2094                 }
2095         }
2096         gui->lock_window("MWindow::redo_entry 3");
2097
2098         undo->redo();
2099
2100         save_backup();
2101         restart_brender();
2102         update_plugin_states();
2103         update_plugin_guis();
2104
2105         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 1);
2106         gui->update_proxy_toggle();
2107         gui->unlock_window();
2108         cwindow->gui->unlock_window();
2109         cwindow->update(1, 1, 1, 1, 1);
2110
2111         for( int i=0; i < vwindows.size(); ++i ) {
2112                 if( vwindows.get(i)->is_running() ) {
2113                         if( calling_window_gui != vwindows.get(i)->gui ) {
2114                                 vwindows.get(i)->gui->unlock_window();
2115                         }
2116                 }
2117         }
2118
2119         awindow->gui->async_update_assets();
2120
2121         cwindow->refresh_frame(CHANGE_ALL);
2122         calling_window_gui->lock_window("MWindow::redo_entry 4");
2123 }
2124
2125
2126 void MWindow::resize_track(Track *track, int w, int h)
2127 {
2128         undo_before();
2129 // We have to move all maskpoints so they do not move in relation to image areas
2130         ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->translate_masks(
2131                 (w - track->track_w) / 2,
2132                 (h - track->track_h) / 2);
2133         track->track_w = w;
2134         track->track_h = h;
2135         undo_after(_("resize track"), LOAD_ALL);
2136         save_backup();
2137
2138         restart_brender();
2139         sync_parameters(CHANGE_EDL);
2140 }
2141
2142
2143 void MWindow::set_inpoint()
2144 {
2145         undo_before();
2146         edl->set_inpoint(edl->local_session->get_selectionstart(1));
2147         save_backup();
2148         undo_after(_("in point"), LOAD_TIMEBAR);
2149         gui->update_timebar(1);
2150
2151         cwindow->gui->lock_window("MWindow::set_inpoint 2");
2152         cwindow->gui->timebar->update(1);
2153         cwindow->gui->unlock_window();
2154 }
2155
2156 void MWindow::set_outpoint()
2157 {
2158         undo_before();
2159         edl->set_outpoint(edl->local_session->get_selectionend(1));
2160         save_backup();
2161         undo_after(_("out point"), LOAD_TIMEBAR);
2162
2163         gui->update_timebar(1);
2164         cwindow->gui->lock_window("MWindow::set_outpoint 2");
2165         cwindow->gui->timebar->update(1);
2166         cwindow->gui->unlock_window();
2167 }
2168
2169 void MWindow::unset_inoutpoint()
2170 {
2171         undo_before();
2172         edl->unset_inoutpoint();
2173         save_backup();
2174         undo_after(_("clear in/out"), LOAD_TIMEBAR);
2175
2176         gui->update_timebar(1);
2177         cwindow->gui->lock_window("MWindow::unset_inoutpoint 2");
2178         cwindow->gui->timebar->update(1);
2179         cwindow->gui->unlock_window();
2180 }
2181
2182 void MWindow::splice(EDL *source, int all)
2183 {
2184         FileXML file;
2185         LocalSession *src = source->local_session;
2186
2187         undo_before();
2188         double source_start = all ? 0 :
2189                 src->inpoint_valid() ? src->get_inpoint() :
2190                 src->outpoint_valid() ? 0 : src->get_selectionstart();
2191         double source_end = all ? source->tracks->total_length() :
2192                 src->outpoint_valid() ? src->get_outpoint() :
2193                 src->inpoint_valid() ? source->tracks->total_length() :
2194                         src->get_selectionend();
2195         source->copy(COPY_EDL, source_start, source_end, &file, "", 1);
2196 //file.dump();
2197         double start = edl->local_session->get_selectionstart();
2198         //double end = edl->local_session->get_selectionend();
2199
2200         paste(start, start, &file,
2201                 edl->session->labels_follow_edits,
2202                 edl->session->plugins_follow_edits,
2203                 edl->session->autos_follow_edits,
2204                 0, 0);
2205
2206 // Position at end of clip
2207         edl->local_session->set_selectionstart(start + source_end - source_start);
2208         edl->local_session->set_selectionend(start + source_end - source_start);
2209
2210         save_backup();
2211         undo_after(_("splice"), LOAD_EDITS | LOAD_TIMEBAR);
2212         update_plugin_guis();
2213         restart_brender();
2214         gui->update(1, NORMAL_DRAW, 1, 1, 0, 1, 0);
2215         sync_parameters(CHANGE_EDL);
2216 }
2217
2218 void MWindow::save_clip(EDL *new_edl, const char *txt)
2219 {
2220         new_edl->local_session->set_selectionstart(0);
2221         new_edl->local_session->set_selectionend(0);
2222         sprintf(new_edl->local_session->clip_title, _("Clip %d"),
2223                 session->clip_number++);
2224         char duration[BCTEXTLEN];
2225         Units::totext(duration, new_edl->tracks->total_length(),
2226                 new_edl->session->time_format,
2227                 new_edl->session->sample_rate,
2228                 new_edl->session->frame_rate,
2229                 new_edl->session->frames_per_foot);
2230
2231         Track *track = new_edl->tracks->first;
2232         const char *path = edl->path;
2233         for( ; (!path || !*path) && track; track=track->next ) {
2234                 if( !track->is_armed() ) continue;
2235                 Edit *edit = track->edits->first;
2236                 if( !edit ) continue;
2237                 Indexable *indexable = edit->get_source();
2238                 if( !indexable ) continue;
2239                 path = indexable->path;
2240         }
2241
2242         time_t now;  time(&now);
2243         struct tm dtm;   localtime_r(&now, &dtm);
2244         char *cp = new_edl->local_session->clip_notes;
2245         char *ep = cp + sizeof(new_edl->local_session->clip_notes)-1;
2246         if( txt && *txt )
2247                 cp += snprintf(cp, ep-cp, "%s", txt);
2248         cp += snprintf(cp, ep-cp,
2249                 "%02d/%02d/%02d %02d:%02d:%02d,  +%s\n",
2250                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
2251                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec, duration);
2252         if( path && *path ) {
2253                 FileSystem fs;
2254                 char title[BCTEXTLEN];
2255                 fs.extract_name(title, path);
2256                 cp += snprintf(cp, ep-cp, "%s", title);
2257         }
2258         sprintf(new_edl->local_session->clip_icon,
2259                 "clip_%02d%02d%02d-%02d%02d%02d-%d.png",
2260                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
2261                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec,
2262                 new_edl->id);
2263         new_edl->folder_no = AW_CLIP_FOLDER;
2264         edl->update_assets(new_edl);
2265         int cur_x, cur_y;
2266         gui->get_abs_cursor(cur_x, cur_y, 0);
2267         gui->unlock_window();
2268
2269         awindow->clip_edit->create_clip(new_edl, cur_x, cur_y);
2270         new_edl->remove_user();
2271
2272         gui->lock_window("MWindow::save_clip");
2273         save_backup();
2274 }
2275
2276 void MWindow::to_clip(EDL *edl, const char *txt, int all)
2277 {
2278         FileXML file;
2279         LocalSession *src = edl->local_session;
2280
2281         gui->lock_window("MWindow::to_clip 1");
2282         double start = all ? 0 :
2283                 src->inpoint_valid() ? src->get_inpoint() :
2284                 src->outpoint_valid() ? 0 : src->get_selectionstart();
2285         double end = all ? edl->tracks->total_length() :
2286                 src->outpoint_valid() ? src->get_outpoint() :
2287                 src->inpoint_valid() ? edl->tracks->total_length() :
2288                         src->get_selectionend();
2289         if( EQUIV(end, start) ) {
2290                 start = 0;
2291                 end = edl->tracks->total_length();
2292         }
2293
2294 // Don't copy all since we don't want the clips twice.
2295         edl->copy(copy_flags(), start, end, &file, "", 1);
2296
2297         EDL *new_edl = new EDL(edl);
2298         new_edl->create_objects();
2299         new_edl->load_xml(&file, LOAD_ALL);
2300         save_clip(new_edl, txt);
2301         gui->unlock_window();
2302 }
2303
2304 int MWindow::toggle_label()
2305 {
2306         double position1, position2;
2307         undo_before();
2308
2309         position1 = edl->local_session->get_selectionstart(1);
2310         position2 = edl->local_session->get_selectionend(1);
2311         position1 = edl->align_to_frame(position1, 0);
2312         position2 = edl->align_to_frame(position2, 0);
2313
2314         edl->labels->toggle_label(position1, position2);
2315         save_backup();
2316
2317         gui->update_timebar(0);
2318         gui->activate_timeline();
2319         gui->flush();
2320
2321         cwindow->gui->lock_window("MWindow::toggle_label 2");
2322         cwindow->gui->timebar->update(1);
2323         cwindow->gui->unlock_window();
2324         awindow->gui->async_update_assets();
2325
2326         undo_after(_("label"), LOAD_TIMEBAR);
2327         return 0;
2328 }
2329
2330 void MWindow::trim_selection()
2331 {
2332         undo_before();
2333
2334
2335         edl->trim_selection(edl->local_session->get_selectionstart(),
2336                 edl->local_session->get_selectionend(),
2337                 edl->session->labels_follow_edits,
2338                 edl->session->plugins_follow_edits,
2339                 edl->session->autos_follow_edits);
2340
2341         save_backup();
2342         undo_after(_("trim selection"), LOAD_EDITS | LOAD_TIMEBAR);
2343         update_plugin_guis();
2344         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2345         cwindow->update(1, 0, 0, 0, 1);
2346         awindow->gui->async_update_assets();
2347         restart_brender();
2348         cwindow->refresh_frame(CHANGE_EDL);
2349 }
2350
2351
2352 void MWindow::undo_entry(BC_WindowBase *calling_window_gui)
2353 {
2354         calling_window_gui->unlock_window();
2355         stop_playback(0);
2356         if( undo->undo_load_flags() & LOAD_SESSION )
2357                 close_mixers();
2358
2359         cwindow->gui->lock_window("MWindow::undo_entry 1");
2360         for( int i=0; i<vwindows.size(); ++i ) {
2361                 if( vwindows.get(i)->is_running() ) {
2362                         if( calling_window_gui != vwindows.get(i)->gui ) {
2363                                 vwindows.get(i)->gui->lock_window("MWindow::undo_entry 4");
2364                         }
2365                 }
2366         }
2367         gui->lock_window("MWindow::undo_entry 2");
2368
2369         undo->undo();
2370
2371         save_backup();
2372         restart_brender();
2373         update_plugin_states();
2374         update_plugin_guis();
2375
2376         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 1);
2377         gui->update_proxy_toggle();
2378         gui->unlock_window();
2379         cwindow->gui->unlock_window();
2380         cwindow->update(1, 1, 1, 1, 1);
2381
2382         for( int i=0; i<vwindows.size(); ++i ) {
2383                 if( vwindows.get(i)->is_running() ) {
2384                         if( calling_window_gui != vwindows.get(i)->gui ) {
2385                                 vwindows.get(i)->gui->unlock_window();
2386                         }
2387                 }
2388         }
2389
2390         awindow->gui->async_update_assets();
2391
2392         cwindow->refresh_frame(CHANGE_ALL);
2393         calling_window_gui->lock_window("MWindow::undo_entry 4");
2394 }
2395
2396
2397 void MWindow::new_folder(const char *new_folder, int is_clips)
2398 {
2399         undo_before();
2400         if( edl->new_folder(new_folder, is_clips) ) {
2401                 MainError::show_error(_("create new folder failed"));
2402         }
2403         undo_after(_("new folder"), LOAD_ALL);
2404         awindow->gui->async_update_assets();
2405 }
2406
2407 void MWindow::delete_folder(char *folder)
2408 {
2409         undo_before();
2410         if( edl->delete_folder(folder) < 0 ) {
2411                 MainError::show_error(_("delete folder failed"));
2412         }
2413         undo_after(_("del folder"), LOAD_ALL);
2414         awindow->gui->async_update_assets();
2415 }
2416
2417 void MWindow::select_point(double position)
2418 {
2419         gui->unlock_window();
2420         gui->stop_drawing();
2421         cwindow->stop_playback(0);
2422         gui->lock_window("MWindow::select_point");
2423
2424         edl->local_session->set_selectionstart(position);
2425         edl->local_session->set_selectionend(position);
2426
2427 // Que the CWindow
2428         cwindow->update(1, 0, 0, 0, 1);
2429
2430         update_plugin_guis();
2431         gui->update_patchbay();
2432         gui->hide_cursor(0);
2433         gui->draw_cursor(0);
2434         gui->mainclock->update(edl->local_session->get_selectionstart(1));
2435         gui->zoombar->update();
2436         gui->update_timebar(0);
2437         gui->flash_canvas(0);
2438         gui->flush();
2439 }
2440
2441
2442
2443
2444 void MWindow::map_audio(int pattern)
2445 {
2446         undo_before();
2447         remap_audio(pattern);
2448         undo_after(
2449                 pattern == MWindow::AUDIO_1_TO_1 ? _("map 1:1") : _("map 5.1:2"),
2450                 LOAD_AUTOMATION);
2451         sync_parameters(CHANGE_PARAMS);
2452         gui->update(0, NORMAL_DRAW, 0, 0, 1, 0, 0);
2453 }
2454
2455 void MWindow::remap_audio(int pattern)
2456 {
2457         int current_channel = 0;
2458         int current_track = 0;
2459         for( Track *current=edl->tracks->first; current; current=NEXT ) {
2460                 if( current->data_type == TRACK_AUDIO &&
2461                         current->is_armed() ) {
2462                         Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
2463                         PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
2464
2465                         for( int i=0; i < MAXCHANNELS; ++i ) {
2466                                 pan_auto->values[i] = 0.0;
2467                         }
2468
2469                         if( pattern == MWindow::AUDIO_1_TO_1 ) {
2470                                 pan_auto->values[current_channel] = 1.0;
2471                         }
2472                         else
2473                         if( pattern == MWindow::AUDIO_5_1_TO_2 ) {
2474                                 switch( current_track ) {
2475                                 case 0:
2476                                 case 4:
2477                                         pan_auto->values[0] = 1;
2478                                         break;
2479                                 case 1:
2480                                 case 5:
2481                                         pan_auto->values[1] = 1;
2482                                         break;
2483                                 case 2:
2484                                 case 3:
2485                                         pan_auto->values[0] = 0.5;
2486                                         pan_auto->values[1] = 0.5;
2487                                         break;
2488                                 }
2489                         }
2490
2491                         BC_Pan::calculate_stick_position(edl->session->audio_channels,
2492                                 edl->session->achannel_positions, pan_auto->values,
2493                                 MAX_PAN, PAN_RADIUS, pan_auto->handle_x, pan_auto->handle_y);
2494
2495                         current_channel++;
2496                         current_track++;
2497                         if( current_channel >= edl->session->audio_channels )
2498                                 current_channel = 0;
2499                 }
2500         }
2501 }
2502
2503
2504 void MWindow::rescale_proxy(EDL *clip, int orig_scale, int new_scale)
2505 {
2506         edl->rescale_proxy(orig_scale, new_scale);
2507 }
2508
2509 void MWindow::add_proxy(ArrayList<Indexable*> *orig_assets, ArrayList<Indexable*> *proxy_assets)
2510 {
2511         edl->add_proxy(orig_assets, proxy_assets);
2512 }
2513
2514 void MWindow::start_convert(Asset *format_asset, const char *suffix,
2515                 float beep, int to_proxy, int remove_originals)
2516 {
2517         if( !convert_render )
2518                 convert_render = new ConvertRender(this);
2519         convert_render->set_format(format_asset, suffix, to_proxy);
2520         int found = convert_render->find_convertable_assets(edl);
2521         if( convert_render->needed_idxbls.size() > 0 )
2522                 convert_render->start_convert(beep, remove_originals);
2523         else if( found > 0 )
2524                 finish_convert(remove_originals);
2525         else if( found < 0 )
2526                 eprintf(_("convert assets format error"));
2527         else
2528                 eprintf(_("No convertable assets found"));
2529 }
2530
2531 void MWindow::finish_convert(int remove_originals)
2532 {
2533         gui->lock_window("MWindow::finish_convert");
2534         undo_before();
2535         edl->replace_assets(
2536                 convert_render->orig_idxbls,
2537                 convert_render->orig_copies);
2538         if( remove_originals ) {
2539                 remove_assets_from_project(0, 0, 0,
2540                         &convert_render->orig_idxbls, 0);
2541         }
2542         save_backup();
2543         undo_after(_("convert"), LOAD_ALL);
2544
2545         update_plugin_guis();
2546         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2547         cwindow->update(1, 0, 0, 0, 1);
2548         awindow->gui->async_update_assets();
2549         cwindow->refresh_frame(CHANGE_EDL);
2550         gui->unlock_window();
2551 }
2552
2553 void MWindow::cut_commercials()
2554 {
2555 #ifdef HAVE_COMMERCIAL
2556         undo_before();
2557         commercials->scan_media();
2558         edl->optimize();
2559         save_backup();
2560         undo_after(_("cut ads"), LOAD_EDITS | LOAD_TIMEBAR);
2561
2562         restart_brender();
2563         update_plugin_guis();
2564         gui->update(1, FORCE_REDRAW, 1, 1, 1, 1, 0);
2565         cwindow->update(1, 0, 0, 0, 1);
2566         cwindow->refresh_frame(CHANGE_EDL);
2567 #endif
2568 }
2569
2570 int MWindow::normalize_speed(EDL *old_edl, EDL *new_edl)
2571 {
2572         int result = 0;
2573         Track *old_track = old_edl->tracks->first;
2574         Track *new_track = new_edl->tracks->first;
2575         for( ; old_track && new_track; old_track=old_track->next, new_track=new_track->next ) {
2576                 if( old_track->data_type != new_track->data_type ) continue;
2577                 FloatAutos *old_speeds = (FloatAutos *)old_track->automation->autos[AUTOMATION_SPEED];
2578                 FloatAutos *new_speeds = (FloatAutos *)new_track->automation->autos[AUTOMATION_SPEED];
2579                 if( !old_speeds || !new_speeds ) continue;
2580                 FloatAuto *old_speed = (FloatAuto *)old_speeds->first;
2581                 FloatAuto *new_speed = (FloatAuto *)new_speeds->first;
2582                 while( old_speed && new_speed && old_speed->equals(new_speed) ) {
2583                         old_speed = (FloatAuto *)old_speed->next;
2584                         new_speed = (FloatAuto *)new_speed->next;
2585                 }
2586                 Edit *old_edit = old_track->edits->first;
2587                 Edit *new_edit = new_track->edits->first;
2588                 for( ; old_edit && new_edit; old_edit=old_edit->next, new_edit=new_edit->next ) {
2589                         int64_t edit_start = old_edit->startproject, edit_end = edit_start + old_edit->length;
2590                         if( old_speed || new_speed ) {
2591                                 double orig_start = old_speeds->automation_integral(0, edit_start, PLAY_FORWARD);
2592                                 double orig_end   = old_speeds->automation_integral(0, edit_end, PLAY_FORWARD);
2593                                 edit_start = new_speeds->speed_position(orig_start);
2594                                 edit_end = new_speeds->speed_position(orig_end);
2595                                 result = 1;
2596                         }
2597                         new_edit->startproject = edit_start;
2598                         new_edit->length = edit_end - edit_start;
2599                 }
2600         }
2601         return result;
2602 }
2603
2604 void MWindow::speed_before()
2605 {
2606         if( !speed_edl ) {
2607                 speed_edl = new EDL;
2608                 speed_edl->create_objects();
2609         }
2610         speed_edl->copy_all(edl);
2611 }
2612
2613 int MWindow::speed_after(int done)
2614 {
2615         int result = 0;
2616         if( speed_edl ) {
2617                 if( done >= 0 )
2618                         result = normalize_speed(speed_edl, edl);
2619                 if( done != 0 ) {
2620                         speed_edl->remove_user();
2621                         speed_edl = 0;
2622                 }
2623         }
2624         return result;
2625 }
2626
2627 void MWindow::collect_effects()
2628 {
2629         FileXML file;
2630         const char *file_string = "";
2631         EDL *group = 0;
2632         int ret = edl->collect_effects(group);
2633         switch( ret ) {
2634         case COLLECT_EFFECTS_RECORD:
2635                 eprintf(_("Selected edit track not armed."));
2636                 break;
2637         case COLLECT_EFFECTS_MULTIPLE:
2638                 eprintf(_("More than one edit selected on a track."));
2639                 break;
2640         case COLLECT_EFFECTS_MISSING:
2641                 eprintf(_("No effects under selected edit."));
2642                 break;
2643         case COLLECT_EFFECTS_EMPTY:
2644                 eprintf(_("No edits selected."));
2645                 break;
2646         case COLLECT_EFFECTS_MASTER:
2647                 eprintf(_("Shared effect added without master."));
2648                 break;
2649         case 0:
2650                 group->save_xml(&file, "");
2651                 file_string = file.string();
2652                 group->remove_user();
2653         }
2654         long file_length = strlen(file_string);
2655         gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
2656         gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
2657 }
2658
2659 void MWindow::paste_effects()
2660 {
2661         char *string = 0;
2662         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
2663         if( len ) {
2664                 string = new char[len];
2665                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
2666         }
2667         if( !string || !string[0] ) {
2668                 eprintf(_("Error clipboard buffer empty."));
2669                 return;
2670         }
2671         FileXML file;
2672         file.read_from_string(string);
2673         EDL *group = new EDL();
2674         group->create_objects();
2675         if( !group->load_xml(&file, LOAD_ALL) ) {
2676                 undo_before();
2677                 int ret = edl->insert_effects(group);
2678                 switch( ret ) {
2679                 case INSERT_EFFECTS_RECORD:
2680                         eprintf(_("Selected edit track not armed."));
2681                         break;
2682                 case INSERT_EFFECTS_TYPE:
2683                         eprintf(_("Track type mismatched."));
2684                         break;
2685                 case INSERT_EFFECTS_MULTIPLE:
2686                         eprintf(_("More than one edit selected on a track."));
2687                         break;
2688                 case INSERT_EFFECTS_MISSING:
2689                         eprintf(_("Too few target edits to add group effects."));
2690                         break;
2691                 case INSERT_EFFECTS_EXTRA:
2692                         eprintf(_("Too many target edits to add group effects."));
2693                         break;
2694                 case INSERT_EFFECTS_MASTER:
2695                         eprintf(_("Shared effect added without master."));
2696                         break;
2697                 case 0:
2698                         break;
2699                 }
2700                 save_backup();
2701                 undo_after(_("paste effects"), LOAD_ALL);
2702                 restart_brender();
2703                 cwindow->refresh_frame(CHANGE_EDL);
2704                 update_plugin_guis();
2705                 gui->update(1, NORMAL_DRAW, 1, 0, 0, 0, 0);
2706         }
2707         else
2708                 eprintf(_("Error loading clip from clipboard buffer."));
2709         delete [] string;
2710         group->remove_user();
2711 }
2712
2713 void MWindow::align_timecodes()
2714 {
2715         undo_before();
2716         double offset = edl->tracks->align_timecodes();
2717         set_timecode_offset(offset);
2718         save_backup();
2719         undo_after(_("align timecodes"), LOAD_ALL);
2720         restart_brender();
2721         cwindow->refresh_frame(CHANGE_EDL);
2722         update_plugin_guis();
2723 }
2724
2725
2726 int MWindow::masters_to_mixers()
2727 {
2728         Track *master_track = edl->tracks->first;
2729         while( master_track && !master_track->master )
2730                 master_track = master_track->next;
2731         while( master_track ) { // test for track/mixer conflicts
2732                 int failed = 0;
2733                 Track *mixer_last = master_track;
2734                 Track *track = master_track->next;
2735                 for( ; track && !track->master; track=track->next )
2736                         mixer_last = track;
2737                 Track *next_track = track;
2738                 Mixer *master_mixer = 0;
2739                 for( int i=0, n=edl->mixers.size(); i<n; ++i ) {
2740                         if( master_track->index_in(edl->mixers[i]) >= 0 ) {
2741                                 master_mixer = edl->mixers[i];
2742                                 break;
2743                         }
2744                 }
2745                 if( master_mixer ) { // existing mixer track group
2746                         for( track=master_track; !failed && track; track=track->next ) {
2747                                 if( track->index_in(master_mixer) < 0 ) {
2748                                         eprintf("Mixer: %s missing track: %s",
2749                                                 master_mixer->title, track->title);
2750                                         failed = 1;
2751                                 }
2752                                 if( track == mixer_last ) break;
2753                         }
2754                         for( int i=0, n=master_mixer->mixer_ids.size(); !failed && i<n; ++i ) {
2755                                 int mixer_id = master_mixer->mixer_ids[i], found = 0;
2756                                 for( track=master_track; track; track=track->next ) {
2757                                         if( track->mixer_id == mixer_id ) {
2758                                                 found = 1;
2759                                                 break;
2760                                         }
2761                                         if( track == mixer_last ) break;
2762                                 }
2763                                 if( !found ) {
2764                                         eprintf("Mixer: %s track missing: %s",
2765                                                 master_mixer->title, track->title);
2766                                         failed = 1;
2767                                 }
2768                         }
2769                 }
2770                 else { // create mixer
2771                         for( track=master_track->next; !failed && track; track=track->next ) {
2772                                 for( int i=0, n=edl->mixers.size(); !failed && i<n; ++i ) {
2773                                         Mixer *mixer = edl->mixers[i];
2774                                         if( track->index_in(mixer) >= 0 ) {
2775                                                 eprintf("Track: %s already exists in mixer: %s",
2776                                                         track->title, mixer->title);
2777                                                 failed = 1;
2778                                                 break;
2779                                         }
2780                                 }
2781                                 if( track == mixer_last ) break;
2782                         }
2783                         if( !failed ) { // new mixer
2784                                 ZWindow *zwindow = get_mixer(master_mixer);
2785                                 zwindow->set_title(master_track->title);
2786                                 sprintf(master_track->title, _("Mixer %d"), zwindow->idx);
2787                                 for( track=master_track; track; track=track->next ) {
2788                                         track->play = track->armed = 0;
2789                                         master_mixer->mixer_ids.append(track->get_mixer_id());
2790                                         if( track == mixer_last ) break;
2791                                 }
2792                                 zwindow->start();
2793                         }
2794                 }
2795                 master_track = next_track;
2796         }
2797         return 0;
2798 }
2799
2800 void MWindow::mix_masters()
2801 {
2802         undo_before();
2803         masters_to_mixers();
2804         undo_after(_("mix masters"), LOAD_ALL);
2805
2806         restart_brender();
2807         gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
2808         gui->activate_timeline();
2809         cwindow->refresh_frame(CHANGE_EDL);
2810         save_backup();
2811 }
2812