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