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