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