Fix T43612: VSE crash clearing proxies
[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                 case RETKEY:
1602                 case SPACEKEY:
1603                 {
1604                         ED_region_draw_cb_exit(ar->type, data->draw_handle);
1605                         MEM_freeN(data->seq_array);
1606                         MEM_freeN(data->trim);
1607                         MEM_freeN(data->ts);
1608                         MEM_freeN(data);
1609                         op->customdata = NULL;
1610                         if (sa) {
1611                                 ED_area_headerprint(sa, NULL);
1612                         }
1613                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1614                         return OPERATOR_FINISHED;
1615                 }
1616
1617                 case ESCKEY:
1618                 case RIGHTMOUSE:
1619                 {
1620                         int i;
1621                         Editing *ed = BKE_sequencer_editing_get(scene, false);
1622
1623                         for (i = 0; i < data->num_seq; i++) {
1624                                 transseq_restore(data->ts + i, data->seq_array[i]);
1625                         }
1626
1627                         for (i = 0; i < data->num_seq; i++) {
1628                                 Sequence *seq = data->seq_array[i];
1629                                 BKE_sequence_reload_new_file(scene, seq, false);
1630                                 BKE_sequence_calc(scene, seq);
1631                         }
1632
1633                         ED_region_draw_cb_exit(ar->type, data->draw_handle);
1634
1635                         MEM_freeN(data->seq_array);
1636                         MEM_freeN(data->ts);
1637                         MEM_freeN(data->trim);
1638                         MEM_freeN(data);
1639                         op->customdata = NULL;
1640
1641                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1642
1643                         BKE_sequencer_free_imbuf(scene, &ed->seqbase, false);
1644
1645                         if (sa) {
1646                                 ED_area_headerprint(sa, NULL);
1647                         }
1648
1649                         return OPERATOR_CANCELLED;
1650                 }
1651
1652                 case RIGHTSHIFTKEY:
1653                 case LEFTSHIFTKEY:
1654                         if (!has_numInput) {
1655                                 if (event->val == KM_PRESS) {
1656                                         data->slow = true;
1657                                         data->slow_offset = event->mval[0];
1658                                 }
1659                                 else if (event->val == KM_RELEASE) {
1660                                         data->slow = false;
1661                                 }
1662                         }
1663                         break;
1664
1665                 default:
1666                         handled = false;
1667                         break;
1668         }
1669
1670         /* Modal numinput inactive, try to handle numeric inputs last... */
1671         if (!handled && event->val == KM_PRESS && handleNumInput(C, &data->num_input, event)) {
1672                 float offset;
1673                 applyNumInput(&data->num_input, &offset);
1674
1675                 sequencer_slip_update_header(scene, sa, data, (int)offset);
1676
1677                 RNA_int_set(op->ptr, "offset", offset);
1678
1679                 if (sequencer_slip_recursively(scene, data, offset)) {
1680                         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1681                 }
1682         }
1683
1684         return OPERATOR_RUNNING_MODAL;
1685 }
1686
1687 void SEQUENCER_OT_slip(struct wmOperatorType *ot)
1688 {
1689         /* identifiers */
1690         ot->name = "Trim Strips";
1691         ot->idname = "SEQUENCER_OT_slip";
1692         ot->description = "Trim the contents of the active strip";
1693
1694         /* api callbacks */
1695         ot->invoke = sequencer_slip_invoke;
1696         ot->modal = sequencer_slip_modal;
1697         ot->exec = sequencer_slip_exec;
1698         ot->poll = sequencer_edit_poll;
1699
1700         /* flags */
1701         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1702
1703         RNA_def_int(ot->srna, "offset", 0, INT32_MIN, INT32_MAX, "Offset", "Offset to the data of the strip",
1704                     INT32_MIN, INT32_MAX);
1705 }
1706
1707 /* mute operator */
1708 static int sequencer_mute_exec(bContext *C, wmOperator *op)
1709 {
1710         Scene *scene = CTX_data_scene(C);
1711         Editing *ed = BKE_sequencer_editing_get(scene, false);
1712         Sequence *seq;
1713         bool selected;
1714
1715         selected = !RNA_boolean_get(op->ptr, "unselected");
1716         
1717         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1718                 if ((seq->flag & SEQ_LOCK) == 0) {
1719                         if (selected) { /* mute unselected */
1720                                 if (seq->flag & SELECT) {
1721                                         seq->flag |= SEQ_MUTE;
1722                                         BKE_sequence_invalidate_dependent(scene, seq);
1723                                 }
1724                         }
1725                         else {
1726                                 if ((seq->flag & SELECT) == 0) {
1727                                         seq->flag |= SEQ_MUTE;
1728                                         BKE_sequence_invalidate_dependent(scene, seq);
1729                                 }
1730                         }
1731                 }
1732         }
1733         
1734         BKE_sequencer_update_muting(ed);
1735         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1736         
1737         return OPERATOR_FINISHED;
1738 }
1739
1740 void SEQUENCER_OT_mute(struct wmOperatorType *ot)
1741 {
1742         /* identifiers */
1743         ot->name = "Mute Strips";
1744         ot->idname = "SEQUENCER_OT_mute";
1745         ot->description = "Mute selected strips";
1746         
1747         /* api callbacks */
1748         ot->exec = sequencer_mute_exec;
1749         ot->poll = sequencer_edit_poll;
1750         
1751         /* flags */
1752         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1753         
1754         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Mute unselected rather than selected strips");
1755 }
1756
1757
1758 /* unmute operator */
1759 static int sequencer_unmute_exec(bContext *C, wmOperator *op)
1760 {
1761         Scene *scene = CTX_data_scene(C);
1762         Editing *ed = BKE_sequencer_editing_get(scene, false);
1763         Sequence *seq;
1764         bool selected;
1765
1766         selected = !RNA_boolean_get(op->ptr, "unselected");
1767         
1768         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1769                 if ((seq->flag & SEQ_LOCK) == 0) {
1770                         if (selected) { /* unmute unselected */
1771                                 if (seq->flag & SELECT) {
1772                                         seq->flag &= ~SEQ_MUTE;
1773                                         BKE_sequence_invalidate_dependent(scene, seq);
1774                                 }
1775                         }
1776                         else {
1777                                 if ((seq->flag & SELECT) == 0) {
1778                                         seq->flag &= ~SEQ_MUTE;
1779                                         BKE_sequence_invalidate_dependent(scene, seq);
1780                                 }
1781                         }
1782                 }
1783         }
1784         
1785         BKE_sequencer_update_muting(ed);
1786         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1787         
1788         return OPERATOR_FINISHED;
1789 }
1790
1791 void SEQUENCER_OT_unmute(struct wmOperatorType *ot)
1792 {
1793         /* identifiers */
1794         ot->name = "Un-Mute Strips";
1795         ot->idname = "SEQUENCER_OT_unmute";
1796         ot->description = "Un-Mute unselected rather than selected strips";
1797         
1798         /* api callbacks */
1799         ot->exec = sequencer_unmute_exec;
1800         ot->poll = sequencer_edit_poll;
1801         
1802         /* flags */
1803         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1804         
1805         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "UnMute unselected rather than selected strips");
1806 }
1807
1808
1809 /* lock operator */
1810 static int sequencer_lock_exec(bContext *C, wmOperator *UNUSED(op))
1811 {
1812         Scene *scene = CTX_data_scene(C);
1813         Editing *ed = BKE_sequencer_editing_get(scene, false);
1814         Sequence *seq;
1815
1816         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1817                 if (seq->flag & SELECT) {
1818                         seq->flag |= SEQ_LOCK;
1819                 }
1820         }
1821
1822         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1823
1824         return OPERATOR_FINISHED;
1825 }
1826
1827 void SEQUENCER_OT_lock(struct wmOperatorType *ot)
1828 {
1829         /* identifiers */
1830         ot->name = "Lock Strips";
1831         ot->idname = "SEQUENCER_OT_lock";
1832         ot->description = "Lock the active strip so that it can't be transformed";
1833         
1834         /* api callbacks */
1835         ot->exec = sequencer_lock_exec;
1836         ot->poll = sequencer_edit_poll;
1837         
1838         /* flags */
1839         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1840 }
1841
1842 /* unlock operator */
1843 static int sequencer_unlock_exec(bContext *C, wmOperator *UNUSED(op))
1844 {
1845         Scene *scene = CTX_data_scene(C);
1846         Editing *ed = BKE_sequencer_editing_get(scene, false);
1847         Sequence *seq;
1848
1849         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1850                 if (seq->flag & SELECT) {
1851                         seq->flag &= ~SEQ_LOCK;
1852                 }
1853         }
1854
1855         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1856
1857         return OPERATOR_FINISHED;
1858 }
1859
1860 void SEQUENCER_OT_unlock(struct wmOperatorType *ot)
1861 {
1862         /* identifiers */
1863         ot->name = "UnLock Strips";
1864         ot->idname = "SEQUENCER_OT_unlock";
1865         ot->description = "Unlock the active strip so that it can't be transformed";
1866         
1867         /* api callbacks */
1868         ot->exec = sequencer_unlock_exec;
1869         ot->poll = sequencer_edit_poll;
1870         
1871         /* flags */
1872         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1873 }
1874
1875 /* reload operator */
1876 static int sequencer_reload_exec(bContext *C, wmOperator *op)
1877 {
1878         Scene *scene = CTX_data_scene(C);
1879         Editing *ed = BKE_sequencer_editing_get(scene, false);
1880         Sequence *seq;
1881         const bool adjust_length = RNA_boolean_get(op->ptr, "adjust_length");
1882
1883         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1884                 if (seq->flag & SELECT) {
1885                         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
1886                         BKE_sequence_reload_new_file(scene, seq, !adjust_length);
1887
1888                         if (adjust_length) {
1889                                 if (BKE_sequence_test_overlap(ed->seqbasep, seq))
1890                                         BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1891                         }
1892                 }
1893         }
1894
1895         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1896
1897         return OPERATOR_FINISHED;
1898 }
1899
1900 void SEQUENCER_OT_reload(struct wmOperatorType *ot)
1901 {
1902         PropertyRNA *prop;
1903
1904         /* identifiers */
1905         ot->name = "Reload Strips";
1906         ot->idname = "SEQUENCER_OT_reload";
1907         ot->description = "Reload strips in the sequencer";
1908         
1909         /* api callbacks */
1910         ot->exec = sequencer_reload_exec;
1911         ot->poll = sequencer_edit_poll;
1912         
1913         /* flags */
1914         ot->flag = OPTYPE_REGISTER; /* no undo, the data changed is stored outside 'main' */
1915
1916         prop = RNA_def_boolean(ot->srna, "adjust_length", 0, "Adjust Length",
1917                                "Adjust length of strips to their data length");
1918         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1919 }
1920
1921 /* reload operator */
1922 static int sequencer_refresh_all_exec(bContext *C, wmOperator *UNUSED(op))
1923 {
1924         Scene *scene = CTX_data_scene(C);
1925         Editing *ed = BKE_sequencer_editing_get(scene, false);
1926
1927         BKE_sequencer_free_imbuf(scene, &ed->seqbase, false);
1928
1929         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1930
1931         return OPERATOR_FINISHED;
1932 }
1933
1934 void SEQUENCER_OT_refresh_all(struct wmOperatorType *ot)
1935 {
1936         /* identifiers */
1937         ot->name = "Refresh Sequencer";
1938         ot->idname = "SEQUENCER_OT_refresh_all";
1939         ot->description = "Refresh the sequencer editor";
1940         
1941         /* api callbacks */
1942         ot->exec = sequencer_refresh_all_exec;
1943         ot->poll = sequencer_edit_poll;
1944 }
1945
1946 static int sequencer_reassign_inputs_exec(bContext *C, wmOperator *op)
1947 {
1948         Scene *scene = CTX_data_scene(C);
1949         Sequence *seq1, *seq2, *seq3, *last_seq = BKE_sequencer_active_get(scene);
1950         const char *error_msg;
1951
1952         if (!seq_effect_find_selected(scene, last_seq, last_seq->type, &seq1, &seq2, &seq3, &error_msg)) {
1953                 BKE_report(op->reports, RPT_ERROR, error_msg);
1954                 return OPERATOR_CANCELLED;
1955         }
1956         /* see reassigning would create a cycle */
1957         if (seq_is_predecessor(seq1, last_seq) ||
1958             seq_is_predecessor(seq2, last_seq) ||
1959             seq_is_predecessor(seq3, last_seq))
1960         {
1961                 BKE_report(op->reports, RPT_ERROR, "Cannot reassign inputs: no cycles allowed");
1962                 return OPERATOR_CANCELLED;
1963         }
1964
1965         last_seq->seq1 = seq1;
1966         last_seq->seq2 = seq2;
1967         last_seq->seq3 = seq3;
1968
1969         BKE_sequencer_update_changed_seq_and_deps(scene, last_seq, 1, 1);
1970
1971         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1972
1973         return OPERATOR_FINISHED;
1974 }
1975
1976 static int sequencer_effect_poll(bContext *C)
1977 {
1978         Scene *scene = CTX_data_scene(C);
1979         Editing *ed = BKE_sequencer_editing_get(scene, false);
1980
1981         if (ed) {
1982                 Sequence *last_seq = BKE_sequencer_active_get(scene);
1983                 if (last_seq && (last_seq->type & SEQ_TYPE_EFFECT)) {
1984                         return 1;
1985                 }
1986         }
1987
1988         return 0;
1989 }
1990
1991 void SEQUENCER_OT_reassign_inputs(struct wmOperatorType *ot)
1992 {
1993         /* identifiers */
1994         ot->name = "Reassign Inputs";
1995         ot->idname = "SEQUENCER_OT_reassign_inputs";
1996         ot->description = "Reassign the inputs for the effect strip";
1997
1998         /* api callbacks */
1999         ot->exec = sequencer_reassign_inputs_exec;
2000         ot->poll = sequencer_effect_poll;
2001
2002         /* flags */
2003         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2004 }
2005
2006
2007 static int sequencer_swap_inputs_exec(bContext *C, wmOperator *op)
2008 {
2009         Scene *scene = CTX_data_scene(C);
2010         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene);
2011
2012         if (last_seq->seq1 == NULL || last_seq->seq2 == NULL) {
2013                 BKE_report(op->reports, RPT_ERROR, "No valid inputs to swap");
2014                 return OPERATOR_CANCELLED;
2015         }
2016
2017         seq = last_seq->seq1;
2018         last_seq->seq1 = last_seq->seq2;
2019         last_seq->seq2 = seq;
2020
2021         BKE_sequencer_update_changed_seq_and_deps(scene, last_seq, 1, 1);
2022
2023         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2024
2025         return OPERATOR_FINISHED;
2026 }
2027 void SEQUENCER_OT_swap_inputs(struct wmOperatorType *ot)
2028 {
2029         /* identifiers */
2030         ot->name = "Swap Inputs";
2031         ot->idname = "SEQUENCER_OT_swap_inputs";
2032         ot->description = "Swap the first two inputs for the effect strip";
2033
2034         /* api callbacks */
2035         ot->exec = sequencer_swap_inputs_exec;
2036         ot->poll = sequencer_effect_poll;
2037
2038         /* flags */
2039         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2040 }
2041
2042
2043 /* cut operator */
2044 static EnumPropertyItem prop_cut_types[] = {
2045         {SEQ_CUT_SOFT, "SOFT", 0, "Soft", ""},
2046         {SEQ_CUT_HARD, "HARD", 0, "Hard", ""},
2047         {0, NULL, 0, NULL, NULL}
2048 };
2049
2050 static int sequencer_cut_exec(bContext *C, wmOperator *op)
2051 {
2052         Scene *scene = CTX_data_scene(C);
2053         Editing *ed = BKE_sequencer_editing_get(scene, false);
2054         int cut_side, cut_hard, cut_frame;
2055
2056         bool changed;
2057
2058         cut_frame = RNA_int_get(op->ptr, "frame");
2059         cut_hard = RNA_enum_get(op->ptr, "type");
2060         cut_side = RNA_enum_get(op->ptr, "side");
2061         
2062         if (cut_hard == SEQ_CUT_HARD) {
2063                 changed = cut_seq_list(scene, ed->seqbasep, cut_frame, cut_seq_hard);
2064         }
2065         else {
2066                 changed = cut_seq_list(scene, ed->seqbasep, cut_frame, cut_seq_soft);
2067         }
2068         
2069         if (changed) { /* got new strips ? */
2070                 Sequence *seq;
2071
2072                 if (cut_side != SEQ_SIDE_BOTH) {
2073                         SEQP_BEGIN (ed, seq)
2074                         {
2075                                 if (cut_side == SEQ_SIDE_LEFT) {
2076                                         if (seq->startdisp >= cut_frame) {
2077                                                 seq->flag &= ~SEQ_ALLSEL;
2078                                         }
2079                                 }
2080                                 else {
2081                                         if (seq->enddisp <= cut_frame) {
2082                                                 seq->flag &= ~SEQ_ALLSEL;
2083                                         }
2084                                 }
2085                         }
2086                         SEQ_END;
2087                 }
2088
2089                 SEQP_BEGIN (ed, seq)
2090                 {
2091                         if (seq->seq1 || seq->seq2 || seq->seq3) {
2092                                 BKE_sequence_calc(scene, seq);
2093                         }
2094                 }
2095                 SEQ_END;
2096
2097                 /* as last: */
2098                 BKE_sequencer_sort(scene);
2099         }
2100
2101         if (changed) {
2102                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2103                 return OPERATOR_FINISHED;
2104         }
2105         else {
2106                 return OPERATOR_CANCELLED;
2107         }
2108 }
2109
2110
2111 static int sequencer_cut_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2112 {
2113         Scene *scene = CTX_data_scene(C);
2114         View2D *v2d = UI_view2d_fromcontext(C);
2115
2116         int cut_side = SEQ_SIDE_BOTH;
2117         int cut_frame = CFRA;
2118
2119         if (ED_operator_sequencer_active(C) && v2d)
2120                 cut_side = mouse_frame_side(v2d, event->mval[0], cut_frame);
2121         
2122         RNA_int_set(op->ptr, "frame", cut_frame);
2123         RNA_enum_set(op->ptr, "side", cut_side);
2124         /*RNA_enum_set(op->ptr, "type", cut_hard); */ /*This type is set from the key shortcut */
2125
2126         return sequencer_cut_exec(C, op);
2127 }
2128
2129
2130 void SEQUENCER_OT_cut(struct wmOperatorType *ot)
2131 {
2132         /* identifiers */
2133         ot->name = "Cut Strips";
2134         ot->idname = "SEQUENCER_OT_cut";
2135         ot->description = "Cut the selected strips";
2136         
2137         /* api callbacks */
2138         ot->invoke = sequencer_cut_invoke;
2139         ot->exec = sequencer_cut_exec;
2140         ot->poll = sequencer_edit_poll;
2141         
2142         /* flags */
2143         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2144         
2145         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be cut", INT_MIN, INT_MAX);
2146         RNA_def_enum(ot->srna, "type", prop_cut_types, SEQ_CUT_SOFT, "Type", "The type of cut operation to perform on strips");
2147         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side that remains selected after cutting");
2148 }
2149
2150 /* duplicate operator */
2151 static int apply_unique_name_cb(Sequence *seq, void *arg_pt)
2152 {
2153         Scene *scene = (Scene *)arg_pt;
2154         char name[sizeof(seq->name) - 2];
2155
2156         BLI_strncpy_utf8(name, seq->name + 2, sizeof(name));
2157         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
2158         BKE_sequencer_dupe_animdata(scene, name, seq->name + 2);
2159         return 1;
2160
2161 }
2162
2163 static int sequencer_add_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
2164 {
2165         Scene *scene = CTX_data_scene(C);
2166         Editing *ed = BKE_sequencer_editing_get(scene, false);
2167
2168         ListBase nseqbase = {NULL, NULL};
2169
2170         if (ed == NULL)
2171                 return OPERATOR_CANCELLED;
2172
2173         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT);
2174
2175         if (nseqbase.first) {
2176                 Sequence *seq = nseqbase.first;
2177                 /* rely on the nseqbase list being added at the end */
2178                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2179
2180                 for (; seq; seq = seq->next)
2181                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
2182
2183                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2184                 return OPERATOR_FINISHED;
2185         }
2186
2187         return OPERATOR_CANCELLED;
2188 }
2189
2190 void SEQUENCER_OT_duplicate(wmOperatorType *ot)
2191 {
2192         /* identifiers */
2193         ot->name = "Duplicate Strips";
2194         ot->idname = "SEQUENCER_OT_duplicate";
2195         ot->description = "Duplicate the selected strips";
2196         
2197         /* api callbacks */
2198         ot->exec = sequencer_add_duplicate_exec;
2199         ot->poll = ED_operator_sequencer_active;
2200         
2201         /* flags */
2202         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2203         
2204         /* to give to transform */
2205         RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
2206 }
2207
2208 /* delete operator */
2209 static int sequencer_delete_exec(bContext *C, wmOperator *UNUSED(op))
2210 {
2211         Scene *scene = CTX_data_scene(C);
2212         Editing *ed = BKE_sequencer_editing_get(scene, false);
2213         Sequence *seq;
2214         MetaStack *ms;
2215         bool nothingSelected = true;
2216
2217         seq = BKE_sequencer_active_get(scene);
2218         if (seq && seq->flag & SELECT) { /* avoid a loop since this is likely to be selected */
2219                 nothingSelected = false;
2220         }
2221         else {
2222                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2223                         if (seq->flag & SELECT) {
2224                                 nothingSelected = false;
2225                                 break;
2226                         }
2227                 }
2228         }
2229
2230         if (nothingSelected)
2231                 return OPERATOR_FINISHED;
2232
2233         /* for effects and modifiers, try to find a replacement input */
2234         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2235                 if (!(seq->flag & SELECT)) {
2236                         if ((seq->type & SEQ_TYPE_EFFECT)) {
2237                                 del_seq_find_replace_recurs(scene, seq);
2238                         }
2239                 }
2240                 else {
2241                         del_seq_clear_modifiers_recurs(scene, seq);
2242                 }
2243         }
2244
2245         /* delete all selected strips */
2246         recurs_del_seq_flag(scene, ed->seqbasep, SELECT, 0);
2247
2248         /* updates lengths etc */
2249         seq = ed->seqbasep->first;
2250         while (seq) {
2251                 BKE_sequence_calc(scene, seq);
2252                 seq = seq->next;
2253         }
2254
2255         /* free parent metas */
2256         ms = ed->metastack.last;
2257         while (ms) {
2258                 BKE_sequence_calc(scene, ms->parseq);
2259                 ms = ms->prev;
2260         }
2261
2262         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2263         
2264         return OPERATOR_FINISHED;
2265 }
2266
2267 static int sequencer_delete_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2268 {
2269         ARegion *ar = CTX_wm_region(C);
2270
2271         if (ar->regiontype == RGN_TYPE_WINDOW) {
2272                 /* bounding box of 30 pixels is used for markers shortcuts,
2273                  * prevent conflict with markers shortcuts here
2274                  */
2275                 if (event->mval[1] <= 30)
2276                         return OPERATOR_PASS_THROUGH;
2277         }
2278
2279         return WM_operator_confirm(C, op, event);
2280 }
2281
2282 void SEQUENCER_OT_delete(wmOperatorType *ot)
2283 {
2284
2285         /* identifiers */
2286         ot->name = "Erase Strips";
2287         ot->idname = "SEQUENCER_OT_delete";
2288         ot->description = "Erase selected strips from the sequencer";
2289         
2290         /* api callbacks */
2291         ot->invoke = sequencer_delete_invoke;
2292         ot->exec = sequencer_delete_exec;
2293         ot->poll = sequencer_edit_poll;
2294         
2295         /* flags */
2296         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2297 }
2298
2299
2300 /* offset clear operator */
2301 static int sequencer_offset_clear_exec(bContext *C, wmOperator *UNUSED(op))
2302 {
2303         Scene *scene = CTX_data_scene(C);
2304         Editing *ed = BKE_sequencer_editing_get(scene, false);
2305         Sequence *seq;
2306
2307         /* for effects, try to find a replacement input */
2308         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2309                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
2310                         seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
2311                 }
2312         }
2313
2314         /* updates lengths etc */
2315         seq = ed->seqbasep->first;
2316         while (seq) {
2317                 BKE_sequence_calc(scene, seq);
2318                 seq = seq->next;
2319         }
2320
2321         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2322                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
2323                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2324                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2325                         }
2326                 }
2327         }
2328
2329         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2330
2331         return OPERATOR_FINISHED;
2332 }
2333
2334
2335 void SEQUENCER_OT_offset_clear(wmOperatorType *ot)
2336 {
2337
2338         /* identifiers */
2339         ot->name = "Clear Strip Offset";
2340         ot->idname = "SEQUENCER_OT_offset_clear";
2341         ot->description = "Clear strip offsets from the start and end frames";
2342
2343         /* api callbacks */
2344         ot->exec = sequencer_offset_clear_exec;
2345         ot->poll = sequencer_edit_poll;
2346
2347         /* flags */
2348         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2349 }
2350
2351
2352 /* separate_images operator */
2353 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
2354 {
2355         Scene *scene = CTX_data_scene(C);
2356         Editing *ed = BKE_sequencer_editing_get(scene, false);
2357         
2358         Sequence *seq, *seq_new;
2359         Strip *strip_new;
2360         StripElem *se, *se_new;
2361         int start_ofs, cfra, frame_end;
2362         int step = RNA_int_get(op->ptr, "length");
2363
2364         seq = ed->seqbasep->first; /* poll checks this is valid */
2365
2366         while (seq) {
2367                 if ((seq->flag & SELECT) && (seq->type == SEQ_TYPE_IMAGE) && (seq->len > 1)) {
2368                         Sequence *seq_next;
2369
2370                         /* remove seq so overlap tests don't conflict,
2371                          * see seq_free_sequence below for the real free'ing */
2372                         BLI_remlink(ed->seqbasep, seq);
2373                         /* if (seq->ipo) seq->ipo->id.us--; */
2374                         /* XXX, remove fcurve and assign to split image strips */
2375
2376                         start_ofs = cfra = BKE_sequence_tx_get_final_left(seq, false);
2377                         frame_end = BKE_sequence_tx_get_final_right(seq, false);
2378
2379                         while (cfra < frame_end) {
2380                                 /* new seq */
2381                                 se = BKE_sequencer_give_stripelem(seq, cfra);
2382
2383                                 seq_new = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
2384                                 BLI_addtail(ed->seqbasep, seq_new);
2385
2386                                 seq_new->start = start_ofs;
2387                                 seq_new->type = SEQ_TYPE_IMAGE;
2388                                 seq_new->len = 1;
2389                                 seq_new->endstill = step - 1;
2390
2391                                 /* new strip */
2392                                 strip_new = seq_new->strip;
2393                                 strip_new->us = 1;
2394
2395                                 /* new stripdata (only one element now!) */
2396                                 /* Note this assume all elements (images) have the same dimension, since we only copy the name here. */
2397                                 se_new = MEM_reallocN(strip_new->stripdata, sizeof(*se_new));
2398                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
2399                                 strip_new->stripdata = se_new;
2400
2401                                 BKE_sequence_calc(scene, seq_new);
2402
2403                                 if (step > 1) {
2404                                         seq_new->flag &= ~SEQ_OVERLAP;
2405                                         if (BKE_sequence_test_overlap(ed->seqbasep, seq_new)) {
2406                                                 BKE_sequence_base_shuffle(ed->seqbasep, seq_new, scene);
2407                                         }
2408                                 }
2409
2410                                 /* XXX, COPY FCURVES */
2411
2412                                 cfra++;
2413                                 start_ofs += step;
2414                         }
2415
2416                         seq_next = seq->next;
2417                         BKE_sequence_free(scene, seq);
2418                         seq = seq_next;
2419                 }
2420                 else {
2421                         seq = seq->next;
2422                 }
2423         }
2424
2425         /* as last: */
2426         BKE_sequencer_sort(scene);
2427         
2428         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2429
2430         return OPERATOR_FINISHED;
2431 }
2432
2433
2434 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
2435 {
2436         /* identifiers */
2437         ot->name = "Separate Images";
2438         ot->idname = "SEQUENCER_OT_images_separate";
2439         ot->description = "On image sequence strips, it returns a strip for each image";
2440         
2441         /* api callbacks */
2442         ot->exec = sequencer_separate_images_exec;
2443         ot->invoke = WM_operator_props_popup;
2444         ot->poll = sequencer_edit_poll;
2445         
2446         /* flags */
2447         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2448
2449         RNA_def_int(ot->srna, "length", 1, 1, INT_MAX, "Length", "Length of each frame", 1, 1000);
2450 }
2451
2452
2453 /* META Operators */
2454
2455 /* separate_meta_toggle operator */
2456 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2457 {
2458         Scene *scene = CTX_data_scene(C);
2459         Editing *ed = BKE_sequencer_editing_get(scene, false);
2460         Sequence *last_seq = BKE_sequencer_active_get(scene);
2461         MetaStack *ms;
2462
2463         if (last_seq && last_seq->type == SEQ_TYPE_META && last_seq->flag & SELECT) {
2464                 /* Enter Metastrip */
2465                 ms = MEM_mallocN(sizeof(MetaStack), "metastack");
2466                 BLI_addtail(&ed->metastack, ms);
2467                 ms->parseq = last_seq;
2468                 ms->oldbasep = ed->seqbasep;
2469                 copy_v2_v2_int(ms->disp_range, &ms->parseq->startdisp);
2470
2471                 ed->seqbasep = &last_seq->seqbase;
2472
2473                 BKE_sequencer_active_set(scene, NULL);
2474
2475         }
2476         else {
2477                 /* Exit Metastrip (if possible) */
2478
2479                 Sequence *seq;
2480
2481                 if (BLI_listbase_is_empty(&ed->metastack))
2482                         return OPERATOR_CANCELLED;
2483
2484                 ms = ed->metastack.last;
2485                 BLI_remlink(&ed->metastack, ms);
2486
2487                 ed->seqbasep = ms->oldbasep;
2488
2489                 /* for old files, update from meta */
2490                 if (ms->disp_range[0] ==  ms->disp_range[1]) {
2491                         copy_v2_v2_int(ms->disp_range, &ms->parseq->startdisp);
2492                 }
2493
2494                 /* recalc all: the meta can have effects connected to it */
2495                 for (seq = ed->seqbasep->first; seq; seq = seq->next)
2496                         BKE_sequence_calc(scene, seq);
2497
2498                 /* 2.73+, keeping endpoings is important!
2499                  * moving them around means you can't usefully use metas in a complex edit */
2500 #if 1
2501                 BKE_sequence_tx_set_final_left(ms->parseq, ms->disp_range[0]);
2502                 BKE_sequence_tx_set_final_right(ms->parseq, ms->disp_range[1]);
2503                 BKE_sequence_calc(scene, ms->parseq);
2504 #else
2505                 if (BKE_sequence_test_overlap(ed->seqbasep, ms->parseq))
2506                         BKE_sequence_base_shuffle(ed->seqbasep, ms->parseq, scene);
2507 #endif
2508
2509                 BKE_sequencer_active_set(scene, ms->parseq);
2510
2511                 ms->parseq->flag |= SELECT;
2512                 recurs_sel_seq(ms->parseq);
2513
2514                 MEM_freeN(ms);
2515
2516         }
2517
2518         BKE_sequencer_update_muting(ed);
2519         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2520
2521         return OPERATOR_FINISHED;
2522 }
2523
2524 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
2525 {
2526         /* identifiers */
2527         ot->name = "Toggle Meta Strip";
2528         ot->idname = "SEQUENCER_OT_meta_toggle";
2529         ot->description = "Toggle a metastrip (to edit enclosed strips)";
2530         
2531         /* api callbacks */
2532         ot->exec = sequencer_meta_toggle_exec;
2533         ot->poll = sequencer_edit_poll;
2534         
2535         /* flags */
2536         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2537 }
2538
2539
2540 /* separate_meta_make operator */
2541 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
2542 {
2543         Scene *scene = CTX_data_scene(C);
2544         Editing *ed = BKE_sequencer_editing_get(scene, false);
2545         
2546         Sequence *seq, *seqm, *next, *last_seq = BKE_sequencer_active_get(scene);
2547         int channel_max = 1;
2548
2549         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == false) {
2550                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2551                 return OPERATOR_CANCELLED;
2552         }
2553
2554         /* remove all selected from main list, and put in meta */
2555
2556         seqm = BKE_sequence_alloc(ed->seqbasep, 1, 1); /* channel number set later */
2557         strcpy(seqm->name + 2, "MetaStrip");
2558         seqm->type = SEQ_TYPE_META;
2559         seqm->flag = SELECT;
2560
2561         seq = ed->seqbasep->first;
2562         while (seq) {
2563                 next = seq->next;
2564                 if (seq != seqm && (seq->flag & SELECT)) {
2565                         BKE_sequence_invalidate_cache(scene, seq);
2566                         channel_max = max_ii(seq->machine, channel_max);
2567                         BLI_remlink(ed->seqbasep, seq);
2568                         BLI_addtail(&seqm->seqbase, seq);
2569                 }
2570                 seq = next;
2571         }
2572         seqm->machine = last_seq ? last_seq->machine : channel_max;
2573         BKE_sequence_calc(scene, seqm);
2574
2575         seqm->strip = MEM_callocN(sizeof(Strip), "metastrip");
2576         seqm->strip->us = 1;
2577         
2578         BKE_sequencer_active_set(scene, seqm);
2579
2580         if (BKE_sequence_test_overlap(ed->seqbasep, seqm) ) BKE_sequence_base_shuffle(ed->seqbasep, seqm, scene);
2581
2582         BKE_sequencer_update_muting(ed);
2583
2584         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seqm);
2585
2586         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2587
2588         return OPERATOR_FINISHED;
2589 }
2590
2591 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
2592 {
2593         /* identifiers */
2594         ot->name = "Make Meta Strip";
2595         ot->idname = "SEQUENCER_OT_meta_make";
2596         ot->description = "Group selected strips into a metastrip";
2597         
2598         /* api callbacks */
2599         ot->exec = sequencer_meta_make_exec;
2600         ot->poll = sequencer_edit_poll;
2601         
2602         /* flags */
2603         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2604 }
2605
2606
2607 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
2608 {
2609         if (seq == seqm) return 1;
2610         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
2611         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
2612         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
2613         else return 0;
2614 }
2615
2616 /* separate_meta_make operator */
2617 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
2618 {
2619         Scene *scene = CTX_data_scene(C);
2620         Editing *ed = BKE_sequencer_editing_get(scene, false);
2621
2622         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene); /* last_seq checks (ed == NULL) */
2623
2624         if (last_seq == NULL || last_seq->type != SEQ_TYPE_META)
2625                 return OPERATOR_CANCELLED;
2626
2627         for (seq = last_seq->seqbase.first; seq != NULL; seq = seq->next) {
2628                 BKE_sequence_invalidate_cache(scene, seq);
2629         }
2630
2631         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
2632
2633         BLI_listbase_clear(&last_seq->seqbase);
2634
2635         BLI_remlink(ed->seqbasep, last_seq);
2636         BKE_sequence_free(scene, last_seq);
2637
2638         /* emtpy meta strip, delete all effects depending on it */
2639         for (seq = ed->seqbasep->first; seq; seq = seq->next)
2640                 if ((seq->type & SEQ_TYPE_EFFECT) && seq_depends_on_meta(seq, last_seq))
2641                         seq->flag |= SEQ_FLAG_DELETE;
2642
2643         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2644
2645         /* test for effects and overlap
2646          * don't use SEQP_BEGIN since that would be recursive */
2647         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2648                 if (seq->flag & SELECT) {
2649                         seq->flag &= ~SEQ_OVERLAP;
2650                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2651                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2652                         }
2653                 }
2654         }
2655
2656         BKE_sequencer_sort(scene);
2657         BKE_sequencer_update_muting(ed);
2658
2659         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2660
2661         return OPERATOR_FINISHED;
2662 }
2663
2664 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2665 {
2666         /* identifiers */
2667         ot->name = "UnMeta Strip";
2668         ot->idname = "SEQUENCER_OT_meta_separate";
2669         ot->description = "Put the contents of a metastrip back in the sequencer";
2670         
2671         /* api callbacks */
2672         ot->exec = sequencer_meta_separate_exec;
2673         ot->poll = sequencer_edit_poll;
2674         
2675         /* flags */
2676         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2677 }
2678
2679 /* view_all operator */
2680 static int sequencer_view_all_exec(bContext *C, wmOperator *op)
2681 {
2682         ARegion *ar = CTX_wm_region(C);
2683         View2D *v2d = UI_view2d_fromcontext(C);
2684         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2685
2686         UI_view2d_smooth_view(C, ar, &v2d->tot, smooth_viewtx);
2687         return OPERATOR_FINISHED;
2688 }
2689
2690 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2691 {
2692         /* identifiers */
2693         ot->name = "View All";
2694         ot->idname = "SEQUENCER_OT_view_all";
2695         ot->description = "View all the strips in the sequencer";
2696         
2697         /* api callbacks */
2698         ot->exec = sequencer_view_all_exec;
2699         ot->poll = ED_operator_sequencer_active;
2700         
2701         /* flags */
2702         ot->flag = OPTYPE_REGISTER;
2703 }
2704
2705 /* view_all operator */
2706 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2707 {
2708         bScreen *sc = CTX_wm_screen(C);
2709         ScrArea *area = CTX_wm_area(C);
2710 #if 0
2711         ARegion *ar = CTX_wm_region(C);
2712         SpaceSeq *sseq = area->spacedata.first;
2713         Scene *scene = CTX_data_scene(C);
2714 #endif
2715         View2D *v2d = UI_view2d_fromcontext(C);
2716
2717         v2d->cur = v2d->tot;
2718         UI_view2d_curRect_validate(v2d);
2719         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2720         
2721 #if 0
2722         /* Like zooming on an image view */
2723         float zoomX, zoomY;
2724         int width, height, imgwidth, imgheight;
2725
2726         width = ar->winx;
2727         height = ar->winy;
2728
2729         seq_reset_imageofs(sseq);
2730
2731         imgwidth = (scene->r.size * scene->r.xsch) / 100;
2732         imgheight = (scene->r.size * scene->r.ysch) / 100;
2733
2734         /* Apply aspect, dosnt need to be that accurate */
2735         imgwidth = (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2736
2737         if (((imgwidth >= width) || (imgheight >= height)) &&
2738             ((width > 0) && (height > 0)))
2739         {
2740                 /* Find the zoom value that will fit the image in the image space */
2741                 zoomX = ((float)width) / ((float)imgwidth);
2742                 zoomY = ((float)height) / ((float)imgheight);
2743                 sseq->zoom = (zoomX < zoomY) ? zoomX : zoomY;
2744
2745                 sseq->zoom = 1.0f / power_of_2(1 / min_ff(zoomX, zoomY));
2746         }
2747         else {
2748                 sseq->zoom = 1.0f;
2749         }
2750 #endif
2751
2752         ED_area_tag_redraw(CTX_wm_area(C));
2753         return OPERATOR_FINISHED;
2754 }
2755
2756 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2757 {
2758         /* identifiers */
2759         ot->name = "View All";
2760         ot->idname = "SEQUENCER_OT_view_all_preview";
2761         ot->description = "Zoom preview to fit in the area";
2762         
2763         /* api callbacks */
2764         ot->exec = sequencer_view_all_preview_exec;
2765         ot->poll = ED_operator_sequencer_active;
2766         
2767         /* flags */
2768         ot->flag = OPTYPE_REGISTER;
2769 }
2770
2771
2772 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2773 {
2774         RenderData *rd = &CTX_data_scene(C)->r;
2775         View2D *v2d = UI_view2d_fromcontext(C);
2776
2777         float ratio = RNA_float_get(op->ptr, "ratio");
2778
2779         float winx = (int)(rd->size * rd->xsch) / 100;
2780         float winy = (int)(rd->size * rd->ysch) / 100;
2781
2782         float facx = BLI_rcti_size_x(&v2d->mask) / winx;
2783         float facy = BLI_rcti_size_y(&v2d->mask) / winy;
2784
2785         BLI_rctf_resize(&v2d->cur, floorf(winx * facx / ratio + 0.5f), floorf(winy * facy / ratio + 0.5f));
2786
2787         ED_region_tag_redraw(CTX_wm_region(C));
2788
2789         return OPERATOR_FINISHED;
2790 }
2791
2792 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2793 {
2794         /* identifiers */
2795         ot->name = "Sequencer View Zoom Ratio";
2796         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2797         ot->description = "Change zoom ratio of sequencer preview";
2798
2799         /* api callbacks */
2800         ot->exec = sequencer_view_zoom_ratio_exec;
2801         ot->poll = ED_operator_sequencer_active;
2802
2803         /* properties */
2804         RNA_def_float(ot->srna, "ratio", 1.0f, -FLT_MAX, FLT_MAX,
2805                       "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2806 }
2807
2808
2809 #if 0
2810 static EnumPropertyItem view_type_items[] = {
2811         {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2812         {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2813         {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2814         {0, NULL, 0, NULL, NULL}
2815 };
2816 #endif
2817
2818 /* view_all operator */
2819 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2820 {
2821         SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
2822
2823         sseq->view++;
2824         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2825
2826         ED_area_tag_refresh(CTX_wm_area(C));
2827
2828         return OPERATOR_FINISHED;
2829 }
2830
2831 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2832 {
2833         /* identifiers */
2834         ot->name = "View Toggle";
2835         ot->idname = "SEQUENCER_OT_view_toggle";
2836         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2837         
2838         /* api callbacks */
2839         ot->exec = sequencer_view_toggle_exec;
2840         ot->poll = ED_operator_sequencer_active;
2841         
2842         /* flags */
2843         ot->flag = OPTYPE_REGISTER;
2844 }
2845
2846
2847 /* view_selected operator */
2848 static int sequencer_view_selected_exec(bContext *C, wmOperator *op)
2849 {
2850         Scene *scene = CTX_data_scene(C);
2851         View2D *v2d = UI_view2d_fromcontext(C);
2852         ARegion *ar = CTX_wm_region(C);
2853         Editing *ed = BKE_sequencer_editing_get(scene, false);
2854         Sequence *last_seq = BKE_sequencer_active_get(scene);
2855         Sequence *seq;
2856         rctf cur_new = v2d->cur;
2857
2858         int xmin =  MAXFRAME * 2;
2859         int xmax = -MAXFRAME * 2;
2860         int ymin =  MAXSEQ + 1;
2861         int ymax = 0;
2862         int orig_height;
2863         int ymid;
2864         int ymargin = 1;
2865         int xmargin = FPS;
2866
2867         if (ed == NULL)
2868                 return OPERATOR_CANCELLED;
2869
2870         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2871                 if ((seq->flag & SELECT) || (seq == last_seq)) {
2872                         xmin = min_ii(xmin, seq->startdisp);
2873                         xmax = max_ii(xmax, seq->enddisp);
2874
2875                         ymin = min_ii(ymin, seq->machine);
2876                         ymax = max_ii(ymax, seq->machine);
2877                 }
2878         }
2879
2880         if (ymax != 0) {
2881                 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2882                 
2883                 xmax += xmargin;
2884                 xmin -= xmargin;
2885                 ymax += ymargin;
2886                 ymin -= ymargin;
2887
2888                 orig_height = BLI_rctf_size_y(&cur_new);
2889
2890                 cur_new.xmin = xmin;
2891                 cur_new.xmax = xmax;
2892
2893                 cur_new.ymin = ymin;
2894                 cur_new.ymax = ymax;
2895
2896                 /* only zoom out vertically */
2897                 if (orig_height > BLI_rctf_size_y(&cur_new)) {
2898                         ymid = BLI_rctf_cent_y(&cur_new);
2899
2900                         cur_new.ymin = ymid - (orig_height / 2);
2901                         cur_new.ymax = ymid + (orig_height / 2);
2902                 }
2903
2904                 UI_view2d_smooth_view(C, ar, &cur_new, smooth_viewtx);
2905
2906                 return OPERATOR_FINISHED;
2907         }
2908         else {
2909                 return OPERATOR_CANCELLED;
2910         }
2911         
2912 }
2913
2914 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2915 {
2916         /* identifiers */
2917         ot->name = "View Selected";
2918         ot->idname = "SEQUENCER_OT_view_selected";
2919         ot->description = "Zoom the sequencer on the selected strips";
2920         
2921         /* api callbacks */
2922         ot->exec = sequencer_view_selected_exec;
2923         ot->poll = ED_operator_sequencer_active;
2924         
2925         /* flags */
2926         ot->flag = OPTYPE_REGISTER;
2927 }
2928
2929 static bool strip_jump_internal(Scene *scene,
2930                                 const short side,
2931                                 const bool do_skip_mute, const bool do_center)
2932 {
2933         bool changed = false;
2934         int cfra = CFRA;
2935         int nfra = BKE_sequencer_find_next_prev_edit(scene, cfra, side, do_skip_mute, do_center, false);
2936         
2937         if (nfra != cfra) {
2938                 CFRA = nfra;
2939                 changed = true;
2940         }
2941
2942         return changed;
2943 }
2944
2945 static int sequencer_strip_jump_poll(bContext *C)
2946 {
2947         /* prevent changes during render */
2948         if (G.is_rendering)
2949                 return 0;
2950
2951         return sequencer_edit_poll(C);
2952 }
2953
2954 /* jump frame to edit point operator */
2955 static int sequencer_strip_jump_exec(bContext *C, wmOperator *op)
2956 {
2957         Scene *scene = CTX_data_scene(C);
2958         const bool next = RNA_boolean_get(op->ptr, "next");
2959         const bool center = RNA_boolean_get(op->ptr, "center");
2960
2961         /* currently do_skip_mute is always true */
2962         if (!strip_jump_internal(scene, next ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT, true, center)) {
2963                 return OPERATOR_CANCELLED;
2964         }
2965
2966         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2967         
2968         return OPERATOR_FINISHED;
2969 }
2970
2971 void SEQUENCER_OT_strip_jump(wmOperatorType *ot)
2972 {
2973         /* identifiers */
2974         ot->name = "Jump to Strip";
2975         ot->idname = "SEQUENCER_OT_strip_jump";
2976         ot->description = "Move frame to previous edit point";
2977
2978         /* api callbacks */
2979         ot->exec = sequencer_strip_jump_exec;
2980         ot->poll = sequencer_strip_jump_poll;
2981
2982         /* flags */
2983         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2984         
2985         /* properties */
2986         RNA_def_boolean(ot->srna, "next", true, "Next Strip", "");
2987         RNA_def_boolean(ot->srna, "center", true, "Use strip center", "");
2988 }
2989
2990 static void swap_sequence(Scene *scene, Sequence *seqa, Sequence *seqb)
2991 {
2992         int gap = seqb->startdisp - seqa->enddisp;
2993         int seq_a_start;
2994         int seq_b_start;
2995
2996         seq_b_start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2997         BKE_sequence_translate(scene, seqb, seq_b_start - seqb->start);
2998         BKE_sequence_calc(scene, seqb);
2999
3000         seq_a_start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
3001         BKE_sequence_translate(scene, seqa, seq_a_start - seqa->start);
3002         BKE_sequence_calc(scene, seqa);
3003 }
3004
3005 #if 0
3006 static Sequence *sequence_find_parent(Scene *scene, Sequence *child)
3007 {
3008         Editing *ed = BKE_sequencer_editing_get(scene, false);
3009         Sequence *parent = NULL;
3010         Sequence *seq;
3011
3012         if (ed == NULL) return NULL;
3013
3014         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
3015                 if ((seq != child) && seq_is_parent(seq, child)) {
3016                         parent = seq;
3017                         break;
3018                 }
3019         }
3020
3021         return parent;
3022 }
3023 #endif
3024
3025 static int sequencer_swap_exec(bContext *C, wmOperator *op)
3026 {
3027         Scene *scene = CTX_data_scene(C);
3028         Editing *ed = BKE_sequencer_editing_get(scene, false);
3029         Sequence *active_seq = BKE_sequencer_active_get(scene);
3030         Sequence *seq, *iseq;
3031         int side = RNA_enum_get(op->ptr, "side");
3032
3033         if (active_seq == NULL) return OPERATOR_CANCELLED;
3034
3035         seq = find_next_prev_sequence(scene, active_seq, side, -1);
3036         
3037         if (seq) {
3038                 
3039                 /* disallow effect strips */
3040                 if (BKE_sequence_effect_get_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
3041                         return OPERATOR_CANCELLED;
3042                 if ((BKE_sequence_effect_get_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
3043                         return OPERATOR_CANCELLED;
3044
3045                 switch (side) {
3046                         case SEQ_SIDE_LEFT: 
3047                                 swap_sequence(scene, seq, active_seq);
3048                                 break;
3049                         case SEQ_SIDE_RIGHT: 
3050                                 swap_sequence(scene, active_seq, seq);
3051                                 break;
3052                 }
3053
3054                 // XXX - should be a generic function
3055                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
3056                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
3057                                 BKE_sequence_calc(scene, iseq);
3058                         }
3059                 }
3060
3061                 /* do this in a new loop since both effects need to be calculated first */
3062                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
3063                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
3064                                 /* this may now overlap */
3065                                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq) ) {
3066                                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
3067                                 }
3068                         }
3069                 }
3070
3071
3072
3073                 BKE_sequencer_sort(scene);
3074
3075                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3076
3077                 return OPERATOR_FINISHED;
3078         }
3079
3080         return OPERATOR_CANCELLED;
3081 }
3082
3083 void SEQUENCER_OT_swap(wmOperatorType *ot)
3084 {
3085         /* identifiers */
3086         ot->name = "Swap Strip";
3087         ot->idname = "SEQUENCER_OT_swap";
3088         ot->description = "Swap active strip with strip to the right or left";
3089         
3090         /* api callbacks */
3091         ot->exec =&n