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