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