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