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