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