Fix compilation error in Freestyle
[blender.git] / source / blender / editors / space_sequencer / sequencer_edit.c
1 /*
2  * ***** begin GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_sequencer/sequencer_edit.c
27  *  \ingroup spseq
28  */
29
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_ghash.h"
39 #include "BLI_math.h"
40 #include "BLI_timecode.h"
41 #include "BLI_utildefines.h"
42
43 #include "BLT_translation.h"
44
45 #include "DNA_scene_types.h"
46 #include "DNA_sound_types.h"
47
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_sequencer.h"
52 #include "BKE_report.h"
53 #include "BKE_sound.h"
54
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "RNA_define.h"
60 #include "RNA_enum_types.h"
61
62 /* for menu/popup icons etc etc*/
63
64 #include "ED_anim_api.h"
65 #include "ED_numinput.h"
66 #include "ED_screen.h"
67 #include "ED_transform.h"
68 #include "ED_sequencer.h"
69 #include "ED_space_api.h"
70
71 #include "UI_view2d.h"
72 #include "UI_interface.h"
73
74
75 /* own include */
76 #include "sequencer_intern.h"
77
78 /* XXX */
79 /* RNA Enums, used in multiple files */
80 EnumPropertyItem sequencer_prop_effect_types[] = {
81         {SEQ_TYPE_CROSS, "CROSS", 0, "Crossfade", "Crossfade effect strip type"},
82         {SEQ_TYPE_ADD, "ADD", 0, "Add", "Add effect strip type"},
83         {SEQ_TYPE_SUB, "SUBTRACT", 0, "Subtract", "Subtract effect strip type"},
84         {SEQ_TYPE_ALPHAOVER, "ALPHA_OVER", 0, "Alpha Over", "Alpha Over effect strip type"},
85         {SEQ_TYPE_ALPHAUNDER, "ALPHA_UNDER", 0, "Alpha Under", "Alpha Under effect strip type"},
86         {SEQ_TYPE_GAMCROSS, "GAMMA_CROSS", 0, "Gamma Cross", "Gamma Cross effect strip type"},
87         {SEQ_TYPE_MUL, "MULTIPLY", 0, "Multiply", "Multiply effect strip type"},
88         {SEQ_TYPE_OVERDROP, "OVER_DROP", 0, "Alpha Over Drop", "Alpha Over Drop effect strip type"},
89         {SEQ_TYPE_WIPE, "WIPE", 0, "Wipe", "Wipe effect strip type"},
90         {SEQ_TYPE_GLOW, "GLOW", 0, "Glow", "Glow effect strip type"},
91         {SEQ_TYPE_TRANSFORM, "TRANSFORM", 0, "Transform", "Transform effect strip type"},
92         {SEQ_TYPE_COLOR, "COLOR", 0, "Color", "Color effect strip type"},
93         {SEQ_TYPE_SPEED, "SPEED", 0, "Speed", "Color effect strip type"},
94         {SEQ_TYPE_MULTICAM, "MULTICAM", 0, "Multicam Selector", ""},
95         {SEQ_TYPE_ADJUSTMENT, "ADJUSTMENT", 0, "Adjustment Layer", ""},
96         {SEQ_TYPE_GAUSSIAN_BLUR, "GAUSSIAN_BLUR", 0, "Gaussian Blur", ""},
97         {SEQ_TYPE_TEXT, "TEXT", 0, "Text", ""},
98         {0, NULL, 0, NULL, NULL}
99 };
100
101 /* mute operator */
102
103 EnumPropertyItem prop_side_types[] = {
104         {SEQ_SIDE_LEFT, "LEFT", 0, "Left", ""},
105         {SEQ_SIDE_RIGHT, "RIGHT", 0, "Right", ""},
106         {SEQ_SIDE_BOTH, "BOTH", 0, "Both", ""},
107         {0, NULL, 0, NULL, NULL}
108 };
109
110 static EnumPropertyItem prop_side_lr_types[] = {
111         {SEQ_SIDE_LEFT, "LEFT", 0, "Left", ""},
112         {SEQ_SIDE_RIGHT, "RIGHT", 0, "Right", ""},
113         {0, NULL, 0, NULL, NULL}
114 };
115
116 typedef struct TransSeq {
117         int start, machine;
118         int startstill, endstill;
119         int startdisp, enddisp;
120         int startofs, endofs;
121         int anim_startofs, anim_endofs;
122         /* int final_left, final_right; */ /* UNUSED */
123         int len;
124 } TransSeq;
125
126 /* ********************************************************************** */
127
128 /* ***************** proxy job manager ********************** */
129
130 typedef struct ProxyBuildJob {
131         Scene *scene; 
132         struct Main *main;
133         ListBase queue;
134         int stop;
135 } ProxyJob;
136
137 static void proxy_freejob(void *pjv)
138 {
139         ProxyJob *pj = pjv;
140
141         BLI_freelistN(&pj->queue);
142
143         MEM_freeN(pj);
144 }
145
146 /* only this runs inside thread */
147 static void proxy_startjob(void *pjv, short *stop, short *do_update, float *progress)
148 {
149         ProxyJob *pj = pjv;
150         LinkData *link;
151
152         for (link = pj->queue.first; link; link = link->next) {
153                 struct SeqIndexBuildContext *context = link->data;
154
155                 BKE_sequencer_proxy_rebuild(context, stop, do_update, progress);
156                 
157                 if (*stop) {
158                         pj->stop = 1;
159                         fprintf(stderr,  "Canceling proxy rebuild on users request...\n");
160                         break;
161                 }
162         }
163 }
164
165 static void proxy_endjob(void *pjv)
166 {
167         ProxyJob *pj = pjv;
168         Editing *ed = BKE_sequencer_editing_get(pj->scene, false);
169         LinkData *link;
170
171         for (link = pj->queue.first; link; link = link->next) {
172                 BKE_sequencer_proxy_rebuild_finish(link->data, pj->stop);
173         }
174
175         BKE_sequencer_free_imbuf(pj->scene, &ed->seqbase, false);
176
177         WM_main_add_notifier(NC_SCENE | ND_SEQUENCER, pj->scene);
178 }
179
180 static void seq_proxy_build_job(const bContext *C)
181 {
182         wmJob *wm_job;
183         ProxyJob *pj;
184         Scene *scene = CTX_data_scene(C);
185         Editing *ed = BKE_sequencer_editing_get(scene, false);
186         ScrArea *sa = CTX_wm_area(C);
187         Sequence *seq;
188         GSet *file_list;
189         
190         if (ed == NULL) {
191                 return;
192         }
193
194         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Building Proxies",
195                              WM_JOB_PROGRESS, WM_JOB_TYPE_SEQ_BUILD_PROXY);
196
197         pj = WM_jobs_customdata_get(wm_job);
198
199         if (!pj) {
200                 pj = MEM_callocN(sizeof(ProxyJob), "proxy rebuild job");
201         
202                 pj->scene = scene;
203                 pj->main = CTX_data_main(C);
204
205                 WM_jobs_customdata_set(wm_job, pj, proxy_freejob);
206                 WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_SEQUENCER, NC_SCENE | ND_SEQUENCER);
207                 WM_jobs_callbacks(wm_job, proxy_startjob, NULL, NULL, proxy_endjob);
208         }
209
210         file_list = BLI_gset_new(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, "file list");
211         SEQP_BEGIN (ed, seq)
212         {
213                 if ((seq->flag & SELECT)) {
214                         BKE_sequencer_proxy_rebuild_context(pj->main, pj->scene, seq, file_list, &pj->queue);
215                 }
216         }
217         SEQ_END
218
219         BLI_gset_free(file_list, MEM_freeN);
220         
221         if (!WM_jobs_is_running(wm_job)) {
222                 G.is_break = false;
223                 WM_jobs_start(CTX_wm_manager(C), wm_job);
224         }
225
226         ED_area_tag_redraw(sa);
227 }
228
229 /* ********************************************************************** */
230
231 void seq_rectf(Sequence *seq, rctf *rectf)
232 {
233         if (seq->startstill) rectf->xmin = seq->start;
234         else rectf->xmin = seq->startdisp;
235         rectf->ymin = seq->machine + SEQ_STRIP_OFSBOTTOM;
236         if (seq->endstill) rectf->xmax = seq->start + seq->len;
237         else rectf->xmax = seq->enddisp;
238         rectf->ymax = seq->machine + SEQ_STRIP_OFSTOP;
239 }
240
241 void boundbox_seq(Scene *scene, rctf *rect)
242 {
243         Sequence *seq;
244         Editing *ed = BKE_sequencer_editing_get(scene, false);
245         float min[2], max[2];
246
247         
248         if (ed == NULL) return;
249
250         min[0] = 0.0;
251         max[0] = EFRA + 1;
252         min[1] = 0.0;
253         max[1] = 8.0;
254
255         seq = ed->seqbasep->first;
256         while (seq) {
257
258                 if (min[0] > seq->startdisp - 1) min[0] = seq->startdisp - 1;
259                 if (max[0] < seq->enddisp + 1) max[0] = seq->enddisp + 1;
260                 if (max[1] < seq->machine + 2) max[1] = seq->machine + 2;
261
262                 seq = seq->next;
263         }
264
265         rect->xmin = min[0];
266         rect->xmax = max[0];
267         rect->ymin = min[1];
268         rect->ymax = max[1];
269
270 }
271
272 static int mouse_frame_side(View2D *v2d, short mouse_x, int frame)
273 {
274         int mval[2];
275         float mouseloc[2];
276         
277         mval[0] = mouse_x;
278         mval[1] = 0;
279         
280         /* choose the side based on which side of the playhead the mouse is on */
281         UI_view2d_region_to_view(v2d, mval[0], mval[1], &mouseloc[0], &mouseloc[1]);
282         
283         return mouseloc[0] > frame ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT;
284 }
285
286
287 Sequence *find_neighboring_sequence(Scene *scene, Sequence *test, int lr, int sel) 
288 {
289         /* sel - 0==unselected, 1==selected, -1==done care*/
290         Sequence *seq;
291         Editing *ed = BKE_sequencer_editing_get(scene, false);
292
293         if (ed == NULL) return NULL;
294
295         if (sel > 0) sel = SELECT;
296         
297         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
298                 if ((seq != test) &&
299                     (test->machine == seq->machine) &&
300                     ((sel == -1) || (sel && (seq->flag & SELECT)) || (sel == 0 && (seq->flag & SELECT) == 0)))
301                 {
302                         switch (lr) {
303                                 case SEQ_SIDE_LEFT:
304                                         if (test->startdisp == (seq->enddisp)) {
305                                                 return seq;
306                                         }
307                                         break;
308                                 case SEQ_SIDE_RIGHT:
309                                         if (test->enddisp == (seq->startdisp)) {
310                                                 return seq;
311                                         }
312                                         break;
313                         }
314                 }
315         }
316         return NULL;
317 }
318
319 static Sequence *find_next_prev_sequence(Scene *scene, Sequence *test, int lr, int sel) 
320 {
321         /* sel - 0==unselected, 1==selected, -1==done care*/
322         Sequence *seq, *best_seq = NULL;
323         Editing *ed = BKE_sequencer_editing_get(scene, false);
324         
325         int dist, best_dist;
326         best_dist = MAXFRAME * 2;
327
328         
329         if (ed == NULL) return NULL;
330
331         seq = ed->seqbasep->first;
332         while (seq) {
333                 if ((seq != test) &&
334                     (test->machine == seq->machine) &&
335                     (test->depth == seq->depth) &&
336                     ((sel == -1) || (sel == (seq->flag & SELECT))))
337                 {
338                         dist = MAXFRAME * 2;
339                         
340                         switch (lr) {
341                                 case SEQ_SIDE_LEFT:
342                                         if (seq->enddisp <= test->startdisp) {
343                                                 dist = test->enddisp - seq->startdisp;
344                                         }
345                                         break;
346                                 case SEQ_SIDE_RIGHT:
347                                         if (seq->startdisp >= test->enddisp) {
348                                                 dist = seq->startdisp - test->enddisp;
349                                         }
350                                         break;
351                         }
352                         
353                         if (dist == 0) {
354                                 best_seq = seq;
355                                 break;
356                         }
357                         else if (dist < best_dist) {
358                                 best_dist = dist;
359                                 best_seq = seq;
360                         }
361                 }
362                 seq = seq->next;
363         }
364         return best_seq; /* can be null */
365 }
366
367
368 Sequence *find_nearest_seq(Scene *scene, View2D *v2d, int *hand, const int mval[2])
369 {
370         Sequence *seq;
371         Editing *ed = BKE_sequencer_editing_get(scene, false);
372         float x, y;
373         float pixelx;
374         float handsize;
375         float displen;
376         *hand = SEQ_SIDE_NONE;
377
378         
379         if (ed == NULL) return NULL;
380         
381         pixelx = BLI_rctf_size_x(&v2d->cur) / BLI_rcti_size_x(&v2d->mask);
382
383         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
384         
385         seq = ed->seqbasep->first;
386         
387         while (seq) {
388                 if (seq->machine == (int)y) {
389                         /* check for both normal strips, and strips that have been flipped horizontally */
390                         if (((seq->startdisp < seq->enddisp) && (seq->startdisp <= x && seq->enddisp >= x)) ||
391                             ((seq->startdisp > seq->enddisp) && (seq->startdisp >= x && seq->enddisp <= x)) )
392                         {
393                                 if (BKE_sequence_tx_test(seq)) {
394                                         
395                                         /* clamp handles to defined size in pixel space */
396                                         
397                                         handsize = seq->handsize;
398                                         displen = (float)abs(seq->startdisp - seq->enddisp);
399                                         
400                                         if (displen / pixelx > 16) { /* don't even try to grab the handles of small strips */
401                                                 /* Set the max value to handle to 1/3 of the total len when its less than 28.
402                                                  * This is important because otherwise selecting handles happens even when you click in the middle */
403                                                 
404                                                 if ((displen / 3) < 30 * pixelx) {
405                                                         handsize = displen / 3;
406                                                 }
407                                                 else {
408                                                         CLAMP(handsize, 7 * pixelx, 30 * pixelx);
409                                                 }
410                                                 
411                                                 if (handsize + seq->startdisp >= x)
412                                                         *hand = SEQ_SIDE_LEFT;
413                                                 else if (-handsize + seq->enddisp <= x)
414                                                         *hand = SEQ_SIDE_RIGHT;
415                                         }
416                                 }
417                                 return seq;
418                         }
419                 }
420                 seq = seq->next;
421         }
422         return NULL;
423 }
424
425
426 static bool seq_is_parent(Sequence *par, Sequence *seq)
427 {
428         return ((par->seq1 == seq) || (par->seq2 == seq) || (par->seq3 == seq));
429 }
430
431 static bool seq_is_predecessor(Sequence *pred, Sequence *seq)
432 {
433         if (!pred) return 0;
434         if (pred == seq) return 0;
435         else if (seq_is_parent(pred, seq)) return 1;
436         else if (pred->seq1 && seq_is_predecessor(pred->seq1, seq)) return 1;
437         else if (pred->seq2 && seq_is_predecessor(pred->seq2, seq)) return 1;
438         else if (pred->seq3 && seq_is_predecessor(pred->seq3, seq)) return 1;
439
440         return 0;
441 }
442
443 void ED_sequencer_deselect_all(Scene *scene)
444 {
445         Sequence *seq;
446         Editing *ed = BKE_sequencer_editing_get(scene, false);
447
448         
449         if (ed == NULL) return;
450
451         SEQP_BEGIN (ed, seq)
452         {
453                 seq->flag &= ~SEQ_ALLSEL;
454         }
455         SEQ_END
456                 
457 }
458
459 void recurs_sel_seq(Sequence *seqm)
460 {
461         Sequence *seq;
462
463         seq = seqm->seqbase.first;
464         while (seq) {
465
466                 if (seqm->flag & (SEQ_LEFTSEL + SEQ_RIGHTSEL)) seq->flag &= ~SEQ_ALLSEL;
467                 else if (seqm->flag & SELECT) seq->flag |= SELECT;
468                 else seq->flag &= ~SEQ_ALLSEL;
469
470                 if (seq->seqbase.first) recurs_sel_seq(seq);
471
472                 seq = seq->next;
473         }
474 }
475
476 int ED_space_sequencer_maskedit_mask_poll(bContext *C)
477 {
478         /* in this case both funcs are the same, for clip editor not */
479         return ED_space_sequencer_maskedit_poll(C);
480 }
481
482 bool ED_space_sequencer_check_show_maskedit(SpaceSeq *sseq, Scene *scene)
483 {
484         if (sseq && sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
485                 return (BKE_sequencer_mask_get(scene) != NULL);
486         }
487
488         return false;
489 }
490
491 int ED_space_sequencer_maskedit_poll(bContext *C)
492 {
493         SpaceSeq *sseq = CTX_wm_space_seq(C);
494
495         if (sseq) {
496                 Scene *scene = CTX_data_scene(C);
497                 return ED_space_sequencer_check_show_maskedit(sseq, scene);
498         }
499
500         return false;
501 }
502
503 /* are we displaying the seq output (not channels or histogram)*/
504 bool ED_space_sequencer_check_show_imbuf(SpaceSeq *sseq)
505 {
506         return (ELEM(sseq->view, SEQ_VIEW_PREVIEW, SEQ_VIEW_SEQUENCE_PREVIEW) &&
507                 ELEM(sseq->mainb, SEQ_DRAW_SEQUENCE, SEQ_DRAW_IMG_IMBUF));
508 }
509
510 bool ED_space_sequencer_check_show_strip(SpaceSeq *sseq)
511 {
512         return (ELEM(sseq->view, SEQ_VIEW_SEQUENCE, SEQ_VIEW_SEQUENCE_PREVIEW) &&
513                 ELEM(sseq->mainb, SEQ_DRAW_SEQUENCE, SEQ_DRAW_IMG_IMBUF));
514 }
515
516
517 int seq_effect_find_selected(Scene *scene, Sequence *activeseq, int type, Sequence **selseq1, Sequence **selseq2, Sequence **selseq3, const char **error_str)
518 {
519         Editing *ed = BKE_sequencer_editing_get(scene, false);
520         Sequence *seq1 = NULL, *seq2 = NULL, *seq3 = NULL, *seq;
521         
522         *error_str = NULL;
523
524         if (!activeseq)
525                 seq2 = BKE_sequencer_active_get(scene);
526
527         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
528                 if (seq->flag & SELECT) {
529                         if (seq->type == SEQ_TYPE_SOUND_RAM && BKE_sequence_effect_get_num_inputs(type) != 0) {
530                                 *error_str = N_("Cannot apply effects to audio sequence strips");
531                                 return 0;
532                         }
533                         if ((seq != activeseq) && (seq != seq2)) {
534                                 if      (seq2 == NULL) seq2 = seq;
535                                 else if (seq1 == NULL) seq1 = seq;
536                                 else if (seq3 == NULL) seq3 = seq;
537                                 else {
538                                         *error_str = N_("Cannot apply effect to more than 3 sequence strips");
539                                         return 0;
540                                 }
541                         }
542                 }
543         }
544
545         /* make sequence selection a little bit more intuitive
546          * for 3 strips: the last-strip should be sequence3 */
547         if (seq3 != NULL && seq2 != NULL) {
548                 Sequence *tmp = seq2;
549                 seq2 = seq3;
550                 seq3 = tmp;
551         }
552         
553
554         switch (BKE_sequence_effect_get_num_inputs(type)) {
555                 case 0:
556                         *selseq1 = *selseq2 = *selseq3 = NULL;
557                         return 1; /* succsess */
558                 case 1:
559                         if (seq2 == NULL) {
560                                 *error_str = N_("At least one selected sequence strip is needed");
561                                 return 0;
562                         }
563                         if (seq1 == NULL) seq1 = seq2;
564                         if (seq3 == NULL) seq3 = seq2;
565                         ATTR_FALLTHROUGH;
566                 case 2:
567                         if (seq1 == NULL || seq2 == NULL) {
568                                 *error_str = N_("2 selected sequence strips are needed");
569                                 return 0;
570                         }
571                         if (seq3 == NULL) seq3 = seq2;
572                         break;
573         }
574         
575         if (seq1 == NULL && seq2 == NULL && seq3 == NULL) {
576                 *error_str = N_("TODO: in what cases does this happen?");
577                 return 0;
578         }
579         
580         *selseq1 = seq1;
581         *selseq2 = seq2;
582         *selseq3 = seq3;
583
584         return 1;
585 }
586
587 static Sequence *del_seq_find_replace_recurs(Scene *scene, Sequence *seq)
588 {
589         Sequence *seq1, *seq2, *seq3;
590
591         /* try to find a replacement input sequence, and flag for later deletion if
592          * no replacement can be found */
593
594         if (!seq)
595                 return NULL;
596         else if (!(seq->type & SEQ_TYPE_EFFECT))
597                 return ((seq->flag & SELECT) ? NULL : seq);
598         else if (!(seq->flag & SELECT)) {
599                 /* try to find replacement for effect inputs */
600                 seq1 = del_seq_find_replace_recurs(scene, seq->seq1);
601                 seq2 = del_seq_find_replace_recurs(scene, seq->seq2);
602                 seq3 = del_seq_find_replace_recurs(scene, seq->seq3);
603
604                 if (seq1 == seq->seq1 && seq2 == seq->seq2 && seq3 == seq->seq3) {
605                         /* pass */
606                 }
607                 else if (seq1 || seq2 || seq3) {
608                         seq->seq1 = (seq1) ? seq1 : (seq2) ? seq2 : seq3;
609                         seq->seq2 = (seq2) ? seq2 : (seq1) ? seq1 : seq3;
610                         seq->seq3 = (seq3) ? seq3 : (seq1) ? seq1 : seq2;
611
612                         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 1, 1);
613                 }
614                 else
615                         seq->flag |= SELECT;  /* mark for delete */
616         }
617
618         if (seq->flag & SELECT) {
619                 if ((seq1 = del_seq_find_replace_recurs(scene, seq->seq1))) return seq1;
620                 if ((seq2 = del_seq_find_replace_recurs(scene, seq->seq2))) return seq2;
621                 if ((seq3 = del_seq_find_replace_recurs(scene, seq->seq3))) return seq3;
622                 else return NULL;
623         }
624         else
625                 return seq;
626 }
627
628 static void del_seq_clear_modifiers_recurs(Scene *scene, Sequence *deleting_sequence)
629 {
630         Editing *ed = BKE_sequencer_editing_get(scene, false);
631         Sequence *current_sequence;
632
633         SEQP_BEGIN(ed, current_sequence)
634         {
635                 if (!(current_sequence->flag & SELECT) && current_sequence != deleting_sequence) {
636                         SequenceModifierData *smd;
637
638                         for (smd = current_sequence->modifiers.first; smd; smd = smd->next) {
639                                 if (smd->mask_sequence == deleting_sequence) {
640                                         smd->mask_sequence = NULL;
641                                 }
642                         }
643                 }
644         }
645         SEQ_END
646 }
647
648 static void recurs_del_seq_flag(Scene *scene, ListBase *lb, short flag, short deleteall)
649 {
650         Sequence *seq, *seqn;
651         Sequence *last_seq = BKE_sequencer_active_get(scene);
652
653         seq = lb->first;
654         while (seq) {
655                 seqn = seq->next;
656                 if ((seq->flag & flag) || deleteall) {
657                         BLI_remlink(lb, seq);
658                         if (seq == last_seq) BKE_sequencer_active_set(scene, NULL);
659                         if (seq->type == SEQ_TYPE_META) recurs_del_seq_flag(scene, &seq->seqbase, flag, 1);
660                         BKE_sequence_free(scene, seq);
661                 }
662                 seq = seqn;
663         }
664 }
665
666
667 static Sequence *cut_seq_hard(Scene *scene, Sequence *seq, int cutframe)
668 {
669         TransSeq ts;
670         Sequence *seqn = NULL;
671         bool skip_dup = false;
672
673         /* backup values */
674         ts.start = seq->start;
675         ts.machine = seq->machine;
676         ts.startstill = seq->startstill;
677         ts.endstill = seq->endstill;
678         ts.startdisp = seq->startdisp;
679         ts.enddisp = seq->enddisp;
680         ts.startofs = seq->startofs;
681         ts.endofs = seq->endofs;
682         ts.anim_startofs = seq->anim_startofs;
683         ts.anim_endofs = seq->anim_endofs;
684         ts.len = seq->len;
685         
686         /* First Strip! */
687         /* strips with extended stillfames before */
688         
689         /* Precaution, needed because the length saved on-disk may not match the length saved in the blend file,
690          * or our code may have minor differences reading file length between versions.
691          * This causes hard-cut to fail, see: T47862 */
692         if (seq->type != SEQ_TYPE_META) {
693                 BKE_sequence_reload_new_file(scene, seq, true);
694                 BKE_sequence_calc(scene, seq);
695         }
696
697         if ((seq->startstill) && (cutframe < seq->start)) {
698                 /* don't do funny things with METAs ... */
699                 if (seq->type == SEQ_TYPE_META) {
700                         skip_dup = true;
701                         seq->startstill = seq->start - cutframe;
702                 }
703                 else {
704                         seq->start = cutframe - 1;
705                         seq->startstill = cutframe - seq->startdisp - 1;
706                         seq->anim_endofs += seq->len - 1;
707                         seq->endstill = 0;
708                 }
709         }
710         /* normal strip */
711         else if ((cutframe >= seq->start) && (cutframe <= (seq->start + seq->len))) {
712                 seq->endofs = 0;
713                 seq->endstill = 0;
714                 seq->anim_endofs += (seq->start + seq->len) - cutframe;
715         }
716         /* strips with extended stillframes after */
717         else if (((seq->start + seq->len) < cutframe) && (seq->endstill)) {
718                 seq->endstill -= seq->enddisp - cutframe;
719                 /* don't do funny things with METAs ... */
720                 if (seq->type == SEQ_TYPE_META) {
721                         skip_dup = true;
722                 }
723         }
724         
725         BKE_sequence_reload_new_file(scene, seq, false);
726         BKE_sequence_calc(scene, seq);
727
728         if (!skip_dup) {
729                 /* Duplicate AFTER the first change */
730                 seqn = BKE_sequence_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM);
731         }
732         
733         if (seqn) {
734                 seqn->flag |= SELECT;
735                         
736                 /* Second Strip! */
737                 /* strips with extended stillframes before */
738                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
739                         seqn->start = ts.start;
740                         seqn->startstill = ts.start - cutframe;
741                         seqn->anim_endofs = ts.anim_endofs;
742                         seqn->endstill = ts.endstill;
743                 }
744                 
745                 /* normal strip */
746                 else if ((cutframe >= seqn->start) && (cutframe <= (seqn->start + seqn->len))) {
747                         seqn->start = cutframe;
748                         seqn->startstill = 0;
749                         seqn->startofs = 0;
750                         seqn->endofs = ts.endofs;
751                         seqn->anim_startofs += cutframe - ts.start;
752                         seqn->anim_endofs = ts.anim_endofs;
753                         seqn->endstill = ts.endstill;
754                 }
755                 
756                 /* strips with extended stillframes after */
757                 else if (((seqn->start + seqn->len) < cutframe) && (seqn->endstill)) {
758                         seqn->start = cutframe;
759                         seqn->startofs = 0;
760                         seqn->anim_startofs += ts.len - 1;
761                         seqn->endstill = ts.enddisp - cutframe - 1;
762                         seqn->startstill = 0;
763                 }
764                 
765                 BKE_sequence_reload_new_file(scene, seqn, false);
766                 BKE_sequence_calc(scene, seqn);
767         }
768         return seqn;
769 }
770
771 static Sequence *cut_seq_soft(Scene *scene, Sequence *seq, int cutframe)
772 {
773         TransSeq ts;
774         Sequence *seqn = NULL;
775         bool skip_dup = false;
776
777         /* backup values */
778         ts.start = seq->start;
779         ts.machine = seq->machine;
780         ts.startstill = seq->startstill;
781         ts.endstill = seq->endstill;
782         ts.startdisp = seq->startdisp;
783         ts.enddisp = seq->enddisp;
784         ts.startofs = seq->startofs;
785         ts.endofs = seq->endofs;
786         ts.anim_startofs = seq->anim_startofs;
787         ts.anim_endofs = seq->anim_endofs;
788         ts.len = seq->len;
789         
790         /* First Strip! */
791         /* strips with extended stillfames before */
792         
793         if ((seq->startstill) && (cutframe < seq->start)) {
794                 /* don't do funny things with METAs ... */
795                 if (seq->type == SEQ_TYPE_META) {
796                         skip_dup = true;
797                         seq->startstill = seq->start - cutframe;
798                 }
799                 else {
800                         seq->start = cutframe - 1;
801                         seq->startstill = cutframe - seq->startdisp - 1;
802                         seq->endofs = seq->len - 1;
803                         seq->endstill = 0;
804                 }
805         }
806         /* normal strip */
807         else if ((cutframe >= seq->start) && (cutframe <= (seq->start + seq->len))) {
808                 seq->endofs = (seq->start + seq->len) - cutframe;
809         }
810         /* strips with extended stillframes after */
811         else if (((seq->start + seq->len) < cutframe) && (seq->endstill)) {
812                 seq->endstill -= seq->enddisp - cutframe;
813                 /* don't do funny things with METAs ... */
814                 if (seq->type == SEQ_TYPE_META) {
815                         skip_dup = true;
816                 }
817         }
818         
819         BKE_sequence_calc(scene, seq);
820
821         if (!skip_dup) {
822                 /* Duplicate AFTER the first change */
823                 seqn = BKE_sequence_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM);
824         }
825         
826         if (seqn) {
827                 seqn->flag |= SELECT;
828                         
829                 /* Second Strip! */
830                 /* strips with extended stillframes before */
831                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
832                         seqn->start = ts.start;
833                         seqn->startstill = ts.start - cutframe;
834                         seqn->endofs = ts.endofs;
835                         seqn->endstill = ts.endstill;
836                 }
837                 
838                 /* normal strip */
839                 else if ((cutframe >= seqn->start) && (cutframe <= (seqn->start + seqn->len))) {
840                         seqn->startstill = 0;
841                         seqn->startofs = cutframe - ts.start;
842                         seqn->endofs = ts.endofs;
843                         seqn->endstill = ts.endstill;
844                 }
845                 
846                 /* strips with extended stillframes after */
847                 else if (((seqn->start + seqn->len) < cutframe) && (seqn->endstill)) {
848                         seqn->start = cutframe - ts.len + 1;
849                         seqn->startofs = ts.len - 1;
850                         seqn->endstill = ts.enddisp - cutframe - 1;
851                         seqn->startstill = 0;
852                 }
853                 
854                 BKE_sequence_calc(scene, seqn);
855         }
856         return seqn;
857 }
858
859
860 /* like duplicate, but only duplicate and cut overlapping strips,
861  * strips to the left of the cutframe are ignored and strips to the right 
862  * are moved to the end of slist
863  * we have to work on the same slist (not using a separate list), since
864  * otherwise dupli_seq can't check for duplicate names properly and
865  * may generate strips with the same name (which will mess up animdata)
866  */
867
868 static bool cut_seq_list(Scene *scene, ListBase *slist, int cutframe,
869                          Sequence * (*cut_seq)(Scene *, Sequence *, int))
870 {
871         Sequence *seq, *seq_next_iter;
872         Sequence *seq_first_new = NULL;
873         
874         seq = slist->first;
875
876         while (seq && seq != seq_first_new) {
877                 seq_next_iter = seq->next; /* we need this because we may remove seq */
878                 seq->tmp = NULL;
879                 if (seq->flag & SELECT) {
880                         if (cutframe > seq->startdisp && 
881                             cutframe < seq->enddisp)
882                         {
883                                 Sequence *seqn = cut_seq(scene, seq, cutframe);
884                                 if (seqn) {
885                                         BLI_addtail(slist, seqn);
886                                         if (seq_first_new == NULL) {
887                                                 seq_first_new = seqn;
888                                         }
889                                 }
890                         }
891                         else if (seq->enddisp <= cutframe) {
892                                 /* do nothing */
893                         }
894                         else if (seq->startdisp >= cutframe) {
895                                 /* move to tail */
896                                 BLI_remlink(slist, seq);
897                                 BLI_addtail(slist, seq);
898
899                                 if (seq_first_new == NULL) {
900                                         seq_first_new = seq;
901                                 }
902                         }
903                 }
904                 seq = seq_next_iter;
905         }
906
907         return (seq_first_new != NULL);
908 }
909
910 static bool sequence_offset_after_frame(Scene *scene, const int delta, const int cfra)
911 {
912         Sequence *seq;
913         Editing *ed = BKE_sequencer_editing_get(scene, false);
914         bool done = false;
915         TimeMarker *marker;
916
917         /* all strips >= cfra are shifted */
918         
919         if (ed == NULL) return 0;
920
921         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
922                 if (seq->startdisp >= cfra) {
923                         BKE_sequence_translate(scene, seq, delta);
924                         BKE_sequence_calc(scene, seq);
925                         done = true;
926                 }
927         }
928
929         if (!scene->toolsettings->lock_markers) {
930                 for (marker = scene->markers.first; marker; marker = marker->next) {
931                         if (marker->frame >= cfra) {
932                                 marker->frame += delta;
933                         }
934                 }
935         }
936
937         return done;
938 }
939
940 #if 0
941 static void set_filter_seq(Scene *scene)
942 {
943         Sequence *seq;
944         Editing *ed = BKE_sequencer_editing_get(scene, false);
945
946         
947         if (ed == NULL) return;
948
949         if (okee("Set Deinterlace") == 0) return;
950
951         SEQP_BEGIN (ed, seq)
952         {
953                 if (seq->flag & SELECT) {
954                         if (seq->type == SEQ_TYPE_MOVIE) {
955                                 seq->flag |= SEQ_FILTERY;
956                                 BKE_sequence_reload_new_file(scene, seq, false);
957                                 BKE_sequence_calc(scene, seq);
958                         }
959
960                 }
961         }
962         SEQ_END
963 }
964 #endif
965
966 static void UNUSED_FUNCTION(seq_remap_paths) (Scene *scene)
967 {
968         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene);
969         Editing *ed = BKE_sequencer_editing_get(scene, false);
970         char from[FILE_MAX], to[FILE_MAX], stripped[FILE_MAX];
971         
972         
973         if (last_seq == NULL)
974                 return;
975         
976         BLI_strncpy(from, last_seq->strip->dir, sizeof(from));
977 // XXX  if (0 == sbutton(from, 0, sizeof(from) - 1, "From: "))
978 //              return;
979         
980         BLI_strncpy(to, from, sizeof(to));
981 // XXX  if (0 == sbutton(to, 0, sizeof(to) - 1, "To: "))
982 //              return;
983         
984         if (STREQ(to, from))
985                 return;
986         
987         SEQP_BEGIN (ed, seq)
988         {
989                 if (seq->flag & SELECT) {
990                         if (STREQLEN(seq->strip->dir, from, strlen(from))) {
991                                 printf("found %s\n", seq->strip->dir);
992                                 
993                                 /* strip off the beginning */
994                                 stripped[0] = 0;
995                                 BLI_strncpy(stripped, seq->strip->dir + strlen(from), FILE_MAX);
996                                 
997                                 /* new path */
998                                 BLI_snprintf(seq->strip->dir, sizeof(seq->strip->dir), "%s%s", to, stripped);
999                                 printf("new %s\n", seq->strip->dir);
1000                         }
1001                 }
1002         }
1003         SEQ_END
1004                 
1005 }
1006
1007
1008 static int sequencer_gap_remove_exec(bContext *C, wmOperator *op)
1009 {
1010         Scene *scene = CTX_data_scene(C);
1011         rctf rectf;
1012         int cfra, efra, sfra;
1013         bool first = false, done;
1014         bool do_all = RNA_boolean_get(op->ptr, "all");
1015
1016         /* get first and last frame */
1017         boundbox_seq(scene, &rectf);
1018         sfra = (int)rectf.xmin;
1019         efra = (int)rectf.xmax;
1020         
1021         /* first check if the current frame has a gap already */
1022         for (cfra = CFRA; cfra >= sfra; cfra--) {
1023                 if (BKE_sequencer_evaluate_frame(scene, cfra)) {
1024                         first = true;
1025                         break;
1026                 }
1027         }
1028
1029         for ( ; cfra < efra; cfra++) {
1030                 /* first == 0 means there's still no strip to remove a gap for */
1031                 if (first == false) {
1032                         if (BKE_sequencer_evaluate_frame(scene, cfra) ) first = true;
1033                 }
1034                 else if (BKE_sequencer_evaluate_frame(scene, cfra) == 0) {
1035                         done = true;
1036                         while (BKE_sequencer_evaluate_frame(scene, cfra) == 0) {
1037                                 done = sequence_offset_after_frame(scene, -1, cfra);
1038                                 if (done == false) break;
1039                         }
1040                         if (done == false || do_all == false) break;
1041                 }
1042         }
1043
1044         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1045         
1046         return OPERATOR_FINISHED;
1047
1048 }
1049
1050
1051 void SEQUENCER_OT_gap_remove(struct wmOperatorType *ot)
1052 {
1053         /* identifiers */
1054         ot->name = "Remove Gaps";
1055         ot->idname = "SEQUENCER_OT_gap_remove";
1056         ot->description = "Remove gap at current frame to first strip at the right, independent of selection or locked state of strips";
1057         
1058         /* api callbacks */
1059 //      ot->invoke = sequencer_snap_invoke;
1060         ot->exec = sequencer_gap_remove_exec;
1061         ot->poll = sequencer_edit_poll;
1062         
1063         /* flags */
1064         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1065         
1066         RNA_def_boolean(ot->srna, "all", 0, "All Gaps", "Do all gaps to right of current frame");
1067 }
1068
1069 static int sequencer_gap_insert_exec(bContext *C, wmOperator *op)
1070 {
1071         Scene *scene = CTX_data_scene(C);
1072         int frames = RNA_int_get(op->ptr, "frames");
1073         
1074         sequence_offset_after_frame(scene, frames, CFRA);
1075         
1076         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1077         
1078         return OPERATOR_FINISHED;
1079         
1080 }
1081
1082 void SEQUENCER_OT_gap_insert(struct wmOperatorType *ot)
1083 {
1084         /* identifiers */
1085         ot->name = "Insert Gaps";
1086         ot->idname = "SEQUENCER_OT_gap_insert";
1087         ot->description = "Insert gap at current frame to first strips at the right, independent of selection or locked state of strips";
1088         
1089         /* api callbacks */
1090         //      ot->invoke = sequencer_snap_invoke;
1091         ot->exec = sequencer_gap_insert_exec;
1092         ot->poll = sequencer_edit_poll;
1093         
1094         /* flags */
1095         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1096         
1097         RNA_def_int(ot->srna, "frames", 10, 0, INT_MAX, "Frames", "Frames to insert after current strip", 0, 1000);
1098 }
1099
1100
1101 #if 0
1102 static int seq_get_snaplimit(View2D *v2d)
1103 {
1104         /* fake mouse coords to get the snap value
1105          * a bit lazy but its only done once pre transform */
1106         float xmouse, ymouse, x;
1107         int mval[2] = {24, 0}; /* 24 screen px snap */
1108         
1109         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
1110         x = xmouse;
1111         mval[0] = 0;
1112         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
1113         return (int)(x - xmouse);
1114 }
1115 #endif
1116
1117 /* Operator functions */
1118 int sequencer_edit_poll(bContext *C)
1119 {
1120         return (BKE_sequencer_editing_get(CTX_data_scene(C), false) != NULL);
1121 }
1122
1123 #if 0 /* UNUSED */
1124 int sequencer_strip_poll(bContext *C)
1125 {
1126         Editing *ed;
1127         return (((ed = BKE_sequencer_editing_get(CTX_data_scene(C), false)) != NULL) && (ed->act_seq != NULL));
1128 }
1129 #endif
1130
1131 int sequencer_strip_has_path_poll(bContext *C)
1132 {
1133         Editing *ed;
1134         Sequence *seq;
1135         return (((ed = BKE_sequencer_editing_get(CTX_data_scene(C), false)) != NULL) && ((seq = ed->act_seq) != NULL) && (SEQ_HAS_PATH(seq)));
1136 }
1137
1138 int sequencer_view_preview_poll(bContext *C)
1139 {
1140         SpaceSeq *sseq = CTX_wm_space_seq(C);
1141         Editing *ed = BKE_sequencer_editing_get(CTX_data_scene(C), false);
1142         if (ed && sseq && (sseq->mainb == SEQ_DRAW_IMG_IMBUF))
1143                 return 1;
1144
1145         return 0;
1146 }
1147
1148 int sequencer_view_strips_poll(bContext *C)
1149 {
1150         SpaceSeq *sseq = CTX_wm_space_seq(C);
1151         if (sseq && ED_space_sequencer_check_show_strip(sseq))
1152                 return 1;
1153
1154         return 0;
1155 }
1156
1157 /* snap operator*/
1158 static int sequencer_snap_exec(bContext *C, wmOperator *op)
1159 {
1160         Scene *scene = CTX_data_scene(C);
1161         
1162         Editing *ed = BKE_sequencer_editing_get(scene, false);
1163         Sequence *seq;
1164         int snap_frame;
1165
1166         snap_frame = RNA_int_get(op->ptr, "frame");
1167
1168         /* also check metas */
1169         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1170                 if (seq->flag & SELECT && !(seq->depth == 0 && seq->flag & SEQ_LOCK) &&
1171                     BKE_sequence_tx_test(seq))
1172                 {
1173                         if ((seq->flag & (SEQ_LEFTSEL + SEQ_RIGHTSEL)) == 0) {
1174                                 /* simple but no anim update */
1175                                 /* seq->start = snap_frame-seq->startofs+seq->startstill; */
1176
1177                                 BKE_sequence_translate(scene, seq, (snap_frame - seq->startofs + seq->startstill) - seq->start);
1178                         }
1179                         else {
1180                                 if (seq->flag & SEQ_LEFTSEL) {
1181                                         BKE_sequence_tx_set_final_left(seq, snap_frame);
1182                                 }
1183                                 else { /* SEQ_RIGHTSEL */
1184                                         BKE_sequence_tx_set_final_right(seq, snap_frame);
1185                                 }
1186                                 BKE_sequence_tx_handle_xlimits(seq, seq->flag & SEQ_LEFTSEL, seq->flag & SEQ_RIGHTSEL);
1187                                 BKE_sequence_single_fix(seq);
1188                         }
1189                         BKE_sequence_calc(scene, seq);
1190                 }
1191         }
1192
1193         /* test for effects and overlap
1194          * don't use SEQP_BEGIN since that would be recursive */
1195         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1196                 if (seq->flag & SELECT && !(seq->depth == 0 && seq->flag & SEQ_LOCK)) {
1197                         seq->flag &= ~SEQ_OVERLAP;
1198                         if (BKE_sequence_test_overlap(ed->seqbasep, seq) ) {
1199                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1200                         }
1201                 }
1202                 else if (seq->type & SEQ_TYPE_EFFECT) {
1203                         if (seq->seq1 && (seq->seq1->flag & SELECT)) 
1204                                 BKE_sequence_calc(scene, seq);
1205                         else if (seq->seq2 && (seq->seq2->flag & SELECT)) 
1206                                 BKE_sequence_calc(scene, seq);
1207                         else if (seq->seq3 && (seq->seq3->flag & SELECT)) 
1208                                 BKE_sequence_calc(scene, seq);
1209                 }
1210         }
1211
1212         /* as last: */
1213         BKE_sequencer_sort(scene);
1214         
1215         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1216         
1217         return OPERATOR_FINISHED;
1218 }
1219
1220 static int sequencer_snap_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1221 {
1222         Scene *scene = CTX_data_scene(C);
1223         
1224         int snap_frame;
1225         
1226         snap_frame = CFRA;
1227         
1228         RNA_int_set(op->ptr, "frame", snap_frame);
1229         return sequencer_snap_exec(C, op);
1230 }
1231
1232 void SEQUENCER_OT_snap(struct wmOperatorType *ot)
1233 {
1234         /* identifiers */
1235         ot->name = "Snap Strips";
1236         ot->idname = "SEQUENCER_OT_snap";
1237         ot->description = "Frame where selected strips will be snapped";
1238         
1239         /* api callbacks */
1240         ot->invoke = sequencer_snap_invoke;
1241         ot->exec = sequencer_snap_exec;
1242         ot->poll = sequencer_edit_poll;
1243         
1244         /* flags */
1245         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1246         
1247         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be snapped", INT_MIN, INT_MAX);
1248 }
1249
1250 typedef struct SlipData {
1251         int init_mouse[2];
1252         float init_mouseloc[2];
1253         TransSeq *ts;
1254         Sequence **seq_array;
1255         bool *trim;
1256         int num_seq;
1257         bool slow;
1258         int slow_offset; /* offset at the point where offset was turned on */
1259         NumInput num_input;
1260 } SlipData;
1261
1262 static void transseq_backup(TransSeq *ts, Sequence *seq)
1263 {
1264         ts->start = seq->start;
1265         ts->machine = seq->machine;
1266         ts->startstill = seq->startstill;
1267         ts->endstill = seq->endstill;
1268         ts->startdisp = seq->startdisp;
1269         ts->enddisp = seq->enddisp;
1270         ts->startofs = seq->startofs;
1271         ts->endofs = seq->endofs;
1272         ts->anim_startofs = seq->anim_startofs;
1273         ts->anim_endofs = seq->anim_endofs;
1274         ts->len = seq->len;
1275 }
1276
1277
1278 static void transseq_restore(TransSeq *ts, Sequence *seq)
1279 {
1280         seq->start = ts->start;
1281         seq->machine = ts->machine;
1282         seq->startstill = ts->startstill;
1283         seq->endstill = ts->endstill;
1284         seq->startdisp = ts->startdisp;
1285         seq->enddisp = ts->enddisp;
1286         seq->startofs = ts->startofs;
1287         seq->endofs = ts->endofs;
1288         seq->anim_startofs = ts->anim_startofs;
1289         seq->anim_endofs = ts->anim_endofs;
1290         seq->len = ts->len;
1291 }
1292
1293 static int slip_add_sequences_rec(ListBase *seqbasep, Sequence **seq_array, bool *trim, int offset, bool do_trim)
1294 {
1295         Sequence *seq;
1296         int num_items = 0;
1297
1298         for (seq = seqbasep->first; seq; seq = seq->next) {
1299                 if (!do_trim || (!(seq->type & SEQ_TYPE_EFFECT) && (seq->flag & SELECT))) {
1300                         seq_array[offset + num_items] = seq;
1301                         trim[offset + num_items] = do_trim;
1302                         num_items++;
1303
1304                         if (seq->type == SEQ_TYPE_META) {
1305                                 /* trim the sub-sequences */
1306                                 num_items += slip_add_sequences_rec(&seq->seqbase, seq_array, trim, num_items + offset, false);
1307                         }
1308                         else if (seq->type & SEQ_TYPE_EFFECT) {
1309                                 trim[offset + num_items] = false;
1310                         }
1311                 }
1312         }
1313
1314         return num_items;
1315 }
1316
1317 static int slip_count_sequences_rec(ListBase *seqbasep, bool first_level)
1318 {
1319         Sequence *seq;
1320         int trimmed_sequences = 0;
1321
1322         for (seq = seqbasep->first; seq; seq = seq->next) {
1323                 if (!first_level || (!(seq->type & SEQ_TYPE_EFFECT) && (seq->flag & SELECT))) {
1324                         trimmed_sequences++;
1325
1326                         if (seq->type == SEQ_TYPE_META) {
1327                                 /* trim the sub-sequences */
1328                                 trimmed_sequences += slip_count_sequences_rec(&seq->seqbase, false);
1329                         }
1330                 }
1331         }
1332
1333         return trimmed_sequences;
1334 }
1335
1336 static int sequencer_slip_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1337 {
1338         SlipData *data;
1339         Scene *scene = CTX_data_scene(C);
1340         Editing *ed = BKE_sequencer_editing_get(scene, false);
1341         float mouseloc[2];
1342         int num_seq, i;
1343         View2D *v2d = UI_view2d_fromcontext(C);
1344
1345         /* first recursively cound the trimmed elements */
1346         num_seq = slip_count_sequences_rec(ed->seqbasep, true);
1347
1348         if (num_seq == 0)
1349                 return OPERATOR_CANCELLED;
1350
1351         data = op->customdata = MEM_mallocN(sizeof(SlipData), "trimdata");
1352         data->ts = MEM_mallocN(num_seq * sizeof(TransSeq), "trimdata_transform");
1353         data->seq_array = MEM_mallocN(num_seq * sizeof(Sequence *), "trimdata_sequences");
1354         data->trim = MEM_mallocN(num_seq * sizeof(bool), "trimdata_trim");
1355         data->num_seq = num_seq;
1356
1357         initNumInput(&data->num_input);
1358         data->num_input.idx_max = 0;
1359         data->num_input.val_flag[0] |= NUM_NO_FRACTION;
1360         data->num_input.unit_sys = USER_UNIT_NONE;
1361         data->num_input.unit_type[0] = 0;
1362
1363
1364         slip_add_sequences_rec(ed->seqbasep, data->seq_array, data->trim, 0, true);
1365
1366         for (i = 0; i < num_seq; i++) {
1367                 transseq_backup(data->ts + i, data->seq_array[i]);
1368         }
1369
1370         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &mouseloc[0], &mouseloc[1]);
1371
1372         copy_v2_v2_int(data->init_mouse, event->mval);
1373         copy_v2_v2(data->init_mouseloc, mouseloc);
1374
1375         data->slow = false;
1376
1377         WM_event_add_modal_handler(C, op);
1378
1379         /* notify so we draw extensions immediately */
1380         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1381
1382         return OPERATOR_RUNNING_MODAL;
1383 }
1384
1385 static bool sequencer_slip_recursively(Scene *scene, SlipData *data, int offset)
1386 {
1387
1388         /* only data types supported for now */
1389         if (offset != 0) {
1390                 Editing *ed = BKE_sequencer_editing_get(scene, false);
1391                 int i;
1392
1393                 /* we iterate in reverse so metastrips are iterated after their children */
1394                 for (i = data->num_seq - 1; i >= 0; i--) {
1395                         Sequence *seq = data->seq_array[i];
1396                         int endframe;
1397                         /* we have the offset, do the terrible math */
1398
1399                         /* first, do the offset */
1400                         seq->start = data->ts[i].start + offset;
1401
1402                         if (data->trim[i]) {
1403                                 /* find the endframe */
1404                                 endframe = seq->start + seq->len;
1405
1406                                 /* now compute the terrible offsets */
1407                                 if (endframe > seq->enddisp) {
1408                                         seq->endstill = 0;
1409                                         seq->endofs = endframe - seq->enddisp;
1410                                 }
1411                                 else if (endframe <= seq->enddisp) {
1412                                         seq->endstill = seq->enddisp - endframe;
1413                                         seq->endofs = 0;
1414                                 }
1415
1416                                 if (seq->start > seq->startdisp) {
1417                                         seq->startstill = seq->start - seq->startdisp;
1418                                         seq->startofs = 0;
1419                                 }
1420                                 else if (seq->start <= seq->startdisp) {
1421                                         seq->startstill = 0;
1422                                         seq->startofs = seq->startdisp - seq->start;
1423                                 }
1424                         }
1425                         else {
1426                                 /* if no real trim, don't change the data, rather transform the strips themselves */
1427                                 seq->startdisp = data->ts[i].startdisp + offset;
1428                                 seq->enddisp = data->ts[i].enddisp + offset;
1429                         }
1430
1431                         /* effects are only added if we they are in a metastrip. In this case, dependent strips will just be transformed and we can skip calculating for effects
1432                          * This way we can avoid an extra loop just for effects*/
1433                         if (!(seq->type & SEQ_TYPE_EFFECT))
1434                                 BKE_sequence_calc(scene, seq);
1435                 }
1436                 BKE_sequencer_free_imbuf(scene, &ed->seqbase, false);
1437
1438                 return true;
1439         }
1440
1441         return false;
1442 }
1443
1444 static int sequencer_slip_exec(bContext *C, wmOperator *op)
1445 {
1446         SlipData *data;
1447         Scene *scene = CTX_data_scene(C);
1448         Editing *ed = BKE_sequencer_editing_get(scene, false);
1449         int num_seq, i;
1450         int offset = RNA_int_get(op->ptr, "offset");
1451         bool success = false;
1452
1453         /* first recursively cound the trimmed elements */
1454         num_seq = slip_count_sequences_rec(ed->seqbasep, true);
1455
1456         if (num_seq == 0)
1457                 return OPERATOR_CANCELLED;
1458
1459         data = op->customdata = MEM_mallocN(sizeof(SlipData), "trimdata");
1460         data->ts = MEM_mallocN(num_seq * sizeof(TransSeq), "trimdata_transform");
1461         data->seq_array = MEM_mallocN(num_seq * sizeof(Sequence *), "trimdata_sequences");
1462         data->trim = MEM_mallocN(num_seq * sizeof(bool), "trimdata_trim");
1463         data->num_seq = num_seq;
1464
1465         slip_add_sequences_rec(ed->seqbasep, data->seq_array, data->trim, 0, true);
1466
1467         for (i = 0; i < num_seq; i++) {
1468                 transseq_backup(data->ts + i, data->seq_array[i]);
1469         }
1470
1471         success = sequencer_slip_recursively(scene, data, offset);
1472
1473         MEM_freeN(data->seq_array);
1474         MEM_freeN(data->trim);
1475         MEM_freeN(data->ts);
1476         MEM_freeN(data);
1477
1478         if (success) {
1479                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1480                 return OPERATOR_FINISHED;
1481         }
1482         else {
1483                 return OPERATOR_CANCELLED;
1484         }
1485 }
1486
1487
1488 static void sequencer_slip_update_header(Scene *scene, ScrArea *sa, SlipData *data, int offset)
1489 {
1490         char msg[UI_MAX_DRAW_STR];
1491
1492         if (sa) {
1493                 if (hasNumInput(&data->num_input)) {
1494                         char num_str[NUM_STR_REP_LEN];
1495                         outputNumInput(&data->num_input, num_str, &scene->unit);
1496                         BLI_snprintf(msg, sizeof(msg), IFACE_("Trim offset: %s"), num_str);
1497                 }
1498                 else {
1499                         BLI_snprintf(msg, sizeof(msg), IFACE_("Trim offset: %d"), offset);
1500                 }
1501         }
1502
1503         ED_area_headerprint(sa, msg);
1504 }
1505
1506 static int sequencer_slip_modal(bContext *C, wmOperator *op, const wmEvent *event)
1507 {
1508         Scene *scene = CTX_data_scene(C);
1509         SlipData *data = (SlipData *)op->customdata;
1510         ScrArea *sa = CTX_wm_area(C);
1511         const bool has_numInput = hasNumInput(&data->num_input);
1512         bool handled = true;
1513
1514         /* Modal numinput active, try to handle numeric inputs first... */
1515         if (event->val == KM_PRESS && has_numInput && handleNumInput(C, &data->num_input, event)) {
1516                 float offset;
1517                 applyNumInput(&data->num_input, &offset);
1518
1519                 sequencer_slip_update_header(scene, sa, data, (int)offset);
1520
1521                 RNA_int_set(op->ptr, "offset", offset);
1522
1523                 if (sequencer_slip_recursively(scene, data, offset)) {
1524                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1525                 }
1526
1527                 return OPERATOR_RUNNING_MODAL;
1528         }
1529
1530         switch (event->type) {
1531                 case MOUSEMOVE:
1532                 {
1533                         if (!has_numInput) {
1534                                 float mouseloc[2];
1535                                 int offset;
1536                                 int mouse_x;
1537                                 View2D *v2d = UI_view2d_fromcontext(C);
1538
1539                                 if (data->slow) {
1540                                         mouse_x = event->mval[0] - data->slow_offset;
1541                                         mouse_x *= 0.1f;
1542                                         mouse_x += data->slow_offset;
1543                                 }
1544                                 else {
1545                                         mouse_x = event->mval[0];
1546                                 }
1547
1548
1549                                 /* choose the side based on which side of the playhead the mouse is on */
1550                                 UI_view2d_region_to_view(v2d, mouse_x, 0, &mouseloc[0], &mouseloc[1]);
1551                                 offset = mouseloc[0] - data->init_mouseloc[0];
1552
1553                                 sequencer_slip_update_header(scene, sa, data, offset);
1554
1555                                 RNA_int_set(op->ptr, "offset", offset);
1556
1557                                 if (sequencer_slip_recursively(scene, data, offset)) {
1558                                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1559                                 }
1560                         }
1561                         break;
1562                 }
1563
1564                 case LEFTMOUSE:
1565                 case RETKEY:
1566                 case SPACEKEY:
1567                 {
1568                         MEM_freeN(data->seq_array);
1569                         MEM_freeN(data->trim);
1570                         MEM_freeN(data->ts);
1571                         MEM_freeN(data);
1572                         op->customdata = NULL;
1573                         if (sa) {
1574                                 ED_area_headerprint(sa, NULL);
1575                         }
1576                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1577                         return OPERATOR_FINISHED;
1578                 }
1579
1580                 case ESCKEY:
1581                 case RIGHTMOUSE:
1582                 {
1583                         int i;
1584                         Editing *ed = BKE_sequencer_editing_get(scene, false);
1585
1586                         for (i = 0; i < data->num_seq; i++) {
1587                                 transseq_restore(data->ts + i, data->seq_array[i]);
1588                         }
1589
1590                         for (i = 0; i < data->num_seq; i++) {
1591                                 Sequence *seq = data->seq_array[i];
1592                                 BKE_sequence_reload_new_file(scene, seq, false);
1593                                 BKE_sequence_calc(scene, seq);
1594                         }
1595
1596                         MEM_freeN(data->seq_array);
1597                         MEM_freeN(data->ts);
1598                         MEM_freeN(data->trim);
1599                         MEM_freeN(data);
1600                         op->customdata = NULL;
1601
1602                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1603
1604                         BKE_sequencer_free_imbuf(scene, &ed->seqbase, false);
1605
1606                         if (sa) {
1607                                 ED_area_headerprint(sa, NULL);
1608                         }
1609
1610                         return OPERATOR_CANCELLED;
1611                 }
1612
1613                 case RIGHTSHIFTKEY:
1614                 case LEFTSHIFTKEY:
1615                         if (!has_numInput) {
1616                                 if (event->val == KM_PRESS) {
1617                                         data->slow = true;
1618                                         data->slow_offset = event->mval[0];
1619                                 }
1620                                 else if (event->val == KM_RELEASE) {
1621                                         data->slow = false;
1622                                 }
1623                         }
1624                         break;
1625
1626                 default:
1627                         handled = false;
1628                         break;
1629         }
1630
1631         /* Modal numinput inactive, try to handle numeric inputs last... */
1632         if (!handled && event->val == KM_PRESS && handleNumInput(C, &data->num_input, event)) {
1633                 float offset;
1634                 applyNumInput(&data->num_input, &offset);
1635
1636                 sequencer_slip_update_header(scene, sa, data, (int)offset);
1637
1638                 RNA_int_set(op->ptr, "offset", offset);
1639
1640                 if (sequencer_slip_recursively(scene, data, offset)) {
1641                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1642                 }
1643         }
1644
1645         return OPERATOR_RUNNING_MODAL;
1646 }
1647
1648 void SEQUENCER_OT_slip(struct wmOperatorType *ot)
1649 {
1650         /* identifiers */
1651         ot->name = "Trim Strips";
1652         ot->idname = "SEQUENCER_OT_slip";
1653         ot->description = "Trim the contents of the active strip";
1654
1655         /* api callbacks */
1656         ot->invoke = sequencer_slip_invoke;
1657         ot->modal = sequencer_slip_modal;
1658         ot->exec = sequencer_slip_exec;
1659         ot->poll = sequencer_edit_poll;
1660
1661         /* flags */
1662         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1663
1664         RNA_def_int(ot->srna, "offset", 0, INT32_MIN, INT32_MAX, "Offset", "Offset to the data of the strip",
1665                     INT32_MIN, INT32_MAX);
1666 }
1667
1668 /* mute operator */
1669 static int sequencer_mute_exec(bContext *C, wmOperator *op)
1670 {
1671         Scene *scene = CTX_data_scene(C);
1672         Editing *ed = BKE_sequencer_editing_get(scene, false);
1673         Sequence *seq;
1674         bool selected;
1675
1676         selected = !RNA_boolean_get(op->ptr, "unselected");
1677         
1678         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1679                 if ((seq->flag & SEQ_LOCK) == 0) {
1680                         if (selected) { /* mute unselected */
1681                                 if (seq->flag & SELECT) {
1682                                         seq->flag |= SEQ_MUTE;
1683                                         BKE_sequence_invalidate_dependent(scene, seq);
1684                                 }
1685                         }
1686                         else {
1687                                 if ((seq->flag & SELECT) == 0) {
1688                                         seq->flag |= SEQ_MUTE;
1689                                         BKE_sequence_invalidate_dependent(scene, seq);
1690                                 }
1691                         }
1692                 }
1693         }
1694         
1695         BKE_sequencer_update_muting(ed);
1696         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1697         
1698         return OPERATOR_FINISHED;
1699 }
1700
1701 void SEQUENCER_OT_mute(struct wmOperatorType *ot)
1702 {
1703         /* identifiers */
1704         ot->name = "Mute Strips";
1705         ot->idname = "SEQUENCER_OT_mute";
1706         ot->description = "Mute (un)selected strips";
1707         
1708         /* api callbacks */
1709         ot->exec = sequencer_mute_exec;
1710         ot->poll = sequencer_edit_poll;
1711         
1712         /* flags */
1713         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1714         
1715         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Mute unselected rather than selected strips");
1716 }
1717
1718
1719 /* unmute operator */
1720 static int sequencer_unmute_exec(bContext *C, wmOperator *op)
1721 {
1722         Scene *scene = CTX_data_scene(C);
1723         Editing *ed = BKE_sequencer_editing_get(scene, false);
1724         Sequence *seq;
1725         bool selected;
1726
1727         selected = !RNA_boolean_get(op->ptr, "unselected");
1728         
1729         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1730                 if ((seq->flag & SEQ_LOCK) == 0) {
1731                         if (selected) { /* unmute unselected */
1732                                 if (seq->flag & SELECT) {
1733                                         seq->flag &= ~SEQ_MUTE;
1734                                         BKE_sequence_invalidate_dependent(scene, seq);
1735                                 }
1736                         }
1737                         else {
1738                                 if ((seq->flag & SELECT) == 0) {
1739                                         seq->flag &= ~SEQ_MUTE;
1740                                         BKE_sequence_invalidate_dependent(scene, seq);
1741                                 }
1742                         }
1743                 }
1744         }
1745         
1746         BKE_sequencer_update_muting(ed);
1747         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1748         
1749         return OPERATOR_FINISHED;
1750 }
1751
1752 void SEQUENCER_OT_unmute(struct wmOperatorType *ot)
1753 {
1754         /* identifiers */
1755         ot->name = "Un-Mute Strips";
1756         ot->idname = "SEQUENCER_OT_unmute";
1757         ot->description = "Unmute (un)selected strips";
1758         
1759         /* api callbacks */
1760         ot->exec = sequencer_unmute_exec;
1761         ot->poll = sequencer_edit_poll;
1762         
1763         /* flags */
1764         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1765         
1766         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Unmute unselected rather than selected strips");
1767 }
1768
1769
1770 /* lock operator */
1771 static int sequencer_lock_exec(bContext *C, wmOperator *UNUSED(op))
1772 {
1773         Scene *scene = CTX_data_scene(C);
1774         Editing *ed = BKE_sequencer_editing_get(scene, false);
1775         Sequence *seq;
1776
1777         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1778                 if (seq->flag & SELECT) {
1779                         seq->flag |= SEQ_LOCK;
1780                 }
1781         }
1782
1783         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1784
1785         return OPERATOR_FINISHED;
1786 }
1787
1788 void SEQUENCER_OT_lock(struct wmOperatorType *ot)
1789 {
1790         /* identifiers */
1791         ot->name = "Lock Strips";
1792         ot->idname = "SEQUENCER_OT_lock";
1793         ot->description = "Lock the active strip so that it can't be transformed";
1794         
1795         /* api callbacks */
1796         ot->exec = sequencer_lock_exec;
1797         ot->poll = sequencer_edit_poll;
1798         
1799         /* flags */
1800         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1801 }
1802
1803 /* unlock operator */
1804 static int sequencer_unlock_exec(bContext *C, wmOperator *UNUSED(op))
1805 {
1806         Scene *scene = CTX_data_scene(C);
1807         Editing *ed = BKE_sequencer_editing_get(scene, false);
1808         Sequence *seq;
1809
1810         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1811                 if (seq->flag & SELECT) {
1812                         seq->flag &= ~SEQ_LOCK;
1813                 }
1814         }
1815
1816         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1817
1818         return OPERATOR_FINISHED;
1819 }
1820
1821 void SEQUENCER_OT_unlock(struct wmOperatorType *ot)
1822 {
1823         /* identifiers */
1824         ot->name = "UnLock Strips";
1825         ot->idname = "SEQUENCER_OT_unlock";
1826         ot->description = "Unlock the active strip so that it can't be transformed";
1827         
1828         /* api callbacks */
1829         ot->exec = sequencer_unlock_exec;
1830         ot->poll = sequencer_edit_poll;
1831         
1832         /* flags */
1833         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1834 }
1835
1836 /* reload operator */
1837 static int sequencer_reload_exec(bContext *C, wmOperator *op)
1838 {
1839         Scene *scene = CTX_data_scene(C);
1840         Editing *ed = BKE_sequencer_editing_get(scene, false);
1841         Sequence *seq;
1842         const bool adjust_length = RNA_boolean_get(op->ptr, "adjust_length");
1843
1844         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1845                 if (seq->flag & SELECT) {
1846                         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
1847                         BKE_sequence_reload_new_file(scene, seq, !adjust_length);
1848
1849                         if (adjust_length) {
1850                                 if (BKE_sequence_test_overlap(ed->seqbasep, seq))
1851                                         BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1852                         }
1853                 }
1854         }
1855
1856         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1857
1858         return OPERATOR_FINISHED;
1859 }
1860
1861 void SEQUENCER_OT_reload(struct wmOperatorType *ot)
1862 {
1863         PropertyRNA *prop;
1864
1865         /* identifiers */
1866         ot->name = "Reload Strips";
1867         ot->idname = "SEQUENCER_OT_reload";
1868         ot->description = "Reload strips in the sequencer";
1869         
1870         /* api callbacks */
1871         ot->exec = sequencer_reload_exec;
1872         ot->poll = sequencer_edit_poll;
1873         
1874         /* flags */
1875         ot->flag = OPTYPE_REGISTER; /* no undo, the data changed is stored outside 'main' */
1876
1877         prop = RNA_def_boolean(ot->srna, "adjust_length", 0, "Adjust Length",
1878                                "Adjust length of strips to their data length");
1879         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1880 }
1881
1882 /* reload operator */
1883 static int sequencer_refresh_all_poll(bContext *C)
1884 {
1885         if (G.is_rendering) {
1886                 return 0;
1887         }
1888         return sequencer_edit_poll(C);
1889 }
1890
1891 static int sequencer_refresh_all_exec(bContext *C, wmOperator *UNUSED(op))
1892 {
1893         Scene *scene = CTX_data_scene(C);
1894         Editing *ed = BKE_sequencer_editing_get(scene, false);
1895
1896         BKE_sequencer_free_imbuf(scene, &ed->seqbase, false);
1897
1898         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1899
1900         return OPERATOR_FINISHED;
1901 }
1902
1903 void SEQUENCER_OT_refresh_all(struct wmOperatorType *ot)
1904 {
1905         /* identifiers */
1906         ot->name = "Refresh Sequencer";
1907         ot->idname = "SEQUENCER_OT_refresh_all";
1908         ot->description = "Refresh the sequencer editor";
1909         
1910         /* api callbacks */
1911         ot->exec = sequencer_refresh_all_exec;
1912         ot->poll = sequencer_refresh_all_poll;
1913 }
1914
1915 static int sequencer_reassign_inputs_exec(bContext *C, wmOperator *op)
1916 {
1917         Scene *scene = CTX_data_scene(C);
1918         Sequence *seq1, *seq2, *seq3, *last_seq = BKE_sequencer_active_get(scene);
1919         const char *error_msg;
1920
1921         if (!seq_effect_find_selected(scene, last_seq, last_seq->type, &seq1, &seq2, &seq3, &error_msg)) {
1922                 BKE_report(op->reports, RPT_ERROR, error_msg);
1923                 return OPERATOR_CANCELLED;
1924         }
1925         /* see reassigning would create a cycle */
1926         if (seq_is_predecessor(seq1, last_seq) ||
1927             seq_is_predecessor(seq2, last_seq) ||
1928             seq_is_predecessor(seq3, last_seq))
1929         {
1930                 BKE_report(op->reports, RPT_ERROR, "Cannot reassign inputs: no cycles allowed");
1931                 return OPERATOR_CANCELLED;
1932         }
1933
1934         last_seq->seq1 = seq1;
1935         last_seq->seq2 = seq2;
1936         last_seq->seq3 = seq3;
1937
1938         BKE_sequencer_update_changed_seq_and_deps(scene, last_seq, 1, 1);
1939
1940         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1941
1942         return OPERATOR_FINISHED;
1943 }
1944
1945 static int sequencer_effect_poll(bContext *C)
1946 {
1947         Scene *scene = CTX_data_scene(C);
1948         Editing *ed = BKE_sequencer_editing_get(scene, false);
1949
1950         if (ed) {
1951                 Sequence *last_seq = BKE_sequencer_active_get(scene);
1952                 if (last_seq && (last_seq->type & SEQ_TYPE_EFFECT)) {
1953                         return 1;
1954                 }
1955         }
1956
1957         return 0;
1958 }
1959
1960 void SEQUENCER_OT_reassign_inputs(struct wmOperatorType *ot)
1961 {
1962         /* identifiers */
1963         ot->name = "Reassign Inputs";
1964         ot->idname = "SEQUENCER_OT_reassign_inputs";
1965         ot->description = "Reassign the inputs for the effect strip";
1966
1967         /* api callbacks */
1968         ot->exec = sequencer_reassign_inputs_exec;
1969         ot->poll = sequencer_effect_poll;
1970
1971         /* flags */
1972         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1973 }
1974
1975
1976 static int sequencer_swap_inputs_exec(bContext *C, wmOperator *op)
1977 {
1978         Scene *scene = CTX_data_scene(C);
1979         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene);
1980
1981         if (last_seq->seq1 == NULL || last_seq->seq2 == NULL) {
1982                 BKE_report(op->reports, RPT_ERROR, "No valid inputs to swap");
1983                 return OPERATOR_CANCELLED;
1984         }
1985
1986         seq = last_seq->seq1;
1987         last_seq->seq1 = last_seq->seq2;
1988         last_seq->seq2 = seq;
1989
1990         BKE_sequencer_update_changed_seq_and_deps(scene, last_seq, 1, 1);
1991
1992         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1993
1994         return OPERATOR_FINISHED;
1995 }
1996 void SEQUENCER_OT_swap_inputs(struct wmOperatorType *ot)
1997 {
1998         /* identifiers */
1999         ot->name = "Swap Inputs";
2000         ot->idname = "SEQUENCER_OT_swap_inputs";
2001         ot->description = "Swap the first two inputs for the effect strip";
2002
2003         /* api callbacks */
2004         ot->exec = sequencer_swap_inputs_exec;
2005         ot->poll = sequencer_effect_poll;
2006
2007         /* flags */
2008         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2009 }
2010
2011
2012 /* cut operator */
2013 static EnumPropertyItem prop_cut_types[] = {
2014         {SEQ_CUT_SOFT, "SOFT", 0, "Soft", ""},
2015         {SEQ_CUT_HARD, "HARD", 0, "Hard", ""},
2016         {0, NULL, 0, NULL, NULL}
2017 };
2018
2019 static int sequencer_cut_exec(bContext *C, wmOperator *op)
2020 {
2021         Scene *scene = CTX_data_scene(C);
2022         Editing *ed = BKE_sequencer_editing_get(scene, false);
2023         int cut_side, cut_hard, cut_frame;
2024
2025         bool changed;
2026
2027         cut_frame = RNA_int_get(op->ptr, "frame");
2028         cut_hard = RNA_enum_get(op->ptr, "type");
2029         cut_side = RNA_enum_get(op->ptr, "side");
2030         
2031         if (cut_hard == SEQ_CUT_HARD) {
2032                 changed = cut_seq_list(scene, ed->seqbasep, cut_frame, cut_seq_hard);
2033         }
2034         else {
2035                 changed = cut_seq_list(scene, ed->seqbasep, cut_frame, cut_seq_soft);
2036         }
2037         
2038         if (changed) { /* got new strips ? */
2039                 Sequence *seq;
2040
2041                 if (cut_side != SEQ_SIDE_BOTH) {
2042                         SEQP_BEGIN (ed, seq)
2043                         {
2044                                 if (cut_side == SEQ_SIDE_LEFT) {
2045                                         if (seq->startdisp >= cut_frame) {
2046                                                 seq->flag &= ~SEQ_ALLSEL;
2047                                         }
2048                                 }
2049                                 else {
2050                                         if (seq->enddisp <= cut_frame) {
2051                                                 seq->flag &= ~SEQ_ALLSEL;
2052                                         }
2053                                 }
2054                         }
2055                         SEQ_END;
2056                 }
2057
2058                 SEQP_BEGIN (ed, seq)
2059                 {
2060                         if (seq->seq1 || seq->seq2 || seq->seq3) {
2061                                 BKE_sequence_calc(scene, seq);
2062                         }
2063                 }
2064                 SEQ_END;
2065
2066                 /* as last: */
2067                 BKE_sequencer_sort(scene);
2068         }
2069
2070         if (changed) {
2071                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2072                 return OPERATOR_FINISHED;
2073         }
2074         else {
2075                 return OPERATOR_CANCELLED;
2076         }
2077 }
2078
2079
2080 static int sequencer_cut_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2081 {
2082         Scene *scene = CTX_data_scene(C);
2083         View2D *v2d = UI_view2d_fromcontext(C);
2084
2085         int cut_side = SEQ_SIDE_BOTH;
2086         int cut_frame = CFRA;
2087
2088         if (ED_operator_sequencer_active(C) && v2d)
2089                 cut_side = mouse_frame_side(v2d, event->mval[0], cut_frame);
2090         
2091         RNA_int_set(op->ptr, "frame", cut_frame);
2092         RNA_enum_set(op->ptr, "side", cut_side);
2093         /*RNA_enum_set(op->ptr, "type", cut_hard); */ /*This type is set from the key shortcut */
2094
2095         return sequencer_cut_exec(C, op);
2096 }
2097
2098
2099 void SEQUENCER_OT_cut(struct wmOperatorType *ot)
2100 {
2101         /* identifiers */
2102         ot->name = "Cut Strips";
2103         ot->idname = "SEQUENCER_OT_cut";
2104         ot->description = "Cut the selected strips";
2105         
2106         /* api callbacks */
2107         ot->invoke = sequencer_cut_invoke;
2108         ot->exec = sequencer_cut_exec;
2109         ot->poll = sequencer_edit_poll;
2110         
2111         /* flags */
2112         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2113         
2114         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be cut", INT_MIN, INT_MAX);
2115         RNA_def_enum(ot->srna, "type", prop_cut_types, SEQ_CUT_SOFT, "Type", "The type of cut operation to perform on strips");
2116         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side that remains selected after cutting");
2117 }
2118
2119 /* duplicate operator */
2120 static int apply_unique_name_cb(Sequence *seq, void *arg_pt)
2121 {
2122         Scene *scene = (Scene *)arg_pt;
2123         char name[sizeof(seq->name) - 2];
2124
2125         BLI_strncpy_utf8(name, seq->name + 2, sizeof(name));
2126         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
2127         BKE_sequencer_dupe_animdata(scene, name, seq->name + 2);
2128         return 1;
2129
2130 }
2131
2132 static int sequencer_add_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
2133 {
2134         Scene *scene = CTX_data_scene(C);
2135         Editing *ed = BKE_sequencer_editing_get(scene, false);
2136
2137         ListBase nseqbase = {NULL, NULL};
2138
2139         if (ed == NULL)
2140                 return OPERATOR_CANCELLED;
2141
2142         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT);
2143
2144         if (nseqbase.first) {
2145                 Sequence *seq = nseqbase.first;
2146                 /* rely on the nseqbase list being added at the end */
2147                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2148
2149                 for (; seq; seq = seq->next)
2150                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
2151
2152                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2153                 return OPERATOR_FINISHED;
2154         }
2155
2156         return OPERATOR_CANCELLED;
2157 }
2158
2159 void SEQUENCER_OT_duplicate(wmOperatorType *ot)
2160 {
2161         /* identifiers */
2162         ot->name = "Duplicate Strips";
2163         ot->idname = "SEQUENCER_OT_duplicate";
2164         ot->description = "Duplicate the selected strips";
2165         
2166         /* api callbacks */
2167         ot->exec = sequencer_add_duplicate_exec;
2168         ot->poll = ED_operator_sequencer_active;
2169         
2170         /* flags */
2171         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2172         
2173         /* to give to transform */
2174         RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2175 }
2176
2177 /* delete operator */
2178 static int sequencer_delete_exec(bContext *C, wmOperator *UNUSED(op))
2179 {
2180         Scene *scene = CTX_data_scene(C);
2181         Editing *ed = BKE_sequencer_editing_get(scene, false);
2182         Sequence *seq;
2183         MetaStack *ms;
2184         bool nothingSelected = true;
2185
2186         seq = BKE_sequencer_active_get(scene);
2187         if (seq && seq->flag & SELECT) { /* avoid a loop since this is likely to be selected */
2188                 nothingSelected = false;
2189         }
2190         else {
2191                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2192                         if (seq->flag & SELECT) {
2193                                 nothingSelected = false;
2194                                 break;
2195                         }
2196                 }
2197         }
2198
2199         if (nothingSelected)
2200                 return OPERATOR_FINISHED;
2201
2202         /* for effects and modifiers, try to find a replacement input */
2203         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2204                 if (!(seq->flag & SELECT)) {
2205                         if ((seq->type & SEQ_TYPE_EFFECT)) {
2206                                 del_seq_find_replace_recurs(scene, seq);
2207                         }
2208                 }
2209                 else {
2210                         del_seq_clear_modifiers_recurs(scene, seq);
2211                 }
2212         }
2213
2214         /* delete all selected strips */
2215         recurs_del_seq_flag(scene, ed->seqbasep, SELECT, 0);
2216
2217         /* updates lengths etc */
2218         seq = ed->seqbasep->first;
2219         while (seq) {
2220                 BKE_sequence_calc(scene, seq);
2221                 seq = seq->next;
2222         }
2223
2224         /* free parent metas */
2225         ms = ed->metastack.last;
2226         while (ms) {
2227                 BKE_sequence_calc(scene, ms->parseq);
2228                 ms = ms->prev;
2229         }
2230
2231         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2232         
2233         return OPERATOR_FINISHED;
2234 }
2235
2236 static int sequencer_delete_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2237 {
2238         ARegion *ar = CTX_wm_region(C);
2239
2240         if (ar->regiontype == RGN_TYPE_WINDOW) {
2241                 /* bounding box of 30 pixels is used for markers shortcuts,
2242                  * prevent conflict with markers shortcuts here
2243                  */
2244                 if (event->mval[1] <= 30)
2245                         return OPERATOR_PASS_THROUGH;
2246         }
2247
2248         return WM_operator_confirm(C, op, event);
2249 }
2250
2251 void SEQUENCER_OT_delete(wmOperatorType *ot)
2252 {
2253
2254         /* identifiers */
2255         ot->name = "Erase Strips";
2256         ot->idname = "SEQUENCER_OT_delete";
2257         ot->description = "Erase selected strips from the sequencer";
2258         
2259         /* api callbacks */
2260         ot->invoke = sequencer_delete_invoke;
2261         ot->exec = sequencer_delete_exec;
2262         ot->poll = sequencer_edit_poll;
2263         
2264         /* flags */
2265         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2266 }
2267
2268
2269 /* offset clear operator */
2270 static int sequencer_offset_clear_exec(bContext *C, wmOperator *UNUSED(op))
2271 {
2272         Scene *scene = CTX_data_scene(C);
2273         Editing *ed = BKE_sequencer_editing_get(scene, false);
2274         Sequence *seq;
2275
2276         /* for effects, try to find a replacement input */
2277         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2278                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
2279                         seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
2280                 }
2281         }
2282
2283         /* updates lengths etc */
2284         seq = ed->seqbasep->first;
2285         while (seq) {
2286                 BKE_sequence_calc(scene, seq);
2287                 seq = seq->next;
2288         }
2289
2290         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2291                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
2292                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2293                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2294                         }
2295                 }
2296         }
2297
2298         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2299
2300         return OPERATOR_FINISHED;
2301 }
2302
2303
2304 void SEQUENCER_OT_offset_clear(wmOperatorType *ot)
2305 {
2306
2307         /* identifiers */
2308         ot->name = "Clear Strip Offset";
2309         ot->idname = "SEQUENCER_OT_offset_clear";
2310         ot->description = "Clear strip offsets from the start and end frames";
2311
2312         /* api callbacks */
2313         ot->exec = sequencer_offset_clear_exec;
2314         ot->poll = sequencer_edit_poll;
2315
2316         /* flags */
2317         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2318 }
2319
2320
2321 /* separate_images operator */
2322 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
2323 {
2324         Scene *scene = CTX_data_scene(C);
2325         Editing *ed = BKE_sequencer_editing_get(scene, false);
2326         
2327         Sequence *seq, *seq_new;
2328         Strip *strip_new;
2329         StripElem *se, *se_new;
2330         int start_ofs, cfra, frame_end;
2331         int step = RNA_int_get(op->ptr, "length");
2332
2333         seq = ed->seqbasep->first; /* poll checks this is valid */
2334
2335         while (seq) {
2336                 if ((seq->flag & SELECT) && (seq->type == SEQ_TYPE_IMAGE) && (seq->len > 1)) {
2337                         Sequence *seq_next;
2338
2339                         /* remove seq so overlap tests don't conflict,
2340                          * see seq_free_sequence below for the real free'ing */
2341                         BLI_remlink(ed->seqbasep, seq);
2342                         /* if (seq->ipo) id_us_min(&seq->ipo->id); */
2343                         /* XXX, remove fcurve and assign to split image strips */
2344
2345                         start_ofs = cfra = BKE_sequence_tx_get_final_left(seq, false);
2346                         frame_end = BKE_sequence_tx_get_final_right(seq, false);
2347
2348                         while (cfra < frame_end) {
2349                                 /* new seq */
2350                                 se = BKE_sequencer_give_stripelem(seq, cfra);
2351
2352                                 seq_new = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
2353                                 BLI_addtail(ed->seqbasep, seq_new);
2354
2355                                 seq_new->start = start_ofs;
2356                                 seq_new->type = SEQ_TYPE_IMAGE;
2357                                 seq_new->len = 1;
2358                                 seq_new->endstill = step - 1;
2359
2360                                 /* new strip */
2361                                 strip_new = seq_new->strip;
2362                                 strip_new->us = 1;
2363
2364                                 /* new stripdata (only one element now!) */
2365                                 /* Note this assume all elements (images) have the same dimension, since we only copy the name here. */
2366                                 se_new = MEM_reallocN(strip_new->stripdata, sizeof(*se_new));
2367                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
2368                                 strip_new->stripdata = se_new;
2369
2370                                 BKE_sequence_calc(scene, seq_new);
2371
2372                                 if (step > 1) {
2373                                         seq_new->flag &= ~SEQ_OVERLAP;
2374                                         if (BKE_sequence_test_overlap(ed->seqbasep, seq_new)) {
2375                                                 BKE_sequence_base_shuffle(ed->seqbasep, seq_new, scene);
2376                                         }
2377                                 }
2378
2379                                 /* XXX, COPY FCURVES */
2380
2381                                 cfra++;
2382                                 start_ofs += step;
2383                         }
2384
2385                         seq_next = seq->next;
2386                         BKE_sequence_free(scene, seq);
2387                         seq = seq_next;
2388                 }
2389                 else {
2390                         seq = seq->next;
2391                 }
2392         }
2393
2394         /* as last: */
2395         BKE_sequencer_sort(scene);
2396         
2397         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2398
2399         return OPERATOR_FINISHED;
2400 }
2401
2402
2403 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
2404 {
2405         /* identifiers */
2406         ot->name = "Separate Images";
2407         ot->idname = "SEQUENCER_OT_images_separate";
2408         ot->description = "On image sequence strips, it returns a strip for each image";
2409         
2410         /* api callbacks */
2411         ot->exec = sequencer_separate_images_exec;
2412         ot->invoke = WM_operator_props_popup_confirm;
2413         ot->poll = sequencer_edit_poll;
2414         
2415         /* flags */
2416         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2417
2418         RNA_def_int(ot->srna, "length", 1, 1, INT_MAX, "Length", "Length of each frame", 1, 1000);
2419 }
2420
2421
2422 /* META Operators */
2423
2424 /* separate_meta_toggle operator */
2425 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2426 {
2427         Scene *scene = CTX_data_scene(C);
2428         Editing *ed = BKE_sequencer_editing_get(scene, false);
2429         Sequence *last_seq = BKE_sequencer_active_get(scene);
2430         MetaStack *ms;
2431
2432         if (last_seq && last_seq->type == SEQ_TYPE_META && last_seq->flag & SELECT) {
2433                 /* Enter Metastrip */
2434                 ms = MEM_mallocN(sizeof(MetaStack), "metastack");
2435                 BLI_addtail(&ed->metastack, ms);
2436                 ms->parseq = last_seq;
2437                 ms->oldbasep = ed->seqbasep;
2438                 copy_v2_v2_int(ms->disp_range, &ms->parseq->startdisp);
2439
2440                 ed->seqbasep = &last_seq->seqbase;
2441
2442                 BKE_sequencer_active_set(scene, NULL);
2443
2444         }
2445         else {
2446                 /* Exit Metastrip (if possible) */
2447
2448                 Sequence *seq;
2449
2450                 if (BLI_listbase_is_empty(&ed->metastack))
2451                         return OPERATOR_CANCELLED;
2452
2453                 ms = ed->metastack.last;
2454                 BLI_remlink(&ed->metastack, ms);
2455
2456                 ed->seqbasep = ms->oldbasep;
2457
2458                 /* for old files, update from meta */
2459                 if (ms->disp_range[0] ==  ms->disp_range[1]) {
2460                         copy_v2_v2_int(ms->disp_range, &ms->parseq->startdisp);
2461                 }
2462
2463                 /* recalc all: the meta can have effects connected to it */
2464                 for (seq = ed->seqbasep->first; seq; seq = seq->next)
2465                         BKE_sequence_calc(scene, seq);
2466
2467                 /* 2.73+, keeping endpoings is important!
2468                  * moving them around means you can't usefully use metas in a complex edit */
2469 #if 1
2470                 BKE_sequence_tx_set_final_left(ms->parseq, ms->disp_range[0]);
2471                 BKE_sequence_tx_set_final_right(ms->parseq, ms->disp_range[1]);
2472                 BKE_sequence_single_fix(ms->parseq);
2473                 BKE_sequence_calc(scene, ms->parseq);
2474 #else
2475                 if (BKE_sequence_test_overlap(ed->seqbasep, ms->parseq))
2476                         BKE_sequence_base_shuffle(ed->seqbasep, ms->parseq, scene);
2477 #endif
2478
2479                 BKE_sequencer_active_set(scene, ms->parseq);
2480
2481                 ms->parseq->flag |= SELECT;
2482                 recurs_sel_seq(ms->parseq);
2483
2484                 MEM_freeN(ms);
2485
2486         }
2487
2488         BKE_sequencer_update_muting(ed);
2489         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2490
2491         return OPERATOR_FINISHED;
2492 }
2493
2494 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
2495 {
2496         /* identifiers */
2497         ot->name = "Toggle Meta Strip";
2498         ot->idname = "SEQUENCER_OT_meta_toggle";
2499         ot->description = "Toggle a metastrip (to edit enclosed strips)";
2500         
2501         /* api callbacks */
2502         ot->exec = sequencer_meta_toggle_exec;
2503         ot->poll = sequencer_edit_poll;
2504         
2505         /* flags */
2506         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2507 }
2508
2509
2510 /* separate_meta_make operator */
2511 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
2512 {
2513         Scene *scene = CTX_data_scene(C);
2514         Editing *ed = BKE_sequencer_editing_get(scene, false);
2515         
2516         Sequence *seq, *seqm, *next, *last_seq = BKE_sequencer_active_get(scene);
2517         int channel_max = 1;
2518
2519         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == false) {
2520                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2521                 return OPERATOR_CANCELLED;
2522         }
2523
2524         /* remove all selected from main list, and put in meta */
2525
2526         seqm = BKE_sequence_alloc(ed->seqbasep, 1, 1); /* channel number set later */
2527         strcpy(seqm->name + 2, "MetaStrip");
2528         seqm->type = SEQ_TYPE_META;
2529         seqm->flag = SELECT;
2530
2531         seq = ed->seqbasep->first;
2532         while (seq) {
2533                 next = seq->next;
2534                 if (seq != seqm && (seq->flag & SELECT)) {
2535                         BKE_sequence_invalidate_cache(scene, seq);
2536                         channel_max = max_ii(seq->machine, channel_max);
2537                         BLI_remlink(ed->seqbasep, seq);
2538                         BLI_addtail(&seqm->seqbase, seq);
2539                 }
2540                 seq = next;
2541         }
2542         seqm->machine = last_seq ? last_seq->machine : channel_max;
2543         BKE_sequence_calc(scene, seqm);
2544
2545         seqm->strip = MEM_callocN(sizeof(Strip), "metastrip");
2546         seqm->strip->us = 1;
2547         
2548         BKE_sequencer_active_set(scene, seqm);
2549
2550         if (BKE_sequence_test_overlap(ed->seqbasep, seqm) ) BKE_sequence_base_shuffle(ed->seqbasep, seqm, scene);
2551
2552         BKE_sequencer_update_muting(ed);
2553
2554         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seqm);
2555
2556         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2557
2558         return OPERATOR_FINISHED;
2559 }
2560
2561 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
2562 {
2563         /* identifiers */
2564         ot->name = "Make Meta Strip";
2565         ot->idname = "SEQUENCER_OT_meta_make";
2566         ot->description = "Group selected strips into a metastrip";
2567         
2568         /* api callbacks */
2569         ot->exec = sequencer_meta_make_exec;
2570         ot->poll = sequencer_edit_poll;
2571         
2572         /* flags */
2573         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2574 }
2575
2576
2577 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
2578 {
2579         if (seq == seqm) return 1;
2580         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
2581         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
2582         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
2583         else return 0;
2584 }
2585
2586 /* separate_meta_make operator */
2587 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
2588 {
2589         Scene *scene = CTX_data_scene(C);
2590         Editing *ed = BKE_sequencer_editing_get(scene, false);
2591
2592         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene); /* last_seq checks (ed == NULL) */
2593
2594         if (last_seq == NULL || last_seq->type != SEQ_TYPE_META)
2595                 return OPERATOR_CANCELLED;
2596
2597         for (seq = last_seq->seqbase.first; seq != NULL; seq = seq->next) {
2598                 BKE_sequence_invalidate_cache(scene, seq);
2599         }
2600
2601         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
2602
2603         BLI_listbase_clear(&last_seq->seqbase);
2604
2605         BLI_remlink(ed->seqbasep, last_seq);
2606         BKE_sequence_free(scene, last_seq);
2607
2608         /* emtpy meta strip, delete all effects depending on it */
2609         for (seq = ed->seqbasep->first; seq; seq = seq->next)
2610                 if ((seq->type & SEQ_TYPE_EFFECT) && seq_depends_on_meta(seq, last_seq))
2611                         seq->flag |= SEQ_FLAG_DELETE;
2612
2613         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2614
2615         /* test for effects and overlap
2616          * don't use SEQP_BEGIN since that would be recursive */
2617         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2618                 if (seq->flag & SELECT) {
2619                         seq->flag &= ~SEQ_OVERLAP;
2620                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2621                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2622                         }
2623                 }
2624         }
2625
2626         BKE_sequencer_sort(scene);
2627         BKE_sequencer_update_muting(ed);
2628
2629         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2630
2631         return OPERATOR_FINISHED;
2632 }
2633
2634 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2635 {
2636         /* identifiers */
2637         ot->name = "UnMeta Strip";
2638         ot->idname = "SEQUENCER_OT_meta_separate";
2639         ot->description = "Put the contents of a metastrip back in the sequencer";
2640         
2641         /* api callbacks */
2642         ot->exec = sequencer_meta_separate_exec;
2643         ot->poll = sequencer_edit_poll;
2644         
2645         /* flags */
2646         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2647 }
2648
2649 /* view_all operator */
2650 static int sequencer_view_all_exec(bContext *C, wmOperator *op)
2651 {
2652         ARegion *ar = CTX_wm_region(C);
2653         View2D *v2d = UI_view2d_fromcontext(C);
2654         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2655
2656         UI_view2d_smooth_view(C, ar, &v2d->tot, smooth_viewtx);
2657         return OPERATOR_FINISHED;
2658 }
2659
2660 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2661 {
2662         /* identifiers */
2663         ot->name = "View All";
2664         ot->idname = "SEQUENCER_OT_view_all";
2665         ot->description = "View all the strips in the sequencer";
2666         
2667         /* api callbacks */
2668         ot->exec = sequencer_view_all_exec;
2669         ot->poll = ED_operator_sequencer_active;
2670         
2671         /* flags */
2672         ot->flag = OPTYPE_REGISTER;
2673 }
2674
2675 static int sequencer_view_frame_exec(bContext *C, wmOperator *op)
2676 {
2677         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2678         ANIM_center_frame(C, smooth_viewtx);
2679         
2680         return OPERATOR_FINISHED;
2681 }
2682
2683 void SEQUENCER_OT_view_frame(wmOperatorType *ot)
2684 {
2685         /* identifiers */
2686         ot->name = "View Frame";
2687         ot->idname = "SEQUENCER_OT_view_frame";
2688         ot->description = "Reset viewable area to show range around current frame";
2689         
2690         /* api callbacks */
2691         ot->exec = sequencer_view_frame_exec;
2692         ot->poll = ED_operator_sequencer_active;
2693         
2694         /* flags */
2695         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2696 }
2697
2698 /* view_all operator */
2699 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2700 {
2701         bScreen *sc = CTX_wm_screen(C);
2702         ScrArea *area = CTX_wm_area(C);
2703 #if 0
2704         ARegion *ar = CTX_wm_region(C);
2705         SpaceSeq *sseq = area->spacedata.first;
2706         Scene *scene = CTX_data_scene(C);
2707 #endif
2708         View2D *v2d = UI_view2d_fromcontext(C);
2709
2710         v2d->cur = v2d->tot;
2711         UI_view2d_curRect_validate(v2d);
2712         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2713         
2714 #if 0
2715         /* Like zooming on an image view */
2716         float zoomX, zoomY;
2717         int width, height, imgwidth, imgheight;
2718
2719         width = ar->winx;
2720         height = ar->winy;
2721
2722         seq_reset_imageofs(sseq);
2723
2724         imgwidth = (scene->r.size * scene->r.xsch) / 100;
2725         imgheight = (scene->r.size * scene->r.ysch) / 100;
2726
2727         /* Apply aspect, dosnt need to be that accurate */
2728         imgwidth = (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2729
2730         if (((imgwidth >= width) || (imgheight >= height)) &&
2731             ((width > 0) && (height > 0)))
2732         {
2733                 /* Find the zoom value that will fit the image in the image space */
2734                 zoomX = ((float)width) / ((float)imgwidth);
2735                 zoomY = ((float)height) / ((float)imgheight);
2736                 sseq->zoom = (zoomX < zoomY) ? zoomX : zoomY;
2737
2738                 sseq->zoom = 1.0f / power_of_2(1 / min_ff(zoomX, zoomY));
2739         }
2740         else {
2741                 sseq->zoom = 1.0f;
2742         }
2743 #endif
2744
2745         ED_area_tag_redraw(CTX_wm_area(C));
2746         return OPERATOR_FINISHED;
2747 }
2748
2749 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2750 {
2751         /* identifiers */
2752         ot->name = "View All";
2753         ot->idname = "SEQUENCER_OT_view_all_preview";
2754         ot->description = "Zoom preview to fit in the area";
2755         
2756         /* api callbacks */
2757         ot->exec = sequencer_view_all_preview_exec;
2758         ot->poll = ED_operator_sequencer_active;
2759         
2760         /* flags */
2761         ot->flag = OPTYPE_REGISTER;
2762 }
2763
2764
2765 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2766 {
2767         RenderData *rd = &CTX_data_scene(C)->r;
2768         View2D *v2d = UI_view2d_fromcontext(C);
2769
2770         float ratio = RNA_float_get(op->ptr, "ratio");
2771
2772         float winx = (int)(rd->size * rd->xsch) / 100;
2773         float winy = (int)(rd->size * rd->ysch) / 100;
2774
2775         float facx = BLI_rcti_size_x(&v2d->mask) / winx;
2776         float facy = BLI_rcti_size_y(&v2d->mask) / winy;
2777
2778         BLI_rctf_resize(&v2d->cur, floorf(winx * facx / ratio + 0.5f), floorf(winy * facy / ratio + 0.5f));
2779
2780         ED_region_tag_redraw(CTX_wm_region(C));
2781
2782         return OPERATOR_FINISHED;
2783 }
2784
2785 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2786 {
2787         /* identifiers */
2788         ot->name = "Sequencer View Zoom Ratio";
2789         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2790         ot->description = "Change zoom ratio of sequencer preview";
2791
2792         /* api callbacks */
2793         ot->exec = sequencer_view_zoom_ratio_exec;
2794         ot->poll = ED_operator_sequencer_active;
2795
2796         /* properties */
2797         RNA_def_float(ot->srna, "ratio", 1.0f, -FLT_MAX, FLT_MAX,
2798                       "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2799 }
2800
2801
2802 #if 0
2803 static EnumPropertyItem view_type_items[] = {
2804         {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2805         {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2806         {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2807         {0, NULL, 0, NULL, NULL}
2808 };
2809 #endif
2810
2811 /* view_all operator */
2812 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2813 {
2814         SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
2815
2816         sseq->view++;
2817         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2818
2819         ED_area_tag_refresh(CTX_wm_area(C));
2820
2821         return OPERATOR_FINISHED;
2822 }
2823
2824 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2825 {
2826         /* identifiers */
2827         ot->name = "View Toggle";
2828         ot->idname = "SEQUENCER_OT_view_toggle";
2829         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2830         
2831         /* api callbacks */
2832         ot->exec = sequencer_view_toggle_exec;
2833         ot->poll = ED_operator_sequencer_active;
2834         
2835         /* flags */
2836         ot->flag = OPTYPE_REGISTER;
2837 }
2838
2839
2840 /* view_selected operator */
2841 static int sequencer_view_selected_exec(bContext *C, wmOperator *op)
2842 {
2843         Scene *scene = CTX_data_scene(C);
2844         View2D *v2d = UI_view2d_fromcontext(C);
2845         ARegion *ar = CTX_wm_region(C);
2846         Editing *ed = BKE_sequencer_editing_get(scene, false);
2847         Sequence *last_seq = BKE_sequencer_active_get(scene);
2848         Sequence *seq;
2849         rctf cur_new = v2d->cur;
2850
2851         int xmin =  MAXFRAME * 2;
2852         int xmax = -MAXFRAME * 2;
2853         int ymin =  MAXSEQ + 1;
2854         int ymax = 0;
2855         int orig_height;
2856         int ymid;
2857         int ymargin = 1;
2858         int xmargin = FPS;
2859
2860         if (ed == NULL)
2861                 return OPERATOR_CANCELLED;
2862
2863         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2864                 if ((seq->flag & SELECT) || (seq == last_seq)) {
2865                         xmin = min_ii(xmin, seq->startdisp);
2866                         xmax = max_ii(xmax, seq->enddisp);
2867
2868                         ymin = min_ii(ymin, seq->machine);
2869                         ymax = max_ii(ymax, seq->machine);
2870                 }
2871         }
2872
2873         if (ymax != 0) {
2874                 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2875                 
2876                 xmax += xmargin;
2877                 xmin -= xmargin;
2878                 ymax += ymargin;
2879                 ymin -= ymargin;
2880
2881                 orig_height = BLI_rctf_size_y(&cur_new);
2882
2883                 cur_new.xmin = xmin;
2884                 cur_new.xmax = xmax;
2885
2886                 cur_new.ymin = ymin;
2887                 cur_new.ymax = ymax;
2888
2889                 /* only zoom out vertically */
2890                 if (orig_height > BLI_rctf_size_y(&cur_new)) {
2891                         ymid = BLI_rctf_cent_y(&cur_new);
2892
2893                         cur_new.ymin = ymid - (orig_height / 2);
2894                         cur_new.ymax = ymid + (orig_height / 2);
2895                 }
2896
2897                 UI_view2d_smooth_view(C, ar, &cur_new, smooth_viewtx);
2898
2899                 return OPERATOR_FINISHED;
2900         }
2901         else {
2902                 return OPERATOR_CANCELLED;
2903         }
2904         
2905 }
2906
2907 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2908 {
2909         /* identifiers */
2910         ot->name = "View Selected";
2911         ot->idname = "SEQUENCER_OT_view_selected";
2912         ot->description = "Zoom the sequencer on the selected strips";
2913         
2914         /* api callbacks */
2915         ot->exec = sequencer_view_selected_exec;
2916         ot->poll = ED_operator_sequencer_active;
2917         
2918         /* flags */
2919         ot->flag = OPTYPE_REGISTER;
2920 }
2921
2922 static bool strip_jump_internal(Scene *scene,
2923                                 const short side,
2924                                 const bool do_skip_mute, const bool do_center)
2925 {
2926         bool changed = false;
2927         int cfra = CFRA;
2928         int nfra = BKE_sequencer_find_next_prev_edit(scene, cfra, side, do_skip_mute, do_center, false);
2929         
2930         if (nfra != cfra) {
2931                 CFRA = nfra;
2932                 changed = true;
2933         }
2934
2935         return changed;
2936 }
2937
2938 static int sequencer_strip_jump_poll(bContext *C)
2939 {
2940         /* prevent changes during render */
2941         if (G.is_rendering)
2942                 return 0;
2943
2944         return sequencer_edit_poll(C);
2945 }
2946
2947 /* jump frame to edit point operator */
2948 static int sequencer_strip_jump_exec(bContext *C, wmOperator *op)
2949 {
2950         Scene *scene = CTX_data_scene(C);
2951         const bool next = RNA_boolean_get(op->ptr, "next");
2952         const bool center = RNA_boolean_get(op->ptr, "center");
2953
2954         /* currently do_skip_mute is always true */
2955         if (!strip_jump_internal(scene, next ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT, true, center)) {
2956                 return OPERATOR_CANCELLED;
2957         }
2958
2959         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2960         
2961         return OPERATOR_FINISHED;
2962 }
2963
2964 void SEQUENCER_OT_strip_jump(wmOperatorType *ot)
2965 {
2966         /* identifiers */
2967         ot->name = "Jump to Strip";
2968         ot->idname = "SEQUENCER_OT_strip_jump";
2969         ot->description = "Move frame to previous edit point";
2970
2971         /* api callbacks */
2972         ot->exec = sequencer_strip_jump_exec;
2973         ot->poll = sequencer_strip_jump_poll;
2974
2975         /* flags */
2976         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2977         
2978         /* properties */
2979         RNA_def_boolean(ot->srna, "next", true, "Next Strip", "");
2980         RNA_def_boolean(ot->srna, "center", true, "Use strip center", "");
2981 }
2982
2983 static void swap_sequence(Scene *scene, Sequence *seqa, Sequence *seqb)
2984 {
2985         int gap = seqb->startdisp - seqa->enddisp;
2986         int seq_a_start;
2987         int seq_b_start;
2988
2989         seq_b_start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2990         BKE_sequence_translate(scene, seqb, seq_b_start - seqb->start);
2991         BKE_sequence_calc(scene, seqb);
2992
2993         seq_a_start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2994         BKE_sequence_translate(scene, seqa, seq_a_start - seqa->start);
2995         BKE_sequence_calc(scene, seqa);
2996 }
2997
2998 #if 0
2999 static Sequence *sequence_find_parent(Scene *scene, Sequence *child)
3000 {
3001         Editing *ed = BKE_sequencer_editing_get(scene, false);
3002         Sequence *parent = NULL;
3003         Sequence *seq;
3004
3005         if (ed == NULL) return NULL;
3006
3007         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
3008                 if ((seq != child) && seq_is_parent(seq, child)) {
3009                         parent = seq;
3010                         break;
3011                 }
3012         }
3013
3014         return parent;
3015 }
3016 #endif
3017
3018 static int sequencer_swap_exec(bContext *C, wmOperator *op)
3019 {
3020         Scene *scene = CTX_data_scene(C);
3021         Editing *ed = BKE_sequencer_editing_get(scene, false);
3022         Sequence *active_seq = BKE_sequencer_active_get(scene);
3023         Sequence *seq, *iseq;
3024         int side = RNA_enum_get(op->ptr, "side");
3025
3026         if (active_seq == NULL) return OPERATOR_CANCELLED;
3027
3028         seq = find_next_prev_sequence(scene, active_seq, side, -1);
3029         
3030         if (seq) {
3031                 
3032                 /* disallow effect strips */
3033                 if (BKE_sequence_effect_get_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
3034                         return OPERATOR_CANCELLED;
3035                 if ((BKE_sequence_effect_get_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
3036                         return OPERATOR_CANCELLED;
3037
3038                 switch (side) {
3039                         case SEQ_SIDE_LEFT: 
3040                                 swap_sequence(scene, seq, active_seq);
3041                                 break;
3042                         case SEQ_SIDE_RIGHT: 
3043                                 swap_sequence(scene, active_seq, seq);
3044                                 break;
3045                 }
3046
3047                 // XXX - should be a generic function
3048                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
3049                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
3050                                 BKE_sequence_calc(scene, iseq);
3051                         }
3052                 }
3053
3054                 /* do this in a new loop since both effects need to be calculated first */
3055                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
3056                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
3057                                 /* this may now overlap */
3058                                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq) ) {
3059                                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
3060                                 }
3061                         }
3062                 }
3063
3064
3065
3066                 BKE_sequencer_sort(scene);
3067
3068                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3069
3070                 return OPERATOR_FINISHED;
3071         }
3072
3073         return OPERATOR_CANCELLED;
3074 }
3075
3076 void SEQUENCER_OT_swap(wmOperatorType *ot)
3077 {
3078         /* identifiers */
3079         ot->name = "Swap Strip";
3080         ot->idname = "SEQUENCER_OT_swap";
3081         ot->description = "Swap active strip with strip to the right or left";
3082         
3083         /* api callbacks */
3084         ot->exec = sequencer_swap_exec;
3085         ot->poll = sequencer_edit_poll;
3086         
3087         /* flags */
3088         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3089         
3090         /* properties */
3091         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
3092 }
3093
3094 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
3095 {
3096         int retval = OPERATOR_CANCELLED;
3097         Scene *scene = CTX_data_scene(C);
3098         Sequence *active_seq = BKE_sequencer_active_get(scene);
3099         StripElem *se = NULL;
3100
3101         if (active_seq == NULL)
3102                 return OPERATOR_CANCELLED;
3103
3104
3105         if (active_seq->strip) {
3106                 switch (active_seq->type) {
3107                         case SEQ_TYPE_IMAGE:
3108                                 se = BKE_sequencer_give_stripelem(active_seq, scene->r.cfra);
3109                                 break;
3110                         case SEQ_TYPE_MOVIE:
3111                                 se = active_seq->strip->stripdata;
3112                                 break;
3113                         case SEQ_TYPE_SCENE:
3114                         case SEQ_TYPE_META:
3115                         case SEQ_TYPE_SOUND_RAM:
3116                         case SEQ_TYPE_SOUND_HD:
3117                         default:
3118                                 break;
3119                 }
3120         }
3121
3122         if (se) {
3123                 // prevent setting the render size if sequence values aren't initialized
3124                 if ((se->orig_width > 0) && (se->orig_height > 0)) {
3125                         scene->r.xsch = se->orig_width;
3126                         scene->r.ysch = se->orig_height;
3127                         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
3128                         retval = OPERATOR_FINISHED;
3129                 }
3130         }
3131
3132         return retval;
3133 }
3134
3135 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
3136 {
3137         /* identifiers */
3138         ot->name = "Set Render Size";
3139         ot->idname = "SEQUENCER_OT_rendersize";
3140         ot->description = "Set render size and aspect from active sequence";
3141         
3142         /* api callbacks */
3143         ot->exec = sequencer_rendersize_exec;
3144         ot->poll = sequencer_edit_poll;
3145         
3146         /* flags */
3147         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3148         
3149         /* properties */
3150 }
3151
3152 static void seq_copy_del_sound(Scene *scene, Sequence *seq)
3153 {
3154         if (seq->type == SEQ_TYPE_META) {
3155                 Sequence *iseq;
3156                 for (iseq = seq->seqbase.first; iseq; iseq = iseq->next) {
3157                         seq_copy_del_sound(scene, iseq);
3158                 }
3159         }
3160         else if (seq->scene_sound) {
3161                 BKE_sound_remove_scene_sound(scene, seq->scene_sound);
3162                 seq->scene_sound = NULL;
3163         }
3164 }
3165
3166 static int sequencer_copy_exec(bContext *C, wmOperator *op)
3167 {