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