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