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