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