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